author | haftmann |
Sat, 01 Feb 2020 19:10:37 +0100 | |
changeset 71412 | 96d126844adc |
parent 71408 | 554385d4cf59 |
child 71413 | 65ffe9e910d4 |
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 |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
14 |
class semiring_parity = comm_semiring_1 + semiring_modulo + |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
15 |
assumes even_iff_mod_2_eq_zero: "2 dvd a \<longleftrightarrow> a mod 2 = 0" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
16 |
and odd_iff_mod_2_eq_one: "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
17 |
and odd_one [simp]: "\<not> 2 dvd 1" |
66839 | 18 |
begin |
19 |
||
58740 | 20 |
abbreviation even :: "'a \<Rightarrow> bool" |
63654 | 21 |
where "even a \<equiv> 2 dvd a" |
54228 | 22 |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
23 |
abbreviation odd :: "'a \<Rightarrow> bool" |
63654 | 24 |
where "odd a \<equiv> \<not> 2 dvd a" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
25 |
|
66815 | 26 |
lemma parity_cases [case_names even odd]: |
27 |
assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P" |
|
28 |
assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P" |
|
29 |
shows P |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
30 |
using assms by (cases "even a") |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
31 |
(simp_all add: even_iff_mod_2_eq_zero [symmetric] odd_iff_mod_2_eq_one [symmetric]) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
32 |
|
71181 | 33 |
lemma odd_of_bool_self [simp]: |
34 |
\<open>odd (of_bool p) \<longleftrightarrow> p\<close> |
|
35 |
by (cases p) simp_all |
|
36 |
||
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
37 |
lemma not_mod_2_eq_0_eq_1 [simp]: |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
38 |
"a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
39 |
by (cases a rule: parity_cases) simp_all |
66815 | 40 |
|
41 |
lemma not_mod_2_eq_1_eq_0 [simp]: |
|
42 |
"a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0" |
|
43 |
by (cases a rule: parity_cases) simp_all |
|
44 |
||
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
45 |
lemma mod2_eq_if: "a mod 2 = (if 2 dvd a then 0 else 1)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
46 |
by (simp add: even_iff_mod_2_eq_zero odd_iff_mod_2_eq_one) |
58787 | 47 |
|
58690 | 48 |
lemma evenE [elim?]: |
49 |
assumes "even a" |
|
50 |
obtains b where "a = 2 * b" |
|
58740 | 51 |
using assms by (rule dvdE) |
58690 | 52 |
|
58681 | 53 |
lemma oddE [elim?]: |
58680 | 54 |
assumes "odd a" |
55 |
obtains b where "a = 2 * b + 1" |
|
58787 | 56 |
proof - |
66815 | 57 |
have "a = 2 * (a div 2) + a mod 2" |
58 |
by (simp add: mult_div_mod_eq) |
|
59 |
with assms have "a = 2 * (a div 2) + 1" |
|
60 |
by (simp add: odd_iff_mod_2_eq_one) |
|
61 |
then show ?thesis .. |
|
62 |
qed |
|
63 |
||
64 |
lemma mod_2_eq_odd: |
|
65 |
"a mod 2 = of_bool (odd a)" |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
66 |
by (auto elim: oddE simp add: even_iff_mod_2_eq_zero) |
66815 | 67 |
|
67816 | 68 |
lemma of_bool_odd_eq_mod_2: |
69 |
"of_bool (odd a) = a mod 2" |
|
70 |
by (simp add: mod_2_eq_odd) |
|
71 |
||
66815 | 72 |
lemma even_zero [simp]: |
73 |
"even 0" |
|
74 |
by (fact dvd_0_right) |
|
75 |
||
76 |
lemma odd_even_add: |
|
77 |
"even (a + b)" if "odd a" and "odd b" |
|
78 |
proof - |
|
79 |
from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1" |
|
80 |
by (blast elim: oddE) |
|
81 |
then have "a + b = 2 * c + 2 * d + (1 + 1)" |
|
82 |
by (simp only: ac_simps) |
|
83 |
also have "\<dots> = 2 * (c + d + 1)" |
|
84 |
by (simp add: algebra_simps) |
|
85 |
finally show ?thesis .. |
|
86 |
qed |
|
87 |
||
88 |
lemma even_add [simp]: |
|
89 |
"even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)" |
|
90 |
by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add) |
|
91 |
||
92 |
lemma odd_add [simp]: |
|
93 |
"odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)" |
|
94 |
by simp |
|
95 |
||
96 |
lemma even_plus_one_iff [simp]: |
|
97 |
"even (a + 1) \<longleftrightarrow> odd a" |
|
98 |
by (auto simp add: dvd_add_right_iff intro: odd_even_add) |
|
99 |
||
100 |
lemma even_mult_iff [simp]: |
|
101 |
"even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q") |
|
102 |
proof |
|
103 |
assume ?Q |
|
104 |
then show ?P |
|
105 |
by auto |
|
106 |
next |
|
107 |
assume ?P |
|
108 |
show ?Q |
|
109 |
proof (rule ccontr) |
|
110 |
assume "\<not> (even a \<or> even b)" |
|
111 |
then have "odd a" and "odd b" |
|
112 |
by auto |
|
113 |
then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1" |
|
114 |
by (blast elim: oddE) |
|
115 |
then have "a * b = (2 * r + 1) * (2 * s + 1)" |
|
116 |
by simp |
|
117 |
also have "\<dots> = 2 * (2 * r * s + r + s) + 1" |
|
118 |
by (simp add: algebra_simps) |
|
119 |
finally have "odd (a * b)" |
|
120 |
by simp |
|
121 |
with \<open>?P\<close> show False |
|
122 |
by auto |
|
123 |
qed |
|
124 |
qed |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
125 |
|
63654 | 126 |
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
127 |
proof - |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
128 |
have "even (2 * numeral n)" |
66815 | 129 |
unfolding even_mult_iff by simp |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
130 |
then have "even (numeral n + numeral n)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
131 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
132 |
then show ?thesis |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
133 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
134 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
135 |
|
63654 | 136 |
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
137 |
proof |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
138 |
assume "even (numeral (num.Bit1 n))" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
139 |
then have "even (numeral n + numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
140 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
141 |
then have "even (2 * numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
142 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
143 |
then have "2 dvd numeral n * 2 + 1" |
58740 | 144 |
by (simp add: ac_simps) |
63654 | 145 |
then have "2 dvd 1" |
146 |
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
|
147 |
then show False by simp |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
148 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
149 |
|
63654 | 150 |
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0" |
58680 | 151 |
by (induct n) auto |
152 |
||
71412 | 153 |
lemma mask_eq_sum_exp: |
154 |
\<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close> |
|
155 |
proof - |
|
156 |
have *: \<open>{q. q < Suc m} = insert m {q. q < m}\<close> for m |
|
157 |
by auto |
|
158 |
have \<open>2 ^ n = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1\<close> |
|
159 |
by (induction n) (simp_all add: ac_simps mult_2 *) |
|
160 |
then have \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1 - 1\<close> |
|
161 |
by simp |
|
162 |
then show ?thesis |
|
163 |
by simp |
|
164 |
qed |
|
165 |
||
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
166 |
end |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
167 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
168 |
class ring_parity = ring + semiring_parity |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
169 |
begin |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
170 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
171 |
subclass comm_ring_1 .. |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
172 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
173 |
lemma even_minus: |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
174 |
"even (- a) \<longleftrightarrow> even a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
175 |
by (fact dvd_minus_iff) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
176 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
177 |
lemma even_diff [simp]: |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
178 |
"even (a - b) \<longleftrightarrow> even (a + b)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
179 |
using even_add [of a "- b"] by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
180 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
181 |
end |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
182 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
183 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
184 |
subsection \<open>Special case: euclidean rings containing the natural numbers\<close> |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
185 |
|
71157 | 186 |
context unique_euclidean_semiring_with_nat |
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
187 |
begin |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
188 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
189 |
subclass semiring_parity |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
190 |
proof |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
191 |
show "2 dvd a \<longleftrightarrow> a mod 2 = 0" for a |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
192 |
by (fact dvd_eq_mod_eq_0) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
193 |
show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" for a |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
194 |
proof |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
195 |
assume "a mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
196 |
then show "\<not> 2 dvd a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
197 |
by auto |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
198 |
next |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
199 |
assume "\<not> 2 dvd a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
200 |
have eucl: "euclidean_size (a mod 2) = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
201 |
proof (rule order_antisym) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
202 |
show "euclidean_size (a mod 2) \<le> 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
203 |
using mod_size_less [of 2 a] by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
204 |
show "1 \<le> euclidean_size (a mod 2)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
205 |
using \<open>\<not> 2 dvd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
206 |
qed |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
207 |
from \<open>\<not> 2 dvd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
208 |
by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
209 |
then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
210 |
by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
211 |
then have "\<not> 2 dvd euclidean_size a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
212 |
using of_nat_dvd_iff [of 2] by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
213 |
then have "euclidean_size a mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
214 |
by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
215 |
then have "of_nat (euclidean_size a mod 2) = of_nat 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
216 |
by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
217 |
then have "of_nat (euclidean_size a) mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
218 |
by (simp add: of_nat_mod) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
219 |
from \<open>\<not> 2 dvd a\<close> eucl |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
220 |
show "a mod 2 = 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
221 |
by (auto intro: division_segment_eq_iff simp add: division_segment_mod) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
222 |
qed |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
223 |
show "\<not> is_unit 2" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
224 |
proof (rule notI) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
225 |
assume "is_unit 2" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
226 |
then have "of_nat 2 dvd of_nat 1" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
227 |
by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
228 |
then have "is_unit (2::nat)" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
229 |
by (simp only: of_nat_dvd_iff) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
230 |
then show False |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
231 |
by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
232 |
qed |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
233 |
qed |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
234 |
|
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
235 |
lemma even_of_nat [simp]: |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
236 |
"even (of_nat a) \<longleftrightarrow> even a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
237 |
proof - |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
238 |
have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
239 |
by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
240 |
also have "\<dots> \<longleftrightarrow> even a" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
241 |
by (simp only: of_nat_dvd_iff) |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
242 |
finally show ?thesis . |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
243 |
qed |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
244 |
|
66815 | 245 |
lemma even_succ_div_two [simp]: |
246 |
"even a \<Longrightarrow> (a + 1) div 2 = a div 2" |
|
247 |
by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero) |
|
248 |
||
249 |
lemma odd_succ_div_two [simp]: |
|
250 |
"odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1" |
|
251 |
by (auto elim!: oddE simp add: add.assoc) |
|
252 |
||
253 |
lemma even_two_times_div_two: |
|
254 |
"even a \<Longrightarrow> 2 * (a div 2) = a" |
|
255 |
by (fact dvd_mult_div_cancel) |
|
256 |
||
257 |
lemma odd_two_times_div_two_succ [simp]: |
|
258 |
"odd a \<Longrightarrow> 2 * (a div 2) + 1 = a" |
|
259 |
using mult_div_mod_eq [of 2 a] |
|
260 |
by (simp add: even_iff_mod_2_eq_zero) |
|
261 |
||
67051 | 262 |
lemma coprime_left_2_iff_odd [simp]: |
263 |
"coprime 2 a \<longleftrightarrow> odd a" |
|
264 |
proof |
|
265 |
assume "odd a" |
|
266 |
show "coprime 2 a" |
|
267 |
proof (rule coprimeI) |
|
268 |
fix b |
|
269 |
assume "b dvd 2" "b dvd a" |
|
270 |
then have "b dvd a mod 2" |
|
271 |
by (auto intro: dvd_mod) |
|
272 |
with \<open>odd a\<close> show "is_unit b" |
|
273 |
by (simp add: mod_2_eq_odd) |
|
274 |
qed |
|
275 |
next |
|
276 |
assume "coprime 2 a" |
|
277 |
show "odd a" |
|
278 |
proof (rule notI) |
|
279 |
assume "even a" |
|
280 |
then obtain b where "a = 2 * b" .. |
|
281 |
with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)" |
|
282 |
by simp |
|
283 |
moreover have "\<not> coprime 2 (2 * b)" |
|
284 |
by (rule not_coprimeI [of 2]) simp_all |
|
285 |
ultimately show False |
|
286 |
by blast |
|
287 |
qed |
|
288 |
qed |
|
289 |
||
290 |
lemma coprime_right_2_iff_odd [simp]: |
|
291 |
"coprime a 2 \<longleftrightarrow> odd a" |
|
292 |
using coprime_left_2_iff_odd [of a] by (simp add: ac_simps) |
|
293 |
||
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
294 |
end |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
295 |
|
71157 | 296 |
context unique_euclidean_ring_with_nat |
58679 | 297 |
begin |
298 |
||
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
299 |
subclass ring_parity .. |
58680 | 300 |
|
67906 | 301 |
lemma minus_1_mod_2_eq [simp]: |
302 |
"- 1 mod 2 = 1" |
|
303 |
by (simp add: mod_2_eq_odd) |
|
304 |
||
305 |
lemma minus_1_div_2_eq [simp]: |
|
306 |
"- 1 div 2 = - 1" |
|
307 |
proof - |
|
308 |
from div_mult_mod_eq [of "- 1" 2] |
|
309 |
have "- 1 div 2 * 2 = - 1 * 2" |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
310 |
using add_implies_diff by fastforce |
67906 | 311 |
then show ?thesis |
312 |
using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp |
|
313 |
qed |
|
314 |
||
58679 | 315 |
end |
316 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
317 |
|
69593 | 318 |
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
319 |
|
70340 | 320 |
instance nat :: unique_euclidean_semiring_with_nat |
66815 | 321 |
by standard (simp_all add: dvd_eq_mod_eq_0) |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66582
diff
changeset
|
322 |
|
66815 | 323 |
lemma even_Suc_Suc_iff [simp]: |
324 |
"even (Suc (Suc n)) \<longleftrightarrow> even n" |
|
58787 | 325 |
using dvd_add_triv_right_iff [of 2 n] by simp |
58687 | 326 |
|
66815 | 327 |
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n" |
328 |
using even_plus_one_iff [of n] by simp |
|
58787 | 329 |
|
66815 | 330 |
lemma even_diff_nat [simp]: |
331 |
"even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat |
|
58787 | 332 |
proof (cases "n \<le> m") |
333 |
case True |
|
334 |
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right) |
|
66815 | 335 |
moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp |
336 |
ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:) |
|
58787 | 337 |
then show ?thesis by auto |
338 |
next |
|
339 |
case False |
|
340 |
then show ?thesis by simp |
|
63654 | 341 |
qed |
342 |
||
66815 | 343 |
lemma odd_pos: |
344 |
"odd n \<Longrightarrow> 0 < n" for n :: nat |
|
58690 | 345 |
by (auto elim: oddE) |
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
346 |
|
66815 | 347 |
lemma Suc_double_not_eq_double: |
348 |
"Suc (2 * m) \<noteq> 2 * n" |
|
62597 | 349 |
proof |
350 |
assume "Suc (2 * m) = 2 * n" |
|
351 |
moreover have "odd (Suc (2 * m))" and "even (2 * n)" |
|
352 |
by simp_all |
|
353 |
ultimately show False by simp |
|
354 |
qed |
|
355 |
||
66815 | 356 |
lemma double_not_eq_Suc_double: |
357 |
"2 * m \<noteq> Suc (2 * n)" |
|
62597 | 358 |
using Suc_double_not_eq_double [of n m] by simp |
359 |
||
66815 | 360 |
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n" |
361 |
by (auto elim: oddE) |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
362 |
|
66815 | 363 |
lemma even_Suc_div_two [simp]: |
364 |
"even n \<Longrightarrow> Suc n div 2 = n div 2" |
|
365 |
using even_succ_div_two [of n] by simp |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
366 |
|
66815 | 367 |
lemma odd_Suc_div_two [simp]: |
368 |
"odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)" |
|
369 |
using odd_succ_div_two [of n] by simp |
|
60343
063698416239
correct sort constraints for abbreviations in type classes
haftmann
parents:
59816
diff
changeset
|
370 |
|
66815 | 371 |
lemma odd_two_times_div_two_nat [simp]: |
372 |
assumes "odd n" |
|
373 |
shows "2 * (n div 2) = n - (1 :: nat)" |
|
374 |
proof - |
|
375 |
from assms have "2 * (n div 2) + 1 = n" |
|
376 |
by (rule odd_two_times_div_two_succ) |
|
377 |
then have "Suc (2 * (n div 2)) - 1 = n - 1" |
|
58787 | 378 |
by simp |
66815 | 379 |
then show ?thesis |
380 |
by simp |
|
58787 | 381 |
qed |
58680 | 382 |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
383 |
lemma not_mod2_eq_Suc_0_eq_0 [simp]: |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
384 |
"n mod 2 \<noteq> Suc 0 \<longleftrightarrow> n mod 2 = 0" |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
385 |
using not_mod_2_eq_1_eq_0 [of n] by simp |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
386 |
|
69502 | 387 |
lemma odd_card_imp_not_empty: |
388 |
\<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close> |
|
389 |
using that by auto |
|
390 |
||
70365
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
391 |
lemma nat_induct2 [case_names 0 1 step]: |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
392 |
assumes "P 0" "P 1" and step: "\<And>n::nat. P n \<Longrightarrow> P (n + 2)" |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
393 |
shows "P n" |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
394 |
proof (induct n rule: less_induct) |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
395 |
case (less n) |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
396 |
show ?case |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
397 |
proof (cases "n < Suc (Suc 0)") |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
398 |
case True |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
399 |
then show ?thesis |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
400 |
using assms by (auto simp: less_Suc_eq) |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
401 |
next |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
402 |
case False |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
403 |
then obtain k where k: "n = Suc (Suc k)" |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
404 |
by (force simp: not_less nat_le_iff_add) |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
405 |
then have "k<n" |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
406 |
by simp |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
407 |
with less assms have "P (k+2)" |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
408 |
by blast |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
409 |
then show ?thesis |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
410 |
by (simp add: k) |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
411 |
qed |
4df0628e8545
a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents:
70353
diff
changeset
|
412 |
qed |
58687 | 413 |
|
71412 | 414 |
context semiring_parity |
415 |
begin |
|
416 |
||
417 |
lemma even_sum_iff: |
|
418 |
\<open>even (sum f A) \<longleftrightarrow> even (card {a\<in>A. odd (f a)})\<close> if \<open>finite A\<close> |
|
419 |
using that proof (induction A) |
|
420 |
case empty |
|
421 |
then show ?case |
|
422 |
by simp |
|
423 |
next |
|
424 |
case (insert a A) |
|
425 |
moreover have \<open>{b \<in> insert a A. odd (f b)} = (if odd (f a) then {a} else {}) \<union> {b \<in> A. odd (f b)}\<close> |
|
426 |
by auto |
|
427 |
ultimately show ?case |
|
428 |
by simp |
|
429 |
qed |
|
430 |
||
431 |
lemma even_prod_iff: |
|
432 |
\<open>even (prod f A) \<longleftrightarrow> (\<exists>a\<in>A. even (f a))\<close> if \<open>finite A\<close> |
|
433 |
using that by (induction A) simp_all |
|
434 |
||
435 |
lemma even_mask_iff [simp]: |
|
436 |
\<open>even (2 ^ n - 1) \<longleftrightarrow> n = 0\<close> |
|
437 |
proof (cases \<open>n = 0\<close>) |
|
438 |
case True |
|
439 |
then show ?thesis |
|
440 |
by simp |
|
441 |
next |
|
442 |
case False |
|
443 |
then have \<open>{a. a = 0 \<and> a < n} = {0}\<close> |
|
444 |
by auto |
|
445 |
then show ?thesis |
|
446 |
by (auto simp add: mask_eq_sum_exp even_sum_iff) |
|
447 |
qed |
|
448 |
||
449 |
end |
|
450 |
||
71138 | 451 |
|
60758 | 452 |
subsection \<open>Parity and powers\<close> |
58689 | 453 |
|
61531
ab2e862263e7
Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents:
60867
diff
changeset
|
454 |
context ring_1 |
58689 | 455 |
begin |
456 |
||
63654 | 457 |
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n" |
58690 | 458 |
by (auto elim: evenE) |
58689 | 459 |
|
63654 | 460 |
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)" |
58690 | 461 |
by (auto elim: oddE) |
462 |
||
66815 | 463 |
lemma uminus_power_if: |
464 |
"(- a) ^ n = (if even n then a ^ n else - (a ^ n))" |
|
465 |
by auto |
|
466 |
||
63654 | 467 |
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1" |
58690 | 468 |
by simp |
58689 | 469 |
|
63654 | 470 |
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1" |
58690 | 471 |
by simp |
58689 | 472 |
|
66582 | 473 |
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)" |
474 |
by (cases "even (n + k)") auto |
|
475 |
||
67371
2d9cf74943e1
moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents:
67083
diff
changeset
|
476 |
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
|
477 |
by (induct n) auto |
2d9cf74943e1
moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents:
67083
diff
changeset
|
478 |
|
63654 | 479 |
end |
58689 | 480 |
|
481 |
context linordered_idom |
|
482 |
begin |
|
483 |
||
63654 | 484 |
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n" |
58690 | 485 |
by (auto elim: evenE) |
58689 | 486 |
|
63654 | 487 |
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a" |
58689 | 488 |
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE) |
489 |
||
63654 | 490 |
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a" |
58787 | 491 |
by (auto simp add: zero_le_even_power zero_le_odd_power) |
63654 | 492 |
|
493 |
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 | 494 |
proof - |
495 |
have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0" |
|
58787 | 496 |
unfolding power_eq_0_iff [of a n, symmetric] by blast |
58689 | 497 |
show ?thesis |
63654 | 498 |
unfolding less_le zero_le_power_eq by auto |
58689 | 499 |
qed |
500 |
||
63654 | 501 |
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0" |
58689 | 502 |
unfolding not_le [symmetric] zero_le_power_eq by auto |
503 |
||
63654 | 504 |
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)" |
505 |
unfolding not_less [symmetric] zero_less_power_eq by auto |
|
506 |
||
507 |
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n" |
|
58689 | 508 |
using power_abs [of a n] by (simp add: zero_le_even_power) |
509 |
||
510 |
lemma power_mono_even: |
|
511 |
assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>" |
|
512 |
shows "a ^ n \<le> b ^ n" |
|
513 |
proof - |
|
514 |
have "0 \<le> \<bar>a\<bar>" by auto |
|
63654 | 515 |
with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" |
516 |
by (rule power_mono) |
|
517 |
with \<open>even n\<close> show ?thesis |
|
518 |
by (simp add: power_even_abs) |
|
58689 | 519 |
qed |
520 |
||
521 |
lemma power_mono_odd: |
|
522 |
assumes "odd n" and "a \<le> b" |
|
523 |
shows "a ^ n \<le> b ^ n" |
|
524 |
proof (cases "b < 0") |
|
63654 | 525 |
case True |
526 |
with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto |
|
527 |
then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono) |
|
60758 | 528 |
with \<open>odd n\<close> show ?thesis by simp |
58689 | 529 |
next |
63654 | 530 |
case False |
531 |
then have "0 \<le> b" by auto |
|
58689 | 532 |
show ?thesis |
533 |
proof (cases "a < 0") |
|
63654 | 534 |
case True |
535 |
then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto |
|
60758 | 536 |
then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto |
63654 | 537 |
moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto |
58689 | 538 |
ultimately show ?thesis by auto |
539 |
next |
|
63654 | 540 |
case False |
541 |
then have "0 \<le> a" by auto |
|
542 |
with \<open>a \<le> b\<close> show ?thesis |
|
543 |
using power_mono by auto |
|
58689 | 544 |
qed |
545 |
qed |
|
62083 | 546 |
|
60758 | 547 |
text \<open>Simplify, when the exponent is a numeral\<close> |
58689 | 548 |
|
549 |
lemma zero_le_power_eq_numeral [simp]: |
|
550 |
"0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a" |
|
551 |
by (fact zero_le_power_eq) |
|
552 |
||
553 |
lemma zero_less_power_eq_numeral [simp]: |
|
63654 | 554 |
"0 < a ^ numeral w \<longleftrightarrow> |
555 |
numeral w = (0 :: nat) \<or> |
|
556 |
even (numeral w :: nat) \<and> a \<noteq> 0 \<or> |
|
557 |
odd (numeral w :: nat) \<and> 0 < a" |
|
58689 | 558 |
by (fact zero_less_power_eq) |
559 |
||
560 |
lemma power_le_zero_eq_numeral [simp]: |
|
63654 | 561 |
"a ^ numeral w \<le> 0 \<longleftrightarrow> |
562 |
(0 :: nat) < numeral w \<and> |
|
563 |
(odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)" |
|
58689 | 564 |
by (fact power_le_zero_eq) |
565 |
||
566 |
lemma power_less_zero_eq_numeral [simp]: |
|
567 |
"a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0" |
|
568 |
by (fact power_less_zero_eq) |
|
569 |
||
570 |
lemma power_even_abs_numeral [simp]: |
|
571 |
"even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w" |
|
572 |
by (fact power_even_abs) |
|
573 |
||
574 |
end |
|
575 |
||
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
576 |
|
69593 | 577 |
subsection \<open>Instance for \<^typ>\<open>int\<close>\<close> |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
578 |
|
67816 | 579 |
lemma even_diff_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
580 |
"even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 581 |
by (fact even_diff) |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
582 |
|
67816 | 583 |
lemma even_abs_add_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
584 |
"even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 585 |
by simp |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
586 |
|
67816 | 587 |
lemma even_add_abs_iff: |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
588 |
"even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int |
67816 | 589 |
by simp |
66816
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
590 |
|
212a3334e7da
more fundamental definition of div and mod on int
haftmann
parents:
66815
diff
changeset
|
591 |
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
|
592 |
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
|
593 |
|
71138 | 594 |
lemma zdiv_zmult2_eq: |
595 |
\<open>a div (b * c) = (a div b) div c\<close> if \<open>c \<ge> 0\<close> for a b c :: int |
|
596 |
proof (cases \<open>b \<ge> 0\<close>) |
|
597 |
case True |
|
598 |
with that show ?thesis |
|
599 |
using div_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp |
|
600 |
next |
|
601 |
case False |
|
602 |
with that show ?thesis |
|
603 |
using div_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp |
|
604 |
qed |
|
605 |
||
606 |
lemma zmod_zmult2_eq: |
|
607 |
\<open>a mod (b * c) = b * (a div b mod c) + a mod b\<close> if \<open>c \<ge> 0\<close> for a b c :: int |
|
608 |
proof (cases \<open>b \<ge> 0\<close>) |
|
609 |
case True |
|
610 |
with that show ?thesis |
|
611 |
using mod_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp |
|
612 |
next |
|
613 |
case False |
|
614 |
with that show ?thesis |
|
615 |
using mod_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp |
|
616 |
qed |
|
617 |
||
71094 | 618 |
|
71181 | 619 |
subsection \<open>Abstract bit structures\<close> |
71094 | 620 |
|
621 |
class semiring_bits = semiring_parity + |
|
71195 | 622 |
assumes bits_induct [case_names stable rec]: |
71094 | 623 |
\<open>(\<And>a. a div 2 = a \<Longrightarrow> P a) |
624 |
\<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)) |
|
625 |
\<Longrightarrow> P a\<close> |
|
71138 | 626 |
assumes bits_div_0 [simp]: \<open>0 div a = 0\<close> |
627 |
and bits_div_by_1 [simp]: \<open>a div 1 = a\<close> |
|
71195 | 628 |
and bits_mod_div_trivial [simp]: \<open>a mod b div b = 0\<close> |
71138 | 629 |
and even_succ_div_2 [simp]: \<open>even a \<Longrightarrow> (1 + a) div 2 = a div 2\<close> |
71182 | 630 |
and exp_div_exp_eq: \<open>2 ^ m div 2 ^ n = of_bool (2 ^ m \<noteq> 0 \<and> m \<ge> n) * 2 ^ (m - n)\<close> |
71138 | 631 |
and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close> |
632 |
and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close> |
|
633 |
and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close> |
|
634 |
and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
635 |
begin |
|
636 |
||
71195 | 637 |
lemma bits_div_by_0 [simp]: |
638 |
\<open>a div 0 = 0\<close> |
|
639 |
by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero) |
|
640 |
||
71138 | 641 |
lemma bits_1_div_2 [simp]: |
642 |
\<open>1 div 2 = 0\<close> |
|
643 |
using even_succ_div_2 [of 0] by simp |
|
644 |
||
645 |
lemma bits_1_div_exp [simp]: |
|
646 |
\<open>1 div 2 ^ n = of_bool (n = 0)\<close> |
|
647 |
using div_exp_eq [of 1 1] by (cases n) simp_all |
|
648 |
||
649 |
lemma even_succ_div_exp [simp]: |
|
650 |
\<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
651 |
proof (cases n) |
|
652 |
case 0 |
|
653 |
with that show ?thesis |
|
654 |
by simp |
|
655 |
next |
|
656 |
case (Suc n) |
|
657 |
with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close> |
|
658 |
proof (induction n) |
|
659 |
case 0 |
|
660 |
then show ?case |
|
661 |
by simp |
|
662 |
next |
|
663 |
case (Suc n) |
|
664 |
then show ?case |
|
665 |
using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric] |
|
666 |
by simp |
|
667 |
qed |
|
668 |
with Suc show ?thesis |
|
669 |
by simp |
|
670 |
qed |
|
671 |
||
672 |
lemma even_succ_mod_exp [simp]: |
|
673 |
\<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
674 |
using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] that |
|
675 |
apply simp |
|
676 |
by (metis local.add.left_commute local.add_left_cancel local.div_mult_mod_eq) |
|
677 |
||
678 |
lemma bits_mod_by_1 [simp]: |
|
679 |
\<open>a mod 1 = 0\<close> |
|
680 |
using div_mult_mod_eq [of a 1] by simp |
|
681 |
||
682 |
lemma bits_mod_0 [simp]: |
|
683 |
\<open>0 mod a = 0\<close> |
|
684 |
using div_mult_mod_eq [of 0 a] by simp |
|
685 |
||
71195 | 686 |
lemma bits_one_mod_two_eq_one [simp]: |
71138 | 687 |
\<open>1 mod 2 = 1\<close> |
688 |
by (simp add: mod2_eq_if) |
|
689 |
||
71181 | 690 |
definition bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close> |
691 |
where \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> |
|
692 |
||
693 |
lemma bit_0 [simp]: |
|
694 |
\<open>bit a 0 \<longleftrightarrow> odd a\<close> |
|
695 |
by (simp add: bit_def) |
|
696 |
||
697 |
lemma bit_Suc [simp]: |
|
698 |
\<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close> |
|
699 |
using div_exp_eq [of a 1 n] by (simp add: bit_def) |
|
700 |
||
71195 | 701 |
lemma bit_0_eq [simp]: |
702 |
\<open>bit 0 = bot\<close> |
|
703 |
by (simp add: fun_eq_iff bit_def) |
|
704 |
||
71181 | 705 |
context |
706 |
fixes a |
|
707 |
assumes stable: \<open>a div 2 = a\<close> |
|
708 |
begin |
|
709 |
||
71195 | 710 |
lemma bits_stable_imp_add_self: |
71181 | 711 |
\<open>a + a mod 2 = 0\<close> |
712 |
proof - |
|
713 |
have \<open>a div 2 * 2 + a mod 2 = a\<close> |
|
714 |
by (fact div_mult_mod_eq) |
|
715 |
then have \<open>a * 2 + a mod 2 = a\<close> |
|
716 |
by (simp add: stable) |
|
717 |
then show ?thesis |
|
718 |
by (simp add: mult_2_right ac_simps) |
|
719 |
qed |
|
720 |
||
721 |
lemma stable_imp_bit_iff_odd: |
|
722 |
\<open>bit a n \<longleftrightarrow> odd a\<close> |
|
723 |
by (induction n) (simp_all add: stable) |
|
724 |
||
725 |
end |
|
726 |
||
727 |
lemma bit_iff_idd_imp_stable: |
|
728 |
\<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close> |
|
71195 | 729 |
using that proof (induction a rule: bits_induct) |
71181 | 730 |
case (stable a) |
731 |
then show ?case |
|
732 |
by simp |
|
733 |
next |
|
734 |
case (rec a b) |
|
735 |
from rec.prems [of 1] have [simp]: \<open>b = odd a\<close> |
|
736 |
by (simp add: rec.hyps) |
|
737 |
from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close> |
|
738 |
by simp |
|
739 |
have \<open>bit a n \<longleftrightarrow> odd a\<close> for n |
|
740 |
using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp) |
|
741 |
then have \<open>a div 2 = a\<close> |
|
742 |
by (rule rec.IH) |
|
743 |
then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close> |
|
744 |
by (simp add: ac_simps) |
|
745 |
also have \<open>\<dots> = a\<close> |
|
746 |
using mult_div_mod_eq [of 2 a] |
|
747 |
by (simp add: of_bool_odd_eq_mod_2) |
|
748 |
finally show ?case |
|
749 |
using \<open>a div 2 = a\<close> by (simp add: hyp) |
|
750 |
qed |
|
751 |
||
752 |
lemma bit_eqI: |
|
753 |
\<open>a = b\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> bit b n\<close> |
|
71195 | 754 |
using that proof (induction a arbitrary: b rule: bits_induct) |
71181 | 755 |
case (stable a) |
756 |
from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close> |
|
757 |
by simp |
|
758 |
have \<open>b div 2 = b\<close> |
|
759 |
proof (rule bit_iff_idd_imp_stable) |
|
760 |
fix n |
|
761 |
from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close> |
|
762 |
by simp |
|
763 |
also have \<open>bit a n \<longleftrightarrow> odd a\<close> |
|
764 |
using stable by (simp add: stable_imp_bit_iff_odd) |
|
765 |
finally show \<open>bit b n \<longleftrightarrow> odd b\<close> |
|
766 |
by (simp add: **) |
|
767 |
qed |
|
768 |
from ** have \<open>a mod 2 = b mod 2\<close> |
|
769 |
by (simp add: mod2_eq_if) |
|
770 |
then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close> |
|
771 |
by simp |
|
772 |
then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close> |
|
773 |
by (simp add: ac_simps) |
|
774 |
with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case |
|
71195 | 775 |
by (simp add: bits_stable_imp_add_self) |
71181 | 776 |
next |
777 |
case (rec a p) |
|
778 |
from rec.prems [of 0] have [simp]: \<open>p = odd b\<close> |
|
779 |
by simp |
|
780 |
from rec.hyps have \<open>bit a n \<longleftrightarrow> bit (b div 2) n\<close> for n |
|
781 |
using rec.prems [of \<open>Suc n\<close>] by simp |
|
782 |
then have \<open>a = b div 2\<close> |
|
783 |
by (rule rec.IH) |
|
784 |
then have \<open>2 * a = 2 * (b div 2)\<close> |
|
785 |
by simp |
|
786 |
then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close> |
|
787 |
by simp |
|
788 |
also have \<open>\<dots> = b\<close> |
|
789 |
by (fact mod_mult_div_eq) |
|
790 |
finally show ?case |
|
791 |
by (auto simp add: mod2_eq_if) |
|
792 |
qed |
|
793 |
||
794 |
lemma bit_eq_iff: |
|
795 |
\<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close> |
|
796 |
by (auto intro: bit_eqI) |
|
797 |
||
798 |
lemma bit_eq_rec: |
|
799 |
\<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> |
|
800 |
apply (simp add: bit_eq_iff) |
|
801 |
apply auto |
|
802 |
using bit_0 apply blast |
|
803 |
using bit_0 apply blast |
|
804 |
using bit_Suc apply blast |
|
805 |
using bit_Suc apply blast |
|
806 |
apply (metis bit_eq_iff local.even_iff_mod_2_eq_zero local.mod_div_mult_eq) |
|
807 |
apply (metis bit_eq_iff local.even_iff_mod_2_eq_zero local.mod_div_mult_eq) |
|
808 |
apply (metis bit_eq_iff local.mod2_eq_if local.mod_div_mult_eq) |
|
809 |
apply (metis bit_eq_iff local.mod2_eq_if local.mod_div_mult_eq) |
|
810 |
done |
|
811 |
||
71182 | 812 |
lemma bit_exp_iff: |
813 |
\<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close> |
|
814 |
by (auto simp add: bit_def exp_div_exp_eq) |
|
815 |
||
71408 | 816 |
lemma bit_1_iff: |
817 |
\<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close> |
|
818 |
using bit_exp_iff [of 0 n] by simp |
|
819 |
||
820 |
lemma bit_2_iff: |
|
821 |
\<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close> |
|
822 |
using bit_exp_iff [of 1 n] by auto |
|
823 |
||
71138 | 824 |
end |
71094 | 825 |
|
826 |
lemma nat_bit_induct [case_names zero even odd]: |
|
827 |
"P n" if zero: "P 0" |
|
828 |
and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)" |
|
829 |
and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))" |
|
830 |
proof (induction n rule: less_induct) |
|
831 |
case (less n) |
|
832 |
show "P n" |
|
833 |
proof (cases "n = 0") |
|
834 |
case True with zero show ?thesis by simp |
|
835 |
next |
|
836 |
case False |
|
837 |
with less have hyp: "P (n div 2)" by simp |
|
838 |
show ?thesis |
|
839 |
proof (cases "even n") |
|
840 |
case True |
|
841 |
then have "n \<noteq> 1" |
|
842 |
by auto |
|
843 |
with \<open>n \<noteq> 0\<close> have "n div 2 > 0" |
|
844 |
by simp |
|
845 |
with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis |
|
846 |
by simp |
|
847 |
next |
|
848 |
case False |
|
849 |
with hyp odd [of "n div 2"] show ?thesis |
|
850 |
by simp |
|
851 |
qed |
|
852 |
qed |
|
853 |
qed |
|
854 |
||
855 |
instance nat :: semiring_bits |
|
856 |
proof |
|
857 |
show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close> |
|
858 |
and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close> |
|
859 |
for P and n :: nat |
|
860 |
proof (induction n rule: nat_bit_induct) |
|
861 |
case zero |
|
862 |
from stable [of 0] show ?case |
|
863 |
by simp |
|
864 |
next |
|
865 |
case (even n) |
|
866 |
with rec [of n False] show ?case |
|
867 |
by simp |
|
868 |
next |
|
869 |
case (odd n) |
|
870 |
with rec [of n True] show ?case |
|
871 |
by simp |
|
872 |
qed |
|
71138 | 873 |
show \<open>q mod 2 ^ m mod 2 ^ n = q mod 2 ^ min m n\<close> |
874 |
for q m n :: nat |
|
875 |
apply (auto simp add: less_iff_Suc_add power_add mod_mod_cancel split: split_min_lin) |
|
876 |
apply (metis div_mult2_eq mod_div_trivial mod_eq_self_iff_div_eq_0 mod_mult_self2_is_0 power_commutes) |
|
877 |
done |
|
878 |
show \<open>(q * 2 ^ m) mod (2 ^ n) = (q mod 2 ^ (n - m)) * 2 ^ m\<close> if \<open>m \<le> n\<close> |
|
879 |
for q m n :: nat |
|
880 |
using that |
|
881 |
apply (auto simp add: mod_mod_cancel div_mult2_eq power_add mod_mult2_eq le_iff_add split: split_min_lin) |
|
882 |
apply (simp add: mult.commute) |
|
883 |
done |
|
71182 | 884 |
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff) |
71094 | 885 |
|
70353 | 886 |
lemma int_bit_induct [case_names zero minus even odd]: |
70338 | 887 |
"P k" if zero_int: "P 0" |
888 |
and minus_int: "P (- 1)" |
|
889 |
and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)" |
|
890 |
and odd_int: "\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))" for k :: int |
|
891 |
proof (cases "k \<ge> 0") |
|
892 |
case True |
|
893 |
define n where "n = nat k" |
|
894 |
with True have "k = int n" |
|
895 |
by simp |
|
896 |
then show "P k" |
|
70353 | 897 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
70338 | 898 |
case zero |
899 |
then show ?case |
|
900 |
by (simp add: zero_int) |
|
901 |
next |
|
902 |
case (even n) |
|
903 |
have "P (int n * 2)" |
|
904 |
by (rule even_int) (use even in simp_all) |
|
905 |
with even show ?case |
|
906 |
by (simp add: ac_simps) |
|
907 |
next |
|
908 |
case (odd n) |
|
909 |
have "P (1 + (int n * 2))" |
|
910 |
by (rule odd_int) (use odd in simp_all) |
|
911 |
with odd show ?case |
|
912 |
by (simp add: ac_simps) |
|
913 |
qed |
|
914 |
next |
|
915 |
case False |
|
916 |
define n where "n = nat (- k - 1)" |
|
917 |
with False have "k = - int n - 1" |
|
918 |
by simp |
|
919 |
then show "P k" |
|
70353 | 920 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
70338 | 921 |
case zero |
922 |
then show ?case |
|
923 |
by (simp add: minus_int) |
|
924 |
next |
|
925 |
case (even n) |
|
926 |
have "P (1 + (- int (Suc n) * 2))" |
|
927 |
by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>) |
|
928 |
also have "\<dots> = - int (2 * n) - 1" |
|
929 |
by (simp add: algebra_simps) |
|
930 |
finally show ?case |
|
931 |
using even by simp |
|
932 |
next |
|
933 |
case (odd n) |
|
934 |
have "P (- int (Suc n) * 2)" |
|
935 |
by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>) |
|
936 |
also have "\<dots> = - int (Suc (2 * n)) - 1" |
|
937 |
by (simp add: algebra_simps) |
|
938 |
finally show ?case |
|
939 |
using odd by simp |
|
940 |
qed |
|
941 |
qed |
|
942 |
||
71094 | 943 |
instance int :: semiring_bits |
944 |
proof |
|
945 |
show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close> |
|
946 |
and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close> |
|
947 |
for P and k :: int |
|
948 |
proof (induction k rule: int_bit_induct) |
|
949 |
case zero |
|
950 |
from stable [of 0] show ?case |
|
951 |
by simp |
|
952 |
next |
|
953 |
case minus |
|
954 |
from stable [of \<open>- 1\<close>] show ?case |
|
955 |
by simp |
|
956 |
next |
|
957 |
case (even k) |
|
958 |
with rec [of k False] show ?case |
|
959 |
by (simp add: ac_simps) |
|
960 |
next |
|
961 |
case (odd k) |
|
962 |
with rec [of k True] show ?case |
|
963 |
by (simp add: ac_simps) |
|
964 |
qed |
|
71182 | 965 |
show \<open>(2::int) ^ m div 2 ^ n = of_bool ((2::int) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close> |
966 |
for m n :: nat |
|
967 |
proof (cases \<open>m < n\<close>) |
|
968 |
case True |
|
969 |
then have \<open>n = m + (n - m)\<close> |
|
970 |
by simp |
|
971 |
then have \<open>(2::int) ^ m div 2 ^ n = (2::int) ^ m div 2 ^ (m + (n - m))\<close> |
|
972 |
by simp |
|
973 |
also have \<open>\<dots> = (2::int) ^ m div (2 ^ m * 2 ^ (n - m))\<close> |
|
974 |
by (simp add: power_add) |
|
975 |
also have \<open>\<dots> = (2::int) ^ m div 2 ^ m div 2 ^ (n - m)\<close> |
|
976 |
by (simp add: zdiv_zmult2_eq) |
|
977 |
finally show ?thesis using \<open>m < n\<close> by simp |
|
978 |
next |
|
979 |
case False |
|
980 |
then show ?thesis |
|
981 |
by (simp add: power_diff) |
|
982 |
qed |
|
71138 | 983 |
show \<open>k mod 2 ^ m mod 2 ^ n = k mod 2 ^ min m n\<close> |
984 |
for m n :: nat and k :: int |
|
985 |
using mod_exp_eq [of \<open>nat k\<close> m n] |
|
986 |
apply (auto simp add: mod_mod_cancel zdiv_zmult2_eq power_add zmod_zmult2_eq le_iff_add split: split_min_lin) |
|
987 |
apply (auto simp add: less_iff_Suc_add mod_mod_cancel power_add) |
|
988 |
apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>]) |
|
989 |
apply (subst zmod_zmult2_eq) apply simp_all |
|
990 |
done |
|
991 |
show \<open>(k * 2 ^ m) mod (2 ^ n) = (k mod 2 ^ (n - m)) * 2 ^ m\<close> |
|
992 |
if \<open>m \<le> n\<close> for m n :: nat and k :: int |
|
993 |
using that |
|
994 |
apply (auto simp add: power_add zmod_zmult2_eq le_iff_add split: split_min_lin) |
|
995 |
apply (simp add: ac_simps) |
|
996 |
done |
|
71182 | 997 |
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le) |
67816 | 998 |
|
71094 | 999 |
class semiring_bit_shifts = semiring_bits + |
1000 |
fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
1001 |
assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close> |
|
1002 |
fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
1003 |
assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close> |
|
67816 | 1004 |
begin |
1005 |
||
71094 | 1006 |
definition take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
1007 |
where take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close> |
|
67816 | 1008 |
|
71094 | 1009 |
text \<open> |
1010 |
Logically, \<^const>\<open>push_bit\<close>, |
|
1011 |
\<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them |
|
1012 |
as separate operations makes proofs easier, otherwise proof automation |
|
1013 |
would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic |
|
1014 |
algebraic relationships between those operations. |
|
1015 |
Having |
|
1016 |
\<^const>\<open>push_bit\<close> and \<^const>\<open>drop_bit\<close> as definitional class operations |
|
1017 |
takes into account that specific instances of these can be implemented |
|
1018 |
differently wrt. code generation. |
|
1019 |
\<close> |
|
67816 | 1020 |
|
71408 | 1021 |
lemma bit_iff_odd_drop_bit: |
1022 |
\<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close> |
|
1023 |
by (simp add: bit_def drop_bit_eq_div) |
|
1024 |
||
1025 |
lemma even_drop_bit_iff_not_bit: |
|
1026 |
\<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close> |
|
1027 |
by (simp add: bit_iff_odd_drop_bit) |
|
1028 |
||
71195 | 1029 |
lemma bits_ident: |
71138 | 1030 |
"push_bit n (drop_bit n a) + take_bit n a = a" |
1031 |
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div) |
|
1032 |
||
1033 |
lemma push_bit_push_bit [simp]: |
|
1034 |
"push_bit m (push_bit n a) = push_bit (m + n) a" |
|
1035 |
by (simp add: push_bit_eq_mult power_add ac_simps) |
|
1036 |
||
1037 |
lemma push_bit_0_id [simp]: |
|
1038 |
"push_bit 0 = id" |
|
1039 |
by (simp add: fun_eq_iff push_bit_eq_mult) |
|
1040 |
||
1041 |
lemma push_bit_of_0 [simp]: |
|
1042 |
"push_bit n 0 = 0" |
|
1043 |
by (simp add: push_bit_eq_mult) |
|
1044 |
||
1045 |
lemma push_bit_of_1: |
|
1046 |
"push_bit n 1 = 2 ^ n" |
|
1047 |
by (simp add: push_bit_eq_mult) |
|
1048 |
||
1049 |
lemma push_bit_Suc [simp]: |
|
1050 |
"push_bit (Suc n) a = push_bit n (a * 2)" |
|
1051 |
by (simp add: push_bit_eq_mult ac_simps) |
|
1052 |
||
1053 |
lemma push_bit_double: |
|
1054 |
"push_bit n (a * 2) = push_bit n a * 2" |
|
1055 |
by (simp add: push_bit_eq_mult ac_simps) |
|
1056 |
||
1057 |
lemma push_bit_add: |
|
1058 |
"push_bit n (a + b) = push_bit n a + push_bit n b" |
|
1059 |
by (simp add: push_bit_eq_mult algebra_simps) |
|
1060 |
||
1061 |
lemma take_bit_0 [simp]: |
|
1062 |
"take_bit 0 a = 0" |
|
1063 |
by (simp add: take_bit_eq_mod) |
|
1064 |
||
1065 |
lemma take_bit_Suc [simp]: |
|
1066 |
\<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + of_bool (odd a)\<close> |
|
1067 |
proof - |
|
1068 |
have \<open>take_bit (Suc n) (a div 2 * 2 + of_bool (odd a)) = take_bit n (a div 2) * 2 + of_bool (odd a)\<close> |
|
1069 |
using even_succ_mod_exp [of \<open>2 * (a div 2)\<close> \<open>Suc n\<close>] |
|
1070 |
mult_exp_mod_exp_eq [of 1 \<open>Suc n\<close> \<open>a div 2\<close>] |
|
1071 |
by (auto simp add: take_bit_eq_mod ac_simps) |
|
1072 |
then show ?thesis |
|
1073 |
using div_mult_mod_eq [of a 2] by (simp add: mod_2_eq_odd) |
|
1074 |
qed |
|
1075 |
||
1076 |
lemma take_bit_of_0 [simp]: |
|
1077 |
"take_bit n 0 = 0" |
|
1078 |
by (simp add: take_bit_eq_mod) |
|
1079 |
||
1080 |
lemma take_bit_of_1 [simp]: |
|
1081 |
"take_bit n 1 = of_bool (n > 0)" |
|
1082 |
by (cases n) simp_all |
|
1083 |
||
1084 |
lemma drop_bit_of_0 [simp]: |
|
1085 |
"drop_bit n 0 = 0" |
|
1086 |
by (simp add: drop_bit_eq_div) |
|
1087 |
||
1088 |
lemma drop_bit_of_1 [simp]: |
|
1089 |
"drop_bit n 1 = of_bool (n = 0)" |
|
1090 |
by (simp add: drop_bit_eq_div) |
|
1091 |
||
1092 |
lemma drop_bit_0 [simp]: |
|
1093 |
"drop_bit 0 = id" |
|
1094 |
by (simp add: fun_eq_iff drop_bit_eq_div) |
|
1095 |
||
1096 |
lemma drop_bit_Suc [simp]: |
|
1097 |
"drop_bit (Suc n) a = drop_bit n (a div 2)" |
|
1098 |
using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div) |
|
1099 |
||
1100 |
lemma drop_bit_half: |
|
1101 |
"drop_bit n (a div 2) = drop_bit n a div 2" |
|
1102 |
by (induction n arbitrary: a) simp_all |
|
1103 |
||
1104 |
lemma drop_bit_of_bool [simp]: |
|
1105 |
"drop_bit n (of_bool d) = of_bool (n = 0 \<and> d)" |
|
1106 |
by (cases n) simp_all |
|
1107 |
||
1108 |
lemma take_bit_eq_0_imp_dvd: |
|
1109 |
"take_bit n a = 0 \<Longrightarrow> 2 ^ n dvd a" |
|
1110 |
by (simp add: take_bit_eq_mod mod_0_imp_dvd) |
|
1111 |
||
1112 |
lemma even_take_bit_eq [simp]: |
|
1113 |
\<open>even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a\<close> |
|
1114 |
by (cases n) simp_all |
|
1115 |
||
1116 |
lemma take_bit_take_bit [simp]: |
|
1117 |
"take_bit m (take_bit n a) = take_bit (min m n) a" |
|
1118 |
by (simp add: take_bit_eq_mod mod_exp_eq ac_simps) |
|
1119 |
||
1120 |
lemma drop_bit_drop_bit [simp]: |
|
1121 |
"drop_bit m (drop_bit n a) = drop_bit (m + n) a" |
|
1122 |
by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps) |
|
1123 |
||
1124 |
lemma push_bit_take_bit: |
|
1125 |
"push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)" |
|
1126 |
apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps) |
|
1127 |
using mult_exp_mod_exp_eq [of m \<open>m + n\<close> a] apply (simp add: ac_simps power_add) |
|
1128 |
done |
|
1129 |
||
1130 |
lemma take_bit_push_bit: |
|
1131 |
"take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)" |
|
1132 |
proof (cases "m \<le> n") |
|
1133 |
case True |
|
1134 |
then show ?thesis |
|
1135 |
apply (simp add:) |
|
1136 |
apply (simp_all add: push_bit_eq_mult take_bit_eq_mod) |
|
1137 |
apply (auto dest!: le_Suc_ex simp add: power_add ac_simps) |
|
1138 |
using mult_exp_mod_exp_eq [of m m \<open>a * 2 ^ n\<close> for n] |
|
1139 |
apply (simp add: ac_simps) |
|
1140 |
done |
|
1141 |
next |
|
1142 |
case False |
|
1143 |
then show ?thesis |
|
1144 |
using push_bit_take_bit [of n "m - n" a] |
|
1145 |
by simp |
|
1146 |
qed |
|
1147 |
||
1148 |
lemma take_bit_drop_bit: |
|
1149 |
"take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)" |
|
1150 |
by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq) |
|
1151 |
||
1152 |
lemma drop_bit_take_bit: |
|
1153 |
"drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)" |
|
1154 |
proof (cases "m \<le> n") |
|
1155 |
case True |
|
1156 |
then show ?thesis |
|
1157 |
using take_bit_drop_bit [of "n - m" m a] by simp |
|
1158 |
next |
|
1159 |
case False |
|
1160 |
then obtain q where \<open>m = n + q\<close> |
|
1161 |
by (auto simp add: not_le dest: less_imp_Suc_add) |
|
1162 |
then have \<open>drop_bit m (take_bit n a) = 0\<close> |
|
1163 |
using div_exp_eq [of \<open>a mod 2 ^ n\<close> n q] |
|
1164 |
by (simp add: take_bit_eq_mod drop_bit_eq_div) |
|
1165 |
with False show ?thesis |
|
1166 |
by simp |
|
1167 |
qed |
|
1168 |
||
71181 | 1169 |
lemma bit_drop_bit_eq: |
1170 |
\<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close> |
|
1171 |
by (simp add: bit_def fun_eq_iff ac_simps flip: drop_bit_eq_div) |
|
1172 |
||
1173 |
lemma bit_take_bit_iff: |
|
1174 |
\<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close> |
|
1175 |
by (simp add: bit_def drop_bit_take_bit not_le flip: drop_bit_eq_div) |
|
1176 |
||
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
1177 |
end |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
1178 |
|
71094 | 1179 |
instantiation nat :: semiring_bit_shifts |
1180 |
begin |
|
1181 |
||
1182 |
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
1183 |
where \<open>push_bit_nat n m = m * 2 ^ n\<close> |
|
1184 |
||
1185 |
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
1186 |
where \<open>drop_bit_nat n m = m div 2 ^ n\<close> |
|
1187 |
||
1188 |
instance proof |
|
1189 |
show \<open>push_bit n m = m * 2 ^ n\<close> for n m :: nat |
|
1190 |
by (simp add: push_bit_nat_def) |
|
1191 |
show \<open>drop_bit n m = m div 2 ^ n\<close> for n m :: nat |
|
1192 |
by (simp add: drop_bit_nat_def) |
|
1193 |
qed |
|
1194 |
||
1195 |
end |
|
1196 |
||
1197 |
instantiation int :: semiring_bit_shifts |
|
1198 |
begin |
|
1199 |
||
1200 |
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
1201 |
where \<open>push_bit_int n k = k * 2 ^ n\<close> |
|
1202 |
||
1203 |
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
1204 |
where \<open>drop_bit_int n k = k div 2 ^ n\<close> |
|
1205 |
||
1206 |
instance proof |
|
1207 |
show \<open>push_bit n k = k * 2 ^ n\<close> for n :: nat and k :: int |
|
1208 |
by (simp add: push_bit_int_def) |
|
1209 |
show \<open>drop_bit n k = k div 2 ^ n\<close> for n :: nat and k :: int |
|
1210 |
by (simp add: drop_bit_int_def) |
|
1211 |
qed |
|
1212 |
||
1213 |
end |
|
1214 |
||
71412 | 1215 |
lemma bit_push_bit_iff_nat: |
1216 |
\<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat |
|
1217 |
proof (cases \<open>m \<le> n\<close>) |
|
1218 |
case True |
|
1219 |
then obtain r where \<open>n = m + r\<close> |
|
1220 |
using le_Suc_ex by blast |
|
1221 |
with True show ?thesis |
|
1222 |
by (simp add: push_bit_eq_mult bit_def power_add mult.commute [of \<open>2 ^ m\<close>]) |
|
1223 |
next |
|
1224 |
case False |
|
1225 |
then obtain r where \<open>m = Suc (n + r)\<close> |
|
1226 |
using less_imp_Suc_add not_le by blast |
|
1227 |
with False show ?thesis |
|
1228 |
by (simp add: push_bit_eq_mult bit_def power_add mult.left_commute [of _ \<open>2 ^ n\<close>]) |
|
1229 |
qed |
|
1230 |
||
1231 |
lemma bit_push_bit_iff_int: |
|
1232 |
\<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int |
|
1233 |
proof (cases \<open>m \<le> n\<close>) |
|
1234 |
case True |
|
1235 |
then obtain r where \<open>n = m + r\<close> |
|
1236 |
using le_Suc_ex by blast |
|
1237 |
with True show ?thesis |
|
1238 |
by (simp add: push_bit_eq_mult bit_def power_add mult.commute [of \<open>2 ^ m\<close>]) |
|
1239 |
next |
|
1240 |
case False |
|
1241 |
then obtain r where \<open>m = Suc (n + r)\<close> |
|
1242 |
using less_imp_Suc_add not_le by blast |
|
1243 |
with False show ?thesis |
|
1244 |
by (simp add: push_bit_eq_mult bit_def power_add mult.left_commute [of _ \<open>2 ^ n\<close>]) |
|
1245 |
qed |
|
1246 |
||
71094 | 1247 |
class unique_euclidean_semiring_with_bit_shifts = |
1248 |
unique_euclidean_semiring_with_nat + semiring_bit_shifts |
|
70341
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
1249 |
begin |
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents:
70340
diff
changeset
|
1250 |
|
71138 | 1251 |
lemma take_bit_of_exp [simp]: |
1252 |
\<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close> |
|
1253 |
by (simp add: take_bit_eq_mod exp_mod_exp) |
|
67960 | 1254 |
|
71138 | 1255 |
lemma take_bit_of_2 [simp]: |
1256 |
\<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close> |
|
1257 |
using take_bit_of_exp [of n 1] by simp |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1258 |
|
71412 | 1259 |
lemma take_bit_of_mask: |
71408 | 1260 |
\<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close> |
71412 | 1261 |
by (simp add: take_bit_eq_mod mask_mod_exp) |
71408 | 1262 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1263 |
lemma push_bit_eq_0_iff [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1264 |
"push_bit n a = 0 \<longleftrightarrow> a = 0" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1265 |
by (simp add: push_bit_eq_mult) |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1266 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1267 |
lemma push_bit_numeral [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1268 |
"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
|
1269 |
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
|
1270 |
|
68010 | 1271 |
lemma push_bit_of_nat: |
1272 |
"push_bit n (of_nat m) = of_nat (push_bit n m)" |
|
1273 |
by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult) |
|
1274 |
||
67961 | 1275 |
lemma take_bit_add: |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
1276 |
"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
|
1277 |
by (simp add: take_bit_eq_mod mod_simps) |
67816 | 1278 |
|
67961 | 1279 |
lemma take_bit_eq_0_iff: |
1280 |
"take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a" |
|
1281 |
by (simp add: take_bit_eq_mod mod_eq_0_iff_dvd) |
|
1282 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
1283 |
lemma take_bit_of_1_eq_0_iff [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
1284 |
"take_bit n 1 = 0 \<longleftrightarrow> n = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67906
diff
changeset
|
1285 |
by (simp add: take_bit_eq_mod) |
67816 | 1286 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1287 |
lemma take_bit_numeral_bit0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1288 |
"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
|
1289 |
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
|
1290 |
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
|
1291 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1292 |
lemma take_bit_numeral_bit1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1293 |
"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
|
1294 |
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
|
1295 |
ac_simps even_add even_mult_iff div_mult_self1 [OF numeral_neq_zero]) (simp add: ac_simps) |
67961 | 1296 |
|
68010 | 1297 |
lemma take_bit_of_nat: |
1298 |
"take_bit n (of_nat m) = of_nat (take_bit n m)" |
|
1299 |
by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"]) |
|
1300 |
||
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1301 |
lemma drop_bit_numeral_bit0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1302 |
"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
|
1303 |
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
|
1304 |
nonzero_mult_div_cancel_left [OF numeral_neq_zero]) |
67816 | 1305 |
|
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1306 |
lemma drop_bit_numeral_bit1 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1307 |
"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
|
1308 |
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
|
1309 |
div_mult_self4 [OF numeral_neq_zero]) simp |
67816 | 1310 |
|
68010 | 1311 |
lemma drop_bit_of_nat: |
1312 |
"drop_bit n (of_nat m) = of_nat (drop_bit n m)" |
|
68389 | 1313 |
by (simp add: drop_bit_eq_div Parity.drop_bit_eq_div of_nat_div [of m "2 ^ n"]) |
68010 | 1314 |
|
71412 | 1315 |
lemma bit_of_nat_iff_bit [simp]: |
1316 |
\<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
|
1317 |
proof - |
|
1318 |
have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close> |
|
1319 |
by simp |
|
1320 |
also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close> |
|
1321 |
by (simp add: of_nat_div) |
|
1322 |
finally show ?thesis |
|
1323 |
by (simp add: bit_def semiring_bits_class.bit_def) |
|
1324 |
qed |
|
1325 |
||
1326 |
lemma of_nat_push_bit: |
|
1327 |
\<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close> |
|
1328 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
1329 |
||
1330 |
lemma of_nat_drop_bit: |
|
1331 |
\<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close> |
|
1332 |
by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div) |
|
1333 |
||
1334 |
lemma of_nat_take_bit: |
|
1335 |
\<open>of_nat (take_bit m n) = take_bit m (of_nat n)\<close> |
|
1336 |
by (simp add: take_bit_eq_mod semiring_bit_shifts_class.take_bit_eq_mod of_nat_mod) |
|
1337 |
||
1338 |
lemma bit_push_bit_iff_of_nat_iff: |
|
1339 |
\<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close> |
|
1340 |
proof - |
|
1341 |
from bit_push_bit_iff_nat |
|
1342 |
have \<open>bit (of_nat (push_bit m r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close> |
|
1343 |
by simp |
|
1344 |
then show ?thesis |
|
1345 |
by (simp add: of_nat_push_bit) |
|
1346 |
qed |
|
1347 |
||
58770 | 1348 |
end |
67816 | 1349 |
|
71094 | 1350 |
instance nat :: unique_euclidean_semiring_with_bit_shifts .. |
1351 |
||
1352 |
instance int :: unique_euclidean_semiring_with_bit_shifts .. |
|
1353 |
||
67988
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1354 |
lemma push_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1355 |
"push_bit n (Suc 0) = 2 ^ n" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1356 |
using push_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1357 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1358 |
lemma take_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1359 |
"take_bit n (Suc 0) = of_bool (0 < n)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1360 |
using take_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1361 |
|
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1362 |
lemma drop_bit_of_Suc_0 [simp]: |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1363 |
"drop_bit n (Suc 0) = of_bool (n = 0)" |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1364 |
using drop_bit_of_1 [where ?'a = nat] by simp |
01c651412081
explicit simp rules for computing abstract bit operations
haftmann
parents:
67961
diff
changeset
|
1365 |
|
70973 | 1366 |
lemma take_bit_eq_self: |
1367 |
\<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for n m :: nat |
|
1368 |
using that by (simp add: take_bit_eq_mod) |
|
1369 |
||
70911 | 1370 |
lemma push_bit_minus_one: |
1371 |
"push_bit n (- 1 :: int) = - (2 ^ n)" |
|
1372 |
by (simp add: push_bit_eq_mult) |
|
1373 |
||
71195 | 1374 |
lemma minus_1_div_exp_eq_int: |
1375 |
\<open>- 1 div (2 :: int) ^ n = - 1\<close> |
|
1376 |
by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>) |
|
1377 |
||
1378 |
lemma drop_bit_minus_one [simp]: |
|
1379 |
\<open>drop_bit n (- 1 :: int) = - 1\<close> |
|
1380 |
by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int) |
|
1381 |
||
1382 |
lemma take_bit_uminus: |
|
1383 |
"take_bit n (- (take_bit n k)) = take_bit n (- k)" |
|
1384 |
for k :: int |
|
1385 |
by (simp add: take_bit_eq_mod mod_minus_eq) |
|
1386 |
||
1387 |
lemma take_bit_minus: |
|
1388 |
"take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)" |
|
1389 |
for k l :: int |
|
1390 |
by (simp add: take_bit_eq_mod mod_diff_eq) |
|
1391 |
||
1392 |
lemma take_bit_nonnegative [simp]: |
|
1393 |
"take_bit n k \<ge> 0" |
|
1394 |
for k :: int |
|
1395 |
by (simp add: take_bit_eq_mod) |
|
1396 |
||
67816 | 1397 |
end |