| author | wenzelm | 
| Wed, 06 Dec 2023 20:16:23 +0100 | |
| changeset 79155 | 53288743c2f0 | 
| parent 79118 | 486a32079c60 | 
| child 79531 | 22a137a6de44 | 
| 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 | |
| 77061 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76387diff
changeset | 9 | imports Euclidean_Rings | 
| 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: 
58645diff
changeset | 13 | |
| 70341 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 14 | class semiring_parity = comm_semiring_1 + semiring_modulo + | 
| 79118 | 15 | assumes mod_2_eq_odd: \<open>a mod 2 = of_bool (\<not> 2 dvd a)\<close> | 
| 16 | and odd_one [simp]: \<open>\<not> 2 dvd 1\<close> | |
| 66839 | 17 | begin | 
| 18 | ||
| 58740 | 19 | abbreviation even :: "'a \<Rightarrow> bool" | 
| 79118 | 20 | where \<open>even a \<equiv> 2 dvd a\<close> | 
| 54228 | 21 | |
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 22 | abbreviation odd :: "'a \<Rightarrow> bool" | 
| 79118 | 23 | where \<open>odd a \<equiv> \<not> 2 dvd a\<close> | 
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 24 | |
| 76387 | 25 | end | 
| 26 | ||
| 27 | class ring_parity = ring + semiring_parity | |
| 28 | begin | |
| 29 | ||
| 30 | subclass comm_ring_1 .. | |
| 31 | ||
| 32 | end | |
| 33 | ||
| 34 | instance nat :: semiring_parity | |
| 35 | by standard (simp_all add: dvd_eq_mod_eq_0) | |
| 36 | ||
| 37 | instance int :: ring_parity | |
| 38 | by standard (auto simp add: dvd_eq_mod_eq_0) | |
| 39 | ||
| 40 | context semiring_parity | |
| 41 | begin | |
| 42 | ||
| 58690 | 43 | lemma evenE [elim?]: | 
| 79118 | 44 | assumes \<open>even a\<close> | 
| 45 | obtains b where \<open>a = 2 * b\<close> | |
| 58740 | 46 | using assms by (rule dvdE) | 
| 58690 | 47 | |
| 58681 | 48 | lemma oddE [elim?]: | 
| 79118 | 49 | assumes \<open>odd a\<close> | 
| 50 | obtains b where \<open>a = 2 * b + 1\<close> | |
| 58787 | 51 | proof - | 
| 79118 | 52 | have \<open>a = 2 * (a div 2) + a mod 2\<close> | 
| 66815 | 53 | by (simp add: mult_div_mod_eq) | 
| 79118 | 54 | with assms have \<open>a = 2 * (a div 2) + 1\<close> | 
| 55 | by (simp add: mod_2_eq_odd) | |
| 56 | then show thesis .. | |
| 66815 | 57 | qed | 
| 58 | ||
| 67816 | 59 | lemma of_bool_odd_eq_mod_2: | 
| 79117 | 60 | \<open>of_bool (odd a) = a mod 2\<close> | 
| 61 | by (simp add: mod_2_eq_odd) | |
| 62 | ||
| 63 | lemma odd_of_bool_self [simp]: | |
| 64 | \<open>odd (of_bool p) \<longleftrightarrow> p\<close> | |
| 65 | by (cases p) simp_all | |
| 66 | ||
| 67 | lemma not_mod_2_eq_0_eq_1 [simp]: | |
| 68 | \<open>a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1\<close> | |
| 69 | by (simp add: mod_2_eq_odd) | |
| 70 | ||
| 71 | lemma not_mod_2_eq_1_eq_0 [simp]: | |
| 72 | \<open>a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0\<close> | |
| 67816 | 73 | by (simp add: mod_2_eq_odd) | 
| 74 | ||
| 79118 | 75 | lemma even_iff_mod_2_eq_zero: | 
| 76 | \<open>2 dvd a \<longleftrightarrow> a mod 2 = 0\<close> | |
| 77 | by (simp add: mod_2_eq_odd) | |
| 78 | ||
| 79 | lemma odd_iff_mod_2_eq_one: | |
| 80 | \<open>\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1\<close> | |
| 81 | by (simp add: mod_2_eq_odd) | |
| 82 | ||
| 71426 | 83 | lemma even_mod_2_iff [simp]: | 
| 84 | \<open>even (a mod 2) \<longleftrightarrow> even a\<close> | |
| 85 | by (simp add: mod_2_eq_odd) | |
| 86 | ||
| 87 | lemma mod2_eq_if: | |
| 88 | "a mod 2 = (if even a then 0 else 1)" | |
| 89 | by (simp add: mod_2_eq_odd) | |
| 90 | ||
| 79117 | 91 | lemma zero_mod_two_eq_zero [simp]: | 
| 92 | \<open>0 mod 2 = 0\<close> | |
| 93 | by (simp add: mod_2_eq_odd) | |
| 94 | ||
| 95 | lemma one_mod_two_eq_one [simp]: | |
| 96 | \<open>1 mod 2 = 1\<close> | |
| 97 | by (simp add: mod_2_eq_odd) | |
| 98 | ||
| 99 | lemma parity_cases [case_names even odd]: | |
| 100 | assumes \<open>even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P\<close> | |
| 101 | assumes \<open>odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P\<close> | |
| 102 | shows P | |
| 103 | using assms by (auto simp add: mod_2_eq_odd) | |
| 104 | ||
| 66815 | 105 | lemma even_zero [simp]: | 
| 79117 | 106 | \<open>even 0\<close> | 
| 66815 | 107 | by (fact dvd_0_right) | 
| 108 | ||
| 109 | lemma odd_even_add: | |
| 110 | "even (a + b)" if "odd a" and "odd b" | |
| 111 | proof - | |
| 112 | from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1" | |
| 113 | by (blast elim: oddE) | |
| 114 | then have "a + b = 2 * c + 2 * d + (1 + 1)" | |
| 115 | by (simp only: ac_simps) | |
| 116 | also have "\<dots> = 2 * (c + d + 1)" | |
| 117 | by (simp add: algebra_simps) | |
| 118 | finally show ?thesis .. | |
| 119 | qed | |
| 120 | ||
| 121 | lemma even_add [simp]: | |
| 122 | "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)" | |
| 123 | by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add) | |
| 124 | ||
| 125 | lemma odd_add [simp]: | |
| 126 | "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)" | |
| 127 | by simp | |
| 128 | ||
| 129 | lemma even_plus_one_iff [simp]: | |
| 130 | "even (a + 1) \<longleftrightarrow> odd a" | |
| 131 | by (auto simp add: dvd_add_right_iff intro: odd_even_add) | |
| 132 | ||
| 133 | lemma even_mult_iff [simp]: | |
| 134 | "even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q") | |
| 135 | proof | |
| 136 | assume ?Q | |
| 137 | then show ?P | |
| 138 | by auto | |
| 139 | next | |
| 140 | assume ?P | |
| 141 | show ?Q | |
| 142 | proof (rule ccontr) | |
| 143 | assume "\<not> (even a \<or> even b)" | |
| 144 | then have "odd a" and "odd b" | |
| 145 | by auto | |
| 146 | then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1" | |
| 147 | by (blast elim: oddE) | |
| 148 | then have "a * b = (2 * r + 1) * (2 * s + 1)" | |
| 149 | by simp | |
| 150 | also have "\<dots> = 2 * (2 * r * s + r + s) + 1" | |
| 151 | by (simp add: algebra_simps) | |
| 152 | finally have "odd (a * b)" | |
| 153 | by simp | |
| 154 | with \<open>?P\<close> show False | |
| 155 | by auto | |
| 156 | qed | |
| 157 | qed | |
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 158 | |
| 63654 | 159 | lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))" | 
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 160 | proof - | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 161 | have "even (2 * numeral n)" | 
| 66815 | 162 | unfolding even_mult_iff by simp | 
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 163 | then have "even (numeral n + numeral n)" | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 164 | unfolding mult_2 . | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 165 | then show ?thesis | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 166 | unfolding numeral.simps . | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 167 | qed | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 168 | |
| 63654 | 169 | lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))" | 
| 58678 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 170 | proof | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 171 | assume "even (numeral (num.Bit1 n))" | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 172 | then have "even (numeral n + numeral n + 1)" | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 173 | unfolding numeral.simps . | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 174 | then have "even (2 * numeral n + 1)" | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 175 | unfolding mult_2 . | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 176 | then have "2 dvd numeral n * 2 + 1" | 
| 58740 | 177 | by (simp add: ac_simps) | 
| 63654 | 178 | then have "2 dvd 1" | 
| 179 | 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: 
58645diff
changeset | 180 | then show False by simp | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 181 | qed | 
| 
398e05aa84d4
purely algebraic characterization of even and odd
 haftmann parents: 
58645diff
changeset | 182 | |
| 71755 | 183 | lemma odd_numeral_BitM [simp]: | 
| 184 | \<open>odd (numeral (Num.BitM w))\<close> | |
| 185 | by (cases w) simp_all | |
| 186 | ||
| 63654 | 187 | lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0" | 
| 58680 | 188 | by (induct n) auto | 
| 189 | ||
| 76387 | 190 | lemma even_prod_iff: | 
| 191 | \<open>even (prod f A) \<longleftrightarrow> (\<exists>a\<in>A. even (f a))\<close> if \<open>finite A\<close> | |
| 192 | using that by (induction A) simp_all | |
| 193 | ||
| 71412 | 194 | lemma mask_eq_sum_exp: | 
| 195 |   \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
 | |
| 196 | proof - | |
| 197 |   have *: \<open>{q. q < Suc m} = insert m {q. q < m}\<close> for m
 | |
| 198 | by auto | |
| 199 |   have \<open>2 ^ n = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1\<close>
 | |
