src/HOL/Integ/IntDiv.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15131 c69542757a4d
child 15221 8412cfdf3287
permissions -rw-r--r--
import -> imports
paulson@6917
     1
(*  Title:      HOL/IntDiv.thy
paulson@6917
     2
    ID:         $Id$
paulson@6917
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6917
     4
    Copyright   1999  University of Cambridge
paulson@6917
     5
paulson@6917
     6
The division operators div, mod and the divides relation "dvd"
paulson@13183
     7
paulson@13183
     8
Here is the division algorithm in ML:
paulson@13183
     9
paulson@13183
    10
    fun posDivAlg (a,b) =
paulson@13183
    11
      if a<b then (0,a)
paulson@13183
    12
      else let val (q,r) = posDivAlg(a, 2*b)
paulson@14288
    13
	       in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
paulson@13183
    14
	   end
paulson@13183
    15
paulson@13183
    16
    fun negDivAlg (a,b) =
paulson@14288
    17
      if 0\<le>a+b then (~1,a+b)
paulson@13183
    18
      else let val (q,r) = negDivAlg(a, 2*b)
paulson@14288
    19
	       in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
paulson@13183
    20
	   end;
paulson@13183
    21
paulson@13183
    22
    fun negateSnd (q,r:int) = (q,~r);
paulson@13183
    23
paulson@14288
    24
    fun divAlg (a,b) = if 0\<le>a then 
paulson@13183
    25
			  if b>0 then posDivAlg (a,b) 
paulson@13183
    26
			   else if a=0 then (0,0)
paulson@13183
    27
				else negateSnd (negDivAlg (~a,~b))
paulson@13183
    28
		       else 
paulson@13183
    29
			  if 0<b then negDivAlg (a,b)
paulson@13183
    30
			  else        negateSnd (posDivAlg (~a,~b));
paulson@6917
    31
*)
paulson@6917
    32
paulson@13183
    33
nipkow@15131
    34
theory IntDiv
nipkow@15140
    35
imports IntArith Recdef
nipkow@15131
    36
files ("IntDiv_setup.ML")
nipkow@15131
    37
begin
paulson@13183
    38
paulson@13183
    39
declare zless_nat_conj [simp]
paulson@6917
    40
paulson@6917
    41
constdefs
paulson@6917
    42
  quorem :: "(int*int) * (int*int) => bool"
paulson@6917
    43
    "quorem == %((a,b), (q,r)).
paulson@6917
    44
                      a = b*q + r &
paulson@14288
    45
                      (if 0 < b then 0\<le>r & r<b else b<r & r \<le> 0)"
paulson@6917
    46
paulson@11868
    47
  adjust :: "[int, int*int] => int*int"
paulson@14288
    48
    "adjust b == %(q,r). if 0 \<le> r-b then (2*q + 1, r-b)
paulson@11868
    49
                         else (2*q, r)"
paulson@6917
    50
paulson@6917
    51
(** the division algorithm **)
paulson@6917
    52
paulson@6917
    53
(*for the case a>=0, b>0*)
paulson@6917
    54
consts posDivAlg :: "int*int => int*int"
paulson@11868
    55
recdef posDivAlg "inv_image less_than (%(a,b). nat(a - b + 1))"
paulson@6917
    56
    "posDivAlg (a,b) =
paulson@14288
    57
       (if (a<b | b\<le>0) then (0,a)
paulson@11868
    58
        else adjust b (posDivAlg(a, 2*b)))"
paulson@6917
    59
paulson@6917
    60
(*for the case a<0, b>0*)
paulson@6917
    61
consts negDivAlg :: "int*int => int*int"
paulson@6917
    62
recdef negDivAlg "inv_image less_than (%(a,b). nat(- a - b))"
paulson@6917
    63
    "negDivAlg (a,b) =
paulson@14288
    64
       (if (0\<le>a+b | b\<le>0) then (-1,a+b)
paulson@11868
    65
        else adjust b (negDivAlg(a, 2*b)))"
paulson@6917
    66
paulson@6917
    67
(*for the general case b~=0*)
paulson@6917
    68
paulson@6917
    69
constdefs
paulson@6917
    70
  negateSnd :: "int*int => int*int"
paulson@6917
    71
    "negateSnd == %(q,r). (q,-r)"
paulson@6917
    72
paulson@6917
    73
  (*The full division algorithm considers all possible signs for a, b
paulson@6917
    74
    including the special case a=0, b<0, because negDivAlg requires a<0*)
paulson@6917
    75
  divAlg :: "int*int => int*int"
paulson@6917
    76
    "divAlg ==
paulson@14288
    77
       %(a,b). if 0\<le>a then
paulson@14288
    78
                  if 0\<le>b then posDivAlg (a,b)
paulson@11868
    79
                  else if a=0 then (0,0)
paulson@6917
    80
                       else negateSnd (negDivAlg (-a,-b))
paulson@6917
    81
               else 
paulson@11868
    82
                  if 0<b then negDivAlg (a,b)
paulson@6917
    83
                  else         negateSnd (posDivAlg (-a,-b))"
paulson@6917
    84
paulson@6917
    85
instance
paulson@13183
    86
  int :: "Divides.div" ..       (*avoid clash with 'div' token*)
paulson@6917
    87
paulson@6917
    88
defs
paulson@13183
    89
  div_def:   "a div b == fst (divAlg (a,b))"
paulson@13183
    90
  mod_def:   "a mod b == snd (divAlg (a,b))"
paulson@13183
    91
paulson@13183
    92
paulson@13183
    93
paulson@14271
    94
subsection{*Uniqueness and Monotonicity of Quotients and Remainders*}
paulson@13183
    95
paulson@13183
    96
lemma unique_quotient_lemma:
paulson@14288
    97
     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  0 < b;  r < b |]  
paulson@14288
    98
      ==> q' \<le> (q::int)"
paulson@14288
    99
apply (subgoal_tac "r' + b * (q'-q) \<le> r")
paulson@14479
   100
 prefer 2 apply (simp add: right_diff_distrib)
paulson@13183
   101
apply (subgoal_tac "0 < b * (1 + q - q') ")
paulson@13183
   102
apply (erule_tac [2] order_le_less_trans)
paulson@14479
   103
 prefer 2 apply (simp add: right_diff_distrib right_distrib)
paulson@13183
   104
apply (subgoal_tac "b * q' < b * (1 + q) ")
paulson@14479
   105
 prefer 2 apply (simp add: right_diff_distrib right_distrib)
paulson@14387
   106
apply (simp add: mult_less_cancel_left)
paulson@13183
   107
done
paulson@13183
   108
paulson@13183
   109
lemma unique_quotient_lemma_neg:
paulson@14288
   110
     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < 0;  b < r' |]  
paulson@14288
   111
      ==> q \<le> (q'::int)"
paulson@13183
   112
by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
paulson@13183
   113
    auto)
paulson@13183
   114
paulson@13183
   115
lemma unique_quotient:
paulson@13183
   116
     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b ~= 0 |]  
paulson@13183
   117
      ==> q = q'"
paulson@13183
   118
apply (simp add: quorem_def linorder_neq_iff split: split_if_asm)
paulson@13183
   119
apply (blast intro: order_antisym
paulson@13183
   120
             dest: order_eq_refl [THEN unique_quotient_lemma] 
paulson@13183
   121
             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
paulson@13183
   122
done
paulson@13183
   123
paulson@13183
   124
paulson@13183
   125
lemma unique_remainder:
paulson@13183
   126
     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b ~= 0 |]  
paulson@13183
   127
      ==> r = r'"
paulson@13183
   128
apply (subgoal_tac "q = q'")
paulson@13183
   129
 apply (simp add: quorem_def)
paulson@13183
   130
apply (blast intro: unique_quotient)
paulson@13183
   131
done
paulson@13183
   132
paulson@13183
   133
paulson@14271
   134
subsection{*Correctness of posDivAlg, the Algorithm for Non-Negative Dividends*}
paulson@14271
   135
paulson@14271
   136
text{*And positive divisors*}
paulson@13183
   137
paulson@13183
   138
lemma adjust_eq [simp]:
paulson@13183
   139
     "adjust b (q,r) = 
paulson@13183
   140
      (let diff = r-b in  
paulson@14288
   141
	if 0 \<le> diff then (2*q + 1, diff)   
paulson@13183
   142
                     else (2*q, r))"
paulson@13183
   143
by (simp add: Let_def adjust_def)
paulson@13183
   144
paulson@13183
   145
declare posDivAlg.simps [simp del]
paulson@13183
   146
paulson@13183
   147
(**use with a simproc to avoid repeatedly proving the premise*)
paulson@13183
   148
lemma posDivAlg_eqn:
paulson@13183
   149
     "0 < b ==>  
paulson@13183
   150
      posDivAlg (a,b) = (if a<b then (0,a) else adjust b (posDivAlg(a, 2*b)))"
paulson@13183
   151
by (rule posDivAlg.simps [THEN trans], simp)
paulson@13183
   152
paulson@13183
   153
(*Correctness of posDivAlg: it computes quotients correctly*)
paulson@13183
   154
lemma posDivAlg_correct [rule_format]:
paulson@14288
   155
     "0 \<le> a --> 0 < b --> quorem ((a, b), posDivAlg (a, b))"
paulson@13183
   156
apply (induct_tac a b rule: posDivAlg.induct, auto)
paulson@13183
   157
 apply (simp_all add: quorem_def)
paulson@13183
   158
 (*base case: a<b*)
paulson@13183
   159
 apply (simp add: posDivAlg_eqn)
paulson@13183
   160
(*main argument*)
paulson@13183
   161
apply (subst posDivAlg_eqn, simp_all)
paulson@13183
   162
apply (erule splitE)
paulson@14479
   163
apply (auto simp add: right_distrib Let_def)
paulson@13183
   164
done
paulson@13183
   165
paulson@13183
   166
paulson@14271
   167
subsection{*Correctness of negDivAlg, the Algorithm for Negative Dividends*}
paulson@14271
   168
paulson@14271
   169
text{*And positive divisors*}
paulson@13183
   170
paulson@13183
   171
declare negDivAlg.simps [simp del]
paulson@13183
   172
paulson@13183
   173
(**use with a simproc to avoid repeatedly proving the premise*)
paulson@13183
   174
lemma negDivAlg_eqn:
paulson@13183
   175
     "0 < b ==>  
paulson@13183
   176
      negDivAlg (a,b) =       
paulson@14288
   177
       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg(a, 2*b)))"
