| author | haftmann | 
| Sat, 20 Jun 2020 05:56:28 +0000 | |
| changeset 71965 | d45f5d4c41bd | 
| parent 71958 | 4320875eb8a1 | 
| child 71966 | e18e9ac8c205 | 
| 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  | 
||
| 
71837
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
677  | 
context  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
678  | 
  assumes "SORT_CONSTRAINT('a::division_ring)"
 | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
679  | 
begin  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
680  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
681  | 
lemma power_int_minus_left:  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
682  | 
"power_int (-a :: 'a) n = (if even n then power_int a n else -power_int a n)"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
683  | 
by (auto simp: power_int_def minus_one_power_iff even_nat_iff)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
684  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
685  | 
lemma power_int_minus_left_even [simp]: "even n \<Longrightarrow> power_int (-a :: 'a) n = power_int a n"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
686  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
687  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
688  | 
lemma power_int_minus_left_odd [simp]: "odd n \<Longrightarrow> power_int (-a :: 'a) n = -power_int a n"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
689  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
690  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
691  | 
lemma power_int_minus_left_distrib:  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
692  | 
"NO_MATCH (-1) x \<Longrightarrow> power_int (-a :: 'a) n = power_int (-1) n * power_int a n"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
693  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
694  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
695  | 
lemma power_int_minus_one_minus: "power_int (-1 :: 'a) (-n) = power_int (-1) n"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
696  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
697  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
698  | 
lemma power_int_minus_one_diff_commute: "power_int (-1 :: 'a) (a - b) = power_int (-1) (b - a)"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
699  | 
by (subst power_int_minus_one_minus [symmetric]) auto  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
700  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
701  | 
lemma power_int_minus_one_mult_self [simp]:  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
702  | 
"power_int (-1 :: 'a) m * power_int (-1) m = 1"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
703  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
704  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
705  | 
lemma power_int_minus_one_mult_self' [simp]:  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
706  | 
"power_int (-1 :: 'a) m * (power_int (-1) m * b) = b"  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
707  | 
by (simp add: power_int_minus_left)  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
708  | 
|
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
709  | 
end  | 
| 
 
dca11678c495
new constant power_int in HOL
 
Manuel Eberl <eberlm@in.tum.de> 
parents: 
71822 
diff
changeset
 | 
710  | 
|
| 71094 | 711  | 
|
| 71181 | 712  | 
subsection \<open>Abstract bit structures\<close>  | 
| 71094 | 713  | 
|
714  | 
class semiring_bits = semiring_parity +  | 
|
| 71195 | 715  | 
assumes bits_induct [case_names stable rec]:  | 
| 71094 | 716  | 
\<open>(\<And>a. a div 2 = a \<Longrightarrow> P a)  | 
717  | 
\<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a))  | 
|
718  | 
\<Longrightarrow> P a\<close>  | 
|
| 71138 | 719  | 
assumes bits_div_0 [simp]: \<open>0 div a = 0\<close>  | 
720  | 
and bits_div_by_1 [simp]: \<open>a div 1 = a\<close>  | 
|
| 71195 | 721  | 
and bits_mod_div_trivial [simp]: \<open>a mod b div b = 0\<close>  | 
| 71138 | 722  | 
and even_succ_div_2 [simp]: \<open>even a \<Longrightarrow> (1 + a) div 2 = a div 2\<close>  | 
| 71413 | 723  | 
and even_mask_div_iff: \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0 \<or> m \<le> n\<close>  | 
| 71182 | 724  | 
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 | 725  | 
and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close>  | 
726  | 
and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close>  | 
|
727  | 
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>  | 
|
728  | 
and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>  | 
|
| 71424 | 729  | 
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>  | 
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
730  | 
fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
731  | 
assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>  | 
| 71138 | 732  | 
begin  | 
733  | 
||
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
734  | 
text \<open>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
735  | 
Having \<^const>\<open>bit\<close> as definitional class operation  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
736  | 
takes into account that specific instances can be implemented  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
737  | 
differently wrt. code generation.  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
738  | 
\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
739  | 
|
| 71195 | 740  | 
lemma bits_div_by_0 [simp]:  | 
741  | 
\<open>a div 0 = 0\<close>  | 
|
742  | 
by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero)  | 
|
743  | 
||
| 71138 | 744  | 
lemma bits_1_div_2 [simp]:  | 
745  | 
\<open>1 div 2 = 0\<close>  | 
|
746  | 
using even_succ_div_2 [of 0] by simp  | 
|
747  | 
||
748  | 
lemma bits_1_div_exp [simp]:  | 
|
749  | 
\<open>1 div 2 ^ n = of_bool (n = 0)\<close>  | 
|
750  | 
using div_exp_eq [of 1 1] by (cases n) simp_all  | 
|
751  | 
||
752  | 
lemma even_succ_div_exp [simp]:  | 
|
753  | 
\<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close>  | 
|
754  | 
proof (cases n)  | 
|
755  | 
case 0  | 
|
756  | 
with that show ?thesis  | 
|
757  | 
by simp  | 
|
758  | 
next  | 
|
759  | 
case (Suc n)  | 
|
760  | 
with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close>  | 
|
761  | 
proof (induction n)  | 
|
762  | 
case 0  | 
|
763  | 
then show ?case  | 
|
764  | 
by simp  | 
|
765  | 
next  | 
|
766  | 
case (Suc n)  | 
|
767  | 
then show ?case  | 
|
768  | 
using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric]  | 
|
769  | 
by simp  | 
|
770  | 
qed  | 
|
771  | 
with Suc show ?thesis  | 
|
772  | 
by simp  | 
|
773  | 
qed  | 
|
774  | 
||
775  | 
lemma even_succ_mod_exp [simp]:  | 
|
776  | 
\<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close>  | 
|
777  | 
using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] that  | 
|
778  | 
apply simp  | 
|
779  | 
by (metis local.add.left_commute local.add_left_cancel local.div_mult_mod_eq)  | 
|
780  | 
||
781  | 
lemma bits_mod_by_1 [simp]:  | 
|
782  | 
\<open>a mod 1 = 0\<close>  | 
|
783  | 
using div_mult_mod_eq [of a 1] by simp  | 
|
784  | 
||
785  | 
lemma bits_mod_0 [simp]:  | 
|
786  | 
\<open>0 mod a = 0\<close>  | 
|
787  | 
using div_mult_mod_eq [of 0 a] by simp  | 
|
788  | 
||
| 71195 | 789  | 
lemma bits_one_mod_two_eq_one [simp]:  | 
| 71138 | 790  | 
\<open>1 mod 2 = 1\<close>  | 
791  | 
by (simp add: mod2_eq_if)  | 
|
792  | 
||
| 71181 | 793  | 
lemma bit_0 [simp]:  | 
794  | 
\<open>bit a 0 \<longleftrightarrow> odd a\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
795  | 
by (simp add: bit_iff_odd)  | 
| 71181 | 796  | 
|
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
797  | 
lemma bit_Suc:  | 
| 71181 | 798  | 
\<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>  | 
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
799  | 
using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd)  | 
| 71181 | 800  | 
|
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
801  | 
lemma bit_rec:  | 
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
802  | 
\<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
 | 
803  | 
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
 | 
