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