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