804  | 
|
| 71195 | 805  | 
lemma bit_0_eq [simp]:  | 
806  | 
\<open>bit 0 = bot\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
807  | 
by (simp add: fun_eq_iff bit_iff_odd)  | 
| 71195 | 808  | 
|
| 71181 | 809  | 
context  | 
810  | 
fixes a  | 
|
811  | 
assumes stable: \<open>a div 2 = a\<close>  | 
|
812  | 
begin  | 
|
813  | 
||
| 71195 | 814  | 
lemma bits_stable_imp_add_self:  | 
| 71181 | 815  | 
\<open>a + a mod 2 = 0\<close>  | 
816  | 
proof -  | 
|
817  | 
have \<open>a div 2 * 2 + a mod 2 = a\<close>  | 
|
818  | 
by (fact div_mult_mod_eq)  | 
|
819  | 
then have \<open>a * 2 + a mod 2 = a\<close>  | 
|
820  | 
by (simp add: stable)  | 
|
821  | 
then show ?thesis  | 
|
822  | 
by (simp add: mult_2_right ac_simps)  | 
|
823  | 
qed  | 
|
824  | 
||
825  | 
lemma stable_imp_bit_iff_odd:  | 
|
826  | 
\<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
 | 
827  | 
by (induction n) (simp_all add: stable bit_Suc)  | 
| 71181 | 828  | 
|
829  | 
end  | 
|
830  | 
||
831  | 
lemma bit_iff_idd_imp_stable:  | 
|
832  | 
\<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close>  | 
|
| 71195 | 833  | 
using that proof (induction a rule: bits_induct)  | 
| 71181 | 834  | 
case (stable a)  | 
835  | 
then show ?case  | 
|
836  | 
by simp  | 
|
837  | 
next  | 
|
838  | 
case (rec a b)  | 
|
839  | 
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
 | 
840  | 
by (simp add: rec.hyps bit_Suc)  | 
| 71181 | 841  | 
from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close>  | 
842  | 
by simp  | 
|
843  | 
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
 | 
844  | 
using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp bit_Suc)  | 
| 71181 | 845  | 
then have \<open>a div 2 = a\<close>  | 
846  | 
by (rule rec.IH)  | 
|
847  | 
then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close>  | 
|
848  | 
by (simp add: ac_simps)  | 
|
849  | 
also have \<open>\<dots> = a\<close>  | 
|
850  | 
using mult_div_mod_eq [of 2 a]  | 
|
851  | 
by (simp add: of_bool_odd_eq_mod_2)  | 
|
852  | 
finally show ?case  | 
|
853  | 
using \<open>a div 2 = a\<close> by (simp add: hyp)  | 
|
854  | 
qed  | 
|
855  | 
||
| 71418 | 856  | 
lemma exp_eq_0_imp_not_bit:  | 
857  | 
\<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
858  | 
using that by (simp add: bit_iff_odd)  | 
| 71418 | 859  | 
|
| 71181 | 860  | 
lemma bit_eqI:  | 
| 71418 | 861  | 
\<open>a = b\<close> if \<open>\<And>n. 2 ^ n \<noteq> 0 \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>  | 
862  | 
proof -  | 
|
863  | 
have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n  | 
|
864  | 
proof (cases \<open>2 ^ n = 0\<close>)  | 
|
865  | 
case True  | 
|
866  | 
then show ?thesis  | 
|
867  | 
by (simp add: exp_eq_0_imp_not_bit)  | 
|
868  | 
next  | 
|
869  | 
case False  | 
|
870  | 
then show ?thesis  | 
|
871  | 
by (rule that)  | 
|
| 71181 | 872  | 
qed  | 
| 71418 | 873  | 
then show ?thesis proof (induction a arbitrary: b rule: bits_induct)  | 
874  | 
case (stable a)  | 
|
875  | 
from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close>  | 
|
876  | 
by simp  | 
|
877  | 
have \<open>b div 2 = b\<close>  | 
|
878  | 
proof (rule bit_iff_idd_imp_stable)  | 
|
879  | 
fix n  | 
|
880  | 
from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close>  | 
|
881  | 
by simp  | 
|
882  | 
also have \<open>bit a n \<longleftrightarrow> odd a\<close>  | 
|
883  | 
using stable by (simp add: stable_imp_bit_iff_odd)  | 
|
884  | 
finally show \<open>bit b n \<longleftrightarrow> odd b\<close>  | 
|
885  | 
by (simp add: **)  | 
|
886  | 
qed  | 
|
887  | 
from ** have \<open>a mod 2 = b mod 2\<close>  | 
|
888  | 
by (simp add: mod2_eq_if)  | 
|
889  | 
then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close>  | 
|
890  | 
by simp  | 
|
891  | 
then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close>  | 
|
892  | 
by (simp add: ac_simps)  | 
|
893  | 
with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case  | 
|
894  | 
by (simp add: bits_stable_imp_add_self)  | 
|
895  | 
next  | 
|
896  | 
case (rec a p)  | 
|
897  | 
from rec.prems [of 0] have [simp]: \<open>p = odd b\<close>  | 
|
898  | 
by simp  | 
|
899  | 
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
 | 