| 200 | by (induction n) (simp_all add: ac_simps mult_2 *) | |
| 201 |   then have \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1 - 1\<close>
 | |
| 202 | by simp | |
| 203 | then show ?thesis | |
| 204 | by simp | |
| 205 | qed | |
| 206 | ||
| 76387 | 207 | lemma (in -) mask_eq_sum_exp_nat: | 
| 208 |   \<open>2 ^ n - Suc 0 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
 | |
| 209 | using mask_eq_sum_exp [where ?'a = nat] by simp | |
| 210 | ||
| 70341 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 211 | end | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 212 | |
| 76387 | 213 | context ring_parity | 
| 70341 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 214 | begin | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 215 | |
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 216 | lemma even_minus: | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 217 | "even (- a) \<longleftrightarrow> even a" | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 218 | by (fact dvd_minus_iff) | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 219 | |
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 220 | lemma even_diff [simp]: | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 221 | "even (a - b) \<longleftrightarrow> even (a + b)" | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 222 | 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: 
70340diff
changeset | 223 | |
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 224 | end | 
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 225 | |
| 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 226 | |
| 69593 | 227 | subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close> | 
| 66808 
1907167b6038
elementary definition of division on natural numbers
 haftmann parents: 
66582diff
changeset | 228 | |
| 66815 | 229 | lemma even_Suc_Suc_iff [simp]: | 
| 230 | "even (Suc (Suc n)) \<longleftrightarrow> even n" | |
| 58787 | 231 | using dvd_add_triv_right_iff [of 2 n] by simp | 
| 58687 | 232 | |
| 66815 | 233 | lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n" | 
| 234 | using even_plus_one_iff [of n] by simp | |
| 58787 | 235 | |
| 66815 | 236 | lemma even_diff_nat [simp]: | 
| 237 | "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat | |
| 58787 | 238 | proof (cases "n \<le> m") | 
| 239 | case True | |
| 240 | then have "m - n + n * 2 = m + n" by (simp add: mult_2_right) | |
| 66815 | 241 | moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp | 
| 242 | ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:) | |
| 58787 | 243 | then show ?thesis by auto | 
| 244 | next | |
| 245 | case False | |
| 246 | then show ?thesis by simp | |
| 63654 | 247 | qed | 
| 248 | ||
| 66815 | 249 | lemma odd_pos: | 
| 250 | "odd n \<Longrightarrow> 0 < n" for n :: nat | |
| 58690 | 251 | by (auto elim: oddE) | 
| 60343 
063698416239
correct sort constraints for abbreviations in type classes
 haftmann parents: 
59816diff
changeset | 252 | |
| 66815 | 253 | lemma Suc_double_not_eq_double: | 
| 254 | "Suc (2 * m) \<noteq> 2 * n" | |
| 62597 | 255 | proof | 
| 256 | assume "Suc (2 * m) = 2 * n" | |
| 257 | moreover have "odd (Suc (2 * m))" and "even (2 * n)" | |
| 258 | by simp_all | |
| 259 | ultimately show False by simp | |
| 260 | qed | |
| 261 | ||
| 66815 | 262 | lemma double_not_eq_Suc_double: | 
| 263 | "2 * m \<noteq> Suc (2 * n)" | |
| 62597 | 264 | using Suc_double_not_eq_double [of n m] by simp | 
| 265 | ||
| 66815 | 266 | lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n" | 
| 267 | by (auto elim: oddE) | |
| 60343 
063698416239
correct sort constraints for abbreviations in type classes
 haftmann parents: 
59816diff
changeset | 268 | |
| 66815 | 269 | lemma even_Suc_div_two [simp]: | 
| 270 | "even n \<Longrightarrow> Suc n div 2 = n div 2" | |
| 76387 | 271 | by auto | 
| 60343 
063698416239
correct sort constraints for abbreviations in type classes
 haftmann parents: 
59816diff
changeset | 272 | |
| 66815 | 273 | lemma odd_Suc_div_two [simp]: | 
| 274 | "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)" | |
| 76387 | 275 | by (auto elim: oddE) | 
| 60343 
063698416239
correct sort constraints for abbreviations in type classes
 haftmann parents: 
59816diff
changeset | 276 | |
| 66815 | 277 | lemma odd_two_times_div_two_nat [simp]: | 
| 278 | assumes "odd n" | |
| 279 | shows "2 * (n div 2) = n - (1 :: nat)" | |
| 280 | proof - | |
| 281 | from assms have "2 * (n div 2) + 1 = n" | |
| 76387 | 282 | by (auto elim: oddE) | 
| 66815 | 283 | then have "Suc (2 * (n div 2)) - 1 = n - 1" | 
| 58787 | 284 | by simp | 
| 66815 | 285 | then show ?thesis | 
| 286 | by simp | |
| 58787 | 287 | qed | 
| 58680 | 288 | |
| 70341 
972c0c744e7c
generalized type classes for parity to cover word types also, which contain zero divisors
 haftmann parents: 
70340diff
changeset | 289 | 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: 
70340diff
changeset | 290 | "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: 
70340diff
changeset | 291 | 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: 
70340diff
changeset | 292 | |
| 69502 | 293 | lemma odd_card_imp_not_empty: | 
| 294 |   \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
 | |
| 295 | using that by auto | |
| 296 | ||
| 70365 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 297 | lemma nat_induct2 [case_names 0 1 step]: | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 298 | 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: 
70353diff
changeset | 299 | shows "P n" | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 300 | proof (induct n rule: less_induct) | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 301 | case (less n) | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 302 | show ?case | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 303 | proof (cases "n < Suc (Suc 0)") | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 304 | case True | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 305 | then show ?thesis | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 306 | using assms by (auto simp: less_Suc_eq) | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 307 | next | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 308 | case False | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 309 | 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: 
70353diff
changeset | 310 | 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: 
70353diff
changeset | 311 | then have "k<n" | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 312 | by simp | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 313 | with less assms have "P (k+2)" | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 314 | by blast | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 315 | then show ?thesis | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 316 | by (simp add: k) | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 317 | qed | 
| 
4df0628e8545
a few new lemmas and a bit of tidying
 paulson <lp15@cam.ac.uk> parents: 
70353diff
changeset | 318 | qed | 
| 58687 | 319 | |
| 78668 | 320 | lemma mod_double_nat: | 
| 321 | \<open>n mod (2 * m) = n mod m \<or> n mod (2 * m) = n mod m + m\<close> | |
| 322 | for m n :: nat | |
| 323 | by (cases \<open>even (n div m)\<close>) | |
| 324 | (simp_all add: mod_mult2_eq ac_simps even_iff_mod_2_eq_zero) | |
| 325 | ||
| 71412 | 326 | context semiring_parity | 
| 327 | begin | |
| 328 | ||
| 329 | lemma even_sum_iff: | |
| 330 |   \<open>even (sum f A) \<longleftrightarrow> even (card {a\<in>A. odd (f a)})\<close> if \<open>finite A\<close>
 | |
| 331 | using that proof (induction A) | |
| 332 | case empty | |
| 333 | then show ?case | |
| 334 | by simp | |
| 335 | next | |
| 336 | case (insert a A) | |
| 337 |   moreover have \<open>{b \<in> insert a A. odd (f b)} = (if odd (f a) then {a} else {}) \<union> {b \<in> A. odd (f b)}\<close>
 | |
| 338 | by auto | |
| 339 | ultimately show ?case | |
| 340 | by simp | |
| 341 | qed | |
| 342 | ||
| 343 | lemma even_mask_iff [simp]: | |
| 344 | \<open>even (2 ^ n - 1) \<longleftrightarrow> n = 0\<close> | |
| 345 | proof (cases \<open>n = 0\<close>) | |
| 346 | case True | |
| 347 | then show ?thesis | |
| 348 | by simp | |
| 349 | next | |
| 350 | case False | |
| 351 |   then have \<open>{a. a = 0 \<and> a < n} = {0}\<close>
 | |
| 352 | by auto | |
| 353 | then show ?thesis | |
| 354 | by (auto simp add: mask_eq_sum_exp even_sum_iff) | |
| 355 | qed | |
| 356 | ||
| 76387 | 357 | lemma even_of_nat_iff [simp]: | 
| 358 | "even (of_nat n) \<longleftrightarrow> even n" | |
| 359 | by (induction n) simp_all | |
| 360 | ||
| 71412 | 361 | end | 
| 362 | ||
| 71138 | 363 | |
| 60758 | 364 | subsection \<open>Parity and powers\<close> | 
| 58689 | 365 | |
| 61531 
ab2e862263e7
Rounding function, uniform limits, cotangent, binomial identities
 eberlm parents: 
60867diff
changeset | 366 | context ring_1 | 
| 58689 | 367 | begin | 
| 368 | ||
| 63654 | 369 | lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n" | 
| 58690 | 370 | by (auto elim: evenE) | 
| 58689 | 371 | |
| 63654 | 372 | lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)" | 
| 58690 | 373 | by (auto elim: oddE) | 
| 374 | ||
| 66815 | 375 | lemma uminus_power_if: | 
| 376 | "(- a) ^ n = (if even n then a ^ n else - (a ^ n))" | |
| 377 | by auto | |
| 378 | ||
| 63654 | 379 | lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1" | 
| 58690 | 380 | by simp | 
| 58689 | 381 | |
| 63654 | 382 | lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1" | 
| 58690 | 383 | by simp | 
| 58689 | 384 | |
| 66582 | 385 | lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)" | 
| 386 | by (cases "even (n + k)") auto | |
| 387 | ||
| 67371 
2d9cf74943e1
moved in some material from Euler-MacLaurin
 paulson <lp15@cam.ac.uk> parents: 
67083diff
changeset | 388 | 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: 
67083diff
changeset | 389 | by (induct n) auto | 
| 
2d9cf74943e1
moved in some material from Euler-MacLaurin
 paulson <lp15@cam.ac.uk> parents: 