paulson@13183
   178
by (rule negDivAlg.simps [THEN trans], simp)
paulson@13183
   179
paulson@13183
   180
(*Correctness of negDivAlg: it computes quotients correctly
paulson@13183
   181
  It doesn't work if a=0 because the 0/b equals 0, not -1*)
paulson@13183
   182
lemma negDivAlg_correct [rule_format]:
paulson@13183
   183
     "a < 0 --> 0 < b --> quorem ((a, b), negDivAlg (a, b))"
paulson@13183
   184
apply (induct_tac a b rule: negDivAlg.induct, auto)
paulson@13183
   185
 apply (simp_all add: quorem_def)
paulson@14288
   186
 (*base case: 0\<le>a+b*)
paulson@13183
   187
 apply (simp add: negDivAlg_eqn)
paulson@13183
   188
(*main argument*)
paulson@13183
   189
apply (subst negDivAlg_eqn, assumption)
paulson@13183
   190
apply (erule splitE)
paulson@14479
   191
apply (auto simp add: right_distrib Let_def)
paulson@13183
   192
done
paulson@13183
   193
paulson@13183
   194
paulson@14271
   195
subsection{*Existence Shown by Proving the Division Algorithm to be Correct*}
paulson@13183
   196
paulson@13183
   197
(*the case a=0*)
paulson@13183
   198
lemma quorem_0: "b ~= 0 ==> quorem ((0,b), (0,0))"
paulson@13183
   199
by (auto simp add: quorem_def linorder_neq_iff)
paulson@13183
   200
paulson@13183
   201
lemma posDivAlg_0 [simp]: "posDivAlg (0, b) = (0, 0)"
paulson@13183
   202
by (subst posDivAlg.simps, auto)
paulson@13183
   203
paulson@13183
   204
lemma negDivAlg_minus1 [simp]: "negDivAlg (-1, b) = (-1, b - 1)"
paulson@13183
   205
by (subst negDivAlg.simps, auto)
paulson@13183
   206
paulson@13183
   207
lemma negateSnd_eq [simp]: "negateSnd(q,r) = (q,-r)"
paulson@13183
   208
by (unfold negateSnd_def, auto)
paulson@13183
   209
paulson@13183
   210
lemma quorem_neg: "quorem ((-a,-b), qr) ==> quorem ((a,b), negateSnd qr)"
paulson@13183
   211
by (auto simp add: split_ifs quorem_def)
paulson@13183
   212
paulson@13183
   213
lemma divAlg_correct: "b ~= 0 ==> quorem ((a,b), divAlg(a,b))"
paulson@13183
   214
by (force simp add: linorder_neq_iff quorem_0 divAlg_def quorem_neg
paulson@13183
   215
                    posDivAlg_correct negDivAlg_correct)
paulson@13183
   216
paulson@13183
   217
(** Arbitrary definitions for division by zero.  Useful to simplify 
paulson@13183
   218
    certain equations **)
paulson@13183
   219
paulson@14271
   220
lemma DIVISION_BY_ZERO [simp]: "a div (0::int) = 0 & a mod (0::int) = a"
paulson@14271
   221
by (simp add: div_def mod_def divAlg_def posDivAlg.simps)  
paulson@13183
   222
paulson@13183
   223
(** Basic laws about division and remainder **)
paulson@13183
   224
paulson@13183
   225
lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
paulson@15013
   226
apply (case_tac "b = 0", simp)
paulson@13183
   227
apply (cut_tac a = a and b = b in divAlg_correct)
paulson@13183
   228
apply (auto simp add: quorem_def div_def mod_def)
paulson@13183
   229
done
paulson@13183
   230
nipkow@13517
   231
lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"
nipkow@13517
   232
by(simp add: zmod_zdiv_equality[symmetric])
nipkow@13517
   233
nipkow@13517
   234
lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"
nipkow@13517
   235
by(simp add: zmult_commute zmod_zdiv_equality[symmetric])
nipkow@13517
   236
nipkow@13517
   237
use "IntDiv_setup.ML"
nipkow@13517
   238
paulson@14288
   239
lemma pos_mod_conj : "(0::int) < b ==> 0 \<le> a mod b & a mod b < b"
paulson@13183
   240
apply (cut_tac a = a and b = b in divAlg_correct)
paulson@13183
   241
apply (auto simp add: quorem_def mod_def)
paulson@13183
   242
done
paulson@13183
   243
nipkow@13788
   244
lemmas pos_mod_sign[simp]  = pos_mod_conj [THEN conjunct1, standard]
nipkow@13788
   245
   and pos_mod_bound[simp] = pos_mod_conj [THEN conjunct2, standard]
paulson@13183
   246
paulson@14288
   247
lemma neg_mod_conj : "b < (0::int) ==> a mod b \<le> 0 & b < a mod b"
paulson@13183
   248
apply (cut_tac a = a and b = b in divAlg_correct)
paulson@13183
   249
apply (auto simp add: quorem_def div_def mod_def)
paulson@13183
   250
done
paulson@13183
   251
nipkow@13788
   252
lemmas neg_mod_sign[simp]  = neg_mod_conj [THEN conjunct1, standard]
nipkow@13788
   253
   and neg_mod_bound[simp] = neg_mod_conj [THEN conjunct2, standard]
paulson@13183
   254
paulson@13183
   255
paulson@13260
   256
paulson@13183
   257
(** proving general properties of div and mod **)
paulson@13183
   258
paulson@13183
   259
lemma quorem_div_mod: "b ~= 0 ==> quorem ((a, b), (a div b, a mod b))"
paulson@13183
   260
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
nipkow@13788
   261
apply (force simp add: quorem_def linorder_neq_iff)
paulson@13183
   262
done
paulson@13183
   263
paulson@13183
   264
lemma quorem_div: "[| quorem((a,b),(q,r));  b ~= 0 |] ==> a div b = q"
paulson@13183
   265
by (simp add: quorem_div_mod [THEN unique_quotient])
paulson@13183
   266
paulson@13183
   267
lemma quorem_mod: "[| quorem((a,b),(q,r));  b ~= 0 |] ==> a mod b = r"
paulson@13183
   268
by (simp add: quorem_div_mod [THEN unique_remainder])
paulson@13183
   269
paulson@14288
   270
lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
paulson@13183
   271
apply (rule quorem_div)
paulson@13183
   272
apply (auto simp add: quorem_def)
paulson@13183
   273
done
paulson@13183
   274
paulson@14288
   275
lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
paulson@13183
   276
apply (rule quorem_div)
paulson@13183
   277
apply (auto simp add: quorem_def)
paulson@13183
   278
done
paulson@13183
   279
paulson@14288
   280
lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
paulson@13183
   281
apply (rule quorem_div)
paulson@13183
   282
apply (auto simp add: quorem_def)
paulson@13183
   283
done
paulson@13183
   284
paulson@13183
   285
(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
paulson@13183
   286
paulson@14288
   287
lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
paulson@13183
   288
apply (rule_tac q = 0 in quorem_mod)
paulson@13183
   289
apply (auto simp add: quorem_def)
paulson@13183
   290
done
paulson@13183
   291
paulson@14288
   292
lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
paulson@13183
   293
apply (rule_tac q = 0 in quorem_mod)
paulson@13183
   294
apply (auto simp add: quorem_def)
paulson@13183
   295
done
paulson@13183
   296
paulson@14288
   297
lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
paulson@13183
   298
apply (rule_tac q = "-1" in quorem_mod)
paulson@13183
   299
apply (auto simp add: quorem_def)
paulson@13183
   300
done
paulson@13183
   301
paulson@13183
   302
(*There is no mod_neg_pos_trivial...*)
paulson@13183
   303
paulson@13183
   304
paulson@13183
   305
(*Simpler laws such as -a div b = -(a div b) FAIL, but see just below*)
paulson@13183
   306
lemma zdiv_zminus_zminus [simp]: "(-a) div (-b) = a div (b::int)"
paulson@15013
   307
apply (case_tac "b = 0", simp)
paulson@13183
   308
apply (simp add: quorem_div_mod [THEN quorem_neg, simplified, 
paulson@13183
   309
                                 THEN quorem_div, THEN sym])
paulson@13183
   310
paulson@13183
   311
done
paulson@13183
   312
paulson@13183
   313
(*Simpler laws such as -a mod b = -(a mod b) FAIL, but see just below*)
paulson@13183
   314
lemma zmod_zminus_zminus [simp]: "(-a) mod (-b) = - (a mod (b::int))"
paulson@15013
   315
apply (case_tac "b = 0", simp)
paulson@13183
   316
apply (subst quorem_div_mod [THEN quorem_neg, simplified, THEN quorem_mod],
paulson@13183
   317
       auto)
paulson@13183
   318
done
paulson@13183
   319
paulson@14271
   320
subsection{*div, mod and unary minus*}
paulson@13183
   321
paulson@13183
   322
lemma zminus1_lemma:
paulson@13183
   323
     "quorem((a,b),(q,r))  
paulson@13183
   324
      ==> quorem ((-a,b), (if r=0 then -q else -q - 1),  
paulson@13183
   325
                          (if r=0 then 0 else b-r))"
paulson@14479
   326
by (force simp add: split_ifs quorem_def linorder_neq_iff right_diff_distrib)
paulson@13183
   327
paulson@13183
   328
paulson@13183
   329
lemma zdiv_zminus1_eq_if:
paulson@13183
   330
     "b ~= (0::int)  
paulson@13183
   331
      ==> (-a) div b =  
paulson@13183
   332
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
paulson@13183
   333
by (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_div])
paulson@13183
   334
paulson@13183
   335
lemma zmod_zminus1_eq_if:
paulson@13183
   336
     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
paulson@15013
   337
apply (case_tac "b = 0", simp)
paulson@13183
   338
apply (blast intro: quorem_div_mod [THEN zminus1_lemma, THEN quorem_mod])
paulson@13183
   339
done
paulson@13183
   340
paulson@13183
   341
lemma zdiv_zminus2: "a div (-b) = (-a::int) div b"
paulson@13183
   342
by (cut_tac a = "-a" in zdiv_zminus_zminus, auto)
paulson@13183
   343
paulson@13183
   344
lemma zmod_zminus2: "a mod (-b) = - ((-a::int) mod b)"
paulson@13183
   345
by (cut_tac a = "-a" and b = b in zmod_zminus_zminus, auto)
paulson@13183
   346
paulson@13183
   347
lemma zdiv_zminus2_eq_if:
paulson@13183
   348
     "b ~= (0::int)  
paulson@13183
   349
      ==> a div (-b) =  
paulson@13183
   350
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
paulson@13183
   351
by (simp add: zdiv_zminus1_eq_if zdiv_zminus2)
paulson@13183
   352
paulson@13183
   353
lemma zmod_zminus2_eq_if:
paulson@13183
   354
     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
paulson@13183
   355
by (simp add: zmod_zminus1_eq_if zmod_zminus2)
paulson@13183
   356
paulson@13183
   357
paulson@14271
   358
subsection{*Division of a Number by Itself*}
paulson@13183
   359
paulson@14288
   360
lemma self_quotient_aux1: "[| (0::int) < a; a = r + a*q; r < a |] ==> 1 \<le> q"
paulson@13183
   361
apply (subgoal_tac "0 < a*q")
paulson@14353
   362
 apply (simp add: zero_less_mult_iff, arith)
paulson@13183
   363
done
paulson@13183
   364
paulson@14288
   365
lemma self_quotient_aux2: "[| (0::int) < a; a = r + a*q; 0 \<le> r |] ==> q \<le> 1"
paulson@14288
   366
apply (subgoal_tac "0 \<le> a* (1-q) ")
paulson@14353
   367
 apply (simp add: zero_le_mult_iff)
paulson@14479
   368
apply (simp add: right_diff_distrib)
paulson@13183
   369
done
paulson@13183
   370
paulson@13183
   371
lemma self_quotient: "[| quorem((a,a),(q,r));  a ~= (0::int) |] ==> q = 1"
paulson@13183
   372
apply (simp add: split_ifs quorem_def linorder_neq_iff)
berghofe@13601
   373
apply (rule order_antisym, safe, simp_all (no_asm_use))
wenzelm@13524
   374
apply (rule_tac [3] a = "-a" and r = "-r" in self_quotient_aux1)
wenzelm@13524
   375
apply (rule_tac a = "-a" and r = "-r" in self_quotient_aux2)
berghofe@13601
   376
apply (force intro: self_quotient_aux1 self_quotient_aux2 simp only: zadd_commute zmult_zminus)+
paulson@13183
   377
done
paulson@13183
   378
paulson@13183
   379
lemma self_remainder: "[| quorem((a,a),(q,r));  a ~= (0::int) |] ==> r = 0"
paulson@13183
   380
apply (frule self_quotient, assumption)
paulson@13183
   381
apply (simp add: quorem_def)
paulson@13183
   382
done
paulson@13183
   383
paulson@13183
   384
lemma zdiv_self [simp]: "a ~= 0 ==> a div a = (1::int)"
paulson@13183
   385
by (simp add: quorem_div_mod [THEN self_quotient])
paulson@13183
   386
paulson@13183
   387
(*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)
paulson@13183
   388
lemma zmod_self [simp]: "a mod a = (0::int)"
paulson@15013
   389
apply (case_tac "a = 0", simp)
paulson@13183
   390
apply (simp add: quorem_div_mod [THEN self_remainder])
paulson@13183
   391
done
paulson@13183
   392
paulson@13183
   393
paulson@14271
   394
subsection{*Computation of Division and Remainder*}
paulson@13183
   395
paulson@13183
   396
lemma zdiv_zero [simp]: "(0::int) div b = 0"
paulson@13183
   397
by (simp add: div_def divAlg_def)
paulson@13183
   398
paulson@13183
   399
lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
paulson@13183
   400
by (simp add: div_def divAlg_def)
paulson@13183
   401
paulson@13183
   402
lemma zmod_zero [simp]: "(0::int) mod b = 0"
paulson@13183
   403
by (simp add: mod_def divAlg_def)
paulson@13183
   404
paulson@13183
   405
lemma zdiv_minus1: "(0::int) < b ==> -1 div b = -1"
paulson@13183
   406
by (simp add: div_def divAlg_def)
paulson@13183
   407
paulson@13183
   408
lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
paulson@13183
   409
by (simp add: mod_def divAlg_def)
paulson@13183
   410
paulson@13183
   411
(** a positive, b positive **)
paulson@13183
   412
paulson@14288
   413
lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg(a,b))"
paulson@13183
   414
