author | huffman |
Sun, 25 Mar 2012 20:15:39 +0200 | |
changeset 47108 | 2a1953f0d20d |
parent 45604 | 29cf40fe8daf |
child 47163 | 248376f8881d |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
Author: Jeremy Dawson, NICTA |
|
24350 | 3 |
*) |
24333 | 4 |
|
24350 | 5 |
header {* Useful Numerical Lemmas *} |
24333 | 6 |
|
37655 | 7 |
theory Misc_Numeric |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45604
diff
changeset
|
8 |
imports "~~/src/HOL/Main" "~~/src/HOL/Parity" |
25592 | 9 |
begin |
24333 | 10 |
|
39910 | 11 |
lemma the_elemI: "y = {x} ==> the_elem y = x" |
12 |
by simp |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26072
diff
changeset
|
13 |
|
27570 | 14 |
lemma nonemptyE: "S ~= {} ==> (!!x. x : S ==> R) ==> R" by auto |
24333 | 15 |
|
27570 | 16 |
lemma gt_or_eq_0: "0 < y \<or> 0 = (y::nat)" by arith |
24333 | 17 |
|
24465 | 18 |
declare iszero_0 [iff] |
19 |
||
24333 | 20 |
lemmas xtr1 = xtrans(1) |
21 |
lemmas xtr2 = xtrans(2) |
|
22 |
lemmas xtr3 = xtrans(3) |
|
23 |
lemmas xtr4 = xtrans(4) |
|
24 |
lemmas xtr5 = xtrans(5) |
|
25 |
lemmas xtr6 = xtrans(6) |
|
26 |
lemmas xtr7 = xtrans(7) |
|
27 |
lemmas xtr8 = xtrans(8) |
|
28 |
||
24465 | 29 |
lemmas nat_simps = diff_add_inverse2 diff_add_inverse |
30 |
lemmas nat_iffs = le_add1 le_add2 |
|
31 |
||
27570 | 32 |
lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)" by arith |
24465 | 33 |
|
27570 | 34 |
lemma zless2: "0 < (2 :: int)" by arith |
24333 | 35 |
|
24465 | 36 |
lemmas zless2p [simp] = zless2 [THEN zero_less_power] |
37 |
lemmas zle2p [simp] = zless2p [THEN order_less_imp_le] |
|
38 |
||
39 |
lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]] |
|
40 |
lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]] |
|
41 |
||
27570 | 42 |
lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1" by arith |
24333 | 43 |
|
44 |
lemma emep1: |
|
45 |
"even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1" |
|
46 |
apply (simp add: add_commute) |
|
47 |
apply (safe dest!: even_equiv_def [THEN iffD1]) |
|
48 |
apply (subst pos_zmod_mult_2) |
|
49 |
apply arith |
|
30943
eb3dbbe971f6
zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents:
30445
diff
changeset
|
50 |
apply (simp add: mod_mult_mult1) |
24333 | 51 |
done |
52 |
||
53 |
lemmas eme1p = emep1 [simplified add_commute] |
|
54 |
||
27570 | 55 |
lemma le_diff_eq': "(a \<le> c - b) = (b + a \<le> (c::int))" by arith |
24465 | 56 |
|
27570 | 57 |
lemma less_diff_eq': "(a < c - b) = (b + a < (c::int))" by arith |
24465 | 58 |
|
27570 | 59 |
lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))" by arith |
24465 | 60 |
|
27570 | 61 |
lemma diff_less_eq': "(a - b < c) = (a < b + (c::int))" by arith |
24333 | 62 |
|
63 |
lemmas m1mod2k = zless2p [THEN zmod_minus1] |
|
24465 | 64 |
lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1] |
24333 | 65 |
lemmas p1mod22k' = zless2p [THEN order_less_imp_le, THEN pos_zmod_mult_2] |
24465 | 66 |
lemmas z1pmod2' = zero_le_one [THEN pos_zmod_mult_2, simplified] |
67 |
lemmas z1pdiv2' = zero_le_one [THEN pos_zdiv_mult_2, simplified] |
|
24333 | 68 |
|
69 |
lemma p1mod22k: |
|
70 |
"(2 * b + 1) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n) + (1::int)" |
|
71 |
by (simp add: p1mod22k' add_commute) |
|
24465 | 72 |
|
73 |
lemma z1pmod2: |
|
27570 | 74 |
"(2 * b + 1) mod 2 = (1::int)" by arith |
24465 | 75 |
|
76 |
lemma z1pdiv2: |
|
27570 | 77 |
"(2 * b + 1) div 2 = (b::int)" by arith |
24333 | 78 |
|
30031 | 79 |
lemmas zdiv_le_dividend = xtr3 [OF div_by_1 [symmetric] zdiv_mono2, |
45604 | 80 |
simplified int_one_le_iff_zero_less, simplified] |
24465 | 81 |
|
82 |
lemma axxbyy: |
|
83 |
"a + m + m = b + n + n ==> (a = 0 | a = 1) ==> (b = 0 | b = 1) ==> |
|
27570 | 84 |
a = b & m = (n :: int)" by arith |
24465 | 85 |
|
86 |
lemma axxmod2: |
|
27570 | 87 |
"(1 + x + x) mod 2 = (1 :: int) & (0 + x + x) mod 2 = (0 :: int)" by arith |
24465 | 88 |
|
89 |
lemma axxdiv2: |
|
27570 | 90 |
"(1 + x + x) div 2 = (x :: int) & (0 + x + x) div 2 = (x :: int)" by arith |
24465 | 91 |
|
92 |
lemmas iszero_minus = trans [THEN trans, |
|
45604 | 93 |
OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric]] |
24333 | 94 |
|
45604 | 95 |
lemmas zadd_diff_inverse = trans [OF diff_add_cancel [symmetric] add_commute] |
24333 | 96 |
|
45604 | 97 |
lemmas add_diff_cancel2 = add_commute [THEN diff_eq_eq [THEN iffD2]] |
24333 | 98 |
|
99 |
lemma zmod_uminus: "- ((a :: int) mod b) mod b = -a mod b" |
|
100 |
by (simp add : zmod_zminus1_eq_if) |
|
24465 | 101 |
|
102 |
lemma zmod_zsub_distrib: "((a::int) - b) mod c = (a mod c - b mod c) mod c" |
|
103 |
apply (unfold diff_int_def) |
|
29948 | 104 |
apply (rule trans [OF _ mod_add_eq [symmetric]]) |
105 |
apply (simp add: zmod_uminus mod_add_eq [symmetric]) |
|
24465 | 106 |
done |
24333 | 107 |
|
108 |
lemma zmod_zsub_right_eq: "((a::int) - b) mod c = (a - b mod c) mod c" |
|
109 |
apply (unfold diff_int_def) |
|
30034 | 110 |
apply (rule trans [OF _ mod_add_right_eq [symmetric]]) |
111 |
apply (simp add : zmod_uminus mod_add_right_eq [symmetric]) |
|
24333 | 112 |
done |
113 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
114 |
lemma zmod_zsub_left_eq: "((a::int) - b) mod c = (a mod c - b) mod c" |
30034 | 115 |
by (rule mod_add_left_eq [where b = "- b", simplified diff_int_def [symmetric]]) |
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
116 |
|
24333 | 117 |
lemma zmod_zsub_self [simp]: |
118 |
"((b :: int) - a) mod a = b mod a" |
|
119 |
by (simp add: zmod_zsub_right_eq) |
|
120 |
||
121 |
lemma zmod_zmult1_eq_rev: |
|
122 |
"b * a mod c = b mod c * a mod (c::int)" |
|
123 |
apply (simp add: mult_commute) |
|
124 |
apply (subst zmod_zmult1_eq) |
|
125 |
apply simp |
|
126 |
done |
|
127 |
||
128 |
lemmas rdmods [symmetric] = zmod_uminus [symmetric] |
|
30034 | 129 |
zmod_zsub_left_eq zmod_zsub_right_eq mod_add_left_eq |
130 |
mod_add_right_eq zmod_zmult1_eq zmod_zmult1_eq_rev |
|
24333 | 131 |
|
132 |
lemma mod_plus_right: |
|
133 |
"((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))" |
|
134 |
apply (induct x) |
|
135 |
apply (simp_all add: mod_Suc) |
|
136 |
apply arith |
|
137 |
done |
|
138 |
||
24465 | 139 |
lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)" |
140 |
by (induct n) (simp_all add : mod_Suc) |
|
141 |
||
142 |
lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric], |
|
45604 | 143 |
THEN mod_plus_right [THEN iffD2], simplified] |
24465 | 144 |
|
45604 | 145 |
lemmas push_mods' = mod_add_eq |
146 |
mod_mult_eq zmod_zsub_distrib |
|
147 |
zmod_uminus [symmetric] |
|
24465 | 148 |
|
45604 | 149 |
lemmas push_mods = push_mods' [THEN eq_reflection] |
150 |
lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection] |
|
24465 | 151 |
lemmas mod_simps = |
30034 | 152 |
mod_mult_self2_is_0 [THEN eq_reflection] |
153 |
mod_mult_self1_is_0 [THEN eq_reflection] |
|
24465 | 154 |
mod_mod_trivial [THEN eq_reflection] |
155 |
||
24333 | 156 |
lemma nat_mod_eq: |
157 |
"!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" |
|
158 |
by (induct a) auto |
|
159 |
||
160 |
lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq] |
|
161 |
||
162 |
lemma nat_mod_lem: |
|
163 |
"(0 :: nat) < n ==> b < n = (b mod n = b)" |
|
164 |
apply safe |
|
165 |
apply (erule nat_mod_eq') |
|
166 |
apply (erule subst) |
|
167 |
apply (erule mod_less_divisor) |
|
168 |
done |
|
169 |
||
170 |
lemma mod_nat_add: |
|
171 |
"(x :: nat) < z ==> y < z ==> |
|
172 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
173 |
apply (rule nat_mod_eq) |
|
174 |
apply auto |
|
175 |
apply (rule trans) |
|
176 |
apply (rule le_mod_geq) |
|
177 |
apply simp |
|
178 |
apply (rule nat_mod_eq') |
|
179 |
apply arith |
|
180 |
done |
|
24465 | 181 |
|
182 |
lemma mod_nat_sub: |
|
183 |
"(x :: nat) < z ==> (x - y) mod z = x - y" |
|
184 |
by (rule nat_mod_eq') arith |
|
24333 | 185 |
|
186 |
lemma int_mod_lem: |
|
187 |
"(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)" |
|
188 |
apply safe |
|
189 |
apply (erule (1) mod_pos_pos_trivial) |
|
190 |
apply (erule_tac [!] subst) |
|
191 |
apply auto |
|
192 |
done |
|
193 |
||
194 |
lemma int_mod_eq: |
|
195 |
"(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b" |
|
196 |
by clarsimp (rule mod_pos_pos_trivial) |
|
197 |
||
198 |
lemmas int_mod_eq' = refl [THEN [3] int_mod_eq] |
|
199 |
||
200 |
lemma int_mod_le: "0 <= a ==> 0 < (n :: int) ==> a mod n <= a" |
|
201 |
apply (cases "a < n") |
|
202 |
apply (auto dest: mod_pos_pos_trivial pos_mod_bound [where a=a]) |
|
203 |
done |
|
204 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
205 |
lemma int_mod_le': "0 <= b - n ==> 0 < (n :: int) ==> b mod n <= b - n" |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
206 |
by (rule int_mod_le [where a = "b - n" and n = n, simplified]) |
24333 | 207 |
|
208 |
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n" |
|
209 |
apply (cases "0 <= a") |
|
210 |
apply (drule (1) mod_pos_pos_trivial) |
|
211 |
apply simp |
|
212 |
apply (rule order_trans [OF _ pos_mod_sign]) |
|
213 |
apply simp |
|
214 |
apply assumption |
|
215 |
done |
|
216 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
217 |
lemma int_mod_ge': "b < 0 ==> 0 < (n :: int) ==> b + n <= b mod n" |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
218 |
by (rule int_mod_ge [where a = "b + n" and n = n, simplified]) |
24333 | 219 |
|
220 |
lemma mod_add_if_z: |
|
221 |
"(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> |
|
222 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
223 |
by (auto intro: int_mod_eq) |
|
224 |
||
225 |
lemma mod_sub_if_z: |
|
226 |
"(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> |
|
227 |
(x - y) mod z = (if y <= x then x - y else x - y + z)" |
|
228 |
by (auto intro: int_mod_eq) |
|
24465 | 229 |
|
230 |
lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric] |
|
231 |
lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule] |
|
232 |
||
233 |
(* already have this for naturals, div_mult_self1/2, but not for ints *) |
|
234 |
lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n" |
|
235 |
apply (rule mcl) |
|
236 |
prefer 2 |
|
237 |
apply (erule asm_rl) |
|
238 |
apply (simp add: zmde ring_distribs) |
|
239 |
done |
|
240 |
||
241 |
lemma mod_power_lem: |
|
242 |
"a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)" |
|
243 |
apply clarsimp |
|
244 |
apply safe |
|
30042 | 245 |
apply (simp add: dvd_eq_mod_eq_0 [symmetric]) |
24465 | 246 |
apply (drule le_iff_add [THEN iffD1]) |
44821 | 247 |
apply (force simp: power_add) |
24465 | 248 |
apply (rule mod_pos_pos_trivial) |
25875 | 249 |
apply (simp) |
24465 | 250 |
apply (rule power_strict_increasing) |
251 |
apply auto |
|
252 |
done |
|
24333 | 253 |
|
27570 | 254 |
lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)" by arith |
24333 | 255 |
|
256 |
lemmas min_pm1 [simp] = trans [OF add_commute min_pm] |
|
257 |
||
27570 | 258 |
lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)" by arith |
24333 | 259 |
|
260 |
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm] |
|
261 |
||
24465 | 262 |
lemma pl_pl_rels: |
263 |
"a + b = c + d ==> |
|
27570 | 264 |
a >= c & b <= d | a <= c & b >= (d :: nat)" by arith |
24465 | 265 |
|
266 |
lemmas pl_pl_rels' = add_commute [THEN [2] trans, THEN pl_pl_rels] |
|
267 |
||
27570 | 268 |
lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))" by arith |
24465 | 269 |
|
27570 | 270 |
lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b" by arith |
24465 | 271 |
|
272 |
lemmas pl_pl_mm' = add_commute [THEN [2] trans, THEN pl_pl_mm] |
|
273 |
||
27570 | 274 |
lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)" by arith |
24333 | 275 |
|
276 |
lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus] |
|
277 |
||
278 |
lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right] |
|
279 |
lemmas dtle = xtr3 [OF dme [symmetric] le_add1] |
|
280 |
lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle] |
|
281 |
||
282 |
lemma td_gal: |
|
283 |
"0 < c ==> (a >= b * c) = (a div c >= (b :: nat))" |
|
284 |
apply safe |
|
285 |
apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m]) |
|
286 |
apply (erule th2) |
|
287 |
done |
|
288 |
||
26072
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25937
diff
changeset
|
289 |
lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified] |
24333 | 290 |
|
291 |
lemma div_mult_le: "(a :: nat) div b * b <= a" |
|
292 |
apply (cases b) |
|
293 |
prefer 2 |
|
294 |
apply (rule order_refl [THEN th2]) |
|
295 |
apply auto |
|
296 |
done |
|
297 |
||
298 |
lemmas sdl = split_div_lemma [THEN iffD1, symmetric] |
|
299 |
||
300 |
lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l" |
|
301 |
by (rule sdl, assumption) (simp (no_asm)) |
|
302 |
||
303 |
lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l" |
|
304 |
apply (frule given_quot) |
|
305 |
apply (rule trans) |
|
306 |
prefer 2 |
|
307 |
apply (erule asm_rl) |
|
308 |
apply (rule_tac f="%n. n div f" in arg_cong) |
|
309 |
apply (simp add : mult_ac) |
|
310 |
done |
|
311 |
||
24465 | 312 |
lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b" |
313 |
apply (unfold dvd_def) |
|
314 |
apply clarify |
|
315 |
apply (case_tac k) |
|
316 |
apply clarsimp |
|
317 |
apply clarify |
|
318 |
apply (cases "b > 0") |
|
319 |
apply (drule mult_commute [THEN xtr1]) |
|
320 |
apply (frule (1) td_gal_lt [THEN iffD1]) |
|
321 |
apply (clarsimp simp: le_simps) |
|
322 |
apply (rule mult_div_cancel [THEN [2] xtr4]) |
|
323 |
apply (rule mult_mono) |
|
324 |
apply auto |
|
325 |
done |
|
326 |
||
24333 | 327 |
lemma less_le_mult': |
328 |
"w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)" |
|
329 |
apply (rule mult_right_mono) |
|
330 |
apply (rule zless_imp_add1_zle) |
|
331 |
apply (erule (1) mult_right_less_imp_less) |
|
332 |
apply assumption |
|
333 |
done |
|
334 |
||
335 |
lemmas less_le_mult = less_le_mult' [simplified left_distrib, simplified] |
|
24465 | 336 |
|
337 |
lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, |
|
45604 | 338 |
simplified left_diff_distrib] |
24333 | 339 |
|
340 |
lemma lrlem': |
|
341 |
assumes d: "(i::nat) \<le> j \<or> m < j'" |
|
342 |
assumes R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
343 |
assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
344 |
shows "R" using d |
|
345 |
apply safe |
|
346 |
apply (rule R1, erule mult_le_mono1) |
|
347 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
348 |
done |
|
349 |
||
350 |
lemma lrlem: "(0::nat) < sc ==> |
|
351 |
(sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)" |
|
352 |
apply safe |
|
353 |
apply arith |
|
354 |
apply (case_tac "sc >= n") |
|
355 |
apply arith |
|
356 |
apply (insert linorder_le_less_linear [of m lb]) |
|
357 |
apply (erule_tac k=n and k'=n in lrlem') |
|
358 |
apply arith |
|
359 |
apply simp |
|
360 |
done |
|
361 |
||
362 |
lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))" |
|
363 |
by auto |
|
364 |
||
27570 | 365 |
lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i" by arith |
24333 | 366 |
|
24465 | 367 |
lemma nonneg_mod_div: |
368 |
"0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b" |
|
369 |
apply (cases "b = 0", clarsimp) |
|
370 |
apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2]) |
|
371 |
done |
|
24399 | 372 |
|
24333 | 373 |
end |