67083diff
changeset | 390 | |
| 63654 | 391 | end | 
| 58689 | 392 | |
| 393 | context linordered_idom | |
| 394 | begin | |
| 395 | ||
| 63654 | 396 | lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n" | 
| 58690 | 397 | by (auto elim: evenE) | 
| 58689 | 398 | |
| 63654 | 399 | lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a" | 
| 58689 | 400 | by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE) | 
| 401 | ||
| 63654 | 402 | lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a" | 
| 58787 | 403 | by (auto simp add: zero_le_even_power zero_le_odd_power) | 
| 63654 | 404 | |
| 405 | 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 | 406 | proof - | 
| 407 | have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0" | |
| 58787 | 408 | unfolding power_eq_0_iff [of a n, symmetric] by blast | 
| 58689 | 409 | show ?thesis | 
| 63654 | 410 | unfolding less_le zero_le_power_eq by auto | 
| 58689 | 411 | qed | 
| 412 | ||
| 63654 | 413 | lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0" | 
| 58689 | 414 | unfolding not_le [symmetric] zero_le_power_eq by auto | 
| 415 | ||
| 63654 | 416 | 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)" | 
| 417 | unfolding not_less [symmetric] zero_less_power_eq by auto | |
| 418 | ||
| 419 | lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n" | |
| 58689 | 420 | using power_abs [of a n] by (simp add: zero_le_even_power) | 
| 421 | ||
| 422 | lemma power_mono_even: | |
| 423 | assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>" | |
| 424 | shows "a ^ n \<le> b ^ n" | |
| 425 | proof - | |
| 426 | have "0 \<le> \<bar>a\<bar>" by auto | |
| 63654 | 427 | with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" | 
| 428 | by (rule power_mono) | |
| 429 | with \<open>even n\<close> show ?thesis | |
| 430 | by (simp add: power_even_abs) | |
| 58689 | 431 | qed | 
| 432 | ||
| 433 | lemma power_mono_odd: | |
| 434 | assumes "odd n" and "a \<le> b" | |
| 435 | shows "a ^ n \<le> b ^ n" | |
| 436 | proof (cases "b < 0") | |
| 63654 | 437 | case True | 
| 438 | with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto | |
| 439 | then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono) | |
| 60758 | 440 | with \<open>odd n\<close> show ?thesis by simp | 
| 58689 | 441 | next | 
| 63654 | 442 | case False | 
| 443 | then have "0 \<le> b" by auto | |
| 58689 | 444 | show ?thesis | 
| 445 | proof (cases "a < 0") | |
| 63654 | 446 | case True | 
| 447 | then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto | |
| 60758 | 448 | then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto | 
| 63654 | 449 | moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto | 
| 58689 | 450 | ultimately show ?thesis by auto | 
| 451 | next | |
| 63654 | 452 | case False | 
| 453 | then have "0 \<le> a" by auto | |
| 454 | with \<open>a \<le> b\<close> show ?thesis | |
| 455 | using power_mono by auto | |
| 58689 | 456 | qed | 
| 457 | qed | |
| 62083 | 458 | |
| 60758 | 459 | text \<open>Simplify, when the exponent is a numeral\<close> | 
| 58689 | 460 | |
| 461 | lemma zero_le_power_eq_numeral [simp]: | |
| 462 | "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a" | |
| 463 | by (fact zero_le_power_eq) | |
| 464 | ||
| 465 | lemma zero_less_power_eq_numeral [simp]: | |
| 63654 | 466 | "0 < a ^ numeral w \<longleftrightarrow> | 
| 467 | numeral w = (0 :: nat) \<or> | |
| 468 | even (numeral w :: nat) \<and> a \<noteq> 0 \<or> | |
| 469 | odd (numeral w :: nat) \<and> 0 < a" | |
| 58689 | 470 | by (fact zero_less_power_eq) | 
| 471 | ||
| 472 | lemma power_le_zero_eq_numeral [simp]: | |
| 63654 | 473 | "a ^ numeral w \<le> 0 \<longleftrightarrow> | 
| 474 | (0 :: nat) < numeral w \<and> | |
| 475 | (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)" | |
| 58689 | 476 | by (fact power_le_zero_eq) | 
| 477 | ||
| 478 | lemma power_less_zero_eq_numeral [simp]: | |
| 479 | "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0" | |
| 480 | by (fact power_less_zero_eq) | |
| 481 | ||
| 482 | lemma power_even_abs_numeral [simp]: | |
| 483 | "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w" | |
| 484 | by (fact power_even_abs) | |
| 485 | ||
| 486 | end | |
| 487 | ||
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 488 | |
| 69593 | 489 | subsection \<open>Instance for \<^typ>\<open>int\<close>\<close> | 
| 76387 | 490 | |
| 67816 | 491 | lemma even_diff_iff: | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 492 | "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int | 
| 67816 | 493 | by (fact even_diff) | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 494 | |
| 67816 | 495 | lemma even_abs_add_iff: | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 496 | "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int | 
| 67816 | 497 | by simp | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 498 | |
| 67816 | 499 | lemma even_add_abs_iff: | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 500 | "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int | 
| 67816 | 501 | by simp | 
| 66816 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 502 | |
| 
212a3334e7da
more fundamental definition of div and mod on int
 haftmann parents: 