by (simp add: div_def divAlg_def)
paulson@13183
   415
paulson@14288
   416
lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg(a,b))"
paulson@13183
   417
by (simp add: mod_def divAlg_def)
paulson@13183
   418
paulson@13183
   419
(** a negative, b positive **)
paulson@13183
   420
paulson@13183
   421
lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg(a,b))"
paulson@13183
   422
by (simp add: div_def divAlg_def)
paulson@13183
   423
paulson@13183
   424
lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg(a,b))"
paulson@13183
   425
by (simp add: mod_def divAlg_def)
paulson@13183
   426
paulson@13183
   427
(** a positive, b negative **)
paulson@13183
   428
paulson@13183
   429
lemma div_pos_neg:
paulson@13183
   430
     "[| 0 < a;  b < 0 |] ==> a div b = fst (negateSnd(negDivAlg(-a,-b)))"
paulson@13183
   431
by (simp add: div_def divAlg_def)
paulson@13183
   432
paulson@13183
   433
lemma mod_pos_neg:
paulson@13183
   434
     "[| 0 < a;  b < 0 |] ==> a mod b = snd (negateSnd(negDivAlg(-a,-b)))"
paulson@13183
   435
by (simp add: mod_def divAlg_def)
paulson@13183
   436
paulson@13183
   437
(** a negative, b negative **)
paulson@13183
   438
paulson@13183
   439
lemma div_neg_neg:
paulson@14288
   440
     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (negateSnd(posDivAlg(-a,-b)))"
paulson@13183
   441
by (simp add: div_def divAlg_def)
paulson@13183
   442
paulson@13183
   443
lemma mod_neg_neg:
paulson@14288
   444
     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (negateSnd(posDivAlg(-a,-b)))"
paulson@13183
   445
by (simp add: mod_def divAlg_def)
paulson@13183
   446
paulson@13183
   447
text {*Simplify expresions in which div and mod combine numerical constants*}
paulson@13183
   448
paulson@13183
   449
declare div_pos_pos [of "number_of v" "number_of w", standard, simp]
paulson@13183
   450
declare div_neg_pos [of "number_of v" "number_of w", standard, simp]
paulson@13183
   451
declare div_pos_neg [of "number_of v" "number_of w", standard, simp]
paulson@13183
   452
declare div_neg_neg [of "number_of v" "number_of w", standard, simp]
paulson@13183
   453
paulson@13183
   454
declare mod_pos_pos [of "number_of v" "number_of w", standard, simp]
paulson@13183
   455
declare mod_neg_pos [of "number_of v" "number_of w", standard, simp]
paulson@13183
   456
declare mod_pos_neg [of "number_of v" "number_of w", standard, simp]
paulson@13183
   457
declare mod_neg_neg [of "number_of v" "number_of w", standard, simp]
paulson@13183
   458
paulson@13183
   459
declare posDivAlg_eqn [of "number_of v" "number_of w", standard, simp]
paulson@13183
   460
declare negDivAlg_eqn [of "number_of v" "number_of w", standard, simp]
paulson@13183
   461
paulson@13183
   462
paulson@13183
   463
(** Special-case simplification **)
paulson@13183
   464
paulson@13183
   465
lemma zmod_1 [simp]: "a mod (1::int) = 0"
paulson@13183
   466
apply (cut_tac a = a and b = 1 in pos_mod_sign)
nipkow@13788
   467
apply (cut_tac [2] a = a and b = 1 in pos_mod_bound)
nipkow@13788
   468
apply (auto simp del:pos_mod_bound pos_mod_sign)
nipkow@13788
   469
done
paulson@13183
   470
paulson@13183
   471
lemma zdiv_1 [simp]: "a div (1::int) = a"
paulson@13183
   472
by (cut_tac a = a and b = 1 in zmod_zdiv_equality, auto)
paulson@13183
   473
paulson@13183
   474
lemma zmod_minus1_right [simp]: "a mod (-1::int) = 0"
paulson@13183
   475
apply (cut_tac a = a and b = "-1" in neg_mod_sign)
nipkow@13788
   476
apply (cut_tac [2] a = a and b = "-1" in neg_mod_bound)
nipkow@13788
   477
apply (auto simp del: neg_mod_sign neg_mod_bound)
paulson@13183
   478
done
paulson@13183
   479
paulson@13183
   480
lemma zdiv_minus1_right [simp]: "a div (-1::int) = -a"
paulson@13183
   481
by (cut_tac a = a and b = "-1" in zmod_zdiv_equality, auto)
paulson@13183
   482
paulson@13183
   483
(** The last remaining special cases for constant arithmetic:
paulson@13183
   484
    1 div z and 1 mod z **)
paulson@13183
   485
paulson@13183
   486
declare div_pos_pos [OF int_0_less_1, of "number_of w", standard, simp]
paulson@13183
   487
declare div_pos_neg [OF int_0_less_1, of "number_of w", standard, simp]
paulson@13183
   488
declare mod_pos_pos [OF int_0_less_1, of "number_of w", standard, simp]
paulson@13183
   489
declare mod_pos_neg [OF int_0_less_1, of "number_of w", standard, simp]
paulson@13183
   490
paulson@13183
   491
declare posDivAlg_eqn [of concl: 1 "number_of w", standard, simp]
paulson@13183
   492
declare negDivAlg_eqn [of concl: 1 "number_of w", standard, simp]
paulson@13183
   493
paulson@13183
   494
paulson@14271
   495
subsection{*Monotonicity in the First Argument (Dividend)*}
paulson@13183
   496
paulson@14288
   497
lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
paulson@13183
   498
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
paulson@13183
   499
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
paulson@13183
   500
apply (rule unique_quotient_lemma)
paulson@13183
   501
apply (erule subst)
paulson@13183
   502
apply (erule subst)
nipkow@13788
   503
apply (simp_all)
paulson@13183
   504
done
paulson@13183
   505
paulson@14288
   506
lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
paulson@13183
   507
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
paulson@13183
   508
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
paulson@13183
   509
apply (rule unique_quotient_lemma_neg)
paulson@13183
   510
apply (erule subst)
paulson@13183
   511
apply (erule subst)
nipkow@13788
   512
apply (simp_all)
paulson@13183
   513
done
paulson@6917
   514
paulson@6917
   515
paulson@14271
   516
subsection{*Monotonicity in the Second Argument (Divisor)*}
paulson@13183
   517
