author | wenzelm |
Fri, 27 Jul 2018 22:23:37 +0200 | |
changeset 68695 | 9072bfd24d8f |
parent 68389 | 1c84a8c513af |
child 69198 | 9218b7652839 |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: HOL/Parity.thy |
2 |
Author: Jeremy Avigad |
|
3 |
Author: Jacques D. Fleuriot |
|
21256 | 4 |
*) |
5 |
||
60758 | 6 |
section \<open>Parity in rings and semirings\<close> |
21256 | 7 |
|
8 |
theory Parity |
|
66815 | 9 |
imports Euclidean_Division |
21256 | 10 |
begin |
11 |
||
61799 | 12 |
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close> |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
13 |
|
67905 | 14 |
class semiring_parity = semidom + semiring_char_0 + unique_euclidean_semiring + |
66815 | 15 |
assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n" |
66839 | 16 |
and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1" |
17 |
and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a" |
|
18 |
begin |
|
19 |
||
20 |
lemma division_segment_eq_iff: |
|
21 |
"a = b" if "division_segment a = division_segment b" |
|
22 |
and "euclidean_size a = euclidean_size b" |
|
23 |
using that division_segment_euclidean_size [of a] by simp |
|
24 |
||
25 |
lemma euclidean_size_of_nat [simp]: |
|
26 |
"euclidean_size (of_nat n) = n" |
|
27 |
proof - |
|
28 |
have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n" |
|
29 |
by (fact division_segment_euclidean_size) |
|
30 |
then show ?thesis by simp |
|
31 |
qed |
|
66815 | 32 |
|
66839 | 33 |
lemma of_nat_euclidean_size: |
34 |
"of_nat (euclidean_size a) = a div division_segment a" |
|
35 |
proof - |
|
36 |
have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a" |
|
37 |
by (subst nonzero_mult_div_cancel_left) simp_all |
|
38 |
also have "\<dots> = a div division_segment a" |
|
39 |
by simp |
|
40 |
finally show ?thesis . |
|
41 |
qed |
|
42 |
||
43 |
lemma division_segment_1 [simp]: |
|
44 |
"division_segment 1 = 1" |
|
45 |
using division_segment_of_nat [of 1] by simp |
|
46 |
||
47 |
lemma division_segment_numeral [simp]: |
|
48 |
"division_segment (numeral k) = 1" |
|
49 |
using division_segment_of_nat [of "numeral k"] by simp |
|
50 |
||
51 |
lemma euclidean_size_1 [simp]: |
|
52 |
"euclidean_size 1 = 1" |
|
53 |
using euclidean_size_of_nat [of 1] by simp |
|
54 |
||
55 |
lemma euclidean_size_numeral [simp]: |
|
56 |
"euclidean_size (numeral k) = numeral k" |
|
57 |
using euclidean_size_of_nat [of "numeral k"] by simp |
|
21256 | 58 |
|
66815 | 59 |
lemma of_nat_dvd_iff: |
60 |
"of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q") |
|
61 |
proof (cases "m = 0") |
|
62 |
case True |
|
63 |
then show ?thesis |
|
64 |
by simp |
|
65 |
next |
|
66 |
case False |
|
67 |
show ?thesis |
|
68 |
proof |
|
69 |
assume ?Q |
|
70 |
then show ?P |
|
71 |
by (auto elim: dvd_class.dvdE) |
|
72 |
next |
|
73 |
assume ?P |
|
74 |
with False have "of_nat n = of_nat n div of_nat m * of_nat m" |
|
75 |
by simp |
|
76 |
then have "of_nat n = of_nat (n div m * m)" |
|
77 |
by (simp add: of_nat_div) |
|
78 |
then have "n = n div m * m" |
|
79 |
by (simp only: of_nat_eq_iff) |
|
80 |
then have "n = m * (n div m)" |
|
81 |
by (simp add: ac_simps) |
|
82 |
then show ?Q .. |
|
83 |
qed |
|
84 |
qed |
|
85 |
||
86 |
lemma of_nat_mod: |
|
87 |
"of_nat (m mod n) = of_nat m mod of_nat n" |
|
88 |
proof - |
|
89 |
have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m" |
|
90 |
by (simp add: div_mult_mod_eq) |
|
91 |
also have "of_nat m = of_nat (m div n * n + m mod n)" |
|
92 |
by simp |
|
93 |
finally show ?thesis |
|
94 |
by (simp only: of_nat_div of_nat_mult of_nat_add) simp |
|
95 |
qed |
|
96 |
||
97 |
lemma one_div_two_eq_zero [simp]: |
|
98 |
"1 div 2 = 0" |
|
99 |
proof - |
|
100 |
from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0" |
|
101 |
by (simp only:) simp |
|
102 |
then show ?thesis |
|
103 |
by simp |
|
104 |
qed |
|
105 |
||
106 |
lemma one_mod_two_eq_one [simp]: |
|
107 |
"1 mod 2 = 1" |
|
108 |
proof - |
|
109 |
from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1" |
|
110 |
by (simp only:) simp |
|
111 |
then show ?thesis |
|
112 |
by simp |
|
113 |
qed |
|
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
114 |
|
58740 | 115 |
abbreviation even :: "'a \<Rightarrow> bool" |
63654 | 116 |
where "even a \<equiv> 2 dvd a" |
54228 | 117 |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
118 |
abbreviation odd :: "'a \<Rightarrow> bool" |
63654 | 119 |
where "odd a \<equiv> \<not> 2 dvd a" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
120 |
|
66815 | 121 |
lemma even_iff_mod_2_eq_zero: |
122 |
"even a \<longleftrightarrow> a mod 2 = 0" |
|
123 |
by (fact dvd_eq_mod_eq_0) |
|
124 |
||
125 |
lemma odd_iff_mod_2_eq_one: |
|
126 |
"odd a \<longleftrightarrow> a mod 2 = 1" |
|
66839 | 127 |
proof |
128 |
assume "a mod 2 = 1" |
|
129 |
then show "odd a" |
|
130 |
by auto |
|
131 |
next |
|
132 |
assume "odd a" |
|
133 |
have eucl: "euclidean_size (a mod 2) = 1" |
|
134 |
proof (rule order_antisym) |
|
135 |
show "euclidean_size (a mod 2) \<le> 1" |
|
136 |
using mod_size_less [of 2 a] by simp |
|
137 |
show "1 \<le> euclidean_size (a mod 2)" |
|
66840 | 138 |
using \<open>odd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0) |
66839 | 139 |
qed |
140 |
from \<open>odd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)" |
|
141 |
by simp |
|
142 |
then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)" |
|
143 |
by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment) |
|
144 |
then have "\<not> 2 dvd euclidean_size a" |
|
145 |
using of_nat_dvd_iff [of 2] by simp |
|
146 |
then have "euclidean_size a mod 2 = 1" |
|
147 |
by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0) |
|
148 |
then have "of_nat (euclidean_size a mod 2) = of_nat 1" |
|
149 |
by simp |
|
150 |
then have "of_nat (euclidean_size a) mod 2 = 1" |
|
151 |
by (simp add: of_nat_mod) |
|
152 |
from \<open>odd a\<close> eucl |
|
153 |
show "a mod 2 = 1" |
|
154 |
by (auto intro: division_segment_eq_iff simp add: division_segment_mod) |
|
155 |
qed |
|
58787 | 156 |
|
66815 | 157 |
lemma parity_cases [case_names even odd]: |
158 |
assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P" |
|
159 |
assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P" |
|
160 |
shows P |
|
161 |
using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one) |
|
162 |
||
163 |
lemma not_mod_2_eq_1_eq_0 [simp]: |
|
164 |
"a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0" |
|
165 |
by (cases a rule: parity_cases) simp_all |
|
166 |
||
167 |
lemma not_mod_2_eq_0_eq_1 [simp]: |
|
168 |
"a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1" |
|
169 |
by (cases a rule: parity_cases) simp_all |
|
58787 | 170 |
|
58690 | 171 |
lemma evenE [elim?]: |
172 |
assumes "even a" |
|
173 |
obtains b where "a = 2 * b" |
|
58740 | 174 |
using assms by (rule dvdE) |
58690 | 175 |
|
58681 | 176 |
lemma oddE [elim?]: |
58680 | 177 |
assumes "odd a" |
178 |
obtains b where "a = 2 * b + 1" |
|
58787 | 179 |
proof - |
66815 | 180 |
have "a = 2 * (a div 2) + a mod 2" |
181 |
by (simp add: mult_div_mod_eq) |
|
182 |
with assms have "a = 2 * (a div 2) + 1" |
|
183 |
by (simp add: odd_iff_mod_2_eq_one) |
|
184 |
then show ?thesis .. |
|
185 |
qed |
|
186 |
||
187 |
lemma mod_2_eq_odd: |
|
188 |
"a mod 2 = of_bool (odd a)" |
|
189 |
by (auto elim: oddE) |
|
190 |
||
67816 | 191 |
lemma of_bool_odd_eq_mod_2: |
192 |
"of_bool (odd a) = a mod 2" |
|
193 |
by (simp add: mod_2_eq_odd) |
|
194 |
||
66815 | 195 |
lemma one_mod_2_pow_eq [simp]: |
196 |
"1 mod (2 ^ n) = of_bool (n > 0)" |
|
197 |
proof - |
|
67083 | 198 |
have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))" |
199 |
using of_nat_mod [of 1 "2 ^ n"] by simp |
|
200 |
also have "\<dots> = of_bool (n > 0)" |
|
66815 | 201 |
by simp |
67083 | 202 |
finally show ?thesis . |
66815 | 203 |
qed |
204 |
||
67816 | 205 |
lemma one_div_2_pow_eq [simp]: |
206 |
"1 div (2 ^ n) = of_bool (n = 0)" |
|
207 |
using div_mult_mod_eq [of 1 "2 ^ n"] by auto |
|
208 |
||
66815 | 209 |
lemma even_of_nat [simp]: |
210 |
"even (of_nat a) \<longleftrightarrow> even a" |
|
211 |
proof - |
|
212 |
have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a" |
|
213 |
by simp |
|
214 |
also have "\<dots> \<longleftrightarrow> even a" |
|
215 |
by (simp only: of_nat_dvd_iff) |
|
216 |
finally show ?thesis . |
|
217 |
qed |
|
218 |
||
219 |
lemma even_zero [simp]: |
|
220 |
"even 0" |
|
221 |
by (fact dvd_0_right) |
|
222 |
||
223 |
lemma odd_one [simp]: |
|
224 |
"odd 1" |
|
225 |
proof - |
|
226 |
have "\<not> (2 :: nat) dvd 1" |
|
227 |
by simp |
|
228 |
then have "\<not> of_nat 2 dvd of_nat 1" |
|
229 |
unfolding of_nat_dvd_iff by simp |
|
230 |
then show ?thesis |
|
231 |
by simp |
|
58787 | 232 |
qed |
63654 | 233 |
|
66815 | 234 |
lemma odd_even_add: |
235 |
"even (a + b)" if "odd a" and "odd b" |
|
236 |
proof - |
|
237 |
from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1" |
|
238 |
by (blast elim: oddE) |
|
239 |
then have "a + b = 2 * c + 2 * d + (1 + 1)" |
|
240 |
by (simp only: ac_simps) |
|
241 |
also have "\<dots> = 2 * (c + d + 1)" |
|
242 |
by (simp add: algebra_simps) |
|
243 |
finally show ?thesis .. |
|
244 |
qed |
|
245 |
||
246 |
lemma even_add [simp]: |
|
247 |
"even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)" |
|
248 |
by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add) |
|
249 |
||
250 |
lemma odd_add [simp]: |
|
251 |
"odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)" |
|
252 |
by simp |
|
253 |
||
254 |
lemma even_plus_one_iff [simp]: |
|
255 |
"even (a + 1) \<longleftrightarrow> odd a" |
|
256 |
by (auto simp add: dvd_add_right_iff intro: odd_even_add) |
|
257 |
||
258 |
lemma even_mult_iff [simp]: |
|
259 |
"even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q") |
|
260 |
proof |
|
261 |
assume ?Q |
|
262 |
then show ?P |
|
263 |
by auto |
|
264 |
next |
|
265 |
assume ?P |
|
266 |
show ?Q |
|
267 |
proof (rule ccontr) |
|
268 |
assume "\<not> (even a \<or> even b)" |
|
269 |
then have "odd a" and "odd b" |
|
270 |
by auto |
|
271 |
then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1" |
|
272 |
by (blast elim: oddE) |
|
273 |
then have "a * b = (2 * r + 1) * (2 * s + 1)" |
|
274 |
by simp |
|
275 |
also have "\<dots> = 2 * (2 * r * s + r + s) + 1" |
|
276 |
by (simp add: algebra_simps) |
|
277 |
finally have "odd (a * b)" |
|
278 |
by simp |
|
279 |
with \<open>?P\<close> show False |
|
280 |
by auto |
|
281 |
qed |
|
282 |
qed |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
283 |
|
63654 | 284 |
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
285 |
proof - |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
286 |
have "even (2 * numeral n)" |
66815 | 287 |
unfolding even_mult_iff by simp |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
288 |
then have "even (numeral n + numeral n)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
289 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
290 |
then show ?thesis |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
291 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
292 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
293 |
|
63654 | 294 |
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
295 |
proof |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
296 |
assume "even (numeral (num.Bit1 n))" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
297 |
then have "even (numeral n + numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
298 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
299 |
then have "even (2 * numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
300 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
301 |
then have "2 dvd numeral n * 2 + 1" |
58740 | 302 |
by (simp add: ac_simps) |
63654 | 303 |
then have "2 dvd 1" |
304 |
using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
305 |
then show False by simp |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
306 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
307 |
|
63654 | 308 |
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0" |
58680 | 309 |
by (induct n) auto |
310 |
||
66815 | 311 |
lemma even_succ_div_two [simp]: |
312 |
"even a \<Longrightarrow> (a + 1) div 2 = a div 2" |
|
313 |
by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero) |
|
314 |
||
315 |
lemma odd_succ_div_two [simp]: |
|
316 |
"odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1" |
|
317 |
by (auto elim!: oddE simp add: add.assoc) |
|
318 |
||
319 |
lemma even_two_times_div_two: |
|
320 |
"even a \<Longrightarrow> 2 * (a div 2) = a" |
|
321 |
by (fact dvd_mult_div_cancel) |
|
322 |
||
323 |
lemma odd_two_times_div_two_succ [simp]: |
|
324 |
"odd a \<Longrightarrow> 2 * (a div 2) + 1 = a" |
|
325 |
using mult_div_mod_eq [of 2 a] |
|
326 |
by (simp add: even_iff_mod_2_eq_zero) |
|
327 |
||
67051 | 328 |
lemma coprime_left_2_iff_odd [simp]: |
329 |
"coprime 2 a \<longleftrightarrow> odd a" |
|
330 |
proof |
|
331 |
assume "odd a" |
|
332 |
show "coprime 2 a" |
|
333 |
proof (rule coprimeI) |
|
334 |
fix b |
|
335 |
assume "b dvd 2" "b dvd a" |
|
336 |
then have "b dvd a mod 2" |
|
337 |
by (auto intro: dvd_mod) |
|
338 |
with \<open>odd a\<close> show "is_unit b" |
|
339 |
by (simp add: mod_2_eq_odd) |
|
340 |
qed |
|
341 |
next |
|
342 |
assume "coprime 2 a" |
|
343 |
show "odd a" |
|
344 |
proof (rule notI) |
|
345 |
assume "even a" |
|
346 |
then obtain b where "a = 2 * b" .. |
|
347 |
with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)" |
|
348 |
by simp |
|
349 |
moreover have "\<not> coprime 2 (2 * b)" |
|
350 |
by (rule not_coprimeI [of 2]) simp_all |
|
351 |
ultimately show False |
|
352 |
by blast |
|
353 |
qed |
|
354 |
qed |
|
355 |
||
356 |
lemma coprime_right_2_iff_odd [simp]: |
|
357 |
"coprime a 2 \<longleftrightarrow> odd a" |
|
358 |
using coprime_left_2_iff_odd [of a] by (simp add: ac_simps) |
|
359 |
||
67828 | 360 |
lemma div_mult2_eq': |
361 |
"a div (of_nat m * of_nat n) = a div of_nat m div of_nat n" |
|
362 |
proof (cases a "of_nat m * of_nat n" rule: divmod_cases) |
|
363 |
case (divides q) |
|
364 |
then show ?thesis |
|
365 |
using nonzero_mult_div_cancel_right [of "of_nat m" "q * of_nat n"] |
|
366 |
by (simp add: ac_simps) |
|
367 |
next |
|
368 |
case (remainder q r) |
|
369 |
then have "division_segment r = 1" |
|
370 |
using division_segment_of_nat [of "m * n"] by simp |
|
371 |
with division_segment_euclidean_size [of r] |
|
372 |
have "of_nat (euclidean_size r) = r" |
|
373 |
by simp |
|
67908 | 374 |
have "a mod (of_nat m * of_nat n) div (of_nat m * of_nat n) = 0" |
375 |
by simp |
|
376 |
with remainder(6) have "r div (of_nat m * of_nat n) = 0" |
|
67828 | 377 |
by simp |
67908 | 378 |
with \<open>of_nat (euclidean_size r) = r\<close> |
379 |
have "of_nat (euclidean_size r) div (of_nat m * of_nat n) = 0" |
|
380 |
by simp |
|
381 |
then have "of_nat (euclidean_size r div (m * n)) = 0" |
|
67828 | 382 |
by (simp add: of_nat_div) |
67908 | 383 |
then have "of_nat (euclidean_size r div m div n) = 0" |
384 |
by (simp add: div_mult2_eq) |
|
385 |
with \<open>of_nat (euclidean_size r) = r\<close> have "r div of_nat m div of_nat n = 0" |
|
386 |
by (simp add: of_nat_div) |
|
67828 | 387 |
with remainder(1) |
388 |
have "q = (r div of_nat m + q * of_nat n * of_nat m div of_nat m) div of_nat n" |
|
389 |
by simp |
|
67908 | 390 |
with remainder(5) remainder(7) show ?thesis |
67828 | 391 |
using div_plus_div_distrib_dvd_right [of "of_nat m" "q * (of_nat m * of_nat n)" r] |
392 |
by (simp add: ac_simps) |
|
393 |
next |
|
394 |
case by0 |
|
395 |
then show ?thesis |
|
396 |
by auto |
|
397 |
qed |
|
398 |
||
399 |
lemma mod_mult2_eq': |
|
400 |
"a mod (of_nat m * of_nat n) = of_nat m * (a div of_nat m mod of_nat n) + a mod of_nat m" |
|
401 |
proof - |
|
402 |
have "a div (of_nat m * of_nat n) * (of_nat m * of_nat n) + a mod (of_nat m * of_nat n) = a div of_nat m div of_nat n * of_nat n * of_nat m + (a div of_nat m mod of_nat n * of_nat m + a mod of_nat m)" |
|
403 |
by (simp add: combine_common_factor div_mult_mod_eq) |
|
404 |
moreover have "a div of_nat m div of_nat n * of_nat n * of_nat m = of_nat n * of_nat m * (a div of_nat m div of_nat n)" |
|
405 |
by (simp add: ac_simps) |
|
406 |
ultimately show ?thesis |
|
407 |
by (simp add: div_mult2_eq' mult_commute) |
|
408 |
qed |
|
409 |
||
68028 | 410 |
lemma div_mult2_numeral_eq: |
411 |
"a div numeral k div numeral l = a div numeral (k * l)" (is "?A = ?B") |
|
412 |
proof - |
|
413 |
have "?A = a div of_nat (numeral k) div of_nat (numeral l)" |
|
414 |
by simp |
|
415 |
also have "\<dots> = a div (of_nat (numeral k) * of_nat (numeral l))" |
|
416 |
by (fact div_mult2_eq' [symmetric]) |
|
417 |
also have "\<dots> = ?B" |
|
418 |
by simp |
|
419 |
finally show ?thesis . |
|
420 |
qed |
|
421 |
||
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
422 |
end |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
423 |
|
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
424 |
class ring_parity = ring + semiring_parity |
58679 | 425 |
begin |
426 |
||
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
427 |
subclass comm_ring_1 .. |
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
428 |
|
67816 | 429 |
lemma even_minus: |
66815 | 430 |
"even (- a) \<longleftrightarrow> even a" |
58740 | 431 |
by (fact dvd_minus_iff) |
58679 | 432 |
|
66815 | 433 |
lemma even_diff [simp]: |
434 |
"even (a - b) \<longleftrightarrow> even (a + b)" |
|
58680 | 435 |
using even_add [of a "- b"] by simp |
436 |
||
67906 | 437 |
lemma minus_1_mod_2_eq [simp]: |
438 |
"- 1 mod 2 = 1" |
|
439 |
by (simp add: mod_2_eq_odd) |
|
440 |
||
441 |
lemma minus_1_div_2_eq [simp]: |
|
442 |
"- 1 div 2 = - 1" |
|
443 |
proof - |
|
444 |
from div_mult_mod_eq [of "- 1" 2] |
|
445 |
have "- 1 div 2 * 2 = - 1 * 2" |
|
446 |
using local.add_implies_diff by fastforce |
|
447 |
then show ?thesis |
|
448 |
using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp |
|
449 |
qed |
|
450 |
||
58679 | 451 |
end |
452 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
453 |
|
66815 | 454 |
subsection \<open>Instance for @{typ nat}\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
455 |
|
66815 | 456 |
instance nat :: semiring_parity |
457 |
by standard (simp_all add: dvd_eq_mod_eq_0) |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
458 |
|
66815 | 459 |
lemma even_Suc_Suc_iff [simp]: |
460 |
"even (Suc (Suc n)) \<longleftrightarrow> even n" |
|
58787 | 461 |
using dvd_add_triv_right_iff [of 2 n] by simp |
58687 | 462 |
|
66815 | 463 |
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n" |
464 |
using even_plus_one_iff [of n] by simp |
|
58787 | 465 |
|
66815 | 466 |
lemma even_diff_nat [simp]: |
467 |
"even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat |
|
58787 | 468 |
proof (cases "n \<le> m") |
469 |
case True |
|
470 |
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right) |
|
66815 | 471 |
moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp |
472 |
ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:) |
|
58787 | 473 |
then show ?thesis by auto |
474 |
next |
|
475 |
case False |
|
476 |
then show ?thesis by simp |
|
63654 | 477 |
qed |
478 |
||
66815 | 479 |
lemma odd_pos: |
480 |
"odd n \<Longrightarrow> 0 < n" for n :: nat |
|
58690 | 481 |
by (auto elim: oddE) |
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
482 |
|
66815 | 483 |
lemma Suc_double_not_eq_double: |
484 |
"Suc (2 * m) \<noteq> 2 * n" |
|
62597 | 485 |
proof |
486 |
assume "Suc (2 * m) = 2 * n" |
|
487 |
moreover have "odd (Suc (2 * m))" and "even (2 * n)" |
|
488 |
by simp_all |
|
489 |
ultimately show False by simp |
|
490 |
qed |
|
491 |
||
66815 | 492 |
lemma double_not_eq_Suc_double: |
493 |
"2 * m \<noteq> Suc (2 * n)" |
|
62597 | 494 |
using Suc_double_not_eq_double [of n m] by simp |
495 |
||
66815 | 496 |
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n" |
497 |
by (auto elim: oddE) |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
498 |
|
66815 | 499 |
lemma even_Suc_div_two [simp]: |
500 |
"even n \<Longrightarrow> Suc n div 2 = n div 2" |
|
501 |
using even_succ_div_two [of n] by simp |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
502 |
|
66815 | 503 |
lemma odd_Suc_div_two [simp]: |
504 |
"odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)" |
|
505 |
using odd_succ_div_two [of n] by simp |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
506 |
|
66815 | 507 |
lemma odd_two_times_div_two_nat [simp]: |
508 |
assumes "odd n" |
|
509 |
shows "2 * (n div 2) = n - (1 :: nat)" |
|
510 |
proof - |
|
511 |
from assms have "2 * (n div 2) + 1 = n" |
|
512 |
by (rule odd_two_times_div_two_succ) |
|
513 |
then have "Suc (2 * (n div 2)) - 1 = n - 1" |
|
58787 | 514 |
by simp |
66815 | 515 |
then show ?thesis |
516 |
by simp |
|
58787 | 517 |
qed |
58680 | 518 |
|
66815 | 519 |
lemma parity_induct [case_names zero even odd]: |
520 |
assumes zero: "P 0" |
|
521 |
assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)" |
|
522 |
assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))" |
|
523 |
shows "P n" |
|
524 |
proof (induct n rule: less_induct) |
|
525 |
case (less n) |
|
526 |
show "P n" |
|
527 |
proof (cases "n = 0") |
|
528 |
case True with zero show ?thesis by simp |
|
529 |
next |
|
530 |
case False |
|
531 |
with less have hyp: "P (n div 2)" by simp |
|
532 |
show ?thesis |
|
533 |
proof (cases "even n") |
|
534 |
case True |
|
535 |
with hyp even [of "n div 2"] show ?thesis |
|
536 |
by simp |
|
537 |
next |
|
538 |
case False |
|
539 |
with hyp odd [of "n div 2"] show ?thesis |
|
540 |
by simp |
|
541 |
qed |
|
542 |
qed |
|
543 |
qed |
|
58687 | 544 |
|
68157 | 545 |
lemma not_mod2_eq_Suc_0_eq_0 [simp]: |
546 |
"n mod 2 \<noteq> Suc 0 \<longleftrightarrow> n mod 2 = 0" |
|
547 |
using not_mod_2_eq_1_eq_0 [of n] by simp |
|
548 |
||
58687 | 549 |
|
60758 | 550 |
subsection \<open>Parity and powers\<close> |
58689 | 551 |
|
61531
ab2e862263e7
Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents:
60867
diff
changeset
|
552 |
context ring_1 |
58689 | 553 |
begin |
554 |
||
63654 | 555 |
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n" |
58690 | 556 |
by (auto elim: evenE) |
58689 | 557 |
|
63654 | 558 |
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)" |
58690 | 559 |
by (auto elim: oddE) |
560 |
||
66815 | 561 |
lemma uminus_power_if: |
562 |
"(- a) ^ n = (if even n then a ^ n else - (a ^ n))" |
|
563 |
by auto |
|
564 |
||
63654 | 565 |
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1" |
58690 | 566 |
by simp |
58689 | 567 |
|
63654 | 568 |
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1" |
58690 | 569 |
by simp |
58689 | 570 |
|
66582 | 571 |
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)" |
572 |
by (cases "even (n + k)") auto |
|
573 |
||
67371
2d9cf74943e1
moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents:
67083
diff
changeset
|
574 |
lemma minus_one_power_iff: "(- 1) ^ n = (if even n then 1 else - 1)" |
2d9cf74943e1
moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents:
67083
diff
changeset
|
575 |
by (induct n) auto |
2d9cf74943e1
moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents:
67083
diff
changeset
|
576 |
|
63654 | 577 |
end |
58689 | 578 |
|
579 |
context linordered_idom |
|
580 |
begin |
|
581 |
||
63654 | 582 |
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n" |
58690 | 583 |
by (auto elim: evenE) |
58689 | 584 |
|
63654 | 585 |
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a" |
58689 | 586 |
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE) |
587 |
||
63654 | 588 |
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a" |
58787 | 589 |
by (auto simp add: zero_le_even_power zero_le_odd_power) |
63654 | 590 |
|
591 |
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a" |
|
58689 | 592 |
proof - |
593 |
have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0" |
|
58787 | 594 |
unfolding power_eq_0_iff [of a n, symmetric] by blast |
58689 | 595 |
show ?thesis |
63654 | 596 |
unfolding less_le zero_le_power_eq by auto |
58689 | 597 |
qed |
598 |
||
63654 | 599 |
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0" |
58689 | 600 |
unfolding not_le [symmetric] zero_le_power_eq by auto |
601 |
||
63654 | 602 |
lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)" |
603 |
unfolding not_less [symmetric] zero_less_power_eq by auto |
|
604 |
||
605 |
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n" |
|
58689 | 606 |
using power_abs [of a n] by (simp add: zero_le_even_power) |
607 |
||
608 |
lemma power_mono_even: |
|
609 |
assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>" |
|
610 |
shows "a ^ n \<le> b ^ n" |
|
611 |
proof - |
|
612 |
have "0 \<le> \<bar>a\<bar>" by auto |
|
63654 | 613 |
with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" |
614 |
by (rule power_mono) |
|
615 |
with \<open>even n\<close> show ?thesis |
|
616 |
by (simp add: power_even_abs) |
|
58689 | 617 |
qed |
618 |
||
619 |
lemma power_mono_odd: |
|
620 |
assumes "odd n" and "a \<le> b" |
|
621 |
shows "a ^ n \<le> b ^ n" |
|
622 |
proof (cases "b < 0") |
|
63654 | 623 |
case True |
624 |
with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto |
|
625 |
then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono) |
|
60758 | 626 |
with \<open>odd n\<close> show ?thesis by simp |
58689 | 627 |
next |
63654 | 628 |
case False |
629 |
then have "0 \<le> b" by auto |
|
58689 | 630 |
show ?thesis |
631 |
proof (cases "a < 0") |
|
63654 | 632 |
case True |
633 |
then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto |
|
60758 | 634 |
then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto |
63654 | 635 |
moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto |
58689 | 636 |
ultimately show ?thesis by auto |
637 |
next |
|
63654 | 638 |
case False |
639 |
then have "0 \<le> a" by auto |
|
640 |
with \<open>a \<le> b\<close> show ?thesis |
|
641 |
using power_mono by auto |
|
58689 | 642 |
qed |
643 |
qed |
|
62083 | 644 |
|
60758 | 645 |
text \<open>Simplify, when the exponent is a numeral\<close> |
58689 | 646 |
|
647 |
lemma zero_le_power_eq_numeral [simp]: |
|
648 |
"0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a" |
|
649 |
by (fact zero_le_power_eq) |
|
650 |
||
651 |
lemma zero_less_power_eq_numeral [simp]: |
|
63654 | 652 |
"0 < a ^ numeral w \<longleftrightarrow> |
653 |
numeral w = (0 :: nat) \<or> |
|
654 |
even (numeral w :: nat) \<and> a \<noteq> 0 \<or> |
|
655 |
odd (numeral w :: nat) \<and> 0 < a" |
|
58689 | 656 |
by (fact zero_less_power_eq) |
657 |
||
658 |
lemma power_le_zero_eq_numeral [simp]: |
|
63654 | 659 |
"a ^ numeral w \<le> 0 \<longleftrightarrow> |
660 |
(0 :: nat) < numeral w \<and> |
|
661 |
(odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)" |
|
58689 | 662 |
by (fact power_le_zero_eq) |
663 |
||
664 |
lemma power_less_zero_eq_numeral [simp]: |
|
665 |
"a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0" |
|
666 |
by (fact power_less_zero_eq) |
|
667 |
||
668 |
lemma power_even_abs_numeral [simp]: |
|
669 |
"even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w" |
|
670 |
by (fact power_even_abs) |
|
671 |
||
672 |
end |
|
673 |
||
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
674 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
675 |
subsection \<open>Instance for @{typ int}\<close> |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
676 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
677 |
instance int :: ring_parity |
66839 | 678 |
by standard (simp_all add: dvd_eq_mod_eq_0 divide_int_def division_segment_int_def) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
679 |
|
67816 | 680 |
lemma even_diff_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
681 |
"even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 682 |
by (fact even_diff) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
683 |
|
67816 | 684 |
lemma even_abs_add_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
685 |
"even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 686 |
by simp |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
687 |
|
67816 | 688 |
lemma even_add_abs_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
689 |
"even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 690 |
by simp |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
691 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
692 |
lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k" |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
693 |
by (simp add: even_of_nat [of "nat k", where ?'a = int, symmetric]) |
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
694 |
|
67816 | 695 |
|
67828 | 696 |
subsection \<open>Abstract bit operations\<close> |
697 |
||
698 |
context semiring_parity |
|
67816 | 699 |
begin |
700 |
||
701 |
text \<open>The primary purpose of the following operations is |
|
702 |
to avoid ad-hoc simplification of concrete expressions @{term "2 ^ n"}\<close> |
|
703 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
704 |
definition push_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
705 |
where push_bit_eq_mult: "push_bit n a = a * 2 ^ n" |
67816 | 706 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
707 |
definition take_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a" |
68010 | 708 |
where take_bit_eq_mod: "take_bit n a = a mod 2 ^ n" |
67816 | 709 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
710 |
definition drop_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a" |
68010 | 711 |
where drop_bit_eq_div: "drop_bit n a = a div 2 ^ n" |
67816 | 712 |
|
713 |
lemma bit_ident: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
714 |
"push_bit n (drop_bit n a) + take_bit n a = a" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
715 |
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div) |
67816 | 716 |
|
67960 | 717 |
lemma push_bit_push_bit [simp]: |
718 |
"push_bit m (push_bit n a) = push_bit (m + n) a" |
|
719 |
by (simp add: push_bit_eq_mult power_add ac_simps) |
|
720 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
721 |
lemma take_bit_take_bit [simp]: |
67960 | 722 |
"take_bit m (take_bit n a) = take_bit (min m n) a" |
723 |
proof (cases "m \<le> n") |
|
724 |
case True |
|
725 |
then show ?thesis |
|
726 |
by (simp add: take_bit_eq_mod not_le min_def mod_mod_cancel le_imp_power_dvd) |
|
727 |
next |
|
728 |
case False |
|
729 |
then have "n < m" and "min m n = n" |
|
730 |
by simp_all |
|
731 |
then have "2 ^ m = of_nat (2 ^ n) * of_nat (2 ^ (m - n))" |
|
732 |
by (simp add: power_add [symmetric]) |
|
733 |
then have "a mod 2 ^ n mod 2 ^ m = a mod 2 ^ n mod (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))" |
|
734 |
by simp |
|
735 |
also have "\<dots> = of_nat (2 ^ n) * (a mod 2 ^ n div of_nat (2 ^ n) mod of_nat (2 ^ (m - n))) + a mod 2 ^ n mod of_nat (2 ^ n)" |
|
736 |
by (simp only: mod_mult2_eq') |
|
737 |
finally show ?thesis |
|
738 |
using \<open>min m n = n\<close> by (simp add: take_bit_eq_mod) |
|
739 |
qed |
|
740 |
||
741 |
lemma drop_bit_drop_bit [simp]: |
|
742 |
"drop_bit m (drop_bit n a) = drop_bit (m + n) a" |
|
743 |
proof - |
|
744 |
have "a div (2 ^ m * 2 ^ n) = a div (of_nat (2 ^ n) * of_nat (2 ^ m))" |
|
745 |
by (simp add: ac_simps) |
|
746 |
also have "\<dots> = a div of_nat (2 ^ n) div of_nat (2 ^ m)" |
|
747 |
by (simp only: div_mult2_eq') |
|
748 |
finally show ?thesis |
|
749 |
by (simp add: drop_bit_eq_div power_add) |
|
750 |
qed |
|
751 |
||
752 |
lemma push_bit_take_bit: |
|
753 |
"push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)" |
|
754 |
by (simp add: push_bit_eq_mult take_bit_eq_mod power_add mult_mod_right ac_simps) |
|
755 |
||
756 |
lemma take_bit_push_bit: |
|
757 |
"take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)" |
|
758 |
proof (cases "m \<le> n") |
|
759 |
case True |
|
760 |
then show ?thesis |
|
761 |
by (simp_all add: push_bit_eq_mult take_bit_eq_mod mod_eq_0_iff_dvd dvd_power_le) |
|
762 |
next |
|
763 |
case False |
|
764 |
then show ?thesis |
|
765 |
using push_bit_take_bit [of n "m - n" a] |
|
766 |
by simp |
|
767 |
qed |
|
768 |
||
769 |
lemma take_bit_drop_bit: |
|
770 |
"take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)" |
|
771 |
using mod_mult2_eq' [of a "2 ^ n" "2 ^ m"] |
|
772 |
by (simp add: drop_bit_eq_div take_bit_eq_mod power_add ac_simps) |
|
773 |
||
774 |
lemma drop_bit_take_bit: |
|
775 |
"drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)" |
|
776 |
proof (cases "m \<le> n") |
|
777 |
case True |
|
778 |
then show ?thesis |
|
779 |
using take_bit_drop_bit [of "n - m" m a] by simp |
|
780 |
next |
|
781 |
case False |
|
782 |
then have "a mod 2 ^ n div 2 ^ m = a mod 2 ^ n div 2 ^ (n + (m - n))" |
|
783 |
by simp |
|
784 |
also have "\<dots> = a mod 2 ^ n div (2 ^ n * 2 ^ (m - n))" |
|
785 |
by (simp add: power_add) |
|
786 |
also have "\<dots> = a mod 2 ^ n div (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))" |
|
787 |
by simp |
|
788 |
also have "\<dots> = a mod 2 ^ n div of_nat (2 ^ n) div of_nat (2 ^ (m - n))" |
|
789 |
by (simp only: div_mult2_eq') |
|
790 |
finally show ?thesis |
|
791 |
using False by (simp add: take_bit_eq_mod drop_bit_eq_div) |
|
792 |
qed |
|
793 |
||
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
794 |
lemma push_bit_0_id [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
795 |
"push_bit 0 = id" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
796 |
by (simp add: fun_eq_iff push_bit_eq_mult) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
797 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
798 |
lemma push_bit_of_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
799 |
"push_bit n 0 = 0" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
800 |
by (simp add: push_bit_eq_mult) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
801 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
802 |
lemma push_bit_of_1: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
803 |
"push_bit n 1 = 2 ^ n" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
804 |
by (simp add: push_bit_eq_mult) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
805 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
806 |
lemma push_bit_Suc [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
807 |
"push_bit (Suc n) a = push_bit n (a * 2)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
808 |
by (simp add: push_bit_eq_mult ac_simps) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
809 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
810 |
lemma push_bit_double: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
811 |
"push_bit n (a * 2) = push_bit n a * 2" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
812 |
by (simp add: push_bit_eq_mult ac_simps) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
813 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
814 |
lemma push_bit_eq_0_iff [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
815 |
"push_bit n a = 0 \<longleftrightarrow> a = 0" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
816 |
by (simp add: push_bit_eq_mult) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
817 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
818 |
lemma push_bit_add: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
819 |
"push_bit n (a + b) = push_bit n a + push_bit n b" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
820 |
by (simp add: push_bit_eq_mult algebra_simps) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
821 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
822 |
lemma push_bit_numeral [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
823 |
"push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
824 |
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric]) (simp add: ac_simps) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
825 |
|
68010 | 826 |
lemma push_bit_of_nat: |
827 |
"push_bit n (of_nat m) = of_nat (push_bit n m)" |
|
828 |
by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult) |
|
829 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
830 |
lemma take_bit_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
831 |
"take_bit 0 a = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
832 |
by (simp add: take_bit_eq_mod) |
67816 | 833 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
834 |
lemma take_bit_Suc [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
835 |
"take_bit (Suc n) a = take_bit n (a div 2) * 2 + of_bool (odd a)" |
67816 | 836 |
proof - |
837 |
have "1 + 2 * (a div 2) mod (2 * 2 ^ n) = (a div 2 * 2 + a mod 2) mod (2 * 2 ^ n)" |
|
838 |
if "odd a" |
|
839 |
using that mod_mult2_eq' [of "1 + 2 * (a div 2)" 2 "2 ^ n"] |
|
840 |
by (simp add: ac_simps odd_iff_mod_2_eq_one mult_mod_right) |
|
841 |
also have "\<dots> = a mod (2 * 2 ^ n)" |
|
842 |
by (simp only: div_mult_mod_eq) |
|
843 |
finally show ?thesis |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
844 |
by (simp add: take_bit_eq_mod algebra_simps mult_mod_right) |
67816 | 845 |
qed |
846 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
847 |
lemma take_bit_of_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
848 |
"take_bit n 0 = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
849 |
by (simp add: take_bit_eq_mod) |
67816 | 850 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
851 |
lemma take_bit_of_1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
852 |
"take_bit n 1 = of_bool (n > 0)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
853 |
by (simp add: take_bit_eq_mod) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
854 |
|
67961 | 855 |
lemma take_bit_add: |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
856 |
"take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
857 |
by (simp add: take_bit_eq_mod mod_simps) |
67816 | 858 |
|
67961 | 859 |
lemma take_bit_eq_0_iff: |
860 |
"take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a" |
|
861 |
by (simp add: take_bit_eq_mod mod_eq_0_iff_dvd) |
|
862 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
863 |
lemma take_bit_of_1_eq_0_iff [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
864 |
"take_bit n 1 = 0 \<longleftrightarrow> n = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
865 |
by (simp add: take_bit_eq_mod) |
67816 | 866 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
867 |
lemma even_take_bit_eq [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
868 |
"even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
869 |
by (cases n) (simp_all add: take_bit_eq_mod dvd_mod_iff) |
67816 | 870 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
871 |
lemma take_bit_numeral_bit0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
872 |
"take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
873 |
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] take_bit_Suc |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
874 |
ac_simps even_mult_iff nonzero_mult_div_cancel_right [OF numeral_neq_zero]) simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
875 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
876 |
lemma take_bit_numeral_bit1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
877 |
"take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
878 |
by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] take_bit_Suc |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
879 |
ac_simps even_add even_mult_iff div_mult_self1 [OF numeral_neq_zero]) (simp add: ac_simps) |
67961 | 880 |
|
68010 | 881 |
lemma take_bit_of_nat: |
882 |
"take_bit n (of_nat m) = of_nat (take_bit n m)" |
|
883 |
by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"]) |
|
884 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
885 |
lemma drop_bit_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
886 |
"drop_bit 0 = id" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
887 |
by (simp add: fun_eq_iff drop_bit_eq_div) |
67816 | 888 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
889 |
lemma drop_bit_of_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
890 |
"drop_bit n 0 = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
891 |
by (simp add: drop_bit_eq_div) |
67816 | 892 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
893 |
lemma drop_bit_of_1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
894 |
"drop_bit n 1 = of_bool (n = 0)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
895 |
by (simp add: drop_bit_eq_div) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
896 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
897 |
lemma drop_bit_Suc [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
898 |
"drop_bit (Suc n) a = drop_bit n (a div 2)" |
67816 | 899 |
proof (cases "even a") |
900 |
case True |
|
901 |
then obtain b where "a = 2 * b" .. |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
902 |
moreover have "drop_bit (Suc n) (2 * b) = drop_bit n b" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
903 |
by (simp add: drop_bit_eq_div) |
67816 | 904 |
ultimately show ?thesis |
905 |
by simp |
|
906 |
next |
|
907 |
case False |
|
908 |
then obtain b where "a = 2 * b + 1" .. |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
909 |
moreover have "drop_bit (Suc n) (2 * b + 1) = drop_bit n b" |
67816 | 910 |
using div_mult2_eq' [of "1 + b * 2" 2 "2 ^ n"] |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
911 |
by (auto simp add: drop_bit_eq_div ac_simps) |
67816 | 912 |
ultimately show ?thesis |
913 |
by simp |
|
914 |
qed |
|
915 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
916 |
lemma drop_bit_half: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
917 |
"drop_bit n (a div 2) = drop_bit n a div 2" |
67816 | 918 |
by (induction n arbitrary: a) simp_all |
919 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
920 |
lemma drop_bit_of_bool [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
921 |
"drop_bit n (of_bool d) = of_bool (n = 0 \<and> d)" |
67816 | 922 |
by (cases n) simp_all |
923 |
||
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
924 |
lemma drop_bit_numeral_bit0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
925 |
"drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
926 |
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] drop_bit_Suc |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
927 |
nonzero_mult_div_cancel_left [OF numeral_neq_zero]) |
67816 | 928 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
929 |
lemma drop_bit_numeral_bit1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
930 |
"drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
931 |
by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] drop_bit_Suc |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
932 |
div_mult_self4 [OF numeral_neq_zero]) simp |
67816 | 933 |
|
68010 | 934 |
lemma drop_bit_of_nat: |
935 |
"drop_bit n (of_nat m) = of_nat (drop_bit n m)" |
|
68389 | 936 |
by (simp add: drop_bit_eq_div Parity.drop_bit_eq_div of_nat_div [of m "2 ^ n"]) |
68010 | 937 |
|
58770 | 938 |
end |
67816 | 939 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
940 |
lemma push_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
941 |
"push_bit n (Suc 0) = 2 ^ n" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
942 |
using push_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
943 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
944 |
lemma take_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
945 |
"take_bit n (Suc 0) = of_bool (0 < n)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
946 |
using take_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
947 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
948 |
lemma drop_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
949 |
"drop_bit n (Suc 0) = of_bool (n = 0)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
950 |
using drop_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
951 |
|
67816 | 952 |
end |