66815diff
changeset | 503 | lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k" | 
| 74592 | 504 | by (simp add: even_of_nat_iff [of "nat k", where ?'a = int, symmetric]) | 
| 71138 | 505 | |
| 71837 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 506 | context | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 507 |   assumes "SORT_CONSTRAINT('a::division_ring)"
 | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 508 | begin | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 509 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 510 | lemma power_int_minus_left: | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 511 | "power_int (-a :: 'a) n = (if even n then power_int a n else -power_int a n)" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 512 | by (auto simp: power_int_def minus_one_power_iff even_nat_iff) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 513 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 514 | lemma power_int_minus_left_even [simp]: "even n \<Longrightarrow> power_int (-a :: 'a) n = power_int a n" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 515 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 516 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 517 | lemma power_int_minus_left_odd [simp]: "odd n \<Longrightarrow> power_int (-a :: 'a) n = -power_int a n" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 518 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 519 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 520 | lemma power_int_minus_left_distrib: | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 521 | "NO_MATCH (-1) x \<Longrightarrow> power_int (-a :: 'a) n = power_int (-1) n * power_int a n" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 522 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 523 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 524 | lemma power_int_minus_one_minus: "power_int (-1 :: 'a) (-n) = power_int (-1) n" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 525 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 526 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 527 | lemma power_int_minus_one_diff_commute: "power_int (-1 :: 'a) (a - b) = power_int (-1) (b - a)" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 528 | by (subst power_int_minus_one_minus [symmetric]) auto | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 529 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 530 | lemma power_int_minus_one_mult_self [simp]: | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 531 | "power_int (-1 :: 'a) m * power_int (-1) m = 1" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 532 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 533 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 534 | lemma power_int_minus_one_mult_self' [simp]: | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 535 | "power_int (-1 :: 'a) m * (power_int (-1) m * b) = b" | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 536 | by (simp add: power_int_minus_left) | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 537 | |
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 538 | end | 
| 
dca11678c495
new constant power_int in HOL
 Manuel Eberl <eberlm@in.tum.de> parents: 
71822diff
changeset | 539 | |
| 75937 | 540 | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 541 | subsection \<open>Special case: euclidean rings structurally containing the natural numbers\<close> | 
| 76387 | 542 | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 543 | class linordered_euclidean_semiring = discrete_linordered_semidom + unique_euclidean_semiring + | 
| 76387 | 544 | assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n" | 
| 545 | and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1" | |
| 546 | and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a" | |
| 547 | begin | |
| 548 | ||
| 549 | lemma division_segment_eq_iff: | |
| 550 | "a = b" if "division_segment a = division_segment b" | |
| 551 | and "euclidean_size a = euclidean_size b" | |
| 552 | using that division_segment_euclidean_size [of a] by simp | |
| 553 | ||
| 554 | lemma euclidean_size_of_nat [simp]: | |
| 555 | "euclidean_size (of_nat n) = n" | |
| 556 | proof - | |
| 557 | have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n" | |
| 558 | by (fact division_segment_euclidean_size) | |
| 559 | then show ?thesis by simp | |
| 560 | qed | |
| 561 | ||
| 562 | lemma of_nat_euclidean_size: | |
| 563 | "of_nat (euclidean_size a) = a div division_segment a" | |
| 564 | proof - | |
| 565 | have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a" | |
| 566 | by (subst nonzero_mult_div_cancel_left) simp_all | |
| 567 | also have "\<dots> = a div division_segment a" | |
| 568 | by simp | |
| 569 | finally show ?thesis . | |
| 570 | qed | |
| 571 | ||
| 572 | lemma division_segment_1 [simp]: | |
| 573 | "division_segment 1 = 1" | |
| 574 | using division_segment_of_nat [of 1] by simp | |
| 575 | ||
| 576 | lemma division_segment_numeral [simp]: | |
| 577 | "division_segment (numeral k) = 1" | |
| 578 | using division_segment_of_nat [of "numeral k"] by simp | |
| 579 | ||
| 580 | lemma euclidean_size_1 [simp]: | |
| 581 | "euclidean_size 1 = 1" | |
| 582 | using euclidean_size_of_nat [of 1] by simp | |
| 583 | ||
| 584 | lemma euclidean_size_numeral [simp]: | |
| 585 | "euclidean_size (numeral k) = numeral k" | |
| 586 | using euclidean_size_of_nat [of "numeral k"] by simp | |
| 587 | ||
| 588 | lemma of_nat_dvd_iff: | |
| 589 | "of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q") | |
| 590 | proof (cases "m = 0") | |
| 591 | case True | |
| 592 | then show ?thesis | |
| 593 | by simp | |
| 594 | next | |
| 595 | case False | |
| 596 | show ?thesis | |
| 597 | proof | |
| 598 | assume ?Q | |
| 599 | then show ?P | |
| 600 | by auto | |
| 601 | next | |
| 602 | assume ?P | |
| 603 | with False have "of_nat n = of_nat n div of_nat m * of_nat m" | |
| 604 | by simp | |
| 605 | then have "of_nat n = of_nat (n div m * m)" | |
| 606 | by (simp add: of_nat_div) | |
| 607 | then have "n = n div m * m" | |
| 608 | by (simp only: of_nat_eq_iff) | |
| 609 | then have "n = m * (n div m)" | |
| 610 | by (simp add: ac_simps) | |
| 611 | then show ?Q .. | |
| 612 | qed | |
| 613 | qed | |
| 614 | ||
| 615 | lemma of_nat_mod: | |
| 616 | "of_nat (m mod n) = of_nat m mod of_nat n" | |
| 617 | proof - | |
| 618 | have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m" | |
| 619 | by (simp add: div_mult_mod_eq) | |
| 620 | also have "of_nat m = of_nat (m div n * n + m mod n)" | |
| 621 | by simp | |
| 622 | finally show ?thesis | |
| 623 | by (simp only: of_nat_div of_nat_mult of_nat_add) simp | |
| 624 | qed | |
| 625 | ||
| 626 | lemma one_div_two_eq_zero [simp]: | |
| 627 | "1 div 2 = 0" | |
| 628 | proof - | |
| 629 | from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0" | |
| 630 | by (simp only:) simp | |
| 631 | then show ?thesis | |
| 632 | by simp | |
| 633 | qed | |
| 634 | ||
| 635 | lemma one_mod_2_pow_eq [simp]: | |
| 636 | "1 mod (2 ^ n) = of_bool (n > 0)" | |
| 637 | proof - | |
| 638 | have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))" | |
| 639 | using of_nat_mod [of 1 "2 ^ n"] by simp | |
| 640 | also have "\<dots> = of_bool (n > 0)" | |
| 641 | by simp | |
| 642 | finally show ?thesis . | |
| 643 | qed | |
| 644 | ||
| 645 | lemma one_div_2_pow_eq [simp]: | |
| 646 | "1 div (2 ^ n) = of_bool (n = 0)" | |
| 647 | using div_mult_mod_eq [of 1 "2 ^ n"] by auto | |
| 648 | ||
| 649 | lemma div_mult2_eq': | |
| 650 | \<open>a div (of_nat m * of_nat n) = a div of_nat m div of_nat n\<close> | |
| 651 | proof (cases \<open>m = 0 \<or> n = 0\<close>) | |
| 652 | case True | |
| 653 | then show ?thesis | |
| 654 | by auto | |
| 655 | next | |
| 656 | case False | |
| 657 | then have \<open>m > 0\<close> \<open>n > 0\<close> | |
| 658 | by simp_all | |
| 659 | show ?thesis | |
| 660 | proof (cases \<open>of_nat m * of_nat n dvd a\<close>) | |
| 661 | case True | |
| 662 | then obtain b where \<open>a = (of_nat m * of_nat n) * b\<close> .. | |
| 663 | then have \<open>a = of_nat m * (of_nat n * b)\<close> | |
| 664 | by (simp add: ac_simps) | |
| 665 | then show ?thesis | |
| 666 | by simp | |
| 667 | next | |
| 668 | case False | |
| 669 | define q where \<open>q = a div (of_nat m * of_nat n)\<close> | |
| 670 | define r where \<open>r = a mod (of_nat m * of_nat n)\<close> | |
| 671 | from \<open>m > 0\<close> \<open>n > 0\<close> \<open>\<not> of_nat m * of_nat n dvd a\<close> r_def have "division_segment r = 1" | |
| 672 | using division_segment_of_nat [of "m * n"] by (simp add: division_segment_mod) | |
| 673 | with division_segment_euclidean_size [of r] | |
| 674 | have "of_nat (euclidean_size r) = r" | |
| 675 | by simp | |
| 676 | have "a mod (of_nat m * of_nat n) div (of_nat m * of_nat n) = 0" | |
| 677 | by simp | |
| 678 | with \<open>m > 0\<close> \<open>n > 0\<close> r_def have "r div (of_nat m * of_nat n) = 0" | |
| 679 | by simp | |
| 680 | with \<open>of_nat (euclidean_size r) = r\<close> | |
| 681 | have "of_nat (euclidean_size r) div (of_nat m * of_nat n) = 0" | |
| 682 | by simp | |
| 683 | then have "of_nat (euclidean_size r div (m * n)) = 0" | |
| 684 | by (simp add: of_nat_div) | |
| 685 | then have "of_nat (euclidean_size r div m div n) = 0" | |
| 686 | by (simp add: div_mult2_eq) | |
| 687 | with \<open>of_nat (euclidean_size r) = r\<close> have "r div of_nat m div of_nat n = 0" | |
| 688 | by (simp add: of_nat_div) | |
| 689 | with \<open>m > 0\<close> \<open>n > 0\<close> q_def | |
| 690 | have "q = (r div of_nat m + q * of_nat n * of_nat m div of_nat m) div of_nat n" | |
| 691 | by simp | |
| 692 | moreover have \<open>a = q * (of_nat m * of_nat n) + r\<close> | |
| 693 | by (simp add: q_def r_def div_mult_mod_eq) | |
| 694 | ultimately show \<open>a div (of_nat m * of_nat n) = a div of_nat m div of_nat n\<close> | |
| 695 | using q_def [symmetric] div_plus_div_distrib_dvd_right [of \<open>of_nat m\<close> \<open>q * (of_nat m * of_nat n)\<close> r] | |
| 696 | by (simp add: ac_simps) | |
| 697 | qed | |
| 698 | qed | |
| 699 | ||
| 700 | lemma mod_mult2_eq': | |
| 701 | "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" | |
| 702 | proof - | |
| 703 | 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)" | |
| 704 | by (simp add: combine_common_factor div_mult_mod_eq) | |
| 705 | 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)" | |
| 706 | by (simp add: ac_simps) | |
| 707 | ultimately show ?thesis | |
| 708 | by (simp add: div_mult2_eq' mult_commute) | |
| 709 | qed | |
| 710 | ||
| 711 | lemma div_mult2_numeral_eq: | |
| 712 | "a div numeral k div numeral l = a div numeral (k * l)" (is "?A = ?B") | |
| 713 | proof - | |
| 714 | have "?A = a div of_nat (numeral k) div of_nat (numeral l)" | |
| 715 | by simp | |
| 716 | also have "\<dots> = a div (of_nat (numeral k) * of_nat (numeral l))" | |
| 717 | by (fact div_mult2_eq' [symmetric]) | |
| 718 | also have "\<dots> = ?B" | |
| 719 | by simp | |
| 720 | finally show ?thesis . | |
| 721 | qed | |
| 722 | ||
| 723 | lemma numeral_Bit0_div_2: | |
| 724 | "numeral (num.Bit0 n) div 2 = numeral n" | |
| 725 | proof - | |
| 726 | have "numeral (num.Bit0 n) = numeral n + numeral n" | |
| 727 | by (simp only: numeral.simps) | |
| 728 | also have "\<dots> = numeral n * 2" | |
| 729 | by (simp add: mult_2_right) | |
| 730 | finally have "numeral (num.Bit0 n) div 2 = numeral n * 2 div 2" | |
| 731 | by simp | |
| 732 | also have "\<dots> = numeral n" | |
| 733 | by (rule nonzero_mult_div_cancel_right) simp | |
| 734 | finally show ?thesis . | |
| 735 | qed | |
| 736 | ||
| 737 | lemma numeral_Bit1_div_2: | |
| 738 | "numeral (num.Bit1 n) div 2 = numeral n" | |
| 739 | proof - | |
| 740 | have "numeral (num.Bit1 n) = numeral n + numeral n + 1" | |
| 741 | by (simp only: numeral.simps) | |
| 742 | also have "\<dots> = numeral n * 2 + 1" | |
| 743 | by (simp add: mult_2_right) | |
| 744 | finally have "numeral (num.Bit1 n) div 2 = (numeral n * 2 + 1) div 2" | |
| 745 | by simp | |
| 746 | also have "\<dots> = numeral n * 2 div 2 + 1 div 2" | |
| 747 | using dvd_triv_right by (rule div_plus_div_distrib_dvd_left) | |
| 748 | also have "\<dots> = numeral n * 2 div 2" | |
| 749 | by simp | |
| 750 | also have "\<dots> = numeral n" | |
| 751 | by (rule nonzero_mult_div_cancel_right) simp | |
| 752 | finally show ?thesis . | |
| 753 | qed | |
| 754 | ||
| 755 | lemma exp_mod_exp: | |
| 756 | \<open>2 ^ m mod 2 ^ n = of_bool (m < n) * 2 ^ m\<close> | |
| 757 | proof - | |
| 758 | have \<open>(2::nat) ^ m mod 2 ^ n = of_bool (m < n) * 2 ^ m\<close> (is \<open>?lhs = ?rhs\<close>) | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 759 | by (auto simp add: linorder_class.not_less monoid_mult_class.power_add dest!: le_Suc_ex) | 
| 76387 | 760 | then have \<open>of_nat ?lhs = of_nat ?rhs\<close> | 
| 761 | by simp | |
| 762 | then show ?thesis | |
| 763 | by (simp add: of_nat_mod) | |
| 764 | qed | |
| 765 | ||
| 766 | lemma mask_mod_exp: | |
| 767 | \<open>(2 ^ n - 1) mod 2 ^ m = 2 ^ min m n - 1\<close> | |
| 768 | proof - | |
| 769 | have \<open>(2 ^ n - 1) mod 2 ^ m = 2 ^ min m n - (1::nat)\<close> (is \<open>?lhs = ?rhs\<close>) | |
| 770 | proof (cases \<open>n \<le> m\<close>) | |
| 771 | case True | |
| 772 | then show ?thesis | |
| 773 | by (simp add: Suc_le_lessD) | |
| 774 | next | |
| 775 | case False | |
| 776 | then have \<open>m < n\<close> | |
| 777 | by simp | |
| 778 | then obtain q where n: \<open>n = Suc q + m\<close> | |
| 779 | by (auto dest: less_imp_Suc_add) | |
| 780 | then have \<open>min m n = m\<close> | |
| 781 | by simp | |
| 782 | moreover have \<open>(2::nat) ^ m \<le> 2 * 2 ^ q * 2 ^ m\<close> | |
| 783 | using mult_le_mono1 [of 1 \<open>2 * 2 ^ q\<close> \<open>2 ^ m\<close>] by simp | |
| 784 | with n have \<open>2 ^ n - 1 = (2 ^ Suc q - 1) * 2 ^ m + (2 ^ m - (1::nat))\<close> | |
| 785 | by (simp add: monoid_mult_class.power_add algebra_simps) | |
| 786 | ultimately show ?thesis | |
| 787 | by (simp only: euclidean_semiring_cancel_class.mod_mult_self3) simp | |
| 788 | qed | |
| 789 | then have \<open>of_nat ?lhs = of_nat ?rhs\<close> | |
| 790 | by simp | |
| 791 | then show ?thesis | |
| 792 | by (simp add: of_nat_mod of_nat_diff) | |
| 793 | qed | |
| 794 | ||
| 795 | lemma of_bool_half_eq_0 [simp]: | |
| 796 | \<open>of_bool b div 2 = 0\<close> | |
| 797 | by simp | |
| 798 | ||
| 78668 | 799 | lemma of_nat_mod_double: | 
| 800 | \<open>of_nat n mod (2 * of_nat m) = of_nat n mod of_nat m \<or> of_nat n mod (2 * of_nat m) = of_nat n mod of_nat m + of_nat m\<close> | |
| 801 | by (simp add: mod_double_nat flip: of_nat_mod of_nat_add of_nat_mult of_nat_numeral) | |
| 802 | ||
| 76387 | 803 | end | 
| 804 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 805 | instance nat :: linordered_euclidean_semiring | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 806 | by standard simp_all | 
| 76387 | 807 | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 808 | instance int :: linordered_euclidean_semiring | 
| 76387 | 809 | by standard (auto simp add: divide_int_def division_segment_int_def elim: contrapos_np) | 
| 810 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 811 | context linordered_euclidean_semiring | 
| 76387 | 812 | begin | 
| 813 | ||
| 814 | subclass semiring_parity | |
| 815 | proof | |
| 79118 | 816 | show \<open>a mod 2 = of_bool (\<not> 2 dvd a)\<close> for a | 
| 817 | proof (cases \<open>2 dvd a\<close>) | |
| 818 | case True | |
| 819 | then show ?thesis | |
| 820 | by (simp add: dvd_eq_mod_eq_0) | |
| 76387 | 821 | next | 
| 79118 | 822 | case False | 
| 76387 | 823 | have eucl: "euclidean_size (a mod 2) = 1" | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 824 | proof (rule Orderings.order_antisym) | 
| 76387 | 825 | show "euclidean_size (a mod 2) \<le> 1" | 
| 826 | using mod_size_less [of 2 a] by simp | |
| 827 | show "1 \<le> euclidean_size (a mod 2)" | |
| 828 | using \<open>\<not> 2 dvd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0) | |
| 829 | qed | |
| 830 | from \<open>\<not> 2 dvd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)" | |
| 831 | by simp | |
| 832 | then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)" | |
| 833 | by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment) | |
| 834 | then have "\<not> 2 dvd euclidean_size a" | |
| 835 | using of_nat_dvd_iff [of 2] by simp | |
| 836 | then have "euclidean_size a mod 2 = 1" | |
| 837 | by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0) | |
| 838 | then have "of_nat (euclidean_size a mod 2) = of_nat 1" | |
| 839 | by simp | |
| 840 | then have "of_nat (euclidean_size a) mod 2 = 1" | |
| 841 | by (simp add: of_nat_mod) | |
| 842 | from \<open>\<not> 2 dvd a\<close> eucl | |
| 79118 | 843 | have "a mod 2 = 1" | 
| 76387 | 844 | by (auto intro: division_segment_eq_iff simp add: division_segment_mod) | 
| 79118 | 845 | with \<open>\<not> 2 dvd a\<close> show ?thesis | 
| 846 | by simp | |
| 76387 | 847 | qed | 
| 79118 | 848 | show \<open>\<not> is_unit 2\<close> | 
| 849 | proof | |
| 850 | assume \<open>is_unit 2\<close> | |
| 851 | then have \<open>of_nat 2 dvd of_nat 1\<close> | |
| 76387 | 852 | by simp | 
| 79118 | 853 | then have \<open>is_unit (2::nat)\<close> | 
| 76387 | 854 | by (simp only: of_nat_dvd_iff) | 
| 855 | then show False | |
| 856 | by simp | |
| 857 | qed | |
| 858 | qed | |
| 859 | ||
| 860 | lemma even_succ_div_two [simp]: | |
| 861 | "even a \<Longrightarrow> (a + 1) div 2 = a div 2" | |
| 862 | by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero) | |
| 863 | ||
| 864 | lemma odd_succ_div_two [simp]: | |
| 865 | "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1" | |
| 866 | by (auto elim!: oddE simp add: add.assoc) | |
| 867 | ||
| 868 | lemma even_two_times_div_two: | |
| 869 | "even a \<Longrightarrow> 2 * (a div 2) = a" | |
| 870 | by (fact dvd_mult_div_cancel) | |
| 871 | ||
| 872 | lemma odd_two_times_div_two_succ [simp]: | |
| 873 | "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a" | |
| 874 | using mult_div_mod_eq [of 2 a] | |
| 875 | by (simp add: even_iff_mod_2_eq_zero) | |
| 876 | ||
| 877 | lemma coprime_left_2_iff_odd [simp]: | |
| 878 | "coprime 2 a \<longleftrightarrow> odd a" | |
| 879 | proof | |
| 880 | assume "odd a" | |
| 881 | show "coprime 2 a" | |
| 882 | proof (rule coprimeI) | |
| 883 | fix b | |
| 884 | assume "b dvd 2" "b dvd a" | |
| 885 | then have "b dvd a mod 2" | |
| 886 | by (auto intro: dvd_mod) | |
| 887 | with \<open>odd a\<close> show "is_unit b" | |
| 888 | by (simp add: mod_2_eq_odd) | |
| 889 | qed | |
| 890 | next | |
| 891 | assume "coprime 2 a" | |
| 892 | show "odd a" | |
| 893 | proof (rule notI) | |
| 894 | assume "even a" | |
| 895 | then obtain b where "a = 2 * b" .. | |
| 896 | with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)" | |
| 897 | by simp | |
| 898 | moreover have "\<not> coprime 2 (2 * b)" | |
| 899 | by (rule not_coprimeI [of 2]) simp_all | |
| 900 | ultimately show False | |
| 901 | by blast | |
| 902 | qed | |
| 903 | qed | |
| 904 | ||
| 905 | lemma coprime_right_2_iff_odd [simp]: | |
| 906 | "coprime a 2 \<longleftrightarrow> odd a" | |
| 907 | using coprime_left_2_iff_odd [of a] by (simp add: ac_simps) | |
| 908 | ||
| 909 | end | |
| 910 | ||
| 911 | lemma minus_1_mod_2_eq [simp]: | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 912 | \<open>- 1 mod 2 = (1::int)\<close> | 
| 76387 | 913 | by (simp add: mod_2_eq_odd) | 
| 914 | ||
| 915 | lemma minus_1_div_2_eq [simp]: | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 916 | "- 1 div 2 = - (1::int)" | 
| 76387 | 917 | proof - | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 918 | from div_mult_mod_eq [of "- 1 :: int" 2] | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 919 | have "- 1 div 2 * 2 = - 1 * (2 :: int)" | 
| 76387 | 920 | using add_implies_diff by fastforce | 
| 921 | then show ?thesis | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 922 | using mult_right_cancel [of 2 "- 1 div 2" "- (1 :: int)"] by simp | 
| 76387 | 923 | qed | 
| 924 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 925 | context linordered_euclidean_semiring | 
| 76387 | 926 | begin | 
| 927 | ||
| 928 | lemma even_mask_div_iff': | |
| 929 | \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> m \<le> n\<close> | |
| 930 | proof - | |
| 931 | have \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> even (of_nat ((2 ^ m - Suc 0) div 2 ^ n))\<close> | |
| 932 | by (simp only: of_nat_div) (simp add: of_nat_diff) | |
| 933 | also have \<open>\<dots> \<longleftrightarrow> even ((2 ^ m - Suc 0) div 2 ^ n)\<close> | |
| 934 | by simp | |
| 935 | also have \<open>\<dots> \<longleftrightarrow> m \<le> n\<close> | |
| 936 | proof (cases \<open>m \<le> n\<close>) | |
| 937 | case True | |
| 938 | then show ?thesis | |
| 939 | by (simp add: Suc_le_lessD) | |
| 940 | next | |
| 941 | case False | |
| 942 | then obtain r where r: \<open>m = n + Suc r\<close> | |
| 943 | using less_imp_Suc_add by fastforce | |
| 944 |     from r have \<open>{q. q < m} \<inter> {q. 2 ^ n dvd (2::nat) ^ q} = {q. n \<le> q \<and> q < m}\<close>
 | |