paulson@13183
   518
lemma q_pos_lemma:
paulson@14288
   519
     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
paulson@13183
   520
apply (subgoal_tac "0 < b'* (q' + 1) ")
paulson@14353
   521
 apply (simp add: zero_less_mult_iff)
paulson@14479
   522
apply (simp add: right_distrib)
paulson@13183
   523
done
paulson@13183
   524
paulson@13183
   525
lemma zdiv_mono2_lemma:
paulson@14288
   526
     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
paulson@14288
   527
         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
paulson@14288
   528
      ==> q \<le> (q'::int)"
paulson@13183
   529
apply (frule q_pos_lemma, assumption+) 
paulson@13183
   530
apply (subgoal_tac "b*q < b* (q' + 1) ")
paulson@14387
   531
 apply (simp add: mult_less_cancel_left)
paulson@13183
   532
apply (subgoal_tac "b*q = r' - r + b'*q'")
paulson@13183
   533
 prefer 2 apply simp
paulson@14479
   534
apply (simp (no_asm_simp) add: right_distrib)
paulson@13183
   535
apply (subst zadd_commute, rule zadd_zless_mono, arith)
paulson@14378
   536
apply (rule mult_right_mono, auto)
paulson@13183
   537
done
paulson@13183
   538
paulson@13183
   539
lemma zdiv_mono2:
paulson@14288
   540
     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
paulson@13183
   541
apply (subgoal_tac "b ~= 0")
paulson@13183
   542
 prefer 2 apply arith
paulson@13183
   543
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
paulson@13183
   544
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
paulson@13183
   545
apply (rule zdiv_mono2_lemma)
paulson@13183
   546
apply (erule subst)
paulson@13183
   547
apply (erule subst)
nipkow@13788
   548
apply (simp_all)
paulson@13183
   549
done
paulson@13183
   550
paulson@13183
   551
lemma q_neg_lemma:
paulson@14288
   552
     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
paulson@13183
   553
apply (subgoal_tac "b'*q' < 0")
paulson@14353
   554
 apply (simp add: mult_less_0_iff, arith)
paulson@13183
   555
done
paulson@13183
   556
paulson@13183
   557
lemma zdiv_mono2_neg_lemma:
paulson@13183
   558
     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
paulson@14288
   559
         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
paulson@14288
   560
      ==> q' \<le> (q::int)"
paulson@13183
   561
apply (frule q_neg_lemma, assumption+) 
paulson@13183
   562
apply (subgoal_tac "b*q' < b* (q + 1) ")
paulson@14387
   563
 apply (simp add: mult_less_cancel_left)
paulson@14479
   564
apply (simp add: right_distrib)
paulson@14288
   565
apply (subgoal_tac "b*q' \<le> b'*q'")
paulson@14378
   566
 prefer 2 apply (simp add: mult_right_mono_neg)
paulson@13183
   567
apply (subgoal_tac "b'*q' < b + b*q")
paulson@13183
   568
 apply arith
paulson@13183
   569
apply simp 
paulson@13183
   570
done
paulson@13183
   571
paulson@13183
   572
lemma zdiv_mono2_neg:
paulson@14288
   573
     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
paulson@13183
   574
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
paulson@13183
   575
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
paulson@13183
   576
apply (rule zdiv_mono2_neg_lemma)
paulson@13183
   577
apply (erule subst)
paulson@13183
   578
apply (erule subst)
nipkow@13788
   579
apply (simp_all)
paulson@13183
   580
done
paulson@13183
   581
paulson@13183
   582
paulson@14271
   583
subsection{*More Algebraic Laws for div and mod*}
paulson@13183
   584
paulson@13183
   585
(** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
paulson@13183
   586
paulson@13183
   587
lemma zmult1_lemma:
paulson@13183
   588
     "[| quorem((b,c),(q,r));  c ~= 0 |]  
paulson@13183
   589
      ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
paulson@14479
   590
by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)
paulson@13183
   591
paulson@13183
   592
lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
paulson@15013
   593
apply (case_tac "c = 0", simp)
paulson@13183
   594
apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_div])
paulson@13183
   595
done
paulson@13183
   596
paulson@13183
   597
lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"
paulson@15013
   598
apply (case_tac "c = 0", simp)
paulson@13183
   599
apply (blast intro: quorem_div_mod [THEN zmult1_lemma, THEN quorem_mod])
paulson@13183
   600
done
paulson@13183
   601
paulson@13183
   602
lemma zmod_zmult1_eq': "(a*b) mod (c::int) = ((a mod c) * b) mod c"
paulson@13183
   603
apply (rule trans)
paulson@13183
   604
apply (rule_tac s = "b*a mod c" in trans)
paulson@13183
   605
apply (rule_tac [2] zmod_zmult1_eq)
paulson@13183
   606
apply (simp_all add: zmult_commute)
paulson@13183
   607
done
paulson@13183
   608
paulson@13183
   609
lemma zmod_zmult_distrib: "(a*b) mod (c::int) = ((a mod c) * (b mod c)) mod c"
paulson@13183
   610
apply (rule zmod_zmult1_eq' [THEN trans])
paulson@13183
   611
apply (rule zmod_zmult1_eq)
paulson@13183
   612
done
paulson@13183
   613
paulson@13183
   614
lemma zdiv_zmult_self1 [simp]: "b ~= (0::int) ==> (a*b) div b = a"
paulson@13183
   615
by (simp add: zdiv_zmult1_eq)
paulson@13183
   616
paulson@13183
   617
lemma zdiv_zmult_self2 [simp]: "b ~= (0::int) ==> (b*a) div b = a"
paulson@13183
   618
by (subst zmult_commute, erule zdiv_zmult_self1)
paulson@13183
   619
paulson@13183
   620
lemma zmod_zmult_self1 [simp]: "(a*b) mod b = (0::int)"
paulson@13183
   621
by (simp add: zmod_zmult1_eq)
paulson@13183
   622
paulson@13183
   623
lemma zmod_zmult_self2 [simp]: "(b*a) mod b = (0::int)"
paulson@13183
   624
by (simp add: zmult_commute zmod_zmult1_eq)
paulson@13183
   625
paulson@13183
   626
lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
nipkow@13517
   627
proof
nipkow@13517
   628
  assume "m mod d = 0"
paulson@14473
   629
  with zmod_zdiv_equality[of m d] show "EX q::int. m = d*q" by auto
nipkow@13517
   630
next
nipkow@13517
   631
  assume "EX q::int. m = d*q"
nipkow@13517
   632
  thus "m mod d = 0" by auto
nipkow@13517
   633
qed
paulson@13183
   634
paulson@13183
   635
declare zmod_eq_0_iff [THEN iffD1, dest!]
paulson@13183
   636
paulson@13183
   637
(** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
paulson@13183
   638
paulson@13183
   639
lemma zadd1_lemma:
paulson@13183
   640
     "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  c ~= 0 |]  
paulson@13183
   641
      ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
paulson@14479
   642
by (force simp add: split_ifs quorem_def linorder_neq_iff right_distrib)
paulson@13183
   643
paulson@13183
   644
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@13183
   645
lemma zdiv_zadd1_eq:
paulson@13183
   646
     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
paulson@15013
   647
apply (case_tac "c = 0", simp)
paulson@13183
   648
apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_div)
paulson@13183
   649
done
paulson@13183
   650
paulson@13183
   651
lemma zmod_zadd1_eq: "(a+b) mod (c::int) = (a mod c + b mod c) mod c"
paulson@15013
   652
apply (case_tac "c = 0", simp)
paulson@13183
   653
apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod] quorem_mod)
paulson@13183
   654
done
paulson@13183
   655
paulson@13183
   656
lemma mod_div_trivial [simp]: "(a mod b) div b = (0::int)"
paulson@15013
   657
apply (case_tac "b = 0", simp)
nipkow@13788
   658
apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial)
paulson@13183
   659
done
paulson@13183
   660
paulson@13183
   661
lemma mod_mod_trivial [simp]: "(a mod b) mod b = a mod (b::int)"
paulson@15013
   662
apply (case_tac "b = 0", simp)
nipkow@13788
   663
apply (force simp add: linorder_neq_iff mod_pos_pos_trivial mod_neg_neg_trivial)
paulson@13183
   664
done
paulson@13183
   665
paulson@13183
   666
lemma zmod_zadd_left_eq: "(a+b) mod (c::int) = ((a mod c) + b) mod c"
paulson@13183
   667
apply (rule trans [symmetric])
paulson@13183
   668
apply (rule zmod_zadd1_eq, simp)
paulson@13183
   669
apply (rule zmod_zadd1_eq [symmetric])
paulson@13183
   670
done
paulson@13183
   671
paulson@13183
   672
lemma zmod_zadd_right_eq: "(a+b) mod (c::int) = (a + (b mod c)) mod c"
paulson@13183
   673
apply (rule trans [symmetric])
paulson@13183
   674
apply (rule zmod_zadd1_eq, simp)
paulson@13183
   675
apply (rule zmod_zadd1_eq [symmetric])
paulson@13183
   676
done
paulson@13183
   677
paulson@13183
   678
lemma zdiv_zadd_self1[simp]: "a ~= (0::int) ==> (a+b) div a = b div a + 1"
paulson@13183
   679
by (simp add: zdiv_zadd1_eq)
paulson@13183
   680
paulson@13183
   681
lemma zdiv_zadd_self2[simp]: "a ~= (0::int) ==> (b+a) div a = b div a + 1"
paulson@13183
   682
by (simp add: zdiv_zadd1_eq)
paulson@13183
   683
paulson@13183
   684
lemma zmod_zadd_self1[simp]: "(a+b) mod a = b mod (a::int)"
paulson@15013
   685
apply (case_tac "a = 0", simp)
paulson@13183
   686
apply (simp add: zmod_zadd1_eq)
paulson@13183
   687
done
paulson@13183
   688
paulson@13183
   689
lemma zmod_zadd_self2[simp]: "(b+a) mod a = b mod (a::int)"
paulson@15013
   690
apply (case_tac "a = 0", simp)
paulson@13183
   691
apply (simp add: zmod_zadd1_eq)
paulson@13183
   692
done
paulson@13183
   693
paulson@13183
   694
paulson@14271
   695
subsection{*Proving  @{term "a div (b*c) = (a div b) div c"} *}
paulson@13183
   696
paulson@13183
   697
(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
paulson@13183
   698
  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
paulson@13183
   699
  to cause particular problems.*)
paulson@13183
   700
