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