900  | 
using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc)  | 
| 71418 | 901  | 
then have \<open>a = b div 2\<close>  | 
902  | 
by (rule rec.IH)  | 
|
903  | 
then have \<open>2 * a = 2 * (b div 2)\<close>  | 
|
904  | 
by simp  | 
|
905  | 
then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close>  | 
|
906  | 
by simp  | 
|
907  | 
also have \<open>\<dots> = b\<close>  | 
|
908  | 
by (fact mod_mult_div_eq)  | 
|
909  | 
finally show ?case  | 
|
910  | 
by (auto simp add: mod2_eq_if)  | 
|
911  | 
qed  | 
|
| 71181 | 912  | 
qed  | 
913  | 
||
914  | 
lemma bit_eq_iff:  | 
|
915  | 
\<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close>  | 
|
916  | 
by (auto intro: bit_eqI)  | 
|
917  | 
||
| 71182 | 918  | 
lemma bit_exp_iff:  | 
919  | 
\<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
920  | 
by (auto simp add: bit_iff_odd exp_div_exp_eq)  | 
| 71182 | 921  | 
|
| 71408 | 922  | 
lemma bit_1_iff:  | 
923  | 
\<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>  | 
|
924  | 
using bit_exp_iff [of 0 n] by simp  | 
|
925  | 
||
926  | 
lemma bit_2_iff:  | 
|
927  | 
\<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>  | 
|
928  | 
using bit_exp_iff [of 1 n] by auto  | 
|
929  | 
||
| 71418 | 930  | 
lemma even_bit_succ_iff:  | 
931  | 
\<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
932  | 
using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd)  | 
| 71418 | 933  | 
|
934  | 
lemma odd_bit_iff_bit_pred:  | 
|
935  | 
\<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close>  | 
|
936  | 
proof -  | 
|
937  | 
from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..  | 
|
938  | 
moreover have \<open>bit (2 * b) n \<or> n = 0 \<longleftrightarrow> bit (1 + 2 * b) n\<close>  | 
|
939  | 
using even_bit_succ_iff by simp  | 
|
940  | 
ultimately show ?thesis by (simp add: ac_simps)  | 
|
941  | 
qed  | 
|
942  | 
||
| 71426 | 943  | 
lemma bit_double_iff:  | 
944  | 
\<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> 2 ^ n \<noteq> 0\<close>  | 
|
945  | 
using even_mult_exp_div_exp_iff [of a 1 n]  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
946  | 
by (cases n, auto simp add: bit_iff_odd ac_simps)  | 
| 71426 | 947  | 
|
948  | 
lemma bit_eq_rec:  | 
|
| 71441 | 949  | 
\<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>)  | 
950  | 
proof  | 
|
951  | 
assume ?P  | 
|
952  | 
then show ?Q  | 
|
953  | 
by simp  | 
|
954  | 
next  | 
|
955  | 
assume ?Q  | 
|
956  | 
then have \<open>even a \<longleftrightarrow> even b\<close> and \<open>a div 2 = b div 2\<close>  | 
|
957  | 
by simp_all  | 
|
958  | 
show ?P  | 
|
959  | 
proof (rule bit_eqI)  | 
|
960  | 
fix n  | 
|
961  | 
show \<open>bit a n \<longleftrightarrow> bit b n\<close>  | 
|
962  | 
proof (cases n)  | 
|
963  | 
case 0  | 
|
964  | 
with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis  | 
|
965  | 
by simp  | 
|
966  | 
next  | 
|
967  | 
case (Suc n)  | 
|
968  | 
moreover from \<open>a div 2 = b div 2\<close> have \<open>bit (a div 2) n = bit (b div 2) n\<close>  | 
|
969  | 
by simp  | 
|
970  | 
ultimately show ?thesis  | 
|
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
971  | 
by (simp add: bit_Suc)  | 
| 71441 | 972  | 
qed  | 
973  | 
qed  | 
|
974  | 
qed  | 
|
| 71426 | 975  | 
|
| 71822 | 976  | 
lemma bit_mod_2_iff [simp]:  | 
977  | 
\<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
978  | 
by (cases a rule: parity_cases) (simp_all add: bit_iff_odd)  | 
| 71822 | 979  | 
|
| 71418 | 980  | 
lemma bit_mask_iff:  | 
981  | 
\<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
982  | 
by (simp add: bit_iff_odd even_mask_div_iff not_le)  | 
| 71418 | 983  | 
|
| 71757 | 984  | 
lemma bit_Numeral1_iff [simp]:  | 
985  | 
\<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>  | 
|
986  | 
by (simp add: bit_rec)  | 
|
987  | 
||
| 71138 | 988  | 
end  | 
| 71094 | 989  | 
|
990  | 
lemma nat_bit_induct [case_names zero even odd]:  | 
|
991  | 
"P n" if zero: "P 0"  | 
|
992  | 
and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"  | 
|
993  | 
and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"  | 
|
994  | 
proof (induction n rule: less_induct)  | 
|
995  | 
case (less n)  | 
|
996  | 
show "P n"  | 
|
997  | 
proof (cases "n = 0")  | 
|
998  | 
case True with zero show ?thesis by simp  | 
|
999  | 
next  | 
|
1000  | 
case False  | 
|
1001  | 
with less have hyp: "P (n div 2)" by simp  | 
|
1002  | 
show ?thesis  | 
|
1003  | 
proof (cases "even n")  | 
|
1004  | 
case True  | 
|
1005  | 
then have "n \<noteq> 1"  | 
|
1006  | 
by auto  | 
|
1007  | 
with \<open>n \<noteq> 0\<close> have "n div 2 > 0"  | 
|
1008  | 
by simp  | 
|
1009  | 
with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis  | 
|
1010  | 
by simp  | 
|
1011  | 
next  | 
|
1012  | 
case False  | 
|
1013  | 
with hyp odd [of "n div 2"] show ?thesis  | 
|
1014  | 
by simp  | 
|
1015  | 
qed  | 
|
1016  | 
qed  | 
|
1017  | 
qed  | 
|
1018  | 
||
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1019  | 
instantiation nat :: semiring_bits  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1020  | 
begin  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1021  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1022  | 
definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1023  | 
where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1024  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1025  | 
instance  | 
| 71094 | 1026  | 
proof  | 
1027  | 
show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close>  | 
|
1028  | 
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>  | 
|
1029  | 
for P and n :: nat  | 
|
1030  | 
proof (induction n rule: nat_bit_induct)  | 
|
1031  | 
case zero  | 
|
1032  | 
from stable [of 0] show ?case  | 
|
1033  | 
by simp  | 
|
1034  | 
next  | 
|
1035  | 
case (even n)  | 
|
1036  | 
with rec [of n False] show ?case  | 
|
1037  | 
by simp  | 
|
1038  | 
next  | 
|
1039  | 
case (odd n)  | 
|
1040  | 
with rec [of n True] show ?case  | 
|
1041  | 
by simp  | 
|
1042  | 
qed  | 
|
| 71138 | 1043  | 
show \<open>q mod 2 ^ m mod 2 ^ n = q mod 2 ^ min m n\<close>  | 
1044  | 
for q m n :: nat  | 
|
1045  | 
apply (auto simp add: less_iff_Suc_add power_add mod_mod_cancel split: split_min_lin)  | 
|
1046  | 
apply (metis div_mult2_eq mod_div_trivial mod_eq_self_iff_div_eq_0 mod_mult_self2_is_0 power_commutes)  | 
|
1047  | 
done  | 
|
1048  | 
show \<open>(q * 2 ^ m) mod (2 ^ n) = (q mod 2 ^ (n - m)) * 2 ^ m\<close> if \<open>m \<le> n\<close>  | 
|
1049  | 
for q m n :: nat  | 
|
1050  | 
using that  | 
|
1051  | 
apply (auto simp add: mod_mod_cancel div_mult2_eq power_add mod_mult2_eq le_iff_add split: split_min_lin)  | 
|
1052  | 
apply (simp add: mult.commute)  | 
|
1053  | 
done  | 
|
| 71413 | 1054  | 
show \<open>even ((2 ^ m - (1::nat)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::nat) \<or> m \<le> n\<close>  | 
1055  | 
for m n :: nat  | 
|
1056  | 
using even_mask_div_iff' [where ?'a = nat, of m n] by simp  | 
|
| 71424 | 1057  | 
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>  | 
1058  | 
for m n q r :: nat  | 
|
1059  | 
apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)  | 
|
1060  | 
apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc)  | 
|
1061  | 
done  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1062  | 
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff bit_nat_def)  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1063  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1064  | 
end  | 
| 71094 | 1065  | 
|
| 70353 | 1066  | 
lemma int_bit_induct [case_names zero minus even odd]:  | 
| 70338 | 1067  | 
"P k" if zero_int: "P 0"  | 
1068  | 
and minus_int: "P (- 1)"  | 
|
1069  | 
and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)"  | 
|
1070  | 
and odd_int: "\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))" for k :: int  | 
|
1071  | 
proof (cases "k \<ge> 0")  | 
|
1072  | 
case True  | 
|
1073  | 
define n where "n = nat k"  | 
|
1074  | 
with True have "k = int n"  | 
|
1075  | 
by simp  | 
|
1076  | 
then show "P k"  | 
|
| 70353 | 1077  | 
proof (induction n arbitrary: k rule: nat_bit_induct)  | 
| 70338 | 1078  | 
case zero  | 
1079  | 
then show ?case  | 
|
1080  | 
by (simp add: zero_int)  | 
|
1081  | 
next  | 
|
1082  | 
case (even n)  | 
|
1083  | 
have "P (int n * 2)"  | 
|
1084  | 
by (rule even_int) (use even in simp_all)  | 
|
1085  | 
with even show ?case  | 
|
1086  | 
by (simp add: ac_simps)  | 
|
1087  | 
next  | 
|
1088  | 
case (odd n)  | 
|
1089  | 
have "P (1 + (int n * 2))"  | 
|
1090  | 
by (rule odd_int) (use odd in simp_all)  | 
|
1091  | 
with odd show ?case  | 
|
1092  | 
by (simp add: ac_simps)  | 
|
1093  | 
qed  | 
|
1094  | 
next  | 
|
1095  | 
case False  | 
|
1096  | 
define n where "n = nat (- k - 1)"  | 
|
1097  | 
with False have "k = - int n - 1"  | 
|
1098  | 
by simp  | 
|
1099  | 
then show "P k"  | 
|
| 70353 | 1100  | 
proof (induction n arbitrary: k rule: nat_bit_induct)  | 
| 70338 | 1101  | 
case zero  | 
1102  | 
then show ?case  | 
|
1103  | 
by (simp add: minus_int)  | 
|
1104  | 
next  | 
|
1105  | 
case (even n)  | 
|
1106  | 
have "P (1 + (- int (Suc n) * 2))"  | 
|
1107  | 
by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>)  | 
|
1108  | 
also have "\<dots> = - int (2 * n) - 1"  | 
|
1109  | 
by (simp add: algebra_simps)  | 
|
1110  | 
finally show ?case  | 
|
1111  | 
using even by simp  | 
|
1112  | 
next  | 
|
1113  | 
case (odd n)  | 
|
1114  | 
have "P (- int (Suc n) * 2)"  | 
|
1115  | 
by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>)  | 
|
1116  | 
also have "\<dots> = - int (Suc (2 * n)) - 1"  | 
|
1117  | 
by (simp add: algebra_simps)  | 
|
1118  | 
finally show ?case  | 
|
1119  | 
using odd by simp  | 
|
1120  | 
qed  | 
|
1121  | 
qed  | 
|
1122  | 
||
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1123  | 
instantiation int :: semiring_bits  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1124  | 
begin  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1125  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1126  | 
definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1127  | 
where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1128  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1129  | 
instance  | 
| 71094 | 1130  | 
proof  | 
1131  | 
show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close>  | 
|
1132  | 
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>  | 
|
1133  | 
for P and k :: int  | 
|
1134  | 
proof (induction k rule: int_bit_induct)  | 
|
1135  | 
case zero  | 
|
1136  | 
from stable [of 0] show ?case  | 
|
1137  | 
by simp  | 
|
1138  | 
next  | 
|
1139  | 
case minus  | 
|
1140  | 
from stable [of \<open>- 1\<close>] show ?case  | 
|
1141  | 
by simp  | 
|
1142  | 
next  | 
|
1143  | 
case (even k)  | 
|
1144  | 
with rec [of k False] show ?case  | 
|
1145  | 
by (simp add: ac_simps)  | 
|
1146  | 
next  | 
|
1147  | 
case (odd k)  | 
|
1148  | 
with rec [of k True] show ?case  | 
|
1149  | 
by (simp add: ac_simps)  | 
|
1150  | 
qed  | 
|
| 71182 | 1151  | 
show \<open>(2::int) ^ m div 2 ^ n = of_bool ((2::int) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close>  | 
1152  | 
for m n :: nat  | 
|
1153  | 
proof (cases \<open>m < n\<close>)  | 
|
1154  | 
case True  | 
|
1155  | 
then have \<open>n = m + (n - m)\<close>  | 
|
1156  | 
by simp  | 
|
1157  | 
then have \<open>(2::int) ^ m div 2 ^ n = (2::int) ^ m div 2 ^ (m + (n - m))\<close>  | 
|
1158  | 
by simp  | 
|
1159  | 
also have \<open>\<dots> = (2::int) ^ m div (2 ^ m * 2 ^ (n - m))\<close>  | 
|
1160  | 
by (simp add: power_add)  | 
|
1161  | 
also have \<open>\<dots> = (2::int) ^ m div 2 ^ m div 2 ^ (n - m)\<close>  | 
|
1162  | 
by (simp add: zdiv_zmult2_eq)  | 
|
1163  | 
finally show ?thesis using \<open>m < n\<close> by simp  | 
|
1164  | 
next  | 
|
1165  | 
case False  | 
|
1166  | 
then show ?thesis  | 
|
1167  | 
by (simp add: power_diff)  | 
|
1168  | 
qed  | 
|
| 71138 | 1169  | 
show \<open>k mod 2 ^ m mod 2 ^ n = k mod 2 ^ min m n\<close>  | 
1170  | 
for m n :: nat and k :: int  | 
|
1171  | 
using mod_exp_eq [of \<open>nat k\<close> m n]  | 
|
1172  | 
apply (auto simp add: mod_mod_cancel zdiv_zmult2_eq power_add zmod_zmult2_eq le_iff_add split: split_min_lin)  | 
|
1173  | 
apply (auto simp add: less_iff_Suc_add mod_mod_cancel power_add)  | 
|
1174  | 
apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>])  | 
|
1175  | 
apply (subst zmod_zmult2_eq) apply simp_all  | 
|
1176  | 
done  | 
|
1177  | 
show \<open>(k * 2 ^ m) mod (2 ^ n) = (k mod 2 ^ (n - m)) * 2 ^ m\<close>  | 
|
1178  | 
if \<open>m \<le> n\<close> for m n :: nat and k :: int  | 
|
1179  | 
using that  | 
|
1180  | 
apply (auto simp add: power_add zmod_zmult2_eq le_iff_add split: split_min_lin)  | 
|
1181  | 
apply (simp add: ac_simps)  | 
|
1182  | 
done  | 
|
| 71413 | 1183  | 
show \<open>even ((2 ^ m - (1::int)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::int) \<or> m \<le> n\<close>  | 
1184  | 
for m n :: nat  | 
|
1185  | 
using even_mask_div_iff' [where ?'a = int, of m n] by simp  | 
|
| 71424 | 1186  | 
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>  | 
1187  | 
for m n :: nat and k l :: int  | 
|
1188  | 
apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)  | 
|
1189  | 
apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2))  | 
|
1190  | 
done  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1191  | 
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le bit_int_def)  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1192  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1193  | 
end  | 
| 67816 | 1194  | 
|
| 71094 | 1195  | 
class semiring_bit_shifts = semiring_bits +  | 
1196  | 
fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>  | 
|
1197  | 
assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close>  | 
|
1198  | 
fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>  | 
|
1199  | 
assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1200  | 
fixes take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1201  | 
assumes take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close>  | 
| 67816 | 1202  | 
begin  | 
1203  | 
||
| 71094 | 1204  | 
text \<open>  | 
1205  | 
Logically, \<^const>\<open>push_bit\<close>,  | 
|
1206  | 
\<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them  | 
|
1207  | 
as separate operations makes proofs easier, otherwise proof automation  | 
|
1208  | 
would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic  | 
|
1209  | 
algebraic relationships between those operations.  | 
|
1210  | 
Having  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1211  | 
them as definitional class operations  | 
| 71094 | 1212  | 
takes into account that specific instances of these can be implemented  | 
1213  | 
differently wrt. code generation.  | 
|
1214  | 
\<close>  | 
|
| 67816 | 1215  | 
|
| 71408 | 1216  | 
lemma bit_iff_odd_drop_bit:  | 
1217  | 
\<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1218  | 
by (simp add: bit_iff_odd drop_bit_eq_div)  | 
| 71408 | 1219  | 
|
1220  | 
lemma even_drop_bit_iff_not_bit:  | 
|
1221  | 
\<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>  | 
|
1222  | 
by (simp add: bit_iff_odd_drop_bit)  | 
|
1223  | 
||
| 71423 | 1224  | 
lemma div_push_bit_of_1_eq_drop_bit:  | 
1225  | 
\<open>a div push_bit n 1 = drop_bit n a\<close>  | 
|
1226  | 
by (simp add: push_bit_eq_mult drop_bit_eq_div)  | 
|
1227  | 
||
| 71195 | 1228  | 
lemma bits_ident:  | 
| 71138 | 1229  | 
"push_bit n (drop_bit n a) + take_bit n a = a"  | 
1230  | 
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div)  | 
|
1231  | 
||
1232  | 
lemma push_bit_push_bit [simp]:  | 
|
1233  | 
"push_bit m (push_bit n a) = push_bit (m + n) a"  | 
|
1234  | 
by (simp add: push_bit_eq_mult power_add ac_simps)  | 
|
1235  | 
||
1236  | 
lemma push_bit_0_id [simp]:  | 
|
1237  | 
"push_bit 0 = id"  | 
|
1238  | 
by (simp add: fun_eq_iff push_bit_eq_mult)  | 
|
1239  | 
||
1240  | 
lemma push_bit_of_0 [simp]:  | 
|
1241  | 
"push_bit n 0 = 0"  | 
|
1242  | 
by (simp add: push_bit_eq_mult)  | 
|
1243  | 
||
1244  | 
lemma push_bit_of_1:  | 
|
1245  | 
"push_bit n 1 = 2 ^ n"  | 
|
1246  | 
by (simp add: push_bit_eq_mult)  | 
|
1247  | 
||
1248  | 
lemma push_bit_Suc [simp]:  | 
|
1249  | 
"push_bit (Suc n) a = push_bit n (a * 2)"  | 
|
1250  | 
by (simp add: push_bit_eq_mult ac_simps)  | 
|
1251  | 
||
1252  | 
lemma push_bit_double:  | 
|
1253  | 
"push_bit n (a * 2) = push_bit n a * 2"  | 
|
1254  | 
by (simp add: push_bit_eq_mult ac_simps)  | 
|
1255  | 
||
1256  | 
lemma push_bit_add:  | 
|
1257  | 
"push_bit n (a + b) = push_bit n a + push_bit n b"  | 
|
1258  | 
by (simp add: push_bit_eq_mult algebra_simps)  | 
|
1259  | 
||
1260  | 
lemma take_bit_0 [simp]:  | 
|
1261  | 
"take_bit 0 a = 0"  | 
|
1262  | 
by (simp add: take_bit_eq_mod)  | 
|
1263  | 
||
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1264  | 
lemma take_bit_Suc:  | 
| 71822 | 1265  | 
\<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + a mod 2\<close>  | 
| 71138 | 1266  | 
proof -  | 
1267  | 
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>  | 
|
1268  | 
using even_succ_mod_exp [of \<open>2 * (a div 2)\<close> \<open>Suc n\<close>]  | 
|
1269  | 
mult_exp_mod_exp_eq [of 1 \<open>Suc n\<close> \<open>a div 2\<close>]  | 
|
1270  | 
by (auto simp add: take_bit_eq_mod ac_simps)  | 
|
1271  | 
then show ?thesis  | 
|
1272  | 
using div_mult_mod_eq [of a 2] by (simp add: mod_2_eq_odd)  | 
|
1273  | 
qed  | 
|
1274  | 
||
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1275  | 
lemma take_bit_rec:  | 
| 71822 | 1276  | 
\<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + a mod 2)\<close>  | 
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1277  | 
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
 | 