paulson@13183
   701
(** first, four lemmas to bound the remainder for the cases b<0 and b>0 **)
paulson@13183
   702
paulson@14288
   703
lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
paulson@13183
   704
apply (subgoal_tac "b * (c - q mod c) < r * 1")
paulson@14479
   705
apply (simp add: right_diff_distrib)
paulson@13183
   706
apply (rule order_le_less_trans)
paulson@14378
   707
apply (erule_tac [2] mult_strict_right_mono)
paulson@14378
   708
apply (rule mult_left_mono_neg)
paulson@14271
   709
apply (auto simp add: compare_rls zadd_commute [of 1]
paulson@13183
   710
                      add1_zle_eq pos_mod_bound)
paulson@13183
   711
done
paulson@13183
   712
paulson@14288
   713
lemma zmult2_lemma_aux2: "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
paulson@14288
   714
apply (subgoal_tac "b * (q mod c) \<le> 0")
paulson@13183
   715
 apply arith
paulson@14353
   716
apply (simp add: mult_le_0_iff)
paulson@13183
   717
done
paulson@13183
   718
paulson@14288
   719
lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
paulson@14288
   720
apply (subgoal_tac "0 \<le> b * (q mod c) ")
paulson@13183
   721
apply arith
paulson@14353
   722
apply (simp add: zero_le_mult_iff)
paulson@13183
   723
done
paulson@13183
   724
paulson@14288
   725
lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
paulson@13183
   726
apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
paulson@14479
   727
apply (simp add: right_diff_distrib)
paulson@13183
   728
apply (rule order_less_le_trans)
paulson@14378
   729
apply (erule mult_strict_right_mono)
paulson@14387
   730
apply (rule_tac [2] mult_left_mono)
paulson@14271
   731
apply (auto simp add: compare_rls zadd_commute [of 1]
paulson@13183
   732
                      add1_zle_eq pos_mod_bound)
paulson@13183
   733
done
paulson@13183
   734
paulson@13183
   735
lemma zmult2_lemma: "[| quorem ((a,b), (q,r));  b ~= 0;  0 < c |]  
paulson@13183
   736
      ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
paulson@14271
   737
by (auto simp add: mult_ac quorem_def linorder_neq_iff
paulson@14479
   738
                   zero_less_mult_iff right_distrib [symmetric] 
wenzelm@13524
   739
                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4)
paulson@13183
   740
paulson@13183
   741
lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
paulson@15013
   742
apply (case_tac "b = 0", simp)
paulson@13183
   743
apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_div])
paulson@13183
   744
done
paulson@13183
   745
paulson@13183
   746
lemma zmod_zmult2_eq:
paulson@13183
   747
     "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
paulson@15013
   748
apply (case_tac "b = 0", simp)
paulson@13183
   749
apply (force simp add: quorem_div_mod [THEN zmult2_lemma, THEN quorem_mod])
paulson@13183
   750
done
paulson@13183
   751
paulson@13183
   752
paulson@14271
   753
subsection{*Cancellation of Common Factors in div*}
paulson@13183
   754
wenzelm@13524
   755
lemma zdiv_zmult_zmult1_aux1: "[| (0::int) < b;  c ~= 0 |] ==> (c*a) div (c*b) = a div b"
paulson@13183
   756
by (subst zdiv_zmult2_eq, auto)
paulson@13183
   757
wenzelm@13524
   758
lemma zdiv_zmult_zmult1_aux2: "[| b < (0::int);  c ~= 0 |] ==> (c*a) div (c*b) = a div b"
paulson@13183
   759
apply (subgoal_tac " (c * (-a)) div (c * (-b)) = (-a) div (-b) ")
wenzelm@13524
   760
apply (rule_tac [2] zdiv_zmult_zmult1_aux1, auto)
paulson@13183
   761
done
paulson@13183
   762
paulson@13183
   763
lemma zdiv_zmult_zmult1: "c ~= (0::int) ==> (c*a) div (c*b) = a div b"
paulson@15013
   764
apply (case_tac "b = 0", simp)
wenzelm@13524
   765
apply (auto simp add: linorder_neq_iff zdiv_zmult_zmult1_aux1 zdiv_zmult_zmult1_aux2)
paulson@13183
   766
done
paulson@13183
   767
paulson@13183
   768
lemma zdiv_zmult_zmult2: "c ~= (0::int) ==> (a*c) div (b*c) = a div b"
paulson@13183
   769
apply (drule zdiv_zmult_zmult1)
paulson@13183
   770
apply (auto simp add: zmult_commute)
paulson@13183
   771
done
paulson@13183
   772
paulson@13183
   773
paulson@13183
   774
paulson@14271
   775
subsection{*Distribution of Factors over mod*}
paulson@13183
   776
wenzelm@13524
   777
lemma zmod_zmult_zmult1_aux1: "[| (0::int) < b;  c ~= 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"
paulson@13183
   778
by (subst zmod_zmult2_eq, auto)
paulson@13183
   779
wenzelm@13524
   780
lemma zmod_zmult_zmult1_aux2: "[| b < (0::int);  c ~= 0 |] ==> (c*a) mod (c*b) = c * (a mod b)"
paulson@13183
   781
apply (subgoal_tac " (c * (-a)) mod (c * (-b)) = c * ((-a) mod (-b))")
wenzelm@13524
   782
apply (rule_tac [2] zmod_zmult_zmult1_aux1, auto)
paulson@13183
   783
done
paulson@13183
   784
paulson@13183
   785
lemma zmod_zmult_zmult1: "(c*a) mod (c*b) = (c::int) * (a mod b)"
paulson@15013
   786
apply (case_tac "b = 0", simp)
paulson@15013
   787
apply (case_tac "c = 0", simp)
wenzelm@13524
   788
apply (auto simp add: linorder_neq_iff zmod_zmult_zmult1_aux1 zmod_zmult_zmult1_aux2)
paulson@13183
   789
done
paulson@13183
   790
paulson@13183
   791
lemma zmod_zmult_zmult2: "(a*c) mod (b*c) = (a mod b) * (c::int)"
paulson@13183
   792
apply (cut_tac c = c in zmod_zmult_zmult1)
paulson@13183
   793
apply (auto simp add: zmult_commute)
paulson@13183
   794
done
paulson@13183
   795
paulson@13183
   796
paulson@14271
   797
subsection {*Splitting Rules for div and mod*}
paulson@13260
   798
paulson@13260
   799
text{*The proofs of the two lemmas below are essentially identical*}
paulson@13260
   800
paulson@13260
   801
lemma split_pos_lemma:
paulson@13260
   802
 "0<k ==> 
paulson@14288
   803
    P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
paulson@13260
   804
apply (rule iffI)
paulson@13260
   805
 apply clarify
paulson@13260
   806
 apply (erule_tac P="P ?x ?y" in rev_mp)  
paulson@13260
   807
 apply (subst zmod_zadd1_eq) 
paulson@13260
   808
 apply (subst zdiv_zadd1_eq) 
paulson@13260
   809
 apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
paulson@13260
   810
txt{*converse direction*}
paulson@13260
   811
apply (drule_tac x = "n div k" in spec) 
paulson@13260
   812
apply (drule_tac x = "n mod k" in spec) 
nipkow@13788
   813
apply (simp)
paulson@13260
   814
done
paulson@13260
   815
paulson@13260
   816
lemma split_neg_lemma:
paulson@13260
   817
 "k<0 ==>
paulson@14288
   818
    P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
paulson@13260
   819
apply (rule iffI)
paulson@13260
   820
 apply clarify
paulson@13260
   821
 apply (erule_tac P="P ?x ?y" in rev_mp)  
paulson@13260
   822
 apply (subst zmod_zadd1_eq) 
paulson@13260
   823
 apply (subst zdiv_zadd1_eq) 
paulson@13260
   824
 apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
paulson@13260
   825
txt{*converse direction*}
paulson@13260
   826
apply (drule_tac x = "n div k" in spec) 
paulson@13260
   827
apply (drule_tac x = "n mod k" in spec) 
nipkow@13788
   828
apply (simp)
paulson@13260
   829
done
paulson@13260
   830
paulson@13260
   831
lemma split_zdiv:
paulson@13260
   832
 "P(n div k :: int) =
paulson@13260
   833
  ((k = 0 --> P 0) & 
paulson@14288
   834
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
paulson@14288
   835
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
paulson@13260
   836
apply (case_tac "k=0")
paulson@15013
   837
 apply (simp)
paulson@13260
   838
apply (simp only: linorder_neq_iff)
paulson@13260
   839
apply (erule disjE) 
paulson@13260
   840
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
paulson@13260
   841
                      split_neg_lemma [of concl: "%x y. P x"])
paulson@13260
   842
done
paulson@13260
   843
paulson@13260
   844
lemma split_zmod:
paulson@13260
   845
 "P(n mod k :: int) =
paulson@13260
   846
  ((k = 0 --> P n) & 
paulson@14288
   847
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
paulson@14288
   848
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
paulson@13260
   849
apply (case_tac "k=0")
paulson@15013
   850
 apply (simp)
paulson@13260
   851
apply (simp only: linorder_neq_iff)
paulson@13260
   852
apply (erule disjE) 
paulson@13260
   853
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
paulson@13260
   854
                      split_neg_lemma [of concl: "%x y. P y"])
paulson@13260
   855
done
paulson@13260
   856
paulson@13260
   857
(* Enable arith to deal with div 2 and mod 2: *)
nipkow@13266
   858
declare split_zdiv [of _ _ "number_of k", simplified, standard, arith_split]
nipkow@13266
   859
declare split_zmod [of _ _ "number_of k", simplified, standard, arith_split]
paulson@13260
   860
paulson@13260
   861
paulson@14271
   862
subsection{*Speeding up the Division Algorithm with Shifting*}
paulson@13183
   863
paulson@13183
   864
(** computing div by shifting **)
paulson@13183
   865
paulson@14288
   866
lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
paulson@14288
   867
proof cases
paulson@14288
   868
  assume "a=0"
paulson@14288
   869
    thus ?thesis by simp
paulson@14288
   870
next
paulson@14288
   871
  assume "a\<noteq>0" and le_a: "0\<le>a"   
paulson@14288
   872
  hence a_pos: "1 \<le> a" by arith
paulson@14288
   873
  hence one_less_a2: "1 < 2*a" by arith
paulson@14288
   874
  hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
paulson@14288
   875
    by (simp add: mult_le_cancel_left zadd_commute [of 1] add1_zle_eq)
