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