src/HOL/IntDiv.thy
changeset 23164 69e55066dbca
child 23306 cdb027d0637e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/IntDiv.thy	Thu May 31 18:16:52 2007 +0200
     1.3 @@ -0,0 +1,1406 @@
     1.4 +(*  Title:      HOL/IntDiv.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1999  University of Cambridge
     1.8 +
     1.9 +*)
    1.10 +
    1.11 +header{*The Division Operators div and mod; the Divides Relation dvd*}
    1.12 +
    1.13 +theory IntDiv
    1.14 +imports IntArith Divides FunDef
    1.15 +begin
    1.16 +
    1.17 +declare zless_nat_conj [simp]
    1.18 +
    1.19 +constdefs
    1.20 +  quorem :: "(int*int) * (int*int) => bool"
    1.21 +    --{*definition of quotient and remainder*}
    1.22 +    [code func]: "quorem == %((a,b), (q,r)).
    1.23 +                      a = b*q + r &
    1.24 +                      (if 0 < b then 0\<le>r & r<b else b<r & r \<le> 0)"
    1.25 +
    1.26 +  adjust :: "[int, int*int] => int*int"
    1.27 +    --{*for the division algorithm*}
    1.28 +    [code func]: "adjust b == %(q,r). if 0 \<le> r-b then (2*q + 1, r-b)
    1.29 +                         else (2*q, r)"
    1.30 +
    1.31 +text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
    1.32 +function
    1.33 +  posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int"
    1.34 +where
    1.35 +  "posDivAlg a b =
    1.36 +     (if (a<b | b\<le>0) then (0,a)
    1.37 +        else adjust b (posDivAlg a (2*b)))"
    1.38 +by auto
    1.39 +termination by (relation "measure (%(a,b). nat(a - b + 1))") auto
    1.40 +
    1.41 +text{*algorithm for the case @{text "a<0, b>0"}*}
    1.42 +function
    1.43 +  negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int"
    1.44 +where
    1.45 +  "negDivAlg a b  =
    1.46 +     (if (0\<le>a+b | b\<le>0) then (-1,a+b)
    1.47 +      else adjust b (negDivAlg a (2*b)))"
    1.48 +by auto
    1.49 +termination by (relation "measure (%(a,b). nat(- a - b))") auto
    1.50 +
    1.51 +text{*algorithm for the general case @{term "b\<noteq>0"}*}
    1.52 +constdefs
    1.53 +  negateSnd :: "int*int => int*int"
    1.54 +    [code func]: "negateSnd == %(q,r). (q,-r)"
    1.55 +
    1.56 +definition
    1.57 +  divAlg :: "int \<times> int \<Rightarrow> int \<times> int"
    1.58 +    --{*The full division algorithm considers all possible signs for a, b
    1.59 +       including the special case @{text "a=0, b<0"} because 
    1.60 +       @{term negDivAlg} requires @{term "a<0"}.*}
    1.61 +where
    1.62 +  "divAlg = (\<lambda>(a, b). (if 0\<le>a then
    1.63 +                  if 0\<le>b then posDivAlg a b
    1.64 +                  else if a=0 then (0, 0)
    1.65 +                       else negateSnd (negDivAlg (-a) (-b))
    1.66 +               else 
    1.67 +                  if 0<b then negDivAlg a b
    1.68 +                  else negateSnd (posDivAlg (-a) (-b))))"
    1.69 +
    1.70 +instance int :: Divides.div
    1.71 +  div_def: "a div b == fst (divAlg (a, b))"
    1.72 +  mod_def: "a mod b == snd (divAlg (a, b))" ..
    1.73 +
    1.74 +lemma divAlg_mod_div:
    1.75 +  "divAlg (p, q) = (p div q, p mod q)"
    1.76 +  by (auto simp add: div_def mod_def)
    1.77 +
    1.78 +text{*
    1.79 +Here is the division algorithm in ML:
    1.80 +
    1.81 +\begin{verbatim}
    1.82 +    fun posDivAlg (a,b) =
    1.83 +      if a<b then (0,a)
    1.84 +      else let val (q,r) = posDivAlg(a, 2*b)
    1.85 +	       in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
    1.86 +	   end
    1.87 +
    1.88 +    fun negDivAlg (a,b) =
    1.89 +      if 0\<le>a+b then (~1,a+b)
    1.90 +      else let val (q,r) = negDivAlg(a, 2*b)
    1.91 +	       in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
    1.92 +	   end;
    1.93 +
    1.94 +    fun negateSnd (q,r:int) = (q,~r);
    1.95 +
    1.96 +    fun divAlg (a,b) = if 0\<le>a then 
    1.97 +			  if b>0 then posDivAlg (a,b) 
    1.98 +			   else if a=0 then (0,0)
    1.99 +				else negateSnd (negDivAlg (~a,~b))
   1.100 +		       else 
   1.101 +			  if 0<b then negDivAlg (a,b)
   1.102 +			  else        negateSnd (posDivAlg (~a,~b));
   1.103 +\end{verbatim}
   1.104 +*}
   1.105 +
   1.106 +
   1.107 +
   1.108 +subsection{*Uniqueness and Monotonicity of Quotients and Remainders*}
   1.109 +
   1.110 +lemma unique_quotient_lemma:
   1.111 +     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
   1.112 +      ==> q' \<le> (q::int)"
   1.113 +apply (subgoal_tac "r' + b * (q'-q) \<le> r")
   1.114 + prefer 2 apply (simp add: right_diff_distrib)
   1.115 +apply (subgoal_tac "0 < b * (1 + q - q') ")
   1.116 +apply (erule_tac [2] order_le_less_trans)
   1.117 + prefer 2 apply (simp add: right_diff_distrib right_distrib)
   1.118 +apply (subgoal_tac "b * q' < b * (1 + q) ")
   1.119 + prefer 2 apply (simp add: right_diff_distrib right_distrib)
   1.120 +apply (simp add: mult_less_cancel_left)
   1.121 +done
   1.122 +
   1.123 +lemma unique_quotient_lemma_neg:
   1.124 +     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
   1.125 +      ==> q \<le> (q'::int)"
   1.126 +by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
   1.127 +    auto)
   1.128 +
   1.129 +lemma unique_quotient:
   1.130 +     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b \<noteq> 0 |]  
   1.131 +      ==> q = q'"
   1.132 +apply (simp add: quorem_def linorder_neq_iff split: split_if_asm)
   1.133 +apply (blast intro: order_antisym
   1.134 +             dest: order_eq_refl [THEN unique_quotient_lemma] 
   1.135 +             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
   1.136 +done
   1.137 +
   1.138 +
   1.139 +lemma unique_remainder:
   1.140 +     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b \<noteq> 0 |]  
   1.141 +      ==> r = r'"
   1.142 +apply (subgoal_tac "q = q'")
   1.143 + apply (simp add: quorem_def)
   1.144 +apply (blast intro: unique_quotient)
   1.145 +done
   1.146 +
   1.147 +
   1.148 +subsection{*Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends*}
   1.149 +
   1.150 +text{*And positive divisors*}
   1.151 +
   1.152 +lemma adjust_eq [simp]:
   1.153 +     "adjust b (q,r) = 
   1.154 +      (let diff = r-b in  
   1.155 +	if 0 \<le> diff then (2*q + 1, diff)   
   1.156 +                     else (2*q, r))"
   1.157 +by (simp add: Let_def adjust_def)
   1.158 +
   1.159 +declare posDivAlg.simps [simp del]
   1.160 +
   1.161 +text{*use with a simproc to avoid repeatedly proving the premise*}
   1.162 +lemma posDivAlg_eqn:
   1.163 +     "0 < b ==>  
   1.164 +      posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
   1.165 +by (rule posDivAlg.simps [THEN trans], simp)
   1.166 +
   1.167 +text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
   1.168 +theorem posDivAlg_correct:
   1.169 +  assumes "0 \<le> a" and "0 < b"
   1.170 +  shows "quorem ((a, b), posDivAlg a b)"
   1.171 +using prems apply (induct a b rule: posDivAlg.induct)
   1.172 +apply auto
   1.173 +apply (simp add: quorem_def)
   1.174 +apply (subst posDivAlg_eqn, simp add: right_distrib)
   1.175 +apply (case_tac "a < b")
   1.176 +apply simp_all
   1.177 +apply (erule splitE)
   1.178 +apply (auto simp add: right_distrib Let_def)
   1.179 +done
   1.180 +
   1.181 +
   1.182 +subsection{*Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends*}
   1.183 +
   1.184 +text{*And positive divisors*}
   1.185 +
   1.186 +declare negDivAlg.simps [simp del]
   1.187 +
   1.188 +text{*use with a simproc to avoid repeatedly proving the premise*}
   1.189 +lemma negDivAlg_eqn:
   1.190 +     "0 < b ==>  
   1.191 +      negDivAlg a b =       
   1.192 +       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
   1.193 +by (rule negDivAlg.simps [THEN trans], simp)
   1.194 +
   1.195 +(*Correctness of negDivAlg: it computes quotients correctly
   1.196 +  It doesn't work if a=0 because the 0/b equals 0, not -1*)
   1.197 +lemma negDivAlg_correct:
   1.198 +  assumes "a < 0" and "b > 0"
   1.199 +  shows "quorem ((a, b), negDivAlg a b)"
   1.200 +using prems apply (induct a b rule: negDivAlg.induct)
   1.201 +apply (auto simp add: linorder_not_le)
   1.202 +apply (simp add: quorem_def)
   1.203 +apply (subst negDivAlg_eqn, assumption)
   1.204 +apply (case_tac "a + b < (0\<Colon>int)")
   1.205 +apply simp_all
   1.206 +apply (erule splitE)
   1.207 +apply (auto simp add: right_distrib Let_def)
   1.208 +done
   1.209 +
   1.210 +
   1.211 +subsection{*Existence Shown by Proving the Division Algorithm to be Correct*}
   1.212 +
   1.213 +(*the case a=0*)
   1.214 +lemma quorem_0: "b \<noteq> 0 ==> quorem ((0,b), (0,0))"
   1.215 +by (auto simp add: quorem_def linorder_neq_iff)
   1.216 +
   1.217 +lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
   1.218 +by (subst posDivAlg.simps, auto)
   1.219 +
   1.220 +lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
   1.221 +by (subst negDivAlg.simps, auto)
   1.222 +
   1.223 +lemma negateSnd_eq [simp]: "negateSnd(q,r) = (q,-r)"
   1.224 +by (simp add: negateSnd_def)
   1.225 +
   1.226 +lemma quorem_neg: "quorem ((-a,-b), qr) ==> quorem ((a,b), negateSnd qr)"
   1.227 +by (auto simp add: split_ifs quorem_def)
   1.228 +
   1.229 +lemma divAlg_correct: "b \<noteq> 0 ==> quorem ((a,b), divAlg (a, b))"
   1.230 +by (force simp add: linorder_neq_iff quorem_0 divAlg_def quorem_neg
   1.231 +                    posDivAlg_correct negDivAlg_correct)
   1.232 +
   1.233 +text{*Arbitrary definitions for division by zero.  Useful to simplify 
   1.234 +    certain equations.*}
   1.235 +
   1.236 +lemma DIVISION_BY_ZERO [simp]: "a div (0::int) = 0 & a mod (0::int) = a"
   1.237 +by (simp add: div_def mod_def divAlg_def posDivAlg.simps)  
   1.238 +
   1.239 +
   1.240 +text{*Basic laws about division and remainder*}
   1.241 +
   1.242 +lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
   1.243 +apply (case_tac "b = 0", simp)
   1.244 +apply (cut_tac a = a and b = b in divAlg_correct)
   1.245 +apply (auto simp add: quorem_def div_def mod_def)
   1.246 +done
   1.247 +
   1.248 +lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"
   1.249 +by(simp add: zmod_zdiv_equality[symmetric])
   1.250 +
   1.251 +lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"
   1.252 +by(simp add: mult_commute zmod_zdiv_equality[symmetric])
   1.253 +
   1.254 +text {* Tool setup *}
   1.255 +
   1.256 +ML_setup {*
   1.257 +local 
   1.258 +
   1.259 +structure CancelDivMod = CancelDivModFun(
   1.260 +struct
   1.261 +  val div_name = @{const_name Divides.div};
   1.262 +  val mod_name = @{const_name Divides.mod};
   1.263 +  val mk_binop = HOLogic.mk_binop;
   1.264 +  val mk_sum = Int_Numeral_Simprocs.mk_sum HOLogic.intT;
   1.265 +  val dest_sum = Int_Numeral_Simprocs.dest_sum;
   1.266 +  val div_mod_eqs =
   1.267 +    map mk_meta_eq [@{thm zdiv_zmod_equality},
   1.268 +      @{thm zdiv_zmod_equality2}];
   1.269 +  val trans = trans;
   1.270 +  val prove_eq_sums =
   1.271 +    let
   1.272 +      val simps = diff_int_def :: Int_Numeral_Simprocs.add_0s @ zadd_ac
   1.273 +    in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all_tac simps) end;
   1.274 +end)
   1.275 +
   1.276 +in
   1.277 +
   1.278 +val cancel_zdiv_zmod_proc = NatArithUtils.prep_simproc
   1.279 +  ("cancel_zdiv_zmod", ["(m::int) + n"], K CancelDivMod.proc)
   1.280 +
   1.281 +end;
   1.282 +
   1.283 +Addsimprocs [cancel_zdiv_zmod_proc]
   1.284 +*}
   1.285 +
   1.286 +lemma pos_mod_conj : "(0::int) < b ==> 0 \<le> a mod b & a mod b < b"
   1.287 +apply (cut_tac a = a and b = b in divAlg_correct)
   1.288 +apply (auto simp add: quorem_def mod_def)
   1.289 +done
   1.290 +
   1.291 +lemmas pos_mod_sign  [simp] = pos_mod_conj [THEN conjunct1, standard]
   1.292 +   and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2, standard]
   1.293 +
   1.294 +lemma neg_mod_conj : "b < (0::int) ==> a mod b \<le> 0 & b < a mod b"
   1.295 +apply (cut_tac a = a and b = b in divAlg_correct)
   1.296 +apply (auto simp add: quorem_def div_def mod_def)
   1.297 +done
   1.298 +
   1.299 +lemmas neg_mod_sign  [simp] = neg_mod_conj [THEN conjunct1, standard]
   1.300 +   and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2, standard]
   1.301 +
   1.302 +
   1.303 +
   1.304 +subsection{*General Properties of div and mod*}
   1.305 +
   1.306 +lemma quorem_div_mod: "b \<noteq> 0 ==> quorem ((a, b), (a div b, a mod b))"
   1.307 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   1.308 +apply (force simp add: quorem_def linorder_neq_iff)
   1.309 +done
   1.310 +
   1.311 +lemma quorem_div: "[| quorem((a,b),(q,r));  b \<noteq> 0 |] ==> a div b = q"
   1.312 +by (simp add: quorem_div_mod [THEN unique_quotient])
   1.313 +
   1.314 +lemma quorem_mod: "[| quorem((a,b),(q,r));  b \<noteq> 0 |] ==> a mod b = r"
   1.315 +by (simp add: quorem_div_mod [THEN unique_remainder])
   1.316 +
   1.317 +lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
   1.318 +apply (rule quorem_div)
   1.319 +apply (auto simp add: quorem_def)
   1.320 +done
   1.321 +
   1.322 +lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
   1.323 +apply (rule quorem_div)
   1.324 +apply (auto simp add: quorem_def)
   1.325 +done
   1.326 +
   1.327 +lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
   1.328 +apply (rule quorem_div)
   1.329 +apply (auto simp add: quorem_def)
   1.330 +done
   1.331 +
   1.332 +(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
   1.333 +
   1.334 +lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
   1.335 +apply (rule_tac q = 0 in quorem_mod)
   1.336 +apply (auto simp add: quorem_def)
   1.337 +done
   1.338 +
   1.339 +lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
   1.340 +apply (rule_tac q = 0 in quorem_mod)
   1.341 +apply (auto simp add: quorem_def)
   1.342 +done
   1.343 +
   1.344 +lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
   1.345 +apply (rule_tac q = "-1" in quorem_mod)
   1.346 +apply (auto simp add: quorem_def)
   1.347 +done
   1.348 +
   1.349 +text{*There is no @{text mod_neg_pos_trivial}.*}
   1.350 +
   1.351 +
   1.352 +(*Simpler laws such as -a div b = -(a div b) FAIL, but see just below*)
   1.353 +lemma zdiv_zminus_zminus [simp]: "(-a) div (-b) = a div (b::int)"
   1.354 +apply (case_tac "b = 0", simp)
   1.355 +apply (simp add: quorem_div_mod [THEN quorem_neg, simplified, 
   1.356 +                                 THEN quorem_div, THEN sym])
   1.357 +
   1.358 +done
   1.359 +
   1.360 +(*Simpler laws such as -a mod b = -(a mod b) FAIL, but see just below*)
   1.361 +lemma zmod_zminus_zminus [simp]: "(-a) mod (-b) = - (a mod (b::int))"
   1.362 +apply (case_tac "b = 0", simp)
   1.363 +apply (subst quorem_div_mod [THEN quorem_neg, simplified, THEN quorem_mod],
   1.364 +       auto)
   1.365 +done
   1.366 +
   1.367 +
   1.368 +subsection{*Laws for div and mod with Unary Minus*}
   1.369 +
   1.370 +lemma zminus1_lemma:
   1.371 +     "quorem((a,b),(q,r))  
   1.372 +      ==> quorem ((-a,b), (if r=0 then -q else -q - 1),  
   1.373 +                          (if r=0 then 0 else b-r))"
   1.374 +by (force simp add: split_ifs quorem_def linorder_neq_iff right_diff_distrib)
   1.375 +
   1.376 +
   1.377 +lemma zdiv_zminus1_eq_if:
   1.378 +     "b \<noteq> (0::int)  
   1.379 +      ==> (-a) div b =  
   1.380 +          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   1.381 +by (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_div])
   1.382 +
   1.383 +lemma zmod_zminus1_eq_if:
   1.384 +     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
   1.385 +apply (case_tac "b = 0", simp)
   1.386 +apply (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_mod])
   1.387 +done
   1.388 +
   1.389 +lemma zdiv_zminus2: "a div (-b) = (-a::int) div b"
   1.390 +by (cut_tac a = "-a" in zdiv_zminus_zminus, auto)
   1.391 +
   1.392 +lemma zmod_zminus2: "a mod (-b) = - ((-a::int) mod b)"
   1.393 +by (cut_tac a = "-a" and b = b in zmod_zminus_zminus, auto)
   1.394 +
   1.395 +lemma zdiv_zminus2_eq_if:
   1.396 +     "b \<noteq> (0::int)  
   1.397 +      ==> a div (-b) =  
   1.398 +          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   1.399 +by (simp add: zdiv_zminus1_eq_if zdiv_zminus2)
   1.400 +
   1.401 +lemma zmod_zminus2_eq_if:
   1.402 +     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
   1.403 +by (simp add: zmod_zminus1_eq_if zmod_zminus2)
   1.404 +
   1.405 +
   1.406 +subsection{*Division of a Number by Itself*}
   1.407 +
   1.408 +lemma self_quotient_aux1: "[| (0::int) < a; a = r + a*q; r < a |] ==> 1 \<le> q"
   1.409 +apply (subgoal_tac "0 < a*q")
   1.410 + apply (simp add: zero_less_mult_iff, arith)
   1.411 +done
   1.412 +
   1.413 +lemma self_quotient_aux2: "[| (0::int) < a; a = r + a*q; 0 \<le> r |] ==> q \<le> 1"
   1.414 +apply (subgoal_tac "0 \<le> a* (1-q) ")
   1.415 + apply (simp add: zero_le_mult_iff)
   1.416 +apply (simp add: right_diff_distrib)
   1.417 +done
   1.418 +
   1.419 +lemma self_quotient: "[| quorem((a,a),(q,r));  a \<noteq> (0::int) |] ==> q = 1"
   1.420 +apply (simp add: split_ifs quorem_def linorder_neq_iff)
   1.421 +apply (rule order_antisym, safe, simp_all)
   1.422 +apply (rule_tac [3] a = "-a" and r = "-r" in self_quotient_aux1)
   1.423 +apply (rule_tac a = "-a" and r = "-r" in self_quotient_aux2)
   1.424 +apply (force intro: self_quotient_aux1 self_quotient_aux2 simp add: add_commute)+
   1.425 +done
   1.426 +
   1.427 +lemma self_remainder: "[| quorem((a,a),(q,r));  a \<noteq> (0::int) |] ==> r = 0"
   1.428 +apply (frule self_quotient, assumption)
   1.429 +apply (simp add: quorem_def)
   1.430 +done
   1.431 +
   1.432 +lemma zdiv_self [simp]: "a \<noteq> 0 ==> a div a = (1::int)"
   1.433 +by (simp add: quorem_div_mod [THEN self_quotient])
   1.434 +
   1.435 +(*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)
   1.436 +lemma zmod_self [simp]: "a mod a = (0::int)"
   1.437 +apply (case_tac "a = 0", simp)
   1.438 +apply (simp add: quorem_div_mod [THEN self_remainder])
   1.439 +done
   1.440 +
   1.441 +
   1.442 +subsection{*Computation of Division and Remainder*}
   1.443 +
   1.444 +lemma zdiv_zero [simp]: "(0::int) div b = 0"
   1.445 +by (simp add: div_def divAlg_def)
   1.446 +
   1.447 +lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
   1.448 +by (simp add: div_def divAlg_def)
   1.449 +
   1.450 +lemma zmod_zero [simp]: "(0::int) mod b = 0"
   1.451 +by (simp add: mod_def divAlg_def)
   1.452 +
   1.453 +lemma zdiv_minus1: "(0::int) < b ==> -1 div b = -1"
   1.454 +by (simp add: div_def divAlg_def)
   1.455 +
   1.456 +lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
   1.457 +by (simp add: mod_def divAlg_def)
   1.458 +
   1.459 +text{*a positive, b positive *}
   1.460 +
   1.461 +lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
   1.462 +by (simp add: div_def divAlg_def)
   1.463 +
   1.464 +lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
   1.465 +by (simp add: mod_def divAlg_def)
   1.466 +
   1.467 +text{*a negative, b positive *}
   1.468 +
   1.469 +lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
   1.470 +by (simp add: div_def divAlg_def)
   1.471 +
   1.472 +lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
   1.473 +by (simp add: mod_def divAlg_def)
   1.474 +
   1.475 +text{*a positive, b negative *}
   1.476 +
   1.477 +lemma div_pos_neg:
   1.478 +     "[| 0 < a;  b < 0 |] ==> a div b = fst (negateSnd (negDivAlg (-a) (-b)))"
   1.479 +by (simp add: div_def divAlg_def)
   1.480 +
   1.481 +lemma mod_pos_neg:
   1.482 +     "[| 0 < a;  b < 0 |] ==> a mod b = snd (negateSnd (negDivAlg (-a) (-b)))"
   1.483 +by (simp add: mod_def divAlg_def)
   1.484 +
   1.485 +text{*a negative, b negative *}
   1.486 +
   1.487 +lemma div_neg_neg:
   1.488 +     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (negateSnd (posDivAlg (-a) (-b)))"
   1.489 +by (simp add: div_def divAlg_def)
   1.490 +
   1.491 +lemma mod_neg_neg:
   1.492 +     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (negateSnd (posDivAlg (-a) (-b)))"
   1.493 +by (simp add: mod_def divAlg_def)
   1.494 +
   1.495 +text {*Simplify expresions in which div and mod combine numerical constants*}
   1.496 +
   1.497 +lemmas div_pos_pos_number_of [simp] =
   1.498 +    div_pos_pos [of "number_of v" "number_of w", standard]
   1.499 +
   1.500 +lemmas div_neg_pos_number_of [simp] =
   1.501 +    div_neg_pos [of "number_of v" "number_of w", standard]
   1.502 +
   1.503 +lemmas div_pos_neg_number_of [simp] =
   1.504 +    div_pos_neg [of "number_of v" "number_of w", standard]
   1.505 +
   1.506 +lemmas div_neg_neg_number_of [simp] =
   1.507 +    div_neg_neg [of "number_of v" "number_of w", standard]
   1.508 +
   1.509 +
   1.510 +lemmas mod_pos_pos_number_of [simp] =
   1.511 +    mod_pos_pos [of "number_of v" "number_of w", standard]
   1.512 +
   1.513 +lemmas mod_neg_pos_number_of [simp] =
   1.514 +    mod_neg_pos [of "number_of v" "number_of w", standard]
   1.515 +
   1.516 +lemmas mod_pos_neg_number_of [simp] =
   1.517 +    mod_pos_neg [of "number_of v" "number_of w", standard]
   1.518 +
   1.519 +lemmas mod_neg_neg_number_of [simp] =
   1.520 +    mod_neg_neg [of "number_of v" "number_of w", standard]
   1.521 +
   1.522 +
   1.523 +lemmas posDivAlg_eqn_number_of [simp] =
   1.524 +    posDivAlg_eqn [of "number_of v" "number_of w", standard]
   1.525 +
   1.526 +lemmas negDivAlg_eqn_number_of [simp] =
   1.527 +    negDivAlg_eqn [of "number_of v" "number_of w", standard]
   1.528 +
   1.529 +
   1.530 +text{*Special-case simplification *}
   1.531 +
   1.532 +lemma zmod_1 [simp]: "a mod (1::int) = 0"
   1.533 +apply (cut_tac a = a and b = 1 in pos_mod_sign)
   1.534 +apply (cut_tac [2] a = a and b = 1 in pos_mod_bound)
   1.535 +apply (auto simp del:pos_mod_bound pos_mod_sign)
   1.536 +done
   1.537 +
   1.538 +lemma zdiv_1 [simp]: "a div (1::int) = a"
   1.539 +by (cut_tac a = a and b = 1 in zmod_zdiv_equality, auto)
   1.540 +
   1.541 +lemma zmod_minus1_right [simp]: "a mod (-1::int) = 0"
   1.542 +apply (cut_tac a = a and b = "-1" in neg_mod_sign)
   1.543 +apply (cut_tac [2] a = a and b = "-1" in neg_mod_bound)
   1.544 +apply (auto simp del: neg_mod_sign neg_mod_bound)
   1.545 +done
   1.546 +
   1.547 +lemma zdiv_minus1_right [simp]: "a div (-1::int) = -a"
   1.548 +by (cut_tac a = a and b = "-1" in zmod_zdiv_equality, auto)
   1.549 +
   1.550 +(** The last remaining special cases for constant arithmetic:
   1.551 +    1 div z and 1 mod z **)
   1.552 +
   1.553 +lemmas div_pos_pos_1_number_of [simp] =
   1.554 +    div_pos_pos [OF int_0_less_1, of "number_of w", standard]
   1.555 +
   1.556 +lemmas div_pos_neg_1_number_of [simp] =
   1.557 +    div_pos_neg [OF int_0_less_1, of "number_of w", standard]
   1.558 +
   1.559 +lemmas mod_pos_pos_1_number_of [simp] =
   1.560 +    mod_pos_pos [OF int_0_less_1, of "number_of w", standard]
   1.561 +
   1.562 +lemmas mod_pos_neg_1_number_of [simp] =
   1.563 +    mod_pos_neg [OF int_0_less_1, of "number_of w", standard]
   1.564 +
   1.565 +
   1.566 +lemmas posDivAlg_eqn_1_number_of [simp] =
   1.567 +    posDivAlg_eqn [of concl: 1 "number_of w", standard]
   1.568 +
   1.569 +lemmas negDivAlg_eqn_1_number_of [simp] =
   1.570 +    negDivAlg_eqn [of concl: 1 "number_of w", standard]
   1.571 +
   1.572 +
   1.573 +
   1.574 +subsection{*Monotonicity in the First Argument (Dividend)*}
   1.575 +
   1.576 +lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
   1.577 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   1.578 +apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   1.579 +apply (rule unique_quotient_lemma)
   1.580 +apply (erule subst)
   1.581 +apply (erule subst, simp_all)
   1.582 +done
   1.583 +
   1.584 +lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
   1.585 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   1.586 +apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   1.587 +apply (rule unique_quotient_lemma_neg)
   1.588 +apply (erule subst)
   1.589 +apply (erule subst, simp_all)
   1.590 +done
   1.591 +
   1.592 +
   1.593 +subsection{*Monotonicity in the Second Argument (Divisor)*}
   1.594 +
   1.595 +lemma q_pos_lemma:
   1.596 +     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
   1.597 +apply (subgoal_tac "0 < b'* (q' + 1) ")
   1.598 + apply (simp add: zero_less_mult_iff)
   1.599 +apply (simp add: right_distrib)
   1.600 +done
   1.601 +
   1.602 +lemma zdiv_mono2_lemma:
   1.603 +     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
   1.604 +         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
   1.605 +      ==> q \<le> (q'::int)"
   1.606 +apply (frule q_pos_lemma, assumption+) 
   1.607 +apply (subgoal_tac "b*q < b* (q' + 1) ")
   1.608 + apply (simp add: mult_less_cancel_left)
   1.609 +apply (subgoal_tac "b*q = r' - r + b'*q'")
   1.610 + prefer 2 apply simp
   1.611 +apply (simp (no_asm_simp) add: right_distrib)
   1.612 +apply (subst add_commute, rule zadd_zless_mono, arith)
   1.613 +apply (rule mult_right_mono, auto)
   1.614 +done
   1.615 +
   1.616 +lemma zdiv_mono2:
   1.617 +     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
   1.618 +apply (subgoal_tac "b \<noteq> 0")
   1.619 + prefer 2 apply arith
   1.620 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   1.621 +apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   1.622 +apply (rule zdiv_mono2_lemma)
   1.623 +apply (erule subst)
   1.624 +apply (erule subst, simp_all)
   1.625 +done
   1.626 +
   1.627 +lemma q_neg_lemma:
   1.628 +     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
   1.629 +apply (subgoal_tac "b'*q' < 0")
   1.630 + apply (simp add: mult_less_0_iff, arith)
   1.631 +done
   1.632 +
   1.633 +lemma zdiv_mono2_neg_lemma:
   1.634 +     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
   1.635 +         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
   1.636 +      ==> q' \<le> (q::int)"
   1.637 +apply (frule q_neg_lemma, assumption+) 
   1.638 +apply (subgoal_tac "b*q' < b* (q + 1) ")
   1.639 + apply (simp add: mult_less_cancel_left)
   1.640 +apply (simp add: right_distrib)
   1.641 +apply (subgoal_tac "b*q' \<le> b'*q'")
   1.642 + prefer 2 apply (simp add: mult_right_mono_neg, arith)
   1.643 +done
   1.644 +
   1.645 +lemma zdiv_mono2_neg:
   1.646 +     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
   1.647 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   1.648 +apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   1.649 +apply (rule zdiv_mono2_neg_lemma)
   1.650 +apply (erule subst)
   1.651 +apply (erule subst, simp_all)
   1.652 +done
   1.653 +
   1.654 +subsection{*More Algebraic Laws for div and mod*}
   1.655 +
   1.656 +text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
   1.657 +
   1.658 +lemma zmult1_lemma:
   1.659 +     "[| quorem((b,c),(q,r));  c \<noteq> 0 |]  
   1.660 +      ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
   1.661 +by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)
   1.662 +
   1.663 +lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
   1.664 +apply (case_tac "c = 0", simp)
   1.665 +apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_div])
   1.666 +done
   1.667 +
   1.668 +lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"
   1.669 +apply (case_tac "c = 0", simp)
   1.670 +apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_mod])
   1.671 +done
   1.672 +
   1.673 +lemma zmod_zmult1_eq': "(a*b) mod (c::int) = ((a mod c) * b) mod c"
   1.674 +apply (rule trans)
   1.675 +apply (rule_tac s = "b*a mod c" in trans)
   1.676 +apply (rule_tac [2] zmod_zmult1_eq)
   1.677 +apply (simp_all add: mult_commute)
   1.678 +done
   1.679 +
   1.680 +lemma zmod_zmult_distrib: "(a*b) mod (c::int) = ((a mod c) * (b mod c)) mod c"
   1.681 +apply (rule zmod_zmult1_eq' [THEN trans])
   1.682 +apply (rule zmod_zmult1_eq)
   1.683 +done
   1.684 +
   1.685 +lemma zdiv_zmult_self1 [simp]: "b \<noteq> (0::int) ==> (a*b) div b = a"
   1.686 +by (simp add: zdiv_zmult1_eq)
   1.687 +
   1.688 +lemma zdiv_zmult_self2 [simp]: "b \<noteq> (0::int) ==> (b*a) div b = a"
   1.689 +by (subst mult_commute, erule zdiv_zmult_self1)
   1.690 +
   1.691 +lemma zmod_zmult_self1 [simp]: "(a*b) mod b = (0::int)"
   1.692 +by (simp add: zmod_zmult1_eq)
   1.693 +
   1.694 +lemma zmod_zmult_self2 [simp]: "(b*a) mod b = (0::int)"
   1.695 +by (simp add: mult_commute zmod_zmult1_eq)
   1.696 +
   1.697 +lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
   1.698 +proof
   1.699 +  assume "m mod d = 0"
   1.700 +  with zmod_zdiv_equality[of m d] show "EX q::int. m = d*q" by auto
   1.701 +next
   1.702 +  assume "EX q::int. m = d*q"
   1.703 +  thus "m mod d = 0" by auto
   1.704 +qed
   1.705 +
   1.706 +lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
   1.707 +
   1.708 +text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
   1.709 +
   1.710 +lemma zadd1_lemma:
   1.711 +     "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  c \<noteq> 0 |]  
   1.712 +      ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
   1.713 +by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)
   1.714 +
   1.715 +(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   1.716 +lemma zdiv_zadd1_eq:
   1.717 +     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
   1.718 +apply (case_tac "c = 0", simp)
   1.719 +apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_div)
   1.720 +done
   1.721 +
   1.722 +lemma zmod_zadd1_eq: "(a+b) mod (c::int) = (a mod c + b mod c) mod c"
   1.723 +apply (case_tac "c = 0", simp)
   1.724 +apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_mod)
   1.725 +done
   1.726 +
   1.727 +lemma mod_div_trivial [simp]: "(a mod b) div b = (0::int)"
   1.728 +apply (case_tac "b = 0", simp)
   1.729 +apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial)
   1.730 +done
   1.731 +
   1.732 +lemma mod_mod_trivial [simp]: "(a mod b) mod b = a mod (b::int)"
   1.733 +apply (case_tac "b = 0", simp)
   1.734 +apply (force simp add: linorder_neq_iff mod_pos_pos_trivial mod_neg_neg_trivial)
   1.735 +done
   1.736 +
   1.737 +lemma zmod_zadd_left_eq: "(a+b) mod (c::int) = ((a mod c) + b) mod c"
   1.738 +apply (rule trans [symmetric])
   1.739 +apply (rule zmod_zadd1_eq, simp)
   1.740 +apply (rule zmod_zadd1_eq [symmetric])
   1.741 +done
   1.742 +
   1.743 +lemma zmod_zadd_right_eq: "(a+b) mod (c::int) = (a + (b mod c)) mod c"
   1.744 +apply (rule trans [symmetric])
   1.745 +apply (rule zmod_zadd1_eq, simp)
   1.746 +apply (rule zmod_zadd1_eq [symmetric])
   1.747 +done
   1.748 +
   1.749 +lemma zdiv_zadd_self1[simp]: "a \<noteq> (0::int) ==> (a+b) div a = b div a + 1"
   1.750 +by (simp add: zdiv_zadd1_eq)
   1.751 +
   1.752 +lemma zdiv_zadd_self2[simp]: "a \<noteq> (0::int) ==> (b+a) div a = b div a + 1"
   1.753 +by (simp add: zdiv_zadd1_eq)
   1.754 +
   1.755 +lemma zmod_zadd_self1[simp]: "(a+b) mod a = b mod (a::int)"
   1.756 +apply (case_tac "a = 0", simp)
   1.757 +apply (simp add: zmod_zadd1_eq)
   1.758 +done
   1.759 +
   1.760 +lemma zmod_zadd_self2[simp]: "(b+a) mod a = b mod (a::int)"
   1.761 +apply (case_tac "a = 0", simp)
   1.762 +apply (simp add: zmod_zadd1_eq)
   1.763 +done
   1.764 +
   1.765 +
   1.766 +subsection{*Proving  @{term "a div (b*c) = (a div b) div c"} *}
   1.767 +
   1.768 +(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
   1.769 +  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
   1.770 +  to cause particular problems.*)
   1.771 +
   1.772 +text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
   1.773 +
   1.774 +lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
   1.775 +apply (subgoal_tac "b * (c - q mod c) < r * 1")
   1.776 +apply (simp add: right_diff_distrib)
   1.777 +apply (rule order_le_less_trans)
   1.778 +apply (erule_tac [2] mult_strict_right_mono)
   1.779 +apply (rule mult_left_mono_neg)
   1.780 +apply (auto simp add: compare_rls add_commute [of 1]
   1.781 +                      add1_zle_eq pos_mod_bound)
   1.782 +done
   1.783 +
   1.784 +lemma zmult2_lemma_aux2:
   1.785 +     "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
   1.786 +apply (subgoal_tac "b * (q mod c) \<le> 0")
   1.787 + apply arith
   1.788 +apply (simp add: mult_le_0_iff)
   1.789 +done
   1.790 +
   1.791 +lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
   1.792 +apply (subgoal_tac "0 \<le> b * (q mod c) ")
   1.793 +apply arith
   1.794 +apply (simp add: zero_le_mult_iff)
   1.795 +done
   1.796 +
   1.797 +lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
   1.798 +apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
   1.799 +apply (simp add: right_diff_distrib)
   1.800 +apply (rule order_less_le_trans)
   1.801 +apply (erule mult_strict_right_mono)
   1.802 +apply (rule_tac [2] mult_left_mono)
   1.803 +apply (auto simp add: compare_rls add_commute [of 1]
   1.804 +                      add1_zle_eq pos_mod_bound)
   1.805 +done
   1.806 +
   1.807 +lemma zmult2_lemma: "[| quorem ((a,b), (q,r));  b \<noteq> 0;  0 < c |]  
   1.808 +      ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
   1.809 +by (auto simp add: mult_ac quorem_def linorder_neq_iff
   1.810 +                   zero_less_mult_iff right_distrib [symmetric] 
   1.811 +                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4)
   1.812 +
   1.813 +lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
   1.814 +apply (case_tac "b = 0", simp)
   1.815 +apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_div])
   1.816 +done
   1.817 +
   1.818 +lemma zmod_zmult2_eq:
   1.819 +     "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
   1.820 +apply (case_tac "b = 0", simp)
   1.821 +apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_mod])
   1.822 +done
   1.823 +
   1.824 +
   1.825 +subsection{*Cancellation of Common Factors in div*}
   1.826 +
   1.827 +lemma zdiv_zmult_zmult1_aux1:
   1.828 +     "[| (0::int) < b;  c \<noteq> 0 |] ==> (c*a) div (c*b) = a div b"
   1.829 +by (subst zdiv_zmult2_eq, auto)
   1.830 +
   1.831 +lemma zdiv_zmult_zmult1_aux2:
   1.832 +     "[| b < (0::int);  c \<noteq> 0 |] ==> (c*a) div (c*b) = a div b"
   1.833 +apply (subgoal_tac " (c * (-a)) div (c * (-b)) = (-a) div (-b) ")
   1.834 +apply (rule_tac [2] zdiv_zmult_zmult1_aux1, auto)
   1.835 +done
   1.836 +
   1.837 +lemma zdiv_zmult_zmult1: "c \<noteq> (0::int) ==> (c*a) div (c*b) = a div b"
   1.838 +apply (case_tac "b = 0", simp)
   1.839 +apply (auto simp add: linorder_neq_iff zdiv_zmult_zmult1_aux1 zdiv_zmult_zmult1_aux2)
   1.840 +done
   1.841 +
   1.842 +lemma zdiv_zmult_zmult2: "c \<noteq> (0::int) ==> (a*c) div (b*c) = a div b"
   1.843 +apply (drule zdiv_zmult_zmult1)
   1.844 +apply (auto simp add: mult_commute)
   1.845 +done
   1.846 +
   1.847 +
   1.848 +
   1.849 +subsection{*Distribution of Factors over mod*}
   1.850 +
   1.851 +lemma zmod_zmult_zmult1_aux1:
   1.852 +     "[| (0::int) < b;  c \<noteq> 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"
   1.853 +by (subst zmod_zmult2_eq, auto)
   1.854 +
   1.855 +lemma zmod_zmult_zmult1_aux2:
   1.856 +     "[| b < (0::int);  c \<noteq> 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"
   1.857 +apply (subgoal_tac " (c * (-a)) mod (c * (-b)) = c * ((-a) mod (-b))")
   1.858 +apply (rule_tac [2] zmod_zmult_zmult1_aux1, auto)
   1.859 +done
   1.860 +
   1.861 +lemma zmod_zmult_zmult1: "(c*a) mod (c*b) = (c::int) * (a mod b)"
   1.862 +apply (case_tac "b = 0", simp)
   1.863 +apply (case_tac "c = 0", simp)
   1.864 +apply (auto simp add: linorder_neq_iff zmod_zmult_zmult1_aux1 zmod_zmult_zmult1_aux2)
   1.865 +done
   1.866 +
   1.867 +lemma zmod_zmult_zmult2: "(a*c) mod (b*c) = (a mod b) * (c::int)"
   1.868 +apply (cut_tac c = c in zmod_zmult_zmult1)
   1.869 +apply (auto simp add: mult_commute)
   1.870 +done
   1.871 +
   1.872 +
   1.873 +subsection {*Splitting Rules for div and mod*}
   1.874 +
   1.875 +text{*The proofs of the two lemmas below are essentially identical*}
   1.876 +
   1.877 +lemma split_pos_lemma:
   1.878 + "0<k ==> 
   1.879 +    P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
   1.880 +apply (rule iffI, clarify)
   1.881 + apply (erule_tac P="P ?x ?y" in rev_mp)  
   1.882 + apply (subst zmod_zadd1_eq) 
   1.883 + apply (subst zdiv_zadd1_eq) 
   1.884 + apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
   1.885 +txt{*converse direction*}
   1.886 +apply (drule_tac x = "n div k" in spec) 
   1.887 +apply (drule_tac x = "n mod k" in spec, simp)
   1.888 +done
   1.889 +
   1.890 +lemma split_neg_lemma:
   1.891 + "k<0 ==>
   1.892 +    P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
   1.893 +apply (rule iffI, clarify)
   1.894 + apply (erule_tac P="P ?x ?y" in rev_mp)  
   1.895 + apply (subst zmod_zadd1_eq) 
   1.896 + apply (subst zdiv_zadd1_eq) 
   1.897 + apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
   1.898 +txt{*converse direction*}
   1.899 +apply (drule_tac x = "n div k" in spec) 
   1.900 +apply (drule_tac x = "n mod k" in spec, simp)
   1.901 +done
   1.902 +
   1.903 +lemma split_zdiv:
   1.904 + "P(n div k :: int) =
   1.905 +  ((k = 0 --> P 0) & 
   1.906 +   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
   1.907 +   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
   1.908 +apply (case_tac "k=0", simp)
   1.909 +apply (simp only: linorder_neq_iff)
   1.910 +apply (erule disjE) 
   1.911 + apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
   1.912 +                      split_neg_lemma [of concl: "%x y. P x"])
   1.913 +done
   1.914 +
   1.915 +lemma split_zmod:
   1.916 + "P(n mod k :: int) =
   1.917 +  ((k = 0 --> P n) & 
   1.918 +   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
   1.919 +   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
   1.920 +apply (case_tac "k=0", simp)
   1.921 +apply (simp only: linorder_neq_iff)
   1.922 +apply (erule disjE) 
   1.923 + apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
   1.924 +                      split_neg_lemma [of concl: "%x y. P y"])
   1.925 +done
   1.926 +
   1.927 +(* Enable arith to deal with div 2 and mod 2: *)
   1.928 +declare split_zdiv [of _ _ "number_of k", simplified, standard, arith_split]
   1.929 +declare split_zmod [of _ _ "number_of k", simplified, standard, arith_split]
   1.930 +
   1.931 +
   1.932 +subsection{*Speeding up the Division Algorithm with Shifting*}
   1.933 +
   1.934 +text{*computing div by shifting *}
   1.935 +
   1.936 +lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
   1.937 +proof cases
   1.938 +  assume "a=0"
   1.939 +    thus ?thesis by simp
   1.940 +next
   1.941 +  assume "a\<noteq>0" and le_a: "0\<le>a"   
   1.942 +  hence a_pos: "1 \<le> a" by arith
   1.943 +  hence one_less_a2: "1 < 2*a" by arith
   1.944 +  hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
   1.945 +    by (simp add: mult_le_cancel_left add_commute [of 1] add1_zle_eq)
   1.946 +  with a_pos have "0 \<le> b mod a" by simp
   1.947 +  hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
   1.948 +    by (simp add: mod_pos_pos_trivial one_less_a2)
   1.949 +  with  le_2a
   1.950 +  have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
   1.951 +    by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2
   1.952 +                  right_distrib) 
   1.953 +  thus ?thesis
   1.954 +    by (subst zdiv_zadd1_eq,
   1.955 +        simp add: zdiv_zmult_zmult1 zmod_zmult_zmult1 one_less_a2
   1.956 +                  div_pos_pos_trivial)
   1.957 +qed
   1.958 +
   1.959 +lemma neg_zdiv_mult_2: "a \<le> (0::int) ==> (1 + 2*b) div (2*a) = (b+1) div a"
   1.960 +apply (subgoal_tac " (1 + 2* (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a) ")
   1.961 +apply (rule_tac [2] pos_zdiv_mult_2)
   1.962 +apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)
   1.963 +apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
   1.964 +apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric],
   1.965 +       simp) 
   1.966 +done
   1.967 +
   1.968 +
   1.969 +(*Not clear why this must be proved separately; probably number_of causes
   1.970 +  simplification problems*)
   1.971 +lemma not_0_le_lemma: "~ 0 \<le> x ==> x \<le> (0::int)"
   1.972 +by auto
   1.973 +
   1.974 +lemma zdiv_number_of_BIT[simp]:
   1.975 +     "number_of (v BIT b) div number_of (w BIT bit.B0) =  
   1.976 +          (if b=bit.B0 | (0::int) \<le> number_of w                    
   1.977 +           then number_of v div (number_of w)     
   1.978 +           else (number_of v + (1::int)) div (number_of w))"
   1.979 +apply (simp only: number_of_eq numeral_simps UNIV_I split: split_if) 
   1.980 +apply (simp add: zdiv_zmult_zmult1 pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac 
   1.981 +            split: bit.split)
   1.982 +done
   1.983 +
   1.984 +
   1.985 +subsection{*Computing mod by Shifting (proofs resemble those for div)*}
   1.986 +
   1.987 +lemma pos_zmod_mult_2:
   1.988 +     "(0::int) \<le> a ==> (1 + 2*b) mod (2*a) = 1 + 2 * (b mod a)"
   1.989 +apply (case_tac "a = 0", simp)
   1.990 +apply (subgoal_tac "1 < a * 2")
   1.991 + prefer 2 apply arith
   1.992 +apply (subgoal_tac "2* (1 + b mod a) \<le> 2*a")
   1.993 + apply (rule_tac [2] mult_left_mono)
   1.994 +apply (auto simp add: add_commute [of 1] mult_commute add1_zle_eq 
   1.995 +                      pos_mod_bound)
   1.996 +apply (subst zmod_zadd1_eq)
   1.997 +apply (simp add: zmod_zmult_zmult2 mod_pos_pos_trivial)
   1.998 +apply (rule mod_pos_pos_trivial)
   1.999 +apply (auto simp add: mod_pos_pos_trivial left_distrib)
  1.1000 +apply (subgoal_tac "0 \<le> b mod a", arith, simp)
  1.1001 +done
  1.1002 +
  1.1003 +lemma neg_zmod_mult_2:
  1.1004 +     "a \<le> (0::int) ==> (1 + 2*b) mod (2*a) = 2 * ((b+1) mod a) - 1"
  1.1005 +apply (subgoal_tac "(1 + 2* (-b - 1)) mod (2* (-a)) = 
  1.1006 +                    1 + 2* ((-b - 1) mod (-a))")
  1.1007 +apply (rule_tac [2] pos_zmod_mult_2)
  1.1008 +apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)
  1.1009 +apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
  1.1010 + prefer 2 apply simp 
  1.1011 +apply (simp only: zmod_zminus_zminus diff_minus minus_add_distrib [symmetric])
  1.1012 +done
  1.1013 +
  1.1014 +lemma zmod_number_of_BIT [simp]:
  1.1015 +     "number_of (v BIT b) mod number_of (w BIT bit.B0) =  
  1.1016 +      (case b of
  1.1017 +          bit.B0 => 2 * (number_of v mod number_of w)
  1.1018 +        | bit.B1 => if (0::int) \<le> number_of w  
  1.1019 +                then 2 * (number_of v mod number_of w) + 1     
  1.1020 +                else 2 * ((number_of v + (1::int)) mod number_of w) - 1)"
  1.1021 +apply (simp only: number_of_eq numeral_simps UNIV_I split: bit.split) 
  1.1022 +apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 
  1.1023 +                 not_0_le_lemma neg_zmod_mult_2 add_ac)
  1.1024 +done
  1.1025 +
  1.1026 +
  1.1027 +subsection{*Quotients of Signs*}
  1.1028 +
  1.1029 +lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
  1.1030 +apply (subgoal_tac "a div b \<le> -1", force)
  1.1031 +apply (rule order_trans)
  1.1032 +apply (rule_tac a' = "-1" in zdiv_mono1)
  1.1033 +apply (auto simp add: zdiv_minus1)
  1.1034 +done
  1.1035 +
  1.1036 +lemma div_nonneg_neg_le0: "[| (0::int) \<le> a;  b < 0 |] ==> a div b \<le> 0"
  1.1037 +by (drule zdiv_mono1_neg, auto)
  1.1038 +
  1.1039 +lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
  1.1040 +apply auto
  1.1041 +apply (drule_tac [2] zdiv_mono1)
  1.1042 +apply (auto simp add: linorder_neq_iff)
  1.1043 +apply (simp (no_asm_use) add: linorder_not_less [symmetric])
  1.1044 +apply (blast intro: div_neg_pos_less0)
  1.1045 +done
  1.1046 +
  1.1047 +lemma neg_imp_zdiv_nonneg_iff:
  1.1048 +     "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
  1.1049 +apply (subst zdiv_zminus_zminus [symmetric])
  1.1050 +apply (subst pos_imp_zdiv_nonneg_iff, auto)
  1.1051 +done
  1.1052 +
  1.1053 +(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
  1.1054 +lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
  1.1055 +by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
  1.1056 +
  1.1057 +(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
  1.1058 +lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
  1.1059 +by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
  1.1060 +
  1.1061 +
  1.1062 +subsection {* The Divides Relation *}
  1.1063 +
  1.1064 +lemma zdvd_iff_zmod_eq_0: "(m dvd n) = (n mod m = (0::int))"
  1.1065 +by(simp add:dvd_def zmod_eq_0_iff)
  1.1066 +
  1.1067 +lemmas zdvd_iff_zmod_eq_0_number_of [simp] =
  1.1068 +  zdvd_iff_zmod_eq_0 [of "number_of x" "number_of y", standard]
  1.1069 +
  1.1070 +lemma zdvd_0_right [iff]: "(m::int) dvd 0"
  1.1071 +by (simp add: dvd_def)
  1.1072 +
  1.1073 +lemma zdvd_0_left [iff]: "(0 dvd (m::int)) = (m = 0)"
  1.1074 +  by (simp add: dvd_def)
  1.1075 +
  1.1076 +lemma zdvd_1_left [iff]: "1 dvd (m::int)"
  1.1077 +  by (simp add: dvd_def)
  1.1078 +
  1.1079 +lemma zdvd_refl [simp]: "m dvd (m::int)"
  1.1080 +by (auto simp add: dvd_def intro: zmult_1_right [symmetric])
  1.1081 +
  1.1082 +lemma zdvd_trans: "m dvd n ==> n dvd k ==> m dvd (k::int)"
  1.1083 +by (auto simp add: dvd_def intro: mult_assoc)
  1.1084 +
  1.1085 +lemma zdvd_zminus_iff: "(m dvd -n) = (m dvd (n::int))"
  1.1086 +  apply (simp add: dvd_def, auto)
  1.1087 +   apply (rule_tac [!] x = "-k" in exI, auto)
  1.1088 +  done
  1.1089 +
  1.1090 +lemma zdvd_zminus2_iff: "(-m dvd n) = (m dvd (n::int))"
  1.1091 +  apply (simp add: dvd_def, auto)
  1.1092 +   apply (rule_tac [!] x = "-k" in exI, auto)
  1.1093 +  done
  1.1094 +lemma zdvd_abs1: "( \<bar>i::int\<bar> dvd j) = (i dvd j)" 
  1.1095 +  apply (cases "i > 0", simp)
  1.1096 +  apply (simp add: dvd_def)
  1.1097 +  apply (rule iffI)
  1.1098 +  apply (erule exE)
  1.1099 +  apply (rule_tac x="- k" in exI, simp)
  1.1100 +  apply (erule exE)
  1.1101 +  apply (rule_tac x="- k" in exI, simp)
  1.1102 +  done
  1.1103 +lemma zdvd_abs2: "( (i::int) dvd \<bar>j\<bar>) = (i dvd j)" 
  1.1104 +  apply (cases "j > 0", simp)
  1.1105 +  apply (simp add: dvd_def)
  1.1106 +  apply (rule iffI)
  1.1107 +  apply (erule exE)
  1.1108 +  apply (rule_tac x="- k" in exI, simp)
  1.1109 +  apply (erule exE)
  1.1110 +  apply (rule_tac x="- k" in exI, simp)
  1.1111 +  done
  1.1112 +
  1.1113 +lemma zdvd_anti_sym:
  1.1114 +    "0 < m ==> 0 < n ==> m dvd n ==> n dvd m ==> m = (n::int)"
  1.1115 +  apply (simp add: dvd_def, auto)
  1.1116 +  apply (simp add: mult_assoc zero_less_mult_iff zmult_eq_1_iff)
  1.1117 +  done
  1.1118 +
  1.1119 +lemma zdvd_zadd: "k dvd m ==> k dvd n ==> k dvd (m + n :: int)"
  1.1120 +  apply (simp add: dvd_def)
  1.1121 +  apply (blast intro: right_distrib [symmetric])
  1.1122 +  done
  1.1123 +
  1.1124 +lemma zdvd_dvd_eq: assumes anz:"a \<noteq> 0" and ab: "(a::int) dvd b" and ba:"b dvd a" 
  1.1125 +  shows "\<bar>a\<bar> = \<bar>b\<bar>"
  1.1126 +proof-
  1.1127 +  from ab obtain k where k:"b = a*k" unfolding dvd_def by blast 
  1.1128 +  from ba obtain k' where k':"a = b*k'" unfolding dvd_def by blast 
  1.1129 +  from k k' have "a = a*k*k'" by simp
  1.1130 +  with mult_cancel_left1[where c="a" and b="k*k'"]
  1.1131 +  have kk':"k*k' = 1" using anz by (simp add: mult_assoc)
  1.1132 +  hence "k = 1 \<and> k' = 1 \<or> k = -1 \<and> k' = -1" by (simp add: zmult_eq_1_iff)
  1.1133 +  thus ?thesis using k k' by auto
  1.1134 +qed
  1.1135 +
  1.1136 +lemma zdvd_zdiff: "k dvd m ==> k dvd n ==> k dvd (m - n :: int)"
  1.1137 +  apply (simp add: dvd_def)
  1.1138 +  apply (blast intro: right_diff_distrib [symmetric])
  1.1139 +  done
  1.1140 +
  1.1141 +lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"
  1.1142 +  apply (subgoal_tac "m = n + (m - n)")
  1.1143 +   apply (erule ssubst)
  1.1144 +   apply (blast intro: zdvd_zadd, simp)
  1.1145 +  done
  1.1146 +
  1.1147 +lemma zdvd_zmult: "k dvd (n::int) ==> k dvd m * n"
  1.1148 +  apply (simp add: dvd_def)
  1.1149 +  apply (blast intro: mult_left_commute)
  1.1150 +  done
  1.1151 +
  1.1152 +lemma zdvd_zmult2: "k dvd (m::int) ==> k dvd m * n"
  1.1153 +  apply (subst mult_commute)
  1.1154 +  apply (erule zdvd_zmult)
  1.1155 +  done
  1.1156 +
  1.1157 +lemma zdvd_triv_right [iff]: "(k::int) dvd m * k"
  1.1158 +  apply (rule zdvd_zmult)
  1.1159 +  apply (rule zdvd_refl)
  1.1160 +  done
  1.1161 +
  1.1162 +lemma zdvd_triv_left [iff]: "(k::int) dvd k * m"
  1.1163 +  apply (rule zdvd_zmult2)
  1.1164 +  apply (rule zdvd_refl)
  1.1165 +  done
  1.1166 +
  1.1167 +lemma zdvd_zmultD2: "j * k dvd n ==> j dvd (n::int)"
  1.1168 +  apply (simp add: dvd_def)
  1.1169 +  apply (simp add: mult_assoc, blast)
  1.1170 +  done
  1.1171 +
  1.1172 +lemma zdvd_zmultD: "j * k dvd n ==> k dvd (n::int)"
  1.1173 +  apply (rule zdvd_zmultD2)
  1.1174 +  apply (subst mult_commute, assumption)
  1.1175 +  done
  1.1176 +
  1.1177 +lemma zdvd_zmult_mono: "i dvd m ==> j dvd (n::int) ==> i * j dvd m * n"
  1.1178 +  apply (simp add: dvd_def, clarify)
  1.1179 +  apply (rule_tac x = "k * ka" in exI)
  1.1180 +  apply (simp add: mult_ac)
  1.1181 +  done
  1.1182 +
  1.1183 +lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"
  1.1184 +  apply (rule iffI)
  1.1185 +   apply (erule_tac [2] zdvd_zadd)
  1.1186 +   apply (subgoal_tac "n = (n + k * m) - k * m")
  1.1187 +    apply (erule ssubst)
  1.1188 +    apply (erule zdvd_zdiff, simp_all)
  1.1189 +  done
  1.1190 +
  1.1191 +lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
  1.1192 +  apply (simp add: dvd_def)
  1.1193 +  apply (auto simp add: zmod_zmult_zmult1)
  1.1194 +  done
  1.1195 +
  1.1196 +lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
  1.1197 +  apply (subgoal_tac "k dvd n * (m div n) + m mod n")
  1.1198 +   apply (simp add: zmod_zdiv_equality [symmetric])
  1.1199 +  apply (simp only: zdvd_zadd zdvd_zmult2)
  1.1200 +  done
  1.1201 +
  1.1202 +lemma zdvd_not_zless: "0 < m ==> m < n ==> \<not> n dvd (m::int)"
  1.1203 +  apply (simp add: dvd_def, auto)
  1.1204 +  apply (subgoal_tac "0 < n")
  1.1205 +   prefer 2
  1.1206 +   apply (blast intro: order_less_trans)
  1.1207 +  apply (simp add: zero_less_mult_iff)
  1.1208 +  apply (subgoal_tac "n * k < n * 1")
  1.1209 +   apply (drule mult_less_cancel_left [THEN iffD1], auto)
  1.1210 +  done
  1.1211 +lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  1.1212 +  using zmod_zdiv_equality[where a="m" and b="n"]
  1.1213 +  by (simp add: ring_eq_simps)
  1.1214 +
  1.1215 +lemma zdvd_mult_div_cancel:"(n::int) dvd m \<Longrightarrow> n * (m div n) = m"
  1.1216 +apply (subgoal_tac "m mod n = 0")
  1.1217 + apply (simp add: zmult_div_cancel)
  1.1218 +apply (simp only: zdvd_iff_zmod_eq_0)
  1.1219 +done
  1.1220 +
  1.1221 +lemma zdvd_mult_cancel: assumes d:"k * m dvd k * n" and kz:"k \<noteq> (0::int)"
  1.1222 +  shows "m dvd n"
  1.1223 +proof-
  1.1224 +  from d obtain h where h: "k*n = k*m * h" unfolding dvd_def by blast
  1.1225 +  {assume "n \<noteq> m*h" hence "k* n \<noteq> k* (m*h)" using kz by simp
  1.1226 +    with h have False by (simp add: mult_assoc)}
  1.1227 +  hence "n = m * h" by blast
  1.1228 +  thus ?thesis by blast
  1.1229 +qed
  1.1230 +
  1.1231 +theorem ex_nat: "(\<exists>x::nat. P x) = (\<exists>x::int. 0 <= x \<and> P (nat x))"
  1.1232 +  apply (simp split add: split_nat)
  1.1233 +  apply (rule iffI)
  1.1234 +  apply (erule exE)
  1.1235 +  apply (rule_tac x = "int x" in exI)
  1.1236 +  apply simp
  1.1237 +  apply (erule exE)
  1.1238 +  apply (rule_tac x = "nat x" in exI)
  1.1239 +  apply (erule conjE)
  1.1240 +  apply (erule_tac x = "nat x" in allE)
  1.1241 +  apply simp
  1.1242 +  done
  1.1243 +
  1.1244 +theorem zdvd_int: "(x dvd y) = (int x dvd int y)"
  1.1245 +  apply (simp only: dvd_def ex_nat int_int_eq [symmetric] zmult_int [symmetric]
  1.1246 +    nat_0_le cong add: conj_cong)
  1.1247 +  apply (rule iffI)
  1.1248 +  apply iprover
  1.1249 +  apply (erule exE)
  1.1250 +  apply (case_tac "x=0")
  1.1251 +  apply (rule_tac x=0 in exI)
  1.1252 +  apply simp
  1.1253 +  apply (case_tac "0 \<le> k")
  1.1254 +  apply iprover
  1.1255 +  apply (simp add: linorder_not_le)
  1.1256 +  apply (drule mult_strict_left_mono_neg [OF iffD2 [OF zero_less_int_conv]])
  1.1257 +  apply assumption
  1.1258 +  apply (simp add: mult_ac)
  1.1259 +  done
  1.1260 +
  1.1261 +lemma zdvd1_eq[simp]: "(x::int) dvd 1 = ( \<bar>x\<bar> = 1)"
  1.1262 +proof
  1.1263 +  assume d: "x dvd 1" hence "int (nat \<bar>x\<bar>) dvd int (nat 1)" by (simp add: zdvd_abs1)
  1.1264 +  hence "nat \<bar>x\<bar> dvd 1" by (simp add: zdvd_int)
  1.1265 +  hence "nat \<bar>x\<bar> = 1"  by simp
  1.1266 +  thus "\<bar>x\<bar> = 1" by (cases "x < 0", auto)
  1.1267 +next
  1.1268 +  assume "\<bar>x\<bar>=1" thus "x dvd 1" 
  1.1269 +    by(cases "x < 0",simp_all add: minus_equation_iff zdvd_iff_zmod_eq_0)
  1.1270 +qed
  1.1271 +lemma zdvd_mult_cancel1: 
  1.1272 +  assumes mp:"m \<noteq>(0::int)" shows "(m * n dvd m) = (\<bar>n\<bar> = 1)"
  1.1273 +proof
  1.1274 +  assume n1: "\<bar>n\<bar> = 1" thus "m * n dvd m" 
  1.1275 +    by (cases "n >0", auto simp add: zdvd_zminus2_iff minus_equation_iff)
  1.1276 +next
  1.1277 +  assume H: "m * n dvd m" hence H2: "m * n dvd m * 1" by simp
  1.1278 +  from zdvd_mult_cancel[OF H2 mp] show "\<bar>n\<bar> = 1" by (simp only: zdvd1_eq)
  1.1279 +qed
  1.1280 +
  1.1281 +lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"
  1.1282 +  apply (auto simp add: dvd_def nat_abs_mult_distrib)
  1.1283 +  apply (auto simp add: nat_eq_iff abs_if split add: split_if_asm)
  1.1284 +   apply (rule_tac x = "-(int k)" in exI)
  1.1285 +  apply (auto simp add: int_mult)
  1.1286 +  done
  1.1287 +
  1.1288 +lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"
  1.1289 +  apply (auto simp add: dvd_def abs_if int_mult)
  1.1290 +    apply (rule_tac [3] x = "nat k" in exI)
  1.1291 +    apply (rule_tac [2] x = "-(int k)" in exI)
  1.1292 +    apply (rule_tac x = "nat (-k)" in exI)
  1.1293 +    apply (cut_tac [3] k = m in int_less_0_conv)
  1.1294 +    apply (cut_tac k = m in int_less_0_conv)
  1.1295 +    apply (auto simp add: zero_le_mult_iff mult_less_0_iff
  1.1296 +      nat_mult_distrib [symmetric] nat_eq_iff2)
  1.1297 +  done
  1.1298 +
  1.1299 +lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \<le> z then (z dvd int m) else m = 0)"
  1.1300 +  apply (auto simp add: dvd_def int_mult)
  1.1301 +  apply (rule_tac x = "nat k" in exI)
  1.1302 +  apply (cut_tac k = m in int_less_0_conv)
  1.1303 +  apply (auto simp add: zero_le_mult_iff mult_less_0_iff
  1.1304 +    nat_mult_distrib [symmetric] nat_eq_iff2)
  1.1305 +  done
  1.1306 +
  1.1307 +lemma zminus_dvd_iff [iff]: "(-z dvd w) = (z dvd (w::int))"
  1.1308 +  apply (auto simp add: dvd_def)
  1.1309 +   apply (rule_tac [!] x = "-k" in exI, auto)
  1.1310 +  done
  1.1311 +
  1.1312 +lemma dvd_zminus_iff [iff]: "(z dvd -w) = (z dvd (w::int))"
  1.1313 +  apply (auto simp add: dvd_def)
  1.1314 +   apply (drule minus_equation_iff [THEN iffD1])
  1.1315 +   apply (rule_tac [!] x = "-k" in exI, auto)
  1.1316 +  done
  1.1317 +
  1.1318 +lemma zdvd_imp_le: "[| z dvd n; 0 < n |] ==> z \<le> (n::int)"
  1.1319 +  apply (rule_tac z=n in int_cases)
  1.1320 +  apply (auto simp add: dvd_int_iff) 
  1.1321 +  apply (rule_tac z=z in int_cases) 
  1.1322 +  apply (auto simp add: dvd_imp_le) 
  1.1323 +  done
  1.1324 +
  1.1325 +
  1.1326 +subsection{*Integer Powers*} 
  1.1327 +
  1.1328 +instance int :: power ..
  1.1329 +
  1.1330 +primrec
  1.1331 +  "p ^ 0 = 1"
  1.1332 +  "p ^ (Suc n) = (p::int) * (p ^ n)"
  1.1333 +
  1.1334 +
  1.1335 +instance int :: recpower
  1.1336 +proof
  1.1337 +  fix z :: int
  1.1338 +  fix n :: nat
  1.1339 +  show "z^0 = 1" by simp
  1.1340 +  show "z^(Suc n) = z * (z^n)" by simp
  1.1341 +qed
  1.1342 +
  1.1343 +
  1.1344 +lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"
  1.1345 +apply (induct "y", auto)
  1.1346 +apply (rule zmod_zmult1_eq [THEN trans])
  1.1347 +apply (simp (no_asm_simp))
  1.1348 +apply (rule zmod_zmult_distrib [symmetric])
  1.1349 +done
  1.1350 +
  1.1351 +lemma zpower_zadd_distrib: "x^(y+z) = ((x^y)*(x^z)::int)"
  1.1352 +  by (rule Power.power_add)
  1.1353 +
  1.1354 +lemma zpower_zpower: "(x^y)^z = (x^(y*z)::int)"
  1.1355 +  by (rule Power.power_mult [symmetric])
  1.1356 +
  1.1357 +lemma zero_less_zpower_abs_iff [simp]:
  1.1358 +     "(0 < (abs x)^n) = (x \<noteq> (0::int) | n=0)"
  1.1359 +apply (induct "n")
  1.1360 +apply (auto simp add: zero_less_mult_iff)
  1.1361 +done
  1.1362 +
  1.1363 +lemma zero_le_zpower_abs [simp]: "(0::int) <= (abs x)^n"
  1.1364 +apply (induct "n")
  1.1365 +apply (auto simp add: zero_le_mult_iff)
  1.1366 +done
  1.1367 +
  1.1368 +lemma int_power: "int (m^n) = (int m) ^ n"
  1.1369 +  by (induct n, simp_all add: int_mult)
  1.1370 +
  1.1371 +text{*Compatibility binding*}
  1.1372 +lemmas zpower_int = int_power [symmetric]
  1.1373 +
  1.1374 +lemma zdiv_int: "int (a div b) = (int a) div (int b)"
  1.1375 +apply (subst split_div, auto)
  1.1376 +apply (subst split_zdiv, auto)
  1.1377 +apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
  1.1378 +apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
  1.1379 +done
  1.1380 +
  1.1381 +lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
  1.1382 +apply (subst split_mod, auto)
  1.1383 +apply (subst split_zmod, auto)
  1.1384 +apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
  1.1385 +       in unique_remainder)
  1.1386 +apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
  1.1387 +done
  1.1388 +
  1.1389 +text{*Suggested by Matthias Daum*}
  1.1390 +lemma int_power_div_base:
  1.1391 +     "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
  1.1392 +apply (subgoal_tac "k ^ m = k ^ ((m - 1) + 1)")
  1.1393 + apply (erule ssubst)
  1.1394 + apply (simp only: power_add)
  1.1395 + apply simp_all
  1.1396 +done
  1.1397 +
  1.1398 +text {* code serializer setup *}
  1.1399 +
  1.1400 +code_modulename SML
  1.1401 +  IntDiv Integer
  1.1402 +
  1.1403 +code_modulename OCaml
  1.1404 +  IntDiv Integer
  1.1405 +
  1.1406 +code_modulename Haskell
  1.1407 +  IntDiv Divides
  1.1408 +
  1.1409 +end