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