src/HOL/Integ/IntDiv.thy
 author paulson Tue Jun 28 15:27:45 2005 +0200 (2005-06-28) changeset 16587 b34c8aa657a5 parent 16417 9bc16273c2d4 child 16733 236dfafbeb63 permissions -rw-r--r--
Constant "If" is now local
 paulson@6917  1 (* Title: HOL/IntDiv.thy  paulson@6917  2  ID: $Id$  paulson@6917  3  Author: Lawrence C Paulson, Cambridge University Computer Laboratory  paulson@6917  4  Copyright 1999 University of Cambridge  paulson@6917  5 paulson@15221  6 *)  paulson@15221  7 paulson@15221  8 paulson@15221  9 header{*The Division Operators div and mod; the Divides Relation dvd*}  paulson@15221  10 paulson@15221  11 theory IntDiv  paulson@15221  12 imports IntArith Recdef  haftmann@16417  13 uses ("IntDiv_setup.ML")  paulson@15221  14 begin  paulson@15221  15 paulson@15221  16 declare zless_nat_conj [simp]  paulson@15221  17 paulson@15221  18 constdefs  paulson@15221  19  quorem :: "(int*int) * (int*int) => bool"  paulson@15221  20  --{*definition of quotient and remainder*}  paulson@15221  21  "quorem == %((a,b), (q,r)).  paulson@15221  22  a = b*q + r &  paulson@15221  23  (if 0 < b then 0\r & r 0)"  paulson@15221  24 paulson@15221  25  adjust :: "[int, int*int] => int*int"  paulson@15221  26  --{*for the division algorithm*}  paulson@15221  27  "adjust b == %(q,r). if 0 \ r-b then (2*q + 1, r-b)  paulson@15221  28  else (2*q, r)"  paulson@15221  29 paulson@15221  30 text{*algorithm for the case @{text "a\0, b>0"}*}  paulson@15221  31 consts posDivAlg :: "int*int => int*int"  paulson@15620  32 recdef posDivAlg "measure (%(a,b). nat(a - b + 1))"  paulson@15221  33  "posDivAlg (a,b) =  paulson@15221  34  (if (a0) then (0,a)  paulson@15221  35  else adjust b (posDivAlg(a, 2*b)))"  paulson@13183  36 paulson@15221  37 text{*algorithm for the case @{text "a<0, b>0"}*}  paulson@15221  38 consts negDivAlg :: "int*int => int*int"  paulson@15620  39 recdef negDivAlg "measure (%(a,b). nat(- a - b))"  paulson@15221  40  "negDivAlg (a,b) =  paulson@15221  41  (if (0\a+b | b\0) then (-1,a+b)  paulson@15221  42  else adjust b (negDivAlg(a, 2*b)))"  paulson@15221  43 paulson@15221  44 text{*algorithm for the general case @{term "b\0"}*}  paulson@15221  45 constdefs  paulson@15221  46  negateSnd :: "int*int => int*int"  paulson@15221  47  "negateSnd == %(q,r). (q,-r)"  paulson@15221  48 paulson@15221  49  divAlg :: "int*int => int*int"  paulson@15221  50  --{*The full division algorithm considers all possible signs for a, b  paulson@15221  51  including the special case @{text "a=0, b<0"} because  paulson@15221  52  @{term negDivAlg} requires @{term "a<0"}.*}  paulson@15221  53  "divAlg ==  paulson@15221  54  %(a,b). if 0\a then  paulson@15221  55  if 0\b then posDivAlg (a,b)  paulson@15221  56  else if a=0 then (0,0)  paulson@15221  57  else negateSnd (negDivAlg (-a,-b))  paulson@15221  58  else  paulson@15221  59  if 0r-b then (2*q+1, r-b) else (2*q, r)  paulson@13183  80  end  paulson@13183  81 paulson@13183  82  fun negDivAlg (a,b) =  paulson@14288  83  if 0\a+b then (~1,a+b)  paulson@13183  84  else let val (q,r) = negDivAlg(a, 2*b)  paulson@14288  85  in if 0\r-b then (2*q+1, r-b) else (2*q, r)  paulson@13183  86  end;  paulson@13183  87 paulson@13183  88  fun negateSnd (q,r:int) = (q,~r);  paulson@13183  89 paulson@14288  90  fun divAlg (a,b) = if 0\a then  paulson@13183  91  if b>0 then posDivAlg (a,b)  paulson@13183  92  else if a=0 then (0,0)  paulson@13183  93  else negateSnd (negDivAlg (~a,~b))  paulson@13183  94  else  paulson@13183  95  if 0 b*q + r; 0 \ r'; 0 < b; r < b |]  paulson@14288  106  ==> q' \ (q::int)"  paulson@14288  107 apply (subgoal_tac "r' + b * (q'-q) \ r")  paulson@14479  108  prefer 2 apply (simp add: right_diff_distrib)  paulson@13183  109 apply (subgoal_tac "0 < b * (1 + q - q') ")  paulson@13183  110 apply (erule_tac [2] order_le_less_trans)  paulson@14479  111  prefer 2 apply (simp add: right_diff_distrib right_distrib)  paulson@13183  112 apply (subgoal_tac "b * q' < b * (1 + q) ")  paulson@14479  113  prefer 2 apply (simp add: right_diff_distrib right_distrib)  paulson@14387  114 apply (simp add: mult_less_cancel_left)  paulson@13183  115 done  paulson@13183  116 paulson@13183  117 lemma unique_quotient_lemma_neg:  paulson@14288  118  "[| b*q' + r' \ b*q + r; r \ 0; b < 0; b < r' |]  paulson@14288  119  ==> q \ (q'::int)"  paulson@13183  120 by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma,  paulson@13183  121  auto)  paulson@13183  122 paulson@13183  123 lemma unique_quotient:  paulson@15221  124  "[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); b \ 0 |]  paulson@13183  125  ==> q = q'"  paulson@13183  126 apply (simp add: quorem_def linorder_neq_iff split: split_if_asm)  paulson@13183  127 apply (blast intro: order_antisym  paulson@13183  128  dest: order_eq_refl [THEN unique_quotient_lemma]  paulson@13183  129  order_eq_refl [THEN unique_quotient_lemma_neg] sym)+  paulson@13183  130 done  paulson@13183  131 paulson@13183  132 paulson@13183  133 lemma unique_remainder:  paulson@15221  134  "[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); b \ 0 |]  paulson@13183  135  ==> r = r'"  paulson@13183  136 apply (subgoal_tac "q = q'")  paulson@13183  137  apply (simp add: quorem_def)  paulson@13183  138 apply (blast intro: unique_quotient)  paulson@13183  139 done  paulson@13183  140 paulson@13183  141 paulson@15221  142 subsection{*Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends*}  paulson@14271  143 paulson@14271  144 text{*And positive divisors*}  paulson@13183  145 paulson@13183  146 lemma adjust_eq [simp]:  paulson@13183  147  "adjust b (q,r) =  paulson@13183  148  (let diff = r-b in  paulson@14288  149  if 0 \ diff then (2*q + 1, diff)  paulson@13183  150  else (2*q, r))"  paulson@13183  151 by (simp add: Let_def adjust_def)  paulson@13183  152 paulson@13183  153 declare posDivAlg.simps [simp del]  paulson@13183  154 paulson@15221  155 text{*use with a simproc to avoid repeatedly proving the premise*}  paulson@13183  156 lemma posDivAlg_eqn:  paulson@13183  157  "0 < b ==>  paulson@13183  158  posDivAlg (a,b) = (if a a --> 0 < b --> quorem ((a, b), posDivAlg (a, b))"  paulson@13183  164 apply (induct_tac a b rule: posDivAlg.induct, auto)  paulson@13183  165  apply (simp_all add: quorem_def)  paulson@13183  166  (*base case: a  paulson@13183  184  negDivAlg (a,b) =  paulson@14288  185  (if 0\a+b then (-1,a+b) else adjust b (negDivAlg(a, 2*b)))"  paulson@13183  186 by (rule negDivAlg.simps [THEN trans], simp)  paulson@13183  187 paulson@13183  188 (*Correctness of negDivAlg: it computes quotients correctly  paulson@13183  189  It doesn't work if a=0 because the 0/b equals 0, not -1*)  paulson@13183  190 lemma negDivAlg_correct [rule_format]:  paulson@13183  191  "a < 0 --> 0 < b --> quorem ((a, b), negDivAlg (a, b))"  paulson@13183  192 apply (induct_tac a b rule: negDivAlg.induct, auto)  paulson@13183  193  apply (simp_all add: quorem_def)  paulson@14288  194  (*base case: 0\a+b*)  paulson@13183  195  apply (simp add: negDivAlg_eqn)  paulson@13183  196 (*main argument*)  paulson@13183  197 apply (subst negDivAlg_eqn, assumption)  paulson@13183  198 apply (erule splitE)  paulson@14479  199 apply (auto simp add: right_distrib Let_def)  paulson@13183  200 done  paulson@13183  201 paulson@13183  202 paulson@14271  203 subsection{*Existence Shown by Proving the Division Algorithm to be Correct*}  paulson@13183  204 paulson@13183  205 (*the case a=0*)  paulson@15221  206 lemma quorem_0: "b \ 0 ==> quorem ((0,b), (0,0))"  paulson@13183  207 by (auto simp add: quorem_def linorder_neq_iff)  paulson@13183  208 paulson@13183  209 lemma posDivAlg_0 [simp]: "posDivAlg (0, b) = (0, 0)"  paulson@13183  210 by (subst posDivAlg.simps, auto)  paulson@13183  211 paulson@13183  212 lemma negDivAlg_minus1 [simp]: "negDivAlg (-1, b) = (-1, b - 1)"  paulson@13183  213 by (subst negDivAlg.simps, auto)  paulson@13183  214 paulson@13183  215 lemma negateSnd_eq [simp]: "negateSnd(q,r) = (q,-r)"  paulson@15221  216 by (simp add: negateSnd_def)  paulson@13183  217 paulson@13183  218 lemma quorem_neg: "quorem ((-a,-b), qr) ==> quorem ((a,b), negateSnd qr)"  paulson@13183  219 by (auto simp add: split_ifs quorem_def)  paulson@13183  220 paulson@15221  221 lemma divAlg_correct: "b \ 0 ==> quorem ((a,b), divAlg(a,b))"  paulson@13183  222 by (force simp add: linorder_neq_iff quorem_0 divAlg_def quorem_neg  paulson@13183  223  posDivAlg_correct negDivAlg_correct)  paulson@13183  224 paulson@15221  225 text{*Arbitrary definitions for division by zero. Useful to simplify  paulson@15221  226  certain equations.*}  paulson@13183  227 paulson@14271  228 lemma DIVISION_BY_ZERO [simp]: "a div (0::int) = 0 & a mod (0::int) = a"  paulson@14271  229 by (simp add: div_def mod_def divAlg_def posDivAlg.simps)  paulson@13183  230 paulson@15221  231 paulson@15221  232 text{*Basic laws about division and remainder*}  paulson@13183  233 paulson@13183  234 lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"  paulson@15013  235 apply (case_tac "b = 0", simp)  paulson@13183  236 apply (cut_tac a = a and b = b in divAlg_correct)  paulson@13183  237 apply (auto simp add: quorem_def div_def mod_def)  paulson@13183  238 done  paulson@13183  239 nipkow@13517  240 lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"  nipkow@13517  241 by(simp add: zmod_zdiv_equality[symmetric])  nipkow@13517  242 nipkow@13517  243 lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"  paulson@15234  244 by(simp add: mult_commute zmod_zdiv_equality[symmetric])  nipkow@13517  245 nipkow@13517  246 use "IntDiv_setup.ML"  nipkow@13517  247 paulson@14288  248 lemma pos_mod_conj : "(0::int) < b ==> 0 \ a mod b & a mod b < b"  paulson@13183  249 apply (cut_tac a = a and b = b in divAlg_correct)  paulson@13183  250 apply (auto simp add: quorem_def mod_def)  paulson@13183  251 done  paulson@13183  252 nipkow@13788  253 lemmas pos_mod_sign[simp] = pos_mod_conj [THEN conjunct1, standard]  nipkow@13788  254  and pos_mod_bound[simp] = pos_mod_conj [THEN conjunct2, standard]  paulson@13183  255 paulson@14288  256 lemma neg_mod_conj : "b < (0::int) ==> a mod b \ 0 & b < a mod b"  paulson@13183  257 apply (cut_tac a = a and b = b in divAlg_correct)  paulson@13183  258 apply (auto simp add: quorem_def div_def mod_def)  paulson@13183  259 done  paulson@13183  260 nipkow@13788  261 lemmas neg_mod_sign[simp] = neg_mod_conj [THEN conjunct1, standard]  nipkow@13788  262  and neg_mod_bound[simp] = neg_mod_conj [THEN conjunct2, standard]  paulson@13183  263 paulson@13183  264 paulson@13260  265 paulson@15221  266 subsection{*General Properties of div and mod*}  paulson@13183  267 paulson@15221  268 lemma quorem_div_mod: "b \ 0 ==> quorem ((a, b), (a div b, a mod b))"  paulson@13183  269 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  nipkow@13788  270 apply (force simp add: quorem_def linorder_neq_iff)  paulson@13183  271 done  paulson@13183  272 paulson@15221  273 lemma quorem_div: "[| quorem((a,b),(q,r)); b \ 0 |] ==> a div b = q"  paulson@13183  274 by (simp add: quorem_div_mod [THEN unique_quotient])  paulson@13183  275 paulson@15221  276 lemma quorem_mod: "[| quorem((a,b),(q,r)); b \ 0 |] ==> a mod b = r"  paulson@13183  277 by (simp add: quorem_div_mod [THEN unique_remainder])  paulson@13183  278 paulson@14288  279 lemma div_pos_pos_trivial: "[| (0::int) \ a; a < b |] ==> a div b = 0"  paulson@13183  280 apply (rule quorem_div)  paulson@13183  281 apply (auto simp add: quorem_def)  paulson@13183  282 done  paulson@13183  283 paulson@14288  284 lemma div_neg_neg_trivial: "[| a \ (0::int); b < a |] ==> a div b = 0"  paulson@13183  285 apply (rule quorem_div)  paulson@13183  286 apply (auto simp add: quorem_def)  paulson@13183  287 done  paulson@13183  288 paulson@14288  289 lemma div_pos_neg_trivial: "[| (0::int) < a; a+b \ 0 |] ==> a div b = -1"  paulson@13183  290 apply (rule quorem_div)  paulson@13183  291 apply (auto simp add: quorem_def)  paulson@13183  292 done  paulson@13183  293 paulson@13183  294 (*There is no div_neg_pos_trivial because 0 div b = 0 would supersede it*)  paulson@13183  295 paulson@14288  296 lemma mod_pos_pos_trivial: "[| (0::int) \ a; a < b |] ==> a mod b = a"  paulson@13183  297 apply (rule_tac q = 0 in quorem_mod)  paulson@13183  298 apply (auto simp add: quorem_def)  paulson@13183  299 done  paulson@13183  300 paulson@14288  301 lemma mod_neg_neg_trivial: "[| a \ (0::int); b < a |] ==> a mod b = a"  paulson@13183  302 apply (rule_tac q = 0 in quorem_mod)  paulson@13183  303 apply (auto simp add: quorem_def)  paulson@13183  304 done  paulson@13183  305 paulson@14288  306 lemma mod_pos_neg_trivial: "[| (0::int) < a; a+b \ 0 |] ==> a mod b = a+b"  paulson@13183  307 apply (rule_tac q = "-1" in quorem_mod)  paulson@13183  308 apply (auto simp add: quorem_def)  paulson@13183  309 done  paulson@13183  310 paulson@15221  311 text{*There is no @{text mod_neg_pos_trivial}.*}  paulson@13183  312 paulson@13183  313 paulson@13183  314 (*Simpler laws such as -a div b = -(a div b) FAIL, but see just below*)  paulson@13183  315 lemma zdiv_zminus_zminus [simp]: "(-a) div (-b) = a div (b::int)"  paulson@15013  316 apply (case_tac "b = 0", simp)  paulson@13183  317 apply (simp add: quorem_div_mod [THEN quorem_neg, simplified,  paulson@13183  318  THEN quorem_div, THEN sym])  paulson@13183  319 paulson@13183  320 done  paulson@13183  321 paulson@13183  322 (*Simpler laws such as -a mod b = -(a mod b) FAIL, but see just below*)  paulson@13183  323 lemma zmod_zminus_zminus [simp]: "(-a) mod (-b) = - (a mod (b::int))"  paulson@15013  324 apply (case_tac "b = 0", simp)  paulson@13183  325 apply (subst quorem_div_mod [THEN quorem_neg, simplified, THEN quorem_mod],  paulson@13183  326  auto)  paulson@13183  327 done  paulson@13183  328 paulson@15221  329 paulson@15221  330 subsection{*Laws for div and mod with Unary Minus*}  paulson@13183  331 paulson@13183  332 lemma zminus1_lemma:  paulson@13183  333  "quorem((a,b),(q,r))  paulson@13183  334  ==> quorem ((-a,b), (if r=0 then -q else -q - 1),  paulson@13183  335  (if r=0 then 0 else b-r))"  paulson@14479  336 by (force simp add: split_ifs quorem_def linorder_neq_iff right_diff_distrib)  paulson@13183  337 paulson@13183  338 paulson@13183  339 lemma zdiv_zminus1_eq_if:  paulson@15221  340  "b \ (0::int)  paulson@13183  341  ==> (-a) div b =  paulson@13183  342  (if a mod b = 0 then - (a div b) else - (a div b) - 1)"  paulson@13183  343 by (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_div])  paulson@13183  344 paulson@13183  345 lemma zmod_zminus1_eq_if:  paulson@13183  346  "(-a::int) mod b = (if a mod b = 0 then 0 else b - (a mod b))"  paulson@15013  347 apply (case_tac "b = 0", simp)  paulson@13183  348 apply (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_mod])  paulson@13183  349 done  paulson@13183  350 paulson@13183  351 lemma zdiv_zminus2: "a div (-b) = (-a::int) div b"  paulson@13183  352 by (cut_tac a = "-a" in zdiv_zminus_zminus, auto)  paulson@13183  353 paulson@13183  354 lemma zmod_zminus2: "a mod (-b) = - ((-a::int) mod b)"  paulson@13183  355 by (cut_tac a = "-a" and b = b in zmod_zminus_zminus, auto)  paulson@13183  356 paulson@13183  357 lemma zdiv_zminus2_eq_if:  paulson@15221  358  "b \ (0::int)  paulson@13183  359  ==> a div (-b) =  paulson@13183  360  (if a mod b = 0 then - (a div b) else - (a div b) - 1)"  paulson@13183  361 by (simp add: zdiv_zminus1_eq_if zdiv_zminus2)  paulson@13183  362 paulson@13183  363 lemma zmod_zminus2_eq_if:  paulson@13183  364  "a mod (-b::int) = (if a mod b = 0 then 0 else (a mod b) - b)"  paulson@13183  365 by (simp add: zmod_zminus1_eq_if zmod_zminus2)  paulson@13183  366 paulson@13183  367 paulson@14271  368 subsection{*Division of a Number by Itself*}  paulson@13183  369 paulson@14288  370 lemma self_quotient_aux1: "[| (0::int) < a; a = r + a*q; r < a |] ==> 1 \ q"  paulson@13183  371 apply (subgoal_tac "0 < a*q")  paulson@14353  372  apply (simp add: zero_less_mult_iff, arith)  paulson@13183  373 done  paulson@13183  374 paulson@14288  375 lemma self_quotient_aux2: "[| (0::int) < a; a = r + a*q; 0 \ r |] ==> q \ 1"  paulson@14288  376 apply (subgoal_tac "0 \ a* (1-q) ")  paulson@14353  377  apply (simp add: zero_le_mult_iff)  paulson@14479  378 apply (simp add: right_diff_distrib)  paulson@13183  379 done  paulson@13183  380 paulson@15221  381 lemma self_quotient: "[| quorem((a,a),(q,r)); a \ (0::int) |] ==> q = 1"  paulson@13183  382 apply (simp add: split_ifs quorem_def linorder_neq_iff)  paulson@15221  383 apply (rule order_antisym, safe, simp_all)  wenzelm@13524  384 apply (rule_tac [3] a = "-a" and r = "-r" in self_quotient_aux1)  wenzelm@13524  385 apply (rule_tac a = "-a" and r = "-r" in self_quotient_aux2)  paulson@15221  386 apply (force intro: self_quotient_aux1 self_quotient_aux2 simp add: add_commute)+  paulson@13183  387 done  paulson@13183  388 paulson@15221  389 lemma self_remainder: "[| quorem((a,a),(q,r)); a \ (0::int) |] ==> r = 0"  paulson@13183  390 apply (frule self_quotient, assumption)  paulson@13183  391 apply (simp add: quorem_def)  paulson@13183  392 done  paulson@13183  393 paulson@15221  394 lemma zdiv_self [simp]: "a \ 0 ==> a div a = (1::int)"  paulson@13183  395 by (simp add: quorem_div_mod [THEN self_quotient])  paulson@13183  396 paulson@13183  397 (*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)  paulson@13183  398 lemma zmod_self [simp]: "a mod a = (0::int)"  paulson@15013  399 apply (case_tac "a = 0", simp)  paulson@13183  400 apply (simp add: quorem_div_mod [THEN self_remainder])  paulson@13183  401 done  paulson@13183  402 paulson@13183  403 paulson@14271  404 subsection{*Computation of Division and Remainder*}  paulson@13183  405 paulson@13183  406 lemma zdiv_zero [simp]: "(0::int) div b = 0"  paulson@13183  407 by (simp add: div_def divAlg_def)  paulson@13183  408 paulson@13183  409 lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"  paulson@13183  410 by (simp add: div_def divAlg_def)  paulson@13183  411 paulson@13183  412 lemma zmod_zero [simp]: "(0::int) mod b = 0"  paulson@13183  413 by (simp add: mod_def divAlg_def)  paulson@13183  414 paulson@13183  415 lemma zdiv_minus1: "(0::int) < b ==> -1 div b = -1"  paulson@13183  416 by (simp add: div_def divAlg_def)  paulson@13183  417 paulson@13183  418 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"  paulson@13183  419 by (simp add: mod_def divAlg_def)  paulson@13183  420 paulson@15221  421 text{*a positive, b positive *}  paulson@13183  422 paulson@14288  423 lemma div_pos_pos: "[| 0 < a; 0 \ b |] ==> a div b = fst (posDivAlg(a,b))"  paulson@13183  424 by (simp add: div_def divAlg_def)  paulson@13183  425 paulson@14288  426 lemma mod_pos_pos: "[| 0 < a; 0 \ b |] ==> a mod b = snd (posDivAlg(a,b))"  paulson@13183  427 by (simp add: mod_def divAlg_def)  paulson@13183  428 paulson@15221  429 text{*a negative, b positive *}  paulson@13183  430 paulson@13183  431 lemma div_neg_pos: "[| a < 0; 0 < b |] ==> a div b = fst (negDivAlg(a,b))"  paulson@13183  432 by (simp add: div_def divAlg_def)  paulson@13183  433 paulson@13183  434 lemma mod_neg_pos: "[| a < 0; 0 < b |] ==> a mod b = snd (negDivAlg(a,b))"  paulson@13183  435 by (simp add: mod_def divAlg_def)  paulson@13183  436 paulson@15221  437 text{*a positive, b negative *}  paulson@13183  438 paulson@13183  439 lemma div_pos_neg:  paulson@13183  440  "[| 0 < a; b < 0 |] ==> a div b = fst (negateSnd(negDivAlg(-a,-b)))"  paulson@13183  441 by (simp add: div_def divAlg_def)  paulson@13183  442 paulson@13183  443 lemma mod_pos_neg:  paulson@13183  444  "[| 0 < a; b < 0 |] ==> a mod b = snd (negateSnd(negDivAlg(-a,-b)))"  paulson@13183  445 by (simp add: mod_def divAlg_def)  paulson@13183  446 paulson@15221  447 text{*a negative, b negative *}  paulson@13183  448 paulson@13183  449 lemma div_neg_neg:  paulson@14288  450  "[| a < 0; b \ 0 |] ==> a div b = fst (negateSnd(posDivAlg(-a,-b)))"  paulson@13183  451 by (simp add: div_def divAlg_def)  paulson@13183  452 paulson@13183  453 lemma mod_neg_neg:  paulson@14288  454  "[| a < 0; b \ 0 |] ==> a mod b = snd (negateSnd(posDivAlg(-a,-b)))"  paulson@13183  455 by (simp add: mod_def divAlg_def)  paulson@13183  456 paulson@13183  457 text {*Simplify expresions in which div and mod combine numerical constants*}  paulson@13183  458 paulson@13183  459 declare div_pos_pos [of "number_of v" "number_of w", standard, simp]  paulson@13183  460 declare div_neg_pos [of "number_of v" "number_of w", standard, simp]  paulson@13183  461 declare div_pos_neg [of "number_of v" "number_of w", standard, simp]  paulson@13183  462 declare div_neg_neg [of "number_of v" "number_of w", standard, simp]  paulson@13183  463 paulson@13183  464 declare mod_pos_pos [of "number_of v" "number_of w", standard, simp]  paulson@13183  465 declare mod_neg_pos [of "number_of v" "number_of w", standard, simp]  paulson@13183  466 declare mod_pos_neg [of "number_of v" "number_of w", standard, simp]  paulson@13183  467 declare mod_neg_neg [of "number_of v" "number_of w", standard, simp]  paulson@13183  468 paulson@13183  469 declare posDivAlg_eqn [of "number_of v" "number_of w", standard, simp]  paulson@13183  470 declare negDivAlg_eqn [of "number_of v" "number_of w", standard, simp]  paulson@13183  471 paulson@13183  472 paulson@15221  473 text{*Special-case simplification *}  paulson@13183  474 paulson@13183  475 lemma zmod_1 [simp]: "a mod (1::int) = 0"  paulson@13183  476 apply (cut_tac a = a and b = 1 in pos_mod_sign)  nipkow@13788  477 apply (cut_tac [2] a = a and b = 1 in pos_mod_bound)  nipkow@13788  478 apply (auto simp del:pos_mod_bound pos_mod_sign)  nipkow@13788  479 done  paulson@13183  480 paulson@13183  481 lemma zdiv_1 [simp]: "a div (1::int) = a"  paulson@13183  482 by (cut_tac a = a and b = 1 in zmod_zdiv_equality, auto)  paulson@13183  483 paulson@13183  484 lemma zmod_minus1_right [simp]: "a mod (-1::int) = 0"  paulson@13183  485 apply (cut_tac a = a and b = "-1" in neg_mod_sign)  nipkow@13788  486 apply (cut_tac [2] a = a and b = "-1" in neg_mod_bound)  nipkow@13788  487 apply (auto simp del: neg_mod_sign neg_mod_bound)  paulson@13183  488 done  paulson@13183  489 paulson@13183  490 lemma zdiv_minus1_right [simp]: "a div (-1::int) = -a"  paulson@13183  491 by (cut_tac a = a and b = "-1" in zmod_zdiv_equality, auto)  paulson@13183  492 paulson@13183  493 (** The last remaining special cases for constant arithmetic:  paulson@13183  494  1 div z and 1 mod z **)  paulson@13183  495 paulson@13183  496 declare div_pos_pos [OF int_0_less_1, of "number_of w", standard, simp]  paulson@13183  497 declare div_pos_neg [OF int_0_less_1, of "number_of w", standard, simp]  paulson@13183  498 declare mod_pos_pos [OF int_0_less_1, of "number_of w", standard, simp]  paulson@13183  499 declare mod_pos_neg [OF int_0_less_1, of "number_of w", standard, simp]  paulson@13183  500 paulson@13183  501 declare posDivAlg_eqn [of concl: 1 "number_of w", standard, simp]  paulson@13183  502 declare negDivAlg_eqn [of concl: 1 "number_of w", standard, simp]  paulson@13183  503 paulson@13183  504 paulson@14271  505 subsection{*Monotonicity in the First Argument (Dividend)*}  paulson@13183  506 paulson@14288  507 lemma zdiv_mono1: "[| a \ a'; 0 < (b::int) |] ==> a div b \ a' div b"  paulson@13183  508 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  paulson@13183  509 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)  paulson@13183  510 apply (rule unique_quotient_lemma)  paulson@13183  511 apply (erule subst)  paulson@15221  512 apply (erule subst, simp_all)  paulson@13183  513 done  paulson@13183  514 paulson@14288  515 lemma zdiv_mono1_neg: "[| a \ a'; (b::int) < 0 |] ==> a' div b \ a div b"  paulson@13183  516 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  paulson@13183  517 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)  paulson@13183  518 apply (rule unique_quotient_lemma_neg)  paulson@13183  519 apply (erule subst)  paulson@15221  520 apply (erule subst, simp_all)  paulson@13183  521 done  paulson@6917  522 paulson@6917  523 paulson@14271  524 subsection{*Monotonicity in the Second Argument (Divisor)*}  paulson@13183  525 paulson@13183  526 lemma q_pos_lemma:  paulson@14288  527  "[| 0 \ b'*q' + r'; r' < b'; 0 < b' |] ==> 0 \ (q'::int)"  paulson@13183  528 apply (subgoal_tac "0 < b'* (q' + 1) ")  paulson@14353  529  apply (simp add: zero_less_mult_iff)  paulson@14479  530 apply (simp add: right_distrib)  paulson@13183  531 done  paulson@13183  532 paulson@13183  533 lemma zdiv_mono2_lemma:  paulson@14288  534  "[| b*q + r = b'*q' + r'; 0 \ b'*q' + r';  paulson@14288  535  r' < b'; 0 \ r; 0 < b'; b' \ b |]  paulson@14288  536  ==> q \ (q'::int)"  paulson@13183  537 apply (frule q_pos_lemma, assumption+)  paulson@13183  538 apply (subgoal_tac "b*q < b* (q' + 1) ")  paulson@14387  539  apply (simp add: mult_less_cancel_left)  paulson@13183  540 apply (subgoal_tac "b*q = r' - r + b'*q'")  paulson@13183  541  prefer 2 apply simp  paulson@14479  542 apply (simp (no_asm_simp) add: right_distrib)  paulson@15221  543 apply (subst add_commute, rule zadd_zless_mono, arith)  paulson@14378  544 apply (rule mult_right_mono, auto)  paulson@13183  545 done  paulson@13183  546 paulson@13183  547 lemma zdiv_mono2:  paulson@14288  548  "[| (0::int) \ a; 0 < b'; b' \ b |] ==> a div b \ a div b'"  paulson@15221  549 apply (subgoal_tac "b \ 0")  paulson@13183  550  prefer 2 apply arith  paulson@13183  551 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  paulson@13183  552 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)  paulson@13183  553 apply (rule zdiv_mono2_lemma)  paulson@13183  554 apply (erule subst)  paulson@15221  555 apply (erule subst, simp_all)  paulson@13183  556 done  paulson@13183  557 paulson@13183  558 lemma q_neg_lemma:  paulson@14288  559  "[| b'*q' + r' < 0; 0 \ r'; 0 < b' |] ==> q' \ (0::int)"  paulson@13183  560 apply (subgoal_tac "b'*q' < 0")  paulson@14353  561  apply (simp add: mult_less_0_iff, arith)  paulson@13183  562 done  paulson@13183  563 paulson@13183  564 lemma zdiv_mono2_neg_lemma:  paulson@13183  565  "[| b*q + r = b'*q' + r'; b'*q' + r' < 0;  paulson@14288  566  r < b; 0 \ r'; 0 < b'; b' \ b |]  paulson@14288  567  ==> q' \ (q::int)"  paulson@13183  568 apply (frule q_neg_lemma, assumption+)  paulson@13183  569 apply (subgoal_tac "b*q' < b* (q + 1) ")  paulson@14387  570  apply (simp add: mult_less_cancel_left)  paulson@14479  571 apply (simp add: right_distrib)  paulson@14288  572 apply (subgoal_tac "b*q' \ b'*q'")  paulson@15221  573  prefer 2 apply (simp add: mult_right_mono_neg, arith)  paulson@13183  574 done  paulson@13183  575 paulson@13183  576 lemma zdiv_mono2_neg:  paulson@14288  577  "[| a < (0::int); 0 < b'; b' \ b |] ==> a div b' \ a div b"  paulson@13183  578 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  paulson@13183  579 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)  paulson@13183  580 apply (rule zdiv_mono2_neg_lemma)  paulson@13183  581 apply (erule subst)  paulson@15221  582 apply (erule subst, simp_all)  paulson@13183  583 done  paulson@13183  584 paulson@14271  585 subsection{*More Algebraic Laws for div and mod*}  paulson@13183  586 paulson@15221  587 text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}  paulson@13183  588 paulson@13183  589 lemma zmult1_lemma:  paulson@15221  590  "[| quorem((b,c),(q,r)); c \ 0 |]  paulson@13183  591  ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"  paulson@14479  592 by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)  paulson@13183  593 paulson@13183  594 lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"  paulson@15013  595 apply (case_tac "c = 0", simp)  paulson@13183  596 apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_div])  paulson@13183  597 done  paulson@13183  598 paulson@13183  599 lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"  paulson@15013  600 apply (case_tac "c = 0", simp)  paulson@13183  601 apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_mod])  paulson@13183  602 done  paulson@13183  603 paulson@13183  604 lemma zmod_zmult1_eq': "(a*b) mod (c::int) = ((a mod c) * b) mod c"  paulson@13183  605 apply (rule trans)  paulson@13183  606 apply (rule_tac s = "b*a mod c" in trans)  paulson@13183  607 apply (rule_tac [2] zmod_zmult1_eq)  paulson@15234  608 apply (simp_all add: mult_commute)  paulson@13183  609 done  paulson@13183  610 paulson@13183  611 lemma zmod_zmult_distrib: "(a*b) mod (c::int) = ((a mod c) * (b mod c)) mod c"  paulson@13183  612 apply (rule zmod_zmult1_eq' [THEN trans])  paulson@13183  613 apply (rule zmod_zmult1_eq)  paulson@13183  614 done  paulson@13183  615 paulson@15221  616 lemma zdiv_zmult_self1 [simp]: "b \ (0::int) ==> (a*b) div b = a"  paulson@13183  617 by (simp add: zdiv_zmult1_eq)  paulson@13183  618 paulson@15221  619 lemma zdiv_zmult_self2 [simp]: "b \ (0::int) ==> (b*a) div b = a"  paulson@15234  620 by (subst mult_commute, erule zdiv_zmult_self1)  paulson@13183  621 paulson@13183  622 lemma zmod_zmult_self1 [simp]: "(a*b) mod b = (0::int)"  paulson@13183  623 by (simp add: zmod_zmult1_eq)  paulson@13183  624 paulson@13183  625 lemma zmod_zmult_self2 [simp]: "(b*a) mod b = (0::int)"  paulson@15234  626 by (simp add: mult_commute zmod_zmult1_eq)  paulson@13183  627 paulson@13183  628 lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"  nipkow@13517  629 proof  nipkow@13517  630  assume "m mod d = 0"  paulson@14473  631  with zmod_zdiv_equality[of m d] show "EX q::int. m = d*q" by auto  nipkow@13517  632 next  nipkow@13517  633  assume "EX q::int. m = d*q"  nipkow@13517  634  thus "m mod d = 0" by auto  nipkow@13517  635 qed  paulson@13183  636 paulson@13183  637 declare zmod_eq_0_iff [THEN iffD1, dest!]  paulson@13183  638 paulson@15221  639 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}  paulson@13183  640 paulson@13183  641 lemma zadd1_lemma:  paulson@15221  642  "[| quorem((a,c),(aq,ar)); quorem((b,c),(bq,br)); c \ 0 |]  paulson@13183  643  ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"  paulson@14479  644 by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)  paulson@13183  645 paulson@13183  646 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)  paulson@13183  647 lemma zdiv_zadd1_eq:  paulson@13183  648  "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"  paulson@15013  649 apply (case_tac "c = 0", simp)  paulson@13183  650 apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_div)  paulson@13183  651 done  paulson@13183  652 paulson@13183  653 lemma zmod_zadd1_eq: "(a+b) mod (c::int) = (a mod c + b mod c) mod c"  paulson@15013  654 apply (case_tac "c = 0", simp)  paulson@13183  655 apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_mod)  paulson@13183  656 done  paulson@13183  657 paulson@13183  658 lemma mod_div_trivial [simp]: "(a mod b) div b = (0::int)"  paulson@15013  659 apply (case_tac "b = 0", simp)  nipkow@13788  660 apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial)  paulson@13183  661 done  paulson@13183  662 paulson@13183  663 lemma mod_mod_trivial [simp]: "(a mod b) mod b = a mod (b::int)"  paulson@15013  664 apply (case_tac "b = 0", simp)  nipkow@13788  665 apply (force simp add: linorder_neq_iff mod_pos_pos_trivial mod_neg_neg_trivial)  paulson@13183  666 done  paulson@13183  667 paulson@13183  668 lemma zmod_zadd_left_eq: "(a+b) mod (c::int) = ((a mod c) + b) mod c"  paulson@13183  669 apply (rule trans [symmetric])  paulson@13183  670 apply (rule zmod_zadd1_eq, simp)  paulson@13183  671 apply (rule zmod_zadd1_eq [symmetric])  paulson@13183  672 done  paulson@13183  673 paulson@13183  674 lemma zmod_zadd_right_eq: "(a+b) mod (c::int) = (a + (b mod c)) mod c"  paulson@13183  675 apply (rule trans [symmetric])  paulson@13183  676 apply (rule zmod_zadd1_eq, simp)  paulson@13183  677 apply (rule zmod_zadd1_eq [symmetric])  paulson@13183  678 done  paulson@13183  679 paulson@15221  680 lemma zdiv_zadd_self1[simp]: "a \ (0::int) ==> (a+b) div a = b div a + 1"  paulson@13183  681 by (simp add: zdiv_zadd1_eq)  paulson@13183  682 paulson@15221  683 lemma zdiv_zadd_self2[simp]: "a \ (0::int) ==> (b+a) div a = b div a + 1"  paulson@13183  684 by (simp add: zdiv_zadd1_eq)  paulson@13183  685 paulson@13183  686 lemma zmod_zadd_self1[simp]: "(a+b) mod a = b mod (a::int)"  paulson@15013  687 apply (case_tac "a = 0", simp)  paulson@13183  688 apply (simp add: zmod_zadd1_eq)  paulson@13183  689 done  paulson@13183  690 paulson@13183  691 lemma zmod_zadd_self2[simp]: "(b+a) mod a = b mod (a::int)"  paulson@15013  692 apply (case_tac "a = 0", simp)  paulson@13183  693 apply (simp add: zmod_zadd1_eq)  paulson@13183  694 done  paulson@13183  695 paulson@13183  696 paulson@14271  697 subsection{*Proving @{term "a div (b*c) = (a div b) div c"} *}  paulson@13183  698 paulson@13183  699 (*The condition c>0 seems necessary. Consider that 7 div ~6 = ~2 but  paulson@13183  700  7 div 2 div ~3 = 3 div ~3 = ~1. The subcase (a div b) mod c = 0 seems  paulson@13183  701  to cause particular problems.*)  paulson@13183  702 paulson@15221  703 text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}  paulson@13183  704 paulson@14288  705 lemma zmult2_lemma_aux1: "[| (0::int) < c; b < r; r \ 0 |] ==> b*c < b*(q mod c) + r"  paulson@13183  706 apply (subgoal_tac "b * (c - q mod c) < r * 1")  paulson@14479  707 apply (simp add: right_diff_distrib)  paulson@13183  708 apply (rule order_le_less_trans)  paulson@14378  709 apply (erule_tac [2] mult_strict_right_mono)  paulson@14378  710 apply (rule mult_left_mono_neg)  paulson@15221  711 apply (auto simp add: compare_rls add_commute [of 1]  paulson@13183  712  add1_zle_eq pos_mod_bound)  paulson@13183  713 done  paulson@13183  714 paulson@15221  715 lemma zmult2_lemma_aux2:  paulson@15221  716  "[| (0::int) < c; b < r; r \ 0 |] ==> b * (q mod c) + r \ 0"  paulson@14288  717 apply (subgoal_tac "b * (q mod c) \ 0")  paulson@13183  718  apply arith  paulson@14353  719 apply (simp add: mult_le_0_iff)  paulson@13183  720 done  paulson@13183  721 paulson@14288  722 lemma zmult2_lemma_aux3: "[| (0::int) < c; 0 \ r; r < b |] ==> 0 \ b * (q mod c) + r"  paulson@14288  723 apply (subgoal_tac "0 \ b * (q mod c) ")  paulson@13183  724 apply arith  paulson@14353  725 apply (simp add: zero_le_mult_iff)  paulson@13183  726 done  paulson@13183  727 paulson@14288  728 lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \ r; r < b |] ==> b * (q mod c) + r < b * c"  paulson@13183  729 apply (subgoal_tac "r * 1 < b * (c - q mod c) ")  paulson@14479  730 apply (simp add: right_diff_distrib)  paulson@13183  731 apply (rule order_less_le_trans)  paulson@14378  732 apply (erule mult_strict_right_mono)  paulson@14387  733 apply (rule_tac [2] mult_left_mono)  paulson@15221  734 apply (auto simp add: compare_rls add_commute [of 1]  paulson@13183  735  add1_zle_eq pos_mod_bound)  paulson@13183  736 done  paulson@13183  737 paulson@15221  738 lemma zmult2_lemma: "[| quorem ((a,b), (q,r)); b \ 0; 0 < c |]  paulson@13183  739  ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"  paulson@14271  740 by (auto simp add: mult_ac quorem_def linorder_neq_iff  paulson@14479  741  zero_less_mult_iff right_distrib [symmetric]  wenzelm@13524  742  zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4)  paulson@13183  743 paulson@13183  744 lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"  paulson@15013  745 apply (case_tac "b = 0", simp)  paulson@13183  746 apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_div])  paulson@13183  747 done  paulson@13183  748 paulson@13183  749 lemma zmod_zmult2_eq:  paulson@13183  750  "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"  paulson@15013  751 apply (case_tac "b = 0", simp)  paulson@13183  752 apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_mod])  paulson@13183  753 done  paulson@13183  754 paulson@13183  755 paulson@14271  756 subsection{*Cancellation of Common Factors in div*}  paulson@13183  757 paulson@15221  758 lemma zdiv_zmult_zmult1_aux1:  paulson@15221  759  "[| (0::int) < b; c \ 0 |] ==> (c*a) div (c*b) = a div b"  paulson@13183  760 by (subst zdiv_zmult2_eq, auto)  paulson@13183  761 paulson@15221  762 lemma zdiv_zmult_zmult1_aux2:  paulson@15221  763  "[| b < (0::int); c \ 0 |] ==> (c*a) div (c*b) = a div b"  paulson@13183  764 apply (subgoal_tac " (c * (-a)) div (c * (-b)) = (-a) div (-b) ")  wenzelm@13524  765 apply (rule_tac [2] zdiv_zmult_zmult1_aux1, auto)  paulson@13183  766 done  paulson@13183  767 paulson@15221  768 lemma zdiv_zmult_zmult1: "c \ (0::int) ==> (c*a) div (c*b) = a div b"  paulson@15013  769 apply (case_tac "b = 0", simp)  wenzelm@13524  770 apply (auto simp add: linorder_neq_iff zdiv_zmult_zmult1_aux1 zdiv_zmult_zmult1_aux2)  paulson@13183  771 done  paulson@13183  772 paulson@15221  773 lemma zdiv_zmult_zmult2: "c \ (0::int) ==> (a*c) div (b*c) = a div b"  paulson@13183  774 apply (drule zdiv_zmult_zmult1)  paulson@15234  775 apply (auto simp add: mult_commute)  paulson@13183  776 done  paulson@13183  777 paulson@13183  778 paulson@13183  779 paulson@14271  780 subsection{*Distribution of Factors over mod*}  paulson@13183  781 paulson@15221  782 lemma zmod_zmult_zmult1_aux1:  paulson@15221  783  "[| (0::int) < b; c \ 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"  paulson@13183  784 by (subst zmod_zmult2_eq, auto)  paulson@13183  785 paulson@15221  786 lemma zmod_zmult_zmult1_aux2:  paulson@15221  787  "[| b < (0::int); c \ 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"  paulson@13183  788 apply (subgoal_tac " (c * (-a)) mod (c * (-b)) = c * ((-a) mod (-b))")  wenzelm@13524  789 apply (rule_tac [2] zmod_zmult_zmult1_aux1, auto)  paulson@13183  790 done  paulson@13183  791 paulson@13183  792 lemma zmod_zmult_zmult1: "(c*a) mod (c*b) = (c::int) * (a mod b)"  paulson@15013  793 apply (case_tac "b = 0", simp)  paulson@15013  794 apply (case_tac "c = 0", simp)  wenzelm@13524  795 apply (auto simp add: linorder_neq_iff zmod_zmult_zmult1_aux1 zmod_zmult_zmult1_aux2)  paulson@13183  796 done  paulson@13183  797 paulson@13183  798 lemma zmod_zmult_zmult2: "(a*c) mod (b*c) = (a mod b) * (c::int)"  paulson@13183  799 apply (cut_tac c = c in zmod_zmult_zmult1)  paulson@15234  800 apply (auto simp add: mult_commute)  paulson@13183  801 done  paulson@13183  802 paulson@13183  803 paulson@14271  804 subsection {*Splitting Rules for div and mod*}  paulson@13260  805 paulson@13260  806 text{*The proofs of the two lemmas below are essentially identical*}  paulson@13260  807 paulson@13260  808 lemma split_pos_lemma:  paulson@13260  809  "0  paulson@14288  810  P(n div k :: int)(n mod k) = (\i j. 0\j & j P i j)"  paulson@15221  811 apply (rule iffI, clarify)  paulson@13260  812  apply (erule_tac P="P ?x ?y" in rev_mp)  paulson@13260  813  apply (subst zmod_zadd1_eq)  paulson@13260  814  apply (subst zdiv_zadd1_eq)  paulson@13260  815  apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  paulson@13260  816 txt{*converse direction*}  paulson@13260  817 apply (drule_tac x = "n div k" in spec)  paulson@15221  818 apply (drule_tac x = "n mod k" in spec, simp)  paulson@13260  819 done  paulson@13260  820 paulson@13260  821 lemma split_neg_lemma:  paulson@13260  822  "k<0 ==>  paulson@14288  823  P(n div k :: int)(n mod k) = (\i j. k0 & n = k*i + j --> P i j)"  paulson@15221  824 apply (rule iffI, clarify)  paulson@13260  825  apply (erule_tac P="P ?x ?y" in rev_mp)  paulson@13260  826  apply (subst zmod_zadd1_eq)  paulson@13260  827  apply (subst zdiv_zadd1_eq)  paulson@13260  828  apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  paulson@13260  829 txt{*converse direction*}  paulson@13260  830 apply (drule_tac x = "n div k" in spec)  paulson@15221  831 apply (drule_tac x = "n mod k" in spec, simp)  paulson@13260  832 done  paulson@13260  833 paulson@13260  834 lemma split_zdiv:  paulson@13260  835  "P(n div k :: int) =  paulson@13260  836  ((k = 0 --> P 0) &  paulson@14288  837  (0 (\i j. 0\j & j P i)) &  paulson@14288  838  (k<0 --> (\i j. k0 & n = k*i + j --> P i)))"  paulson@15221  839 apply (case_tac "k=0", simp)  paulson@13260  840 apply (simp only: linorder_neq_iff)  paulson@13260  841 apply (erule disjE)  paulson@13260  842  apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"]  paulson@13260  843  split_neg_lemma [of concl: "%x y. P x"])  paulson@13260  844 done  paulson@13260  845 paulson@13260  846 lemma split_zmod:  paulson@13260  847  "P(n mod k :: int) =  paulson@13260  848  ((k = 0 --> P n) &  paulson@14288  849  (0 (\i j. 0\j & j P j)) &  paulson@14288  850  (k<0 --> (\i j. k0 & n = k*i + j --> P j)))"  paulson@15221  851 apply (case_tac "k=0", simp)  paulson@13260  852 apply (simp only: linorder_neq_iff)  paulson@13260  853 apply (erule disjE)  paulson@13260  854  apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"]  paulson@13260  855  split_neg_lemma [of concl: "%x y. P y"])  paulson@13260  856 done  paulson@13260  857 paulson@13260  858 (* Enable arith to deal with div 2 and mod 2: *)  nipkow@13266  859 declare split_zdiv [of _ _ "number_of k", simplified, standard, arith_split]  nipkow@13266  860 declare split_zmod [of _ _ "number_of k", simplified, standard, arith_split]  paulson@13260  861 paulson@13260  862 paulson@14271  863 subsection{*Speeding up the Division Algorithm with Shifting*}  paulson@13183  864 paulson@15221  865 text{*computing div by shifting *}  paulson@13183  866 paulson@14288  867 lemma pos_zdiv_mult_2: "(0::int) \ a ==> (1 + 2*b) div (2*a) = b div a"  paulson@14288  868 proof cases  paulson@14288  869  assume "a=0"  paulson@14288  870  thus ?thesis by simp  paulson@14288  871 next  paulson@14288  872  assume "a\0" and le_a: "0\a"  paulson@14288  873  hence a_pos: "1 \ a" by arith  paulson@14288  874  hence one_less_a2: "1 < 2*a" by arith  paulson@14288  875  hence le_2a: "2 * (1 + b mod a) \ 2 * a"  paulson@15221  876  by (simp add: mult_le_cancel_left add_commute [of 1] add1_zle_eq)  paulson@14288  877  with a_pos have "0 \ b mod a" by simp  paulson@14288  878  hence le_addm: "0 \ 1 mod (2*a) + 2*(b mod a)"  paulson@14288  879  by (simp add: mod_pos_pos_trivial one_less_a2)  paulson@14288  880  with le_2a  paulson@14288  881  have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"  paulson@14288  882  by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2  paulson@14288  883  right_distrib)  paulson@14288  884  thus ?thesis  paulson@14288  885  by (subst zdiv_zadd1_eq,  paulson@14288  886  simp add: zdiv_zmult_zmult1 zmod_zmult_zmult1 one_less_a2  paulson@14288  887  div_pos_pos_trivial)  paulson@14288  888 qed  paulson@13183  889 paulson@14288  890 lemma neg_zdiv_mult_2: "a \ (0::int) ==> (1 + 2*b) div (2*a) = (b+1) div a"  paulson@13183  891 apply (subgoal_tac " (1 + 2* (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a) ")  paulson@13183  892 apply (rule_tac [2] pos_zdiv_mult_2)  paulson@14479  893 apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)  paulson@13183  894 apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")  paulson@14479  895 apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric],  paulson@13183  896  simp)  paulson@13183  897 done  paulson@13183  898 paulson@13183  899 paulson@13183  900 (*Not clear why this must be proved separately; probably number_of causes  paulson@13183  901  simplification problems*)  paulson@14288  902 lemma not_0_le_lemma: "~ 0 \ x ==> x \ (0::int)"  paulson@13183  903 by auto  paulson@13183  904 paulson@13183  905 lemma zdiv_number_of_BIT[simp]:  paulson@15620  906  "number_of (v BIT b) div number_of (w BIT bit.B0) =  paulson@15620  907  (if b=bit.B0 | (0::int) \ number_of w  paulson@13183  908  then number_of v div (number_of w)  paulson@13183  909  else (number_of v + (1::int)) div (number_of w))"  paulson@15013  910 apply (simp only: number_of_eq Bin_simps UNIV_I split: split_if)  paulson@15620  911 apply (simp add: zdiv_zmult_zmult1 pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac  paulson@15620  912  split: bit.split)  paulson@13183  913 done  paulson@13183  914 paulson@13183  915 paulson@15013  916 subsection{*Computing mod by Shifting (proofs resemble those for div)*}  paulson@13183  917 paulson@13183  918 lemma pos_zmod_mult_2:  paulson@14288  919  "(0::int) \ a ==> (1 + 2*b) mod (2*a) = 1 + 2 * (b mod a)"  paulson@15013  920 apply (case_tac "a = 0", simp)  paulson@13183  921 apply (subgoal_tac "1 < a * 2")  paulson@13183  922  prefer 2 apply arith  paulson@14288  923 apply (subgoal_tac "2* (1 + b mod a) \ 2*a")  paulson@14387  924  apply (rule_tac [2] mult_left_mono)  paulson@15234  925 apply (auto simp add: add_commute [of 1] mult_commute add1_zle_eq  paulson@13183  926  pos_mod_bound)  paulson@13183  927 apply (subst zmod_zadd1_eq)  paulson@13183  928 apply (simp add: zmod_zmult_zmult2 mod_pos_pos_trivial)  paulson@13183  929 apply (rule mod_pos_pos_trivial)  paulson@14288  930 apply (auto simp add: mod_pos_pos_trivial left_distrib)  paulson@15221  931 apply (subgoal_tac "0 \ b mod a", arith, simp)  paulson@13183  932 done  paulson@13183  933 paulson@13183  934 lemma neg_zmod_mult_2:  paulson@14288  935  "a \ (0::int) ==> (1 + 2*b) mod (2*a) = 2 * ((b+1) mod a) - 1"  paulson@13183  936 apply (subgoal_tac "(1 + 2* (-b - 1)) mod (2* (-a)) =  paulson@13183  937  1 + 2* ((-b - 1) mod (-a))")  paulson@13183  938 apply (rule_tac [2] pos_zmod_mult_2)  paulson@14479  939 apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)  paulson@13183  940 apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")  paulson@13183  941  prefer 2 apply simp  paulson@14479  942 apply (simp only: zmod_zminus_zminus diff_minus minus_add_distrib [symmetric])  paulson@13183  943 done  paulson@13183  944 paulson@13183  945 lemma zmod_number_of_BIT [simp]:  paulson@15620  946  "number_of (v BIT b) mod number_of (w BIT bit.B0) =  paulson@15620  947  (case b of  paulson@15620  948  bit.B0 => 2 * (number_of v mod number_of w)  paulson@15620  949  | bit.B1 => if (0::int) \ number_of w  paulson@13183  950  then 2 * (number_of v mod number_of w) + 1  paulson@15620  951  else 2 * ((number_of v + (1::int)) mod number_of w) - 1)"  paulson@15620  952 apply (simp only: number_of_eq Bin_simps UNIV_I split: bit.split)  paulson@15013  953 apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2  paulson@15013  954  not_0_le_lemma neg_zmod_mult_2 add_ac)  paulson@13183  955 done  paulson@13183  956 paulson@13183  957 paulson@15013  958 subsection{*Quotients of Signs*}  paulson@13183  959 paulson@13183  960 lemma div_neg_pos_less0: "[| a < (0::int); 0 < b |] ==> a div b < 0"  paulson@14288  961 apply (subgoal_tac "a div b \ -1", force)  paulson@13183  962 apply (rule order_trans)  paulson@13183  963 apply (rule_tac a' = "-1" in zdiv_mono1)  paulson@13183  964 apply (auto simp add: zdiv_minus1)  paulson@13183  965 done  paulson@13183  966 paulson@14288  967 lemma div_nonneg_neg_le0: "[| (0::int) \ a; b < 0 |] ==> a div b \ 0"  paulson@13183  968 by (drule zdiv_mono1_neg, auto)  paulson@13183  969 paulson@14288  970 lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \ a div b) = (0 \ a)"  paulson@13183  971 apply auto  paulson@13183  972 apply (drule_tac [2] zdiv_mono1)  paulson@13183  973 apply (auto simp add: linorder_neq_iff)  paulson@13183  974 apply (simp (no_asm_use) add: linorder_not_less [symmetric])  paulson@13183  975 apply (blast intro: div_neg_pos_less0)  paulson@13183  976 done  paulson@13183  977 paulson@13183  978 lemma neg_imp_zdiv_nonneg_iff:  paulson@14288  979  "b < (0::int) ==> (0 \ a div b) = (a \ (0::int))"  paulson@13183  980 apply (subst zdiv_zminus_zminus [symmetric])  paulson@13183  981 apply (subst pos_imp_zdiv_nonneg_iff, auto)  paulson@13183  982 done  paulson@13183  983 paulson@14288  984 (*But not (a div b \ 0 iff a\0); consider a=1, b=2 when a div b = 0.*)  paulson@13183  985 lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"  paulson@13183  986 by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)  paulson@13183  987 paulson@14288  988 (*Again the law fails for \: consider a = -1, b = -2 when a div b = 0*)  paulson@13183  989 lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"  paulson@13183  990 by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)  paulson@13183  991 paulson@13837  992 paulson@14271  993 subsection {* The Divides Relation *}  paulson@13837  994 paulson@13837  995 lemma zdvd_iff_zmod_eq_0: "(m dvd n) = (n mod m = (0::int))"  paulson@13837  996 by(simp add:dvd_def zmod_eq_0_iff)  paulson@13837  997 paulson@13837  998 lemma zdvd_0_right [iff]: "(m::int) dvd 0"  paulson@15221  999 by (simp add: dvd_def)  paulson@13837  1000 paulson@13837  1001 lemma zdvd_0_left [iff]: "(0 dvd (m::int)) = (m = 0)"  paulson@15221  1002  by (simp add: dvd_def)  paulson@13837  1003 paulson@13837  1004 lemma zdvd_1_left [iff]: "1 dvd (m::int)"  paulson@15221  1005  by (simp add: dvd_def)  paulson@13837  1006 paulson@13837  1007 lemma zdvd_refl [simp]: "m dvd (m::int)"  paulson@15221  1008 by (auto simp add: dvd_def intro: zmult_1_right [symmetric])  paulson@13837  1009 paulson@13837  1010 lemma zdvd_trans: "m dvd n ==> n dvd k ==> m dvd (k::int)"  paulson@15234  1011 by (auto simp add: dvd_def intro: mult_assoc)  paulson@13837  1012 paulson@13837  1013 lemma zdvd_zminus_iff: "(m dvd -n) = (m dvd (n::int))"  paulson@15221  1014  apply (simp add: dvd_def, auto)  paulson@13837  1015  apply (rule_tac [!] x = "-k" in exI, auto)  paulson@13837  1016  done  paulson@13837  1017 paulson@13837  1018 lemma zdvd_zminus2_iff: "(-m dvd n) = (m dvd (n::int))"  paulson@15221  1019  apply (simp add: dvd_def, auto)  paulson@13837  1020  apply (rule_tac [!] x = "-k" in exI, auto)  paulson@13837  1021  done  paulson@13837  1022 paulson@13837  1023 lemma zdvd_anti_sym:  paulson@13837  1024  "0 < m ==> 0 < n ==> m dvd n ==> n dvd m ==> m = (n::int)"  paulson@15221  1025  apply (simp add: dvd_def, auto)  paulson@15234  1026  apply (simp add: mult_assoc zero_less_mult_iff zmult_eq_1_iff)  paulson@13837  1027  done  paulson@13837  1028 paulson@13837  1029 lemma zdvd_zadd: "k dvd m ==> k dvd n ==> k dvd (m + n :: int)"  paulson@15221  1030  apply (simp add: dvd_def)  paulson@14479  1031  apply (blast intro: right_distrib [symmetric])  paulson@13837  1032  done  paulson@13837  1033 paulson@13837  1034 lemma zdvd_zdiff: "k dvd m ==> k dvd n ==> k dvd (m - n :: int)"  paulson@15221  1035  apply (simp add: dvd_def)  paulson@14479  1036  apply (blast intro: right_diff_distrib [symmetric])  paulson@13837  1037  done  paulson@13837  1038 paulson@13837  1039 lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"  paulson@13837  1040  apply (subgoal_tac "m = n + (m - n)")  paulson@13837  1041  apply (erule ssubst)  paulson@13837  1042  apply (blast intro: zdvd_zadd, simp)  paulson@13837  1043  done  paulson@13837  1044 paulson@13837  1045 lemma zdvd_zmult: "k dvd (n::int) ==> k dvd m * n"  paulson@15221  1046  apply (simp add: dvd_def)  paulson@14271  1047  apply (blast intro: mult_left_commute)  paulson@13837  1048  done  paulson@13837  1049 paulson@13837  1050 lemma zdvd_zmult2: "k dvd (m::int) ==> k dvd m * n"  paulson@15234  1051  apply (subst mult_commute)  paulson@13837  1052  apply (erule zdvd_zmult)  paulson@13837  1053  done  paulson@13837  1054 paulson@13837  1055 lemma [iff]: "(k::int) dvd m * k"  paulson@13837  1056  apply (rule zdvd_zmult)  paulson@13837  1057  apply (rule zdvd_refl)  paulson@13837  1058  done  paulson@13837  1059 paulson@13837  1060 lemma [iff]: "(k::int) dvd k * m"  paulson@13837  1061  apply (rule zdvd_zmult2)  paulson@13837  1062  apply (rule zdvd_refl)  paulson@13837  1063  done  paulson@13837  1064 paulson@13837  1065 lemma zdvd_zmultD2: "j * k dvd n ==> j dvd (n::int)"  paulson@15221  1066  apply (simp add: dvd_def)  paulson@15234  1067  apply (simp add: mult_assoc, blast)  paulson@13837  1068  done  paulson@13837  1069 paulson@13837  1070 lemma zdvd_zmultD: "j * k dvd n ==> k dvd (n::int)"  paulson@13837  1071  apply (rule zdvd_zmultD2)  paulson@15234  1072  apply (subst mult_commute, assumption)  paulson@13837  1073  done  paulson@13837  1074 paulson@13837  1075 lemma zdvd_zmult_mono: "i dvd m ==> j dvd (n::int) ==> i * j dvd m * n"  paulson@15221  1076  apply (simp add: dvd_def, clarify)  paulson@13837  1077  apply (rule_tac x = "k * ka" in exI)  paulson@14271  1078  apply (simp add: mult_ac)  paulson@13837  1079  done  paulson@13837  1080 paulson@13837  1081 lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"  paulson@13837  1082  apply (rule iffI)  paulson@13837  1083  apply (erule_tac [2] zdvd_zadd)  paulson@13837  1084  apply (subgoal_tac "n = (n + k * m) - k * m")  paulson@13837  1085  apply (erule ssubst)  paulson@13837  1086  apply (erule zdvd_zdiff, simp_all)  paulson@13837  1087  done  paulson@13837  1088 paulson@13837  1089 lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"  paulson@15221  1090  apply (simp add: dvd_def)  paulson@13837  1091  apply (auto simp add: zmod_zmult_zmult1)  paulson@13837  1092  done  paulson@13837  1093 paulson@13837  1094 lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"  paulson@13837  1095  apply (subgoal_tac "k dvd n * (m div n) + m mod n")  paulson@13837  1096  apply (simp add: zmod_zdiv_equality [symmetric])  paulson@13837  1097  apply (simp only: zdvd_zadd zdvd_zmult2)  paulson@13837  1098  done  paulson@13837  1099 paulson@13837  1100 lemma zdvd_not_zless: "0 < m ==> m < n ==> \ n dvd (m::int)"  paulson@15221  1101  apply (simp add: dvd_def, auto)  paulson@13837  1102  apply (subgoal_tac "0 < n")  paulson@13837  1103  prefer 2  paulson@14378  1104  apply (blast intro: order_less_trans)  paulson@14353  1105  apply (simp add: zero_less_mult_iff)  paulson@13837  1106  apply (subgoal_tac "n * k < n * 1")  paulson@14387  1107  apply (drule mult_less_cancel_left [THEN iffD1], auto)  paulson@13837  1108  done  paulson@13837  1109 paulson@13837  1110 lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"  paulson@13837  1111  apply (auto simp add: dvd_def nat_abs_mult_distrib)  paulson@14353  1112  apply (auto simp add: nat_eq_iff abs_if split add: split_if_asm)  paulson@14353  1113  apply (rule_tac x = "-(int k)" in exI)  paulson@16413  1114  apply (auto simp add: int_mult)  paulson@13837  1115  done  paulson@13837  1116 paulson@13837  1117 lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"  paulson@16413  1118  apply (auto simp add: dvd_def abs_if int_mult)  paulson@13837  1119  apply (rule_tac [3] x = "nat k" in exI)  paulson@13837  1120  apply (rule_tac [2] x = "-(int k)" in exI)  paulson@13837  1121  apply (rule_tac x = "nat (-k)" in exI)  paulson@13837  1122  apply (cut_tac [3] k = m in int_less_0_conv)  paulson@13837  1123  apply (cut_tac k = m in int_less_0_conv)  paulson@14353  1124  apply (auto simp add: zero_le_mult_iff mult_less_0_iff  paulson@13837  1125  nat_mult_distrib [symmetric] nat_eq_iff2)  paulson@13837  1126  done  paulson@13837  1127 paulson@13837  1128 lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \ z then (z dvd int m) else m = 0)"  paulson@16413  1129  apply (auto simp add: dvd_def int_mult)  paulson@13837  1130  apply (rule_tac x = "nat k" in exI)  paulson@13837  1131  apply (cut_tac k = m in int_less_0_conv)  paulson@14353  1132  apply (auto simp add: zero_le_mult_iff mult_less_0_iff  paulson@13837  1133  nat_mult_distrib [symmetric] nat_eq_iff2)  paulson@13837  1134  done  paulson@13837  1135 paulson@13837  1136 lemma zminus_dvd_iff [iff]: "(-z dvd w) = (z dvd (w::int))"  paulson@13837  1137  apply (auto simp add: dvd_def)  paulson@13837  1138  apply (rule_tac [!] x = "-k" in exI, auto)  paulson@13837  1139  done  paulson@13837  1140 paulson@13837  1141 lemma dvd_zminus_iff [iff]: "(z dvd -w) = (z dvd (w::int))"  paulson@13837  1142  apply (auto simp add: dvd_def)  paulson@14378  1143  apply (drule minus_equation_iff [THEN iffD1])  paulson@13837  1144  apply (rule_tac [!] x = "-k" in exI, auto)  paulson@13837  1145  done  paulson@13837  1146 paulson@14288  1147 lemma zdvd_imp_le: "[| z dvd n; 0 < n |] ==> z \ (n::int)"  paulson@13837  1148  apply (rule_tac z=n in int_cases)  paulson@13837  1149  apply (auto simp add: dvd_int_iff)  paulson@13837  1150  apply (rule_tac z=z in int_cases)  paulson@13837  1151  apply (auto simp add: dvd_imp_le)  paulson@13837  1152  done  paulson@13837  1153 paulson@13837  1154 paulson@14353  1155 subsection{*Integer Powers*}  paulson@14353  1156 paulson@14353  1157 instance int :: power ..  paulson@14353  1158 paulson@14353  1159 primrec  paulson@14353  1160  "p ^ 0 = 1"  paulson@14353  1161  "p ^ (Suc n) = (p::int) * (p ^ n)"  paulson@14353  1162 paulson@14353  1163 paulson@15003  1164 instance int :: recpower  paulson@14353  1165 proof  paulson@14353  1166  fix z :: int  paulson@14353  1167  fix n :: nat  paulson@14353  1168  show "z^0 = 1" by simp  paulson@14353  1169  show "z^(Suc n) = z * (z^n)" by simp  paulson@14353  1170 qed  paulson@14353  1171 paulson@14353  1172 paulson@14353  1173 lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"  paulson@15251  1174 apply (induct "y", auto)  paulson@14353  1175 apply (rule zmod_zmult1_eq [THEN trans])  paulson@14353  1176 apply (simp (no_asm_simp))  paulson@14353  1177 apply (rule zmod_zmult_distrib [symmetric])  paulson@14353  1178 done  paulson@14353  1179 paulson@14353  1180 lemma zpower_zadd_distrib: "x^(y+z) = ((x^y)*(x^z)::int)"  paulson@14353  1181  by (rule Power.power_add)  paulson@14353  1182 paulson@14353  1183 lemma zpower_zpower: "(x^y)^z = (x^(y*z)::int)"  paulson@14353  1184  by (rule Power.power_mult [symmetric])  paulson@14353  1185 paulson@14353  1186 lemma zero_less_zpower_abs_iff [simp]:  paulson@14353  1187  "(0 < (abs x)^n) = (x \ (0::int) | n=0)"  paulson@15251  1188 apply (induct "n")  paulson@14353  1189 apply (auto simp add: zero_less_mult_iff)  paulson@14353  1190 done  paulson@14353  1191 paulson@14353  1192 lemma zero_le_zpower_abs [simp]: "(0::int) <= (abs x)^n"  paulson@15251  1193 apply (induct "n")  paulson@14353  1194 apply (auto simp add: zero_le_mult_iff)  paulson@14353  1195 done  paulson@14353  1196 paulson@16413  1197 lemma int_power: "int (m^n) = (int m) ^ n"  paulson@16413  1198  by (induct n, simp_all add: int_mult)  paulson@16413  1199 paulson@16413  1200 text{*Compatibility binding*}  paulson@16413  1201 lemmas zpower_int = int_power [symmetric]  berghofe@15320  1202 obua@15101  1203 lemma zdiv_int: "int (a div b) = (int a) div (int b)"  obua@15101  1204 apply (subst split_div, auto)  obua@15101  1205 apply (subst split_zdiv, auto)  obua@15101  1206 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)  obua@15101  1207 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)  obua@15101  1208 done  obua@15101  1209 obua@15101  1210 lemma zmod_int: "int (a mod b) = (int a) mod (int b)"  obua@15101  1211 apply (subst split_mod, auto)  obua@15101  1212 apply (subst split_zmod, auto)  paulson@16413  1213 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia  paulson@16413  1214  in unique_remainder)  obua@15101  1215 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)  obua@15101  1216 done  paulson@14353  1217 paulson@16413  1218 text{*Suggested by Matthias Daum*}  paulson@16413  1219 lemma int_power_div_base:  paulson@16413  1220  "\0 < m; 0 < k\ \ k ^ m div k = (k::int) ^ (m - Suc 0)"  paulson@16413  1221 apply (subgoal_tac "k ^ m = k ^ ((m - 1) + 1)")  paulson@16413  1222  apply (erule ssubst)  paulson@16413  1223  apply (simp only: power_add)  paulson@16413  1224  apply simp_all  paulson@16413  1225 done  paulson@16413  1226 paulson@13183  1227 ML  paulson@13183  1228 {*  paulson@13183  1229 val quorem_def = thm "quorem_def";  paulson@13183  1230 paulson@13183  1231 val unique_quotient = thm "unique_quotient";  paulson@13183  1232 val unique_remainder = thm "unique_remainder";  paulson@13183  1233 val adjust_eq = thm "adjust_eq";  paulson@13183  1234 val posDivAlg_eqn = thm "posDivAlg_eqn";  paulson@13183  1235 val posDivAlg_correct = thm "posDivAlg_correct";  paulson@13183  1236 val negDivAlg_eqn = thm "negDivAlg_eqn";  paulson@13183  1237 val negDivAlg_correct = thm "negDivAlg_correct";  paulson@13183  1238 val quorem_0 = thm "quorem_0";  paulson@13183  1239 val posDivAlg_0 = thm "posDivAlg_0";  paulson@13183  1240 val negDivAlg_minus1 = thm "negDivAlg_minus1";  paulson@13183  1241 val negateSnd_eq = thm "negateSnd_eq";  paulson@13183  1242 val quorem_neg = thm "quorem_neg";  paulson@13183  1243 val divAlg_correct = thm "divAlg_correct";  paulson@13183  1244 val DIVISION_BY_ZERO = thm "DIVISION_BY_ZERO";  paulson@13183  1245 val zmod_zdiv_equality = thm "zmod_zdiv_equality";  paulson@13183  1246 val pos_mod_conj = thm "pos_mod_conj";  paulson@13183  1247 val pos_mod_sign = thm "pos_mod_sign";  paulson@13183  1248 val neg_mod_conj = thm "neg_mod_conj";  paulson@13183  1249 val neg_mod_sign = thm "neg_mod_sign";  paulson@13183  1250 val quorem_div_mod = thm "quorem_div_mod";  paulson@13183  1251 val quorem_div = thm "quorem_div";  paulson@13183  1252 val quorem_mod = thm "quorem_mod";  paulson@13183  1253 val div_pos_pos_trivial = thm "div_pos_pos_trivial";  paulson@13183  1254 val div_neg_neg_trivial = thm "div_neg_neg_trivial";  paulson@13183  1255 val div_pos_neg_trivial = thm "div_pos_neg_trivial";  paulson@13183  1256 val mod_pos_pos_trivial = thm "mod_pos_pos_trivial";  paulson@13183  1257 val mod_neg_neg_trivial = thm "mod_neg_neg_trivial";  paulson@13183  1258 val mod_pos_neg_trivial = thm "mod_pos_neg_trivial";  paulson@13183  1259 val zdiv_zminus_zminus = thm "zdiv_zminus_zminus";  paulson@13183  1260 val zmod_zminus_zminus = thm "zmod_zminus_zminus";  paulson@13183  1261 val zdiv_zminus1_eq_if = thm "zdiv_zminus1_eq_if";  paulson@13183  1262 val zmod_zminus1_eq_if = thm "zmod_zminus1_eq_if";  paulson@13183  1263 val zdiv_zminus2 = thm "zdiv_zminus2";  paulson@13183  1264 val zmod_zminus2 = thm "zmod_zminus2";  paulson@13183  1265 val zdiv_zminus2_eq_if = thm "zdiv_zminus2_eq_if";  paulson@13183  1266 val zmod_zminus2_eq_if = thm "zmod_zminus2_eq_if";  paulson@13183  1267 val self_quotient = thm "self_quotient";  paulson@13183  1268 val self_remainder = thm "self_remainder";  paulson@13183  1269 val zdiv_self = thm "zdiv_self";  paulson@13183  1270 val zmod_self = thm "zmod_self";  paulson@13183  1271 val zdiv_zero = thm "zdiv_zero";  paulson@13183  1272 val div_eq_minus1 = thm "div_eq_minus1";  paulson@13183  1273 val zmod_zero = thm "zmod_zero";  paulson@13183  1274 val zdiv_minus1 = thm "zdiv_minus1";  paulson@13183  1275 val zmod_minus1 = thm "zmod_minus1";  paulson@13183  1276 val div_pos_pos = thm "div_pos_pos";  paulson@13183  1277 val mod_pos_pos = thm "mod_pos_pos";  paulson@13183  1278 val div_neg_pos = thm "div_neg_pos";  paulson@13183  1279 val mod_neg_pos = thm "mod_neg_pos";  paulson@13183  1280 val div_pos_neg = thm "div_pos_neg";  paulson@13183  1281 val mod_pos_neg = thm "mod_pos_neg";  paulson@13183  1282 val div_neg_neg = thm "div_neg_neg";  paulson@13183  1283 val mod_neg_neg = thm "mod_neg_neg";  paulson@13183  1284 val zmod_1 = thm "zmod_1";  paulson@13183  1285 val zdiv_1 = thm "zdiv_1";  paulson@13183  1286 val zmod_minus1_right = thm "zmod_minus1_right";  paulson@13183  1287 val zdiv_minus1_right = thm "zdiv_minus1_right";  paulson@13183  1288 val zdiv_mono1 = thm "zdiv_mono1";  paulson@13183  1289 val zdiv_mono1_neg = thm "zdiv_mono1_neg";  paulson@13183  1290 val zdiv_mono2 = thm "zdiv_mono2";  paulson@13183  1291 val zdiv_mono2_neg = thm "zdiv_mono2_neg";  paulson@13183  1292 val zdiv_zmult1_eq = thm "zdiv_zmult1_eq";  paulson@13183  1293 val zmod_zmult1_eq = thm "zmod_zmult1_eq";  paulson@13183  1294 val zmod_zmult1_eq' = thm "zmod_zmult1_eq'";  paulson@13183  1295 val zmod_zmult_distrib = thm "zmod_zmult_distrib";  paulson@13183  1296 val zdiv_zmult_self1 = thm "zdiv_zmult_self1";  paulson@13183  1297 val zdiv_zmult_self2 = thm "zdiv_zmult_self2";  paulson@13183  1298 val zmod_zmult_self1 = thm "zmod_zmult_self1";  paulson@13183  1299 val zmod_zmult_self2 = thm "zmod_zmult_self2";  paulson@13183  1300 val zmod_eq_0_iff = thm "zmod_eq_0_iff";  paulson@13183  1301 val zdiv_zadd1_eq = thm "zdiv_zadd1_eq";  paulson@13183  1302 val zmod_zadd1_eq = thm "zmod_zadd1_eq";  paulson@13183  1303 val mod_div_trivial = thm "mod_div_trivial";  paulson@13183  1304 val mod_mod_trivial = thm "mod_mod_trivial";  paulson@13183  1305 val zmod_zadd_left_eq = thm "zmod_zadd_left_eq";  paulson@13183  1306 val zmod_zadd_right_eq = thm "zmod_zadd_right_eq";  paulson@13183  1307 val zdiv_zadd_self1 = thm "zdiv_zadd_self1";  paulson@13183  1308 val zdiv_zadd_self2 = thm "zdiv_zadd_self2";  paulson@13183  1309 val zmod_zadd_self1 = thm "zmod_zadd_self1";  paulson@13183  1310 val zmod_zadd_self2 = thm "zmod_zadd_self2";  paulson@13183  1311 val zdiv_zmult2_eq = thm "zdiv_zmult2_eq";  paulson@13183  1312 val zmod_zmult2_eq = thm "zmod_zmult2_eq";  paulson@13183  1313 val zdiv_zmult_zmult1 = thm "zdiv_zmult_zmult1";  paulson@13183  1314 val zdiv_zmult_zmult2 = thm "zdiv_zmult_zmult2";  paulson@13183  1315 val zmod_zmult_zmult1 = thm "zmod_zmult_zmult1";  paulson@13183  1316 val zmod_zmult_zmult2 = thm "zmod_zmult_zmult2";  paulson@13183  1317 val pos_zdiv_mult_2 = thm "pos_zdiv_mult_2";  paulson@13183  1318 val neg_zdiv_mult_2 = thm "neg_zdiv_mult_2";  paulson@13183  1319 val zdiv_number_of_BIT = thm "zdiv_number_of_BIT";  paulson@13183  1320 val pos_zmod_mult_2 = thm "pos_zmod_mult_2";  paulson@13183  1321 val neg_zmod_mult_2 = thm "neg_zmod_mult_2";  paulson@13183  1322 val zmod_number_of_BIT = thm "zmod_number_of_BIT";  paulson@13183  1323 val div_neg_pos_less0 = thm "div_neg_pos_less0";  paulson@13183  1324 val div_nonneg_neg_le0 = thm "div_nonneg_neg_le0";  paulson@13183  1325 val pos_imp_zdiv_nonneg_iff = thm "pos_imp_zdiv_nonneg_iff";  paulson@13183  1326 val neg_imp_zdiv_nonneg_iff = thm "neg_imp_zdiv_nonneg_iff";  paulson@13183  1327 val pos_imp_zdiv_neg_iff = thm "pos_imp_zdiv_neg_iff";  paulson@13183  1328 val neg_imp_zdiv_neg_iff = thm "neg_imp_zdiv_neg_iff";  paulson@14353  1329 paulson@14353  1330 val zpower_zmod = thm "zpower_zmod";  paulson@14353  1331 val zpower_zadd_distrib = thm "zpower_zadd_distrib";  paulson@14353  1332 val zpower_zpower = thm "zpower_zpower";  paulson@14353  1333 val zero_less_zpower_abs_iff = thm "zero_less_zpower_abs_iff";  paulson@14353  1334 val zero_le_zpower_abs = thm "zero_le_zpower_abs";  berghofe@15320  1335 val zpower_int = thm "zpower_int";  paulson@13183  1336 *}  paulson@13183  1337 paulson@6917  1338 end