1278  | 
|
| 71759 | 1279  | 
lemma take_bit_Suc_0 [simp]:  | 
1280  | 
\<open>take_bit (Suc 0) a = a mod 2\<close>  | 
|
1281  | 
by (simp add: take_bit_eq_mod)  | 
|
1282  | 
||
| 71138 | 1283  | 
lemma take_bit_of_0 [simp]:  | 
1284  | 
"take_bit n 0 = 0"  | 
|
1285  | 
by (simp add: take_bit_eq_mod)  | 
|
1286  | 
||
1287  | 
lemma take_bit_of_1 [simp]:  | 
|
1288  | 
"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
 | 
1289  | 
by (cases n) (simp_all add: take_bit_Suc)  | 
| 71138 | 1290  | 
|
1291  | 
lemma drop_bit_of_0 [simp]:  | 
|
1292  | 
"drop_bit n 0 = 0"  | 
|
1293  | 
by (simp add: drop_bit_eq_div)  | 
|
1294  | 
||
1295  | 
lemma drop_bit_of_1 [simp]:  | 
|
1296  | 
"drop_bit n 1 = of_bool (n = 0)"  | 
|
1297  | 
by (simp add: drop_bit_eq_div)  | 
|
1298  | 
||
1299  | 
lemma drop_bit_0 [simp]:  | 
|
1300  | 
"drop_bit 0 = id"  | 
|
1301  | 
by (simp add: fun_eq_iff drop_bit_eq_div)  | 
|
1302  | 
||
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1303  | 
lemma drop_bit_Suc:  | 
| 71138 | 1304  | 
"drop_bit (Suc n) a = drop_bit n (a div 2)"  | 
1305  | 
using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div)  | 
|
1306  | 
||
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1307  | 
lemma drop_bit_rec:  | 
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1308  | 
"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
 | 
