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