src/HOL/IntDiv.thy
 author haftmann Thu Oct 29 11:41:36 2009 +0100 (2009-10-29) changeset 33318 ddd97d9dfbfb parent 33296 a3924d1069e5 child 33340 a165b97f3658 permissions -rw-r--r--
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
     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_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"

  1028   by (rule dvd_mod) (* TODO: remove *)

  1029

  1030 lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"

  1031   by (rule dvd_mod_imp_dvd) (* TODO: remove *)

  1032

  1033 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"

  1034   using zmod_zdiv_equality[where a="m" and b="n"]

  1035   by (simp add: algebra_simps)

  1036

  1037 lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"

  1038 apply (induct "y", auto)

  1039 apply (rule zmod_zmult1_eq [THEN trans])

  1040 apply (simp (no_asm_simp))

  1041 apply (rule mod_mult_eq [symmetric])

  1042 done

  1043

  1044 lemma zdiv_int: "int (a div b) = (int a) div (int b)"

  1045 apply (subst split_div, auto)

  1046 apply (subst split_zdiv, auto)

  1047 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)

  1048 apply (auto simp add: IntDiv.divmod_rel_def of_nat_mult)

  1049 done

  1050

  1051 lemma zmod_int: "int (a mod b) = (int a) mod (int b)"

  1052 apply (subst split_mod, auto)

  1053 apply (subst split_zmod, auto)

  1054 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia

  1055        in unique_remainder)

  1056 apply (auto simp add: IntDiv.divmod_rel_def of_nat_mult)

  1057 done

  1058

  1059 lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"

  1060 by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)

  1061

  1062 lemma zdvd_mult_div_cancel:"(n::int) dvd m \<Longrightarrow> n * (m div n) = m"

  1063 apply (subgoal_tac "m mod n = 0")

  1064  apply (simp add: zmult_div_cancel)

  1065 apply (simp only: dvd_eq_mod_eq_0)

  1066 done

  1067

  1068 text{*Suggested by Matthias Daum*}

  1069 lemma int_power_div_base:

  1070      "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"

  1071 apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")

  1072  apply (erule ssubst)

  1073  apply (simp only: power_add)

  1074  apply simp_all

  1075 done

  1076

  1077 text {* by Brian Huffman *}

  1078 lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"

  1079 by (rule mod_minus_eq [symmetric])

  1080

  1081 lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"

  1082 by (rule mod_diff_left_eq [symmetric])

  1083

  1084 lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"

  1085 by (rule mod_diff_right_eq [symmetric])

  1086

  1087 lemmas zmod_simps =

  1088   mod_add_left_eq  [symmetric]

  1089   mod_add_right_eq [symmetric]

  1090   zmod_zmult1_eq   [symmetric]

  1091   mod_mult_left_eq [symmetric]

  1092   zpower_zmod

  1093   zminus_zmod zdiff_zmod_left zdiff_zmod_right

  1094

  1095 text {* Distributive laws for function @{text nat}. *}

  1096

  1097 lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"

  1098 apply (rule linorder_cases [of y 0])

  1099 apply (simp add: div_nonneg_neg_le0)

  1100 apply simp

  1101 apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)

  1102 done

  1103

  1104 (*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)

  1105 lemma nat_mod_distrib:

  1106   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"

  1107 apply (case_tac "y = 0", simp add: DIVISION_BY_ZERO)

  1108 apply (simp add: nat_eq_iff zmod_int)

  1109 done

  1110

  1111 text{*Suggested by Matthias Daum*}

  1112 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"

  1113 apply (subgoal_tac "nat x div nat k < nat x")

  1114  apply (simp (asm_lr) add: nat_div_distrib [symmetric])

  1115 apply (rule Divides.div_less_dividend, simp_all)

  1116 done

  1117

  1118 text {* code generator setup *}

  1119

  1120 context ring_1

  1121 begin

  1122

  1123 lemma of_int_num [code]:

  1124   "of_int k = (if k = 0 then 0 else if k < 0 then

  1125      - of_int (- k) else let

  1126        (l, m) = divmod k 2;

  1127        l' = of_int l

  1128      in if m = 0 then l' + l' else l' + l' + 1)"

  1129 proof -

  1130   have aux1: "k mod (2\<Colon>int) \<noteq> (0\<Colon>int) \<Longrightarrow>

  1131     of_int k = of_int (k div 2 * 2 + 1)"

  1132   proof -

  1133     have "k mod 2 < 2" by (auto intro: pos_mod_bound)

  1134     moreover have "0 \<le> k mod 2" by (auto intro: pos_mod_sign)

  1135     moreover assume "k mod 2 \<noteq> 0"

  1136     ultimately have "k mod 2 = 1" by arith

  1137     moreover have "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp

  1138     ultimately show ?thesis by auto

  1139   qed

  1140   have aux2: "\<And>x. of_int 2 * x = x + x"

  1141   proof -

  1142     fix x

  1143     have int2: "(2::int) = 1 + 1" by arith

  1144     show "of_int 2 * x = x + x"

  1145     unfolding int2 of_int_add left_distrib by simp

  1146   qed

  1147   have aux3: "\<And>x. x * of_int 2 = x + x"

  1148   proof -

  1149     fix x

  1150     have int2: "(2::int) = 1 + 1" by arith

  1151     show "x * of_int 2 = x + x"

  1152     unfolding int2 of_int_add right_distrib by simp

  1153   qed

  1154   from aux1 show ?thesis by (auto simp add: divmod_mod_div Let_def aux2 aux3)

  1155 qed

  1156

  1157 end

  1158

  1159 lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"

  1160 proof

  1161   assume H: "x mod n = y mod n"

  1162   hence "x mod n - y mod n = 0" by simp

  1163   hence "(x mod n - y mod n) mod n = 0" by simp

  1164   hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])

  1165   thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)

  1166 next

  1167   assume H: "n dvd x - y"

  1168   then obtain k where k: "x-y = n*k" unfolding dvd_def by blast

  1169   hence "x = n*k + y" by simp

  1170   hence "x mod n = (n*k + y) mod n" by simp

  1171   thus "x mod n = y mod n" by (simp add: mod_add_left_eq)

  1172 qed

  1173

  1174 lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"

  1175   shows "\<exists>q. x = y + n * q"

  1176 proof-

  1177   from xy have th: "int x - int y = int (x - y)" by simp

  1178   from xyn have "int x mod int n = int y mod int n"

  1179     by (simp add: zmod_int[symmetric])

  1180   hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric])

  1181   hence "n dvd x - y" by (simp add: th zdvd_int)

  1182   then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith

  1183 qed

  1184

  1185 lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)"

  1186   (is "?lhs = ?rhs")

  1187 proof

  1188   assume H: "x mod n = y mod n"

  1189   {assume xy: "x \<le> y"

  1190     from H have th: "y mod n = x mod n" by simp

  1191     from nat_mod_eq_lemma[OF th xy] have ?rhs

  1192       apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}

  1193   moreover

  1194   {assume xy: "y \<le> x"

  1195     from nat_mod_eq_lemma[OF H xy] have ?rhs

  1196       apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}

  1197   ultimately  show ?rhs using linear[of x y] by blast

  1198 next

  1199   assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast

  1200   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp

  1201   thus  ?lhs by simp

  1202 qed

  1203

  1204 lemma div_nat_number_of [simp]:

  1205      "(number_of v :: nat)  div  number_of v' =

  1206           (if neg (number_of v :: int) then 0

  1207            else nat (number_of v div number_of v'))"

  1208   unfolding nat_number_of_def number_of_is_id neg_def

  1209   by (simp add: nat_div_distrib)

  1210

  1211 lemma one_div_nat_number_of [simp]:

  1212      "Suc 0 div number_of v' = nat (1 div number_of v')"

  1213 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])

  1214

  1215 lemma mod_nat_number_of [simp]:

  1216      "(number_of v :: nat)  mod  number_of v' =

  1217         (if neg (number_of v :: int) then 0

  1218          else if neg (number_of v' :: int) then number_of v

  1219          else nat (number_of v mod number_of v'))"

  1220   unfolding nat_number_of_def number_of_is_id neg_def

  1221   by (simp add: nat_mod_distrib)

  1222

  1223 lemma one_mod_nat_number_of [simp]:

  1224      "Suc 0 mod number_of v' =

  1225         (if neg (number_of v' :: int) then Suc 0

  1226          else nat (1 mod number_of v'))"

  1227 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])

  1228

  1229 lemmas dvd_eq_mod_eq_0_number_of =

  1230   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]

  1231

  1232 declare dvd_eq_mod_eq_0_number_of [simp]

  1233

  1234

  1235 subsection {* Transfer setup *}

  1236

  1237 lemma transfer_nat_int_functions:

  1238     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"

  1239     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"

  1240   by (auto simp add: nat_div_distrib nat_mod_distrib)

  1241

  1242 lemma transfer_nat_int_function_closures:

  1243     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"

  1244     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"

  1245   apply (cases "y = 0")

  1246   apply (auto simp add: pos_imp_zdiv_nonneg_iff)

  1247   apply (cases "y = 0")

  1248   apply auto

  1249 done

  1250

  1251 declare TransferMorphism_nat_int[transfer add return:

  1252   transfer_nat_int_functions

  1253   transfer_nat_int_function_closures

  1254 ]

  1255

  1256 lemma transfer_int_nat_functions:

  1257     "(int x) div (int y) = int (x div y)"

  1258     "(int x) mod (int y) = int (x mod y)"

  1259   by (auto simp add: zdiv_int zmod_int)

  1260

  1261 lemma transfer_int_nat_function_closures:

  1262     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"

  1263     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"

  1264   by (simp_all only: is_nat_def transfer_nat_int_function_closures)

  1265

  1266 declare TransferMorphism_int_nat[transfer add return:

  1267   transfer_int_nat_functions

  1268   transfer_int_nat_function_closures

  1269 ]

  1270

  1271

  1272 subsection {* Code generation *}

  1273

  1274 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where

  1275   "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"

  1276

  1277 lemma pdivmod_posDivAlg [code]:

  1278   "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"

  1279 by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)

  1280

  1281 lemma divmod_pdivmod: "divmod k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else

  1282   apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0

  1283     then pdivmod k l

  1284     else (let (r, s) = pdivmod k l in

  1285       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"

  1286 proof -

  1287   have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto

  1288   show ?thesis

  1289     by (simp add: divmod_mod_div pdivmod_def)

  1290       (auto simp add: aux not_less not_le zdiv_zminus1_eq_if

  1291       zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)

  1292 qed

  1293

  1294 lemma divmod_code [code]: "divmod k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else

  1295   apsnd ((op *) (sgn l)) (if sgn k = sgn l

  1296     then pdivmod k l

  1297     else (let (r, s) = pdivmod k l in

  1298       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"

  1299 proof -

  1300   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"

  1301     by (auto simp add: not_less sgn_if)

  1302   then show ?thesis by (simp add: divmod_pdivmod)

  1303 qed

  1304

  1305 code_modulename SML

  1306   IntDiv Integer

  1307

  1308 code_modulename OCaml

  1309   IntDiv Integer

  1310

  1311 code_modulename Haskell

  1312   IntDiv Integer

  1313

  1314 end
`