1309  | 
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
 | 
1310  | 
|
| 71138 | 1311  | 
lemma drop_bit_half:  | 
1312  | 
"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
 | 
1313  | 
by (induction n arbitrary: a) (simp_all add: drop_bit_Suc)  | 
| 71138 | 1314  | 
|
1315  | 
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
 | 
1316  | 
"drop_bit n (of_bool b) = of_bool (n = 0 \<and> b)"  | 
| 71138 | 1317  | 
by (cases n) simp_all  | 
1318  | 
||
1319  | 
lemma even_take_bit_eq [simp]:  | 
|
1320  | 
\<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
 | 
1321  | 
by (simp add: take_bit_rec [of n a])  | 
| 71138 | 1322  | 
|
1323  | 
lemma take_bit_take_bit [simp]:  | 
|
1324  | 
"take_bit m (take_bit n a) = take_bit (min m n) a"  | 
|
1325  | 
by (simp add: take_bit_eq_mod mod_exp_eq ac_simps)  | 
|
1326  | 
||
1327  | 
lemma drop_bit_drop_bit [simp]:  | 
|
1328  | 
"drop_bit m (drop_bit n a) = drop_bit (m + n) a"  | 
|
1329  | 
by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps)  | 
|
1330  | 
||
1331  | 
lemma push_bit_take_bit:  | 
|
1332  | 
"push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)"  | 
|
1333  | 
apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps)  | 
|
1334  | 
using mult_exp_mod_exp_eq [of m \<open>m + n\<close> a] apply (simp add: ac_simps power_add)  | 
|
1335  | 
done  | 
|
1336  | 
||
1337  | 
lemma take_bit_push_bit:  | 
|
1338  | 
"take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)"  | 
|
1339  | 
proof (cases "m \<le> n")  | 
|
1340  | 
case True  | 
|
1341  | 
then show ?thesis  | 
|
1342  | 
apply (simp add:)  | 
|
1343  | 
apply (simp_all add: push_bit_eq_mult take_bit_eq_mod)  | 
|
1344  | 
apply (auto dest!: le_Suc_ex simp add: power_add ac_simps)  | 
|
1345  | 
using mult_exp_mod_exp_eq [of m m \<open>a * 2 ^ n\<close> for n]  | 
|
1346  | 
apply (simp add: ac_simps)  | 
|
1347  | 
done  | 
|
1348  | 
next  | 
|
1349  | 
case False  | 
|
1350  | 
then show ?thesis  | 
|
1351  | 
using push_bit_take_bit [of n "m - n" a]  | 
|
1352  | 
by simp  | 
|
1353  | 
qed  | 
|
1354  | 
||
1355  | 
lemma take_bit_drop_bit:  | 
|
1356  | 
"take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)"  | 
|
1357  | 
by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq)  | 
|
1358  | 
||
1359  | 
lemma drop_bit_take_bit:  | 
|
1360  | 
"drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)"  | 
|
1361  | 
proof (cases "m \<le> n")  | 
|
1362  | 
case True  | 
|
1363  | 
then show ?thesis  | 
|
1364  | 
using take_bit_drop_bit [of "n - m" m a] by simp  | 
|
1365  | 
next  | 
|
1366  | 
case False  | 
|
1367  | 
then obtain q where \<open>m = n + q\<close>  | 
|
1368  | 
by (auto simp add: not_le dest: less_imp_Suc_add)  | 
|
1369  | 
then have \<open>drop_bit m (take_bit n a) = 0\<close>  | 
|
1370  | 
using div_exp_eq [of \<open>a mod 2 ^ n\<close> n q]  | 
|
1371  | 
by (simp add: take_bit_eq_mod drop_bit_eq_div)  | 
|
1372  | 
with False show ?thesis  | 
|
1373  | 
by simp  | 
|
1374  | 
qed  | 
|
1375  | 
||
| 71424 | 1376  | 
lemma even_push_bit_iff [simp]:  | 
1377  | 
\<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close>  | 
|
1378  | 
by (simp add: push_bit_eq_mult) auto  | 
|
1379  | 
||
1380  | 
lemma bit_push_bit_iff:  | 
|
1381  | 
\<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>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1382  | 
by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff)  | 
| 71424 | 1383  | 
|
| 71181 | 1384  | 
lemma bit_drop_bit_eq:  | 
1385  | 
\<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1386  | 
by (simp add: bit_iff_odd fun_eq_iff ac_simps flip: drop_bit_eq_div)  | 
| 71181 | 1387  | 
|
1388  | 
lemma bit_take_bit_iff:  | 
|
1389  | 
\<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1390  | 
by (simp add: bit_iff_odd drop_bit_take_bit not_le flip: drop_bit_eq_div)  | 
| 71181 | 1391  | 
|
| 
71535
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1392  | 
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
 | 
