author | paulson |
Mon, 12 Jan 2004 16:51:45 +0100 | |
changeset 14353 | 79f9fbef9106 |
parent 14295 | 7f115e5c5de4 |
child 14365 | 3d4df8c166ae |
permissions | -rw-r--r-- |
14259 | 1 |
(* Title: HOL/Integ/IntArith.thy |
2 |
ID: $Id$ |
|
3 |
Authors: Larry Paulson and Tobias Nipkow |
|
4 |
*) |
|
12023 | 5 |
|
6 |
header {* Integer arithmetic *} |
|
7 |
||
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
9214
diff
changeset
|
8 |
theory IntArith = Bin |
14259 | 9 |
files ("int_arith1.ML"): |
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
9214
diff
changeset
|
10 |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
11 |
|
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
12 |
subsection{*Inequality Reasoning for the Arithmetic Simproc*} |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
13 |
|
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
14 |
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
15 |
proof (auto simp add: zle_def zless_iff_Suc_zadd) |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
16 |
fix m n |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
17 |
assume "w + 1 = w + (1 + int m) + (1 + int n)" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
18 |
hence "(w + 1) + (1 + int (m + n)) = (w + 1) + 0" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
19 |
by (simp add: add_ac zadd_int [symmetric]) |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
20 |
hence "int (Suc(m+n)) = 0" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
21 |
by (simp only: Ring_and_Field.add_left_cancel int_Suc) |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
22 |
thus False by (simp only: int_eq_0_conv) |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
23 |
qed |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
24 |
|
12023 | 25 |
use "int_arith1.ML" |
26 |
setup int_arith_setup |
|
14259 | 27 |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
28 |
|
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
29 |
subsection{*More inequality reasoning*} |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
30 |
|
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
31 |
lemma zless_add1_eq: "(w < z + (1::int)) = (w<z | w=z)" |
14259 | 32 |
by arith |
33 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
34 |
lemma add1_zle_eq: "(w + (1::int) \<le> z) = (w<z)" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
35 |
by arith |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
36 |
|
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
37 |
lemma zle_diff1_eq [simp]: "(w \<le> z - (1::int)) = (w<(z::int))" |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
38 |
by arith |
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
39 |
|
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
40 |
lemma zle_add1_eq_le [simp]: "(w < z + 1) = (w\<le>(z::int))" |
14259 | 41 |
by arith |
42 |
||
43 |
lemma zadd_left_cancel0 [simp]: "(z = z + w) = (w = (0::int))" |
|
44 |
by arith |
|
45 |
||
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
46 |
|
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
47 |
subsection{*The Functions @{term nat} and @{term int}*} |
14259 | 48 |
|
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
49 |
lemma nonneg_eq_int: "[| 0 \<le> z; !!m. z = int m ==> P |] ==> P" |
14259 | 50 |
by (blast dest: nat_0_le sym) |
51 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
52 |
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)" |
14259 | 53 |
by auto |
54 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
55 |
lemma nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)" |
14259 | 56 |
by auto |
57 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
58 |
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)" |
14259 | 59 |
apply (rule iffI) |
60 |
apply (erule nat_0_le [THEN subst]) |
|
61 |
apply (simp_all del: zless_int add: zless_int [symmetric]) |
|
62 |
done |
|
63 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
64 |
lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)" |
14259 | 65 |
by (auto simp add: nat_eq_iff2) |
66 |
||
67 |
||
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
68 |
text{*Simplify the terms @{term "int 0"}, @{term "int(Suc 0)"} and |
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
69 |
@{term "w + - z"}*} |
14259 | 70 |
declare Zero_int_def [symmetric, simp] |
71 |
declare One_int_def [symmetric, simp] |
|
72 |
||
73 |
text{*cooper.ML refers to this theorem*} |
|
74 |
lemmas zdiff_def_symmetric = zdiff_def [symmetric, simp] |
|
75 |
||
76 |
lemma nat_0: "nat 0 = 0" |
|
77 |
by (simp add: nat_eq_iff) |
|
78 |
||
79 |
lemma nat_1: "nat 1 = Suc 0" |
|
80 |
by (subst nat_eq_iff, simp) |
|
81 |
||
82 |
lemma nat_2: "nat 2 = Suc (Suc 0)" |
|
83 |
by (subst nat_eq_iff, simp) |
|
84 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
85 |
lemma nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)" |
14259 | 86 |
apply (case_tac "neg z") |
87 |
apply (auto simp add: nat_less_iff) |
|
88 |
apply (auto intro: zless_trans simp add: neg_eq_less_0 zle_def) |
|
89 |
done |
|
90 |
||
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
91 |
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)" |
14259 | 92 |
by (auto simp add: linorder_not_less [symmetric] zless_nat_conj) |
93 |
||
94 |
||
95 |
text{*This simplifies expressions of the form @{term "int n = z"} where |
|
96 |
z is an integer literal.*} |
|
97 |
declare int_eq_iff [of _ "number_of v", standard, simp] |
|
13837
8dd150d36c65
Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents:
13685
diff
changeset
|
98 |
|
13849 | 99 |
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)" |
100 |
by simp |
|
101 |
||
14295 | 102 |
lemma split_nat [arith_split]: |
14259 | 103 |
"P(nat(i::int)) = ((\<forall>n. i = int n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))" |
13575 | 104 |
(is "?P = (?L & ?R)") |
105 |
proof (cases "i < 0") |
|
106 |
case True thus ?thesis by simp |
|
107 |
next |
|
108 |
case False |
|
109 |
have "?P = ?L" |
|
110 |
proof |
|
111 |
assume ?P thus ?L using False by clarsimp |
|
112 |
next |
|
113 |
assume ?L thus ?P using False by simp |
|
114 |
qed |
|
115 |
with False show ?thesis by simp |
|
116 |
qed |
|
117 |
||
13685 | 118 |
subsubsection "Induction principles for int" |
119 |
||
120 |
(* `set:int': dummy construction *) |
|
121 |
theorem int_ge_induct[case_names base step,induct set:int]: |
|
122 |
assumes ge: "k \<le> (i::int)" and |
|
123 |
base: "P(k)" and |
|
124 |
step: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)" |
|
125 |
shows "P i" |
|
126 |
proof - |
|
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
127 |
{ fix n have "\<And>i::int. n = nat(i-k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i" |
13685 | 128 |
proof (induct n) |
129 |
case 0 |
|
130 |
hence "i = k" by arith |
|
131 |
thus "P i" using base by simp |
|
132 |
next |
|
133 |
case (Suc n) |
|
134 |
hence "n = nat((i - 1) - k)" by arith |
|
135 |
moreover |
|
136 |
have ki1: "k \<le> i - 1" using Suc.prems by arith |
|
137 |
ultimately |
|
138 |
have "P(i - 1)" by(rule Suc.hyps) |
|
139 |
from step[OF ki1 this] show ?case by simp |
|
140 |
qed |
|
141 |
} |
|
142 |
from this ge show ?thesis by fast |
|
143 |
qed |
|
144 |
||
145 |
(* `set:int': dummy construction *) |
|
146 |
theorem int_gr_induct[case_names base step,induct set:int]: |
|
147 |
assumes gr: "k < (i::int)" and |
|
148 |
base: "P(k+1)" and |
|
149 |
step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)" |
|
150 |
shows "P i" |
|
151 |
apply(rule int_ge_induct[of "k + 1"]) |
|
152 |
using gr apply arith |
|
153 |
apply(rule base) |
|
14259 | 154 |
apply (rule step, simp+) |
13685 | 155 |
done |
156 |
||
157 |
theorem int_le_induct[consumes 1,case_names base step]: |
|
158 |
assumes le: "i \<le> (k::int)" and |
|
159 |
base: "P(k)" and |
|
160 |
step: "\<And>i. \<lbrakk>i \<le> k; P i\<rbrakk> \<Longrightarrow> P(i - 1)" |
|
161 |
shows "P i" |
|
162 |
proof - |
|
14272
5efbb548107d
Tidying of the integer development; towards removing the
paulson
parents:
14271
diff
changeset
|
163 |
{ fix n have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i" |
13685 | 164 |
proof (induct n) |
165 |
case 0 |
|
166 |
hence "i = k" by arith |
|
167 |
thus "P i" using base by simp |
|
168 |
next |
|
169 |
case (Suc n) |
|
170 |
hence "n = nat(k - (i+1))" by arith |
|
171 |
moreover |
|
172 |
have ki1: "i + 1 \<le> k" using Suc.prems by arith |
|
173 |
ultimately |
|
174 |
have "P(i+1)" by(rule Suc.hyps) |
|
175 |
from step[OF ki1 this] show ?case by simp |
|
176 |
qed |
|
177 |
} |
|
178 |
from this le show ?thesis by fast |
|
179 |
qed |
|
180 |
||
181 |
theorem int_less_induct[consumes 1,case_names base step]: |
|
182 |
assumes less: "(i::int) < k" and |
|
183 |
base: "P(k - 1)" and |
|
184 |
step: "\<And>i. \<lbrakk>i < k; P i\<rbrakk> \<Longrightarrow> P(i - 1)" |
|
185 |
shows "P i" |
|
186 |
apply(rule int_le_induct[of _ "k - 1"]) |
|
187 |
using less apply arith |
|
188 |
apply(rule base) |
|
14259 | 189 |
apply (rule step, simp+) |
190 |
done |
|
191 |
||
192 |
subsection{*Intermediate value theorems*} |
|
193 |
||
194 |
lemma int_val_lemma: |
|
195 |
"(\<forall>i<n::nat. abs(f(i+1) - f i) \<le> 1) --> |
|
196 |
f 0 \<le> k --> k \<le> f n --> (\<exists>i \<le> n. f i = (k::int))" |
|
14271 | 197 |
apply (induct_tac "n", simp) |
14259 | 198 |
apply (intro strip) |
199 |
apply (erule impE, simp) |
|
200 |
apply (erule_tac x = n in allE, simp) |
|
201 |
apply (case_tac "k = f (n+1) ") |
|
202 |
apply force |
|
203 |
apply (erule impE) |
|
204 |
apply (simp add: zabs_def split add: split_if_asm) |
|
205 |
apply (blast intro: le_SucI) |
|
206 |
done |
|
207 |
||
208 |
lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)] |
|
209 |
||
210 |
lemma nat_intermed_int_val: |
|
211 |
"[| \<forall>i. m \<le> i & i < n --> abs(f(i + 1::nat) - f i) \<le> 1; m < n; |
|
212 |
f m \<le> k; k \<le> f n |] ==> ? i. m \<le> i & i \<le> n & f i = (k::int)" |
|
213 |
apply (cut_tac n = "n-m" and f = "%i. f (i+m) " and k = k |
|
214 |
in int_val_lemma) |
|
215 |
apply simp |
|
216 |
apply (erule impE) |
|
217 |
apply (intro strip) |
|
218 |
apply (erule_tac x = "i+m" in allE, arith) |
|
219 |
apply (erule exE) |
|
220 |
apply (rule_tac x = "i+m" in exI, arith) |
|
221 |
done |
|
222 |
||
223 |
||
224 |
subsection{*Products and 1, by T. M. Rasmussen*} |
|
225 |
||
226 |
lemma zmult_eq_self_iff: "(m = m*(n::int)) = (n = 1 | m = 0)" |
|
227 |
apply auto |
|
228 |
apply (subgoal_tac "m*1 = m*n") |
|
229 |
apply (drule zmult_cancel1 [THEN iffD1], auto) |
|
13685 | 230 |
done |
231 |
||
14259 | 232 |
lemma zless_1_zmult: "[| 1 < m; 1 < n |] ==> 1 < m*(n::int)" |
233 |
apply (rule_tac y = "1*n" in order_less_trans) |
|
234 |
apply (rule_tac [2] zmult_zless_mono1) |
|
235 |
apply (simp_all (no_asm_simp)) |
|
236 |
done |
|
237 |
||
238 |
lemma pos_zmult_eq_1_iff: "0 < (m::int) ==> (m * n = 1) = (m = 1 & n = 1)" |
|
239 |
apply auto |
|
240 |
apply (case_tac "m=1") |
|
241 |
apply (case_tac [2] "n=1") |
|
242 |
apply (case_tac [4] "m=1") |
|
243 |
apply (case_tac [5] "n=1", auto) |
|
244 |
apply (tactic"distinct_subgoals_tac") |
|
245 |
apply (subgoal_tac "1<m*n", simp) |
|
246 |
apply (rule zless_1_zmult, arith) |
|
247 |
apply (subgoal_tac "0<n", arith) |
|
248 |
apply (subgoal_tac "0<m*n") |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
249 |
apply (drule zero_less_mult_iff [THEN iffD1], auto) |
14259 | 250 |
done |
251 |
||
252 |
lemma zmult_eq_1_iff: "(m*n = (1::int)) = ((m = 1 & n = 1) | (m = -1 & n = -1))" |
|
253 |
apply (case_tac "0<m") |
|
14271 | 254 |
apply (simp add: pos_zmult_eq_1_iff) |
14259 | 255 |
apply (case_tac "m=0") |
14271 | 256 |
apply (simp del: number_of_reorient) |
14259 | 257 |
apply (subgoal_tac "0 < -m") |
258 |
apply (drule_tac n = "-n" in pos_zmult_eq_1_iff, auto) |
|
259 |
done |
|
260 |
||
261 |
||
262 |
subsection{*More about nat*} |
|
263 |
||
14271 | 264 |
(*Analogous to zadd_int*) |
265 |
lemma zdiff_int: "n \<le> m ==> int m - int n = int (m-n)" |
|
266 |
by (induct m n rule: diff_induct, simp_all) |
|
267 |
||
14259 | 268 |
lemma nat_add_distrib: |
269 |
"[| (0::int) \<le> z; 0 \<le> z' |] ==> nat (z+z') = nat z + nat z'" |
|
270 |
apply (rule inj_int [THEN injD]) |
|
14271 | 271 |
apply (simp add: zadd_int [symmetric]) |
14259 | 272 |
done |
273 |
||
274 |
lemma nat_diff_distrib: |
|
275 |
"[| (0::int) \<le> z'; z' \<le> z |] ==> nat (z-z') = nat z - nat z'" |
|
276 |
apply (rule inj_int [THEN injD]) |
|
14271 | 277 |
apply (simp add: zdiff_int [symmetric] nat_le_eq_zle) |
14259 | 278 |
done |
279 |
||
280 |
lemma nat_mult_distrib: "(0::int) \<le> z ==> nat (z*z') = nat z * nat z'" |
|
281 |
apply (case_tac "0 \<le> z'") |
|
282 |
apply (rule inj_int [THEN injD]) |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
283 |
apply (simp add: zmult_int [symmetric] zero_le_mult_iff) |
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
284 |
apply (simp add: mult_le_0_iff) |
14259 | 285 |
done |
286 |
||
287 |
lemma nat_mult_distrib_neg: "z \<le> (0::int) ==> nat(z*z') = nat(-z) * nat(-z')" |
|
288 |
apply (rule trans) |
|
289 |
apply (rule_tac [2] nat_mult_distrib, auto) |
|
290 |
done |
|
291 |
||
292 |
lemma nat_abs_mult_distrib: "nat (abs (w * z)) = nat (abs w) * nat (abs z)" |
|
293 |
apply (case_tac "z=0 | w=0") |
|
294 |
apply (auto simp add: zabs_def nat_mult_distrib [symmetric] |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
295 |
nat_mult_distrib_neg [symmetric] mult_less_0_iff) |
14259 | 296 |
done |
297 |
||
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14295
diff
changeset
|
298 |
|
14259 | 299 |
ML |
300 |
{* |
|
301 |
val zle_diff1_eq = thm "zle_diff1_eq"; |
|
302 |
val zle_add1_eq_le = thm "zle_add1_eq_le"; |
|
303 |
val nonneg_eq_int = thm "nonneg_eq_int"; |
|
304 |
val nat_eq_iff = thm "nat_eq_iff"; |
|
305 |
val nat_eq_iff2 = thm "nat_eq_iff2"; |
|
306 |
val nat_less_iff = thm "nat_less_iff"; |
|
307 |
val int_eq_iff = thm "int_eq_iff"; |
|
308 |
val nat_0 = thm "nat_0"; |
|
309 |
val nat_1 = thm "nat_1"; |
|
310 |
val nat_2 = thm "nat_2"; |
|
311 |
val nat_less_eq_zless = thm "nat_less_eq_zless"; |
|
312 |
val nat_le_eq_zle = thm "nat_le_eq_zle"; |
|
313 |
||
314 |
val nat_intermed_int_val = thm "nat_intermed_int_val"; |
|
315 |
val zmult_eq_self_iff = thm "zmult_eq_self_iff"; |
|
316 |
val zless_1_zmult = thm "zless_1_zmult"; |
|
317 |
val pos_zmult_eq_1_iff = thm "pos_zmult_eq_1_iff"; |
|
318 |
val zmult_eq_1_iff = thm "zmult_eq_1_iff"; |
|
319 |
val nat_add_distrib = thm "nat_add_distrib"; |
|
320 |
val nat_diff_distrib = thm "nat_diff_distrib"; |
|
321 |
val nat_mult_distrib = thm "nat_mult_distrib"; |
|
322 |
val nat_mult_distrib_neg = thm "nat_mult_distrib_neg"; |
|
323 |
val nat_abs_mult_distrib = thm "nat_abs_mult_distrib"; |
|
324 |
*} |
|
325 |
||
7707 | 326 |
end |