paulson@14288
   876
  with a_pos have "0 \<le> b mod a" by simp
paulson@14288
   877
  hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
paulson@14288
   878
    by (simp add: mod_pos_pos_trivial one_less_a2)
paulson@14288
   879
  with  le_2a
paulson@14288
   880
  have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
paulson@14288
   881
    by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2
paulson@14288
   882
                  right_distrib) 
paulson@14288
   883
  thus ?thesis
paulson@14288
   884
    by (subst zdiv_zadd1_eq,
paulson@14288
   885
        simp add: zdiv_zmult_zmult1 zmod_zmult_zmult1 one_less_a2
paulson@14288
   886
                  div_pos_pos_trivial)
paulson@14288
   887
qed
paulson@13183
   888
paulson@14288
   889
lemma neg_zdiv_mult_2: "a \<le> (0::int) ==> (1 + 2*b) div (2*a) = (b+1) div a"
paulson@13183
   890
apply (subgoal_tac " (1 + 2* (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a) ")
paulson@13183
   891
apply (rule_tac [2] pos_zdiv_mult_2)
paulson@14479
   892
apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)
paulson@13183
   893
apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
paulson@14479
   894
apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric],
paulson@13183
   895
       simp) 
paulson@13183
   896
done
paulson@13183
   897
paulson@13183
   898
paulson@13183
   899
(*Not clear why this must be proved separately; probably number_of causes
paulson@13183
   900
  simplification problems*)
paulson@14288
   901
lemma not_0_le_lemma: "~ 0 \<le> x ==> x \<le> (0::int)"
paulson@13183
   902
by auto
paulson@13183
   903
paulson@13183
   904
lemma zdiv_number_of_BIT[simp]:
paulson@13183
   905
     "number_of (v BIT b) div number_of (w BIT False) =  
paulson@14288
   906
          (if ~b | (0::int) \<le> number_of w                    
paulson@13183
   907
           then number_of v div (number_of w)     
paulson@13183
   908
           else (number_of v + (1::int)) div (number_of w))"
paulson@15013
   909
apply (simp only: number_of_eq Bin_simps UNIV_I split: split_if) 
paulson@15013
   910
apply (simp add: zdiv_zmult_zmult1 pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac)
paulson@13183
   911
done
paulson@13183
   912
paulson@13183
   913
paulson@15013
   914
subsection{*Computing mod by Shifting (proofs resemble those for div)*}
paulson@13183
   915
paulson@13183
   916
lemma pos_zmod_mult_2:
paulson@14288
   917
     "(0::int) \<le> a ==> (1 + 2*b) mod (2*a) = 1 + 2 * (b mod a)"
paulson@15013
   918
apply (case_tac "a = 0", simp)
paulson@14288
   919
apply (subgoal_tac "1 \<le> a")
paulson@13183
   920
 prefer 2 apply arith
paulson@13183
   921
apply (subgoal_tac "1 < a * 2")
paulson@13183
   922
 prefer 2 apply arith
paulson@14288
   923
apply (subgoal_tac "2* (1 + b mod a) \<le> 2*a")
paulson@14387
   924
 apply (rule_tac [2] mult_left_mono)
paulson@13183
   925
apply (auto simp add: zadd_commute [of 1] zmult_commute add1_zle_eq 
paulson@13183
   926
                      pos_mod_bound)
paulson@13183
   927
apply (subst zmod_zadd1_eq)
paulson@13183
   928
apply (simp add: zmod_zmult_zmult2 mod_pos_pos_trivial)
paulson@13183
   929
apply (rule mod_pos_pos_trivial)
paulson@14288
   930
apply (auto simp add: mod_pos_pos_trivial left_distrib)
paulson@14288
   931
apply (subgoal_tac "0 \<le> b mod a", arith)
nipkow@13788
   932
apply (simp)
paulson@13183
   933
done
paulson@13183
   934
paulson@13183
   935
lemma neg_zmod_mult_2:
paulson@14288
   936
     "a \<le> (0::int) ==> (1 + 2*b) mod (2*a) = 2 * ((b+1) mod a) - 1"
paulson@13183
   937
apply (subgoal_tac "(1 + 2* (-b - 1)) mod (2* (-a)) = 
paulson@13183
   938
                    1 + 2* ((-b - 1) mod (-a))")
paulson@13183
   939
apply (rule_tac [2] pos_zmod_mult_2)
paulson@14479
   940
apply (auto simp add: minus_mult_right [symmetric] right_diff_distrib)
paulson@13183
   941
apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))")
paulson@13183
   942
 prefer 2 apply simp 
paulson@14479
   943
apply (simp only: zmod_zminus_zminus diff_minus minus_add_distrib [symmetric])
paulson@13183
   944
done
paulson@13183
   945
paulson@13183
   946
lemma zmod_number_of_BIT [simp]:
paulson@13183
   947
     "number_of (v BIT b) mod number_of (w BIT False) =  
paulson@13183
   948
          (if b then  
paulson@14288
   949
                if (0::int) \<le> number_of w  
paulson@13183
   950
                then 2 * (number_of v mod number_of w) + 1     
paulson@13183
   951
                else 2 * ((number_of v + (1::int)) mod number_of w) - 1   
paulson@13183
   952
           else 2 * (number_of v mod number_of w))"
paulson@15013
   953
apply (simp only: number_of_eq Bin_simps UNIV_I split: split_if) 
paulson@15013
   954
apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 
paulson@15013
   955
                 not_0_le_lemma neg_zmod_mult_2 add_ac)
paulson@13183
   956
done
paulson@13183
   957
paulson@13183
   958
paulson@13183
   959
paulson@15013
   960
subsection{*Quotients of Signs*}
paulson@13183
   961
paulson@13183
   962
lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
paulson@14288
   963
apply (subgoal_tac "a div b \<le> -1", force)
paulson@13183
   964
apply (rule order_trans)
paulson@13183
   965