1393  | 
\<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
 | 
1394  | 
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
 | 
1395  | 
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
 | 
1396  | 
|
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1397  | 
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
 | 
1398  | 
\<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
 | 
1399  | 
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
 | 
1400  | 
proof (rule bit_eqI)  | 
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1401  | 
fix m  | 
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1402  | 
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
 | 
1403  | 
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
 | 
1404  | 
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
 | 
1405  | 
qed  | 
| 
 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 
haftmann 
parents: 
71441 
diff
changeset
 | 
1406  | 
|
| 71958 | 1407  | 
lemma exp_dvdE:  | 
1408  | 
assumes \<open>2 ^ n dvd a\<close>  | 
|
1409  | 
obtains b where \<open>a = push_bit n b\<close>  | 
|
1410  | 
proof -  | 
|
1411  | 
from assms obtain b where \<open>a = 2 ^ n * b\<close> ..  | 
|
1412  | 
then have \<open>a = push_bit n b\<close>  | 
|
1413  | 
by (simp add: push_bit_eq_mult ac_simps)  | 
|
1414  | 
with that show thesis .  | 
|
1415  | 
qed  | 
|
1416  | 
||
1417  | 
lemma take_bit_eq_0_iff:  | 
|
1418  | 
\<open>take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)  | 
|
1419  | 
proof  | 
|
1420  | 
assume ?P  | 
|
1421  | 
then show ?Q  | 
|
1422  | 
by (simp add: take_bit_eq_mod mod_0_imp_dvd)  | 
|
1423  | 
next  | 
|
1424  | 
assume ?Q  | 
|
1425  | 
then obtain b where \<open>a = push_bit n b\<close>  | 
|
1426  | 
by (rule exp_dvdE)  | 
|
1427  | 
then show ?P  | 
|
1428  | 
by (simp add: take_bit_push_bit)  | 
|
1429  | 
qed  | 
|
1430  | 
||
| 
70341
 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 
haftmann 
parents: 
70340 
diff
changeset
 | 
1431  | 
end  | 
| 
 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 
haftmann 
parents: 
70340 
diff
changeset
 | 
1432  | 
|
| 71094 | 1433  | 
instantiation nat :: semiring_bit_shifts  | 
1434  | 
begin  | 
|
1435  | 
||
1436  | 
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>  | 
|
1437  | 
where \<open>push_bit_nat n m = m * 2 ^ n\<close>  | 
|
1438  | 
||
1439  | 
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>  | 
|
1440  | 
where \<open>drop_bit_nat n m = m div 2 ^ n\<close>  | 
|
1441  | 
||
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1442  | 
definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1443  | 
where \<open>take_bit_nat n m = m mod 2 ^ n\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1444  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1445  | 
instance  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1446  | 
by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def)  | 
| 71094 | 1447  | 
|
1448  | 
end  | 
|
1449  | 
||
1450  | 
instantiation int :: semiring_bit_shifts  | 
|
1451  | 
begin  | 
|
1452  | 
||
1453  | 
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>  | 
|
1454  | 
where \<open>push_bit_int n k = k * 2 ^ n\<close>  | 
|
1455  | 
||
1456  | 
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>  | 
|
1457  | 
where \<open>drop_bit_int n k = k div 2 ^ n\<close>  | 
|
1458  | 
||
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1459  | 
definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1460  | 
where \<open>take_bit_int n k = k mod 2 ^ n\<close>  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1461  | 
|
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1462  | 
instance  | 
| 
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1463  | 
by standard (simp_all add: push_bit_int_def drop_bit_int_def take_bit_int_def)  | 
| 71094 | 1464  | 
|
1465  | 
end  | 
|
1466  | 
||
| 71412 | 1467  | 
lemma bit_push_bit_iff_nat:  | 
1468  | 
\<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat  | 
|
| 71424 | 1469  | 
by (auto simp add: bit_push_bit_iff)  | 
| 71412 | 1470  | 
|
1471  | 
lemma bit_push_bit_iff_int:  | 
|
1472  | 
\<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int  | 
|
| 71424 | 1473  | 
by (auto simp add: bit_push_bit_iff)  | 
| 71412 | 1474  | 
|
| 71094 | 1475  | 
class unique_euclidean_semiring_with_bit_shifts =  | 
1476  | 
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
 | 
