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