combined former theories Divides and IntDiv to one theory Divides
authorhaftmann
Fri Oct 30 14:00:43 2009 +0100 (2009-10-30)
changeset 333611f18de40b43f
parent 33360 f7d9c5e5d2f9
child 33362 85adf83af7ce
combined former theories Divides and IntDiv to one theory Divides
NEWS
src/HOL/Divides.thy
src/HOL/Groebner_Basis.thy
src/HOL/IntDiv.thy
src/HOL/Library/Numeral_Type.thy
     1.1 --- a/NEWS	Fri Oct 30 13:59:52 2009 +0100
     1.2 +++ b/NEWS	Fri Oct 30 14:00:43 2009 +0100
     1.3 @@ -37,6 +37,11 @@
     1.4  
     1.5  *** HOL ***
     1.6  
     1.7 +* Combined former theories Divides and IntDiv to one theory Divides
     1.8 +in the spirit of other number theory theories in HOL;  some constants
     1.9 +(and to a lesser extent also facts) have been suffixed with _nat und _int
    1.10 +respectively.  INCOMPATIBILITY.
    1.11 +
    1.12  * Most rules produced by inductive and datatype package
    1.13  have mandatory prefixes.
    1.14  INCOMPATIBILITY.
     2.1 --- a/src/HOL/Divides.thy	Fri Oct 30 13:59:52 2009 +0100
     2.2 +++ b/src/HOL/Divides.thy	Fri Oct 30 14:00:43 2009 +0100
     2.3 @@ -634,6 +634,11 @@
     2.4  
     2.5  end
     2.6  
     2.7 +lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
     2.8 +  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
     2.9 +by (simp add: divmod_nat_zero divmod_nat_base divmod_nat_step)
    2.10 +    (simp add: divmod_nat_div_mod)
    2.11 +
    2.12  text {* Simproc for cancelling @{const div} and @{const mod} *}
    2.13  
    2.14  ML {*
    2.15 @@ -666,22 +671,6 @@
    2.16  end
    2.17  *}
    2.18  
    2.19 -text {* code generator setup *}
    2.20 -
    2.21 -lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
    2.22 -  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
    2.23 -by (simp add: divmod_nat_zero divmod_nat_base divmod_nat_step)
    2.24 -    (simp add: divmod_nat_div_mod)
    2.25 -
    2.26 -code_modulename SML
    2.27 -  Divides Nat
    2.28 -
    2.29 -code_modulename OCaml
    2.30 -  Divides Nat
    2.31 -
    2.32 -code_modulename Haskell
    2.33 -  Divides Nat
    2.34 -
    2.35  
    2.36  subsubsection {* Quotient *}
    2.37  
    2.38 @@ -1136,4 +1125,1339 @@
    2.39      Suc_mod_eq_add3_mod [of _ "number_of v", standard]
    2.40  declare Suc_mod_eq_add3_mod_number_of [simp]
    2.41  
    2.42 +
    2.43 +lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
    2.44 +apply (induct "m")
    2.45 +apply (simp_all add: mod_Suc)
    2.46 +done
    2.47 +
    2.48 +declare Suc_times_mod_eq [of "number_of w", standard, simp]
    2.49 +
    2.50 +lemma [simp]: "n div k \<le> (Suc n) div k"
    2.51 +by (simp add: div_le_mono) 
    2.52 +
    2.53 +lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
    2.54 +by (cases n) simp_all
    2.55 +
    2.56 +lemma div_2_gt_zero [simp]: "(1::nat) < n ==> 0 < n div 2" 
    2.57 +using Suc_n_div_2_gt_zero [of "n - 1"] by simp
    2.58 +
    2.59 +  (* Potential use of algebra : Equality modulo n*)
    2.60 +lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
    2.61 +by (simp add: mult_ac add_ac)
    2.62 +
    2.63 +lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
    2.64 +proof -
    2.65 +  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
    2.66 +  also have "... = Suc m mod n" by (rule mod_mult_self3) 
    2.67 +  finally show ?thesis .
    2.68 +qed
    2.69 +
    2.70 +lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
    2.71 +apply (subst mod_Suc [of m]) 
    2.72 +apply (subst mod_Suc [of "m mod n"], simp) 
    2.73 +done
    2.74 +
    2.75 +
    2.76 +subsection {* Division on @{typ int} *}
    2.77 +
    2.78 +definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
    2.79 +    --{*definition of quotient and remainder*}
    2.80 +    [code]: "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
    2.81 +               (if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0))"
    2.82 +
    2.83 +definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
    2.84 +    --{*for the division algorithm*}
    2.85 +    [code]: "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
    2.86 +                         else (2 * q, r))"
    2.87 +
    2.88 +text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
    2.89 +function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
    2.90 +  "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
    2.91 +     else adjust b (posDivAlg a (2 * b)))"
    2.92 +by auto
    2.93 +termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
    2.94 +  (auto simp add: mult_2)
    2.95 +
    2.96 +text{*algorithm for the case @{text "a<0, b>0"}*}
    2.97 +function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
    2.98 +  "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
    2.99 +     else adjust b (negDivAlg a (2 * b)))"
   2.100 +by auto
   2.101 +termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
   2.102 +  (auto simp add: mult_2)
   2.103 +
   2.104 +text{*algorithm for the general case @{term "b\<noteq>0"}*}
   2.105 +definition negateSnd :: "int \<times> int \<Rightarrow> int \<times> int" where
   2.106 +  [code_unfold]: "negateSnd = apsnd uminus"
   2.107 +
   2.108 +definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
   2.109 +    --{*The full division algorithm considers all possible signs for a, b
   2.110 +       including the special case @{text "a=0, b<0"} because 
   2.111 +       @{term negDivAlg} requires @{term "a<0"}.*}
   2.112 +  "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
   2.113 +                  else if a = 0 then (0, 0)
   2.114 +                       else negateSnd (negDivAlg (-a) (-b))
   2.115 +               else 
   2.116 +                  if 0 < b then negDivAlg a b
   2.117 +                  else negateSnd (posDivAlg (-a) (-b)))"
   2.118 +
   2.119 +instantiation int :: Divides.div
   2.120 +begin
   2.121 +
   2.122 +definition
   2.123 +  "a div b = fst (divmod_int a b)"
   2.124 +
   2.125 +definition
   2.126 + "a mod b = snd (divmod_int a b)"
   2.127 +
   2.128 +instance ..
   2.129 +
   2.130  end
   2.131 +
   2.132 +lemma divmod_int_mod_div:
   2.133 +  "divmod_int p q = (p div q, p mod q)"
   2.134 +  by (auto simp add: div_int_def mod_int_def)
   2.135 +
   2.136 +text{*
   2.137 +Here is the division algorithm in ML:
   2.138 +
   2.139 +\begin{verbatim}
   2.140 +    fun posDivAlg (a,b) =
   2.141 +      if a<b then (0,a)
   2.142 +      else let val (q,r) = posDivAlg(a, 2*b)
   2.143 +               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
   2.144 +           end
   2.145 +
   2.146 +    fun negDivAlg (a,b) =
   2.147 +      if 0\<le>a+b then (~1,a+b)
   2.148 +      else let val (q,r) = negDivAlg(a, 2*b)
   2.149 +               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
   2.150 +           end;
   2.151 +
   2.152 +    fun negateSnd (q,r:int) = (q,~r);
   2.153 +
   2.154 +    fun divmod (a,b) = if 0\<le>a then 
   2.155 +                          if b>0 then posDivAlg (a,b) 
   2.156 +                           else if a=0 then (0,0)
   2.157 +                                else negateSnd (negDivAlg (~a,~b))
   2.158 +                       else 
   2.159 +                          if 0<b then negDivAlg (a,b)
   2.160 +                          else        negateSnd (posDivAlg (~a,~b));
   2.161 +\end{verbatim}
   2.162 +*}
   2.163 +
   2.164 +
   2.165 +
   2.166 +subsubsection{*Uniqueness and Monotonicity of Quotients and Remainders*}
   2.167 +
   2.168 +lemma unique_quotient_lemma:
   2.169 +     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
   2.170 +      ==> q' \<le> (q::int)"
   2.171 +apply (subgoal_tac "r' + b * (q'-q) \<le> r")
   2.172 + prefer 2 apply (simp add: right_diff_distrib)
   2.173 +apply (subgoal_tac "0 < b * (1 + q - q') ")
   2.174 +apply (erule_tac [2] order_le_less_trans)
   2.175 + prefer 2 apply (simp add: right_diff_distrib right_distrib)
   2.176 +apply (subgoal_tac "b * q' < b * (1 + q) ")
   2.177 + prefer 2 apply (simp add: right_diff_distrib right_distrib)
   2.178 +apply (simp add: mult_less_cancel_left)
   2.179 +done
   2.180 +
   2.181 +lemma unique_quotient_lemma_neg:
   2.182 +     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
   2.183 +      ==> q \<le> (q'::int)"
   2.184 +by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
   2.185 +    auto)
   2.186 +
   2.187 +lemma unique_quotient:
   2.188 +     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r');  b \<noteq> 0 |]  
   2.189 +      ==> q = q'"
   2.190 +apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
   2.191 +apply (blast intro: order_antisym
   2.192 +             dest: order_eq_refl [THEN unique_quotient_lemma] 
   2.193 +             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
   2.194 +done
   2.195 +
   2.196 +
   2.197 +lemma unique_remainder:
   2.198 +     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r');  b \<noteq> 0 |]  
   2.199 +      ==> r = r'"
   2.200 +apply (subgoal_tac "q = q'")
   2.201 + apply (simp add: divmod_int_rel_def)
   2.202 +apply (blast intro: unique_quotient)
   2.203 +done
   2.204 +
   2.205 +
   2.206 +subsubsection{*Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends*}
   2.207 +
   2.208 +text{*And positive divisors*}
   2.209 +
   2.210 +lemma adjust_eq [simp]:
   2.211 +     "adjust b (q,r) = 
   2.212 +      (let diff = r-b in  
   2.213 +        if 0 \<le> diff then (2*q + 1, diff)   
   2.214 +                     else (2*q, r))"
   2.215 +by (simp add: Let_def adjust_def)
   2.216 +
   2.217 +declare posDivAlg.simps [simp del]
   2.218 +
   2.219 +text{*use with a simproc to avoid repeatedly proving the premise*}
   2.220 +lemma posDivAlg_eqn:
   2.221 +     "0 < b ==>  
   2.222 +      posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
   2.223 +by (rule posDivAlg.simps [THEN trans], simp)
   2.224 +
   2.225 +text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
   2.226 +theorem posDivAlg_correct:
   2.227 +  assumes "0 \<le> a" and "0 < b"
   2.228 +  shows "divmod_int_rel a b (posDivAlg a b)"
   2.229 +using prems apply (induct a b rule: posDivAlg.induct)
   2.230 +apply auto
   2.231 +apply (simp add: divmod_int_rel_def)
   2.232 +apply (subst posDivAlg_eqn, simp add: right_distrib)
   2.233 +apply (case_tac "a < b")
   2.234 +apply simp_all
   2.235 +apply (erule splitE)
   2.236 +apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
   2.237 +done
   2.238 +
   2.239 +
   2.240 +subsubsection{*Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends*}
   2.241 +
   2.242 +text{*And positive divisors*}
   2.243 +
   2.244 +declare negDivAlg.simps [simp del]
   2.245 +
   2.246 +text{*use with a simproc to avoid repeatedly proving the premise*}
   2.247 +lemma negDivAlg_eqn:
   2.248 +     "0 < b ==>  
   2.249 +      negDivAlg a b =       
   2.250 +       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
   2.251 +by (rule negDivAlg.simps [THEN trans], simp)
   2.252 +
   2.253 +(*Correctness of negDivAlg: it computes quotients correctly
   2.254 +  It doesn't work if a=0 because the 0/b equals 0, not -1*)
   2.255 +lemma negDivAlg_correct:
   2.256 +  assumes "a < 0" and "b > 0"
   2.257 +  shows "divmod_int_rel a b (negDivAlg a b)"
   2.258 +using prems apply (induct a b rule: negDivAlg.induct)
   2.259 +apply (auto simp add: linorder_not_le)
   2.260 +apply (simp add: divmod_int_rel_def)
   2.261 +apply (subst negDivAlg_eqn, assumption)
   2.262 +apply (case_tac "a + b < (0\<Colon>int)")
   2.263 +apply simp_all
   2.264 +apply (erule splitE)
   2.265 +apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
   2.266 +done
   2.267 +
   2.268 +
   2.269 +subsubsection{*Existence Shown by Proving the Division Algorithm to be Correct*}
   2.270 +
   2.271 +(*the case a=0*)
   2.272 +lemma divmod_int_rel_0: "b \<noteq> 0 ==> divmod_int_rel 0 b (0, 0)"
   2.273 +by (auto simp add: divmod_int_rel_def linorder_neq_iff)
   2.274 +
   2.275 +lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
   2.276 +by (subst posDivAlg.simps, auto)
   2.277 +
   2.278 +lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
   2.279 +by (subst negDivAlg.simps, auto)
   2.280 +
   2.281 +lemma negateSnd_eq [simp]: "negateSnd(q,r) = (q,-r)"
   2.282 +by (simp add: negateSnd_def)
   2.283 +
   2.284 +lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (negateSnd qr)"
   2.285 +by (auto simp add: split_ifs divmod_int_rel_def)
   2.286 +
   2.287 +lemma divmod_int_correct: "b \<noteq> 0 ==> divmod_int_rel a b (divmod_int a b)"
   2.288 +by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
   2.289 +                    posDivAlg_correct negDivAlg_correct)
   2.290 +
   2.291 +text{*Arbitrary definitions for division by zero.  Useful to simplify 
   2.292 +    certain equations.*}
   2.293 +
   2.294 +lemma DIVISION_BY_ZERO [simp]: "a div (0::int) = 0 & a mod (0::int) = a"
   2.295 +by (simp add: div_int_def mod_int_def divmod_int_def posDivAlg.simps)  
   2.296 +
   2.297 +
   2.298 +text{*Basic laws about division and remainder*}
   2.299 +
   2.300 +lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
   2.301 +apply (case_tac "b = 0", simp)
   2.302 +apply (cut_tac a = a and b = b in divmod_int_correct)
   2.303 +apply (auto simp add: divmod_int_rel_def div_int_def mod_int_def)
   2.304 +done
   2.305 +
   2.306 +lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"
   2.307 +by(simp add: zmod_zdiv_equality[symmetric])
   2.308 +
   2.309 +lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"
   2.310 +by(simp add: mult_commute zmod_zdiv_equality[symmetric])
   2.311 +
   2.312 +text {* Tool setup *}
   2.313 +
   2.314 +ML {*
   2.315 +local
   2.316 +
   2.317 +structure CancelDivMod = CancelDivModFun(struct
   2.318 +
   2.319 +  val div_name = @{const_name div};
   2.320 +  val mod_name = @{const_name mod};
   2.321 +  val mk_binop = HOLogic.mk_binop;
   2.322 +  val mk_sum = Arith_Data.mk_sum HOLogic.intT;
   2.323 +  val dest_sum = Arith_Data.dest_sum;
   2.324 +
   2.325 +  val div_mod_eqs = map mk_meta_eq [@{thm zdiv_zmod_equality}, @{thm zdiv_zmod_equality2}];
   2.326 +
   2.327 +  val trans = trans;
   2.328 +
   2.329 +  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
   2.330 +    (@{thm diff_minus} :: @{thms add_0s} @ @{thms add_ac}))
   2.331 +
   2.332 +end)
   2.333 +
   2.334 +in
   2.335 +
   2.336 +val cancel_div_mod_int_proc = Simplifier.simproc @{theory}
   2.337 +  "cancel_zdiv_zmod" ["(k::int) + l"] (K CancelDivMod.proc);
   2.338 +
   2.339 +val _ = Addsimprocs [cancel_div_mod_int_proc];
   2.340 +
   2.341 +end
   2.342 +*}
   2.343 +
   2.344 +lemma pos_mod_conj : "(0::int) < b ==> 0 \<le> a mod b & a mod b < b"
   2.345 +apply (cut_tac a = a and b = b in divmod_int_correct)
   2.346 +apply (auto simp add: divmod_int_rel_def mod_int_def)
   2.347 +done
   2.348 +
   2.349 +lemmas pos_mod_sign  [simp] = pos_mod_conj [THEN conjunct1, standard]
   2.350 +   and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2, standard]
   2.351 +
   2.352 +lemma neg_mod_conj : "b < (0::int) ==> a mod b \<le> 0 & b < a mod b"
   2.353 +apply (cut_tac a = a and b = b in divmod_int_correct)
   2.354 +apply (auto simp add: divmod_int_rel_def div_int_def mod_int_def)
   2.355 +done
   2.356 +
   2.357 +lemmas neg_mod_sign  [simp] = neg_mod_conj [THEN conjunct1, standard]
   2.358 +   and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2, standard]
   2.359 +
   2.360 +
   2.361 +
   2.362 +subsubsection{*General Properties of div and mod*}
   2.363 +
   2.364 +lemma divmod_int_rel_div_mod: "b \<noteq> 0 ==> divmod_int_rel a b (a div b, a mod b)"
   2.365 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   2.366 +apply (force simp add: divmod_int_rel_def linorder_neq_iff)
   2.367 +done
   2.368 +
   2.369 +lemma divmod_int_rel_div: "[| divmod_int_rel a b (q, r);  b \<noteq> 0 |] ==> a div b = q"
   2.370 +by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
   2.371 +
   2.372 +lemma divmod_int_rel_mod: "[| divmod_int_rel a b (q, r);  b \<noteq> 0 |] ==> a mod b = r"
   2.373 +by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
   2.374 +
   2.375 +lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
   2.376 +apply (rule divmod_int_rel_div)
   2.377 +apply (auto simp add: divmod_int_rel_def)
   2.378 +done
   2.379 +
   2.380 +lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
   2.381 +apply (rule divmod_int_rel_div)
   2.382 +apply (auto simp add: divmod_int_rel_def)
   2.383 +done
   2.384 +
   2.385 +lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
   2.386 +apply (rule divmod_int_rel_div)
   2.387 +apply (auto simp add: divmod_int_rel_def)
   2.388 +done
   2.389 +
   2.390 +(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
   2.391 +
   2.392 +lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
   2.393 +apply (rule_tac q = 0 in divmod_int_rel_mod)
   2.394 +apply (auto simp add: divmod_int_rel_def)
   2.395 +done
   2.396 +
   2.397 +lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
   2.398 +apply (rule_tac q = 0 in divmod_int_rel_mod)
   2.399 +apply (auto simp add: divmod_int_rel_def)
   2.400 +done
   2.401 +
   2.402 +lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
   2.403 +apply (rule_tac q = "-1" in divmod_int_rel_mod)
   2.404 +apply (auto simp add: divmod_int_rel_def)
   2.405 +done
   2.406 +
   2.407 +text{*There is no @{text mod_neg_pos_trivial}.*}
   2.408 +
   2.409 +
   2.410 +(*Simpler laws such as -a div b = -(a div b) FAIL, but see just below*)
   2.411 +lemma zdiv_zminus_zminus [simp]: "(-a) div (-b) = a div (b::int)"
   2.412 +apply (case_tac "b = 0", simp)
   2.413 +apply (simp add: divmod_int_rel_div_mod [THEN divmod_int_rel_neg, simplified, 
   2.414 +                                 THEN divmod_int_rel_div, THEN sym])
   2.415 +
   2.416 +done
   2.417 +
   2.418 +(*Simpler laws such as -a mod b = -(a mod b) FAIL, but see just below*)
   2.419 +lemma zmod_zminus_zminus [simp]: "(-a) mod (-b) = - (a mod (b::int))"
   2.420 +apply (case_tac "b = 0", simp)
   2.421 +apply (subst divmod_int_rel_div_mod [THEN divmod_int_rel_neg, simplified, THEN divmod_int_rel_mod],
   2.422 +       auto)
   2.423 +done
   2.424 +
   2.425 +
   2.426 +subsubsection{*Laws for div and mod with Unary Minus*}
   2.427 +
   2.428 +lemma zminus1_lemma:
   2.429 +     "divmod_int_rel a b (q, r)
   2.430 +      ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
   2.431 +                          if r=0 then 0 else b-r)"
   2.432 +by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
   2.433 +
   2.434 +
   2.435 +lemma zdiv_zminus1_eq_if:
   2.436 +     "b \<noteq> (0::int)  
   2.437 +      ==> (-a) div b =  
   2.438 +          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   2.439 +by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN divmod_int_rel_div])
   2.440 +
   2.441 +lemma zmod_zminus1_eq_if:
   2.442 +     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
   2.443 +apply (case_tac "b = 0", simp)
   2.444 +apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN divmod_int_rel_mod])
   2.445 +done
   2.446 +
   2.447 +lemma zmod_zminus1_not_zero:
   2.448 +  fixes k l :: int
   2.449 +  shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
   2.450 +  unfolding zmod_zminus1_eq_if by auto
   2.451 +
   2.452 +lemma zdiv_zminus2: "a div (-b) = (-a::int) div b"
   2.453 +by (cut_tac a = "-a" in zdiv_zminus_zminus, auto)
   2.454 +
   2.455 +lemma zmod_zminus2: "a mod (-b) = - ((-a::int) mod b)"
   2.456 +by (cut_tac a = "-a" and b = b in zmod_zminus_zminus, auto)
   2.457 +
   2.458 +lemma zdiv_zminus2_eq_if:
   2.459 +     "b \<noteq> (0::int)  
   2.460 +      ==> a div (-b) =  
   2.461 +          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   2.462 +by (simp add: zdiv_zminus1_eq_if zdiv_zminus2)
   2.463 +
   2.464 +lemma zmod_zminus2_eq_if:
   2.465 +     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
   2.466 +by (simp add: zmod_zminus1_eq_if zmod_zminus2)
   2.467 +
   2.468 +lemma zmod_zminus2_not_zero:
   2.469 +  fixes k l :: int
   2.470 +  shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
   2.471 +  unfolding zmod_zminus2_eq_if by auto 
   2.472 +
   2.473 +
   2.474 +subsubsection{*Division of a Number by Itself*}
   2.475 +
   2.476 +lemma self_quotient_aux1: "[| (0::int) < a; a = r + a*q; r < a |] ==> 1 \<le> q"
   2.477 +apply (subgoal_tac "0 < a*q")
   2.478 + apply (simp add: zero_less_mult_iff, arith)
   2.479 +done
   2.480 +
   2.481 +lemma self_quotient_aux2: "[| (0::int) < a; a = r + a*q; 0 \<le> r |] ==> q \<le> 1"
   2.482 +apply (subgoal_tac "0 \<le> a* (1-q) ")
   2.483 + apply (simp add: zero_le_mult_iff)
   2.484 +apply (simp add: right_diff_distrib)
   2.485 +done
   2.486 +
   2.487 +lemma self_quotient: "[| divmod_int_rel a a (q, r);  a \<noteq> (0::int) |] ==> q = 1"
   2.488 +apply (simp add: split_ifs divmod_int_rel_def linorder_neq_iff)
   2.489 +apply (rule order_antisym, safe, simp_all)
   2.490 +apply (rule_tac [3] a = "-a" and r = "-r" in self_quotient_aux1)
   2.491 +apply (rule_tac a = "-a" and r = "-r" in self_quotient_aux2)
   2.492 +apply (force intro: self_quotient_aux1 self_quotient_aux2 simp add: add_commute)+
   2.493 +done
   2.494 +
   2.495 +lemma self_remainder: "[| divmod_int_rel a a (q, r);  a \<noteq> (0::int) |] ==> r = 0"
   2.496 +apply (frule self_quotient, assumption)
   2.497 +apply (simp add: divmod_int_rel_def)
   2.498 +done
   2.499 +
   2.500 +lemma zdiv_self [simp]: "a \<noteq> 0 ==> a div a = (1::int)"
   2.501 +by (simp add: divmod_int_rel_div_mod [THEN self_quotient])
   2.502 +
   2.503 +(*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)
   2.504 +lemma zmod_self [simp]: "a mod a = (0::int)"
   2.505 +apply (case_tac "a = 0", simp)
   2.506 +apply (simp add: divmod_int_rel_div_mod [THEN self_remainder])
   2.507 +done
   2.508 +
   2.509 +
   2.510 +subsubsection{*Computation of Division and Remainder*}
   2.511 +
   2.512 +lemma zdiv_zero [simp]: "(0::int) div b = 0"
   2.513 +by (simp add: div_int_def divmod_int_def)
   2.514 +
   2.515 +lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
   2.516 +by (simp add: div_int_def divmod_int_def)
   2.517 +
   2.518 +lemma zmod_zero [simp]: "(0::int) mod b = 0"
   2.519 +by (simp add: mod_int_def divmod_int_def)
   2.520 +
   2.521 +lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
   2.522 +by (simp add: mod_int_def divmod_int_def)
   2.523 +
   2.524 +text{*a positive, b positive *}
   2.525 +
   2.526 +lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
   2.527 +by (simp add: div_int_def divmod_int_def)
   2.528 +
   2.529 +lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
   2.530 +by (simp add: mod_int_def divmod_int_def)
   2.531 +
   2.532 +text{*a negative, b positive *}
   2.533 +
   2.534 +lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
   2.535 +by (simp add: div_int_def divmod_int_def)
   2.536 +
   2.537 +lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
   2.538 +by (simp add: mod_int_def divmod_int_def)
   2.539 +
   2.540 +text{*a positive, b negative *}
   2.541 +
   2.542 +lemma div_pos_neg:
   2.543 +     "[| 0 < a;  b < 0 |] ==> a div b = fst (negateSnd (negDivAlg (-a) (-b)))"
   2.544 +by (simp add: div_int_def divmod_int_def)
   2.545 +
   2.546 +lemma mod_pos_neg:
   2.547 +     "[| 0 < a;  b < 0 |] ==> a mod b = snd (negateSnd (negDivAlg (-a) (-b)))"
   2.548 +by (simp add: mod_int_def divmod_int_def)
   2.549 +
   2.550 +text{*a negative, b negative *}
   2.551 +
   2.552 +lemma div_neg_neg:
   2.553 +     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (negateSnd (posDivAlg (-a) (-b)))"
   2.554 +by (simp add: div_int_def divmod_int_def)
   2.555 +
   2.556 +lemma mod_neg_neg:
   2.557 +     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (negateSnd (posDivAlg (-a) (-b)))"
   2.558 +by (simp add: mod_int_def divmod_int_def)
   2.559 +
   2.560 +text {*Simplify expresions in which div and mod combine numerical constants*}
   2.561 +
   2.562 +lemma divmod_int_relI:
   2.563 +  "\<lbrakk>a == b * q + r; if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0\<rbrakk>
   2.564 +    \<Longrightarrow> divmod_int_rel a b (q, r)"
   2.565 +  unfolding divmod_int_rel_def by simp
   2.566 +
   2.567 +lemmas divmod_int_rel_div_eq = divmod_int_relI [THEN divmod_int_rel_div, THEN eq_reflection]
   2.568 +lemmas divmod_int_rel_mod_eq = divmod_int_relI [THEN divmod_int_rel_mod, THEN eq_reflection]
   2.569 +lemmas arithmetic_simps =
   2.570 +  arith_simps
   2.571 +  add_special
   2.572 +  OrderedGroup.add_0_left
   2.573 +  OrderedGroup.add_0_right
   2.574 +  mult_zero_left
   2.575 +  mult_zero_right
   2.576 +  mult_1_left
   2.577 +  mult_1_right
   2.578 +
   2.579 +(* simprocs adapted from HOL/ex/Binary.thy *)
   2.580 +ML {*
   2.581 +local
   2.582 +  val mk_number = HOLogic.mk_number HOLogic.intT;
   2.583 +  fun mk_cert u k l = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"} $
   2.584 +    (@{term "times :: int \<Rightarrow> int \<Rightarrow> int"} $ u $ mk_number k) $
   2.585 +      mk_number l;
   2.586 +  fun prove ctxt prop = Goal.prove ctxt [] [] prop
   2.587 +    (K (ALLGOALS (full_simp_tac (HOL_basic_ss addsimps @{thms arithmetic_simps}))));
   2.588 +  fun binary_proc proc ss ct =
   2.589 +    (case Thm.term_of ct of
   2.590 +      _ $ t $ u =>
   2.591 +      (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of
   2.592 +        SOME args => proc (Simplifier.the_context ss) args
   2.593 +      | NONE => NONE)
   2.594 +    | _ => NONE);
   2.595 +in
   2.596 +  fun divmod_proc rule = binary_proc (fn ctxt => fn ((m, t), (n, u)) =>
   2.597 +    if n = 0 then NONE
   2.598 +    else let val (k, l) = Integer.div_mod m n;
   2.599 +    in SOME (rule OF [prove ctxt (Logic.mk_equals (t, mk_cert u k l))]) end);
   2.600 +end
   2.601 +*}
   2.602 +
   2.603 +simproc_setup binary_int_div ("number_of m div number_of n :: int") =
   2.604 +  {* K (divmod_proc (@{thm divmod_int_rel_div_eq})) *}
   2.605 +
   2.606 +simproc_setup binary_int_mod ("number_of m mod number_of n :: int") =
   2.607 +  {* K (divmod_proc (@{thm divmod_int_rel_mod_eq})) *}
   2.608 +
   2.609 +lemmas posDivAlg_eqn_number_of [simp] =
   2.610 +    posDivAlg_eqn [of "number_of v" "number_of w", standard]
   2.611 +
   2.612 +lemmas negDivAlg_eqn_number_of [simp] =
   2.613 +    negDivAlg_eqn [of "number_of v" "number_of w", standard]
   2.614 +
   2.615 +
   2.616 +text{*Special-case simplification *}
   2.617 +
   2.618 +lemma zmod_minus1_right [simp]: "a mod (-1::int) = 0"
   2.619 +apply (cut_tac a = a and b = "-1" in neg_mod_sign)
   2.620 +apply (cut_tac [2] a = a and b = "-1" in neg_mod_bound)
   2.621 +apply (auto simp del: neg_mod_sign neg_mod_bound)
   2.622 +done
   2.623 +
   2.624 +lemma zdiv_minus1_right [simp]: "a div (-1::int) = -a"
   2.625 +by (cut_tac a = a and b = "-1" in zmod_zdiv_equality, auto)
   2.626 +
   2.627 +(** The last remaining special cases for constant arithmetic:
   2.628 +    1 div z and 1 mod z **)
   2.629 +
   2.630 +lemmas div_pos_pos_1_number_of [simp] =
   2.631 +    div_pos_pos [OF int_0_less_1, of "number_of w", standard]
   2.632 +
   2.633 +lemmas div_pos_neg_1_number_of [simp] =
   2.634 +    div_pos_neg [OF int_0_less_1, of "number_of w", standard]
   2.635 +
   2.636 +lemmas mod_pos_pos_1_number_of [simp] =
   2.637 +    mod_pos_pos [OF int_0_less_1, of "number_of w", standard]
   2.638 +
   2.639 +lemmas mod_pos_neg_1_number_of [simp] =
   2.640 +    mod_pos_neg [OF int_0_less_1, of "number_of w", standard]
   2.641 +
   2.642 +
   2.643 +lemmas posDivAlg_eqn_1_number_of [simp] =
   2.644 +    posDivAlg_eqn [of concl: 1 "number_of w", standard]
   2.645 +
   2.646 +lemmas negDivAlg_eqn_1_number_of [simp] =
   2.647 +    negDivAlg_eqn [of concl: 1 "number_of w", standard]
   2.648 +
   2.649 +
   2.650 +
   2.651 +subsubsection{*Monotonicity in the First Argument (Dividend)*}
   2.652 +
   2.653 +lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
   2.654 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   2.655 +apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   2.656 +apply (rule unique_quotient_lemma)
   2.657 +apply (erule subst)
   2.658 +apply (erule subst, simp_all)
   2.659 +done
   2.660 +
   2.661 +lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
   2.662 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   2.663 +apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   2.664 +apply (rule unique_quotient_lemma_neg)
   2.665 +apply (erule subst)
   2.666 +apply (erule subst, simp_all)
   2.667 +done
   2.668 +
   2.669 +
   2.670 +subsubsection{*Monotonicity in the Second Argument (Divisor)*}
   2.671 +
   2.672 +lemma q_pos_lemma:
   2.673 +     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
   2.674 +apply (subgoal_tac "0 < b'* (q' + 1) ")
   2.675 + apply (simp add: zero_less_mult_iff)
   2.676 +apply (simp add: right_distrib)
   2.677 +done
   2.678 +
   2.679 +lemma zdiv_mono2_lemma:
   2.680 +     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
   2.681 +         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
   2.682 +      ==> q \<le> (q'::int)"
   2.683 +apply (frule q_pos_lemma, assumption+) 
   2.684 +apply (subgoal_tac "b*q < b* (q' + 1) ")
   2.685 + apply (simp add: mult_less_cancel_left)
   2.686 +apply (subgoal_tac "b*q = r' - r + b'*q'")
   2.687 + prefer 2 apply simp
   2.688 +apply (simp (no_asm_simp) add: right_distrib)
   2.689 +apply (subst add_commute, rule zadd_zless_mono, arith)
   2.690 +apply (rule mult_right_mono, auto)
   2.691 +done
   2.692 +
   2.693 +lemma zdiv_mono2:
   2.694 +     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
   2.695 +apply (subgoal_tac "b \<noteq> 0")
   2.696 + prefer 2 apply arith
   2.697 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   2.698 +apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   2.699 +apply (rule zdiv_mono2_lemma)
   2.700 +apply (erule subst)
   2.701 +apply (erule subst, simp_all)
   2.702 +done
   2.703 +
   2.704 +lemma q_neg_lemma:
   2.705 +     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
   2.706 +apply (subgoal_tac "b'*q' < 0")
   2.707 + apply (simp add: mult_less_0_iff, arith)
   2.708 +done
   2.709 +
   2.710 +lemma zdiv_mono2_neg_lemma:
   2.711 +     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
   2.712 +         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
   2.713 +      ==> q' \<le> (q::int)"
   2.714 +apply (frule q_neg_lemma, assumption+) 
   2.715 +apply (subgoal_tac "b*q' < b* (q + 1) ")
   2.716 + apply (simp add: mult_less_cancel_left)
   2.717 +apply (simp add: right_distrib)
   2.718 +apply (subgoal_tac "b*q' \<le> b'*q'")
   2.719 + prefer 2 apply (simp add: mult_right_mono_neg, arith)
   2.720 +done
   2.721 +
   2.722 +lemma zdiv_mono2_neg:
   2.723 +     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
   2.724 +apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   2.725 +apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   2.726 +apply (rule zdiv_mono2_neg_lemma)
   2.727 +apply (erule subst)
   2.728 +apply (erule subst, simp_all)
   2.729 +done
   2.730 +
   2.731 +
   2.732 +subsubsection{*More Algebraic Laws for div and mod*}
   2.733 +
   2.734 +text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
   2.735 +
   2.736 +lemma zmult1_lemma:
   2.737 +     "[| divmod_int_rel b c (q, r);  c \<noteq> 0 |]  
   2.738 +      ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
   2.739 +by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib mult_ac)
   2.740 +
   2.741 +lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
   2.742 +apply (case_tac "c = 0", simp)
   2.743 +apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN divmod_int_rel_div])
   2.744 +done
   2.745 +
   2.746 +lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"
   2.747 +apply (case_tac "c = 0", simp)
   2.748 +apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN divmod_int_rel_mod])
   2.749 +done
   2.750 +
   2.751 +lemma zmod_zdiv_trivial: "(a mod b) div b = (0::int)"
   2.752 +apply (case_tac "b = 0", simp)
   2.753 +apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial)
   2.754 +done
   2.755 +
   2.756 +text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
   2.757 +
   2.758 +lemma zadd1_lemma:
   2.759 +     "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br);  c \<noteq> 0 |]  
   2.760 +      ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
   2.761 +by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib)
   2.762 +
   2.763 +(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   2.764 +lemma zdiv_zadd1_eq:
   2.765 +     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
   2.766 +apply (case_tac "c = 0", simp)
   2.767 +apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] divmod_int_rel_div)
   2.768 +done
   2.769 +
   2.770 +instance int :: ring_div
   2.771 +proof
   2.772 +  fix a b c :: int
   2.773 +  assume not0: "b \<noteq> 0"
   2.774 +  show "(a + c * b) div b = c + a div b"
   2.775 +    unfolding zdiv_zadd1_eq [of a "c * b"] using not0 
   2.776 +      by (simp add: zmod_zmult1_eq zmod_zdiv_trivial zdiv_zmult1_eq)
   2.777 +next
   2.778 +  fix a b c :: int
   2.779 +  assume "a \<noteq> 0"
   2.780 +  then show "(a * b) div (a * c) = b div c"
   2.781 +  proof (cases "b \<noteq> 0 \<and> c \<noteq> 0")
   2.782 +    case False then show ?thesis by auto
   2.783 +  next
   2.784 +    case True then have "b \<noteq> 0" and "c \<noteq> 0" by auto
   2.785 +    with `a \<noteq> 0`
   2.786 +    have "\<And>q r. divmod_int_rel b c (q, r) \<Longrightarrow> divmod_int_rel (a * b) (a * c) (q, a * r)"
   2.787 +      apply (auto simp add: divmod_int_rel_def) 
   2.788 +      apply (auto simp add: algebra_simps)
   2.789 +      apply (auto simp add: zero_less_mult_iff zero_le_mult_iff mult_le_0_iff mult_commute [of a] mult_less_cancel_right)
   2.790 +      done
   2.791 +    moreover with `c \<noteq> 0` divmod_int_rel_div_mod have "divmod_int_rel b c (b div c, b mod c)" by auto
   2.792 +    ultimately have "divmod_int_rel (a * b) (a * c) (b div c, a * (b mod c))" .
   2.793 +    moreover from  `a \<noteq> 0` `c \<noteq> 0` have "a * c \<noteq> 0" by simp
   2.794 +    ultimately show ?thesis by (rule divmod_int_rel_div)
   2.795 +  qed
   2.796 +qed auto
   2.797 +
   2.798 +lemma posDivAlg_div_mod:
   2.799 +  assumes "k \<ge> 0"
   2.800 +  and "l \<ge> 0"
   2.801 +  shows "posDivAlg k l = (k div l, k mod l)"
   2.802 +proof (cases "l = 0")
   2.803 +  case True then show ?thesis by (simp add: posDivAlg.simps)
   2.804 +next
   2.805 +  case False with assms posDivAlg_correct
   2.806 +    have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
   2.807 +    by simp
   2.808 +  from divmod_int_rel_div [OF this `l \<noteq> 0`] divmod_int_rel_mod [OF this `l \<noteq> 0`]
   2.809 +  show ?thesis by simp
   2.810 +qed
   2.811 +
   2.812 +lemma negDivAlg_div_mod:
   2.813 +  assumes "k < 0"
   2.814 +  and "l > 0"
   2.815 +  shows "negDivAlg k l = (k div l, k mod l)"
   2.816 +proof -
   2.817 +  from assms have "l \<noteq> 0" by simp
   2.818 +  from assms negDivAlg_correct
   2.819 +    have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
   2.820 +    by simp
   2.821 +  from divmod_int_rel_div [OF this `l \<noteq> 0`] divmod_int_rel_mod [OF this `l \<noteq> 0`]
   2.822 +  show ?thesis by simp
   2.823 +qed
   2.824 +
   2.825 +lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
   2.826 +by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   2.827 +
   2.828 +(* REVISIT: should this be generalized to all semiring_div types? *)
   2.829 +lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
   2.830 +
   2.831 +
   2.832 +subsubsection{*Proving  @{term "a div (b*c) = (a div b) div c"} *}
   2.833 +
   2.834 +(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
   2.835 +  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
   2.836 +  to cause particular problems.*)
   2.837 +
   2.838 +text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
   2.839 +
   2.840 +lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
   2.841 +apply (subgoal_tac "b * (c - q mod c) < r * 1")
   2.842 + apply (simp add: algebra_simps)
   2.843 +apply (rule order_le_less_trans)
   2.844 + apply (erule_tac [2] mult_strict_right_mono)
   2.845 + apply (rule mult_left_mono_neg)
   2.846 +  using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps pos_mod_bound)
   2.847 + apply (simp)
   2.848 +apply (simp)
   2.849 +done
   2.850 +
   2.851 +lemma zmult2_lemma_aux2:
   2.852 +     "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
   2.853 +apply (subgoal_tac "b * (q mod c) \<le> 0")
   2.854 + apply arith
   2.855 +apply (simp add: mult_le_0_iff)
   2.856 +done
   2.857 +
   2.858 +lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
   2.859 +apply (subgoal_tac "0 \<le> b * (q mod c) ")
   2.860 +apply arith
   2.861 +apply (simp add: zero_le_mult_iff)
   2.862 +done
   2.863 +
   2.864 +lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
   2.865 +apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
   2.866 + apply (simp add: right_diff_distrib)
   2.867 +apply (rule order_less_le_trans)
   2.868 + apply (erule mult_strict_right_mono)
   2.869 + apply (rule_tac [2] mult_left_mono)
   2.870 +  apply simp
   2.871 + using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps pos_mod_bound)
   2.872 +apply simp
   2.873 +done
   2.874 +
   2.875 +lemma zmult2_lemma: "[| divmod_int_rel a b (q, r);  b \<noteq> 0;  0 < c |]  
   2.876 +      ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
   2.877 +by (auto simp add: mult_ac divmod_int_rel_def linorder_neq_iff
   2.878 +                   zero_less_mult_iff right_distrib [symmetric] 
   2.879 +                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4)
   2.880 +
   2.881 +lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
   2.882 +apply (case_tac "b = 0", simp)
   2.883 +apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN divmod_int_rel_div])
   2.884 +done
   2.885 +
   2.886 +lemma zmod_zmult2_eq:
   2.887 +     "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
   2.888 +apply (case_tac "b = 0", simp)
   2.889 +apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN divmod_int_rel_mod])
   2.890 +done
   2.891 +
   2.892 +
   2.893 +subsubsection {*Splitting Rules for div and mod*}
   2.894 +
   2.895 +text{*The proofs of the two lemmas below are essentially identical*}
   2.896 +
   2.897 +lemma split_pos_lemma:
   2.898 + "0<k ==> 
   2.899 +    P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
   2.900 +apply (rule iffI, clarify)
   2.901 + apply (erule_tac P="P ?x ?y" in rev_mp)  
   2.902 + apply (subst mod_add_eq) 
   2.903 + apply (subst zdiv_zadd1_eq) 
   2.904 + apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
   2.905 +txt{*converse direction*}
   2.906 +apply (drule_tac x = "n div k" in spec) 
   2.907 +apply (drule_tac x = "n mod k" in spec, simp)
   2.908 +done
   2.909 +
   2.910 +lemma split_neg_lemma:
   2.911 + "k<0 ==>
   2.912 +    P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
   2.913 +apply (rule iffI, clarify)
   2.914 + apply (erule_tac P="P ?x ?y" in rev_mp)  
   2.915 + apply (subst mod_add_eq) 
   2.916 + apply (subst zdiv_zadd1_eq) 
   2.917 + apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
   2.918 +txt{*converse direction*}
   2.919 +apply (drule_tac x = "n div k" in spec) 
   2.920 +apply (drule_tac x = "n mod k" in spec, simp)
   2.921 +done
   2.922 +
   2.923 +lemma split_zdiv:
   2.924 + "P(n div k :: int) =
   2.925 +  ((k = 0 --> P 0) & 
   2.926 +   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
   2.927 +   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
   2.928 +apply (case_tac "k=0", simp)
   2.929 +apply (simp only: linorder_neq_iff)
   2.930 +apply (erule disjE) 
   2.931 + apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
   2.932 +                      split_neg_lemma [of concl: "%x y. P x"])
   2.933 +done
   2.934 +
   2.935 +lemma split_zmod:
   2.936 + "P(n mod k :: int) =
   2.937 +  ((k = 0 --> P n) & 
   2.938 +   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
   2.939 +   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
   2.940 +apply (case_tac "k=0", simp)
   2.941 +apply (simp only: linorder_neq_iff)
   2.942 +apply (erule disjE) 
   2.943 + apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
   2.944 +                      split_neg_lemma [of concl: "%x y. P y"])
   2.945 +done
   2.946 +
   2.947 +(* Enable arith to deal with div 2 and mod 2: *)
   2.948 +declare split_zdiv [of _ _ "number_of k", simplified, standard, arith_split]
   2.949 +declare split_zmod [of _ _ "number_of k", simplified, standard, arith_split]
   2.950 +
   2.951 +
   2.952 +subsubsection{*Speeding up the Division Algorithm with Shifting*}
   2.953 +
   2.954 +text{*computing div by shifting *}
   2.955 +
   2.956 +lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
   2.957 +proof cases
   2.958 +  assume "a=0"
   2.959 +    thus ?thesis by simp
   2.960 +next
   2.961 +  assume "a\<noteq>0" and le_a: "0\<le>a"   
   2.962 +  hence a_pos: "1 \<le> a" by arith
   2.963 +  hence one_less_a2: "1 < 2 * a" by arith
   2.964 +  hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
   2.965 +    unfolding mult_le_cancel_left
   2.966 +    by (simp add: add1_zle_eq add_commute [of 1])
   2.967 +  with a_pos have "0 \<le> b mod a" by simp
   2.968 +  hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
   2.969 +    by (simp add: mod_pos_pos_trivial one_less_a2)
   2.970 +  with  le_2a
   2.971 +  have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
   2.972 +    by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2
   2.973 +                  right_distrib) 
   2.974 +  thus ?thesis
   2.975 +    by (subst zdiv_zadd1_eq,
   2.976 +        simp add: mod_mult_mult1 one_less_a2
   2.977 +                  div_pos_pos_trivial)
   2.978 +qed
   2.979 +
   2.980 +lemma neg_zdiv_mult_2: "a \<le> (0::int) ==> (1 + 2*b) div (2*a) = (b+1) div a"
   2.981 +apply (subgoal_tac " (1 + 2* (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a) ")
   2.982 +apply (rule_tac [2] pos_zdiv_mult_2)
   2.983 +apply (auto simp add: right_diff_distrib)
   2.984 +apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
   2.985 +apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric])
   2.986 +apply (simp_all add: algebra_simps)
   2.987 +apply (simp only: ab_diff_minus minus_add_distrib [symmetric] number_of_Min zdiv_zminus_zminus)
   2.988 +done
   2.989 +
   2.990 +lemma zdiv_number_of_Bit0 [simp]:
   2.991 +     "number_of (Int.Bit0 v) div number_of (Int.Bit0 w) =  
   2.992 +          number_of v div (number_of w :: int)"
   2.993 +by (simp only: number_of_eq numeral_simps) (simp add: mult_2 [symmetric])
   2.994 +
   2.995 +lemma zdiv_number_of_Bit1 [simp]:
   2.996 +     "number_of (Int.Bit1 v) div number_of (Int.Bit0 w) =  
   2.997 +          (if (0::int) \<le> number_of w                    
   2.998 +           then number_of v div (number_of w)     
   2.999 +           else (number_of v + (1::int)) div (number_of w))"
  2.1000 +apply (simp only: number_of_eq numeral_simps UNIV_I split: split_if) 
  2.1001 +apply (simp add: pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac mult_2 [symmetric])
  2.1002 +done
  2.1003 +
  2.1004 +
  2.1005 +subsubsection{*Computing mod by Shifting (proofs resemble those for div)*}
  2.1006 +
  2.1007 +lemma pos_zmod_mult_2:
  2.1008 +  fixes a b :: int
  2.1009 +  assumes "0 \<le> a"
  2.1010 +  shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
  2.1011 +proof (cases "0 < a")
  2.1012 +  case False with assms show ?thesis by simp
  2.1013 +next
  2.1014 +  case True
  2.1015 +  then have "b mod a < a" by (rule pos_mod_bound)
  2.1016 +  then have "1 + b mod a \<le> a" by simp
  2.1017 +  then have A: "2 * (1 + b mod a) \<le> 2 * a" by simp
  2.1018 +  from `0 < a` have "0 \<le> b mod a" by (rule pos_mod_sign)
  2.1019 +  then have B: "0 \<le> 1 + 2 * (b mod a)" by simp
  2.1020 +  have "((1\<Colon>int) mod ((2\<Colon>int) * a) + (2\<Colon>int) * b mod ((2\<Colon>int) * a)) mod ((2\<Colon>int) * a) = (1\<Colon>int) + (2\<Colon>int) * (b mod a)"
  2.1021 +    using `0 < a` and A
  2.1022 +    by (auto simp add: mod_mult_mult1 mod_pos_pos_trivial ring_distribs intro!: mod_pos_pos_trivial B)
  2.1023 +  then show ?thesis by (subst mod_add_eq)
  2.1024 +qed
  2.1025 +
  2.1026 +lemma neg_zmod_mult_2:
  2.1027 +  fixes a b :: int
  2.1028 +  assumes "a \<le> 0"
  2.1029 +  shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
  2.1030 +proof -
  2.1031 +  from assms have "0 \<le> - a" by auto
  2.1032 +  then have "(1 + 2 * (- b - 1)) mod (2 * (- a)) = 1 + 2 * ((- b - 1) mod (- a))"
  2.1033 +    by (rule pos_zmod_mult_2)
  2.1034 +  then show ?thesis by (simp add: zmod_zminus2 algebra_simps)
  2.1035 +     (simp add: diff_minus add_ac)
  2.1036 +qed
  2.1037 +
  2.1038 +lemma zmod_number_of_Bit0 [simp]:
  2.1039 +     "number_of (Int.Bit0 v) mod number_of (Int.Bit0 w) =  
  2.1040 +      (2::int) * (number_of v mod number_of w)"
  2.1041 +apply (simp only: number_of_eq numeral_simps) 
  2.1042 +apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  2.1043 +                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  2.1044 +done
  2.1045 +
  2.1046 +lemma zmod_number_of_Bit1 [simp]:
  2.1047 +     "number_of (Int.Bit1 v) mod number_of (Int.Bit0 w) =  
  2.1048 +      (if (0::int) \<le> number_of w  
  2.1049 +                then 2 * (number_of v mod number_of w) + 1     
  2.1050 +                else 2 * ((number_of v + (1::int)) mod number_of w) - 1)"
  2.1051 +apply (simp only: number_of_eq numeral_simps) 
  2.1052 +apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  2.1053 +                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  2.1054 +done
  2.1055 +
  2.1056 +
  2.1057 +subsubsection{*Quotients of Signs*}
  2.1058 +
  2.1059 +lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
  2.1060 +apply (subgoal_tac "a div b \<le> -1", force)
  2.1061 +apply (rule order_trans)
  2.1062 +apply (rule_tac a' = "-1" in zdiv_mono1)
  2.1063 +apply (auto simp add: div_eq_minus1)
  2.1064 +done
  2.1065 +
  2.1066 +lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
  2.1067 +by (drule zdiv_mono1_neg, auto)
  2.1068 +
  2.1069 +lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
  2.1070 +by (drule zdiv_mono1, auto)
  2.1071 +
  2.1072 +lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
  2.1073 +apply auto
  2.1074 +apply (drule_tac [2] zdiv_mono1)
  2.1075 +apply (auto simp add: linorder_neq_iff)
  2.1076 +apply (simp (no_asm_use) add: linorder_not_less [symmetric])
  2.1077 +apply (blast intro: div_neg_pos_less0)
  2.1078 +done
  2.1079 +
  2.1080 +lemma neg_imp_zdiv_nonneg_iff:
  2.1081 +     "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
  2.1082 +apply (subst zdiv_zminus_zminus [symmetric])
  2.1083 +apply (subst pos_imp_zdiv_nonneg_iff, auto)
  2.1084 +done
  2.1085 +
  2.1086 +(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
  2.1087 +lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
  2.1088 +by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
  2.1089 +
  2.1090 +(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
  2.1091 +lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
  2.1092 +by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
  2.1093 +
  2.1094 +
  2.1095 +subsubsection {* The Divides Relation *}
  2.1096 +
  2.1097 +lemmas zdvd_iff_zmod_eq_0_number_of [simp] =
  2.1098 +  dvd_eq_mod_eq_0 [of "number_of x::int" "number_of y::int", standard]
  2.1099 +
  2.1100 +lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
  2.1101 +  by (rule dvd_mod) (* TODO: remove *)
  2.1102 +
  2.1103 +lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
  2.1104 +  by (rule dvd_mod_imp_dvd) (* TODO: remove *)
  2.1105 +
  2.1106 +lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  2.1107 +  using zmod_zdiv_equality[where a="m" and b="n"]
  2.1108 +  by (simp add: algebra_simps)
  2.1109 +
  2.1110 +lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"
  2.1111 +apply (induct "y", auto)
  2.1112 +apply (rule zmod_zmult1_eq [THEN trans])
  2.1113 +apply (simp (no_asm_simp))
  2.1114 +apply (rule mod_mult_eq [symmetric])
  2.1115 +done
  2.1116 +
  2.1117 +lemma zdiv_int: "int (a div b) = (int a) div (int b)"
  2.1118 +apply (subst split_div, auto)
  2.1119 +apply (subst split_zdiv, auto)
  2.1120 +apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in unique_quotient)
  2.1121 +apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2.1122 +done
  2.1123 +
  2.1124 +lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
  2.1125 +apply (subst split_mod, auto)
  2.1126 +apply (subst split_zmod, auto)
  2.1127 +apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
  2.1128 +       in unique_remainder)
  2.1129 +apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2.1130 +done
  2.1131 +
  2.1132 +lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"
  2.1133 +by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
  2.1134 +
  2.1135 +lemma zdvd_mult_div_cancel:"(n::int) dvd m \<Longrightarrow> n * (m div n) = m"
  2.1136 +apply (subgoal_tac "m mod n = 0")
  2.1137 + apply (simp add: zmult_div_cancel)
  2.1138 +apply (simp only: dvd_eq_mod_eq_0)
  2.1139 +done
  2.1140 +
  2.1141 +text{*Suggested by Matthias Daum*}
  2.1142 +lemma int_power_div_base:
  2.1143 +     "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
  2.1144 +apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
  2.1145 + apply (erule ssubst)
  2.1146 + apply (simp only: power_add)
  2.1147 + apply simp_all
  2.1148 +done
  2.1149 +
  2.1150 +text {* by Brian Huffman *}
  2.1151 +lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
  2.1152 +by (rule mod_minus_eq [symmetric])
  2.1153 +
  2.1154 +lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
  2.1155 +by (rule mod_diff_left_eq [symmetric])
  2.1156 +
  2.1157 +lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
  2.1158 +by (rule mod_diff_right_eq [symmetric])
  2.1159 +
  2.1160 +lemmas zmod_simps =
  2.1161 +  mod_add_left_eq  [symmetric]
  2.1162 +  mod_add_right_eq [symmetric]
  2.1163 +  zmod_zmult1_eq   [symmetric]
  2.1164 +  mod_mult_left_eq [symmetric]
  2.1165 +  zpower_zmod
  2.1166 +  zminus_zmod zdiff_zmod_left zdiff_zmod_right
  2.1167 +
  2.1168 +text {* Distributive laws for function @{text nat}. *}
  2.1169 +
  2.1170 +lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
  2.1171 +apply (rule linorder_cases [of y 0])
  2.1172 +apply (simp add: div_nonneg_neg_le0)
  2.1173 +apply simp
  2.1174 +apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
  2.1175 +done
  2.1176 +
  2.1177 +(*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
  2.1178 +lemma nat_mod_distrib:
  2.1179 +  "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
  2.1180 +apply (case_tac "y = 0", simp)
  2.1181 +apply (simp add: nat_eq_iff zmod_int)
  2.1182 +done
  2.1183 +
  2.1184 +text  {* transfer setup *}
  2.1185 +
  2.1186 +lemma transfer_nat_int_functions:
  2.1187 +    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
  2.1188 +    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
  2.1189 +  by (auto simp add: nat_div_distrib nat_mod_distrib)
  2.1190 +
  2.1191 +lemma transfer_nat_int_function_closures:
  2.1192 +    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
  2.1193 +    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
  2.1194 +  apply (cases "y = 0")
  2.1195 +  apply (auto simp add: pos_imp_zdiv_nonneg_iff)
  2.1196 +  apply (cases "y = 0")
  2.1197 +  apply auto
  2.1198 +done
  2.1199 +
  2.1200 +declare TransferMorphism_nat_int [transfer add return:
  2.1201 +  transfer_nat_int_functions
  2.1202 +  transfer_nat_int_function_closures
  2.1203 +]
  2.1204 +
  2.1205 +lemma transfer_int_nat_functions:
  2.1206 +    "(int x) div (int y) = int (x div y)"
  2.1207 +    "(int x) mod (int y) = int (x mod y)"
  2.1208 +  by (auto simp add: zdiv_int zmod_int)
  2.1209 +
  2.1210 +lemma transfer_int_nat_function_closures:
  2.1211 +    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
  2.1212 +    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
  2.1213 +  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
  2.1214 +
  2.1215 +declare TransferMorphism_int_nat [transfer add return:
  2.1216 +  transfer_int_nat_functions
  2.1217 +  transfer_int_nat_function_closures
  2.1218 +]
  2.1219 +
  2.1220 +text{*Suggested by Matthias Daum*}
  2.1221 +lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
  2.1222 +apply (subgoal_tac "nat x div nat k < nat x")
  2.1223 + apply (simp (asm_lr) add: nat_div_distrib [symmetric])
  2.1224 +apply (rule Divides.div_less_dividend, simp_all)
  2.1225 +done
  2.1226 +
  2.1227 +text {* code generator setup *}
  2.1228 +
  2.1229 +context ring_1
  2.1230 +begin
  2.1231 +
  2.1232 +lemma of_int_num [code]:
  2.1233 +  "of_int k = (if k = 0 then 0 else if k < 0 then
  2.1234 +     - of_int (- k) else let
  2.1235 +       (l, m) = divmod_int k 2;
  2.1236 +       l' = of_int l
  2.1237 +     in if m = 0 then l' + l' else l' + l' + 1)"
  2.1238 +proof -
  2.1239 +  have aux1: "k mod (2\<Colon>int) \<noteq> (0\<Colon>int) \<Longrightarrow> 
  2.1240 +    of_int k = of_int (k div 2 * 2 + 1)"
  2.1241 +  proof -
  2.1242 +    have "k mod 2 < 2" by (auto intro: pos_mod_bound)
  2.1243 +    moreover have "0 \<le> k mod 2" by (auto intro: pos_mod_sign)
  2.1244 +    moreover assume "k mod 2 \<noteq> 0"
  2.1245 +    ultimately have "k mod 2 = 1" by arith
  2.1246 +    moreover have "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
  2.1247 +    ultimately show ?thesis by auto
  2.1248 +  qed
  2.1249 +  have aux2: "\<And>x. of_int 2 * x = x + x"
  2.1250 +  proof -
  2.1251 +    fix x
  2.1252 +    have int2: "(2::int) = 1 + 1" by arith
  2.1253 +    show "of_int 2 * x = x + x"
  2.1254 +    unfolding int2 of_int_add left_distrib by simp
  2.1255 +  qed
  2.1256 +  have aux3: "\<And>x. x * of_int 2 = x + x"
  2.1257 +  proof -
  2.1258 +    fix x
  2.1259 +    have int2: "(2::int) = 1 + 1" by arith
  2.1260 +    show "x * of_int 2 = x + x" 
  2.1261 +    unfolding int2 of_int_add right_distrib by simp
  2.1262 +  qed
  2.1263 +  from aux1 show ?thesis by (auto simp add: divmod_int_mod_div Let_def aux2 aux3)
  2.1264 +qed
  2.1265 +
  2.1266 +end
  2.1267 +
  2.1268 +lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
  2.1269 +proof
  2.1270 +  assume H: "x mod n = y mod n"
  2.1271 +  hence "x mod n - y mod n = 0" by simp
  2.1272 +  hence "(x mod n - y mod n) mod n = 0" by simp 
  2.1273 +  hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
  2.1274 +  thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
  2.1275 +next
  2.1276 +  assume H: "n dvd x - y"
  2.1277 +  then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
  2.1278 +  hence "x = n*k + y" by simp
  2.1279 +  hence "x mod n = (n*k + y) mod n" by simp
  2.1280 +  thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
  2.1281 +qed
  2.1282 +
  2.1283 +lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
  2.1284 +  shows "\<exists>q. x = y + n * q"
  2.1285 +proof-
  2.1286 +  from xy have th: "int x - int y = int (x - y)" by simp 
  2.1287 +  from xyn have "int x mod int n = int y mod int n" 
  2.1288 +    by (simp add: zmod_int[symmetric])
  2.1289 +  hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric]) 
  2.1290 +  hence "n dvd x - y" by (simp add: th zdvd_int)
  2.1291 +  then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
  2.1292 +qed
  2.1293 +
  2.1294 +lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" 
  2.1295 +  (is "?lhs = ?rhs")
  2.1296 +proof
  2.1297 +  assume H: "x mod n = y mod n"
  2.1298 +  {assume xy: "x \<le> y"
  2.1299 +    from H have th: "y mod n = x mod n" by simp
  2.1300 +    from nat_mod_eq_lemma[OF th xy] have ?rhs 
  2.1301 +      apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
  2.1302 +  moreover
  2.1303 +  {assume xy: "y \<le> x"
  2.1304 +    from nat_mod_eq_lemma[OF H xy] have ?rhs 
  2.1305 +      apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
  2.1306 +  ultimately  show ?rhs using linear[of x y] by blast  
  2.1307 +next
  2.1308 +  assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
  2.1309 +  hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  2.1310 +  thus  ?lhs by simp
  2.1311 +qed
  2.1312 +
  2.1313 +lemma div_nat_number_of [simp]:
  2.1314 +     "(number_of v :: nat)  div  number_of v' =  
  2.1315 +          (if neg (number_of v :: int) then 0  
  2.1316 +           else nat (number_of v div number_of v'))"
  2.1317 +  unfolding nat_number_of_def number_of_is_id neg_def
  2.1318 +  by (simp add: nat_div_distrib)
  2.1319 +
  2.1320 +lemma one_div_nat_number_of [simp]:
  2.1321 +     "Suc 0 div number_of v' = nat (1 div number_of v')" 
  2.1322 +by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
  2.1323 +
  2.1324 +lemma mod_nat_number_of [simp]:
  2.1325 +     "(number_of v :: nat)  mod  number_of v' =  
  2.1326 +        (if neg (number_of v :: int) then 0  
  2.1327 +         else if neg (number_of v' :: int) then number_of v  
  2.1328 +         else nat (number_of v mod number_of v'))"
  2.1329 +  unfolding nat_number_of_def number_of_is_id neg_def
  2.1330 +  by (simp add: nat_mod_distrib)
  2.1331 +
  2.1332 +lemma one_mod_nat_number_of [simp]:
  2.1333 +     "Suc 0 mod number_of v' =  
  2.1334 +        (if neg (number_of v' :: int) then Suc 0
  2.1335 +         else nat (1 mod number_of v'))"
  2.1336 +by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
  2.1337 +
  2.1338 +lemmas dvd_eq_mod_eq_0_number_of =
  2.1339 +  dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
  2.1340 +
  2.1341 +declare dvd_eq_mod_eq_0_number_of [simp]
  2.1342 +
  2.1343 +
  2.1344 +subsubsection {* Code generation *}
  2.1345 +
  2.1346 +definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  2.1347 +  "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  2.1348 +
  2.1349 +lemma pdivmod_posDivAlg [code]:
  2.1350 +  "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"
  2.1351 +by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)
  2.1352 +
  2.1353 +lemma divmod_int_pdivmod: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2.1354 +  apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
  2.1355 +    then pdivmod k l
  2.1356 +    else (let (r, s) = pdivmod k l in
  2.1357 +      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2.1358 +proof -
  2.1359 +  have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
  2.1360 +  show ?thesis
  2.1361 +    by (simp add: divmod_int_mod_div pdivmod_def)
  2.1362 +      (auto simp add: aux not_less not_le zdiv_zminus1_eq_if
  2.1363 +      zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)
  2.1364 +qed
  2.1365 +
  2.1366 +lemma divmod_int_code [code]: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2.1367 +  apsnd ((op *) (sgn l)) (if sgn k = sgn l
  2.1368 +    then pdivmod k l
  2.1369 +    else (let (r, s) = pdivmod k l in
  2.1370 +      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2.1371 +proof -
  2.1372 +  have "k \<noteq> 0 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0 \<longleftrightarrow> sgn k = sgn l"
  2.1373 +    by (auto simp add: not_less sgn_if)
  2.1374 +  then show ?thesis by (simp add: divmod_int_pdivmod)
  2.1375 +qed
  2.1376 +
  2.1377 +end
     3.1 --- a/src/HOL/Groebner_Basis.thy	Fri Oct 30 13:59:52 2009 +0100
     3.2 +++ b/src/HOL/Groebner_Basis.thy	Fri Oct 30 14:00:43 2009 +0100
     3.3 @@ -5,7 +5,7 @@
     3.4  header {* Semiring normalization and Groebner Bases *}
     3.5  
     3.6  theory Groebner_Basis
     3.7 -imports IntDiv
     3.8 +imports Numeral_Simprocs
     3.9  uses
    3.10    "Tools/Groebner_Basis/misc.ML"
    3.11    "Tools/Groebner_Basis/normalizer_data.ML"
     4.1 --- a/src/HOL/IntDiv.thy	Fri Oct 30 13:59:52 2009 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,1474 +0,0 @@
     4.4 -(*  Title:      HOL/IntDiv.thy
     4.5 -    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4.6 -    Copyright   1999  University of Cambridge
     4.7 -*)
     4.8 -
     4.9 -header{* The Division Operators div and mod *}
    4.10 -
    4.11 -theory IntDiv
    4.12 -imports Int Divides FunDef
    4.13 -uses
    4.14 -  "~~/src/Provers/Arith/assoc_fold.ML"
    4.15 -  "~~/src/Provers/Arith/cancel_numerals.ML"
    4.16 -  "~~/src/Provers/Arith/combine_numerals.ML"
    4.17 -  "~~/src/Provers/Arith/cancel_numeral_factor.ML"
    4.18 -  "~~/src/Provers/Arith/extract_common_term.ML"
    4.19 -  ("Tools/numeral_simprocs.ML")
    4.20 -  ("Tools/nat_numeral_simprocs.ML")
    4.21 -begin
    4.22 -
    4.23 -definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
    4.24 -    --{*definition of quotient and remainder*}
    4.25 -    [code]: "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
    4.26 -               (if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0))"
    4.27 -
    4.28 -definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
    4.29 -    --{*for the division algorithm*}
    4.30 -    [code]: "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
    4.31 -                         else (2 * q, r))"
    4.32 -
    4.33 -text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
    4.34 -function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
    4.35 -  "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
    4.36 -     else adjust b (posDivAlg a (2 * b)))"
    4.37 -by auto
    4.38 -termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
    4.39 -  (auto simp add: mult_2)
    4.40 -
    4.41 -text{*algorithm for the case @{text "a<0, b>0"}*}
    4.42 -function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
    4.43 -  "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
    4.44 -     else adjust b (negDivAlg a (2 * b)))"
    4.45 -by auto
    4.46 -termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
    4.47 -  (auto simp add: mult_2)
    4.48 -
    4.49 -text{*algorithm for the general case @{term "b\<noteq>0"}*}
    4.50 -definition negateSnd :: "int \<times> int \<Rightarrow> int \<times> int" where
    4.51 -  [code_unfold]: "negateSnd = apsnd uminus"
    4.52 -
    4.53 -definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
    4.54 -    --{*The full division algorithm considers all possible signs for a, b
    4.55 -       including the special case @{text "a=0, b<0"} because 
    4.56 -       @{term negDivAlg} requires @{term "a<0"}.*}
    4.57 -  "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
    4.58 -                  else if a = 0 then (0, 0)
    4.59 -                       else negateSnd (negDivAlg (-a) (-b))
    4.60 -               else 
    4.61 -                  if 0 < b then negDivAlg a b
    4.62 -                  else negateSnd (posDivAlg (-a) (-b)))"
    4.63 -
    4.64 -instantiation int :: Divides.div
    4.65 -begin
    4.66 -
    4.67 -definition
    4.68 -  "a div b = fst (divmod_int a b)"
    4.69 -
    4.70 -definition
    4.71 - "a mod b = snd (divmod_int a b)"
    4.72 -
    4.73 -instance ..
    4.74 -
    4.75 -end
    4.76 -
    4.77 -lemma divmod_int_mod_div:
    4.78 -  "divmod_int p q = (p div q, p mod q)"
    4.79 -  by (auto simp add: div_int_def mod_int_def)
    4.80 -
    4.81 -text{*
    4.82 -Here is the division algorithm in ML:
    4.83 -
    4.84 -\begin{verbatim}
    4.85 -    fun posDivAlg (a,b) =
    4.86 -      if a<b then (0,a)
    4.87 -      else let val (q,r) = posDivAlg(a, 2*b)
    4.88 -               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
    4.89 -           end
    4.90 -
    4.91 -    fun negDivAlg (a,b) =
    4.92 -      if 0\<le>a+b then (~1,a+b)
    4.93 -      else let val (q,r) = negDivAlg(a, 2*b)
    4.94 -               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
    4.95 -           end;
    4.96 -
    4.97 -    fun negateSnd (q,r:int) = (q,~r);
    4.98 -
    4.99 -    fun divmod (a,b) = if 0\<le>a then 
   4.100 -                          if b>0 then posDivAlg (a,b) 
   4.101 -                           else if a=0 then (0,0)
   4.102 -                                else negateSnd (negDivAlg (~a,~b))
   4.103 -                       else 
   4.104 -                          if 0<b then negDivAlg (a,b)
   4.105 -                          else        negateSnd (posDivAlg (~a,~b));
   4.106 -\end{verbatim}
   4.107 -*}
   4.108 -
   4.109 -
   4.110 -
   4.111 -subsection{*Uniqueness and Monotonicity of Quotients and Remainders*}
   4.112 -
   4.113 -lemma unique_quotient_lemma:
   4.114 -     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
   4.115 -      ==> q' \<le> (q::int)"
   4.116 -apply (subgoal_tac "r' + b * (q'-q) \<le> r")
   4.117 - prefer 2 apply (simp add: right_diff_distrib)
   4.118 -apply (subgoal_tac "0 < b * (1 + q - q') ")
   4.119 -apply (erule_tac [2] order_le_less_trans)
   4.120 - prefer 2 apply (simp add: right_diff_distrib right_distrib)
   4.121 -apply (subgoal_tac "b * q' < b * (1 + q) ")
   4.122 - prefer 2 apply (simp add: right_diff_distrib right_distrib)
   4.123 -apply (simp add: mult_less_cancel_left)
   4.124 -done
   4.125 -
   4.126 -lemma unique_quotient_lemma_neg:
   4.127 -     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
   4.128 -      ==> q \<le> (q'::int)"
   4.129 -by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
   4.130 -    auto)
   4.131 -
   4.132 -lemma unique_quotient:
   4.133 -     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r');  b \<noteq> 0 |]  
   4.134 -      ==> q = q'"
   4.135 -apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
   4.136 -apply (blast intro: order_antisym
   4.137 -             dest: order_eq_refl [THEN unique_quotient_lemma] 
   4.138 -             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
   4.139 -done
   4.140 -
   4.141 -
   4.142 -lemma unique_remainder:
   4.143 -     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r');  b \<noteq> 0 |]  
   4.144 -      ==> r = r'"
   4.145 -apply (subgoal_tac "q = q'")
   4.146 - apply (simp add: divmod_int_rel_def)
   4.147 -apply (blast intro: unique_quotient)
   4.148 -done
   4.149 -
   4.150 -
   4.151 -subsection{*Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends*}
   4.152 -
   4.153 -text{*And positive divisors*}
   4.154 -
   4.155 -lemma adjust_eq [simp]:
   4.156 -     "adjust b (q,r) = 
   4.157 -      (let diff = r-b in  
   4.158 -        if 0 \<le> diff then (2*q + 1, diff)   
   4.159 -                     else (2*q, r))"
   4.160 -by (simp add: Let_def adjust_def)
   4.161 -
   4.162 -declare posDivAlg.simps [simp del]
   4.163 -
   4.164 -text{*use with a simproc to avoid repeatedly proving the premise*}
   4.165 -lemma posDivAlg_eqn:
   4.166 -     "0 < b ==>  
   4.167 -      posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
   4.168 -by (rule posDivAlg.simps [THEN trans], simp)
   4.169 -
   4.170 -text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
   4.171 -theorem posDivAlg_correct:
   4.172 -  assumes "0 \<le> a" and "0 < b"
   4.173 -  shows "divmod_int_rel a b (posDivAlg a b)"
   4.174 -using prems apply (induct a b rule: posDivAlg.induct)
   4.175 -apply auto
   4.176 -apply (simp add: divmod_int_rel_def)
   4.177 -apply (subst posDivAlg_eqn, simp add: right_distrib)
   4.178 -apply (case_tac "a < b")
   4.179 -apply simp_all
   4.180 -apply (erule splitE)
   4.181 -apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
   4.182 -done
   4.183 -
   4.184 -
   4.185 -subsection{*Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends*}
   4.186 -
   4.187 -text{*And positive divisors*}
   4.188 -
   4.189 -declare negDivAlg.simps [simp del]
   4.190 -
   4.191 -text{*use with a simproc to avoid repeatedly proving the premise*}
   4.192 -lemma negDivAlg_eqn:
   4.193 -     "0 < b ==>  
   4.194 -      negDivAlg a b =       
   4.195 -       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
   4.196 -by (rule negDivAlg.simps [THEN trans], simp)
   4.197 -
   4.198 -(*Correctness of negDivAlg: it computes quotients correctly
   4.199 -  It doesn't work if a=0 because the 0/b equals 0, not -1*)
   4.200 -lemma negDivAlg_correct:
   4.201 -  assumes "a < 0" and "b > 0"
   4.202 -  shows "divmod_int_rel a b (negDivAlg a b)"
   4.203 -using prems apply (induct a b rule: negDivAlg.induct)
   4.204 -apply (auto simp add: linorder_not_le)
   4.205 -apply (simp add: divmod_int_rel_def)
   4.206 -apply (subst negDivAlg_eqn, assumption)
   4.207 -apply (case_tac "a + b < (0\<Colon>int)")
   4.208 -apply simp_all
   4.209 -apply (erule splitE)
   4.210 -apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
   4.211 -done
   4.212 -
   4.213 -
   4.214 -subsection{*Existence Shown by Proving the Division Algorithm to be Correct*}
   4.215 -
   4.216 -(*the case a=0*)
   4.217 -lemma divmod_int_rel_0: "b \<noteq> 0 ==> divmod_int_rel 0 b (0, 0)"
   4.218 -by (auto simp add: divmod_int_rel_def linorder_neq_iff)
   4.219 -
   4.220 -lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
   4.221 -by (subst posDivAlg.simps, auto)
   4.222 -
   4.223 -lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
   4.224 -by (subst negDivAlg.simps, auto)
   4.225 -
   4.226 -lemma negateSnd_eq [simp]: "negateSnd(q,r) = (q,-r)"
   4.227 -by (simp add: negateSnd_def)
   4.228 -
   4.229 -lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (negateSnd qr)"
   4.230 -by (auto simp add: split_ifs divmod_int_rel_def)
   4.231 -
   4.232 -lemma divmod_int_correct: "b \<noteq> 0 ==> divmod_int_rel a b (divmod_int a b)"
   4.233 -by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
   4.234 -                    posDivAlg_correct negDivAlg_correct)
   4.235 -
   4.236 -text{*Arbitrary definitions for division by zero.  Useful to simplify 
   4.237 -    certain equations.*}
   4.238 -
   4.239 -lemma DIVISION_BY_ZERO [simp]: "a div (0::int) = 0 & a mod (0::int) = a"
   4.240 -by (simp add: div_int_def mod_int_def divmod_int_def posDivAlg.simps)  
   4.241 -
   4.242 -
   4.243 -text{*Basic laws about division and remainder*}
   4.244 -
   4.245 -lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
   4.246 -apply (case_tac "b = 0", simp)
   4.247 -apply (cut_tac a = a and b = b in divmod_int_correct)
   4.248 -apply (auto simp add: divmod_int_rel_def div_int_def mod_int_def)
   4.249 -done
   4.250 -
   4.251 -lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"
   4.252 -by(simp add: zmod_zdiv_equality[symmetric])
   4.253 -
   4.254 -lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"
   4.255 -by(simp add: mult_commute zmod_zdiv_equality[symmetric])
   4.256 -
   4.257 -text {* Tool setup *}
   4.258 -
   4.259 -ML {*
   4.260 -local
   4.261 -
   4.262 -fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
   4.263 -
   4.264 -fun find_first_numeral past (t::terms) =
   4.265 -        ((snd (HOLogic.dest_number t), rev past @ terms)
   4.266 -         handle TERM _ => find_first_numeral (t::past) terms)
   4.267 -  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
   4.268 -
   4.269 -val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
   4.270 -
   4.271 -fun mk_minus t = 
   4.272 -  let val T = Term.fastype_of t
   4.273 -  in Const (@{const_name HOL.uminus}, T --> T) $ t end;
   4.274 -
   4.275 -(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
   4.276 -fun mk_sum T []        = mk_number T 0
   4.277 -  | mk_sum T [t,u]     = mk_plus (t, u)
   4.278 -  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
   4.279 -
   4.280 -(*this version ALWAYS includes a trailing zero*)
   4.281 -fun long_mk_sum T []        = mk_number T 0
   4.282 -  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
   4.283 -
   4.284 -val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
   4.285 -
   4.286 -(*decompose additions AND subtractions as a sum*)
   4.287 -fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
   4.288 -        dest_summing (pos, t, dest_summing (pos, u, ts))
   4.289 -  | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
   4.290 -        dest_summing (pos, t, dest_summing (not pos, u, ts))
   4.291 -  | dest_summing (pos, t, ts) =
   4.292 -        if pos then t::ts else mk_minus t :: ts;
   4.293 -
   4.294 -fun dest_sum t = dest_summing (true, t, []);
   4.295 -
   4.296 -structure CancelDivMod = CancelDivModFun(struct
   4.297 -
   4.298 -  val div_name = @{const_name div};
   4.299 -  val mod_name = @{const_name mod};
   4.300 -  val mk_binop = HOLogic.mk_binop;
   4.301 -  val mk_sum = mk_sum HOLogic.intT;
   4.302 -  val dest_sum = dest_sum;
   4.303 -
   4.304 -  val div_mod_eqs = map mk_meta_eq [@{thm zdiv_zmod_equality}, @{thm zdiv_zmod_equality2}];
   4.305 -
   4.306 -  val trans = trans;
   4.307 -
   4.308 -  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
   4.309 -    (@{thm diff_minus} :: @{thms add_0s} @ @{thms add_ac}))
   4.310 -
   4.311 -end)
   4.312 -
   4.313 -in
   4.314 -
   4.315 -val cancel_div_mod_int_proc = Simplifier.simproc @{theory}
   4.316 -  "cancel_zdiv_zmod" ["(k::int) + l"] (K CancelDivMod.proc);
   4.317 -
   4.318 -val _ = Addsimprocs [cancel_div_mod_int_proc];
   4.319 -
   4.320 -end
   4.321 -*}
   4.322 -
   4.323 -lemma pos_mod_conj : "(0::int) < b ==> 0 \<le> a mod b & a mod b < b"
   4.324 -apply (cut_tac a = a and b = b in divmod_int_correct)
   4.325 -apply (auto simp add: divmod_int_rel_def mod_int_def)
   4.326 -done
   4.327 -
   4.328 -lemmas pos_mod_sign  [simp] = pos_mod_conj [THEN conjunct1, standard]
   4.329 -   and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2, standard]
   4.330 -
   4.331 -lemma neg_mod_conj : "b < (0::int) ==> a mod b \<le> 0 & b < a mod b"
   4.332 -apply (cut_tac a = a and b = b in divmod_int_correct)
   4.333 -apply (auto simp add: divmod_int_rel_def div_int_def mod_int_def)
   4.334 -done
   4.335 -
   4.336 -lemmas neg_mod_sign  [simp] = neg_mod_conj [THEN conjunct1, standard]
   4.337 -   and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2, standard]
   4.338 -
   4.339 -
   4.340 -
   4.341 -subsection{*General Properties of div and mod*}
   4.342 -
   4.343 -lemma divmod_int_rel_div_mod: "b \<noteq> 0 ==> divmod_int_rel a b (a div b, a mod b)"
   4.344 -apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   4.345 -apply (force simp add: divmod_int_rel_def linorder_neq_iff)
   4.346 -done
   4.347 -
   4.348 -lemma divmod_int_rel_div: "[| divmod_int_rel a b (q, r);  b \<noteq> 0 |] ==> a div b = q"
   4.349 -by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
   4.350 -
   4.351 -lemma divmod_int_rel_mod: "[| divmod_int_rel a b (q, r);  b \<noteq> 0 |] ==> a mod b = r"
   4.352 -by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
   4.353 -
   4.354 -lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
   4.355 -apply (rule divmod_int_rel_div)
   4.356 -apply (auto simp add: divmod_int_rel_def)
   4.357 -done
   4.358 -
   4.359 -lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
   4.360 -apply (rule divmod_int_rel_div)
   4.361 -apply (auto simp add: divmod_int_rel_def)
   4.362 -done
   4.363 -
   4.364 -lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
   4.365 -apply (rule divmod_int_rel_div)
   4.366 -apply (auto simp add: divmod_int_rel_def)
   4.367 -done
   4.368 -
   4.369 -(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
   4.370 -
   4.371 -lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
   4.372 -apply (rule_tac q = 0 in divmod_int_rel_mod)
   4.373 -apply (auto simp add: divmod_int_rel_def)
   4.374 -done
   4.375 -
   4.376 -lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
   4.377 -apply (rule_tac q = 0 in divmod_int_rel_mod)
   4.378 -apply (auto simp add: divmod_int_rel_def)
   4.379 -done
   4.380 -
   4.381 -lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
   4.382 -apply (rule_tac q = "-1" in divmod_int_rel_mod)
   4.383 -apply (auto simp add: divmod_int_rel_def)
   4.384 -done
   4.385 -
   4.386 -text{*There is no @{text mod_neg_pos_trivial}.*}
   4.387 -
   4.388 -
   4.389 -(*Simpler laws such as -a div b = -(a div b) FAIL, but see just below*)
   4.390 -lemma zdiv_zminus_zminus [simp]: "(-a) div (-b) = a div (b::int)"
   4.391 -apply (case_tac "b = 0", simp)
   4.392 -apply (simp add: divmod_int_rel_div_mod [THEN divmod_int_rel_neg, simplified, 
   4.393 -                                 THEN divmod_int_rel_div, THEN sym])
   4.394 -
   4.395 -done
   4.396 -
   4.397 -(*Simpler laws such as -a mod b = -(a mod b) FAIL, but see just below*)
   4.398 -lemma zmod_zminus_zminus [simp]: "(-a) mod (-b) = - (a mod (b::int))"
   4.399 -apply (case_tac "b = 0", simp)
   4.400 -apply (subst divmod_int_rel_div_mod [THEN divmod_int_rel_neg, simplified, THEN divmod_int_rel_mod],
   4.401 -       auto)
   4.402 -done
   4.403 -
   4.404 -
   4.405 -subsection{*Laws for div and mod with Unary Minus*}
   4.406 -
   4.407 -lemma zminus1_lemma:
   4.408 -     "divmod_int_rel a b (q, r)
   4.409 -      ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
   4.410 -                          if r=0 then 0 else b-r)"
   4.411 -by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
   4.412 -
   4.413 -
   4.414 -lemma zdiv_zminus1_eq_if:
   4.415 -     "b \<noteq> (0::int)  
   4.416 -      ==> (-a) div b =  
   4.417 -          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   4.418 -by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN divmod_int_rel_div])
   4.419 -
   4.420 -lemma zmod_zminus1_eq_if:
   4.421 -     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
   4.422 -apply (case_tac "b = 0", simp)
   4.423 -apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN divmod_int_rel_mod])
   4.424 -done
   4.425 -
   4.426 -lemma zmod_zminus1_not_zero:
   4.427 -  fixes k l :: int
   4.428 -  shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
   4.429 -  unfolding zmod_zminus1_eq_if by auto
   4.430 -
   4.431 -lemma zdiv_zminus2: "a div (-b) = (-a::int) div b"
   4.432 -by (cut_tac a = "-a" in zdiv_zminus_zminus, auto)
   4.433 -
   4.434 -lemma zmod_zminus2: "a mod (-b) = - ((-a::int) mod b)"
   4.435 -by (cut_tac a = "-a" and b = b in zmod_zminus_zminus, auto)
   4.436 -
   4.437 -lemma zdiv_zminus2_eq_if:
   4.438 -     "b \<noteq> (0::int)  
   4.439 -      ==> a div (-b) =  
   4.440 -          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
   4.441 -by (simp add: zdiv_zminus1_eq_if zdiv_zminus2)
   4.442 -
   4.443 -lemma zmod_zminus2_eq_if:
   4.444 -     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
   4.445 -by (simp add: zmod_zminus1_eq_if zmod_zminus2)
   4.446 -
   4.447 -lemma zmod_zminus2_not_zero:
   4.448 -  fixes k l :: int
   4.449 -  shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
   4.450 -  unfolding zmod_zminus2_eq_if by auto 
   4.451 -
   4.452 -
   4.453 -subsection{*Division of a Number by Itself*}
   4.454 -
   4.455 -lemma self_quotient_aux1: "[| (0::int) < a; a = r + a*q; r < a |] ==> 1 \<le> q"
   4.456 -apply (subgoal_tac "0 < a*q")
   4.457 - apply (simp add: zero_less_mult_iff, arith)
   4.458 -done
   4.459 -
   4.460 -lemma self_quotient_aux2: "[| (0::int) < a; a = r + a*q; 0 \<le> r |] ==> q \<le> 1"
   4.461 -apply (subgoal_tac "0 \<le> a* (1-q) ")
   4.462 - apply (simp add: zero_le_mult_iff)
   4.463 -apply (simp add: right_diff_distrib)
   4.464 -done
   4.465 -
   4.466 -lemma self_quotient: "[| divmod_int_rel a a (q, r);  a \<noteq> (0::int) |] ==> q = 1"
   4.467 -apply (simp add: split_ifs divmod_int_rel_def linorder_neq_iff)
   4.468 -apply (rule order_antisym, safe, simp_all)
   4.469 -apply (rule_tac [3] a = "-a" and r = "-r" in self_quotient_aux1)
   4.470 -apply (rule_tac a = "-a" and r = "-r" in self_quotient_aux2)
   4.471 -apply (force intro: self_quotient_aux1 self_quotient_aux2 simp add: add_commute)+
   4.472 -done
   4.473 -
   4.474 -lemma self_remainder: "[| divmod_int_rel a a (q, r);  a \<noteq> (0::int) |] ==> r = 0"
   4.475 -apply (frule self_quotient, assumption)
   4.476 -apply (simp add: divmod_int_rel_def)
   4.477 -done
   4.478 -
   4.479 -lemma zdiv_self [simp]: "a \<noteq> 0 ==> a div a = (1::int)"
   4.480 -by (simp add: divmod_int_rel_div_mod [THEN self_quotient])
   4.481 -
   4.482 -(*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)
   4.483 -lemma zmod_self [simp]: "a mod a = (0::int)"
   4.484 -apply (case_tac "a = 0", simp)
   4.485 -apply (simp add: divmod_int_rel_div_mod [THEN self_remainder])
   4.486 -done
   4.487 -
   4.488 -
   4.489 -subsection{*Computation of Division and Remainder*}
   4.490 -
   4.491 -lemma zdiv_zero [simp]: "(0::int) div b = 0"
   4.492 -by (simp add: div_int_def divmod_int_def)
   4.493 -
   4.494 -lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
   4.495 -by (simp add: div_int_def divmod_int_def)
   4.496 -
   4.497 -lemma zmod_zero [simp]: "(0::int) mod b = 0"
   4.498 -by (simp add: mod_int_def divmod_int_def)
   4.499 -
   4.500 -lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
   4.501 -by (simp add: mod_int_def divmod_int_def)
   4.502 -
   4.503 -text{*a positive, b positive *}
   4.504 -
   4.505 -lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
   4.506 -by (simp add: div_int_def divmod_int_def)
   4.507 -
   4.508 -lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
   4.509 -by (simp add: mod_int_def divmod_int_def)
   4.510 -
   4.511 -text{*a negative, b positive *}
   4.512 -
   4.513 -lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
   4.514 -by (simp add: div_int_def divmod_int_def)
   4.515 -
   4.516 -lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
   4.517 -by (simp add: mod_int_def divmod_int_def)
   4.518 -
   4.519 -text{*a positive, b negative *}
   4.520 -
   4.521 -lemma div_pos_neg:
   4.522 -     "[| 0 < a;  b < 0 |] ==> a div b = fst (negateSnd (negDivAlg (-a) (-b)))"
   4.523 -by (simp add: div_int_def divmod_int_def)
   4.524 -
   4.525 -lemma mod_pos_neg:
   4.526 -     "[| 0 < a;  b < 0 |] ==> a mod b = snd (negateSnd (negDivAlg (-a) (-b)))"
   4.527 -by (simp add: mod_int_def divmod_int_def)
   4.528 -
   4.529 -text{*a negative, b negative *}
   4.530 -
   4.531 -lemma div_neg_neg:
   4.532 -     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (negateSnd (posDivAlg (-a) (-b)))"
   4.533 -by (simp add: div_int_def divmod_int_def)
   4.534 -
   4.535 -lemma mod_neg_neg:
   4.536 -     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (negateSnd (posDivAlg (-a) (-b)))"
   4.537 -by (simp add: mod_int_def divmod_int_def)
   4.538 -
   4.539 -text {*Simplify expresions in which div and mod combine numerical constants*}
   4.540 -
   4.541 -lemma divmod_int_relI:
   4.542 -  "\<lbrakk>a == b * q + r; if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0\<rbrakk>
   4.543 -    \<Longrightarrow> divmod_int_rel a b (q, r)"
   4.544 -  unfolding divmod_int_rel_def by simp
   4.545 -
   4.546 -lemmas divmod_int_rel_div_eq = divmod_int_relI [THEN divmod_int_rel_div, THEN eq_reflection]
   4.547 -lemmas divmod_int_rel_mod_eq = divmod_int_relI [THEN divmod_int_rel_mod, THEN eq_reflection]
   4.548 -lemmas arithmetic_simps =
   4.549 -  arith_simps
   4.550 -  add_special
   4.551 -  OrderedGroup.add_0_left
   4.552 -  OrderedGroup.add_0_right
   4.553 -  mult_zero_left
   4.554 -  mult_zero_right
   4.555 -  mult_1_left
   4.556 -  mult_1_right
   4.557 -
   4.558 -(* simprocs adapted from HOL/ex/Binary.thy *)
   4.559 -ML {*
   4.560 -local
   4.561 -  val mk_number = HOLogic.mk_number HOLogic.intT;
   4.562 -  fun mk_cert u k l = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"} $
   4.563 -    (@{term "times :: int \<Rightarrow> int \<Rightarrow> int"} $ u $ mk_number k) $
   4.564 -      mk_number l;
   4.565 -  fun prove ctxt prop = Goal.prove ctxt [] [] prop
   4.566 -    (K (ALLGOALS (full_simp_tac (HOL_basic_ss addsimps @{thms arithmetic_simps}))));
   4.567 -  fun binary_proc proc ss ct =
   4.568 -    (case Thm.term_of ct of
   4.569 -      _ $ t $ u =>
   4.570 -      (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of
   4.571 -        SOME args => proc (Simplifier.the_context ss) args
   4.572 -      | NONE => NONE)
   4.573 -    | _ => NONE);
   4.574 -in
   4.575 -  fun divmod_proc rule = binary_proc (fn ctxt => fn ((m, t), (n, u)) =>
   4.576 -    if n = 0 then NONE
   4.577 -    else let val (k, l) = Integer.div_mod m n;
   4.578 -    in SOME (rule OF [prove ctxt (Logic.mk_equals (t, mk_cert u k l))]) end);
   4.579 -end
   4.580 -*}
   4.581 -
   4.582 -simproc_setup binary_int_div ("number_of m div number_of n :: int") =
   4.583 -  {* K (divmod_proc (@{thm divmod_int_rel_div_eq})) *}
   4.584 -
   4.585 -simproc_setup binary_int_mod ("number_of m mod number_of n :: int") =
   4.586 -  {* K (divmod_proc (@{thm divmod_int_rel_mod_eq})) *}
   4.587 -
   4.588 -lemmas posDivAlg_eqn_number_of [simp] =
   4.589 -    posDivAlg_eqn [of "number_of v" "number_of w", standard]
   4.590 -
   4.591 -lemmas negDivAlg_eqn_number_of [simp] =
   4.592 -    negDivAlg_eqn [of "number_of v" "number_of w", standard]
   4.593 -
   4.594 -
   4.595 -text{*Special-case simplification *}
   4.596 -
   4.597 -lemma zmod_minus1_right [simp]: "a mod (-1::int) = 0"
   4.598 -apply (cut_tac a = a and b = "-1" in neg_mod_sign)
   4.599 -apply (cut_tac [2] a = a and b = "-1" in neg_mod_bound)
   4.600 -apply (auto simp del: neg_mod_sign neg_mod_bound)
   4.601 -done
   4.602 -
   4.603 -lemma zdiv_minus1_right [simp]: "a div (-1::int) = -a"
   4.604 -by (cut_tac a = a and b = "-1" in zmod_zdiv_equality, auto)
   4.605 -
   4.606 -(** The last remaining special cases for constant arithmetic:
   4.607 -    1 div z and 1 mod z **)
   4.608 -
   4.609 -lemmas div_pos_pos_1_number_of [simp] =
   4.610 -    div_pos_pos [OF int_0_less_1, of "number_of w", standard]
   4.611 -
   4.612 -lemmas div_pos_neg_1_number_of [simp] =
   4.613 -    div_pos_neg [OF int_0_less_1, of "number_of w", standard]
   4.614 -
   4.615 -lemmas mod_pos_pos_1_number_of [simp] =
   4.616 -    mod_pos_pos [OF int_0_less_1, of "number_of w", standard]
   4.617 -
   4.618 -lemmas mod_pos_neg_1_number_of [simp] =
   4.619 -    mod_pos_neg [OF int_0_less_1, of "number_of w", standard]
   4.620 -
   4.621 -
   4.622 -lemmas posDivAlg_eqn_1_number_of [simp] =
   4.623 -    posDivAlg_eqn [of concl: 1 "number_of w", standard]
   4.624 -
   4.625 -lemmas negDivAlg_eqn_1_number_of [simp] =
   4.626 -    negDivAlg_eqn [of concl: 1 "number_of w", standard]
   4.627 -
   4.628 -
   4.629 -
   4.630 -subsection{*Monotonicity in the First Argument (Dividend)*}
   4.631 -
   4.632 -lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
   4.633 -apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   4.634 -apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   4.635 -apply (rule unique_quotient_lemma)
   4.636 -apply (erule subst)
   4.637 -apply (erule subst, simp_all)
   4.638 -done
   4.639 -
   4.640 -lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
   4.641 -apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   4.642 -apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
   4.643 -apply (rule unique_quotient_lemma_neg)
   4.644 -apply (erule subst)
   4.645 -apply (erule subst, simp_all)
   4.646 -done
   4.647 -
   4.648 -
   4.649 -subsection{*Monotonicity in the Second Argument (Divisor)*}
   4.650 -
   4.651 -lemma q_pos_lemma:
   4.652 -     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
   4.653 -apply (subgoal_tac "0 < b'* (q' + 1) ")
   4.654 - apply (simp add: zero_less_mult_iff)
   4.655 -apply (simp add: right_distrib)
   4.656 -done
   4.657 -
   4.658 -lemma zdiv_mono2_lemma:
   4.659 -     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
   4.660 -         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
   4.661 -      ==> q \<le> (q'::int)"
   4.662 -apply (frule q_pos_lemma, assumption+) 
   4.663 -apply (subgoal_tac "b*q < b* (q' + 1) ")
   4.664 - apply (simp add: mult_less_cancel_left)
   4.665 -apply (subgoal_tac "b*q = r' - r + b'*q'")
   4.666 - prefer 2 apply simp
   4.667 -apply (simp (no_asm_simp) add: right_distrib)
   4.668 -apply (subst add_commute, rule zadd_zless_mono, arith)
   4.669 -apply (rule mult_right_mono, auto)
   4.670 -done
   4.671 -
   4.672 -lemma zdiv_mono2:
   4.673 -     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
   4.674 -apply (subgoal_tac "b \<noteq> 0")
   4.675 - prefer 2 apply arith
   4.676 -apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   4.677 -apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   4.678 -apply (rule zdiv_mono2_lemma)
   4.679 -apply (erule subst)
   4.680 -apply (erule subst, simp_all)
   4.681 -done
   4.682 -
   4.683 -lemma q_neg_lemma:
   4.684 -     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
   4.685 -apply (subgoal_tac "b'*q' < 0")
   4.686 - apply (simp add: mult_less_0_iff, arith)
   4.687 -done
   4.688 -
   4.689 -lemma zdiv_mono2_neg_lemma:
   4.690 -     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
   4.691 -         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
   4.692 -      ==> q' \<le> (q::int)"
   4.693 -apply (frule q_neg_lemma, assumption+) 
   4.694 -apply (subgoal_tac "b*q' < b* (q + 1) ")
   4.695 - apply (simp add: mult_less_cancel_left)
   4.696 -apply (simp add: right_distrib)
   4.697 -apply (subgoal_tac "b*q' \<le> b'*q'")
   4.698 - prefer 2 apply (simp add: mult_right_mono_neg, arith)
   4.699 -done
   4.700 -
   4.701 -lemma zdiv_mono2_neg:
   4.702 -     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
   4.703 -apply (cut_tac a = a and b = b in zmod_zdiv_equality)
   4.704 -apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
   4.705 -apply (rule zdiv_mono2_neg_lemma)
   4.706 -apply (erule subst)
   4.707 -apply (erule subst, simp_all)
   4.708 -done
   4.709 -
   4.710 -
   4.711 -subsection{*More Algebraic Laws for div and mod*}
   4.712 -
   4.713 -text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
   4.714 -
   4.715 -lemma zmult1_lemma:
   4.716 -     "[| divmod_int_rel b c (q, r);  c \<noteq> 0 |]  
   4.717 -      ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
   4.718 -by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib mult_ac)
   4.719 -
   4.720 -lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
   4.721 -apply (case_tac "c = 0", simp)
   4.722 -apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN divmod_int_rel_div])
   4.723 -done
   4.724 -
   4.725 -lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"
   4.726 -apply (case_tac "c = 0", simp)
   4.727 -apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN divmod_int_rel_mod])
   4.728 -done
   4.729 -
   4.730 -lemma zmod_zdiv_trivial: "(a mod b) div b = (0::int)"
   4.731 -apply (case_tac "b = 0", simp)
   4.732 -apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial)
   4.733 -done
   4.734 -
   4.735 -text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
   4.736 -
   4.737 -lemma zadd1_lemma:
   4.738 -     "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br);  c \<noteq> 0 |]  
   4.739 -      ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
   4.740 -by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib)
   4.741 -
   4.742 -(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   4.743 -lemma zdiv_zadd1_eq:
   4.744 -     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
   4.745 -apply (case_tac "c = 0", simp)
   4.746 -apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] divmod_int_rel_div)
   4.747 -done
   4.748 -
   4.749 -instance int :: ring_div
   4.750 -proof
   4.751 -  fix a b c :: int
   4.752 -  assume not0: "b \<noteq> 0"
   4.753 -  show "(a + c * b) div b = c + a div b"
   4.754 -    unfolding zdiv_zadd1_eq [of a "c * b"] using not0 
   4.755 -      by (simp add: zmod_zmult1_eq zmod_zdiv_trivial zdiv_zmult1_eq)
   4.756 -next
   4.757 -  fix a b c :: int
   4.758 -  assume "a \<noteq> 0"
   4.759 -  then show "(a * b) div (a * c) = b div c"
   4.760 -  proof (cases "b \<noteq> 0 \<and> c \<noteq> 0")
   4.761 -    case False then show ?thesis by auto
   4.762 -  next
   4.763 -    case True then have "b \<noteq> 0" and "c \<noteq> 0" by auto
   4.764 -    with `a \<noteq> 0`
   4.765 -    have "\<And>q r. divmod_int_rel b c (q, r) \<Longrightarrow> divmod_int_rel (a * b) (a * c) (q, a * r)"
   4.766 -      apply (auto simp add: divmod_int_rel_def) 
   4.767 -      apply (auto simp add: algebra_simps)
   4.768 -      apply (auto simp add: zero_less_mult_iff zero_le_mult_iff mult_le_0_iff mult_commute [of a] mult_less_cancel_right)
   4.769 -      done
   4.770 -    moreover with `c \<noteq> 0` divmod_int_rel_div_mod have "divmod_int_rel b c (b div c, b mod c)" by auto
   4.771 -    ultimately have "divmod_int_rel (a * b) (a * c) (b div c, a * (b mod c))" .
   4.772 -    moreover from  `a \<noteq> 0` `c \<noteq> 0` have "a * c \<noteq> 0" by simp
   4.773 -    ultimately show ?thesis by (rule divmod_int_rel_div)
   4.774 -  qed
   4.775 -qed auto
   4.776 -
   4.777 -lemma posDivAlg_div_mod:
   4.778 -  assumes "k \<ge> 0"
   4.779 -  and "l \<ge> 0"
   4.780 -  shows "posDivAlg k l = (k div l, k mod l)"
   4.781 -proof (cases "l = 0")
   4.782 -  case True then show ?thesis by (simp add: posDivAlg.simps)
   4.783 -next
   4.784 -  case False with assms posDivAlg_correct
   4.785 -    have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
   4.786 -    by simp
   4.787 -  from divmod_int_rel_div [OF this `l \<noteq> 0`] divmod_int_rel_mod [OF this `l \<noteq> 0`]
   4.788 -  show ?thesis by simp
   4.789 -qed
   4.790 -
   4.791 -lemma negDivAlg_div_mod:
   4.792 -  assumes "k < 0"
   4.793 -  and "l > 0"
   4.794 -  shows "negDivAlg k l = (k div l, k mod l)"
   4.795 -proof -
   4.796 -  from assms have "l \<noteq> 0" by simp
   4.797 -  from assms negDivAlg_correct
   4.798 -    have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
   4.799 -    by simp
   4.800 -  from divmod_int_rel_div [OF this `l \<noteq> 0`] divmod_int_rel_mod [OF this `l \<noteq> 0`]
   4.801 -  show ?thesis by simp
   4.802 -qed
   4.803 -
   4.804 -lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
   4.805 -by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   4.806 -
   4.807 -(* REVISIT: should this be generalized to all semiring_div types? *)
   4.808 -lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
   4.809 -
   4.810 -
   4.811 -subsection{*Proving  @{term "a div (b*c) = (a div b) div c"} *}
   4.812 -
   4.813 -(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
   4.814 -  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
   4.815 -  to cause particular problems.*)
   4.816 -
   4.817 -text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
   4.818 -
   4.819 -lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
   4.820 -apply (subgoal_tac "b * (c - q mod c) < r * 1")
   4.821 - apply (simp add: algebra_simps)
   4.822 -apply (rule order_le_less_trans)
   4.823 - apply (erule_tac [2] mult_strict_right_mono)
   4.824 - apply (rule mult_left_mono_neg)
   4.825 -  using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps pos_mod_bound)
   4.826 - apply (simp)
   4.827 -apply (simp)
   4.828 -done
   4.829 -
   4.830 -lemma zmult2_lemma_aux2:
   4.831 -     "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
   4.832 -apply (subgoal_tac "b * (q mod c) \<le> 0")
   4.833 - apply arith
   4.834 -apply (simp add: mult_le_0_iff)
   4.835 -done
   4.836 -
   4.837 -lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
   4.838 -apply (subgoal_tac "0 \<le> b * (q mod c) ")
   4.839 -apply arith
   4.840 -apply (simp add: zero_le_mult_iff)
   4.841 -done
   4.842 -
   4.843 -lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
   4.844 -apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
   4.845 - apply (simp add: right_diff_distrib)
   4.846 -apply (rule order_less_le_trans)
   4.847 - apply (erule mult_strict_right_mono)
   4.848 - apply (rule_tac [2] mult_left_mono)
   4.849 -  apply simp
   4.850 - using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps pos_mod_bound)
   4.851 -apply simp
   4.852 -done
   4.853 -
   4.854 -lemma zmult2_lemma: "[| divmod_int_rel a b (q, r);  b \<noteq> 0;  0 < c |]  
   4.855 -      ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
   4.856 -by (auto simp add: mult_ac divmod_int_rel_def linorder_neq_iff
   4.857 -                   zero_less_mult_iff right_distrib [symmetric] 
   4.858 -                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4)
   4.859 -
   4.860 -lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
   4.861 -apply (case_tac "b = 0", simp)
   4.862 -apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN divmod_int_rel_div])
   4.863 -done
   4.864 -
   4.865 -lemma zmod_zmult2_eq:
   4.866 -     "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
   4.867 -apply (case_tac "b = 0", simp)
   4.868 -apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN divmod_int_rel_mod])
   4.869 -done
   4.870 -
   4.871 -
   4.872 -subsection {*Splitting Rules for div and mod*}
   4.873 -
   4.874 -text{*The proofs of the two lemmas below are essentially identical*}
   4.875 -
   4.876 -lemma split_pos_lemma:
   4.877 - "0<k ==> 
   4.878 -    P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
   4.879 -apply (rule iffI, clarify)
   4.880 - apply (erule_tac P="P ?x ?y" in rev_mp)  
   4.881 - apply (subst mod_add_eq) 
   4.882 - apply (subst zdiv_zadd1_eq) 
   4.883 - apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
   4.884 -txt{*converse direction*}
   4.885 -apply (drule_tac x = "n div k" in spec) 
   4.886 -apply (drule_tac x = "n mod k" in spec, simp)
   4.887 -done
   4.888 -
   4.889 -lemma split_neg_lemma:
   4.890 - "k<0 ==>
   4.891 -    P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
   4.892 -apply (rule iffI, clarify)
   4.893 - apply (erule_tac P="P ?x ?y" in rev_mp)  
   4.894 - apply (subst mod_add_eq) 
   4.895 - apply (subst zdiv_zadd1_eq) 
   4.896 - apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
   4.897 -txt{*converse direction*}
   4.898 -apply (drule_tac x = "n div k" in spec) 
   4.899 -apply (drule_tac x = "n mod k" in spec, simp)
   4.900 -done
   4.901 -
   4.902 -lemma split_zdiv:
   4.903 - "P(n div k :: int) =
   4.904 -  ((k = 0 --> P 0) & 
   4.905 -   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
   4.906 -   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
   4.907 -apply (case_tac "k=0", simp)
   4.908 -apply (simp only: linorder_neq_iff)
   4.909 -apply (erule disjE) 
   4.910 - apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
   4.911 -                      split_neg_lemma [of concl: "%x y. P x"])
   4.912 -done
   4.913 -
   4.914 -lemma split_zmod:
   4.915 - "P(n mod k :: int) =
   4.916 -  ((k = 0 --> P n) & 
   4.917 -   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
   4.918 -   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
   4.919 -apply (case_tac "k=0", simp)
   4.920 -apply (simp only: linorder_neq_iff)
   4.921 -apply (erule disjE) 
   4.922 - apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
   4.923 -                      split_neg_lemma [of concl: "%x y. P y"])
   4.924 -done
   4.925 -
   4.926 -(* Enable arith to deal with div 2 and mod 2: *)
   4.927 -declare split_zdiv [of _ _ "number_of k", simplified, standard, arith_split]
   4.928 -declare split_zmod [of _ _ "number_of k", simplified, standard, arith_split]
   4.929 -
   4.930 -
   4.931 -subsection{*Speeding up the Division Algorithm with Shifting*}
   4.932 -
   4.933 -text{*computing div by shifting *}
   4.934 -
   4.935 -lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
   4.936 -proof cases
   4.937 -  assume "a=0"
   4.938 -    thus ?thesis by simp
   4.939 -next
   4.940 -  assume "a\<noteq>0" and le_a: "0\<le>a"   
   4.941 -  hence a_pos: "1 \<le> a" by arith
   4.942 -  hence one_less_a2: "1 < 2 * a" by arith
   4.943 -  hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
   4.944 -    unfolding mult_le_cancel_left
   4.945 -    by (simp add: add1_zle_eq add_commute [of 1])
   4.946 -  with a_pos have "0 \<le> b mod a" by simp
   4.947 -  hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
   4.948 -    by (simp add: mod_pos_pos_trivial one_less_a2)
   4.949 -  with  le_2a
   4.950 -  have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
   4.951 -    by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2
   4.952 -                  right_distrib) 
   4.953 -  thus ?thesis
   4.954 -    by (subst zdiv_zadd1_eq,
   4.955 -        simp add: mod_mult_mult1 one_less_a2
   4.956 -                  div_pos_pos_trivial)
   4.957 -qed
   4.958 -
   4.959 -lemma neg_zdiv_mult_2: "a \<le> (0::int) ==> (1 + 2*b) div (2*a) = (b+1) div a"
   4.960 -apply (subgoal_tac " (1 + 2* (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a) ")
   4.961 -apply (rule_tac [2] pos_zdiv_mult_2)
   4.962 -apply (auto simp add: right_diff_distrib)
   4.963 -apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
   4.964 -apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric])
   4.965 -apply (simp_all add: algebra_simps)
   4.966 -apply (simp only: ab_diff_minus minus_add_distrib [symmetric] number_of_Min zdiv_zminus_zminus)
   4.967 -done
   4.968 -
   4.969 -lemma zdiv_number_of_Bit0 [simp]:
   4.970 -     "number_of (Int.Bit0 v) div number_of (Int.Bit0 w) =  
   4.971 -          number_of v div (number_of w :: int)"
   4.972 -by (simp only: number_of_eq numeral_simps) (simp add: mult_2 [symmetric])
   4.973 -
   4.974 -lemma zdiv_number_of_Bit1 [simp]:
   4.975 -     "number_of (Int.Bit1 v) div number_of (Int.Bit0 w) =  
   4.976 -          (if (0::int) \<le> number_of w                    
   4.977 -           then number_of v div (number_of w)     
   4.978 -           else (number_of v + (1::int)) div (number_of w))"
   4.979 -apply (simp only: number_of_eq numeral_simps UNIV_I split: split_if) 
   4.980 -apply (simp add: pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac mult_2 [symmetric])
   4.981 -done
   4.982 -
   4.983 -
   4.984 -subsection{*Computing mod by Shifting (proofs resemble those for div)*}
   4.985 -
   4.986 -lemma pos_zmod_mult_2:
   4.987 -  fixes a b :: int
   4.988 -  assumes "0 \<le> a"
   4.989 -  shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
   4.990 -proof (cases "0 < a")
   4.991 -  case False with assms show ?thesis by simp
   4.992 -next
   4.993 -  case True
   4.994 -  then have "b mod a < a" by (rule pos_mod_bound)
   4.995 -  then have "1 + b mod a \<le> a" by simp
   4.996 -  then have A: "2 * (1 + b mod a) \<le> 2 * a" by simp
   4.997 -  from `0 < a` have "0 \<le> b mod a" by (rule pos_mod_sign)
   4.998 -  then have B: "0 \<le> 1 + 2 * (b mod a)" by simp
   4.999 -  have "((1\<Colon>int) mod ((2\<Colon>int) * a) + (2\<Colon>int) * b mod ((2\<Colon>int) * a)) mod ((2\<Colon>int) * a) = (1\<Colon>int) + (2\<Colon>int) * (b mod a)"
  4.1000 -    using `0 < a` and A
  4.1001 -    by (auto simp add: mod_mult_mult1 mod_pos_pos_trivial ring_distribs intro!: mod_pos_pos_trivial B)
  4.1002 -  then show ?thesis by (subst mod_add_eq)
  4.1003 -qed
  4.1004 -
  4.1005 -lemma neg_zmod_mult_2:
  4.1006 -  fixes a b :: int
  4.1007 -  assumes "a \<le> 0"
  4.1008 -  shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
  4.1009 -proof -
  4.1010 -  from assms have "0 \<le> - a" by auto
  4.1011 -  then have "(1 + 2 * (- b - 1)) mod (2 * (- a)) = 1 + 2 * ((- b - 1) mod (- a))"
  4.1012 -    by (rule pos_zmod_mult_2)
  4.1013 -  then show ?thesis by (simp add: zmod_zminus2 algebra_simps)
  4.1014 -     (simp add: diff_minus add_ac)
  4.1015 -qed
  4.1016 -
  4.1017 -lemma zmod_number_of_Bit0 [simp]:
  4.1018 -     "number_of (Int.Bit0 v) mod number_of (Int.Bit0 w) =  
  4.1019 -      (2::int) * (number_of v mod number_of w)"
  4.1020 -apply (simp only: number_of_eq numeral_simps) 
  4.1021 -apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  4.1022 -                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  4.1023 -done
  4.1024 -
  4.1025 -lemma zmod_number_of_Bit1 [simp]:
  4.1026 -     "number_of (Int.Bit1 v) mod number_of (Int.Bit0 w) =  
  4.1027 -      (if (0::int) \<le> number_of w  
  4.1028 -                then 2 * (number_of v mod number_of w) + 1     
  4.1029 -                else 2 * ((number_of v + (1::int)) mod number_of w) - 1)"
  4.1030 -apply (simp only: number_of_eq numeral_simps) 
  4.1031 -apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  4.1032 -                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  4.1033 -done
  4.1034 -
  4.1035 -
  4.1036 -subsection{*Quotients of Signs*}
  4.1037 -
  4.1038 -lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
  4.1039 -apply (subgoal_tac "a div b \<le> -1", force)
  4.1040 -apply (rule order_trans)
  4.1041 -apply (rule_tac a' = "-1" in zdiv_mono1)
  4.1042 -apply (auto simp add: div_eq_minus1)
  4.1043 -done
  4.1044 -
  4.1045 -lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
  4.1046 -by (drule zdiv_mono1_neg, auto)
  4.1047 -
  4.1048 -lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
  4.1049 -by (drule zdiv_mono1, auto)
  4.1050 -
  4.1051 -lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
  4.1052 -apply auto
  4.1053 -apply (drule_tac [2] zdiv_mono1)
  4.1054 -apply (auto simp add: linorder_neq_iff)
  4.1055 -apply (simp (no_asm_use) add: linorder_not_less [symmetric])
  4.1056 -apply (blast intro: div_neg_pos_less0)
  4.1057 -done
  4.1058 -
  4.1059 -lemma neg_imp_zdiv_nonneg_iff:
  4.1060 -     "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
  4.1061 -apply (subst zdiv_zminus_zminus [symmetric])
  4.1062 -apply (subst pos_imp_zdiv_nonneg_iff, auto)
  4.1063 -done
  4.1064 -
  4.1065 -(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
  4.1066 -lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
  4.1067 -by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
  4.1068 -
  4.1069 -(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
  4.1070 -lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
  4.1071 -by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
  4.1072 -
  4.1073 -
  4.1074 -subsection {* The Divides Relation *}
  4.1075 -
  4.1076 -lemmas zdvd_iff_zmod_eq_0_number_of [simp] =
  4.1077 -  dvd_eq_mod_eq_0 [of "number_of x::int" "number_of y::int", standard]
  4.1078 -
  4.1079 -lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
  4.1080 -  by (rule dvd_mod) (* TODO: remove *)
  4.1081 -
  4.1082 -lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
  4.1083 -  by (rule dvd_mod_imp_dvd) (* TODO: remove *)
  4.1084 -
  4.1085 -lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  4.1086 -  using zmod_zdiv_equality[where a="m" and b="n"]
  4.1087 -  by (simp add: algebra_simps)
  4.1088 -
  4.1089 -lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"
  4.1090 -apply (induct "y", auto)
  4.1091 -apply (rule zmod_zmult1_eq [THEN trans])
  4.1092 -apply (simp (no_asm_simp))
  4.1093 -apply (rule mod_mult_eq [symmetric])
  4.1094 -done
  4.1095 -
  4.1096 -lemma zdiv_int: "int (a div b) = (int a) div (int b)"
  4.1097 -apply (subst split_div, auto)
  4.1098 -apply (subst split_zdiv, auto)
  4.1099 -apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
  4.1100 -apply (auto simp add: IntDiv.divmod_int_rel_def of_nat_mult)
  4.1101 -done
  4.1102 -
  4.1103 -lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
  4.1104 -apply (subst split_mod, auto)
  4.1105 -apply (subst split_zmod, auto)
  4.1106 -apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
  4.1107 -       in unique_remainder)
  4.1108 -apply (auto simp add: IntDiv.divmod_int_rel_def of_nat_mult)
  4.1109 -done
  4.1110 -
  4.1111 -lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"
  4.1112 -by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
  4.1113 -
  4.1114 -lemma zdvd_mult_div_cancel:"(n::int) dvd m \<Longrightarrow> n * (m div n) = m"
  4.1115 -apply (subgoal_tac "m mod n = 0")
  4.1116 - apply (simp add: zmult_div_cancel)
  4.1117 -apply (simp only: dvd_eq_mod_eq_0)
  4.1118 -done
  4.1119 -
  4.1120 -text{*Suggested by Matthias Daum*}
  4.1121 -lemma int_power_div_base:
  4.1122 -     "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
  4.1123 -apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
  4.1124 - apply (erule ssubst)
  4.1125 - apply (simp only: power_add)
  4.1126 - apply simp_all
  4.1127 -done
  4.1128 -
  4.1129 -text {* by Brian Huffman *}
  4.1130 -lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
  4.1131 -by (rule mod_minus_eq [symmetric])
  4.1132 -
  4.1133 -lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
  4.1134 -by (rule mod_diff_left_eq [symmetric])
  4.1135 -
  4.1136 -lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
  4.1137 -by (rule mod_diff_right_eq [symmetric])
  4.1138 -
  4.1139 -lemmas zmod_simps =
  4.1140 -  mod_add_left_eq  [symmetric]
  4.1141 -  mod_add_right_eq [symmetric]
  4.1142 -  zmod_zmult1_eq   [symmetric]
  4.1143 -  mod_mult_left_eq [symmetric]
  4.1144 -  zpower_zmod
  4.1145 -  zminus_zmod zdiff_zmod_left zdiff_zmod_right
  4.1146 -
  4.1147 -text {* Distributive laws for function @{text nat}. *}
  4.1148 -
  4.1149 -lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
  4.1150 -apply (rule linorder_cases [of y 0])
  4.1151 -apply (simp add: div_nonneg_neg_le0)
  4.1152 -apply simp
  4.1153 -apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
  4.1154 -done
  4.1155 -
  4.1156 -(*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
  4.1157 -lemma nat_mod_distrib:
  4.1158 -  "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
  4.1159 -apply (case_tac "y = 0", simp add: DIVISION_BY_ZERO)
  4.1160 -apply (simp add: nat_eq_iff zmod_int)
  4.1161 -done
  4.1162 -
  4.1163 -text{*Suggested by Matthias Daum*}
  4.1164 -lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
  4.1165 -apply (subgoal_tac "nat x div nat k < nat x")
  4.1166 - apply (simp (asm_lr) add: nat_div_distrib [symmetric])
  4.1167 -apply (rule Divides.div_less_dividend, simp_all)
  4.1168 -done
  4.1169 -
  4.1170 -text {* code generator setup *}
  4.1171 -
  4.1172 -context ring_1
  4.1173 -begin
  4.1174 -
  4.1175 -lemma of_int_num [code]:
  4.1176 -  "of_int k = (if k = 0 then 0 else if k < 0 then
  4.1177 -     - of_int (- k) else let
  4.1178 -       (l, m) = divmod_int k 2;
  4.1179 -       l' = of_int l
  4.1180 -     in if m = 0 then l' + l' else l' + l' + 1)"
  4.1181 -proof -
  4.1182 -  have aux1: "k mod (2\<Colon>int) \<noteq> (0\<Colon>int) \<Longrightarrow> 
  4.1183 -    of_int k = of_int (k div 2 * 2 + 1)"
  4.1184 -  proof -
  4.1185 -    have "k mod 2 < 2" by (auto intro: pos_mod_bound)
  4.1186 -    moreover have "0 \<le> k mod 2" by (auto intro: pos_mod_sign)
  4.1187 -    moreover assume "k mod 2 \<noteq> 0"
  4.1188 -    ultimately have "k mod 2 = 1" by arith
  4.1189 -    moreover have "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
  4.1190 -    ultimately show ?thesis by auto
  4.1191 -  qed
  4.1192 -  have aux2: "\<And>x. of_int 2 * x = x + x"
  4.1193 -  proof -
  4.1194 -    fix x
  4.1195 -    have int2: "(2::int) = 1 + 1" by arith
  4.1196 -    show "of_int 2 * x = x + x"
  4.1197 -    unfolding int2 of_int_add left_distrib by simp
  4.1198 -  qed
  4.1199 -  have aux3: "\<And>x. x * of_int 2 = x + x"
  4.1200 -  proof -
  4.1201 -    fix x
  4.1202 -    have int2: "(2::int) = 1 + 1" by arith
  4.1203 -    show "x * of_int 2 = x + x" 
  4.1204 -    unfolding int2 of_int_add right_distrib by simp
  4.1205 -  qed
  4.1206 -  from aux1 show ?thesis by (auto simp add: divmod_int_mod_div Let_def aux2 aux3)
  4.1207 -qed
  4.1208 -
  4.1209 -end
  4.1210 -
  4.1211 -lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
  4.1212 -proof
  4.1213 -  assume H: "x mod n = y mod n"
  4.1214 -  hence "x mod n - y mod n = 0" by simp
  4.1215 -  hence "(x mod n - y mod n) mod n = 0" by simp 
  4.1216 -  hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
  4.1217 -  thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
  4.1218 -next
  4.1219 -  assume H: "n dvd x - y"
  4.1220 -  then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
  4.1221 -  hence "x = n*k + y" by simp
  4.1222 -  hence "x mod n = (n*k + y) mod n" by simp
  4.1223 -  thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
  4.1224 -qed
  4.1225 -
  4.1226 -lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
  4.1227 -  shows "\<exists>q. x = y + n * q"
  4.1228 -proof-
  4.1229 -  from xy have th: "int x - int y = int (x - y)" by simp 
  4.1230 -  from xyn have "int x mod int n = int y mod int n" 
  4.1231 -    by (simp add: zmod_int[symmetric])
  4.1232 -  hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric]) 
  4.1233 -  hence "n dvd x - y" by (simp add: th zdvd_int)
  4.1234 -  then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
  4.1235 -qed
  4.1236 -
  4.1237 -lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" 
  4.1238 -  (is "?lhs = ?rhs")
  4.1239 -proof
  4.1240 -  assume H: "x mod n = y mod n"
  4.1241 -  {assume xy: "x \<le> y"
  4.1242 -    from H have th: "y mod n = x mod n" by simp
  4.1243 -    from nat_mod_eq_lemma[OF th xy] have ?rhs 
  4.1244 -      apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
  4.1245 -  moreover
  4.1246 -  {assume xy: "y \<le> x"
  4.1247 -    from nat_mod_eq_lemma[OF H xy] have ?rhs 
  4.1248 -      apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
  4.1249 -  ultimately  show ?rhs using linear[of x y] by blast  
  4.1250 -next
  4.1251 -  assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
  4.1252 -  hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  4.1253 -  thus  ?lhs by simp
  4.1254 -qed
  4.1255 -
  4.1256 -lemma div_nat_number_of [simp]:
  4.1257 -     "(number_of v :: nat)  div  number_of v' =  
  4.1258 -          (if neg (number_of v :: int) then 0  
  4.1259 -           else nat (number_of v div number_of v'))"
  4.1260 -  unfolding nat_number_of_def number_of_is_id neg_def
  4.1261 -  by (simp add: nat_div_distrib)
  4.1262 -
  4.1263 -lemma one_div_nat_number_of [simp]:
  4.1264 -     "Suc 0 div number_of v' = nat (1 div number_of v')" 
  4.1265 -by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
  4.1266 -
  4.1267 -lemma mod_nat_number_of [simp]:
  4.1268 -     "(number_of v :: nat)  mod  number_of v' =  
  4.1269 -        (if neg (number_of v :: int) then 0  
  4.1270 -         else if neg (number_of v' :: int) then number_of v  
  4.1271 -         else nat (number_of v mod number_of v'))"
  4.1272 -  unfolding nat_number_of_def number_of_is_id neg_def
  4.1273 -  by (simp add: nat_mod_distrib)
  4.1274 -
  4.1275 -lemma one_mod_nat_number_of [simp]:
  4.1276 -     "Suc 0 mod number_of v' =  
  4.1277 -        (if neg (number_of v' :: int) then Suc 0
  4.1278 -         else nat (1 mod number_of v'))"
  4.1279 -by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
  4.1280 -
  4.1281 -lemmas dvd_eq_mod_eq_0_number_of =
  4.1282 -  dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
  4.1283 -
  4.1284 -declare dvd_eq_mod_eq_0_number_of [simp]
  4.1285 -
  4.1286 -
  4.1287 -subsection {* Transfer setup *}
  4.1288 -
  4.1289 -lemma transfer_nat_int_functions:
  4.1290 -    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
  4.1291 -    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
  4.1292 -  by (auto simp add: nat_div_distrib nat_mod_distrib)
  4.1293 -
  4.1294 -lemma transfer_nat_int_function_closures:
  4.1295 -    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
  4.1296 -    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
  4.1297 -  apply (cases "y = 0")
  4.1298 -  apply (auto simp add: pos_imp_zdiv_nonneg_iff)
  4.1299 -  apply (cases "y = 0")
  4.1300 -  apply auto
  4.1301 -done
  4.1302 -
  4.1303 -declare TransferMorphism_nat_int[transfer add return:
  4.1304 -  transfer_nat_int_functions
  4.1305 -  transfer_nat_int_function_closures
  4.1306 -]
  4.1307 -
  4.1308 -lemma transfer_int_nat_functions:
  4.1309 -    "(int x) div (int y) = int (x div y)"
  4.1310 -    "(int x) mod (int y) = int (x mod y)"
  4.1311 -  by (auto simp add: zdiv_int zmod_int)
  4.1312 -
  4.1313 -lemma transfer_int_nat_function_closures:
  4.1314 -    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
  4.1315 -    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
  4.1316 -  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
  4.1317 -
  4.1318 -declare TransferMorphism_int_nat[transfer add return:
  4.1319 -  transfer_int_nat_functions
  4.1320 -  transfer_int_nat_function_closures
  4.1321 -]
  4.1322 -
  4.1323 -
  4.1324 -subsection {* Code generation *}
  4.1325 -
  4.1326 -definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  4.1327 -  "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  4.1328 -
  4.1329 -lemma pdivmod_posDivAlg [code]:
  4.1330 -  "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"
  4.1331 -by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)
  4.1332 -
  4.1333 -lemma divmod_int_pdivmod: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  4.1334 -  apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
  4.1335 -    then pdivmod k l
  4.1336 -    else (let (r, s) = pdivmod k l in
  4.1337 -      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  4.1338 -proof -
  4.1339 -  have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
  4.1340 -  show ?thesis
  4.1341 -    by (simp add: divmod_int_mod_div pdivmod_def)
  4.1342 -      (auto simp add: aux not_less not_le zdiv_zminus1_eq_if
  4.1343 -      zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)
  4.1344 -qed
  4.1345 -
  4.1346 -lemma divmod_int_code [code]: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  4.1347 -  apsnd ((op *) (sgn l)) (if sgn k = sgn l
  4.1348 -    then pdivmod k l
  4.1349 -    else (let (r, s) = pdivmod k l in
  4.1350 -      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  4.1351 -proof -
  4.1352 -  have "k \<noteq> 0 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0 \<longleftrightarrow> sgn k = sgn l"
  4.1353 -    by (auto simp add: not_less sgn_if)
  4.1354 -  then show ?thesis by (simp add: divmod_int_pdivmod)
  4.1355 -qed
  4.1356 -
  4.1357 -code_modulename SML
  4.1358 -  IntDiv Integer
  4.1359 -
  4.1360 -code_modulename OCaml
  4.1361 -  IntDiv Integer
  4.1362 -
  4.1363 -code_modulename Haskell
  4.1364 -  IntDiv Integer
  4.1365 -
  4.1366 -
  4.1367 -
  4.1368 -subsection {* Proof Tools setup; Combination and Cancellation Simprocs *}
  4.1369 -
  4.1370 -declare split_div[of _ _ "number_of k", standard, arith_split]
  4.1371 -declare split_mod[of _ _ "number_of k", standard, arith_split]
  4.1372 -
  4.1373 -
  4.1374 -subsubsection{*For @{text combine_numerals}*}
  4.1375 -
  4.1376 -lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
  4.1377 -by (simp add: add_mult_distrib)
  4.1378 -
  4.1379 -
  4.1380 -subsubsection{*For @{text cancel_numerals}*}
  4.1381 -
  4.1382 -lemma nat_diff_add_eq1:
  4.1383 -     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
  4.1384 -by (simp split add: nat_diff_split add: add_mult_distrib)
  4.1385 -
  4.1386 -lemma nat_diff_add_eq2:
  4.1387 -     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
  4.1388 -by (simp split add: nat_diff_split add: add_mult_distrib)
  4.1389 -
  4.1390 -lemma nat_eq_add_iff1:
  4.1391 -     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
  4.1392 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1393 -
  4.1394 -lemma nat_eq_add_iff2:
  4.1395 -     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
  4.1396 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1397 -
  4.1398 -lemma nat_less_add_iff1:
  4.1399 -     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
  4.1400 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1401 -
  4.1402 -lemma nat_less_add_iff2:
  4.1403 -     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
  4.1404 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1405 -
  4.1406 -lemma nat_le_add_iff1:
  4.1407 -     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
  4.1408 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1409 -
  4.1410 -lemma nat_le_add_iff2:
  4.1411 -     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
  4.1412 -by (auto split add: nat_diff_split simp add: add_mult_distrib)
  4.1413 -
  4.1414 -
  4.1415 -subsubsection{*For @{text cancel_numeral_factors} *}
  4.1416 -
  4.1417 -lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
  4.1418 -by auto
  4.1419 -
  4.1420 -lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
  4.1421 -by auto
  4.1422 -
  4.1423 -lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
  4.1424 -by auto
  4.1425 -
  4.1426 -lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
  4.1427 -by auto
  4.1428 -
  4.1429 -lemma nat_mult_dvd_cancel_disj[simp]:
  4.1430 -  "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
  4.1431 -by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
  4.1432 -
  4.1433 -lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
  4.1434 -by(auto)
  4.1435 -
  4.1436 -
  4.1437 -subsubsection{*For @{text cancel_factor} *}
  4.1438 -
  4.1439 -lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
  4.1440 -by auto
  4.1441 -
  4.1442 -lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
  4.1443 -by auto
  4.1444 -
  4.1445 -lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
  4.1446 -by auto
  4.1447 -
  4.1448 -lemma nat_mult_div_cancel_disj[simp]:
  4.1449 -     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
  4.1450 -by (simp add: nat_mult_div_cancel1)
  4.1451 -
  4.1452 -
  4.1453 -use "Tools/numeral_simprocs.ML"
  4.1454 -
  4.1455 -use "Tools/nat_numeral_simprocs.ML"
  4.1456 -
  4.1457 -declaration {* 
  4.1458 -  K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
  4.1459 -  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
  4.1460 -     @{thm nat_0}, @{thm nat_1},
  4.1461 -     @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
  4.1462 -     @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
  4.1463 -     @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
  4.1464 -     @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
  4.1465 -     @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
  4.1466 -     @{thm mult_Suc}, @{thm mult_Suc_right},
  4.1467 -     @{thm add_Suc}, @{thm add_Suc_right},
  4.1468 -     @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
  4.1469 -     @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
  4.1470 -     @{thm if_True}, @{thm if_False}])
  4.1471 -  #> Lin_Arith.add_simprocs (Numeral_Simprocs.assoc_fold_simproc
  4.1472 -      :: Numeral_Simprocs.combine_numerals
  4.1473 -      :: Numeral_Simprocs.cancel_numerals)
  4.1474 -  #> Lin_Arith.add_simprocs (Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals))
  4.1475 -*}
  4.1476 -
  4.1477 -end
     5.1 --- a/src/HOL/Library/Numeral_Type.thy	Fri Oct 30 13:59:52 2009 +0100
     5.2 +++ b/src/HOL/Library/Numeral_Type.thy	Fri Oct 30 14:00:43 2009 +0100
     5.3 @@ -188,7 +188,7 @@
     5.4  by (rule type_definition.Abs_inverse [OF type])
     5.5  
     5.6  lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
     5.7 -by (simp add: Abs_inverse IntDiv.pos_mod_conj [OF size0])
     5.8 +by (simp add: Abs_inverse pos_mod_conj [OF size0])
     5.9  
    5.10  lemma Rep_Abs_0: "Rep (Abs 0) = 0"
    5.11  by (simp add: Abs_inverse size0)