apply (rule_tac a' = "-1" in zdiv_mono1)
paulson@13183
   966
apply (auto simp add: zdiv_minus1)
paulson@13183
   967
done
paulson@13183
   968
paulson@14288
   969
lemma div_nonneg_neg_le0: "[| (0::int) \<le> a;  b < 0 |] ==> a div b \<le> 0"
paulson@13183
   970
by (drule zdiv_mono1_neg, auto)
paulson@13183
   971
paulson@14288
   972
lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
paulson@13183
   973
apply auto
paulson@13183
   974
apply (drule_tac [2] zdiv_mono1)
paulson@13183
   975
apply (auto simp add: linorder_neq_iff)
paulson@13183
   976
apply (simp (no_asm_use) add: linorder_not_less [symmetric])
paulson@13183
   977
apply (blast intro: div_neg_pos_less0)
paulson@13183
   978
done
paulson@13183
   979
paulson@13183
   980
lemma neg_imp_zdiv_nonneg_iff:
paulson@14288
   981
     "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
paulson@13183
   982
apply (subst zdiv_zminus_zminus [symmetric])
paulson@13183
   983
apply (subst pos_imp_zdiv_nonneg_iff, auto)
paulson@13183
   984
done
paulson@13183
   985
paulson@14288
   986
(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
paulson@13183
   987
lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
paulson@13183
   988
by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
paulson@13183
   989
paulson@14288
   990
(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
paulson@13183
   991
lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
paulson@13183
   992
by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
paulson@13183
   993
paulson@13837
   994
paulson@14271
   995
subsection {* The Divides Relation *}
paulson@13837
   996
paulson@13837
   997
lemma zdvd_iff_zmod_eq_0: "(m dvd n) = (n mod m = (0::int))"
paulson@13837
   998
by(simp add:dvd_def zmod_eq_0_iff)
paulson@13837
   999
paulson@13837
  1000
lemma zdvd_0_right [iff]: "(m::int) dvd 0"
paulson@13837
  1001
  apply (unfold dvd_def)
paulson@14479
  1002
  apply (blast intro: mult_zero_right [symmetric])
paulson@13837
  1003
  done
paulson@13837
  1004
paulson@13837
  1005
lemma zdvd_0_left [iff]: "(0 dvd (m::int)) = (m = 0)"
paulson@13837
  1006
  by (unfold dvd_def, auto)
paulson@13837
  1007
paulson@13837
  1008
lemma zdvd_1_left [iff]: "1 dvd (m::int)"
paulson@13837
  1009
  by (unfold dvd_def, simp)
paulson@13837
  1010
paulson@13837
  1011
lemma zdvd_refl [simp]: "m dvd (m::int)"
paulson@13837
  1012
  apply (unfold dvd_def)
paulson@13837
  1013
  apply (blast intro: zmult_1_right [symmetric])
paulson@13837
  1014
  done
paulson@13837
  1015
paulson@13837
  1016
lemma zdvd_trans: "m dvd n ==> n dvd k ==> m dvd (k::int)"
paulson@13837
  1017
  apply (unfold dvd_def)
paulson@13837
  1018
  apply (blast intro: zmult_assoc)
paulson@13837
  1019
  done
paulson@13837
  1020
paulson@13837
  1021
lemma zdvd_zminus_iff: "(m dvd -n) = (m dvd (n::int))"
paulson@13837
  1022
  apply (unfold dvd_def, auto)
paulson@13837
  1023
   apply (rule_tac [!] x = "-k" in exI, auto)
paulson@13837
  1024
  done
paulson@13837
  1025
paulson@13837
  1026
lemma zdvd_zminus2_iff: "(-m dvd n) = (m dvd (n::int))"
paulson@13837
  1027
  apply (unfold dvd_def, auto)
paulson@13837
  1028
   apply (rule_tac [!] x = "-k" in exI, auto)
paulson@13837
  1029
  done
paulson@13837
  1030
paulson@13837
  1031
lemma zdvd_anti_sym:
paulson@13837
  1032
    "0 < m ==> 0 < n ==> m dvd n ==> n dvd m ==> m = (n::int)"
paulson@13837
  1033
  apply (unfold dvd_def, auto)
paulson@14353
  1034
  apply (simp add: zmult_assoc zmult_eq_self_iff zero_less_mult_iff zmult_eq_1_iff)
paulson@13837
  1035
  done
paulson@13837
  1036
paulson@13837
  1037
lemma zdvd_zadd: "k dvd m ==> k dvd n ==> k dvd (m + n :: int)"
paulson@13837
  1038
  apply (unfold dvd_def)
paulson@14479
  1039
  apply (blast intro: right_distrib [symmetric])
paulson@13837
  1040
  done
paulson@13837
  1041
paulson@13837
  1042
lemma zdvd_zdiff: "k dvd m ==> k dvd n ==> k dvd (m - n :: int)"
paulson@13837
  1043
  apply (unfold dvd_def)
paulson@14479
  1044
  apply (blast intro: right_diff_distrib [symmetric])
paulson@13837
  1045
  done
paulson@13837
  1046
paulson@13837
  1047
lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"
paulson@13837
  1048
  apply (subgoal_tac "m = n + (m - n)")
paulson@13837
  1049
   apply (erule ssubst)
paulson@13837
  1050
   apply (blast intro: zdvd_zadd, simp)
paulson@13837
  1051
  done
paulson@13837
  1052
paulson@13837
  1053
lemma zdvd_zmult: "k dvd (n::int) ==> k dvd m * n"
paulson@13837
  1054
  apply (unfold dvd_def)
paulson@14271
  1055
  apply (blast intro: mult_left_commute)
paulson@13837
  1056
  done
paulson@13837
  1057
paulson@13837
  1058
lemma zdvd_zmult2: "k dvd (m::int) ==> k dvd m * n"
paulson@13837
  1059
  apply (subst zmult_commute)
paulson@13837
  1060
  apply (erule zdvd_zmult)
paulson@13837
  1061
  done
paulson@13837
  1062
paulson@13837
  1063
lemma [iff]: "(k::int) dvd m * k"
paulson@13837
  1064
  apply (rule zdvd_zmult)
paulson@13837
  1065
  apply (rule zdvd_refl)
paulson@13837
  1066
  done
paulson@13837
  1067
paulson@13837
  1068
lemma [iff]: "(k::int) dvd k * m"
paulson@13837
  1069
  apply (rule zdvd_zmult2)
paulson@13837
  1070
  apply (rule zdvd_refl)
paulson@13837
  1071
  done
paulson@13837
  1072
paulson@13837
  1073
lemma zdvd_zmultD2: "j * k dvd n ==> j dvd (n::int)"
paulson@13837
  1074
  apply (unfold dvd_def)
paulson@13837
  1075
  apply (simp add: zmult_assoc, blast)
paulson@13837
  1076
  done
paulson@13837
  1077
paulson@13837
  1078
lemma zdvd_zmultD: "j * k dvd n ==> k dvd (n::int)"
paulson@13837
  1079
  apply (rule zdvd_zmultD2)
paulson@13837
  1080
  apply (subst zmult_commute, assumption)
paulson@13837
  1081
  done
paulson@13837
  1082
paulson@13837
  1083
lemma zdvd_zmult_mono: "i dvd m ==> j dvd (n::int) ==> i * j dvd m * n"
paulson@13837
  1084
  apply (unfold dvd_def, clarify)
paulson@13837
  1085
  apply (rule_tac x = "k * ka" in exI)
paulson@14271
  1086
  apply (simp add: mult_ac)
paulson@13837
  1087
  done
paulson@13837
  1088
paulson@13837
  1089
lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"
paulson@13837
  1090
  apply (rule iffI)
paulson@13837
  1091
   apply (erule_tac [2] zdvd_zadd)
paulson@13837
  1092
   apply (subgoal_tac "n = (n + k * m) - k * m")
paulson@13837
  1093
    apply (erule ssubst)
paulson@13837
  1094
    apply (erule zdvd_zdiff, simp_all)
paulson@13837
  1095
  done
paulson@13837
  1096
paulson@13837
  1097
lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
paulson@13837
  1098
  apply (unfold dvd_def)
paulson@13837
  1099
  apply (auto simp add: zmod_zmult_zmult1)
paulson@13837
  1100
  done
paulson@13837
  1101
paulson@13837
  1102
lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
paulson@13837
  1103
  apply (subgoal_tac "k dvd n * (m div n) + m mod n")
paulson@13837
  1104
   apply (simp add: zmod_zdiv_equality [symmetric])
paulson@13837
  1105
  apply (simp only: zdvd_zadd zdvd_zmult2)
paulson@13837
  1106
  done
paulson@13837
  1107
paulson@13837
  1108
lemma zdvd_not_zless: "0 < m ==> m < n ==> \<not> n dvd (m::int)"
paulson@13837
  1109
  apply (unfold dvd_def, auto)
paulson@13837
  1110
  apply (subgoal_tac "0 < n")
paulson@13837
  1111
   prefer 2
paulson@14378
  1112
   apply (blast intro: order_less_trans)
paulson@14353
  1113
  apply (simp add: zero_less_mult_iff)
paulson@13837
  1114
  apply (subgoal_tac "n * k < n * 1")
paulson@14387
  1115
   apply (drule mult_less_cancel_left [THEN iffD1], auto)
paulson@13837
  1116
  done
paulson@13837
  1117
paulson@13837
  1118
lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"
paulson@13837
  1119
  apply (auto simp add: dvd_def nat_abs_mult_distrib)
paulson@14353
  1120
  apply (auto simp add: nat_eq_iff abs_if split add: split_if_asm)
paulson@14353
  1121
   apply (rule_tac x = "-(int k)" in exI)
paulson@13837
  1122
  apply (auto simp add: zmult_int [symmetric])
paulson@13837
  1123
  done
paulson@13837
  1124
paulson@13837
  1125
lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"
paulson@15003
  1126
  apply (auto simp add: dvd_def abs_if zmult_int [symmetric])
paulson@13837
  1127
    apply (rule_tac [3] x = "nat k" in exI)
paulson@13837
  1128
    apply (rule_tac [2] x = "-(int k)" in exI)
paulson@13837
  1129
    apply (rule_tac x = "nat (-k)" in exI)
paulson@13837
  1130
    apply (cut_tac [3] k = m in int_less_0_conv)
paulson@13837
  1131
    apply (cut_tac k = m in int_less_0_conv)
paulson@14353
  1132
    apply (auto simp add: zero_le_mult_iff mult_less_0_iff
paulson@13837
  1133
      nat_mult_distrib [symmetric] nat_eq_iff2)
paulson@13837
  1134
  done
paulson@13837
  1135
paulson@13837
  1136
lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \<le> z then (z dvd int m) else m = 0)"
paulson@13837
  1137
  apply (auto simp add: dvd_def zmult_int [symmetric])
paulson@13837
  1138
  apply (rule_tac x = "nat k" in exI)
paulson@13837
  1139
  apply (cut_tac k = m in int_less_0_conv)
paulson@14353
  1140
  apply (auto simp add: zero_le_mult_iff mult_less_0_iff
paulson@13837
  1141
    nat_mult_distrib [symmetric] nat_eq_iff2)
paulson@13837
  1142
  done
paulson@13837
  1143
paulson@13837
  1144
lemma zminus_dvd_iff [iff]: "(-z dvd w) = (z dvd (w::int))"
paulson@13837
  1145
  apply (auto simp add: dvd_def)
paulson@13837
  1146
   apply (rule_tac [!] x = "-k" in exI, auto)
paulson@13837
  1147
  done
paulson@13837
  1148
paulson@13837
  1149
lemma dvd_zminus_iff [iff]: "(z dvd -w) = (z dvd (w::int))"
paulson@13837
  1150
  apply (auto simp add: dvd_def)
paulson@14378
  1151
   apply (drule minus_equation_iff [THEN iffD1])
paulson@13837
  1152
   apply (rule_tac [!] x = "-k" in exI, auto)
paulson@13837
  1153
  done
paulson@13837
  1154
paulson@14288
  1155
lemma zdvd_imp_le: "[| z dvd n; 0 < n |] ==> z \<le> (n::int)"
paulson@13837
  1156
  apply (rule_tac z=n in int_cases)
paulson@13837
  1157
  apply (auto simp add: dvd_int_iff) 
paulson@13837
  1158
  apply (rule_tac z=z in int_cases) 
paulson@13837
  1159
  apply (auto simp add: dvd_imp_le) 
paulson@13837
  1160
  done
paulson@13837
  1161
paulson@13837
  1162
paulson@14353
  1163
subsection{*Integer Powers*} 
paulson@14353
  1164
paulson@14353
  1165
instance int :: power ..
paulson@14353
  1166
paulson@14353
  1167
primrec
paulson@14353
  1168
  "p ^ 0 = 1"
paulson@14353
  1169
  "p ^ (Suc n) = (p::int) * (p ^ n)"
paulson@14353
  1170
paulson@14353
  1171
paulson@15003
  1172
instance int :: recpower
paulson@14353
  1173
proof
paulson@14353
  1174
  fix z :: int
paulson@14353
  1175
  fix n :: nat
paulson@14353
  1176
  show "z^0 = 1" by simp
paulson@14353
  1177
  show "z^(Suc n) = z * (z^n)" by simp
paulson@14353
  1178
qed
paulson@14353
  1179
paulson@14353
  1180
paulson@14353
  1181
lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"
paulson@14353
  1182
apply (induct_tac "y", auto)
paulson@14353
  1183
apply (rule zmod_zmult1_eq [THEN trans])
paulson@14353
  1184
apply (simp (no_asm_simp))
paulson@14353
  1185
apply (rule zmod_zmult_distrib [symmetric])
paulson@14353
  1186
done
paulson@14353
  1187
paulson@14353
  1188
lemma zpower_zadd_distrib: "x^(y+z) = ((x^y)*(x^z)::int)"
paulson@14353
  1189
  by (rule Power.power_add)
paulson@14353
  1190
paulson@14353
  1191
lemma zpower_zpower: "(x^y)^z = (x^(y*z)::int)"
paulson@14353
  1192
  by (rule Power.power_mult [symmetric])
paulson@14353
  1193
paulson@14353
  1194
lemma zero_less_zpower_abs_iff [simp]:
paulson@14353
  1195
     "(0 < (abs x)^n) = (x \<noteq> (0::int) | n=0)"
paulson@14353
  1196
apply (induct_tac "n")
paulson@14353
  1197
apply (auto simp add: zero_less_mult_iff)
paulson@14353
  1198
done
paulson@14353
  1199
paulson@14353
  1200
lemma zero_le_zpower_abs [simp]: "(0::int) <= (abs x)^n"
paulson@14353
  1201
apply (induct_tac "n")
paulson@14353
  1202
apply (auto simp add: zero_le_mult_iff)
paulson@14353
  1203
done
paulson@14353
  1204
obua@15101
  1205
lemma zdiv_int: "int (a div b) = (int a) div (int b)"
obua@15101
  1206
apply (subst split_div, auto)
obua@15101
  1207
apply (subst split_zdiv, auto)
obua@15101
  1208
apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
obua@15101
  1209
apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
obua@15101
  1210
done
obua@15101
  1211
obua@15101
  1212
lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
obua@15101
  1213
apply (subst split_mod, auto)
obua@15101
  1214
apply (subst split_zmod, auto)
obua@15101
  1215
apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia in IntDiv.unique_remainder)
obua@15101
  1216
apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
obua@15101
  1217
done
paulson@14353
  1218
paulson@13183
  1219
ML
paulson@13183
  1220
{*
paulson@13183
  1221
val quorem_def = thm "quorem_def";
paulson@13183
  1222
paulson@13183
  1223
val unique_quotient = thm "unique_quotient";
paulson@13183
  1224
val unique_remainder = thm "unique_remainder";
paulson@13183
  1225
val adjust_eq = thm "adjust_eq";
paulson@13183
  1226
val posDivAlg_eqn = thm "posDivAlg_eqn";
paulson@13183
  1227
val posDivAlg_correct = thm "posDivAlg_correct";
paulson@13183
  1228
val negDivAlg_eqn = thm "negDivAlg_eqn";
paulson@13183
  1229
val negDivAlg_correct = thm "negDivAlg_correct";
paulson@13183
  1230
val quorem_0 = thm "quorem_0";
paulson@13183
  1231
val posDivAlg_0 = thm "posDivAlg_0";
paulson@13183
  1232
val negDivAlg_minus1 = thm "negDivAlg_minus1";
paulson@13183
  1233
val negateSnd_eq = thm "negateSnd_eq";
paulson@13183
  1234
val quorem_neg = thm "quorem_neg";
paulson@13183
  1235
val divAlg_correct = thm "divAlg_correct";
paulson@13183
  1236
val DIVISION_BY_ZERO = thm "DIVISION_BY_ZERO";
paulson@13183
  1237
val zmod_zdiv_equality = thm "zmod_zdiv_equality";
paulson@13183
  1238
val pos_mod_conj = thm "pos_mod_conj";
paulson@13183
  1239
val pos_mod_sign = thm "pos_mod_sign";
paulson@13183
  1240
val neg_mod_conj = thm "neg_mod_conj";
paulson@13183
  1241
val neg_mod_sign = thm "neg_mod_sign";
paulson@13183
  1242
val quorem_div_mod = thm "quorem_div_mod";
paulson@13183
  1243
val quorem_div = thm "quorem_div";
paulson@13183
  1244
val quorem_mod = thm "quorem_mod";
paulson@13183
  1245
val div_pos_pos_trivial = thm "div_pos_pos_trivial";
paulson@13183
  1246
val div_neg_neg_trivial = thm "div_neg_neg_trivial";
paulson@13183
  1247
val div_pos_neg_trivial = thm "div_pos_neg_trivial";
paulson@13183
  1248
val mod_pos_pos_trivial = thm "mod_pos_pos_trivial";
paulson@13183
  1249
val mod_neg_neg_trivial = thm "mod_neg_neg_trivial";
paulson@13183
  1250
val mod_pos_neg_trivial = thm "mod_pos_neg_trivial";
paulson@13183
  1251
val zdiv_zminus_zminus = thm "zdiv_zminus_zminus";
paulson@13183
  1252
val zmod_zminus_zminus = thm "zmod_zminus_zminus";
paulson@13183
  1253
val zdiv_zminus1_eq_if = thm "zdiv_zminus1_eq_if";
paulson@13183
  1254
val zmod_zminus1_eq_if = thm "zmod_zminus1_eq_if";
paulson@13183
  1255
val zdiv_zminus2 = thm "zdiv_zminus2";
paulson@13183
  1256
val zmod_zminus2 = thm "zmod_zminus2";
paulson@13183
  1257
val zdiv_zminus2_eq_if = thm "zdiv_zminus2_eq_if";
paulson@13183
  1258
val zmod_zminus2_eq_if = thm "zmod_zminus2_eq_if";
paulson@13183
  1259
val self_quotient = thm "self_quotient";
paulson@13183
  1260
val self_remainder = thm "self_remainder";
paulson@13183
  1261
val zdiv_self = thm "zdiv_self";
paulson@13183
  1262
val zmod_self = thm "zmod_self";
paulson@13183
  1263
val zdiv_zero = thm "zdiv_zero";
paulson@13183
  1264
val div_eq_minus1 = thm "div_eq_minus1";
paulson@13183
  1265
val zmod_zero = thm "zmod_zero";
paulson@13183
  1266
val zdiv_minus1 = thm "zdiv_minus1";
paulson@13183
  1267
val zmod_minus1 = thm "zmod_minus1";
paulson@13183
  1268
val div_pos_pos = thm "div_pos_pos";
paulson@13183
  1269
val mod_pos_pos = thm "mod_pos_pos";
paulson@13183
  1270
val div_neg_pos = thm "div_neg_pos";
paulson@13183
  1271
val mod_neg_pos = thm "mod_neg_pos";
paulson@13183
  1272
val div_pos_neg = thm "div_pos_neg";
paulson@13183
  1273
val mod_pos_neg = thm "mod_pos_neg";
paulson@13183
  1274
val div_neg_neg = thm "div_neg_neg";
paulson@13183
  1275
val mod_neg_neg = thm "mod_neg_neg";
paulson@13183
  1276
val zmod_1 = thm "zmod_1";
paulson@13183
  1277
val zdiv_1 = thm "zdiv_1";
paulson@13183
  1278
val zmod_minus1_right = thm "zmod_minus1_right";
paulson@13183
  1279
val zdiv_minus1_right = thm "zdiv_minus1_right";
paulson@13183
  1280
val zdiv_mono1 = thm "zdiv_mono1";
paulson@13183
  1281
val zdiv_mono1_neg = thm "zdiv_mono1_neg";
paulson@13183
  1282
val zdiv_mono2 = thm "zdiv_mono2";
paulson@13183
  1283
val zdiv_mono2_neg = thm "zdiv_mono2_neg";
paulson@13183
  1284
val zdiv_zmult1_eq = thm "zdiv_zmult1_eq";
paulson@13183
  1285
val zmod_zmult1_eq = thm "zmod_zmult1_eq";
paulson@13183
  1286
val zmod_zmult1_eq' = thm "zmod_zmult1_eq'";
paulson@13183
  1287
val zmod_zmult_distrib = thm "zmod_zmult_distrib";
paulson@13183
  1288
val zdiv_zmult_self1 = thm "zdiv_zmult_self1";
paulson@13183
  1289
val zdiv_zmult_self2 = thm "zdiv_zmult_self2";
paulson@13183
  1290
val zmod_zmult_self1 = thm "zmod_zmult_self1";
paulson@13183
  1291
val zmod_zmult_self2 = thm "zmod_zmult_self2";
paulson@13183
  1292
val zmod_eq_0_iff = thm "zmod_eq_0_iff";
paulson@13183
  1293
val zdiv_zadd1_eq = thm "zdiv_zadd1_eq";
paulson@13183
  1294
val zmod_zadd1_eq = thm "zmod_zadd1_eq";
paulson@13183
  1295
val mod_div_trivial = thm "mod_div_trivial";
paulson@13183
  1296
val mod_mod_trivial = thm "mod_mod_trivial";
paulson@13183
  1297
val zmod_zadd_left_eq = thm "zmod_zadd_left_eq";
paulson@13183
  1298
val zmod_zadd_right_eq = thm "zmod_zadd_right_eq";
paulson@13183
  1299
val zdiv_zadd_self1 = thm "zdiv_zadd_self1";
paulson@13183
  1300
val zdiv_zadd_self2 = thm "zdiv_zadd_self2";
paulson@13183
  1301
val zmod_zadd_self1 = thm "zmod_zadd_self1";
paulson@13183
  1302
val zmod_zadd_self2 = thm "zmod_zadd_self2";
paulson@13183
  1303
val zdiv_zmult2_eq = thm "zdiv_zmult2_eq";
paulson@13183
  1304
val zmod_zmult2_eq = thm "zmod_zmult2_eq";
paulson@13183
  1305
val zdiv_zmult_zmult1 = thm "zdiv_zmult_zmult1";
paulson@13183
  1306
val zdiv_zmult_zmult2 = thm "zdiv_zmult_zmult2";
paulson@13183
  1307
val zmod_zmult_zmult1 = thm "zmod_zmult_zmult1";
paulson@13183
  1308
val zmod_zmult_zmult2 = thm "zmod_zmult_zmult2";
paulson@13183
  1309
val pos_zdiv_mult_2 = thm "pos_zdiv_mult_2";
paulson@13183
  1310
val neg_zdiv_mult_2 = thm "neg_zdiv_mult_2";
paulson@13183
  1311
val zdiv_number_of_BIT = thm "zdiv_number_of_BIT";
paulson@13183
  1312
val pos_zmod_mult_2 = thm "pos_zmod_mult_2";
paulson@13183
  1313
val neg_zmod_mult_2 = thm "neg_zmod_mult_2";
paulson@13183
  1314
val zmod_number_of_BIT = thm "zmod_number_of_BIT";
paulson@13183
  1315
val div_neg_pos_less0 = thm "div_neg_pos_less0";
paulson@13183
  1316
val div_nonneg_neg_le0 = thm "div_nonneg_neg_le0";
paulson@13183
  1317
val pos_imp_zdiv_nonneg_iff = thm "pos_imp_zdiv_nonneg_iff";
paulson@13183
  1318
val neg_imp_zdiv_nonneg_iff = thm "neg_imp_zdiv_nonneg_iff";
paulson@13183
  1319
val pos_imp_zdiv_neg_iff = thm "pos_imp_zdiv_neg_iff";
paulson@13183
  1320
val neg_imp_zdiv_neg_iff = thm "neg_imp_zdiv_neg_iff";
paulson@14353
  1321
paulson@14353
  1322
val zpower_zmod = thm "zpower_zmod";
paulson@14353
  1323
val zpower_zadd_distrib = thm "zpower_zadd_distrib";
paulson@14353
  1324
val zpower_zpower = thm "zpower_zpower";
paulson@14353
  1325
val zero_less_zpower_abs_iff = thm "zero_less_zpower_abs_iff";
paulson@14353
  1326
val zero_le_zpower_abs = thm "zero_le_zpower_abs";
paulson@13183
  1327
*}
paulson@13183
  1328
paulson@6917
  1329
end