| 945 | by (auto simp add: dvd_power_iff_le) | |
| 946 |     moreover from r have \<open>{q. q < m} \<inter> {q. \<not> 2 ^ n dvd (2::nat) ^ q} = {q. q < n}\<close>
 | |
| 947 | by (auto simp add: dvd_power_iff_le) | |
| 948 |     moreover from False have \<open>{q. n \<le> q \<and> q < m \<and> q \<le> n} = {n}\<close>
 | |
| 949 | by auto | |
| 950 |     then have \<open>odd ((\<Sum>a\<in>{q. n \<le> q \<and> q < m}. 2 ^ a div (2::nat) ^ n) + sum ((^) 2) {q. q < n} div 2 ^ n)\<close>
 | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 951 | by (simp_all add: euclidean_semiring_cancel_class.power_diff_power_eq semiring_parity_class.even_sum_iff | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 952 | linorder_class.not_less mask_eq_sum_exp_nat [symmetric]) | 
| 76387 | 953 |     ultimately have \<open>odd (sum ((^) (2::nat)) {q. q < m} div 2 ^ n)\<close>
 | 
| 954 | by (subst euclidean_semiring_cancel_class.sum_div_partition) simp_all | |
| 955 | with False show ?thesis | |
| 956 | by (simp add: mask_eq_sum_exp_nat) | |
| 957 | qed | |
| 958 | finally show ?thesis . | |
| 959 | qed | |
| 960 | ||
| 961 | end | |
| 962 | ||
| 963 | ||
| 964 | subsection \<open>Generic symbolic computations\<close> | |
| 965 | ||
| 966 | text \<open> | |
| 967 | The following type class contains everything necessary to formulate | |
| 968 | a division algorithm in ring structures with numerals, restricted | |
| 969 | to its positive segments. | |
| 970 | \<close> | |
| 971 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 972 | class linordered_euclidean_semiring_division = linordered_euclidean_semiring + | 
| 76387 | 973 | fixes divmod :: \<open>num \<Rightarrow> num \<Rightarrow> 'a \<times> 'a\<close> | 
| 974 | and divmod_step :: \<open>'a \<Rightarrow> 'a \<times> 'a \<Rightarrow> 'a \<times> 'a\<close> \<comment> \<open> | |
| 975 | These are conceptually definitions but force generated code | |
| 976 | to be monomorphic wrt. particular instances of this class which | |
| 977 | yields a significant speedup.\<close> | |
| 978 | assumes divmod_def: \<open>divmod m n = (numeral m div numeral n, numeral m mod numeral n)\<close> | |
| 979 | and divmod_step_def [simp]: \<open>divmod_step l (q, r) = | |
| 980 | (if euclidean_size l \<le> euclidean_size r then (2 * q + 1, r - l) | |
| 981 | else (2 * q, r))\<close> \<comment> \<open> | |
| 982 | This is a formulation of one step (referring to one digit position) | |
| 983 | in school-method division: compare the dividend at the current | |
| 984 | digit position with the remainder from previous division steps | |
| 985 | and evaluate accordingly.\<close> | |
| 986 | begin | |
| 987 | ||
| 988 | lemma fst_divmod: | |
| 989 | \<open>fst (divmod m n) = numeral m div numeral n\<close> | |
| 990 | by (simp add: divmod_def) | |
| 991 | ||
| 992 | lemma snd_divmod: | |
| 993 | \<open>snd (divmod m n) = numeral m mod numeral n\<close> | |
| 994 | by (simp add: divmod_def) | |
| 995 | ||
| 996 | text \<open> | |
| 997 | Following a formulation of school-method division. | |
| 998 | If the divisor is smaller than the dividend, terminate. | |
| 999 | If not, shift the dividend to the right until termination | |
| 1000 | occurs and then reiterate single division steps in the | |
| 1001 | opposite direction. | |
| 1002 | \<close> | |
| 1003 | ||
| 1004 | lemma divmod_divmod_step: | |
| 1005 | \<open>divmod m n = (if m < n then (0, numeral m) | |
| 1006 | else divmod_step (numeral n) (divmod m (Num.Bit0 n)))\<close> | |
| 1007 | proof (cases \<open>m < n\<close>) | |
| 1008 | case True | |
| 1009 | then show ?thesis | |
| 1010 | by (simp add: prod_eq_iff fst_divmod snd_divmod flip: of_nat_numeral of_nat_div of_nat_mod) | |
| 1011 | next | |
| 1012 | case False | |
| 1013 | define r s t where \<open>r = (numeral m :: nat)\<close> \<open>s = (numeral n :: nat)\<close> \<open>t = 2 * s\<close> | |
| 1014 | then have *: \<open>numeral m = of_nat r\<close> \<open>numeral n = of_nat s\<close> \<open>numeral (num.Bit0 n) = of_nat t\<close> | |
| 1015 | and \<open>\<not> s \<le> r mod s\<close> | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1016 | by (simp_all add: linorder_class.not_le) | 
| 76387 | 1017 | have t: \<open>2 * (r div t) = r div s - r div s mod 2\<close> | 
| 1018 | \<open>r mod t = s * (r div s mod 2) + r mod s\<close> | |
| 77061 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76387diff
changeset | 1019 | by (simp add: Rings.minus_mod_eq_mult_div Groups.mult.commute [of 2] Euclidean_Rings.div_mult2_eq \<open>t = 2 * s\<close>) | 
| 76387 | 1020 | (use mod_mult2_eq [of r s 2] in \<open>simp add: ac_simps \<open>t = 2 * s\<close>\<close>) | 
| 1021 | have rs: \<open>r div s mod 2 = 0 \<or> r div s mod 2 = Suc 0\<close> | |
| 1022 | by auto | |
| 1023 | from \<open>\<not> s \<le> r mod s\<close> have \<open>s \<le> r mod t \<Longrightarrow> | |
| 1024 | r div s = Suc (2 * (r div t)) \<and> | |
| 1025 | r mod s = r mod t - s\<close> | |
| 1026 | using rs | |
| 1027 | by (auto simp add: t) | |
| 1028 | moreover have \<open>r mod t < s \<Longrightarrow> | |
| 1029 | r div s = 2 * (r div t) \<and> | |
| 1030 | r mod s = r mod t\<close> | |
| 1031 | using rs | |
| 1032 | by (auto simp add: t) | |
| 1033 | ultimately show ?thesis | |
| 1034 | by (simp add: divmod_def prod_eq_iff split_def Let_def | |
| 1035 | not_less mod_eq_0_iff_dvd Rings.mod_eq_0_iff_dvd False not_le *) | |
| 1036 | (simp add: flip: of_nat_numeral of_nat_mult add.commute [of 1] of_nat_div of_nat_mod of_nat_Suc of_nat_diff) | |
| 1037 | qed | |
| 1038 | ||
| 1039 | text \<open>The division rewrite proper -- first, trivial results involving \<open>1\<close>\<close> | |
| 1040 | ||
| 1041 | lemma divmod_trivial [simp]: | |
| 1042 | "divmod m Num.One = (numeral m, 0)" | |
| 1043 | "divmod num.One (num.Bit0 n) = (0, Numeral1)" | |
| 1044 | "divmod num.One (num.Bit1 n) = (0, Numeral1)" | |
| 1045 | using divmod_divmod_step [of "Num.One"] by (simp_all add: divmod_def) | |
| 1046 | ||
| 1047 | text \<open>Division by an even number is a right-shift\<close> | |
| 1048 | ||
| 1049 | lemma divmod_cancel [simp]: | |
| 1050 | \<open>divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))\<close> (is ?P) | |
| 1051 | \<open>divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))\<close> (is ?Q) | |
| 1052 | proof - | |
| 1053 | define r s where \<open>r = (numeral m :: nat)\<close> \<open>s = (numeral n :: nat)\<close> | |
| 1054 | then have *: \<open>numeral m = of_nat r\<close> \<open>numeral n = of_nat s\<close> | |
| 1055 | \<open>numeral (num.Bit0 m) = of_nat (2 * r)\<close> \<open>numeral (num.Bit0 n) = of_nat (2 * s)\<close> | |
| 1056 | \<open>numeral (num.Bit1 m) = of_nat (Suc (2 * r))\<close> | |
| 1057 | by simp_all | |
| 1058 | have **: \<open>Suc (2 * r) div 2 = r\<close> | |
| 1059 | by simp | |
| 1060 | show ?P and ?Q | |
| 1061 | by (simp_all add: divmod_def *) | |
| 1062 | (simp_all flip: of_nat_numeral of_nat_div of_nat_mod of_nat_mult add.commute [of 1] of_nat_Suc | |
| 77061 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76387diff
changeset | 1063 | add: Euclidean_Rings.mod_mult_mult1 div_mult2_eq [of _ 2] mod_mult2_eq [of _ 2] **) | 
| 76387 | 1064 | qed | 
| 1065 | ||
| 1066 | text \<open>The really hard work\<close> | |
| 1067 | ||
| 1068 | lemma divmod_steps [simp]: | |
| 1069 | "divmod (num.Bit0 m) (num.Bit1 n) = | |
| 1070 | (if m \<le> n then (0, numeral (num.Bit0 m)) | |
| 1071 | else divmod_step (numeral (num.Bit1 n)) | |
| 1072 | (divmod (num.Bit0 m) | |
| 1073 | (num.Bit0 (num.Bit1 n))))" | |
| 1074 | "divmod (num.Bit1 m) (num.Bit1 n) = | |
| 1075 | (if m < n then (0, numeral (num.Bit1 m)) | |
| 1076 | else divmod_step (numeral (num.Bit1 n)) | |
| 1077 | (divmod (num.Bit1 m) | |
| 1078 | (num.Bit0 (num.Bit1 n))))" | |
| 1079 | by (simp_all add: divmod_divmod_step) | |
| 1080 | ||
| 1081 | lemmas divmod_algorithm_code = divmod_trivial divmod_cancel divmod_steps | |
| 1082 | ||
| 1083 | text \<open>Special case: divisibility\<close> | |
| 1084 | ||
| 1085 | definition divides_aux :: "'a \<times> 'a \<Rightarrow> bool" | |
| 1086 | where | |
| 1087 | "divides_aux qr \<longleftrightarrow> snd qr = 0" | |
| 1088 | ||
| 1089 | lemma divides_aux_eq [simp]: | |
| 1090 | "divides_aux (q, r) \<longleftrightarrow> r = 0" | |
| 1091 | by (simp add: divides_aux_def) | |
| 1092 | ||
| 1093 | lemma dvd_numeral_simp [simp]: | |
| 1094 | "numeral m dvd numeral n \<longleftrightarrow> divides_aux (divmod n m)" | |
| 1095 | by (simp add: divmod_def mod_eq_0_iff_dvd) | |
| 1096 | ||
| 1097 | text \<open>Generic computation of quotient and remainder\<close> | |
| 1098 | ||
| 1099 | lemma numeral_div_numeral [simp]: | |
| 1100 | "numeral k div numeral l = fst (divmod k l)" | |
| 1101 | by (simp add: fst_divmod) | |
| 1102 | ||
| 1103 | lemma numeral_mod_numeral [simp]: | |
| 1104 | "numeral k mod numeral l = snd (divmod k l)" | |
| 1105 | by (simp add: snd_divmod) | |
| 1106 | ||
| 1107 | lemma one_div_numeral [simp]: | |
| 1108 | "1 div numeral n = fst (divmod num.One n)" | |
| 1109 | by (simp add: fst_divmod) | |
| 1110 | ||
| 1111 | lemma one_mod_numeral [simp]: | |
| 1112 | "1 mod numeral n = snd (divmod num.One n)" | |
| 1113 | by (simp add: snd_divmod) | |
| 1114 | ||
| 1115 | end | |
| 1116 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1117 | instantiation nat :: linordered_euclidean_semiring_division | 
| 76387 | 1118 | begin | 
| 1119 | ||
| 1120 | definition divmod_nat :: "num \<Rightarrow> num \<Rightarrow> nat \<times> nat" | |
| 1121 | where | |
| 1122 | divmod'_nat_def: "divmod_nat m n = (numeral m div numeral n, numeral m mod numeral n)" | |
| 1123 | ||
| 1124 | definition divmod_step_nat :: "nat \<Rightarrow> nat \<times> nat \<Rightarrow> nat \<times> nat" | |
| 1125 | where | |
| 1126 | "divmod_step_nat l qr = (let (q, r) = qr | |
| 1127 | in if r \<ge> l then (2 * q + 1, r - l) | |
| 1128 | else (2 * q, r))" | |
| 1129 | ||
| 1130 | instance | |
| 1131 | by standard (simp_all add: divmod'_nat_def divmod_step_nat_def) | |
| 1132 | ||
| 1133 | end | |
| 1134 | ||
| 1135 | declare divmod_algorithm_code [where ?'a = nat, code] | |
| 1136 | ||
| 1137 | lemma Suc_0_div_numeral [simp]: | |
| 1138 | \<open>Suc 0 div numeral Num.One = 1\<close> | |
| 1139 | \<open>Suc 0 div numeral (Num.Bit0 n) = 0\<close> | |
| 1140 | \<open>Suc 0 div numeral (Num.Bit1 n) = 0\<close> | |
| 1141 | by simp_all | |
| 1142 | ||
| 1143 | lemma Suc_0_mod_numeral [simp]: | |
| 1144 | \<open>Suc 0 mod numeral Num.One = 0\<close> | |
| 1145 | \<open>Suc 0 mod numeral (Num.Bit0 n) = 1\<close> | |
| 1146 | \<open>Suc 0 mod numeral (Num.Bit1 n) = 1\<close> | |
| 1147 | by simp_all | |
| 1148 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1149 | instantiation int :: linordered_euclidean_semiring_division | 
| 76387 | 1150 | begin | 
| 1151 | ||
| 1152 | definition divmod_int :: "num \<Rightarrow> num \<Rightarrow> int \<times> int" | |
| 1153 | where | |
| 1154 | "divmod_int m n = (numeral m div numeral n, numeral m mod numeral n)" | |
| 1155 | ||
| 1156 | definition divmod_step_int :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" | |
| 1157 | where | |
| 1158 | "divmod_step_int l qr = (let (q, r) = qr | |
| 1159 | in if \<bar>l\<bar> \<le> \<bar>r\<bar> then (2 * q + 1, r - l) | |
| 1160 | else (2 * q, r))" | |
| 1161 | ||
| 1162 | instance | |
| 1163 | by standard (auto simp add: divmod_int_def divmod_step_int_def) | |
| 1164 | ||
| 1165 | end | |
| 1166 | ||
| 1167 | declare divmod_algorithm_code [where ?'a = int, code] | |
| 1168 | ||
| 1169 | context | |
| 1170 | begin | |
| 1171 | ||
| 1172 | qualified definition adjust_div :: "int \<times> int \<Rightarrow> int" | |
| 1173 | where | |
| 1174 | "adjust_div qr = (let (q, r) = qr in q + of_bool (r \<noteq> 0))" | |
| 1175 | ||
| 1176 | qualified lemma adjust_div_eq [simp, code]: | |
| 1177 | "adjust_div (q, r) = q + of_bool (r \<noteq> 0)" | |
| 1178 | by (simp add: adjust_div_def) | |
| 1179 | ||
| 1180 | qualified definition adjust_mod :: "num \<Rightarrow> int \<Rightarrow> int" | |
| 1181 | where | |
| 1182 | [simp]: "adjust_mod l r = (if r = 0 then 0 else numeral l - r)" | |
| 1183 | ||
| 1184 | lemma minus_numeral_div_numeral [simp]: | |
| 1185 | "- numeral m div numeral n = - (adjust_div (divmod m n) :: int)" | |
| 1186 | proof - | |
| 1187 | have "int (fst (divmod m n)) = fst (divmod m n)" | |
| 1188 | by (simp only: fst_divmod divide_int_def) auto | |
| 1189 | then show ?thesis | |
| 1190 | by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def) | |
| 1191 | qed | |
| 1192 | ||
| 1193 | lemma minus_numeral_mod_numeral [simp]: | |
| 1194 | "- numeral m mod numeral n = adjust_mod n (snd (divmod m n) :: int)" | |
| 1195 | proof (cases "snd (divmod m n) = (0::int)") | |
| 1196 | case True | |
| 1197 | then show ?thesis | |
| 1198 | by (simp add: mod_eq_0_iff_dvd divides_aux_def) | |
| 1199 | next | |
| 1200 | case False | |
| 1201 | then have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)" | |
| 1202 | by (simp only: snd_divmod modulo_int_def) auto | |
| 1203 | then show ?thesis | |
| 1204 | by (simp add: divides_aux_def adjust_div_def) | |
| 1205 | (simp add: divides_aux_def modulo_int_def) | |
| 1206 | qed | |
| 1207 | ||
| 1208 | lemma numeral_div_minus_numeral [simp]: | |
| 1209 | "numeral m div - numeral n = - (adjust_div (divmod m n) :: int)" | |
| 1210 | proof - | |
| 1211 | have "int (fst (divmod m n)) = fst (divmod m n)" | |
| 1212 | by (simp only: fst_divmod divide_int_def) auto | |
| 1213 | then show ?thesis | |
| 1214 | by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def) | |
| 1215 | qed | |
| 1216 | ||
| 1217 | lemma numeral_mod_minus_numeral [simp]: | |
| 1218 | "numeral m mod - numeral n = - adjust_mod n (snd (divmod m n) :: int)" | |
| 1219 | proof (cases "snd (divmod m n) = (0::int)") | |
| 1220 | case True | |
| 1221 | then show ?thesis | |
| 1222 | by (simp add: mod_eq_0_iff_dvd divides_aux_def) | |
| 1223 | next | |
| 1224 | case False | |
| 1225 | then have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)" | |
| 1226 | by (simp only: snd_divmod modulo_int_def) auto | |
| 1227 | then show ?thesis | |
| 1228 | by (simp add: divides_aux_def adjust_div_def) | |
| 1229 | (simp add: divides_aux_def modulo_int_def) | |
| 1230 | qed | |
| 1231 | ||
| 1232 | lemma minus_one_div_numeral [simp]: | |
| 1233 | "- 1 div numeral n = - (adjust_div (divmod Num.One n) :: int)" | |
| 1234 | using minus_numeral_div_numeral [of Num.One n] by simp | |
| 1235 | ||
| 1236 | lemma minus_one_mod_numeral [simp]: | |
| 1237 | "- 1 mod numeral n = adjust_mod n (snd (divmod Num.One n) :: int)" | |
| 1238 | using minus_numeral_mod_numeral [of Num.One n] by simp | |
| 1239 | ||
| 1240 | lemma one_div_minus_numeral [simp]: | |
| 1241 | "1 div - numeral n = - (adjust_div (divmod Num.One n) :: int)" | |
| 1242 | using numeral_div_minus_numeral [of Num.One n] by simp | |
| 1243 | ||
| 1244 | lemma one_mod_minus_numeral [simp]: | |
| 1245 | "1 mod - numeral n = - adjust_mod n (snd (divmod Num.One n) :: int)" | |
| 1246 | using numeral_mod_minus_numeral [of Num.One n] by simp | |
| 1247 | ||
| 1248 | lemma [code]: | |
| 1249 | fixes k :: int | |
| 1250 | shows | |
| 1251 | "k div 0 = 0" | |
| 1252 | "k mod 0 = k" | |
| 1253 | "0 div k = 0" | |
| 1254 | "0 mod k = 0" | |
| 1255 | "k div Int.Pos Num.One = k" | |
| 1256 | "k mod Int.Pos Num.One = 0" | |
| 1257 | "k div Int.Neg Num.One = - k" | |
| 1258 | "k mod Int.Neg Num.One = 0" | |
| 1259 | "Int.Pos m div Int.Pos n = (fst (divmod m n) :: int)" | |
| 1260 | "Int.Pos m mod Int.Pos n = (snd (divmod m n) :: int)" | |
| 1261 | "Int.Neg m div Int.Pos n = - (adjust_div (divmod m n) :: int)" | |
| 1262 | "Int.Neg m mod Int.Pos n = adjust_mod n (snd (divmod m n) :: int)" | |
| 1263 | "Int.Pos m div Int.Neg n = - (adjust_div (divmod m n) :: int)" | |
| 1264 | "Int.Pos m mod Int.Neg n = - adjust_mod n (snd (divmod m n) :: int)" | |
| 1265 | "Int.Neg m div Int.Neg n = (fst (divmod m n) :: int)" | |
| 1266 | "Int.Neg m mod Int.Neg n = - (snd (divmod m n) :: int)" | |
| 1267 | by simp_all | |
| 1268 | ||
| 1269 | end | |
| 1270 | ||
| 1271 | lemma divmod_BitM_2_eq [simp]: | |
| 1272 | \<open>divmod (Num.BitM m) (Num.Bit0 Num.One) = (numeral m - 1, (1 :: int))\<close> | |
| 1273 | by (cases m) simp_all | |
| 1274 | ||
| 1275 | ||
| 1276 | subsubsection \<open>Computation by simplification\<close> | |
| 1277 | ||
| 1278 | lemma euclidean_size_nat_less_eq_iff: | |
| 1279 | \<open>euclidean_size m \<le> euclidean_size n \<longleftrightarrow> m \<le> n\<close> for m n :: nat | |
| 1280 | by simp | |
| 1281 | ||
| 1282 | lemma euclidean_size_int_less_eq_iff: | |
| 1283 | \<open>euclidean_size k \<le> euclidean_size l \<longleftrightarrow> \<bar>k\<bar> \<le> \<bar>l\<bar>\<close> for k l :: int | |
| 1284 | by auto | |
| 1285 | ||
| 1286 | simproc_setup numeral_divmod | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1287 |   ("0 div 0 :: 'a :: linordered_euclidean_semiring_division" | "0 mod 0 :: 'a :: linordered_euclidean_semiring_division" |
 | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1288 | "0 div 1 :: 'a :: linordered_euclidean_semiring_division" | "0 mod 1 :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1289 | "0 div - 1 :: int" | "0 mod - 1 :: int" | | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1290 | "0 div numeral b :: 'a :: linordered_euclidean_semiring_division" | "0 mod numeral b :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1291 | "0 div - numeral b :: int" | "0 mod - numeral b :: int" | | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1292 | "1 div 0 :: 'a :: linordered_euclidean_semiring_division" | "1 mod 0 :: 'a :: linordered_euclidean_semiring_division" | | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1293 | "1 div 1 :: 'a :: linordered_euclidean_semiring_division" | "1 mod 1 :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1294 | "1 div - 1 :: int" | "1 mod - 1 :: int" | | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1295 | "1 div numeral b :: 'a :: linordered_euclidean_semiring_division" | "1 mod numeral b :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1296 | "1 div - numeral b :: int" |"1 mod - numeral b :: int" | | 
| 1297 | "- 1 div 0 :: int" | "- 1 mod 0 :: int" | "- 1 div 1 :: int" | "- 1 mod 1 :: int" | | |
| 1298 | "- 1 div - 1 :: int" | "- 1 mod - 1 :: int" | "- 1 div numeral b :: int" | "- 1 mod numeral b :: int" | | |
| 1299 | "- 1 div - numeral b :: int" | "- 1 mod - numeral b :: int" | | |
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1300 | "numeral a div 0 :: 'a :: linordered_euclidean_semiring_division" | "numeral a mod 0 :: 'a :: linordered_euclidean_semiring_division" | | 
| 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1301 | "numeral a div 1 :: 'a :: linordered_euclidean_semiring_division" | "numeral a mod 1 :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1302 | "numeral a div - 1 :: int" | "numeral a mod - 1 :: int" | | 
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1303 | "numeral a div numeral b :: 'a :: linordered_euclidean_semiring_division" | "numeral a mod numeral b :: 'a :: linordered_euclidean_semiring_division" | | 
| 76387 | 1304 | "numeral a div - numeral b :: int" | "numeral a mod - numeral b :: int" | | 
| 1305 | "- numeral a div 0 :: int" | "- numeral a mod 0 :: int" | | |
| 1306 | "- numeral a div 1 :: int" | "- numeral a mod 1 :: int" | | |
| 1307 | "- numeral a div - 1 :: int" | "- numeral a mod - 1 :: int" | | |
| 1308 | "- numeral a div numeral b :: int" | "- numeral a mod numeral b :: int" | | |
| 1309 | "- numeral a div - numeral b :: int" | "- numeral a mod - numeral b :: int") = \<open> | |
| 1310 | let | |
| 1311 | val if_cong = the (Code.get_case_cong \<^theory> \<^const_name>\<open>If\<close>); | |
| 1312 | fun successful_rewrite ctxt ct = | |
| 1313 | let | |
| 1314 | val thm = Simplifier.rewrite ctxt ct | |
| 1315 | in if Thm.is_reflexive thm then NONE else SOME thm end; | |
| 78082 | 1316 |     val simps = @{thms div_0 mod_0 div_by_0 mod_by_0 div_by_1 mod_by_1
 | 
| 1317 | one_div_numeral one_mod_numeral minus_one_div_numeral minus_one_mod_numeral | |
| 1318 | one_div_minus_numeral one_mod_minus_numeral | |
| 1319 | numeral_div_numeral numeral_mod_numeral minus_numeral_div_numeral minus_numeral_mod_numeral | |
| 1320 | numeral_div_minus_numeral numeral_mod_minus_numeral | |
| 1321 | div_minus_minus mod_minus_minus Parity.adjust_div_eq of_bool_eq one_neq_zero | |
| 1322 | numeral_neq_zero neg_equal_0_iff_equal arith_simps arith_special divmod_trivial | |
| 1323 | divmod_cancel divmod_steps divmod_step_def fst_conv snd_conv numeral_One | |
| 1324 | case_prod_beta rel_simps Parity.adjust_mod_def div_minus1_right mod_minus1_right | |
| 1325 | minus_minus numeral_times_numeral mult_zero_right mult_1_right | |
| 1326 | euclidean_size_nat_less_eq_iff euclidean_size_int_less_eq_iff diff_nat_numeral nat_numeral} | |
| 1327 |       @ [@{lemma "0 = 0 \<longleftrightarrow> True" by simp}];
 | |