1477  | 
begin  | 
| 
 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 
haftmann 
parents: 
70340 
diff
changeset
 | 
1478  | 
|
| 71138 | 1479  | 
lemma take_bit_of_exp [simp]:  | 
1480  | 
\<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close>  | 
|
1481  | 
by (simp add: take_bit_eq_mod exp_mod_exp)  | 
|
| 67960 | 1482  | 
|
| 71138 | 1483  | 
lemma take_bit_of_2 [simp]:  | 
1484  | 
\<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close>  | 
|
1485  | 
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
 | 
1486  | 
|
| 71412 | 1487  | 
lemma take_bit_of_mask:  | 
| 71408 | 1488  | 
\<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close>  | 
| 71412 | 1489  | 
by (simp add: take_bit_eq_mod mask_mod_exp)  | 
| 71408 | 1490  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1491  | 
lemma push_bit_eq_0_iff [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1492  | 
"push_bit n a = 0 \<longleftrightarrow> a = 0"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1493  | 
by (simp add: push_bit_eq_mult)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1494  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1495  | 
lemma push_bit_numeral [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1496  | 
"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
 | 
1497  | 
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
 | 
1498  | 
|
| 68010 | 1499  | 
lemma push_bit_of_nat:  | 
1500  | 
"push_bit n (of_nat m) = of_nat (push_bit n m)"  | 
|
1501  | 
by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult)  | 
|
1502  | 
||
| 67961 | 1503  | 
lemma take_bit_add:  | 
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
1504  | 
"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
 | 
1505  | 
by (simp add: take_bit_eq_mod mod_simps)  | 
| 67816 | 1506  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
1507  | 
lemma take_bit_of_1_eq_0_iff [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
1508  | 
"take_bit n 1 = 0 \<longleftrightarrow> n = 0"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
1509  | 
by (simp add: take_bit_eq_mod)  | 
| 67816 | 1510  | 
|
| 71799 | 1511  | 
lemma take_bit_Suc_bit0 [simp]:  | 
1512  | 
\<open>take_bit (Suc n) (numeral (Num.Bit0 k)) = take_bit n (numeral k) * 2\<close>  | 
|
1513  | 
by (simp add: take_bit_Suc numeral_Bit0_div_2)  | 
|
1514  | 
||
1515  | 
lemma take_bit_Suc_bit1 [simp]:  | 
|
1516  | 
\<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close>  | 
|
| 71822 | 1517  | 
by (simp add: take_bit_Suc numeral_Bit1_div_2 mod_2_eq_odd)  | 
| 71799 | 1518  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1519  | 
lemma take_bit_numeral_bit0 [simp]:  | 
| 71799 | 1520  | 
\<open>take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2\<close>  | 
1521  | 
by (simp add: take_bit_rec numeral_Bit0_div_2)  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1522  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1523  | 
lemma take_bit_numeral_bit1 [simp]:  | 
| 71799 | 1524  | 
\<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close>  | 
| 71822 | 1525  | 
by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd)  | 
| 67961 | 1526  | 
|
| 68010 | 1527  | 
lemma take_bit_of_nat:  | 
1528  | 
"take_bit n (of_nat m) = of_nat (take_bit n m)"  | 
|
1529  | 
by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"])  | 
|
1530  | 
||
| 71799 | 1531  | 
lemma drop_bit_Suc_bit0 [simp]:  | 
1532  | 
\<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close>  | 
|
1533  | 
by (simp add: drop_bit_Suc numeral_Bit0_div_2)  | 
|
1534  | 
||
1535  | 
lemma drop_bit_Suc_bit1 [simp]:  | 
|
1536  | 
\<open>drop_bit (Suc n) (numeral (Num.Bit1 k)) = drop_bit n (numeral k)\<close>  | 
|
1537  | 
by (simp add: drop_bit_Suc numeral_Bit1_div_2)  | 
|
1538  | 
||
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1539  | 
lemma drop_bit_numeral_bit0 [simp]:  | 
| 71799 | 1540  | 
\<open>drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)\<close>  | 
1541  | 
by (simp add: drop_bit_rec numeral_Bit0_div_2)  | 
|
| 67816 | 1542  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1543  | 
lemma drop_bit_numeral_bit1 [simp]:  | 
| 71799 | 1544  | 
\<open>drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)\<close>  | 
1545  | 
by (simp add: drop_bit_rec numeral_Bit1_div_2)  | 
|
| 67816 | 1546  | 
|
| 68010 | 1547  | 
lemma drop_bit_of_nat:  | 
1548  | 
"drop_bit n (of_nat m) = of_nat (drop_bit n m)"  | 
|
| 68389 | 1549  | 
by (simp add: drop_bit_eq_div Parity.drop_bit_eq_div of_nat_div [of m "2 ^ n"])  | 
| 68010 | 1550  | 
|
| 71412 | 1551  | 
lemma bit_of_nat_iff_bit [simp]:  | 
1552  | 
\<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>  | 
|
1553  | 
proof -  | 
|
1554  | 
have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>  | 
|
1555  | 
by simp  | 
|
1556  | 
also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close>  | 
|
1557  | 
by (simp add: of_nat_div)  | 
|
1558  | 
finally show ?thesis  | 
|
| 
71965
 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 
haftmann 
parents: 
71958 
diff
changeset
 | 
1559  | 
by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd)  | 
| 71412 | 1560  | 
qed  | 
1561  | 
||
1562  | 
lemma of_nat_push_bit:  | 
|
1563  | 
\<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>  | 
|
1564  | 
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)  | 
|
1565  | 
||
1566  | 
lemma of_nat_drop_bit:  | 
|
1567  | 
\<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close>  | 
|
1568  | 
by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div)  | 
|
1569  | 
||
1570  | 
lemma of_nat_take_bit:  | 
|
1571  | 
\<open>of_nat (take_bit m n) = take_bit m (of_nat n)\<close>  | 
|
1572  | 
by (simp add: take_bit_eq_mod semiring_bit_shifts_class.take_bit_eq_mod of_nat_mod)  | 
|
1573  | 
||
1574  | 
lemma bit_push_bit_iff_of_nat_iff:  | 
|
1575  | 
\<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close>  | 
|
| 71424 | 1576  | 
by (auto simp add: bit_push_bit_iff)  | 
| 71412 | 1577  | 
|
| 58770 | 1578  | 
end  | 
| 67816 | 1579  | 
|
| 71094 | 1580  | 
instance nat :: unique_euclidean_semiring_with_bit_shifts ..  | 
1581  | 
||
1582  | 
instance int :: unique_euclidean_semiring_with_bit_shifts ..  | 
|
1583  | 
||
| 
71804
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1584  | 
lemma bit_nat_iff [simp]:  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1585  | 
\<open>bit (nat k) n \<longleftrightarrow> k > 0 \<and> bit k n\<close>  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1586  | 
proof (cases \<open>k > 0\<close>)  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1587  | 
case True  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1588  | 
moreover define m where \<open>m = nat k\<close>  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1589  | 
ultimately have \<open>k = int m\<close>  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1590  | 
by simp  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1591  | 
then show ?thesis  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1592  | 
by (auto intro: ccontr)  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1593  | 
next  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1594  | 
case False  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1595  | 
then show ?thesis  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1596  | 
by simp  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1597  | 
qed  | 
| 
 
