author | wenzelm |
Sat, 07 Apr 2012 16:41:59 +0200 | |
changeset 47389 | e8552cba702d |
parent 47171 | 80c432404204 |
child 49962 | a8cc904a6820 |
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 |
|
47171 | 36 |
lemmas zless2p = zless2 [THEN zero_less_power] |
37 |
lemmas zle2p = zless2p [THEN order_less_imp_le] |
|
24465 | 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_zsub_self [simp]: |
|
100 |
"((b :: int) - a) mod a = b mod a" |
|
47168 | 101 |
by (simp add: mod_diff_right_eq) |
24333 | 102 |
|
47168 | 103 |
lemmas rdmods [symmetric] = mod_minus_eq |
104 |
mod_diff_left_eq mod_diff_right_eq mod_add_left_eq |
|
105 |
mod_add_right_eq mod_mult_right_eq mod_mult_left_eq |
|
24333 | 106 |
|
107 |
lemma mod_plus_right: |
|
108 |
"((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))" |
|
109 |
apply (induct x) |
|
110 |
apply (simp_all add: mod_Suc) |
|
111 |
apply arith |
|
112 |
done |
|
113 |
||
24465 | 114 |
lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)" |
115 |
by (induct n) (simp_all add : mod_Suc) |
|
116 |
||
117 |
lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric], |
|
45604 | 118 |
THEN mod_plus_right [THEN iffD2], simplified] |
24465 | 119 |
|
45604 | 120 |
lemmas push_mods' = mod_add_eq |
47168 | 121 |
mod_mult_eq mod_diff_eq |
122 |
mod_minus_eq |
|
24465 | 123 |
|
45604 | 124 |
lemmas push_mods = push_mods' [THEN eq_reflection] |
125 |
lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection] |
|
24465 | 126 |
lemmas mod_simps = |
30034 | 127 |
mod_mult_self2_is_0 [THEN eq_reflection] |
128 |
mod_mult_self1_is_0 [THEN eq_reflection] |
|
24465 | 129 |
mod_mod_trivial [THEN eq_reflection] |
130 |
||
24333 | 131 |
lemma nat_mod_eq: |
132 |
"!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" |
|
133 |
by (induct a) auto |
|
134 |
||
135 |
lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq] |
|
136 |
||
137 |
lemma nat_mod_lem: |
|
138 |
"(0 :: nat) < n ==> b < n = (b mod n = b)" |
|
139 |
apply safe |
|
140 |
apply (erule nat_mod_eq') |
|
141 |
apply (erule subst) |
|
142 |
apply (erule mod_less_divisor) |
|
143 |
done |
|
144 |
||
145 |
lemma mod_nat_add: |
|
146 |
"(x :: nat) < z ==> y < z ==> |
|
147 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
148 |
apply (rule nat_mod_eq) |
|
149 |
apply auto |
|
150 |
apply (rule trans) |
|
151 |
apply (rule le_mod_geq) |
|
152 |
apply simp |
|
153 |
apply (rule nat_mod_eq') |
|
154 |
apply arith |
|
155 |
done |
|
24465 | 156 |
|
157 |
lemma mod_nat_sub: |
|
158 |
"(x :: nat) < z ==> (x - y) mod z = x - y" |
|
159 |
by (rule nat_mod_eq') arith |
|
24333 | 160 |
|
161 |
lemma int_mod_lem: |
|
162 |
"(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)" |
|
163 |
apply safe |
|
164 |
apply (erule (1) mod_pos_pos_trivial) |
|
165 |
apply (erule_tac [!] subst) |
|
166 |
apply auto |
|
167 |
done |
|
168 |
||
169 |
lemma int_mod_eq: |
|
170 |
"(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b" |
|
171 |
by clarsimp (rule mod_pos_pos_trivial) |
|
172 |
||
173 |
lemmas int_mod_eq' = refl [THEN [3] int_mod_eq] |
|
174 |
||
47170 | 175 |
lemma int_mod_le: "(0::int) <= a ==> a mod n <= a" |
176 |
by (fact zmod_le_nonneg_dividend) (* FIXME: delete *) |
|
24333 | 177 |
|
47170 | 178 |
lemma int_mod_le': "(0::int) <= b - n ==> b mod n <= b - n" |
179 |
using zmod_le_nonneg_dividend [of "b - n" "n"] by simp |
|
24333 | 180 |
|
181 |
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n" |
|
182 |
apply (cases "0 <= a") |
|
183 |
apply (drule (1) mod_pos_pos_trivial) |
|
184 |
apply simp |
|
185 |
apply (rule order_trans [OF _ pos_mod_sign]) |
|
186 |
apply simp |
|
187 |
apply assumption |
|
188 |
done |
|
189 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
24465
diff
changeset
|
190 |
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
|
191 |
by (rule int_mod_ge [where a = "b + n" and n = n, simplified]) |
24333 | 192 |
|
193 |
lemma mod_add_if_z: |
|
194 |
"(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> |
|
195 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
196 |
by (auto intro: int_mod_eq) |
|
197 |
||
198 |
lemma mod_sub_if_z: |
|
199 |
"(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> |
|
200 |
(x - y) mod z = (if y <= x then x - y else x - y + z)" |
|
201 |
by (auto intro: int_mod_eq) |
|
24465 | 202 |
|
203 |
lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric] |
|
204 |
lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule] |
|
205 |
||
206 |
(* already have this for naturals, div_mult_self1/2, but not for ints *) |
|
207 |
lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n" |
|
208 |
apply (rule mcl) |
|
209 |
prefer 2 |
|
210 |
apply (erule asm_rl) |
|
211 |
apply (simp add: zmde ring_distribs) |
|
212 |
done |
|
213 |
||
214 |
lemma mod_power_lem: |
|
215 |
"a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)" |
|
216 |
apply clarsimp |
|
217 |
apply safe |
|
30042 | 218 |
apply (simp add: dvd_eq_mod_eq_0 [symmetric]) |
24465 | 219 |
apply (drule le_iff_add [THEN iffD1]) |
44821 | 220 |
apply (force simp: power_add) |
24465 | 221 |
apply (rule mod_pos_pos_trivial) |
25875 | 222 |
apply (simp) |
24465 | 223 |
apply (rule power_strict_increasing) |
224 |
apply auto |
|
225 |
done |
|
24333 | 226 |
|
27570 | 227 |
lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)" by arith |
24333 | 228 |
|
229 |
lemmas min_pm1 [simp] = trans [OF add_commute min_pm] |
|
230 |
||
27570 | 231 |
lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)" by arith |
24333 | 232 |
|
233 |
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm] |
|
234 |
||
24465 | 235 |
lemma pl_pl_rels: |
236 |
"a + b = c + d ==> |
|
27570 | 237 |
a >= c & b <= d | a <= c & b >= (d :: nat)" by arith |
24465 | 238 |
|
239 |
lemmas pl_pl_rels' = add_commute [THEN [2] trans, THEN pl_pl_rels] |
|
240 |
||
27570 | 241 |
lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))" by arith |
24465 | 242 |
|
27570 | 243 |
lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b" by arith |
24465 | 244 |
|
245 |
lemmas pl_pl_mm' = add_commute [THEN [2] trans, THEN pl_pl_mm] |
|
246 |
||
27570 | 247 |
lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)" by arith |
24333 | 248 |
|
249 |
lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus] |
|
250 |
||
251 |
lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right] |
|
252 |
lemmas dtle = xtr3 [OF dme [symmetric] le_add1] |
|
253 |
lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle] |
|
254 |
||
255 |
lemma td_gal: |
|
256 |
"0 < c ==> (a >= b * c) = (a div c >= (b :: nat))" |
|
257 |
apply safe |
|
258 |
apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m]) |
|
259 |
apply (erule th2) |
|
260 |
done |
|
261 |
||
26072
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25937
diff
changeset
|
262 |
lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified] |
24333 | 263 |
|
264 |
lemma div_mult_le: "(a :: nat) div b * b <= a" |
|
47168 | 265 |
by (fact dtle) |
24333 | 266 |
|
267 |
lemmas sdl = split_div_lemma [THEN iffD1, symmetric] |
|
268 |
||
269 |
lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l" |
|
270 |
by (rule sdl, assumption) (simp (no_asm)) |
|
271 |
||
272 |
lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l" |
|
273 |
apply (frule given_quot) |
|
274 |
apply (rule trans) |
|
275 |
prefer 2 |
|
276 |
apply (erule asm_rl) |
|
277 |
apply (rule_tac f="%n. n div f" in arg_cong) |
|
278 |
apply (simp add : mult_ac) |
|
279 |
done |
|
280 |
||
24465 | 281 |
lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b" |
282 |
apply (unfold dvd_def) |
|
283 |
apply clarify |
|
284 |
apply (case_tac k) |
|
285 |
apply clarsimp |
|
286 |
apply clarify |
|
287 |
apply (cases "b > 0") |
|
288 |
apply (drule mult_commute [THEN xtr1]) |
|
289 |
apply (frule (1) td_gal_lt [THEN iffD1]) |
|
290 |
apply (clarsimp simp: le_simps) |
|
291 |
apply (rule mult_div_cancel [THEN [2] xtr4]) |
|
292 |
apply (rule mult_mono) |
|
293 |
apply auto |
|
294 |
done |
|
295 |
||
24333 | 296 |
lemma less_le_mult': |
297 |
"w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)" |
|
298 |
apply (rule mult_right_mono) |
|
299 |
apply (rule zless_imp_add1_zle) |
|
300 |
apply (erule (1) mult_right_less_imp_less) |
|
301 |
apply assumption |
|
302 |
done |
|
303 |
||
304 |
lemmas less_le_mult = less_le_mult' [simplified left_distrib, simplified] |
|
24465 | 305 |
|
306 |
lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, |
|
45604 | 307 |
simplified left_diff_distrib] |
24333 | 308 |
|
309 |
lemma lrlem': |
|
310 |
assumes d: "(i::nat) \<le> j \<or> m < j'" |
|
311 |
assumes R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
312 |
assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
313 |
shows "R" using d |
|
314 |
apply safe |
|
315 |
apply (rule R1, erule mult_le_mono1) |
|
316 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
317 |
done |
|
318 |
||
319 |
lemma lrlem: "(0::nat) < sc ==> |
|
320 |
(sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)" |
|
321 |
apply safe |
|
322 |
apply arith |
|
323 |
apply (case_tac "sc >= n") |
|
324 |
apply arith |
|
325 |
apply (insert linorder_le_less_linear [of m lb]) |
|
326 |
apply (erule_tac k=n and k'=n in lrlem') |
|
327 |
apply arith |
|
328 |
apply simp |
|
329 |
done |
|
330 |
||
331 |
lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))" |
|
332 |
by auto |
|
333 |
||
27570 | 334 |
lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i" by arith |
24333 | 335 |
|
24465 | 336 |
lemma nonneg_mod_div: |
337 |
"0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b" |
|
338 |
apply (cases "b = 0", clarsimp) |
|
339 |
apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2]) |
|
340 |
done |
|
24399 | 341 |
|
24333 | 342 |
end |