| 78083 | 1328 | val simpset = | 
| 1329 | HOL_ss |> Simplifier.simpset_map \<^context> | |
| 1330 | (Simplifier.add_cong if_cong #> fold Simplifier.add_simp simps); | |
| 1331 | in K (fn ctxt => successful_rewrite (Simplifier.put_simpset simpset ctxt)) end | |
| 76387 | 1332 | \<close> \<comment> \<open> | 
| 1333 | There is space for improvement here: the calculation itself | |
| 1334 | could be carried out outside the logic, and a generic simproc | |
| 1335 | (simplifier setup) for generic calculation would be helpful. | |
| 1336 | \<close> | |
| 1337 | ||
| 1338 | ||
| 75937 | 1339 | subsection \<open>Computing congruences modulo \<open>2 ^ q\<close>\<close> | 
| 1340 | ||
| 78937 
5e6b195eee83
slightly less technical formulation of very specific type class
 haftmann parents: 
78668diff
changeset | 1341 | context linordered_euclidean_semiring_division | 
| 75937 | 1342 | begin | 
| 1343 | ||
| 1344 | lemma cong_exp_iff_simps: | |
| 1345 | "numeral n mod numeral Num.One = 0 | |
| 1346 | \<longleftrightarrow> True" | |
| 1347 | "numeral (Num.Bit0 n) mod numeral (Num.Bit0 q) = 0 | |
| 1348 | \<longleftrightarrow> numeral n mod numeral q = 0" | |
| 1349 | "numeral (Num.Bit1 n) mod numeral (Num.Bit0 q) = 0 | |
| 1350 | \<longleftrightarrow> False" | |
| 1351 | "numeral m mod numeral Num.One = (numeral n mod numeral Num.One) | |
| 1352 | \<longleftrightarrow> True" | |
| 1353 | "numeral Num.One mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) | |
| 1354 | \<longleftrightarrow> True" | |
| 1355 | "numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) | |
| 1356 | \<longleftrightarrow> False" | |
| 1357 | "numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) | |
| 1358 | \<longleftrightarrow> (numeral n mod numeral q) = 0" | |
| 1359 | "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) | |
| 1360 | \<longleftrightarrow> False" | |
| 1361 | "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) | |
| 1362 | \<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)" | |
| 1363 | "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) | |
| 1364 | \<longleftrightarrow> False" | |
| 1365 | "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q)) | |
| 1366 | \<longleftrightarrow> (numeral m mod numeral q) = 0" | |
| 1367 | "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q)) | |
| 1368 | \<longleftrightarrow> False" | |
| 1369 | "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q)) | |
| 1370 | \<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)" | |
| 1371 | by (auto simp add: case_prod_beta dest: arg_cong [of _ _ even]) | |
| 1372 | ||
| 1373 | end | |
| 1374 | ||
| 1375 | ||
| 71853 | 1376 | code_identifier | 
| 1377 | code_module Parity \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith | |
| 1378 | ||
| 74592 | 1379 | lemmas even_of_nat = even_of_nat_iff | 
| 1380 | ||
| 67816 | 1381 | end |