6fd70ed18199
simplified construction of binary bit operations
 
haftmann 
parents: 
71802 
diff
changeset
 | 
1598  | 
|
| 71802 | 1599  | 
lemma not_exp_less_eq_0_int [simp]:  | 
1600  | 
\<open>\<not> 2 ^ n \<le> (0::int)\<close>  | 
|
1601  | 
by (simp add: power_le_zero_eq)  | 
|
1602  | 
||
1603  | 
lemma half_nonnegative_int_iff [simp]:  | 
|
1604  | 
\<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int  | 
|
1605  | 
proof (cases \<open>k \<ge> 0\<close>)  | 
|
1606  | 
case True  | 
|
1607  | 
then show ?thesis  | 
|
1608  | 
by (auto simp add: divide_int_def sgn_1_pos)  | 
|
1609  | 
next  | 
|
1610  | 
case False  | 
|
1611  | 
then show ?thesis  | 
|
1612  | 
apply (auto simp add: divide_int_def not_le elim!: evenE)  | 
|
1613  | 
apply (simp only: minus_mult_right)  | 
|
1614  | 
apply (subst nat_mult_distrib)  | 
|
1615  | 
apply simp_all  | 
|
1616  | 
done  | 
|
1617  | 
qed  | 
|
1618  | 
||
1619  | 
lemma half_negative_int_iff [simp]:  | 
|
1620  | 
\<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int  | 
|
1621  | 
by (subst Not_eq_iff [symmetric]) (simp add: not_less)  | 
|
1622  | 
||
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1623  | 
lemma push_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1624  | 
"push_bit n (Suc 0) = 2 ^ n"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1625  | 
using push_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1626  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1627  | 
lemma take_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1628  | 
"take_bit n (Suc 0) = of_bool (0 < n)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1629  | 
using take_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1630  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1631  | 
lemma drop_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1632  | 
"drop_bit n (Suc 0) = of_bool (n = 0)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1633  | 
using drop_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1634  | 
|
| 70973 | 1635  | 
lemma take_bit_eq_self:  | 
1636  | 
\<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for n m :: nat  | 
|
1637  | 
using that by (simp add: take_bit_eq_mod)  | 
|
1638  | 
||
| 70911 | 1639  | 
lemma push_bit_minus_one:  | 
1640  | 
"push_bit n (- 1 :: int) = - (2 ^ n)"  | 
|
1641  | 
by (simp add: push_bit_eq_mult)  | 
|
1642  | 
||
| 71195 | 1643  | 
lemma minus_1_div_exp_eq_int:  | 
1644  | 
\<open>- 1 div (2 :: int) ^ n = - 1\<close>  | 
|
1645  | 
by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>)  | 
|
1646  | 
||
1647  | 
lemma drop_bit_minus_one [simp]:  | 
|
1648  | 
\<open>drop_bit n (- 1 :: int) = - 1\<close>  | 
|
1649  | 
by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int)  | 
|
1650  | 
||
| 71424 | 1651  | 
lemma take_bit_minus:  | 
| 71195 | 1652  | 
"take_bit n (- (take_bit n k)) = take_bit n (- k)"  | 
1653  | 
for k :: int  | 
|
1654  | 
by (simp add: take_bit_eq_mod mod_minus_eq)  | 
|
1655  | 
||
| 71424 | 1656  | 
lemma take_bit_diff:  | 
| 71195 | 1657  | 
"take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)"  | 
1658  | 
for k l :: int  | 
|
1659  | 
by (simp add: take_bit_eq_mod mod_diff_eq)  | 
|
1660  | 
||
1661  | 
lemma take_bit_nonnegative [simp]:  | 
|
1662  | 
"take_bit n k \<ge> 0"  | 
|
1663  | 
for k :: int  | 
|
1664  | 
by (simp add: take_bit_eq_mod)  | 
|
1665  | 
||
| 71759 | 1666  | 
lemma take_bit_minus_small_eq:  | 
1667  | 
\<open>take_bit n (- k) = 2 ^ n - k\<close> if \<open>0 < k\<close> \<open>k \<le> 2 ^ n\<close> for k :: int  | 
|
1668  | 
proof -  | 
|
1669  | 
define m where \<open>m = nat k\<close>  | 
|
1670  | 
with that have \<open>k = int m\<close> and \<open>0 < m\<close> and \<open>m \<le> 2 ^ n\<close>  | 
|
1671  | 
by simp_all  | 
|
1672  | 
have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close>  | 
|
1673  | 
using \<open>0 < m\<close> by simp  | 
|
1674  | 
then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close>  | 
|
1675  | 
by simp  | 
|
1676  | 
then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close>  | 
|
1677  | 
using \<open>m \<le> 2 ^ n\<close> by (simp only: of_nat_mod of_nat_diff) simp  | 
|
1678  | 
with \<open>k = int m\<close> have \<open>(2 ^ n - k) mod 2 ^ n = 2 ^ n - k\<close>  | 
|
1679  | 
by simp  | 
|
1680  | 
then show ?thesis  | 
|
1681  | 
by (simp add: take_bit_eq_mod)  | 
|
1682  | 
qed  | 
|
1683  | 
||
| 71424 | 1684  | 
lemma drop_bit_push_bit_int:  | 
1685  | 
\<open>drop_bit m (push_bit n k) = drop_bit (m - n) (push_bit (n - m) k)\<close> for k :: int  | 
|
1686  | 
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  | 
|
| 71802 | 1687  | 
mult.commute [of k] drop_bit_eq_div push_bit_eq_mult not_le power_add dest!: le_Suc_ex less_imp_Suc_add)  | 
1688  | 
||
1689  | 
lemma push_bit_nonnegative_int_iff [simp]:  | 
|
1690  | 
\<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int  | 
|
1691  | 
by (simp add: push_bit_eq_mult zero_le_mult_iff)  | 
|
1692  | 
||
1693  | 
lemma push_bit_negative_int_iff [simp]:  | 
|
1694  | 
\<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int  | 
|
1695  | 
by (subst Not_eq_iff [symmetric]) (simp add: not_less)  | 
|
1696  | 
||
1697  | 
lemma drop_bit_nonnegative_int_iff [simp]:  | 
|
1698  | 
\<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int  | 
|
1699  | 
by (induction n) (simp_all add: drop_bit_Suc drop_bit_half)  | 
|
1700  | 
||
1701  | 
lemma drop_bit_negative_int_iff [simp]:  | 
|
1702  | 
\<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int  | 
|
1703  | 
by (subst Not_eq_iff [symmetric]) (simp add: not_less)  | 
|
| 71424 | 1704  | 
|
| 71853 | 1705  | 
code_identifier  | 
1706  | 
code_module Parity \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith  | 
|
1707  | 
||
| 67816 | 1708  | 
end  |