| author | wenzelm | 
| Sun, 26 Mar 2023 15:02:08 +0200 | |
| changeset 77714 | be0b9396604e | 
| parent 77225 | b6f3eb537d91 | 
| child 77812 | fb3d81bd9803 | 
| permissions | -rw-r--r-- | 
| 72515 
c7038c397ae3
moved most material from session HOL-Word to Word_Lib in the AFP
 haftmann parents: 
72512diff
changeset | 1 | (* Title: HOL/Library/Word.thy | 
| 
c7038c397ae3
moved most material from session HOL-Word to Word_Lib in the AFP
 haftmann parents: 
72512diff
changeset | 2 | Author: Jeremy Dawson and Gerwin Klein, NICTA, et. al. | 
| 24333 | 3 | *) | 
| 4 | ||
| 61799 | 5 | section \<open>A type of finite bit strings\<close> | 
| 24350 | 6 | |
| 29628 | 7 | theory Word | 
| 41413 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41060diff
changeset | 8 | imports | 
| 66453 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
65363diff
changeset | 9 | "HOL-Library.Type_Length" | 
| 37660 | 10 | begin | 
| 11 | ||
| 72243 | 12 | subsection \<open>Preliminaries\<close> | 
| 13 | ||
| 14 | lemma signed_take_bit_decr_length_iff: | |
| 15 |   \<open>signed_take_bit (LENGTH('a::len) - Suc 0) k = signed_take_bit (LENGTH('a) - Suc 0) l
 | |
| 16 |     \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
 | |
| 17 |   by (cases \<open>LENGTH('a)\<close>)
 | |
| 18 | (simp_all add: signed_take_bit_eq_iff_take_bit_eq) | |
| 19 | ||
| 20 | ||
| 72244 | 21 | subsection \<open>Fundamentals\<close> | 
| 22 | ||
| 23 | subsubsection \<open>Type definition\<close> | |
| 37660 | 24 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 25 | quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
 | 
| 72243 | 26 | morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI) | 
| 27 | ||
| 28 | hide_const (open) rep \<comment> \<open>only for foundational purpose\<close> | |
| 72130 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 29 | hide_const (open) Word \<comment> \<open>only for code generation\<close> | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 30 | |
| 72244 | 31 | |
| 32 | subsubsection \<open>Basic arithmetic\<close> | |
| 33 | ||
| 72243 | 34 | instantiation word :: (len) comm_ring_1 | 
| 35 | begin | |
| 36 | ||
| 37 | lift_definition zero_word :: \<open>'a word\<close> | |
| 38 | is 0 . | |
| 39 | ||
| 40 | lift_definition one_word :: \<open>'a word\<close> | |
| 41 | is 1 . | |
| 42 | ||
| 43 | lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 44 | is \<open>(+)\<close> | |
| 45 | by (auto simp add: take_bit_eq_mod intro: mod_add_cong) | |
| 46 | ||
| 47 | lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 48 | is \<open>(-)\<close> | |
| 49 | by (auto simp add: take_bit_eq_mod intro: mod_diff_cong) | |
| 50 | ||
| 51 | lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close> | |
| 52 | is uminus | |
| 53 | by (auto simp add: take_bit_eq_mod intro: mod_minus_cong) | |
| 54 | ||
| 55 | lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 56 | is \<open>(*)\<close> | |
| 57 | by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) | |
| 58 | ||
| 59 | instance | |
| 60 | by (standard; transfer) (simp_all add: algebra_simps) | |
| 61 | ||
| 62 | end | |
| 63 | ||
| 64 | context | |
| 65 | includes lifting_syntax | |
| 72244 | 66 | notes | 
| 67 | power_transfer [transfer_rule] | |
| 68 | transfer_rule_of_bool [transfer_rule] | |
| 69 | transfer_rule_numeral [transfer_rule] | |
| 70 | transfer_rule_of_nat [transfer_rule] | |
| 71 | transfer_rule_of_int [transfer_rule] | |
| 72243 | 72 | begin | 
| 73 | ||
| 74 | lemma power_transfer_word [transfer_rule]: | |
| 75 | \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close> | |
| 76 | by transfer_prover | |
| 77 | ||
| 72244 | 78 | lemma [transfer_rule]: | 
| 79 | \<open>((=) ===> pcr_word) of_bool of_bool\<close> | |
| 80 | by transfer_prover | |
| 81 | ||
| 82 | lemma [transfer_rule]: | |
| 83 | \<open>((=) ===> pcr_word) numeral numeral\<close> | |
| 84 | by transfer_prover | |
| 85 | ||
| 86 | lemma [transfer_rule]: | |
| 87 | \<open>((=) ===> pcr_word) int of_nat\<close> | |
| 88 | by transfer_prover | |
| 89 | ||
| 90 | lemma [transfer_rule]: | |
| 91 | \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close> | |
| 92 | proof - | |
| 93 | have \<open>((=) ===> pcr_word) of_int of_int\<close> | |
| 94 | by transfer_prover | |
| 95 | then show ?thesis by (simp add: id_def) | |
| 96 | qed | |
| 97 | ||
| 98 | lemma [transfer_rule]: | |
| 99 | \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close> | |
| 100 | proof - | |
| 101 |   have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
 | |
| 102 | for k :: int | |
| 103 | proof | |
| 104 | assume ?P | |
| 105 | then show ?Q | |
| 106 | by auto | |
| 107 | next | |
| 108 | assume ?Q | |
| 109 |     then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
 | |
| 110 |     then have "even (take_bit LENGTH('a) k)"
 | |
| 111 | by simp | |
| 112 | then show ?P | |
| 113 | by simp | |
| 114 | qed | |
| 115 | show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def]) | |
| 116 | transfer_prover | |
| 117 | qed | |
| 118 | ||
| 72243 | 119 | end | 
| 120 | ||
| 72512 | 121 | lemma exp_eq_zero_iff [simp]: | 
| 122 |   \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
 | |
| 73535 | 123 | by transfer auto | 
| 72512 | 124 | |
| 72244 | 125 | lemma word_exp_length_eq_0 [simp]: | 
| 126 |   \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
 | |
| 72512 | 127 | by simp | 
| 72262 | 128 | |
| 72244 | 129 | |
| 72489 | 130 | subsubsection \<open>Basic tool setup\<close> | 
| 131 | ||
| 132 | ML_file \<open>Tools/word_lib.ML\<close> | |
| 133 | ||
| 134 | ||
| 72244 | 135 | subsubsection \<open>Basic code generation setup\<close> | 
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 136 | |
| 72262 | 137 | context | 
| 138 | begin | |
| 139 | ||
| 140 | qualified lift_definition the_int :: \<open>'a::len word \<Rightarrow> int\<close> | |
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 141 |   is \<open>take_bit LENGTH('a)\<close> .
 | 
| 37660 | 142 | |
| 72262 | 143 | end | 
| 144 | ||
| 72243 | 145 | lemma [code abstype]: | 
| 72262 | 146 | \<open>Word.Word (Word.the_int w) = w\<close> | 
| 72243 | 147 | by transfer simp | 
| 148 | ||
| 72262 | 149 | lemma Word_eq_word_of_int [code_post, simp]: | 
| 150 | \<open>Word.Word = of_int\<close> | |
| 151 | by (rule; transfer) simp | |
| 152 | ||
| 72243 | 153 | quickcheck_generator word | 
| 154 | constructors: | |
| 155 | \<open>0 :: 'a::len word\<close>, | |
| 156 | \<open>numeral :: num \<Rightarrow> 'a::len word\<close> | |
| 157 | ||
| 158 | instantiation word :: (len) equal | |
| 159 | begin | |
| 160 | ||
| 161 | lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close> | |
| 162 |   is \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
 | |
| 163 | by simp | |
| 164 | ||
| 165 | instance | |
| 166 | by (standard; transfer) rule | |
| 167 | ||
| 168 | end | |
| 169 | ||
| 170 | lemma [code]: | |
| 72262 | 171 | \<open>HOL.equal v w \<longleftrightarrow> HOL.equal (Word.the_int v) (Word.the_int w)\<close> | 
| 72243 | 172 | by transfer (simp add: equal) | 
| 173 | ||
| 174 | lemma [code]: | |
| 72262 | 175 | \<open>Word.the_int 0 = 0\<close> | 
| 72243 | 176 | by transfer simp | 
| 177 | ||
| 178 | lemma [code]: | |
| 72262 | 179 | \<open>Word.the_int 1 = 1\<close> | 
| 72243 | 180 | by transfer simp | 
| 181 | ||
| 182 | lemma [code]: | |
| 72262 | 183 |   \<open>Word.the_int (v + w) = take_bit LENGTH('a) (Word.the_int v + Word.the_int w)\<close>
 | 
| 72243 | 184 | for v w :: \<open>'a::len word\<close> | 
| 185 | by transfer (simp add: take_bit_add) | |
| 186 | ||
| 187 | lemma [code]: | |
| 72262 | 188 |   \<open>Word.the_int (- w) = (let k = Word.the_int w in if w = 0 then 0 else 2 ^ LENGTH('a) - k)\<close>
 | 
| 72243 | 189 | for w :: \<open>'a::len word\<close> | 
| 190 | by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if) | |
| 191 | ||
| 192 | lemma [code]: | |
| 72262 | 193 |   \<open>Word.the_int (v - w) = take_bit LENGTH('a) (Word.the_int v - Word.the_int w)\<close>
 | 
| 72243 | 194 | for v w :: \<open>'a::len word\<close> | 
| 195 | by transfer (simp add: take_bit_diff) | |
| 196 | ||
| 197 | lemma [code]: | |
| 72262 | 198 |   \<open>Word.the_int (v * w) = take_bit LENGTH('a) (Word.the_int v * Word.the_int w)\<close>
 | 
| 72243 | 199 | for v w :: \<open>'a::len word\<close> | 
| 200 | by transfer (simp add: take_bit_mult) | |
| 201 | ||
| 202 | ||
| 72244 | 203 | subsubsection \<open>Basic conversions\<close> | 
| 70185 | 204 | |
| 72262 | 205 | abbreviation word_of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close> | 
| 206 | where \<open>word_of_nat \<equiv> of_nat\<close> | |
| 207 | ||
| 208 | abbreviation word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close> | |
| 209 | where \<open>word_of_int \<equiv> of_int\<close> | |
| 210 | ||
| 211 | lemma word_of_nat_eq_iff: | |
| 212 |   \<open>word_of_nat m = (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
 | |
| 213 | by transfer (simp add: take_bit_of_nat) | |
| 214 | ||
| 215 | lemma word_of_int_eq_iff: | |
| 216 |   \<open>word_of_int k = (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
 | |
| 217 | by transfer rule | |
| 218 | ||
| 74496 | 219 | lemma word_of_nat_eq_0_iff: | 
| 72262 | 220 |   \<open>word_of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
 | 
| 221 | using word_of_nat_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff) | |
| 222 | ||
| 74496 | 223 | lemma word_of_int_eq_0_iff: | 
| 72262 | 224 |   \<open>word_of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
 | 
| 225 | using word_of_int_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff) | |
| 226 | ||
| 227 | context semiring_1 | |
| 228 | begin | |
| 229 | ||
| 230 | lift_definition unsigned :: \<open>'b::len word \<Rightarrow> 'a\<close> | |
| 231 |   is \<open>of_nat \<circ> nat \<circ> take_bit LENGTH('b)\<close>
 | |
| 72244 | 232 | by simp | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 233 | |
| 72262 | 234 | lemma unsigned_0 [simp]: | 
| 235 | \<open>unsigned 0 = 0\<close> | |
| 236 | by transfer simp | |
| 237 | ||
| 238 | lemma unsigned_1 [simp]: | |
| 239 | \<open>unsigned 1 = 1\<close> | |
| 240 | by transfer simp | |
| 241 | ||
| 242 | lemma unsigned_numeral [simp]: | |
| 243 |   \<open>unsigned (numeral n :: 'b::len word) = of_nat (take_bit LENGTH('b) (numeral n))\<close>
 | |
| 244 | by transfer (simp add: nat_take_bit_eq) | |
| 245 | ||
| 246 | lemma unsigned_neg_numeral [simp]: | |
| 247 |   \<open>unsigned (- numeral n :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) (- numeral n)))\<close>
 | |
| 248 | by transfer simp | |
| 249 | ||
| 250 | end | |
| 251 | ||
| 252 | context semiring_1 | |
| 253 | begin | |
| 254 | ||
| 74496 | 255 | lemma unsigned_of_nat: | 
| 72262 | 256 |   \<open>unsigned (word_of_nat n :: 'b::len word) = of_nat (take_bit LENGTH('b) n)\<close>
 | 
| 257 | by transfer (simp add: nat_eq_iff take_bit_of_nat) | |
| 258 | ||
| 74496 | 259 | lemma unsigned_of_int: | 
| 72262 | 260 |   \<open>unsigned (word_of_int k :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) k))\<close>
 | 
| 261 | by transfer simp | |
| 262 | ||
| 263 | end | |
| 264 | ||
| 265 | context semiring_char_0 | |
| 266 | begin | |
| 267 | ||
| 268 | lemma unsigned_word_eqI: | |
| 269 | \<open>v = w\<close> if \<open>unsigned v = unsigned w\<close> | |
| 270 | using that by transfer (simp add: eq_nat_nat_iff) | |
| 271 | ||
| 272 | lemma word_eq_iff_unsigned: | |
| 273 | \<open>v = w \<longleftrightarrow> unsigned v = unsigned w\<close> | |
| 274 | by (auto intro: unsigned_word_eqI) | |
| 275 | ||
| 72292 | 276 | lemma inj_unsigned [simp]: | 
| 277 | \<open>inj unsigned\<close> | |
| 278 | by (rule injI) (simp add: unsigned_word_eqI) | |
| 279 | ||
| 280 | lemma unsigned_eq_0_iff: | |
| 72281 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 281 | \<open>unsigned w = 0 \<longleftrightarrow> w = 0\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 282 | using word_eq_iff_unsigned [of w 0] by simp | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 283 | |
| 72262 | 284 | end | 
| 285 | ||
| 286 | context ring_1 | |
| 287 | begin | |
| 288 | ||
| 289 | lift_definition signed :: \<open>'b::len word \<Rightarrow> 'a\<close> | |
| 290 |   is \<open>of_int \<circ> signed_take_bit (LENGTH('b) - Suc 0)\<close>
 | |
| 291 | by (simp flip: signed_take_bit_decr_length_iff) | |
| 292 | ||
| 293 | lemma signed_0 [simp]: | |
| 294 | \<open>signed 0 = 0\<close> | |
| 295 | by transfer simp | |
| 296 | ||
| 297 | lemma signed_1 [simp]: | |
| 298 |   \<open>signed (1 :: 'b::len word) = (if LENGTH('b) = 1 then - 1 else 1)\<close>
 | |
| 72488 | 299 |   by (transfer fixing: uminus; cases \<open>LENGTH('b)\<close>) (auto dest: gr0_implies_Suc)
 | 
| 72262 | 300 | |
| 301 | lemma signed_minus_1 [simp]: | |
| 302 | \<open>signed (- 1 :: 'b::len word) = - 1\<close> | |
| 303 | by (transfer fixing: uminus) simp | |
| 304 | ||
| 305 | lemma signed_numeral [simp]: | |
| 306 |   \<open>signed (numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (numeral n))\<close>
 | |
| 307 | by transfer simp | |
| 308 | ||
| 309 | lemma signed_neg_numeral [simp]: | |
| 310 |   \<open>signed (- numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (- numeral n))\<close>
 | |
| 311 | by transfer simp | |
| 312 | ||
| 74496 | 313 | lemma signed_of_nat: | 
| 72262 | 314 |   \<open>signed (word_of_nat n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) (int n))\<close>
 | 
| 315 | by transfer simp | |
| 316 | ||
| 74496 | 317 | lemma signed_of_int: | 
| 72262 | 318 |   \<open>signed (word_of_int n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) n)\<close>
 | 
| 319 | by transfer simp | |
| 320 | ||
| 321 | end | |
| 322 | ||
| 323 | context ring_char_0 | |
| 324 | begin | |
| 325 | ||
| 326 | lemma signed_word_eqI: | |
| 327 | \<open>v = w\<close> if \<open>signed v = signed w\<close> | |
| 328 | using that by transfer (simp flip: signed_take_bit_decr_length_iff) | |
| 329 | ||
| 330 | lemma word_eq_iff_signed: | |
| 331 | \<open>v = w \<longleftrightarrow> signed v = signed w\<close> | |
| 332 | by (auto intro: signed_word_eqI) | |
| 333 | ||
| 72292 | 334 | lemma inj_signed [simp]: | 
| 335 | \<open>inj signed\<close> | |
| 336 | by (rule injI) (simp add: signed_word_eqI) | |
| 337 | ||
| 72281 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 338 | lemma signed_eq_0_iff: | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 339 | \<open>signed w = 0 \<longleftrightarrow> w = 0\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 340 | using word_eq_iff_signed [of w 0] by simp | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 341 | |
| 72262 | 342 | end | 
| 343 | ||
| 344 | abbreviation unat :: \<open>'a::len word \<Rightarrow> nat\<close> | |
| 345 | where \<open>unat \<equiv> unsigned\<close> | |
| 346 | ||
| 347 | abbreviation uint :: \<open>'a::len word \<Rightarrow> int\<close> | |
| 348 | where \<open>uint \<equiv> unsigned\<close> | |
| 349 | ||
| 350 | abbreviation sint :: \<open>'a::len word \<Rightarrow> int\<close> | |
| 351 | where \<open>sint \<equiv> signed\<close> | |
| 352 | ||
| 353 | abbreviation ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 354 | where \<open>ucast \<equiv> unsigned\<close> | |
| 355 | ||
| 356 | abbreviation scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 357 | where \<open>scast \<equiv> signed\<close> | |
| 358 | ||
| 359 | context | |
| 360 | includes lifting_syntax | |
| 361 | begin | |
| 362 | ||
| 363 | lemma [transfer_rule]: | |
| 364 |   \<open>(pcr_word ===> (=)) (nat \<circ> take_bit LENGTH('a)) (unat :: 'a::len word \<Rightarrow> nat)\<close>
 | |
| 365 | using unsigned.transfer [where ?'a = nat] by simp | |
| 366 | ||
| 367 | lemma [transfer_rule]: | |
| 368 |   \<open>(pcr_word ===> (=)) (take_bit LENGTH('a)) (uint :: 'a::len word \<Rightarrow> int)\<close>
 | |
| 369 | using unsigned.transfer [where ?'a = int] by (simp add: comp_def) | |
| 370 | ||
| 371 | lemma [transfer_rule]: | |
| 372 |   \<open>(pcr_word ===> (=)) (signed_take_bit (LENGTH('a) - Suc 0)) (sint :: 'a::len word \<Rightarrow> int)\<close>
 | |
| 373 | using signed.transfer [where ?'a = int] by simp | |
| 374 | ||
| 375 | lemma [transfer_rule]: | |
| 376 |   \<open>(pcr_word ===> pcr_word) (take_bit LENGTH('a)) (ucast :: 'a::len word \<Rightarrow> 'b::len word)\<close>
 | |
| 377 | proof (rule rel_funI) | |
| 378 | fix k :: int and w :: \<open>'a word\<close> | |
| 379 | assume \<open>pcr_word k w\<close> | |
| 380 | then have \<open>w = word_of_int k\<close> | |
| 381 | by (simp add: pcr_word_def cr_word_def relcompp_apply) | |
| 382 |   moreover have \<open>pcr_word (take_bit LENGTH('a) k) (ucast (word_of_int k :: 'a word))\<close>
 | |
| 383 | by transfer (simp add: pcr_word_def cr_word_def relcompp_apply) | |
| 384 |   ultimately show \<open>pcr_word (take_bit LENGTH('a) k) (ucast w)\<close>
 | |
| 385 | by simp | |
| 386 | qed | |
| 387 | ||
| 388 | lemma [transfer_rule]: | |
| 389 |   \<open>(pcr_word ===> pcr_word) (signed_take_bit (LENGTH('a) - Suc 0)) (scast :: 'a::len word \<Rightarrow> 'b::len word)\<close>
 | |
| 390 | proof (rule rel_funI) | |
| 391 | fix k :: int and w :: \<open>'a word\<close> | |
| 392 | assume \<open>pcr_word k w\<close> | |
| 393 | then have \<open>w = word_of_int k\<close> | |
| 394 | by (simp add: pcr_word_def cr_word_def relcompp_apply) | |
| 395 |   moreover have \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast (word_of_int k :: 'a word))\<close>
 | |
| 396 | by transfer (simp add: pcr_word_def cr_word_def relcompp_apply) | |
| 397 |   ultimately show \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast w)\<close>
 | |
| 398 | by simp | |
| 399 | qed | |
| 400 | ||
| 401 | end | |
| 402 | ||
| 403 | lemma of_nat_unat [simp]: | |
| 404 | \<open>of_nat (unat w) = unsigned w\<close> | |
| 405 | by transfer simp | |
| 406 | ||
| 407 | lemma of_int_uint [simp]: | |
| 408 | \<open>of_int (uint w) = unsigned w\<close> | |
| 409 | by transfer simp | |
| 410 | ||
| 411 | lemma of_int_sint [simp]: | |
| 412 | \<open>of_int (sint a) = signed a\<close> | |
| 413 | by transfer (simp_all add: take_bit_signed_take_bit) | |
| 72079 | 414 | |
| 415 | lemma nat_uint_eq [simp]: | |
| 416 | \<open>nat (uint w) = unat w\<close> | |
| 417 | by transfer simp | |
| 418 | ||
| 72281 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 419 | lemma sgn_uint_eq [simp]: | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 420 | \<open>sgn (uint w) = of_bool (w \<noteq> 0)\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 421 | by transfer (simp add: less_le) | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 422 | |
| 72262 | 423 | text \<open>Aliasses only for code generation\<close> | 
| 424 | ||
| 425 | context | |
| 426 | begin | |
| 427 | ||
| 428 | qualified lift_definition of_int :: \<open>int \<Rightarrow> 'a::len word\<close> | |
| 429 |   is \<open>take_bit LENGTH('a)\<close> .
 | |
| 430 | ||
| 431 | qualified lift_definition of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close> | |
| 432 |   is \<open>int \<circ> take_bit LENGTH('a)\<close> .
 | |
| 433 | ||
| 434 | qualified lift_definition the_nat :: \<open>'a::len word \<Rightarrow> nat\<close> | |
| 435 |   is \<open>nat \<circ> take_bit LENGTH('a)\<close> by simp
 | |
| 436 | ||
| 437 | qualified lift_definition the_signed_int :: \<open>'a::len word \<Rightarrow> int\<close> | |
| 438 |   is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (simp add: signed_take_bit_decr_length_iff)
 | |
| 439 | ||
| 440 | qualified lift_definition cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 441 |   is \<open>take_bit LENGTH('a)\<close> by simp
 | |
| 442 | ||
| 443 | qualified lift_definition signed_cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 444 |   is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (metis signed_take_bit_decr_length_iff)
 | |
| 445 | ||
| 446 | end | |
| 447 | ||
| 448 | lemma [code_abbrev, simp]: | |
| 449 | \<open>Word.the_int = uint\<close> | |
| 450 | by transfer rule | |
| 451 | ||
| 452 | lemma [code]: | |
| 453 |   \<open>Word.the_int (Word.of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
 | |
| 454 | by transfer simp | |
| 455 | ||
| 456 | lemma [code_abbrev, simp]: | |
| 457 | \<open>Word.of_int = word_of_int\<close> | |
| 458 | by (rule; transfer) simp | |
| 459 | ||
| 460 | lemma [code]: | |
| 461 |   \<open>Word.the_int (Word.of_nat n :: 'a::len word) = take_bit LENGTH('a) (int n)\<close>
 | |
| 72244 | 462 | by transfer (simp add: take_bit_of_nat) | 
| 463 | ||
| 72262 | 464 | lemma [code_abbrev, simp]: | 
| 465 | \<open>Word.of_nat = word_of_nat\<close> | |
| 466 | by (rule; transfer) (simp add: take_bit_of_nat) | |
| 467 | ||
| 468 | lemma [code]: | |
| 469 | \<open>Word.the_nat w = nat (Word.the_int w)\<close> | |
| 470 | by transfer simp | |
| 471 | ||
| 472 | lemma [code_abbrev, simp]: | |
| 473 | \<open>Word.the_nat = unat\<close> | |
| 474 | by (rule; transfer) simp | |
| 475 | ||
| 476 | lemma [code]: | |
| 477 |   \<open>Word.the_signed_int w = signed_take_bit (LENGTH('a) - Suc 0) (Word.the_int w)\<close>
 | |
| 478 | for w :: \<open>'a::len word\<close> | |
| 72488 | 479 | by transfer (simp add: signed_take_bit_take_bit) | 
| 72262 | 480 | |
| 481 | lemma [code_abbrev, simp]: | |
| 482 | \<open>Word.the_signed_int = sint\<close> | |
| 483 | by (rule; transfer) simp | |
| 484 | ||
| 485 | lemma [code]: | |
| 486 |   \<open>Word.the_int (Word.cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_int w)\<close>
 | |
| 487 | for w :: \<open>'a::len word\<close> | |
| 488 | by transfer simp | |
| 489 | ||
| 490 | lemma [code_abbrev, simp]: | |
| 491 | \<open>Word.cast = ucast\<close> | |
| 492 | by (rule; transfer) simp | |
| 493 | ||
| 494 | lemma [code]: | |
| 495 |   \<open>Word.the_int (Word.signed_cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_signed_int w)\<close>
 | |
| 496 | for w :: \<open>'a::len word\<close> | |
| 497 | by transfer simp | |
| 498 | ||
| 499 | lemma [code_abbrev, simp]: | |
| 500 | \<open>Word.signed_cast = scast\<close> | |
| 501 | by (rule; transfer) simp | |
| 502 | ||
| 503 | lemma [code]: | |
| 504 | \<open>unsigned w = of_nat (nat (Word.the_int w))\<close> | |
| 505 | by transfer simp | |
| 506 | ||
| 507 | lemma [code]: | |
| 508 | \<open>signed w = of_int (Word.the_signed_int w)\<close> | |
| 509 | by transfer simp | |
| 72244 | 510 | |
| 511 | ||
| 512 | subsubsection \<open>Basic ordering\<close> | |
| 45547 | 513 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 514 | instantiation word :: (len) linorder | 
| 45547 | 515 | begin | 
| 516 | ||
| 71950 | 517 | lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" | 
| 518 |   is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
 | |
| 519 | by simp | |
| 520 | ||
| 521 | lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" | |
| 522 |   is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
 | |
| 523 | by simp | |
| 37660 | 524 | |
| 45547 | 525 | instance | 
| 71950 | 526 | by (standard; transfer) auto | 
| 45547 | 527 | |
| 528 | end | |
| 529 | ||
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 530 | interpretation word_order: ordering_top \<open>(\<le>)\<close> \<open>(<)\<close> \<open>- 1 :: 'a::len word\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 531 | by (standard; transfer) (simp add: take_bit_eq_mod zmod_minus1) | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 532 | |
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 533 | interpretation word_coorder: ordering_top \<open>(\<ge>)\<close> \<open>(>)\<close> \<open>0 :: 'a::len word\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 534 | by (standard; transfer) simp | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 535 | |
| 72262 | 536 | lemma word_of_nat_less_eq_iff: | 
| 537 |   \<open>word_of_nat m \<le> (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
 | |
| 538 | by transfer (simp add: take_bit_of_nat) | |
| 539 | ||
| 540 | lemma word_of_int_less_eq_iff: | |
| 541 |   \<open>word_of_int k \<le> (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
 | |
| 542 | by transfer rule | |
| 543 | ||
| 544 | lemma word_of_nat_less_iff: | |
| 545 |   \<open>word_of_nat m < (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
 | |
| 546 | by transfer (simp add: take_bit_of_nat) | |
| 547 | ||
| 548 | lemma word_of_int_less_iff: | |
| 549 |   \<open>word_of_int k < (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
 | |
| 550 | by transfer rule | |
| 551 | ||
| 71950 | 552 | lemma word_le_def [code]: | 
| 553 | "a \<le> b \<longleftrightarrow> uint a \<le> uint b" | |
| 554 | by transfer rule | |
| 555 | ||
| 556 | lemma word_less_def [code]: | |
| 557 | "a < b \<longleftrightarrow> uint a < uint b" | |
| 558 | by transfer rule | |
| 559 | ||
| 71951 | 560 | lemma word_greater_zero_iff: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 561 | \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close> | 
| 71951 | 562 | by transfer (simp add: less_le) | 
| 563 | ||
| 564 | lemma of_nat_word_less_eq_iff: | |
| 565 |   \<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
 | |
| 566 | by transfer (simp add: take_bit_of_nat) | |
| 567 | ||
| 568 | lemma of_nat_word_less_iff: | |
| 569 |   \<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
 | |
| 570 | by transfer (simp add: take_bit_of_nat) | |
| 571 | ||
| 572 | lemma of_int_word_less_eq_iff: | |
| 573 |   \<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
 | |
| 574 | by transfer rule | |
| 575 | ||
| 576 | lemma of_int_word_less_iff: | |
| 577 |   \<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
 | |
| 578 | by transfer rule | |
| 579 | ||
| 37660 | 580 | |
| 72280 | 581 | |
| 582 | subsection \<open>Enumeration\<close> | |
| 583 | ||
| 584 | lemma inj_on_word_of_nat: | |
| 585 |   \<open>inj_on (word_of_nat :: nat \<Rightarrow> 'a::len word) {0..<2 ^ LENGTH('a)}\<close>
 | |
| 586 | by (rule inj_onI; transfer) (simp_all add: take_bit_int_eq_self) | |
| 587 | ||
| 588 | lemma UNIV_word_eq_word_of_nat: | |
| 589 |   \<open>(UNIV :: 'a::len word set) = word_of_nat ` {0..<2 ^ LENGTH('a)}\<close> (is \<open>_ = ?A\<close>)
 | |
| 590 | proof | |
| 591 |   show \<open>word_of_nat ` {0..<2 ^ LENGTH('a)} \<subseteq> UNIV\<close>
 | |
| 592 | by simp | |
| 593 | show \<open>UNIV \<subseteq> ?A\<close> | |
| 594 | proof | |
| 595 | fix w :: \<open>'a word\<close> | |
| 596 |     show \<open>w \<in> (word_of_nat ` {0..<2 ^ LENGTH('a)} :: 'a word set)\<close>
 | |
| 597 | by (rule image_eqI [of _ _ \<open>unat w\<close>]; transfer) simp_all | |
| 598 | qed | |
| 599 | qed | |
| 600 | ||
| 601 | instantiation word :: (len) enum | |
| 602 | begin | |
| 603 | ||
| 604 | definition enum_word :: \<open>'a word list\<close> | |
| 605 |   where \<open>enum_word = map word_of_nat [0..<2 ^ LENGTH('a)]\<close>
 | |
| 606 | ||
| 607 | definition enum_all_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
 | |
| 77225 | 608 | where \<open>enum_all_word = All\<close> | 
| 72280 | 609 | |
| 610 | definition enum_ex_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
 | |
| 77225 | 611 | where \<open>enum_ex_word = Ex\<close> | 
| 612 | ||
| 613 | instance | |
| 614 | by standard | |
| 615 | (simp_all add: enum_all_word_def enum_ex_word_def enum_word_def distinct_map inj_on_word_of_nat flip: UNIV_word_eq_word_of_nat) | |
| 616 | ||
| 617 | end | |
| 72280 | 618 | |
| 619 | lemma [code]: | |
| 77225 | 620 | \<open>Enum.enum_all P \<longleftrightarrow> list_all P Enum.enum\<close> | 
| 621 | \<open>Enum.enum_ex P \<longleftrightarrow> list_ex P Enum.enum\<close> for P :: \<open>'a::len word \<Rightarrow> bool\<close> | |
| 622 | by (simp_all add: enum_all_word_def enum_ex_word_def enum_UNIV list_all_iff list_ex_iff) | |
| 72280 | 623 | |
| 624 | ||
| 61799 | 625 | subsection \<open>Bit-wise operations\<close> | 
| 37660 | 626 | |
| 72244 | 627 | instantiation word :: (len) semiring_modulo | 
| 628 | begin | |
| 629 | ||
| 630 | lift_definition divide_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 631 |   is \<open>\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b\<close>
 | |
| 632 | by simp | |
| 633 | ||
| 634 | lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 635 |   is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close>
 | |
| 636 | by simp | |
| 637 | ||
| 638 | instance proof | |
| 639 | show "a div b * b + a mod b = a" for a b :: "'a word" | |
| 640 | proof transfer | |
| 641 | fix k l :: int | |
| 642 |     define r :: int where "r = 2 ^ LENGTH('a)"
 | |
| 643 |     then have r: "take_bit LENGTH('a) k = k mod r" for k
 | |
| 644 | by (simp add: take_bit_eq_mod) | |
| 645 | have "k mod r = ((k mod r) div (l mod r) * (l mod r) | |
| 646 | + (k mod r) mod (l mod r)) mod r" | |
| 647 | by (simp add: div_mult_mod_eq) | |
| 648 | also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r | |
| 649 | + (k mod r) mod (l mod r)) mod r" | |
| 650 | by (simp add: mod_add_left_eq) | |
| 651 | also have "... = (((k mod r) div (l mod r) * l) mod r | |
| 652 | + (k mod r) mod (l mod r)) mod r" | |
| 653 | by (simp add: mod_mult_right_eq) | |
| 654 | finally have "k mod r = ((k mod r) div (l mod r) * l | |
| 655 | + (k mod r) mod (l mod r)) mod r" | |
| 656 | by (simp add: mod_simps) | |
| 657 |     with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
 | |
| 658 |       + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
 | |
| 659 | by simp | |
| 660 | qed | |
| 661 | qed | |
| 662 | ||
| 663 | end | |
| 664 | ||
| 665 | instance word :: (len) semiring_parity | |
| 666 | proof | |
| 667 | show "\<not> 2 dvd (1::'a word)" | |
| 668 | by transfer simp | |
| 669 | show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0" | |
| 670 | for a :: "'a word" | |
| 671 | by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) | |
| 672 | show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" | |
| 673 | for a :: "'a word" | |
| 674 | by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) | |
| 675 | qed | |
| 676 | ||
| 71951 | 677 | lemma word_bit_induct [case_names zero even odd]: | 
| 678 | \<open>P a\<close> if word_zero: \<open>P 0\<close> | |
| 72262 | 679 |     and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (2 * a)\<close>
 | 
| 680 |     and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (1 + 2 * a)\<close>
 | |
| 71951 | 681 | for P and a :: \<open>'a::len word\<close> | 
| 682 | proof - | |
| 72262 | 683 |   define m :: nat where \<open>m = LENGTH('a) - Suc 0\<close>
 | 
| 71951 | 684 |   then have l: \<open>LENGTH('a) = Suc m\<close>
 | 
| 685 | by simp | |
| 686 | define n :: nat where \<open>n = unat a\<close> | |
| 687 |   then have \<open>n < 2 ^ LENGTH('a)\<close>
 | |
| 72262 | 688 | by transfer (simp add: take_bit_eq_mod) | 
| 71951 | 689 | then have \<open>n < 2 * 2 ^ m\<close> | 
| 690 | by (simp add: l) | |
| 691 | then have \<open>P (of_nat n)\<close> | |
| 692 | proof (induction n rule: nat_bit_induct) | |
| 693 | case zero | |
| 694 | show ?case | |
| 695 | by simp (rule word_zero) | |
| 696 | next | |
| 697 | case (even n) | |
| 698 | then have \<open>n < 2 ^ m\<close> | |
| 699 | by simp | |
| 700 | with even.IH have \<open>P (of_nat n)\<close> | |
| 701 | by simp | |
| 702 | moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close> | |
| 74496 | 703 | by (auto simp add: word_greater_zero_iff l word_of_nat_eq_0_iff) | 
| 72262 | 704 |     moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close>
 | 
| 71951 | 705 | using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] | 
| 72261 | 706 | by (simp add: l take_bit_eq_mod) | 
| 71951 | 707 | ultimately have \<open>P (2 * of_nat n)\<close> | 
| 708 | by (rule word_even) | |
| 709 | then show ?case | |
| 710 | by simp | |
| 711 | next | |
| 712 | case (odd n) | |
| 713 | then have \<open>Suc n \<le> 2 ^ m\<close> | |
| 714 | by simp | |
| 715 | with odd.IH have \<open>P (of_nat n)\<close> | |
| 716 | by simp | |
| 72262 | 717 |     moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close>
 | 
| 71951 | 718 | using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] | 
| 72261 | 719 | by (simp add: l take_bit_eq_mod) | 
| 71951 | 720 | ultimately have \<open>P (1 + 2 * of_nat n)\<close> | 
| 721 | by (rule word_odd) | |
| 722 | then show ?case | |
| 723 | by simp | |
| 724 | qed | |
| 725 | moreover have \<open>of_nat (nat (uint a)) = a\<close> | |
| 726 | by transfer simp | |
| 727 | ultimately show ?thesis | |
| 72079 | 728 | by (simp add: n_def) | 
| 71951 | 729 | qed | 
| 730 | ||
| 731 | lemma bit_word_half_eq: | |
| 732 | \<open>(of_bool b + a * 2) div 2 = a\<close> | |
| 733 |     if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
 | |
| 734 | for a :: \<open>'a::len word\<close> | |
| 735 | proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
 | |
| 736 | case False | |
| 737 | have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int | |
| 738 | by auto | |
| 739 | with False that show ?thesis | |
| 740 | by transfer (simp add: eq_iff) | |
| 741 | next | |
| 742 | case True | |
| 743 |   obtain n where length: \<open>LENGTH('a) = Suc n\<close>
 | |
| 744 |     by (cases \<open>LENGTH('a)\<close>) simp_all
 | |
| 745 | show ?thesis proof (cases b) | |
| 746 | case False | |
| 747 | moreover have \<open>a * 2 div 2 = a\<close> | |
| 748 | using that proof transfer | |
| 749 | fix k :: int | |
| 750 |       from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close>
 | |
| 751 | by simp | |
| 752 |       moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
 | |
| 76231 | 753 |       with \<open>LENGTH('a) = Suc n\<close> have \<open>take_bit LENGTH('a) k = take_bit n k\<close>
 | 
| 754 | by (auto simp add: take_bit_Suc_from_most) | |
| 71951 | 755 |       ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close>
 | 
| 756 | by (simp add: take_bit_eq_mod) | |
| 757 |       with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2)
 | |
| 758 |         = take_bit LENGTH('a) k\<close>
 | |
| 759 | by simp | |
| 760 | qed | |
| 761 | ultimately show ?thesis | |
| 762 | by simp | |
| 763 | next | |
| 764 | case True | |
| 765 | moreover have \<open>(1 + a * 2) div 2 = a\<close> | |
| 766 | using that proof transfer | |
| 767 | fix k :: int | |
| 768 |       from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close>
 | |
| 769 | using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps) | |
| 770 |       moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
 | |
| 76231 | 771 |       with \<open>LENGTH('a) = Suc n\<close> have \<open>take_bit LENGTH('a) k = take_bit n k\<close>
 | 
| 772 | by (auto simp add: take_bit_Suc_from_most) | |
| 71951 | 773 |       ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close>
 | 
| 774 | by (simp add: take_bit_eq_mod) | |
| 775 |       with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2)
 | |
| 776 |         = take_bit LENGTH('a) k\<close>
 | |
| 777 | by (auto simp add: take_bit_Suc) | |
| 778 | qed | |
| 779 | ultimately show ?thesis | |
| 780 | by simp | |
| 781 | qed | |
| 782 | qed | |
| 783 | ||
| 784 | lemma even_mult_exp_div_word_iff: | |
| 785 | \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> ( | |
| 786 | m \<le> n \<and> | |
| 787 |     n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close>
 | |
| 788 | by transfer | |
| 789 | (auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff, | |
| 790 | simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int) | |
| 791 | ||
| 71965 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 792 | instantiation word :: (len) semiring_bits | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 793 | begin | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 794 | |
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 795 | lift_definition bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool\<close> | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 796 |   is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
 | 
| 71951 | 797 | proof | 
| 71965 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 798 | fix k l :: int and n :: nat | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 799 |   assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
 | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 800 |   show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
 | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 801 |   proof (cases \<open>n < LENGTH('a)\<close>)
 | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 802 | case True | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 803 |     from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
 | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 804 | by simp | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 805 | then show ?thesis | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 806 | by (simp add: bit_take_bit_iff) | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 807 | next | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 808 | case False | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 809 | then show ?thesis | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 810 | by simp | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 811 | qed | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 812 | qed | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 813 | |
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 814 | instance proof | 
| 71951 | 815 | show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close> | 
| 816 | and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close> | |
| 817 | for P and a :: \<open>'a word\<close> | |
| 818 | proof (induction a rule: word_bit_induct) | |
| 819 | case zero | |
| 820 | have \<open>0 div 2 = (0::'a word)\<close> | |
| 821 | by transfer simp | |
| 822 | with stable [of 0] show ?case | |
| 823 | by simp | |
| 824 | next | |
| 825 | case (even a) | |
| 826 | with rec [of a False] show ?case | |
| 827 | using bit_word_half_eq [of a False] by (simp add: ac_simps) | |
| 828 | next | |
| 829 | case (odd a) | |
| 830 | with rec [of a True] show ?case | |
| 831 | using bit_word_half_eq [of a True] by (simp add: ac_simps) | |
| 832 | qed | |
| 71965 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 833 | show \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> for a :: \<open>'a word\<close> and n | 
| 
d45f5d4c41bd
more class operations for the sake of efficient generated code
 haftmann parents: 
71958diff
changeset | 834 | by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit bit_iff_odd_drop_bit) | 
| 71951 | 835 | show \<open>0 div a = 0\<close> | 
| 836 | for a :: \<open>'a word\<close> | |
| 837 | by transfer simp | |
| 838 | show \<open>a div 1 = a\<close> | |
| 839 | for a :: \<open>'a word\<close> | |
| 840 | by transfer simp | |
| 841 | show \<open>a mod b div b = 0\<close> | |
| 842 | for a b :: \<open>'a word\<close> | |
| 843 | apply transfer | |
| 77061 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76231diff
changeset | 844 | apply (simp add: take_bit_eq_mod) | 
| 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76231diff
changeset | 845 | apply (smt (verit, best) Euclidean_Rings.pos_mod_bound Euclidean_Rings.pos_mod_sign div_int_pos_iff | 
| 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76231diff
changeset | 846 | nonneg1_imp_zdiv_pos_iff zero_less_power zmod_le_nonneg_dividend) | 
| 
5de3772609ea
generalized theory name: euclidean division denotes one particular division definition on integers
 haftmann parents: 
76231diff
changeset | 847 | done | 
| 71951 | 848 | show \<open>(1 + a) div 2 = a div 2\<close> | 
| 849 | if \<open>even a\<close> | |
| 850 | for a :: \<open>'a word\<close> | |
| 71953 | 851 | using that by transfer | 
| 73535 | 852 | (auto dest: le_Suc_ex simp add: take_bit_Suc elim!: evenE) | 
| 71951 | 853 | show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close> | 
| 854 | for m n :: nat | |
| 855 | by transfer (simp, simp add: exp_div_exp_eq) | |
| 856 | show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)" | |
| 857 | for a :: "'a word" and m n :: nat | |
| 858 | apply transfer | |
| 859 | apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div) | |
| 860 | apply (simp add: drop_bit_take_bit) | |
| 861 | done | |
| 862 | show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n" | |
| 863 | for a :: "'a word" and m n :: nat | |
| 864 | by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps) | |
| 865 | show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close> | |
| 866 | if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat | |
| 867 | using that apply transfer | |
| 868 | apply (auto simp flip: take_bit_eq_mod) | |
| 869 | apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin) | |
| 870 | done | |
| 871 | show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> | |
| 872 | for a :: "'a word" and m n :: nat | |
| 873 | by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin) | |
| 874 | show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close> | |
| 875 | for m n :: nat | |
| 74592 | 876 | by transfer | 
| 877 | (simp flip: drop_bit_eq_div mask_eq_exp_minus_1 add: bit_simps even_drop_bit_iff_not_bit not_less) | |
| 71951 | 878 | show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n - m))\<close> | 
| 879 | for a :: \<open>'a word\<close> and m n :: nat | |
| 880 | proof transfer | |
| 881 |     show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow>
 | |
| 882 | n < m | |
| 883 |       \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
 | |
| 884 |       \<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close>
 | |
| 885 | for m n :: nat and k l :: int | |
| 886 | by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult | |
| 887 | simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m]) | |
| 888 | qed | |
| 889 | qed | |
| 890 | ||
| 891 | end | |
| 892 | ||
| 72262 | 893 | lemma bit_word_eqI: | 
| 894 |   \<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
 | |
| 895 | for a b :: \<open>'a::len word\<close> | |
| 896 | using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff) | |
| 897 | ||
| 898 | lemma bit_imp_le_length: | |
| 899 |   \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
 | |
| 900 | for w :: \<open>'a::len word\<close> | |
| 901 | using that by transfer simp | |
| 902 | ||
| 903 | lemma not_bit_length [simp]: | |
| 904 |   \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
 | |
| 905 | by transfer simp | |
| 906 | ||
| 72830 | 907 | lemma finite_bit_word [simp]: | 
| 908 |   \<open>finite {n. bit w n}\<close>
 | |
| 909 | for w :: \<open>'a::len word\<close> | |
| 910 | proof - | |
| 911 |   have \<open>{n. bit w n} \<subseteq> {0..LENGTH('a)}\<close>
 | |
| 912 | by (auto dest: bit_imp_le_length) | |
| 913 |   moreover have \<open>finite {0..LENGTH('a)}\<close>
 | |
| 914 | by simp | |
| 915 | ultimately show ?thesis | |
| 916 | by (rule finite_subset) | |
| 917 | qed | |
| 918 | ||
| 73789 | 919 | lemma bit_numeral_word_iff [simp]: | 
| 920 | \<open>bit (numeral w :: 'a::len word) n | |
| 921 |     \<longleftrightarrow> n < LENGTH('a) \<and> bit (numeral w :: int) n\<close>
 | |
| 922 | by transfer simp | |
| 923 | ||
| 924 | lemma bit_neg_numeral_word_iff [simp]: | |
| 925 | \<open>bit (- numeral w :: 'a::len word) n | |
| 926 |     \<longleftrightarrow> n < LENGTH('a) \<and> bit (- numeral w :: int) n\<close>
 | |
| 927 | by transfer simp | |
| 928 | ||
| 72262 | 929 | instantiation word :: (len) ring_bit_operations | 
| 930 | begin | |
| 931 | ||
| 932 | lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close> | |
| 933 | is not | |
| 934 | by (simp add: take_bit_not_iff) | |
| 935 | ||
| 936 | lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 937 | is \<open>and\<close> | |
| 938 | by simp | |
| 939 | ||
| 940 | lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 941 | is or | |
| 942 | by simp | |
| 943 | ||
| 944 | lift_definition xor_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | |
| 945 | is xor | |
| 946 | by simp | |
| 947 | ||
| 948 | lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close> | |
| 949 | is mask | |
| 950 | . | |
| 951 | ||
| 73682 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 952 | lift_definition set_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 953 | is set_bit | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 954 | by (simp add: set_bit_def) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 955 | |
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 956 | lift_definition unset_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 957 | is unset_bit | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 958 | by (simp add: unset_bit_def) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 959 | |
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 960 | lift_definition flip_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 961 | is flip_bit | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 962 | by (simp add: flip_bit_def) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 963 | |
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 964 | lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 965 | is push_bit | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 966 | proof - | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 967 |   show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 968 |     if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 969 | proof - | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 970 | from that | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 971 |     have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 972 |       = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 973 | by simp | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 974 |     moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 975 | by simp | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 976 | ultimately show ?thesis | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 977 | by (simp add: take_bit_push_bit) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 978 | qed | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 979 | qed | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 980 | |
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 981 | lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 982 |   is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 983 | by (simp add: take_bit_eq_mod) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 984 | |
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 985 | lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 986 |   is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 987 | by (simp add: ac_simps) (simp only: flip: take_bit_take_bit) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 988 | |
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 989 | instance apply (standard; transfer) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 990 | apply (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 991 | bit_simps set_bit_def flip_bit_def take_bit_drop_bit | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 992 | simp flip: drop_bit_eq_div take_bit_eq_mod) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 993 | apply (simp_all add: drop_bit_take_bit flip: push_bit_eq_mult) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 994 | done | 
| 72262 | 995 | |
| 996 | end | |
| 997 | ||
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 998 | lemma [code]: | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 999 | \<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1000 | by (fact push_bit_eq_mult) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1001 | |
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1002 | lemma [code]: | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1003 | \<open>Word.the_int (drop_bit n w) = drop_bit n (Word.the_int w)\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1004 | by transfer (simp add: drop_bit_take_bit min_def le_less less_diff_conv) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1005 | |
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1006 | lemma [code]: | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1007 |   \<open>Word.the_int (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (Word.the_int w) else Word.the_int w)\<close>
 | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1008 | for w :: \<open>'a::len word\<close> | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1009 | by transfer (simp add: not_le not_less ac_simps min_absorb2) | 
| 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1010 | |
| 72262 | 1011 | lemma [code_abbrev]: | 
| 1012 | \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> | |
| 1013 | by (fact push_bit_of_1) | |
| 1014 | ||
| 74391 | 1015 | context | 
| 1016 | includes bit_operations_syntax | |
| 1017 | begin | |
| 1018 | ||
| 72262 | 1019 | lemma [code]: | 
| 1020 | \<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close> | |
| 1021 | for w :: \<open>'a::len word\<close> | |
| 1022 | by transfer (simp add: take_bit_not_take_bit) | |
| 1023 | ||
| 1024 | lemma [code]: | |
| 1025 | \<open>Word.the_int (v AND w) = Word.the_int v AND Word.the_int w\<close> | |
| 71990 | 1026 | by transfer simp | 
| 1027 | ||
| 72262 | 1028 | lemma [code]: | 
| 1029 | \<open>Word.the_int (v OR w) = Word.the_int v OR Word.the_int w\<close> | |
| 1030 | by transfer simp | |
| 1031 | ||
| 1032 | lemma [code]: | |
| 1033 | \<open>Word.the_int (v XOR w) = Word.the_int v XOR Word.the_int w\<close> | |
| 1034 | by transfer simp | |
| 1035 | ||
| 1036 | lemma [code]: | |
| 1037 |   \<open>Word.the_int (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close>
 | |
| 1038 | by transfer simp | |
| 1039 | ||
| 73682 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1040 | lemma [code]: | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1041 | \<open>set_bit n w = w OR push_bit n 1\<close> for w :: \<open>'a::len word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1042 | by (fact set_bit_eq_or) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1043 | |
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1044 | lemma [code]: | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1045 | \<open>unset_bit n w = w AND NOT (push_bit n 1)\<close> for w :: \<open>'a::len word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1046 | by (fact unset_bit_eq_and_not) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1047 | |
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1048 | lemma [code]: | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1049 | \<open>flip_bit n w = w XOR push_bit n 1\<close> for w :: \<open>'a::len word\<close> | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1050 | by (fact flip_bit_eq_xor) | 
| 
78044b2f001c
explicit type class operations for type-specific implementations
 haftmann parents: 
73535diff
changeset | 1051 | |
| 72262 | 1052 | context | 
| 1053 | includes lifting_syntax | |
| 1054 | begin | |
| 1055 | ||
| 1056 | lemma set_bit_word_transfer [transfer_rule]: | |
| 1057 | \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close> | |
| 1058 | by (unfold set_bit_def) transfer_prover | |
| 1059 | ||
| 1060 | lemma unset_bit_word_transfer [transfer_rule]: | |
| 1061 | \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close> | |
| 1062 | by (unfold unset_bit_def) transfer_prover | |
| 1063 | ||
| 1064 | lemma flip_bit_word_transfer [transfer_rule]: | |
| 1065 | \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close> | |
| 1066 | by (unfold flip_bit_def) transfer_prover | |
| 1067 | ||
| 1068 | lemma signed_take_bit_word_transfer [transfer_rule]: | |
| 1069 | \<open>((=) ===> pcr_word ===> pcr_word) | |
| 1070 |     (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))
 | |
| 1071 | (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close> | |
| 1072 | proof - | |
| 1073 |   let ?K = \<open>\<lambda>n (k :: int). take_bit (min LENGTH('a) n) k OR of_bool (n < LENGTH('a) \<and> bit k n) * NOT (mask n)\<close>
 | |
| 1074 | let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close> | |
| 1075 | have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close> | |
| 1076 | by transfer_prover | |
| 1077 |   also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close>
 | |
| 1078 | by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps) | |
| 1079 | also have \<open>?W = signed_take_bit\<close> | |
| 1080 | by (simp add: fun_eq_iff signed_take_bit_def) | |
| 1081 | finally show ?thesis . | |
| 1082 | qed | |
| 1083 | ||
| 1084 | end | |
| 1085 | ||
| 74097 | 1086 | end | 
| 1087 | ||
| 72244 | 1088 | |
| 1089 | subsection \<open>Conversions including casts\<close> | |
| 1090 | ||
| 72262 | 1091 | subsubsection \<open>Generic unsigned conversion\<close> | 
| 1092 | ||
| 1093 | context semiring_bits | |
| 1094 | begin | |
| 1095 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 1096 | lemma bit_unsigned_iff [bit_simps]: | 
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1097 |   \<open>bit (unsigned w) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit w n\<close>
 | 
| 72262 | 1098 | for w :: \<open>'b::len word\<close> | 
| 1099 | by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff) | |
| 1100 | ||
| 1101 | end | |
| 1102 | ||
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1103 | lemma possible_bit_word[simp]: | 
| 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1104 |   \<open>possible_bit TYPE(('a :: len) word) m \<longleftrightarrow> m < LENGTH('a)\<close>
 | 
| 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1105 | by (simp add: possible_bit_def linorder_not_le) | 
| 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1106 | |
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1107 | context semiring_bit_operations | 
| 72262 | 1108 | begin | 
| 1109 | ||
| 74592 | 1110 | lemma unsigned_minus_1_eq_mask: | 
| 1111 |   \<open>unsigned (- 1 :: 'b::len word) = mask LENGTH('b)\<close>
 | |
| 1112 | by (transfer fixing: mask) (simp add: nat_mask_eq of_nat_mask_eq) | |
| 1113 | ||
| 72262 | 1114 | lemma unsigned_push_bit_eq: | 
| 1115 |   \<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close>
 | |
| 1116 | for w :: \<open>'b::len word\<close> | |
| 1117 | proof (rule bit_eqI) | |
| 1118 | fix m | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1119 |   assume \<open>possible_bit TYPE('a) m\<close>
 | 
| 72262 | 1120 |   show \<open>bit (unsigned (push_bit n w)) m = bit (take_bit LENGTH('b) (push_bit n (unsigned w))) m\<close>
 | 
| 1121 | proof (cases \<open>n \<le> m\<close>) | |
| 1122 | case True | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1123 |     with \<open>possible_bit TYPE('a) m\<close> have \<open>possible_bit TYPE('a) (m - n)\<close>
 | 
| 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1124 | by (simp add: possible_bit_less_imp) | 
| 72262 | 1125 | with True show ?thesis | 
| 74101 | 1126 | by (simp add: bit_unsigned_iff bit_push_bit_iff Bit_Operations.bit_push_bit_iff bit_take_bit_iff not_le ac_simps) | 
| 72262 | 1127 | next | 
| 1128 | case False | |
| 1129 | then show ?thesis | |
| 74101 | 1130 | by (simp add: not_le bit_unsigned_iff bit_push_bit_iff Bit_Operations.bit_push_bit_iff bit_take_bit_iff) | 
| 72262 | 1131 | qed | 
| 1132 | qed | |
| 1133 | ||
| 1134 | lemma unsigned_take_bit_eq: | |
| 1135 | \<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close> | |
| 1136 | for w :: \<open>'b::len word\<close> | |
| 74101 | 1137 | by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Bit_Operations.bit_take_bit_iff) | 
| 72262 | 1138 | |
| 1139 | end | |
| 1140 | ||
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1141 | context unique_euclidean_semiring_with_bit_operations | 
| 72512 | 1142 | begin | 
| 1143 | ||
| 1144 | lemma unsigned_drop_bit_eq: | |
| 1145 |   \<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close>
 | |
| 1146 | for w :: \<open>'b::len word\<close> | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1147 | by (rule bit_eqI) (auto simp add: bit_unsigned_iff bit_take_bit_iff bit_drop_bit_eq Bit_Operations.bit_drop_bit_eq possible_bit_def dest: bit_imp_le_length) | 
| 72512 | 1148 | |
| 1149 | end | |
| 1150 | ||
| 73853 | 1151 | lemma ucast_drop_bit_eq: | 
| 1152 | \<open>ucast (drop_bit n w) = drop_bit n (ucast w :: 'b::len word)\<close> | |
| 1153 |   if \<open>LENGTH('a) \<le> LENGTH('b)\<close> for w :: \<open>'a::len word\<close>
 | |
| 1154 | by (rule bit_word_eqI) (use that in \<open>auto simp add: bit_unsigned_iff bit_drop_bit_eq dest: bit_imp_le_length\<close>) | |
| 1155 | ||
| 72262 | 1156 | context semiring_bit_operations | 
| 1157 | begin | |
| 1158 | ||
| 74097 | 1159 | context | 
| 1160 | includes bit_operations_syntax | |
| 1161 | begin | |
| 1162 | ||
| 72262 | 1163 | lemma unsigned_and_eq: | 
| 1164 | \<open>unsigned (v AND w) = unsigned v AND unsigned w\<close> | |
| 1165 | for v w :: \<open>'b::len word\<close> | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1166 | by (simp add: bit_eq_iff bit_simps) | 
| 72262 | 1167 | |
| 1168 | lemma unsigned_or_eq: | |
| 1169 | \<open>unsigned (v OR w) = unsigned v OR unsigned w\<close> | |
| 1170 | for v w :: \<open>'b::len word\<close> | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1171 | by (simp add: bit_eq_iff bit_simps) | 
| 72262 | 1172 | |
| 1173 | lemma unsigned_xor_eq: | |
| 1174 | \<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close> | |
| 1175 | for v w :: \<open>'b::len word\<close> | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1176 | by (simp add: bit_eq_iff bit_simps) | 
| 72262 | 1177 | |
| 1178 | end | |
| 1179 | ||
| 74097 | 1180 | end | 
| 1181 | ||
| 72262 | 1182 | context ring_bit_operations | 
| 1183 | begin | |
| 1184 | ||
| 74097 | 1185 | context | 
| 1186 | includes bit_operations_syntax | |
| 1187 | begin | |
| 1188 | ||
| 72262 | 1189 | lemma unsigned_not_eq: | 
| 1190 |   \<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close>
 | |
| 1191 | for w :: \<open>'b::len word\<close> | |
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1192 | by (simp add: bit_eq_iff bit_simps) | 
| 72262 | 1193 | |
| 1194 | end | |
| 1195 | ||
| 74097 | 1196 | end | 
| 1197 | ||
| 72262 | 1198 | context unique_euclidean_semiring_numeral | 
| 1199 | begin | |
| 1200 | ||
| 72292 | 1201 | lemma unsigned_greater_eq [simp]: | 
| 72262 | 1202 | \<open>0 \<le> unsigned w\<close> for w :: \<open>'b::len word\<close> | 
| 1203 | by (transfer fixing: less_eq) simp | |
| 1204 | ||
| 72292 | 1205 | lemma unsigned_less [simp]: | 
| 72262 | 1206 |   \<open>unsigned w < 2 ^ LENGTH('b)\<close> for w :: \<open>'b::len word\<close>
 | 
| 1207 | by (transfer fixing: less) simp | |
| 1208 | ||
| 1209 | end | |
| 1210 | ||
| 1211 | context linordered_semidom | |
| 1212 | begin | |
| 1213 | ||
| 1214 | lemma word_less_eq_iff_unsigned: | |
| 1215 | "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" | |
| 1216 | by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) | |
| 1217 | ||
| 1218 | lemma word_less_iff_unsigned: | |
| 1219 | "a < b \<longleftrightarrow> unsigned a < unsigned b" | |
| 1220 | by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) | |
| 1221 | ||
| 1222 | end | |
| 1223 | ||
| 1224 | ||
| 1225 | subsubsection \<open>Generic signed conversion\<close> | |
| 1226 | ||
| 1227 | context ring_bit_operations | |
| 1228 | begin | |
| 1229 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 1230 | lemma bit_signed_iff [bit_simps]: | 
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1231 |   \<open>bit (signed w) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit w (min (LENGTH('b) - Suc 0) n)\<close>
 | 
| 72262 | 1232 | for w :: \<open>'b::len word\<close> | 
| 1233 | by (transfer fixing: bit) | |
| 1234 | (auto simp add: bit_of_int_iff Bit_Operations.bit_signed_take_bit_iff min_def) | |
| 1235 | ||
| 1236 | lemma signed_push_bit_eq: | |
| 1237 |   \<open>signed (push_bit n w) = signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))\<close>
 | |
| 1238 | for w :: \<open>'b::len word\<close> | |
| 74496 | 1239 | apply (simp add: bit_eq_iff bit_simps possible_bit_less_imp min_less_iff_disj) | 
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1240 | apply (cases n, simp_all add: min_def) | 
| 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1241 | done | 
| 72262 | 1242 | |
| 1243 | lemma signed_take_bit_eq: | |
| 1244 |   \<open>signed (take_bit n w) = (if n < LENGTH('b) then take_bit n (signed w) else signed w)\<close>
 | |
| 1245 | for w :: \<open>'b::len word\<close> | |
| 1246 |   by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>)
 | |
| 1247 | (auto simp add: Bit_Operations.signed_take_bit_take_bit Bit_Operations.take_bit_signed_take_bit take_bit_of_int min_def less_Suc_eq) | |
| 1248 | ||
| 74391 | 1249 | context | 
| 1250 | includes bit_operations_syntax | |
| 1251 | begin | |
| 1252 | ||
| 72262 | 1253 | lemma signed_not_eq: | 
| 1254 |   \<open>signed (NOT w) = signed_take_bit LENGTH('b) (NOT (signed w))\<close>
 | |
| 1255 | for w :: \<open>'b::len word\<close> | |
| 74592 | 1256 | by (simp add: bit_eq_iff bit_simps possible_bit_less_imp min_less_iff_disj) | 
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74163diff
changeset | 1257 | (auto simp: min_def) | 
| 72262 | 1258 | |
| 1259 | lemma signed_and_eq: | |
| 1260 | \<open>signed (v AND w) = signed v AND signed w\<close> | |
| 1261 | for v w :: \<open>'b::len word\<close> | |
| 1262 | by (rule bit_eqI) (simp add: bit_signed_iff bit_and_iff Bit_Operations.bit_and_iff) | |
| 1263 | ||
| 1264 | lemma signed_or_eq: | |
| 1265 | \<open>signed (v OR w) = signed v OR signed w\<close> | |
| 1266 | for v w :: \<open>'b::len word\<close> | |
| 1267 | by (rule bit_eqI) (simp add: bit_signed_iff bit_or_iff Bit_Operations.bit_or_iff) | |
| 1268 | ||
| 1269 | lemma signed_xor_eq: | |
| 1270 | \<open>signed (v XOR w) = signed v XOR signed w\<close> | |
| 1271 | for v w :: \<open>'b::len word\<close> | |
| 1272 | by (rule bit_eqI) (simp add: bit_signed_iff bit_xor_iff Bit_Operations.bit_xor_iff) | |
| 1273 | ||
| 1274 | end | |
| 1275 | ||
| 74097 | 1276 | end | 
| 1277 | ||
| 72262 | 1278 | |
| 1279 | subsubsection \<open>More\<close> | |
| 1280 | ||
| 1281 | lemma sint_greater_eq: | |
| 1282 |   \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> sint w\<close> for w :: \<open>'a::len word\<close>
 | |
| 1283 | proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>)
 | |
| 1284 | case True | |
| 1285 | then show ?thesis | |
| 1286 | by transfer (simp add: signed_take_bit_eq_if_negative minus_exp_eq_not_mask or_greater_eq ac_simps) | |
| 1287 | next | |
| 1288 |   have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close>
 | |
| 1289 | by simp | |
| 1290 | case False | |
| 1291 | then show ?thesis | |
| 1292 | by transfer (auto simp add: signed_take_bit_eq intro: order_trans *) | |
| 1293 | qed | |
| 1294 | ||
| 1295 | lemma sint_less: | |
| 1296 |   \<open>sint w < 2 ^ (LENGTH('a) - Suc 0)\<close> for w :: \<open>'a::len word\<close>
 | |
| 1297 |   by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer)
 | |
| 1298 | (simp_all add: signed_take_bit_eq signed_take_bit_def not_eq_complement mask_eq_exp_minus_1 OR_upper) | |
| 1299 | ||
| 1300 | lemma unat_div_distrib: | |
| 1301 | \<open>unat (v div w) = unat v div unat w\<close> | |
| 1302 | proof transfer | |
| 1303 | fix k l | |
| 1304 |   have \<open>nat (take_bit LENGTH('a) k) div nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close>
 | |
| 1305 | by (rule div_le_dividend) | |
| 1306 |   also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close>
 | |
| 1307 | by (simp add: nat_less_iff) | |
| 1308 |   finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k div take_bit LENGTH('a) l) =
 | |
| 1309 |     (nat \<circ> take_bit LENGTH('a)) k div (nat \<circ> take_bit LENGTH('a)) l\<close>
 | |
| 1310 | by (simp add: nat_take_bit_eq div_int_pos_iff nat_div_distrib take_bit_nat_eq_self_iff) | |
| 1311 | qed | |
| 1312 | ||
| 1313 | lemma unat_mod_distrib: | |
| 1314 | \<open>unat (v mod w) = unat v mod unat w\<close> | |
| 1315 | proof transfer | |
| 1316 | fix k l | |
| 1317 |   have \<open>nat (take_bit LENGTH('a) k) mod nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close>
 | |
| 1318 | by (rule mod_less_eq_dividend) | |
| 1319 |   also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close>
 | |
| 1320 | by (simp add: nat_less_iff) | |
| 1321 |   finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k mod take_bit LENGTH('a) l) =
 | |
| 1322 |     (nat \<circ> take_bit LENGTH('a)) k mod (nat \<circ> take_bit LENGTH('a)) l\<close>
 | |
| 1323 | by (simp add: nat_take_bit_eq mod_int_pos_iff less_le nat_mod_distrib take_bit_nat_eq_self_iff) | |
| 1324 | qed | |
| 1325 | ||
| 1326 | lemma uint_div_distrib: | |
| 1327 | \<open>uint (v div w) = uint v div uint w\<close> | |
| 1328 | proof - | |
| 1329 | have \<open>int (unat (v div w)) = int (unat v div unat w)\<close> | |
| 1330 | by (simp add: unat_div_distrib) | |
| 1331 | then show ?thesis | |
| 1332 | by (simp add: of_nat_div) | |
| 1333 | qed | |
| 1334 | ||
| 72388 | 1335 | lemma unat_drop_bit_eq: | 
| 1336 | \<open>unat (drop_bit n w) = drop_bit n (unat w)\<close> | |
| 1337 | by (rule bit_eqI) (simp add: bit_unsigned_iff bit_drop_bit_eq) | |
| 1338 | ||
| 72262 | 1339 | lemma uint_mod_distrib: | 
| 1340 | \<open>uint (v mod w) = uint v mod uint w\<close> | |
| 1341 | proof - | |
| 1342 | have \<open>int (unat (v mod w)) = int (unat v mod unat w)\<close> | |
| 1343 | by (simp add: unat_mod_distrib) | |
| 1344 | then show ?thesis | |
| 1345 | by (simp add: of_nat_mod) | |
| 1346 | qed | |
| 1347 | ||
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 1348 | context semiring_bit_operations | 
| 72262 | 1349 | begin | 
| 1350 | ||
| 1351 | lemma unsigned_ucast_eq: | |
| 1352 |   \<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close>
 | |
| 1353 | for w :: \<open>'b::len word\<close> | |
| 74101 | 1354 | by (rule bit_eqI) (simp add: bit_unsigned_iff Word.bit_unsigned_iff bit_take_bit_iff not_le) | 
| 72262 | 1355 | |
| 1356 | end | |
| 1357 | ||
| 1358 | context ring_bit_operations | |
| 1359 | begin | |
| 1360 | ||
| 1361 | lemma signed_ucast_eq: | |
| 1362 |   \<open>signed (ucast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)\<close>
 | |
| 1363 | for w :: \<open>'b::len word\<close> | |
| 74592 | 1364 | by (simp add: bit_eq_iff bit_simps min_less_iff_disj) | 
| 72262 | 1365 | |
| 1366 | lemma signed_scast_eq: | |
| 1367 |   \<open>signed (scast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (signed w)\<close>
 | |
| 1368 | for w :: \<open>'b::len word\<close> | |
| 74496 | 1369 | by (simp add: bit_eq_iff bit_simps min_less_iff_disj) | 
| 72262 | 1370 | |
| 1371 | end | |
| 1372 | ||
| 72244 | 1373 | lemma uint_nonnegative: "0 \<le> uint w" | 
| 72262 | 1374 | by (fact unsigned_greater_eq) | 
| 72244 | 1375 | |
| 1376 | lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
 | |
| 1377 | for w :: "'a::len word" | |
| 72262 | 1378 | by (fact unsigned_less) | 
| 72244 | 1379 | |
| 1380 | lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
 | |
| 1381 | for w :: "'a::len word" | |
| 72262 | 1382 | by transfer (simp add: take_bit_eq_mod) | 
| 72244 | 1383 | |
| 1384 | lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b" | |
| 72262 | 1385 | by (fact unsigned_word_eqI) | 
| 72244 | 1386 | |
| 1387 | lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b" | |
| 72262 | 1388 | by (fact word_eq_iff_unsigned) | 
| 1389 | ||
| 1390 | lemma uint_word_of_int_eq: | |
| 72244 | 1391 |   \<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
 | 
| 1392 | by transfer rule | |
| 1393 | ||
| 1394 | lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
 | |
| 1395 | by (simp add: uint_word_of_int_eq take_bit_eq_mod) | |
| 1396 | ||
| 1397 | lemma word_of_int_uint: "word_of_int (uint w) = w" | |
| 1398 | by transfer simp | |
| 1399 | ||
| 1400 | lemma word_div_def [code]: | |
| 1401 | "a div b = word_of_int (uint a div uint b)" | |
| 1402 | by transfer rule | |
| 1403 | ||
| 1404 | lemma word_mod_def [code]: | |
| 1405 | "a mod b = word_of_int (uint a mod uint b)" | |
| 1406 | by transfer rule | |
| 1407 | ||
| 1408 | lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" | |
| 1409 | proof | |
| 1410 | fix x :: "'a word" | |
| 1411 | assume "\<And>x. PROP P (word_of_int x)" | |
| 1412 | then have "PROP P (word_of_int (uint x))" . | |
| 72262 | 1413 | then show "PROP P x" | 
| 1414 | by (simp only: word_of_int_uint) | |
| 72244 | 1415 | qed | 
| 1416 | ||
| 72262 | 1417 | lemma sint_uint: | 
| 1418 |   \<open>sint w = signed_take_bit (LENGTH('a) - Suc 0) (uint w)\<close>
 | |
| 72244 | 1419 | for w :: \<open>'a::len word\<close> | 
| 1420 |   by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit)
 | |
| 1421 | ||
| 72262 | 1422 | lemma unat_eq_nat_uint: | 
| 72244 | 1423 | \<open>unat w = nat (uint w)\<close> | 
| 1424 | by simp | |
| 1425 | ||
| 72262 | 1426 | lemma ucast_eq: | 
| 72244 | 1427 | \<open>ucast w = word_of_int (uint w)\<close> | 
| 1428 | by transfer simp | |
| 1429 | ||
| 72262 | 1430 | lemma scast_eq: | 
| 72244 | 1431 | \<open>scast w = word_of_int (sint w)\<close> | 
| 1432 | by transfer simp | |
| 1433 | ||
| 72262 | 1434 | lemma uint_0_eq: | 
| 72244 | 1435 | \<open>uint 0 = 0\<close> | 
| 72262 | 1436 | by (fact unsigned_0) | 
| 1437 | ||
| 1438 | lemma uint_1_eq: | |
| 72244 | 1439 | \<open>uint 1 = 1\<close> | 
| 72262 | 1440 | by (fact unsigned_1) | 
| 72244 | 1441 | |
| 1442 | lemma word_m1_wi: "- 1 = word_of_int (- 1)" | |
| 72262 | 1443 | by simp | 
| 72244 | 1444 | |
| 1445 | lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0" | |
| 72262 | 1446 | by (auto simp add: unsigned_word_eqI) | 
| 72244 | 1447 | |
| 1448 | lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0" | |
| 72262 | 1449 | by (auto simp add: unsigned_word_eqI) | 
| 1450 | ||
| 1451 | lemma unat_0: "unat 0 = 0" | |
| 1452 | by (fact unsigned_0) | |
| 72244 | 1453 | |
| 1454 | lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0" | |
| 1455 | by (auto simp: unat_0_iff [symmetric]) | |
| 1456 | ||
| 72262 | 1457 | lemma ucast_0: "ucast 0 = 0" | 
| 1458 | by (fact unsigned_0) | |
| 1459 | ||
| 1460 | lemma sint_0: "sint 0 = 0" | |
| 1461 | by (fact signed_0) | |
| 1462 | ||
| 1463 | lemma scast_0: "scast 0 = 0" | |
| 1464 | by (fact signed_0) | |
| 1465 | ||
| 1466 | lemma sint_n1: "sint (- 1) = - 1" | |
| 1467 | by (fact signed_minus_1) | |
| 1468 | ||
| 1469 | lemma scast_n1: "scast (- 1) = - 1" | |
| 1470 | by (fact signed_minus_1) | |
| 72244 | 1471 | |
| 1472 | lemma uint_1: "uint (1::'a::len word) = 1" | |
| 1473 | by (fact uint_1_eq) | |
| 1474 | ||
| 72262 | 1475 | lemma unat_1: "unat (1::'a::len word) = 1" | 
| 1476 | by (fact unsigned_1) | |
| 1477 | ||
| 1478 | lemma ucast_1: "ucast (1::'a::len word) = 1" | |
| 1479 | by (fact unsigned_1) | |
| 72244 | 1480 | |
| 1481 | instantiation word :: (len) size | |
| 1482 | begin | |
| 1483 | ||
| 1484 | lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close> | |
| 1485 |   is \<open>\<lambda>_. LENGTH('a)\<close> ..
 | |
| 1486 | ||
| 1487 | instance .. | |
| 1488 | ||
| 1489 | end | |
| 1490 | ||
| 1491 | lemma word_size [code]: | |
| 1492 |   \<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
 | |
| 1493 | by (fact size_word.rep_eq) | |
| 1494 | ||
| 1495 | lemma word_size_gt_0 [iff]: "0 < size w" | |
| 1496 | for w :: "'a::len word" | |
| 1497 | by (simp add: word_size) | |
| 1498 | ||
| 1499 | lemmas lens_gt_0 = word_size_gt_0 len_gt_0 | |
| 1500 | ||
| 1501 | lemma lens_not_0 [iff]: | |
| 1502 | \<open>size w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> | |
| 1503 | by auto | |
| 1504 | ||
| 1505 | lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close>
 | |
| 1506 |   is \<open>\<lambda>_. LENGTH('a)\<close> .
 | |
| 1507 | ||
| 1508 | lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close>
 | |
| 1509 |   is \<open>\<lambda>_. LENGTH('b)\<close> ..
 | |
| 1510 | ||
| 1511 | lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
 | |
| 1512 |   is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> ..
 | |
| 1513 | ||
| 1514 | lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
 | |
| 1515 |   is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> ..
 | |
| 1516 | ||
| 1517 | lemma is_up_eq: | |
| 1518 | \<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close> | |
| 1519 | for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 1520 | by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq) | |
| 1521 | ||
| 1522 | lemma is_down_eq: | |
| 1523 | \<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close> | |
| 1524 | for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 1525 | by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq) | |
| 1526 | ||
| 1527 | lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close> | |
| 1528 |   is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp
 | |
| 1529 | ||
| 1530 | lemma word_int_case_eq_uint [code]: | |
| 1531 | \<open>word_int_case f w = f (uint w)\<close> | |
| 1532 | by transfer simp | |
| 1533 | ||
| 1534 | translations | |
| 1535 | "case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x" | |
| 1536 | "case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x" | |
| 1537 | ||
| 1538 | ||
| 1539 | subsection \<open>Arithmetic operations\<close> | |
| 1540 | ||
| 74592 | 1541 | lemma div_word_self: | 
| 1542 | \<open>w div w = 1\<close> if \<open>w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> | |
| 1543 | using that by transfer simp | |
| 1544 | ||
| 1545 | lemma mod_word_self [simp]: | |
| 1546 | \<open>w mod w = 0\<close> for w :: \<open>'a::len word\<close> | |
| 1547 | apply (cases \<open>w = 0\<close>) | |
| 1548 | apply auto | |
| 1549 | using div_mult_mod_eq [of w w] by (simp add: div_word_self) | |
| 1550 | ||
| 1551 | lemma div_word_less: | |
| 1552 | \<open>w div v = 0\<close> if \<open>w < v\<close> for w v :: \<open>'a::len word\<close> | |
| 1553 | using that by transfer simp | |
| 1554 | ||
| 1555 | lemma mod_word_less: | |
| 1556 | \<open>w mod v = w\<close> if \<open>w < v\<close> for w v :: \<open>'a::len word\<close> | |
| 1557 | using div_mult_mod_eq [of w v] using that by (simp add: div_word_less) | |
| 1558 | ||
| 1559 | lemma div_word_one [simp]: | |
| 1560 | \<open>1 div w = of_bool (w = 1)\<close> for w :: \<open>'a::len word\<close> | |
| 1561 | proof transfer | |
| 1562 | fix k :: int | |
| 1563 |   show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) 1 div take_bit LENGTH('a) k) =
 | |
| 1564 |          take_bit LENGTH('a) (of_bool (take_bit LENGTH('a) k = take_bit LENGTH('a) 1))\<close>
 | |
| 1565 |   proof (cases \<open>take_bit LENGTH('a) k > 1\<close>)
 | |
| 1566 | case False | |
| 1567 |     with take_bit_nonnegative [of \<open>LENGTH('a)\<close> k]
 | |
| 1568 |     have \<open>take_bit LENGTH('a) k = 0 \<or> take_bit LENGTH('a) k = 1\<close>
 | |
| 1569 | by linarith | |
| 1570 | then show ?thesis | |
| 1571 | by auto | |
| 1572 | next | |
| 1573 | case True | |
| 1574 | then show ?thesis | |
| 1575 | by simp | |
| 1576 | qed | |
| 1577 | qed | |
| 1578 | ||
| 1579 | lemma mod_word_one [simp]: | |
| 1580 | \<open>1 mod w = 1 - w * of_bool (w = 1)\<close> for w :: \<open>'a::len word\<close> | |
| 75087 | 1581 | using div_mult_mod_eq [of 1 w] by auto | 
| 74592 | 1582 | |
| 1583 | lemma div_word_by_minus_1_eq [simp]: | |
| 1584 | \<open>w div - 1 = of_bool (w = - 1)\<close> for w :: \<open>'a::len word\<close> | |
| 1585 | by (auto intro: div_word_less simp add: div_word_self word_order.not_eq_extremum) | |
| 1586 | ||
| 1587 | lemma mod_word_by_minus_1_eq [simp]: | |
| 1588 | \<open>w mod - 1 = w * of_bool (w < - 1)\<close> for w :: \<open>'a::len word\<close> | |
| 75087 | 1589 | proof (cases \<open>w = - 1\<close>) | 
| 1590 | case True | |
| 1591 | then show ?thesis | |
| 1592 | by simp | |
| 1593 | next | |
| 1594 | case False | |
| 1595 | moreover have \<open>w < - 1\<close> | |
| 1596 | using False by (simp add: word_order.not_eq_extremum) | |
| 1597 | ultimately show ?thesis | |
| 1598 | by (simp add: mod_word_less) | |
| 1599 | qed | |
| 74592 | 1600 | |
| 72244 | 1601 | text \<open>Legacy theorems:\<close> | 
| 1602 | ||
| 1603 | lemma word_add_def [code]: | |
| 1604 | "a + b = word_of_int (uint a + uint b)" | |
| 1605 | by transfer (simp add: take_bit_add) | |
| 1606 | ||
| 1607 | lemma word_sub_wi [code]: | |
| 1608 | "a - b = word_of_int (uint a - uint b)" | |
| 1609 | by transfer (simp add: take_bit_diff) | |
| 1610 | ||
| 1611 | lemma word_mult_def [code]: | |
| 1612 | "a * b = word_of_int (uint a * uint b)" | |
| 1613 | by transfer (simp add: take_bit_eq_mod mod_simps) | |
| 1614 | ||
| 1615 | lemma word_minus_def [code]: | |
| 1616 | "- a = word_of_int (- uint a)" | |
| 1617 | by transfer (simp add: take_bit_minus) | |
| 1618 | ||
| 1619 | lemma word_0_wi: | |
| 1620 | "0 = word_of_int 0" | |
| 1621 | by transfer simp | |
| 1622 | ||
| 1623 | lemma word_1_wi: | |
| 1624 | "1 = word_of_int 1" | |
| 1625 | by transfer simp | |
| 1626 | ||
| 1627 | lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" | |
| 1628 | by (auto simp add: take_bit_eq_mod intro: mod_add_cong) | |
| 1629 | ||
| 1630 | lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1" | |
| 1631 | by (auto simp add: take_bit_eq_mod intro: mod_diff_cong) | |
| 1632 | ||
| 1633 | lemma word_succ_alt [code]: | |
| 1634 | "word_succ a = word_of_int (uint a + 1)" | |
| 1635 | by transfer (simp add: take_bit_eq_mod mod_simps) | |
| 1636 | ||
| 1637 | lemma word_pred_alt [code]: | |
| 1638 | "word_pred a = word_of_int (uint a - 1)" | |
| 1639 | by transfer (simp add: take_bit_eq_mod mod_simps) | |
| 1640 | ||
| 1641 | lemmas word_arith_wis = | |
| 1642 | word_add_def word_sub_wi word_mult_def | |
| 1643 | word_minus_def word_succ_alt word_pred_alt | |
| 1644 | word_0_wi word_1_wi | |
| 1645 | ||
| 1646 | lemma wi_homs: | |
| 1647 | shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" | |
| 1648 | and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)" | |
| 1649 | and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" | |
| 1650 | and wi_hom_neg: "- word_of_int a = word_of_int (- a)" | |
| 1651 | and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" | |
| 1652 | and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)" | |
| 1653 | by (transfer, simp)+ | |
| 1654 | ||
| 1655 | lemmas wi_hom_syms = wi_homs [symmetric] | |
| 1656 | ||
| 1657 | lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi | |
| 1658 | ||
| 1659 | lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] | |
| 1660 | ||
| 1661 | lemma double_eq_zero_iff: | |
| 1662 |   \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
 | |
| 1663 | for a :: \<open>'a::len word\<close> | |
| 1664 | proof - | |
| 1665 |   define n where \<open>n = LENGTH('a) - Suc 0\<close>
 | |
| 1666 |   then have *: \<open>LENGTH('a) = Suc n\<close>
 | |
| 1667 | by simp | |
| 1668 |   have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close>
 | |
| 1669 | using that by transfer | |
| 1670 | (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *) | |
| 1671 |   moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
 | |
| 1672 | by transfer simp | |
| 1673 |   then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
 | |
| 1674 | by (simp add: *) | |
| 1675 | ultimately show ?thesis | |
| 1676 | by auto | |
| 1677 | qed | |
| 1678 | ||
| 1679 | ||
| 1680 | subsection \<open>Ordering\<close> | |
| 1681 | ||
| 72388 | 1682 | lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> | 
| 1683 |   is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k \<le> signed_take_bit (LENGTH('a) - Suc 0) l\<close>
 | |
| 1684 | by (simp flip: signed_take_bit_decr_length_iff) | |
| 1685 | ||
| 1686 | lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> | |
| 1687 |   is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k < signed_take_bit (LENGTH('a) - Suc 0) l\<close>
 | |
| 72244 | 1688 | by (simp flip: signed_take_bit_decr_length_iff) | 
| 1689 | ||
| 72388 | 1690 | notation | 
| 1691 |   word_sle    ("'(\<le>s')") and
 | |
| 1692 |   word_sle    ("(_/ \<le>s _)"  [51, 51] 50) and
 | |
| 1693 |   word_sless  ("'(<s')") and
 | |
| 1694 |   word_sless  ("(_/ <s _)"  [51, 51] 50)
 | |
| 1695 | ||
| 1696 | notation (input) | |
| 1697 |   word_sle    ("(_/ <=s _)"  [51, 51] 50)
 | |
| 1698 | ||
| 72244 | 1699 | lemma word_sle_eq [code]: | 
| 1700 | \<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close> | |
| 1701 | by transfer simp | |
| 1702 | ||
| 1703 | lemma [code]: | |
| 1704 | \<open>a <s b \<longleftrightarrow> sint a < sint b\<close> | |
| 1705 | by transfer simp | |
| 1706 | ||
| 72388 | 1707 | lemma signed_ordering: \<open>ordering word_sle word_sless\<close> | 
| 1708 | apply (standard; transfer) | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1709 | using signed_take_bit_decr_length_iff by force+ | 
| 72388 | 1710 | |
| 1711 | lemma signed_linorder: \<open>class.linorder word_sle word_sless\<close> | |
| 1712 | by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff) | |
| 1713 | ||
| 1714 | interpretation signed: linorder word_sle word_sless | |
| 1715 | by (fact signed_linorder) | |
| 1716 | ||
| 1717 | lemma word_sless_eq: | |
| 1718 | \<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close> | |
| 1719 | by (fact signed.less_le) | |
| 1720 | ||
| 72244 | 1721 | lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b" | 
| 1722 | by (fact word_less_def) | |
| 1723 | ||
| 1724 | lemma word_zero_le [simp]: "0 \<le> y" | |
| 1725 | for y :: "'a::len word" | |
| 72388 | 1726 | by (fact word_coorder.extremum) | 
| 72244 | 1727 | |
| 1728 | lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *) | |
| 74592 | 1729 | by transfer (simp add: mask_eq_exp_minus_1) | 
| 72244 | 1730 | |
| 1731 | lemma word_n1_ge [simp]: "y \<le> -1" | |
| 1732 | for y :: "'a::len word" | |
| 1733 | by (fact word_order.extremum) | |
| 1734 | ||
| 1735 | lemmas word_not_simps [simp] = | |
| 1736 | word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] | |
| 1737 | ||
| 1738 | lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y" | |
| 1739 | for y :: "'a::len word" | |
| 1740 | by (simp add: less_le) | |
| 1741 | ||
| 1742 | lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y | |
| 1743 | ||
| 1744 | lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b" | |
| 1745 | by transfer simp | |
| 1746 | ||
| 1747 | lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b" | |
| 1748 | by transfer (simp add: nat_le_eq_zle) | |
| 1749 | ||
| 1750 | lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b" | |
| 1751 | by transfer (auto simp add: less_le [of 0]) | |
| 1752 | ||
| 1753 | lemmas unat_mono = word_less_nat_alt [THEN iffD1] | |
| 1754 | ||
| 1755 | instance word :: (len) wellorder | |
| 1756 | proof | |
| 1757 | fix P :: "'a word \<Rightarrow> bool" and a | |
| 1758 | assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)" | |
| 1759 | have "wf (measure unat)" .. | |
| 1760 |   moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat"
 | |
| 1761 | by (auto simp add: word_less_nat_alt) | |
| 1762 |   ultimately have "wf {(a, b :: ('a::len) word). a < b}"
 | |
| 1763 | by (rule wf_subset) | |
| 1764 | then show "P a" using * | |
| 1765 | by induction blast | |
| 1766 | qed | |
| 1767 | ||
| 1768 | lemma wi_less: | |
| 1769 | "(word_of_int n < (word_of_int m :: 'a::len word)) = | |
| 1770 |     (n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))"
 | |
| 1771 | by transfer (simp add: take_bit_eq_mod) | |
| 1772 | ||
| 1773 | lemma wi_le: | |
| 1774 | "(word_of_int n \<le> (word_of_int m :: 'a::len word)) = | |
| 1775 |     (n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))"
 | |
| 1776 | by transfer (simp add: take_bit_eq_mod) | |
| 1777 | ||
| 1778 | ||
| 1779 | subsection \<open>Bit-wise operations\<close> | |
| 1780 | ||
| 74097 | 1781 | context | 
| 1782 | includes bit_operations_syntax | |
| 1783 | begin | |
| 1784 | ||
| 72262 | 1785 | lemma uint_take_bit_eq: | 
| 72079 | 1786 | \<open>uint (take_bit n w) = take_bit n (uint w)\<close> | 
| 1787 | by transfer (simp add: ac_simps) | |
| 1788 | ||
| 72227 | 1789 | lemma take_bit_word_eq_self: | 
| 1790 |   \<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
 | |
| 1791 | using that by transfer simp | |
| 1792 | ||
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 1793 | lemma take_bit_length_eq [simp]: | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 1794 |   \<open>take_bit LENGTH('a) w = w\<close> for w :: \<open>'a::len word\<close>
 | 
| 72227 | 1795 | by (rule take_bit_word_eq_self) simp | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 1796 | |
| 71990 | 1797 | lemma bit_word_of_int_iff: | 
| 1798 |   \<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close>
 | |
| 1799 | by transfer rule | |
| 1800 | ||
| 1801 | lemma bit_uint_iff: | |
| 1802 |   \<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close>
 | |
| 1803 | for w :: \<open>'a::len word\<close> | |
| 1804 | by transfer (simp add: bit_take_bit_iff) | |
| 1805 | ||
| 1806 | lemma bit_sint_iff: | |
| 1807 |   \<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close>
 | |
| 1808 | for w :: \<open>'a::len word\<close> | |
| 72079 | 1809 | by transfer (auto simp add: bit_signed_take_bit_iff min_def le_less not_less) | 
| 71990 | 1810 | |
| 1811 | lemma bit_word_ucast_iff: | |
| 1812 |   \<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close>
 | |
| 1813 | for w :: \<open>'a::len word\<close> | |
| 72079 | 1814 | by transfer (simp add: bit_take_bit_iff ac_simps) | 
| 71990 | 1815 | |
| 1816 | lemma bit_word_scast_iff: | |
| 1817 | \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow> | |
| 1818 |     n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close>
 | |
| 1819 | for w :: \<open>'a::len word\<close> | |
| 72079 | 1820 | by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def) | 
| 1821 | ||
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 1822 | lemma bit_word_iff_drop_bit_and [code]: | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 1823 | \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 1824 | by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq) | 
| 72079 | 1825 | |
| 1826 | lemma | |
| 72262 | 1827 | word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))" | 
| 65268 | 1828 | and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" | 
| 1829 | and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" | |
| 1830 | and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1831 | by (transfer, simp add: take_bit_not_take_bit)+ | 
| 47374 
9475d524bafb
set up and use lift_definition for word operations
 huffman parents: 
47372diff
changeset | 1832 | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1833 | definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1834 | where [code_abbrev]: \<open>even_word = even\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1835 | |
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1836 | lemma even_word_iff [code]: | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1837 | \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1838 | by (simp add: and_one_eq even_iff_mod_2_eq_zero even_word_def) | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 1839 | |
| 72079 | 1840 | lemma map_bit_range_eq_if_take_bit_eq: | 
| 1841 | \<open>map (bit k) [0..<n] = map (bit l) [0..<n]\<close> | |
| 1842 | if \<open>take_bit n k = take_bit n l\<close> for k l :: int | |
| 1843 | using that proof (induction n arbitrary: k l) | |
| 1844 | case 0 | |
| 1845 | then show ?case | |
| 1846 | by simp | |
| 1847 | next | |
| 1848 | case (Suc n) | |
| 1849 | from Suc.prems have \<open>take_bit n (k div 2) = take_bit n (l div 2)\<close> | |
| 1850 | by (simp add: take_bit_Suc) | |
| 1851 | then have \<open>map (bit (k div 2)) [0..<n] = map (bit (l div 2)) [0..<n]\<close> | |
| 1852 | by (rule Suc.IH) | |
| 1853 | moreover have \<open>bit (r div 2) = bit r \<circ> Suc\<close> for r :: int | |
| 1854 | by (simp add: fun_eq_iff bit_Suc) | |
| 1855 | moreover from Suc.prems have \<open>even k \<longleftrightarrow> even l\<close> | |
| 1856 | by (auto simp add: take_bit_Suc elim!: evenE oddE) arith+ | |
| 1857 | ultimately show ?case | |
| 75085 | 1858 | by (simp only: map_Suc_upt upt_conv_Cons flip: list.map_comp) (simp add: bit_0) | 
| 72079 | 1859 | qed | 
| 1860 | ||
| 72262 | 1861 | lemma | 
| 1862 | take_bit_word_Bit0_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit0 m) :: 'a::len word) | |
| 1863 | = 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?P) | |
| 1864 | and take_bit_word_Bit1_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit1 m) :: 'a::len word) | |
| 1865 | = 1 + 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?Q) | |
| 1866 | and take_bit_word_minus_Bit0_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit0 m) :: 'a::len word) | |
| 1867 | = 2 * take_bit (pred_numeral n) (- numeral m)\<close> (is ?R) | |
| 1868 | and take_bit_word_minus_Bit1_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit1 m) :: 'a::len word) | |
| 1869 | = 1 + 2 * take_bit (pred_numeral n) (- numeral (Num.inc m))\<close> (is ?S) | |
| 1870 | proof - | |
| 1871 | define w :: \<open>'a::len word\<close> | |
| 1872 | where \<open>w = numeral m\<close> | |
| 1873 | moreover define q :: nat | |
| 1874 | where \<open>q = pred_numeral n\<close> | |
| 1875 | ultimately have num: | |
| 1876 | \<open>numeral m = w\<close> | |
| 1877 | \<open>numeral (num.Bit0 m) = 2 * w\<close> | |
| 1878 | \<open>numeral (num.Bit1 m) = 1 + 2 * w\<close> | |
| 1879 | \<open>numeral (Num.inc m) = 1 + w\<close> | |
| 1880 | \<open>pred_numeral n = q\<close> | |
| 1881 | \<open>numeral n = Suc q\<close> | |
| 1882 | by (simp_all only: w_def q_def numeral_Bit0 [of m] numeral_Bit1 [of m] ac_simps | |
| 1883 | numeral_inc numeral_eq_Suc flip: mult_2) | |
| 1884 | have even: \<open>take_bit (Suc q) (2 * w) = 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> | |
| 1885 | by (rule bit_word_eqI) | |
| 1886 | (auto simp add: bit_take_bit_iff bit_double_iff) | |
| 1887 | have odd: \<open>take_bit (Suc q) (1 + 2 * w) = 1 + 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> | |
| 1888 | by (rule bit_eqI) | |
| 1889 | (auto simp add: bit_take_bit_iff bit_double_iff even_bit_succ_iff) | |
| 1890 | show ?P | |
| 1891 | using even [of w] by (simp add: num) | |
| 1892 | show ?Q | |
| 1893 | using odd [of w] by (simp add: num) | |
| 1894 | show ?R | |
| 1895 | using even [of \<open>- w\<close>] by (simp add: num) | |
| 1896 | show ?S | |
| 1897 | using odd [of \<open>- (1 + w)\<close>] by (simp add: num) | |
| 1898 | qed | |
| 1899 | ||
| 72079 | 1900 | |
| 1901 | subsection \<open>More shift operations\<close> | |
| 1902 | ||
| 72388 | 1903 | lift_definition signed_drop_bit :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a::len word\<close> | 
| 1904 |   is \<open>\<lambda>n. drop_bit n \<circ> signed_take_bit (LENGTH('a) - Suc 0)\<close>
 | |
| 1905 | using signed_take_bit_decr_length_iff | |
| 1906 | by (simp add: take_bit_drop_bit) force | |
| 1907 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 1908 | lemma bit_signed_drop_bit_iff [bit_simps]: | 
| 72388 | 1909 |   \<open>bit (signed_drop_bit m w) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else m + n)\<close>
 | 
| 1910 | for w :: \<open>'a::len word\<close> | |
| 1911 | apply transfer | |
| 1912 | apply (auto simp add: bit_drop_bit_eq bit_signed_take_bit_iff not_le min_def) | |
| 1913 | apply (metis add.commute le_antisym less_diff_conv less_eq_decr_length_iff) | |
| 1914 | apply (metis le_antisym less_eq_decr_length_iff) | |
| 1915 | done | |
| 1916 | ||
| 72508 | 1917 | lemma [code]: | 
| 1918 |   \<open>Word.the_int (signed_drop_bit n w) = take_bit LENGTH('a) (drop_bit n (Word.the_signed_int w))\<close>
 | |
| 1919 | for w :: \<open>'a::len word\<close> | |
| 1920 | by transfer simp | |
| 1921 | ||
| 73816 | 1922 | lemma signed_drop_bit_of_0 [simp]: | 
| 1923 | \<open>signed_drop_bit n 0 = 0\<close> | |
| 1924 | by transfer simp | |
| 1925 | ||
| 1926 | lemma signed_drop_bit_of_minus_1 [simp]: | |
| 1927 | \<open>signed_drop_bit n (- 1) = - 1\<close> | |
| 1928 | by transfer simp | |
| 1929 | ||
| 72488 | 1930 | lemma signed_drop_bit_signed_drop_bit [simp]: | 
| 1931 | \<open>signed_drop_bit m (signed_drop_bit n w) = signed_drop_bit (m + n) w\<close> | |
| 1932 | for w :: \<open>'a::len word\<close> | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1933 | proof (cases \<open>LENGTH('a)\<close>)
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1934 | case 0 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1935 | then show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1936 | using len_not_eq_0 by blast | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1937 | next | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1938 | case (Suc n) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1939 | then show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1940 | by (force simp add: bit_signed_drop_bit_iff not_le less_diff_conv ac_simps intro!: bit_word_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1941 | qed | 
| 72488 | 1942 | |
| 72388 | 1943 | lemma signed_drop_bit_0 [simp]: | 
| 1944 | \<open>signed_drop_bit 0 w = w\<close> | |
| 72488 | 1945 | by transfer (simp add: take_bit_signed_take_bit) | 
| 72388 | 1946 | |
| 1947 | lemma sint_signed_drop_bit_eq: | |
| 1948 | \<open>sint (signed_drop_bit n w) = drop_bit n (sint w)\<close> | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1949 | proof (cases \<open>LENGTH('a) = 0 \<or> n=0\<close>)
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1950 | case False | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1951 | then show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1952 | apply simp | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1953 | apply (rule bit_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1954 | by (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1955 | qed auto | 
| 72388 | 1956 | |
| 37660 | 1957 | |
| 75623 | 1958 | subsection \<open>Single-bit operations\<close> | 
| 1959 | ||
| 1960 | lemma set_bit_eq_idem_iff: | |
| 1961 |   \<open>Bit_Operations.set_bit n w = w \<longleftrightarrow> bit w n \<or> n \<ge> LENGTH('a)\<close>
 | |
| 1962 | for w :: \<open>'a::len word\<close> | |
| 1963 | by (simp add: bit_eq_iff) (auto simp add: bit_simps not_le) | |
| 1964 | ||
| 1965 | lemma unset_bit_eq_idem_iff: | |
| 1966 |   \<open>unset_bit n w = w \<longleftrightarrow> bit w n \<longrightarrow> n \<ge> LENGTH('a)\<close>
 | |
| 1967 | for w :: \<open>'a::len word\<close> | |
| 1968 | by (simp add: bit_eq_iff) (auto simp add: bit_simps dest: bit_imp_le_length) | |
| 1969 | ||
| 1970 | lemma flip_bit_eq_idem_iff: | |
| 1971 |   \<open>flip_bit n w = w \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
 | |
| 1972 | for w :: \<open>'a::len word\<close> | |
| 1973 | using linorder_le_less_linear | |
| 1974 | by (simp add: bit_eq_iff) (auto simp add: bit_simps) | |
| 1975 | ||
| 1976 | ||
| 61799 | 1977 | subsection \<open>Rotation\<close> | 
| 37660 | 1978 | |
| 72079 | 1979 | lift_definition word_rotr :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> | 
| 1980 |   is \<open>\<lambda>n k. concat_bit (LENGTH('a) - n mod LENGTH('a))
 | |
| 1981 |     (drop_bit (n mod LENGTH('a)) (take_bit LENGTH('a) k))
 | |
| 1982 |     (take_bit (n mod LENGTH('a)) k)\<close>
 | |
| 1983 | subgoal for n k l | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1984 | by (simp add: concat_bit_def nat_le_iff less_imp_le | 
| 72079 | 1985 |       take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>n mod LENGTH('a::len)\<close>])
 | 
| 1986 | done | |
| 1987 | ||
| 1988 | lift_definition word_rotl :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> | |
| 1989 |   is \<open>\<lambda>n k. concat_bit (n mod LENGTH('a))
 | |
| 1990 |     (drop_bit (LENGTH('a) - n mod LENGTH('a)) (take_bit LENGTH('a) k))
 | |
| 1991 |     (take_bit (LENGTH('a) - n mod LENGTH('a)) k)\<close>
 | |
| 1992 | subgoal for n k l | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 1993 | by (simp add: concat_bit_def nat_le_iff less_imp_le | 
| 72079 | 1994 |       take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>LENGTH('a) - n mod LENGTH('a::len)\<close>])
 | 
| 1995 | done | |
| 1996 | ||
| 1997 | lift_definition word_roti :: \<open>int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> | |
| 1998 |   is \<open>\<lambda>r k. concat_bit (LENGTH('a) - nat (r mod int LENGTH('a)))
 | |
| 1999 |     (drop_bit (nat (r mod int LENGTH('a))) (take_bit LENGTH('a) k))
 | |
| 2000 |     (take_bit (nat (r mod int LENGTH('a))) k)\<close>
 | |
| 2001 | subgoal for r k l | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2002 | by (simp add: concat_bit_def nat_le_iff less_imp_le | 
| 72079 | 2003 |       take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>nat (r mod int LENGTH('a::len))\<close>])
 | 
| 2004 | done | |
| 2005 | ||
| 2006 | lemma word_rotl_eq_word_rotr [code]: | |
| 2007 |   \<open>word_rotl n = (word_rotr (LENGTH('a) - n mod LENGTH('a)) :: 'a::len word \<Rightarrow> 'a word)\<close>
 | |
| 2008 |   by (rule ext, cases \<open>n mod LENGTH('a) = 0\<close>; transfer) simp_all
 | |
| 2009 | ||
| 2010 | lemma word_roti_eq_word_rotr_word_rotl [code]: | |
| 2011 | \<open>word_roti i w = | |
| 2012 | (if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)\<close> | |
| 2013 | proof (cases \<open>i \<ge> 0\<close>) | |
| 2014 | case True | |
| 2015 | moreover define n where \<open>n = nat i\<close> | |
| 2016 | ultimately have \<open>i = int n\<close> | |
| 2017 | by simp | |
| 2018 | moreover have \<open>word_roti (int n) = (word_rotr n :: _ \<Rightarrow> 'a word)\<close> | |
| 2019 | by (rule ext, transfer) (simp add: nat_mod_distrib) | |
| 2020 | ultimately show ?thesis | |
| 2021 | by simp | |
| 2022 | next | |
| 2023 | case False | |
| 2024 | moreover define n where \<open>n = nat (- i)\<close> | |
| 2025 | ultimately have \<open>i = - int n\<close> \<open>n > 0\<close> | |
| 2026 | by simp_all | |
| 2027 | moreover have \<open>word_roti (- int n) = (word_rotl n :: _ \<Rightarrow> 'a word)\<close> | |
| 2028 | by (rule ext, transfer) | |
| 2029 | (simp add: zmod_zminus1_eq_if flip: of_nat_mod of_nat_diff) | |
| 2030 | ultimately show ?thesis | |
| 2031 | by simp | |
| 2032 | qed | |
| 2033 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 2034 | lemma bit_word_rotr_iff [bit_simps]: | 
| 72079 | 2035 | \<open>bit (word_rotr m w) n \<longleftrightarrow> | 
| 2036 |     n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close>
 | |
| 2037 | for w :: \<open>'a::len word\<close> | |
| 2038 | proof transfer | |
| 2039 | fix k :: int and m n :: nat | |
| 2040 |   define q where \<open>q = m mod LENGTH('a)\<close>
 | |
| 2041 |   have \<open>q < LENGTH('a)\<close> 
 | |
| 2042 | by (simp add: q_def) | |
| 2043 |   then have \<open>q \<le> LENGTH('a)\<close>
 | |
| 2044 | by simp | |
| 2045 |   have \<open>m mod LENGTH('a) = q\<close>
 | |
| 2046 | by (simp add: q_def) | |
| 2047 |   moreover have \<open>(n + m) mod LENGTH('a) = (n + q) mod LENGTH('a)\<close>
 | |
| 2048 |     by (subst mod_add_right_eq [symmetric]) (simp add: \<open>m mod LENGTH('a) = q\<close>)
 | |
| 2049 |   moreover have \<open>n < LENGTH('a) \<and>
 | |
| 2050 |     bit (concat_bit (LENGTH('a) - q) (drop_bit q (take_bit LENGTH('a) k)) (take_bit q k)) n \<longleftrightarrow>
 | |
| 2051 |     n < LENGTH('a) \<and> bit k ((n + q) mod LENGTH('a))\<close>
 | |
| 2052 |     using \<open>q < LENGTH('a)\<close>
 | |
| 2053 |     by (cases \<open>q + n \<ge> LENGTH('a)\<close>)
 | |
| 2054 | (auto simp add: bit_concat_bit_iff bit_drop_bit_eq | |
| 2055 | bit_take_bit_iff le_mod_geq ac_simps) | |
| 2056 |   ultimately show \<open>n < LENGTH('a) \<and>
 | |
| 2057 |     bit (concat_bit (LENGTH('a) - m mod LENGTH('a))
 | |
| 2058 |       (drop_bit (m mod LENGTH('a)) (take_bit LENGTH('a) k))
 | |
| 2059 |       (take_bit (m mod LENGTH('a)) k)) n
 | |
| 2060 |     \<longleftrightarrow> n < LENGTH('a) \<and>
 | |
| 2061 |       (n + m) mod LENGTH('a) < LENGTH('a) \<and>
 | |
| 2062 |       bit k ((n + m) mod LENGTH('a))\<close>
 | |
| 2063 | by simp | |
| 2064 | qed | |
| 2065 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 2066 | lemma bit_word_rotl_iff [bit_simps]: | 
| 72079 | 2067 | \<open>bit (word_rotl m w) n \<longleftrightarrow> | 
| 2068 |     n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close>
 | |
| 2069 | for w :: \<open>'a::len word\<close> | |
| 2070 | by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff) | |
| 2071 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 2072 | lemma bit_word_roti_iff [bit_simps]: | 
| 72079 | 2073 | \<open>bit (word_roti k w) n \<longleftrightarrow> | 
| 2074 |     n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close>
 | |
| 2075 | for w :: \<open>'a::len word\<close> | |
| 2076 | proof transfer | |
| 2077 | fix k l :: int and n :: nat | |
| 2078 |   define m where \<open>m = nat (k mod int LENGTH('a))\<close>
 | |
| 2079 |   have \<open>m < LENGTH('a)\<close> 
 | |
| 2080 | by (simp add: nat_less_iff m_def) | |
| 2081 |   then have \<open>m \<le> LENGTH('a)\<close>
 | |
| 2082 | by simp | |
| 2083 |   have \<open>k mod int LENGTH('a) = int m\<close>
 | |
| 2084 | by (simp add: nat_less_iff m_def) | |
| 2085 |   moreover have \<open>(int n + k) mod int LENGTH('a) = int ((n + m) mod LENGTH('a))\<close>
 | |
| 2086 |     by (subst mod_add_right_eq [symmetric]) (simp add: of_nat_mod \<open>k mod int LENGTH('a) = int m\<close>)
 | |
| 2087 |   moreover have \<open>n < LENGTH('a) \<and>
 | |
| 2088 |     bit (concat_bit (LENGTH('a) - m) (drop_bit m (take_bit LENGTH('a) l)) (take_bit m l)) n \<longleftrightarrow>
 | |
| 2089 |     n < LENGTH('a) \<and> bit l ((n + m) mod LENGTH('a))\<close>
 | |
| 2090 |     using \<open>m < LENGTH('a)\<close>
 | |
| 2091 |     by (cases \<open>m + n \<ge> LENGTH('a)\<close>)
 | |
| 2092 | (auto simp add: bit_concat_bit_iff bit_drop_bit_eq | |
| 2093 | bit_take_bit_iff nat_less_iff not_le not_less ac_simps | |
| 2094 | le_diff_conv le_mod_geq) | |
| 2095 |   ultimately show \<open>n < LENGTH('a)
 | |
| 2096 |     \<and> bit (concat_bit (LENGTH('a) - nat (k mod int LENGTH('a)))
 | |
| 2097 |              (drop_bit (nat (k mod int LENGTH('a))) (take_bit LENGTH('a) l))
 | |
| 2098 |              (take_bit (nat (k mod int LENGTH('a))) l)) n \<longleftrightarrow>
 | |
| 2099 |        n < LENGTH('a) 
 | |
| 2100 |     \<and> nat ((int n + k) mod int LENGTH('a)) < LENGTH('a)
 | |
| 2101 |     \<and> bit l (nat ((int n + k) mod int LENGTH('a)))\<close>
 | |
| 2102 | by simp | |
| 2103 | qed | |
| 2104 | ||
| 72262 | 2105 | lemma uint_word_rotr_eq: | 
| 72079 | 2106 |   \<open>uint (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a))
 | 
| 2107 |     (drop_bit (n mod LENGTH('a)) (uint w))
 | |
| 2108 |     (uint (take_bit (n mod LENGTH('a)) w))\<close>
 | |
| 2109 | for w :: \<open>'a::len word\<close> | |
| 74101 | 2110 | by transfer (simp add: take_bit_concat_bit_eq) | 
| 72079 | 2111 | |
| 72262 | 2112 | lemma [code]: | 
| 2113 |   \<open>Word.the_int (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a))
 | |
| 2114 |     (drop_bit (n mod LENGTH('a)) (Word.the_int w))
 | |
| 2115 |     (Word.the_int (take_bit (n mod LENGTH('a)) w))\<close>
 | |
| 2116 | for w :: \<open>'a::len word\<close> | |
| 2117 | using uint_word_rotr_eq [of n w] by simp | |
| 2118 | ||
| 72079 | 2119 | |
| 61799 | 2120 | subsection \<open>Split and cat operations\<close> | 
| 37660 | 2121 | |
| 72079 | 2122 | lift_definition word_cat :: \<open>'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word\<close> | 
| 2123 |   is \<open>\<lambda>k l. concat_bit LENGTH('b) l (take_bit LENGTH('a) k)\<close>
 | |
| 2124 | by (simp add: bit_eq_iff bit_concat_bit_iff bit_take_bit_iff) | |
| 65268 | 2125 | |
| 71990 | 2126 | lemma word_cat_eq: | 
| 2127 |   \<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close>
 | |
| 2128 | for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> | |
| 72128 | 2129 | by transfer (simp add: concat_bit_eq ac_simps) | 
| 72079 | 2130 | |
| 2131 | lemma word_cat_eq' [code]: | |
| 2132 |   \<open>word_cat a b = word_of_int (concat_bit LENGTH('b) (uint b) (uint a))\<close>
 | |
| 2133 | for a :: \<open>'a::len word\<close> and b :: \<open>'b::len word\<close> | |
| 72488 | 2134 | by transfer (simp add: concat_bit_take_bit_eq) | 
| 71990 | 2135 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 2136 | lemma bit_word_cat_iff [bit_simps]: | 
| 71990 | 2137 |   \<open>bit (word_cat v w :: 'c::len word) n \<longleftrightarrow> n < LENGTH('c) \<and> (if n < LENGTH('b) then bit w n else bit v (n - LENGTH('b)))\<close> 
 | 
| 2138 | for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> | |
| 72079 | 2139 | by transfer (simp add: bit_concat_bit_iff bit_take_bit_iff) | 
| 71990 | 2140 | |
| 72488 | 2141 | definition word_split :: \<open>'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word\<close> | 
| 2142 | where \<open>word_split w = | |
| 2143 |     (ucast (drop_bit LENGTH('c) w) :: 'b::len word, ucast w :: 'c::len word)\<close>
 | |
| 65268 | 2144 | |
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 2145 | definition word_rcat :: \<open>'a::len word list \<Rightarrow> 'b::len word\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 2146 |   where \<open>word_rcat = word_of_int \<circ> horner_sum uint (2 ^ LENGTH('a)) \<circ> rev\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 2147 | |
| 37660 | 2148 | |
| 72292 | 2149 | subsection \<open>More on conversions\<close> | 
| 2150 | ||
| 2151 | lemma int_word_sint: | |
| 2152 |   \<open>sint (word_of_int x :: 'a::len word) = (x + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)\<close>
 | |
| 72488 | 2153 | by transfer (simp flip: take_bit_eq_mod add: signed_take_bit_eq_take_bit_shift) | 
| 46010 | 2154 | |
| 72128 | 2155 | lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) bin"
 | 
| 74496 | 2156 | by (simp add: signed_of_int) | 
| 65268 | 2157 | |
| 72488 | 2158 | lemma uint_sint: "uint w = take_bit LENGTH('a) (sint w)"
 | 
| 65328 | 2159 | for w :: "'a::len word" | 
| 72488 | 2160 | by transfer (simp add: take_bit_signed_take_bit) | 
| 65268 | 2161 | |
| 72128 | 2162 | lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> take_bit n (uint w) = uint w"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2163 | for w :: "'a::len word" | 
| 72292 | 2164 | by transfer (simp add: min_def) | 
| 37660 | 2165 | |
| 46057 | 2166 | lemma wi_bintr: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2167 |   "LENGTH('a::len) \<le> n \<Longrightarrow>
 | 
| 72128 | 2168 | word_of_int (take_bit n w) = (word_of_int w :: 'a word)" | 
| 72292 | 2169 | by transfer simp | 
| 45805 | 2170 | |
| 65268 | 2171 | lemma word_numeral_alt: "numeral b = word_of_int (numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2172 | by (induct b, simp_all only: numeral.simps word_of_int_homs) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2173 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2174 | declare word_numeral_alt [symmetric, code_abbrev] | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2175 | |
| 65268 | 2176 | lemma word_neg_numeral_alt: "- numeral b = word_of_int (- numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 2177 | by (simp only: word_numeral_alt wi_hom_neg) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2178 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2179 | declare word_neg_numeral_alt [symmetric, code_abbrev] | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2180 | |
| 45805 | 2181 | lemma uint_bintrunc [simp]: | 
| 65268 | 2182 | "uint (numeral bin :: 'a word) = | 
| 72128 | 2183 |     take_bit (LENGTH('a::len)) (numeral bin)"
 | 
| 72292 | 2184 | by transfer rule | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2185 | |
| 65268 | 2186 | lemma uint_bintrunc_neg [simp]: | 
| 72128 | 2187 |   "uint (- numeral bin :: 'a word) = take_bit (LENGTH('a::len)) (- numeral bin)"
 | 
| 72292 | 2188 | by transfer rule | 
| 37660 | 2189 | |
| 45805 | 2190 | lemma sint_sbintrunc [simp]: | 
| 72128 | 2191 |   "sint (numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (numeral bin)"
 | 
| 72292 | 2192 | by transfer simp | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2193 | |
| 65268 | 2194 | lemma sint_sbintrunc_neg [simp]: | 
| 72128 | 2195 |   "sint (- numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (- numeral bin)"
 | 
| 72292 | 2196 | by transfer simp | 
| 37660 | 2197 | |
| 45805 | 2198 | lemma unat_bintrunc [simp]: | 
| 72128 | 2199 |   "unat (numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (numeral bin))"
 | 
| 72079 | 2200 | by transfer simp | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2201 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2202 | lemma unat_bintrunc_neg [simp]: | 
| 72128 | 2203 |   "unat (- numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (- numeral bin))"
 | 
| 72079 | 2204 | by transfer simp | 
| 37660 | 2205 | |
| 65328 | 2206 | lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2207 | for v w :: "'a::len word" | 
| 72292 | 2208 | by transfer simp | 
| 37660 | 2209 | |
| 65268 | 2210 | lemma uint_ge_0 [iff]: "0 \<le> uint x" | 
| 72292 | 2211 | by (fact unsigned_greater_eq) | 
| 45805 | 2212 | |
| 70185 | 2213 | lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2214 | for x :: "'a::len word" | 
| 72292 | 2215 | by (fact unsigned_less) | 
| 45805 | 2216 | |
| 70185 | 2217 | lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x"
 | 
| 65268 | 2218 | for x :: "'a::len word" | 
| 72292 | 2219 | using sint_greater_eq [of x] by simp | 
| 45805 | 2220 | |
| 70185 | 2221 | lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)"
 | 
| 65268 | 2222 | for x :: "'a::len word" | 
| 72292 | 2223 | using sint_less [of x] by simp | 
| 37660 | 2224 | |
| 70185 | 2225 | lemma uint_m2p_neg: "uint x - 2 ^ LENGTH('a) < 0"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2226 | for x :: "'a::len word" | 
| 45805 | 2227 | by (simp only: diff_less_0_iff_less uint_lt2p) | 
| 2228 | ||
| 70185 | 2229 | lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2230 | for x :: "'a::len word" | 
| 45805 | 2231 | by (simp only: not_le uint_m2p_neg) | 
| 37660 | 2232 | |
| 70185 | 2233 | lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2234 | for w :: "'a::len word" | 
| 72488 | 2235 | using uint_bounded [of w] by (rule less_le_trans) simp | 
| 37660 | 2236 | |
| 45805 | 2237 | lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0" | 
| 70749 
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
 paulson <lp15@cam.ac.uk> parents: 
70342diff
changeset | 2238 | by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) | 
| 37660 | 2239 | |
| 40827 
abbc05c20e24
code preprocessor setup for numerals on word type;
 haftmann parents: 
39910diff
changeset | 2240 | lemma uint_nat: "uint w = int (unat w)" | 
| 72079 | 2241 | by transfer simp | 
| 65268 | 2242 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2243 | lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
 | 
| 72292 | 2244 | by (simp flip: take_bit_eq_mod add: of_nat_take_bit) | 
| 65268 | 2245 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2246 | lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)"
 | 
| 72292 | 2247 | by (simp flip: take_bit_eq_mod add: of_nat_take_bit) | 
| 65268 | 2248 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2249 | lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
 | 
| 72079 | 2250 | by transfer (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) | 
| 37660 | 2251 | |
| 65268 | 2252 | lemma sint_numeral: | 
| 2253 | "sint (numeral b :: 'a::len word) = | |
| 72292 | 2254 |     (numeral b + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)"
 | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2255 | by (metis int_word_sint word_numeral_alt) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2256 | |
| 65268 | 2257 | lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" | 
| 72292 | 2258 | by (fact of_int_0) | 
| 45958 | 2259 | |
| 65268 | 2260 | lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" | 
| 72292 | 2261 | by (fact of_int_1) | 
| 45958 | 2262 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 2263 | lemma word_of_int_neg_1 [simp]: "word_of_int (- 1) = - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 2264 | by (simp add: wi_hom_syms) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 2265 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2266 | lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin" | 
| 72292 | 2267 | by (fact of_int_numeral) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2268 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2269 | lemma word_of_int_neg_numeral [simp]: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2270 | "(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin" | 
| 72292 | 2271 | by (fact of_int_neg_numeral) | 
| 65268 | 2272 | |
| 2273 | lemma word_int_case_wi: | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2274 |   "word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))"
 | 
| 72079 | 2275 | by transfer (simp add: take_bit_eq_mod) | 
| 65268 | 2276 | |
| 2277 | lemma word_int_split: | |
| 2278 | "P (word_int_case f x) = | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2279 |     (\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))"
 | 
| 72079 | 2280 | by transfer (auto simp add: take_bit_eq_mod) | 
| 65268 | 2281 | |
| 2282 | lemma word_int_split_asm: | |
| 2283 | "P (word_int_case f x) = | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2284 |     (\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))"
 | 
| 72079 | 2285 | by transfer (auto simp add: take_bit_eq_mod) | 
| 45805 | 2286 | |
| 65268 | 2287 | lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w" | 
| 72292 | 2288 | by transfer simp | 
| 37660 | 2289 | |
| 65268 | 2290 | lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)" | 
| 72488 | 2291 | by (simp add: word_size sint_greater_eq sint_less) | 
| 37660 | 2292 | |
| 65268 | 2293 | lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x" | 
| 2294 | for w :: "'a::len word" | |
| 45805 | 2295 | unfolding word_size by (rule less_le_trans [OF sint_lt]) | 
| 2296 | ||
| 65268 | 2297 | lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w" | 
| 2298 | for w :: "'a::len word" | |
| 45805 | 2299 | unfolding word_size by (rule order_trans [OF _ sint_ge]) | 
| 37660 | 2300 | |
| 74592 | 2301 | lemma word_unat_eq_iff: | 
| 2302 | \<open>v = w \<longleftrightarrow> unat v = unat w\<close> | |
| 2303 | for v w :: \<open>'a::len word\<close> | |
| 2304 | by (fact word_eq_iff_unsigned) | |
| 2305 | ||
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2306 | |
| 61799 | 2307 | subsection \<open>Testing bits\<close> | 
| 46010 | 2308 | |
| 72488 | 2309 | lemma bin_nth_uint_imp: "bit (uint w) n \<Longrightarrow> n < LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2310 | for w :: "'a::len word" | 
| 72292 | 2311 | by transfer (simp add: bit_take_bit_iff) | 
| 37660 | 2312 | |
| 46057 | 2313 | lemma bin_nth_sint: | 
| 70185 | 2314 |   "LENGTH('a) \<le> n \<Longrightarrow>
 | 
| 72488 | 2315 |     bit (sint w) n = bit (sint w) (LENGTH('a) - 1)"
 | 
| 65268 | 2316 | for w :: "'a::len word" | 
| 72292 | 2317 | by (transfer fixing: n) (simp add: bit_signed_take_bit_iff le_diff_conv min_def) | 
| 37660 | 2318 | |
| 2319 | lemma num_of_bintr': | |
| 72128 | 2320 |   "take_bit (LENGTH('a::len)) (numeral a :: int) = (numeral b) \<Longrightarrow>
 | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2321 | numeral a = (numeral b :: 'a word)" | 
| 72292 | 2322 | proof (transfer fixing: a b) | 
| 2323 |   assume \<open>take_bit LENGTH('a) (numeral a :: int) = numeral b\<close>
 | |
| 2324 |   then have \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close>
 | |
| 2325 | by simp | |
| 2326 |   then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close>
 | |
| 2327 | by simp | |
| 2328 | qed | |
| 37660 | 2329 | |
| 2330 | lemma num_of_sbintr': | |
| 72241 | 2331 |   "signed_take_bit (LENGTH('a::len) - 1) (numeral a :: int) = (numeral b) \<Longrightarrow>
 | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2332 | numeral a = (numeral b :: 'a word)" | 
| 72292 | 2333 | proof (transfer fixing: a b) | 
| 2334 |   assume \<open>signed_take_bit (LENGTH('a) - 1) (numeral a :: int) = numeral b\<close>
 | |
| 2335 |   then have \<open>take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close>
 | |
| 2336 | by simp | |
| 2337 |   then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close>
 | |
| 72488 | 2338 | by (simp add: take_bit_signed_take_bit) | 
| 72292 | 2339 | qed | 
| 2340 | ||
| 46962 
5bdcdb28be83
make more word theorems respect int/bin distinction
 huffman parents: 
46656diff
changeset | 2341 | lemma num_abs_bintr: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2342 | "(numeral x :: 'a word) = | 
| 72128 | 2343 |     word_of_int (take_bit (LENGTH('a::len)) (numeral x))"
 | 
| 72292 | 2344 | by transfer simp | 
| 46962 
5bdcdb28be83
make more word theorems respect int/bin distinction
 huffman parents: 
46656diff
changeset | 2345 | |
| 
5bdcdb28be83
make more word theorems respect int/bin distinction
 huffman parents: 
46656diff
changeset | 2346 | lemma num_abs_sbintr: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2347 | "(numeral x :: 'a word) = | 
| 72128 | 2348 |     word_of_int (signed_take_bit (LENGTH('a::len) - 1) (numeral x))"
 | 
| 72488 | 2349 | by transfer (simp add: take_bit_signed_take_bit) | 
| 46962 
5bdcdb28be83
make more word theorems respect int/bin distinction
 huffman parents: 
46656diff
changeset | 2350 | |
| 67408 | 2351 | text \<open> | 
| 2352 | \<open>cast\<close> -- note, no arg for new length, as it's determined by type of result, | |
| 2353 | thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>! | |
| 2354 | \<close> | |
| 37660 | 2355 | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 2356 | lemma bit_ucast_iff: | 
| 74101 | 2357 |   \<open>bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> bit a n\<close>
 | 
| 72079 | 2358 | by transfer (simp add: bit_take_bit_iff) | 
| 2359 | ||
| 2360 | lemma ucast_id [simp]: "ucast w = w" | |
| 2361 | by transfer simp | |
| 2362 | ||
| 2363 | lemma scast_id [simp]: "scast w = w" | |
| 72488 | 2364 | by transfer (simp add: take_bit_signed_take_bit) | 
| 37660 | 2365 | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 2366 | lemma ucast_mask_eq: | 
| 72082 | 2367 |   \<open>ucast (mask n :: 'b word) = mask (min LENGTH('b::len) n)\<close>
 | 
| 74101 | 2368 | by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff) | 
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 2369 | |
| 67408 | 2370 | \<comment> \<open>literal u(s)cast\<close> | 
| 46001 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
 huffman parents: 
46000diff
changeset | 2371 | lemma ucast_bintr [simp]: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2372 | "ucast (numeral w :: 'a::len word) = | 
| 72128 | 2373 |     word_of_int (take_bit (LENGTH('a)) (numeral w))"
 | 
| 72079 | 2374 | by transfer simp | 
| 65268 | 2375 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2376 | (* TODO: neg_numeral *) | 
| 37660 | 2377 | |
| 46001 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
 huffman parents: 
46000diff
changeset | 2378 | lemma scast_sbintr [simp]: | 
| 65268 | 2379 | "scast (numeral w ::'a::len word) = | 
| 72128 | 2380 |     word_of_int (signed_take_bit (LENGTH('a) - Suc 0) (numeral w))"
 | 
| 72079 | 2381 | by transfer simp | 
| 37660 | 2382 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2383 | lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)"
 | 
| 72079 | 2384 | by transfer simp | 
| 46011 | 2385 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2386 | lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)"
 | 
| 72079 | 2387 | by transfer simp | 
| 46011 | 2388 | |
| 70185 | 2389 | lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2390 | for c :: "'a::len word \<Rightarrow> 'b::len word" | 
| 72079 | 2391 | by transfer simp | 
| 65268 | 2392 | |
| 70185 | 2393 | lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2394 | for c :: "'a::len word \<Rightarrow> 'b::len word" | 
| 72079 | 2395 | by transfer simp | 
| 2396 | ||
| 2397 | lemma is_up_down: | |
| 2398 | \<open>is_up c \<longleftrightarrow> is_down d\<close> | |
| 2399 | for c :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 2400 | and d :: \<open>'b::len word \<Rightarrow> 'a::len word\<close> | |
| 2401 | by transfer simp | |
| 2402 | ||
| 2403 | context | |
| 2404 | fixes dummy_types :: \<open>'a::len \<times> 'b::len\<close> | |
| 2405 | begin | |
| 2406 | ||
| 2407 | private abbreviation (input) UCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 2408 | where \<open>UCAST == ucast\<close> | |
| 2409 | ||
| 2410 | private abbreviation (input) SCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | |
| 2411 | where \<open>SCAST == scast\<close> | |
| 2412 | ||
| 2413 | lemma down_cast_same: | |
| 2414 | \<open>UCAST = scast\<close> if \<open>is_down UCAST\<close> | |
| 2415 | by (rule ext, use that in transfer) (simp add: take_bit_signed_take_bit) | |
| 2416 | ||
| 2417 | lemma sint_up_scast: | |
| 2418 | \<open>sint (SCAST w) = sint w\<close> if \<open>is_up SCAST\<close> | |
| 2419 | using that by transfer (simp add: min_def Suc_leI le_diff_iff) | |
| 2420 | ||
| 2421 | lemma uint_up_ucast: | |
| 2422 | \<open>uint (UCAST w) = uint w\<close> if \<open>is_up UCAST\<close> | |
| 2423 | using that by transfer (simp add: min_def) | |
| 2424 | ||
| 2425 | lemma ucast_up_ucast: | |
| 2426 | \<open>ucast (UCAST w) = ucast w\<close> if \<open>is_up UCAST\<close> | |
| 2427 | using that by transfer (simp add: ac_simps) | |
| 2428 | ||
| 2429 | lemma ucast_up_ucast_id: | |
| 2430 | \<open>ucast (UCAST w) = w\<close> if \<open>is_up UCAST\<close> | |
| 2431 | using that by (simp add: ucast_up_ucast) | |
| 2432 | ||
| 2433 | lemma scast_up_scast: | |
| 2434 | \<open>scast (SCAST w) = scast w\<close> if \<open>is_up SCAST\<close> | |
| 2435 | using that by transfer (simp add: ac_simps) | |
| 2436 | ||
| 2437 | lemma scast_up_scast_id: | |
| 2438 | \<open>scast (SCAST w) = w\<close> if \<open>is_up SCAST\<close> | |
| 2439 | using that by (simp add: scast_up_scast) | |
| 2440 | ||
| 2441 | lemma isduu: | |
| 2442 | \<open>is_up UCAST\<close> if \<open>is_down d\<close> | |
| 2443 | for d :: \<open>'b word \<Rightarrow> 'a word\<close> | |
| 2444 | using that is_up_down [of UCAST d] by simp | |
| 2445 | ||
| 2446 | lemma isdus: | |
| 2447 | \<open>is_up SCAST\<close> if \<open>is_down d\<close> | |
| 2448 | for d :: \<open>'b word \<Rightarrow> 'a word\<close> | |
| 2449 | using that is_up_down [of SCAST d] by simp | |
| 2450 | ||
| 37660 | 2451 | lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] | 
| 72079 | 2452 | lemmas scast_down_scast_id = isdus [THEN scast_up_scast_id] | 
| 37660 | 2453 | |
| 2454 | lemma up_ucast_surj: | |
| 72079 | 2455 | \<open>surj (ucast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up UCAST\<close> | 
| 2456 | by (rule surjI) (use that in \<open>rule ucast_up_ucast_id\<close>) | |
| 37660 | 2457 | |
| 2458 | lemma up_scast_surj: | |
| 72079 | 2459 | \<open>surj (scast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up SCAST\<close> | 
| 2460 | by (rule surjI) (use that in \<open>rule scast_up_scast_id\<close>) | |
| 37660 | 2461 | |
| 2462 | lemma down_ucast_inj: | |
| 72079 | 2463 | \<open>inj_on UCAST A\<close> if \<open>is_down (ucast :: 'b word \<Rightarrow> 'a word)\<close> | 
| 2464 | by (rule inj_on_inverseI) (use that in \<open>rule ucast_down_ucast_id\<close>) | |
| 2465 | ||
| 2466 | lemma down_scast_inj: | |
| 2467 | \<open>inj_on SCAST A\<close> if \<open>is_down (scast :: 'b word \<Rightarrow> 'a word)\<close> | |
| 2468 | by (rule inj_on_inverseI) (use that in \<open>rule scast_down_scast_id\<close>) | |
| 2469 | ||
| 2470 | lemma ucast_down_wi: | |
| 2471 | \<open>UCAST (word_of_int x) = word_of_int x\<close> if \<open>is_down UCAST\<close> | |
| 2472 | using that by transfer simp | |
| 2473 | ||
| 2474 | lemma ucast_down_no: | |
| 2475 | \<open>UCAST (numeral bin) = numeral bin\<close> if \<open>is_down UCAST\<close> | |
| 2476 | using that by transfer simp | |
| 2477 | ||
| 2478 | end | |
| 37660 | 2479 | |
| 2480 | lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def | |
| 2481 | ||
| 72000 | 2482 | lemma bit_last_iff: | 
| 2483 |   \<open>bit w (LENGTH('a) - Suc 0) \<longleftrightarrow> sint w < 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
 | |
| 2484 | for w :: \<open>'a::len word\<close> | |
| 2485 | proof - | |
| 2486 |   have \<open>?P \<longleftrightarrow> bit (uint w) (LENGTH('a) - Suc 0)\<close>
 | |
| 2487 | by (simp add: bit_uint_iff) | |
| 2488 | also have \<open>\<dots> \<longleftrightarrow> ?Q\<close> | |
| 72010 | 2489 | by (simp add: sint_uint) | 
| 72000 | 2490 | finally show ?thesis . | 
| 2491 | qed | |
| 2492 | ||
| 2493 | lemma drop_bit_eq_zero_iff_not_bit_last: | |
| 2494 |   \<open>drop_bit (LENGTH('a) - Suc 0) w = 0 \<longleftrightarrow> \<not> bit w (LENGTH('a) - Suc 0)\<close>
 | |
| 2495 | for w :: "'a::len word" | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2496 | proof (cases \<open>LENGTH('a)\<close>)
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2497 | case (Suc n) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2498 | then show ?thesis | 
| 72000 | 2499 | apply transfer | 
| 2500 | apply (simp add: take_bit_drop_bit) | |
| 74101 | 2501 | by (simp add: bit_iff_odd_drop_bit drop_bit_take_bit odd_iff_mod_2_eq_one) | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2502 | qed auto | 
| 72000 | 2503 | |
| 74592 | 2504 | lemma unat_div: | 
| 2505 | \<open>unat (x div y) = unat x div unat y\<close> | |
| 2506 | by (fact unat_div_distrib) | |
| 2507 | ||
| 2508 | lemma unat_mod: | |
| 2509 | \<open>unat (x mod y) = unat x mod unat y\<close> | |
| 2510 | by (fact unat_mod_distrib) | |
| 2511 | ||
| 37660 | 2512 | |
| 61799 | 2513 | subsection \<open>Word Arithmetic\<close> | 
| 37660 | 2514 | |
| 74592 | 2515 | lemmas less_eq_word_numeral_numeral [simp] = | 
| 2516 | word_le_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2517 | for a b | |
| 2518 | lemmas less_word_numeral_numeral [simp] = | |
| 2519 | word_less_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2520 | for a b | |
| 2521 | lemmas less_eq_word_minus_numeral_numeral [simp] = | |
| 2522 | word_le_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2523 | for a b | |
| 2524 | lemmas less_word_minus_numeral_numeral [simp] = | |
| 2525 | word_less_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2526 | for a b | |
| 2527 | lemmas less_eq_word_numeral_minus_numeral [simp] = | |
| 2528 | word_le_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2529 | for a b | |
| 2530 | lemmas less_word_numeral_minus_numeral [simp] = | |
| 2531 | word_less_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2532 | for a b | |
| 2533 | lemmas less_eq_word_minus_numeral_minus_numeral [simp] = | |
| 2534 | word_le_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2535 | for a b | |
| 2536 | lemmas less_word_minus_numeral_minus_numeral [simp] = | |
| 2537 | word_less_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2538 | for a b | |
| 2539 | lemmas less_word_numeral_minus_1 [simp] = | |
| 2540 | word_less_def [of \<open>numeral a\<close> \<open>- 1\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2541 | for a b | |
| 2542 | lemmas less_word_minus_numeral_minus_1 [simp] = | |
| 2543 | word_less_def [of \<open>- numeral a\<close> \<open>- 1\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2544 | for a b | |
| 2545 | ||
| 2546 | lemmas sless_eq_word_numeral_numeral [simp] = | |
| 2547 | word_sle_eq [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2548 | for a b | |
| 2549 | lemmas sless_word_numeral_numeral [simp] = | |
| 2550 | word_sless_alt [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2551 | for a b | |
| 2552 | lemmas sless_eq_word_minus_numeral_numeral [simp] = | |
| 2553 | word_sle_eq [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2554 | for a b | |
| 2555 | lemmas sless_word_minus_numeral_numeral [simp] = | |
| 2556 | word_sless_alt [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2557 | for a b | |
| 2558 | lemmas sless_eq_word_numeral_minus_numeral [simp] = | |
| 2559 | word_sle_eq [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2560 | for a b | |
| 2561 | lemmas sless_word_numeral_minus_numeral [simp] = | |
| 2562 | word_sless_alt [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2563 | for a b | |
| 2564 | lemmas sless_eq_word_minus_numeral_minus_numeral [simp] = | |
| 2565 | word_sle_eq [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2566 | for a b | |
| 2567 | lemmas sless_word_minus_numeral_minus_numeral [simp] = | |
| 2568 | word_sless_alt [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg] | |
| 2569 | for a b | |
| 2570 | ||
| 2571 | lemmas div_word_numeral_numeral [simp] = | |
| 2572 | word_div_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2573 | for a b | |
| 2574 | lemmas div_word_minus_numeral_numeral [simp] = | |
| 2575 | word_div_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2576 | for a b | |
| 2577 | lemmas div_word_numeral_minus_numeral [simp] = | |
| 2578 | word_div_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2579 | for a b | |
| 2580 | lemmas div_word_minus_numeral_minus_numeral [simp] = | |
| 2581 | word_div_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2582 | for a b | |
| 2583 | lemmas div_word_minus_1_numeral [simp] = | |
| 2584 | word_div_def [of \<open>- 1\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2585 | for a b | |
| 2586 | lemmas div_word_minus_1_minus_numeral [simp] = | |
| 2587 | word_div_def [of \<open>- 1\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2588 | for a b | |
| 2589 | ||
| 2590 | lemmas mod_word_numeral_numeral [simp] = | |
| 2591 | word_mod_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2592 | for a b | |
| 2593 | lemmas mod_word_minus_numeral_numeral [simp] = | |
| 2594 | word_mod_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2595 | for a b | |
| 2596 | lemmas mod_word_numeral_minus_numeral [simp] = | |
| 2597 | word_mod_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2598 | for a b | |
| 2599 | lemmas mod_word_minus_numeral_minus_numeral [simp] = | |
| 2600 | word_mod_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2601 | for a b | |
| 2602 | lemmas mod_word_minus_1_numeral [simp] = | |
| 2603 | word_mod_def [of \<open>- 1\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2604 | for a b | |
| 2605 | lemmas mod_word_minus_1_minus_numeral [simp] = | |
| 2606 | word_mod_def [of \<open>- 1\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1] | |
| 2607 | for a b | |
| 2608 | ||
| 2609 | lemma signed_drop_bit_of_1 [simp]: | |
| 2610 |   \<open>signed_drop_bit n (1 :: 'a::len word) = of_bool (LENGTH('a) = 1 \<or> n = 0)\<close>
 | |
| 2611 | apply (transfer fixing: n) | |
| 2612 |   apply (cases \<open>LENGTH('a)\<close>)
 | |
| 2613 | apply (auto simp add: take_bit_signed_take_bit) | |
| 2614 | apply (auto simp add: take_bit_drop_bit gr0_conv_Suc simp flip: take_bit_eq_self_iff_drop_bit_eq_0) | |
| 2615 | done | |
| 2616 | ||
| 2617 | lemma take_bit_word_beyond_length_eq: | |
| 2618 |   \<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
 | |
| 2619 | using that by transfer simp | |
| 2620 | ||
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2621 | lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2622 | lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2623 | lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 2624 | lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b | 
| 72079 | 2625 | lemmas word_sless_no [simp] = word_sless_eq [of "numeral a" "numeral b"] for a b | 
| 2626 | lemmas word_sle_no [simp] = word_sle_eq [of "numeral a" "numeral b"] for a b | |
| 37660 | 2627 | |
| 65268 | 2628 | lemma size_0_same': "size w = 0 \<Longrightarrow> w = v" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2629 | for v w :: "'a::len word" | 
| 72079 | 2630 | by (unfold word_size) simp | 
| 37660 | 2631 | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 2632 | lemmas size_0_same = size_0_same' [unfolded word_size] | 
| 37660 | 2633 | |
| 2634 | lemmas unat_eq_0 = unat_0_iff | |
| 2635 | lemmas unat_eq_zero = unat_0_iff | |
| 2636 | ||
| 74592 | 2637 | lemma mask_1: "mask 1 = 1" | 
| 2638 | by simp | |
| 2639 | ||
| 2640 | lemma mask_Suc_0: "mask (Suc 0) = 1" | |
| 2641 | by simp | |
| 2642 | ||
| 2643 | lemma bin_last_bintrunc: "odd (take_bit l n) \<longleftrightarrow> l > 0 \<and> odd n" | |
| 2644 | by simp | |
| 2645 | ||
| 2646 | lemma push_bit_word_beyond [simp]: | |
| 2647 |   \<open>push_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
 | |
| 2648 | using that by (transfer fixing: n) (simp add: take_bit_push_bit) | |
| 2649 | ||
| 2650 | lemma drop_bit_word_beyond [simp]: | |
| 2651 |   \<open>drop_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
 | |
| 2652 | using that by (transfer fixing: n) (simp add: drop_bit_take_bit) | |
| 2653 | ||
| 2654 | lemma signed_drop_bit_beyond: | |
| 2655 |   \<open>signed_drop_bit n w = (if bit w (LENGTH('a) - Suc 0) then - 1 else 0)\<close>
 | |
| 2656 |   if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
 | |
| 2657 | by (rule bit_word_eqI) (simp add: bit_signed_drop_bit_iff that) | |
| 2658 | ||
| 2659 | lemma take_bit_numeral_minus_numeral_word [simp]: | |
| 2660 | \<open>take_bit (numeral m) (- numeral n :: 'a::len word) = | |
| 2661 | (case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> take_bit (numeral m) (2 ^ numeral m - numeral q))\<close> (is \<open>?lhs = ?rhs\<close>) | |
| 2662 | proof (cases \<open>LENGTH('a) \<le> numeral m\<close>)
 | |
| 2663 | case True | |
| 2664 | then have *: \<open>(take_bit (numeral m) :: 'a word \<Rightarrow> 'a word) = id\<close> | |
| 2665 | by (simp add: fun_eq_iff take_bit_word_eq_self) | |
| 2666 | have **: \<open>2 ^ numeral m = (0 :: 'a word)\<close> | |
| 2667 | using True by (simp flip: exp_eq_zero_iff) | |
| 2668 | show ?thesis | |
| 2669 | by (auto simp only: * ** split: option.split | |
| 2670 | dest!: take_bit_num_eq_None_imp [where ?'a = \<open>'a word\<close>] take_bit_num_eq_Some_imp [where ?'a = \<open>'a word\<close>]) | |
| 2671 | simp_all | |
| 2672 | next | |
| 2673 | case False | |
| 2674 | then show ?thesis | |
| 2675 | by (transfer fixing: m n) simp | |
| 2676 | qed | |
| 2677 | ||
| 2678 | lemma of_nat_inverse: | |
| 2679 |   \<open>word_of_nat r = a \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> unat a = r\<close>
 | |
| 2680 | for a :: \<open>'a::len word\<close> | |
| 2681 | by (metis id_apply of_nat_eq_id take_bit_nat_eq_self_iff unsigned_of_nat) | |
| 2682 | ||
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2683 | |
| 61799 | 2684 | subsection \<open>Transferring goals from words to ints\<close> | 
| 37660 | 2685 | |
| 65268 | 2686 | lemma word_ths: | 
| 2687 | shows word_succ_p1: "word_succ a = a + 1" | |
| 2688 | and word_pred_m1: "word_pred a = a - 1" | |
| 2689 | and word_pred_succ: "word_pred (word_succ a) = a" | |
| 2690 | and word_succ_pred: "word_succ (word_pred a) = a" | |
| 2691 | and word_mult_succ: "word_succ a * b = b + a * b" | |
| 47374 
9475d524bafb
set up and use lift_definition for word operations
 huffman parents: 
47372diff
changeset | 2692 | by (transfer, simp add: algebra_simps)+ | 
| 37660 | 2693 | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 2694 | lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y" | 
| 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 2695 | by simp | 
| 37660 | 2696 | |
| 55818 | 2697 | lemma uint_word_ariths: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2698 | fixes a b :: "'a::len word" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2699 |   shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)"
 | 
| 70185 | 2700 |     and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)"
 | 
| 2701 |     and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)"
 | |
| 2702 |     and "uint (- a) = - uint a mod 2 ^ LENGTH('a)"
 | |
| 2703 |     and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)"
 | |
| 2704 |     and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)"
 | |
| 2705 |     and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)"
 | |
| 2706 |     and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)"
 | |
| 72262 | 2707 | by (simp_all only: word_arith_wis uint_word_of_int_eq flip: take_bit_eq_mod) | 
| 55818 | 2708 | |
| 2709 | lemma uint_word_arith_bintrs: | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2710 | fixes a b :: "'a::len word" | 
| 72128 | 2711 |   shows "uint (a + b) = take_bit (LENGTH('a)) (uint a + uint b)"
 | 
| 2712 |     and "uint (a - b) = take_bit (LENGTH('a)) (uint a - uint b)"
 | |
| 2713 |     and "uint (a * b) = take_bit (LENGTH('a)) (uint a * uint b)"
 | |
| 2714 |     and "uint (- a) = take_bit (LENGTH('a)) (- uint a)"
 | |
| 2715 |     and "uint (word_succ a) = take_bit (LENGTH('a)) (uint a + 1)"
 | |
| 2716 |     and "uint (word_pred a) = take_bit (LENGTH('a)) (uint a - 1)"
 | |
| 2717 |     and "uint (0 :: 'a word) = take_bit (LENGTH('a)) 0"
 | |
| 2718 |     and "uint (1 :: 'a word) = take_bit (LENGTH('a)) 1"
 | |
| 2719 | by (simp_all add: uint_word_ariths take_bit_eq_mod) | |
| 55818 | 2720 | |
| 2721 | lemma sint_word_ariths: | |
| 2722 | fixes a b :: "'a::len word" | |
| 72128 | 2723 |   shows "sint (a + b) = signed_take_bit (LENGTH('a) - 1) (sint a + sint b)"
 | 
| 2724 |     and "sint (a - b) = signed_take_bit (LENGTH('a) - 1) (sint a - sint b)"
 | |
| 2725 |     and "sint (a * b) = signed_take_bit (LENGTH('a) - 1) (sint a * sint b)"
 | |
| 2726 |     and "sint (- a) = signed_take_bit (LENGTH('a) - 1) (- sint a)"
 | |
| 2727 |     and "sint (word_succ a) = signed_take_bit (LENGTH('a) - 1) (sint a + 1)"
 | |
| 2728 |     and "sint (word_pred a) = signed_take_bit (LENGTH('a) - 1) (sint a - 1)"
 | |
| 2729 |     and "sint (0 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 0"
 | |
| 2730 |     and "sint (1 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 1"
 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2731 | subgoal | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2732 | by transfer (simp add: signed_take_bit_add) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2733 | subgoal | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2734 | by transfer (simp add: signed_take_bit_diff) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2735 | subgoal | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2736 | by transfer (simp add: signed_take_bit_mult) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2737 | subgoal | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2738 | by transfer (simp add: signed_take_bit_minus) | 
| 72488 | 2739 | apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_succ) | 
| 2740 | apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_pred) | |
| 2741 | apply (simp_all add: sint_uint) | |
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
64243diff
changeset | 2742 | done | 
| 45604 | 2743 | |
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58061diff
changeset | 2744 | lemma word_pred_0_n1: "word_pred 0 = word_of_int (- 1)" | 
| 47374 
9475d524bafb
set up and use lift_definition for word operations
 huffman parents: 
47372diff
changeset | 2745 | unfolding word_pred_m1 by simp | 
| 37660 | 2746 | |
| 2747 | lemma succ_pred_no [simp]: | |
| 65268 | 2748 | "word_succ (numeral w) = numeral w + 1" | 
| 2749 | "word_pred (numeral w) = numeral w - 1" | |
| 2750 | "word_succ (- numeral w) = - numeral w + 1" | |
| 2751 | "word_pred (- numeral w) = - numeral w - 1" | |
| 2752 | by (simp_all add: word_succ_p1 word_pred_m1) | |
| 2753 | ||
| 2754 | lemma word_sp_01 [simp]: | |
| 2755 | "word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0" | |
| 2756 | by (simp_all add: word_succ_p1 word_pred_m1) | |
| 37660 | 2757 | |
| 67408 | 2758 | \<comment> \<open>alternative approach to lifting arithmetic equalities\<close> | 
| 65268 | 2759 | lemma word_of_int_Ex: "\<exists>y. x = word_of_int y" | 
| 37660 | 2760 | by (rule_tac x="uint x" in exI) simp | 
| 2761 | ||
| 2762 | ||
| 61799 | 2763 | subsection \<open>Order on fixed-length words\<close> | 
| 37660 | 2764 | |
| 72262 | 2765 | lift_definition udvd :: \<open>'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool\<close> (infixl \<open>udvd\<close> 50) | 
| 2766 |   is \<open>\<lambda>k l. take_bit LENGTH('a) k dvd take_bit LENGTH('a) l\<close> by simp
 | |
| 2767 | ||
| 2768 | lemma udvd_iff_dvd: | |
| 2769 | \<open>x udvd y \<longleftrightarrow> unat x dvd unat y\<close> | |
| 2770 | by transfer (simp add: nat_dvd_iff) | |
| 2771 | ||
| 2772 | lemma udvd_iff_dvd_int: | |
| 2773 | \<open>v udvd w \<longleftrightarrow> uint v dvd uint w\<close> | |
| 2774 | by transfer rule | |
| 2775 | ||
| 2776 | lemma udvdI [intro]: | |
| 2777 | \<open>v udvd w\<close> if \<open>unat w = unat v * unat u\<close> | |
| 2778 | proof - | |
| 2779 | from that have \<open>unat v dvd unat w\<close> .. | |
| 2780 | then show ?thesis | |
| 2781 | by (simp add: udvd_iff_dvd) | |
| 2782 | qed | |
| 2783 | ||
| 2784 | lemma udvdE [elim]: | |
| 2785 | fixes v w :: \<open>'a::len word\<close> | |
| 2786 | assumes \<open>v udvd w\<close> | |
| 2787 | obtains u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> | |
| 2788 | proof (cases \<open>v = 0\<close>) | |
| 2789 | case True | |
| 2790 | moreover from True \<open>v udvd w\<close> have \<open>w = 0\<close> | |
| 2791 | by transfer simp | |
| 2792 | ultimately show thesis | |
| 2793 | using that by simp | |
| 2794 | next | |
| 2795 | case False | |
| 2796 | then have \<open>unat v > 0\<close> | |
| 2797 | by (simp add: unat_gt_0) | |
| 2798 | from \<open>v udvd w\<close> have \<open>unat v dvd unat w\<close> | |
| 2799 | by (simp add: udvd_iff_dvd) | |
| 2800 | then obtain n where \<open>unat w = unat v * n\<close> .. | |
| 2801 |   moreover have \<open>n < 2 ^ LENGTH('a)\<close>
 | |
| 2802 | proof (rule ccontr) | |
| 2803 |     assume \<open>\<not> n < 2 ^ LENGTH('a)\<close>
 | |
| 2804 |     then have \<open>n \<ge> 2 ^ LENGTH('a)\<close>
 | |
| 2805 | by (simp add: not_le) | |
| 2806 |     then have \<open>unat v * n \<ge> 2 ^ LENGTH('a)\<close>
 | |
| 2807 |       using \<open>unat v > 0\<close> mult_le_mono [of 1 \<open>unat v\<close> \<open>2 ^ LENGTH('a)\<close> n]
 | |
| 2808 | by simp | |
| 72292 | 2809 | with \<open>unat w = unat v * n\<close> | 
| 2810 |     have \<open>unat w \<ge> 2 ^ LENGTH('a)\<close>
 | |
| 72262 | 2811 | by simp | 
| 72292 | 2812 | with unsigned_less [of w, where ?'a = nat] show False | 
| 2813 | by linarith | |
| 72262 | 2814 | qed | 
| 2815 | ultimately have \<open>unat w = unat v * unat (word_of_nat n :: 'a word)\<close> | |
| 74496 | 2816 | by (auto simp add: take_bit_nat_eq_self_iff unsigned_of_nat intro: sym) | 
| 72262 | 2817 | with that show thesis . | 
| 2818 | qed | |
| 2819 | ||
| 2820 | lemma udvd_imp_mod_eq_0: | |
| 2821 | \<open>w mod v = 0\<close> if \<open>v udvd w\<close> | |
| 2822 | using that by transfer simp | |
| 2823 | ||
| 2824 | lemma mod_eq_0_imp_udvd [intro?]: | |
| 2825 | \<open>v udvd w\<close> if \<open>w mod v = 0\<close> | |
| 2826 | proof - | |
| 2827 | from that have \<open>unat (w mod v) = unat 0\<close> | |
| 2828 | by simp | |
| 2829 | then have \<open>unat w mod unat v = 0\<close> | |
| 2830 | by (simp add: unat_mod_distrib) | |
| 2831 | then have \<open>unat v dvd unat w\<close> .. | |
| 2832 | then show ?thesis | |
| 2833 | by (simp add: udvd_iff_dvd) | |
| 2834 | qed | |
| 2835 | ||
| 72280 | 2836 | lemma udvd_imp_dvd: | 
| 2837 | \<open>v dvd w\<close> if \<open>v udvd w\<close> for v w :: \<open>'a::len word\<close> | |
| 2838 | proof - | |
| 72281 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2839 | from that obtain u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> .. | 
| 72280 | 2840 | then have \<open>(word_of_nat (unat w) :: 'a word) = word_of_nat (unat v * unat u)\<close> | 
| 2841 | by simp | |
| 2842 | then have \<open>w = v * u\<close> | |
| 2843 | by simp | |
| 2844 | then show \<open>v dvd w\<close> .. | |
| 2845 | qed | |
| 2846 | ||
| 72281 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2847 | lemma exp_dvd_iff_exp_udvd: | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2848 | \<open>2 ^ n dvd w \<longleftrightarrow> 2 ^ n udvd w\<close> for v w :: \<open>'a::len word\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2849 | proof | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2850 | assume \<open>2 ^ n udvd w\<close> then show \<open>2 ^ n dvd w\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2851 | by (rule udvd_imp_dvd) | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2852 | next | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2853 | assume \<open>2 ^ n dvd w\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2854 | then obtain u :: \<open>'a word\<close> where \<open>w = 2 ^ n * u\<close> .. | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2855 | then have \<open>w = push_bit n u\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2856 | by (simp add: push_bit_eq_mult) | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2857 | then show \<open>2 ^ n udvd w\<close> | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2858 | by transfer (simp add: take_bit_push_bit dvd_eq_mod_eq_0 flip: take_bit_eq_mod) | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2859 | qed | 
| 
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
 haftmann parents: 
72280diff
changeset | 2860 | |
| 72262 | 2861 | lemma udvd_nat_alt: | 
| 2862 | \<open>a udvd b \<longleftrightarrow> (\<exists>n. unat b = n * unat a)\<close> | |
| 2863 | by (auto simp add: udvd_iff_dvd) | |
| 2864 | ||
| 2865 | lemma udvd_unfold_int: | |
| 2866 | \<open>a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. uint b = n * uint a)\<close> | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2867 | unfolding udvd_iff_dvd_int | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2868 | by (metis dvd_div_mult_self dvd_triv_right uint_div_distrib uint_ge_0) | 
| 37660 | 2869 | |
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2870 | lemma unat_minus_one: | 
| 72079 | 2871 | \<open>unat (w - 1) = unat w - 1\<close> if \<open>w \<noteq> 0\<close> | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2872 | proof - | 
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2873 | have "0 \<le> uint w" by (fact uint_nonnegative) | 
| 72079 | 2874 | moreover from that have "0 \<noteq> uint w" | 
| 65328 | 2875 | by (simp add: uint_0_iff) | 
| 2876 | ultimately have "1 \<le> uint w" | |
| 2877 | by arith | |
| 70185 | 2878 |   from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)"
 | 
| 65328 | 2879 | by arith | 
| 70185 | 2880 |   with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ LENGTH('a) = uint w - 1"
 | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2881 | by (auto intro: mod_pos_pos_trivial) | 
| 70185 | 2882 |   with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1"
 | 
| 72079 | 2883 | by (auto simp del: nat_uint_eq) | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2884 | then show ?thesis | 
| 72292 | 2885 | by (simp only: unat_eq_nat_uint word_arith_wis mod_diff_right_eq) | 
| 2886 | (metis of_int_1 uint_word_of_int unsigned_1) | |
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2887 | qed | 
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2888 | |
| 65328 | 2889 | lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p" | 
| 37660 | 2890 | by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) | 
| 65268 | 2891 | |
| 45604 | 2892 | lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] | 
| 2893 | lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] | |
| 37660 | 2894 | |
| 70185 | 2895 | lemma uint_sub_lt2p [simp]: "uint x - uint y < 2 ^ LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2896 | for x :: "'a::len word" and y :: "'b::len word" | 
| 37660 | 2897 | using uint_ge_0 [of y] uint_lt2p [of x] by arith | 
| 2898 | ||
| 2899 | ||
| 61799 | 2900 | subsection \<open>Conditions for the addition (etc) of two words to overflow\<close> | 
| 37660 | 2901 | |
| 65268 | 2902 | lemma uint_add_lem: | 
| 70185 | 2903 |   "(uint x + uint y < 2 ^ LENGTH('a)) =
 | 
| 65328 | 2904 | (uint (x + y) = uint x + uint y)" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2905 | for x y :: "'a::len word" | 
| 71997 | 2906 | by (metis add.right_neutral add_mono_thms_linordered_semiring(1) mod_pos_pos_trivial of_nat_0_le_iff uint_lt2p uint_nat uint_word_ariths(1)) | 
| 37660 | 2907 | |
| 65268 | 2908 | lemma uint_mult_lem: | 
| 70185 | 2909 |   "(uint x * uint y < 2 ^ LENGTH('a)) =
 | 
| 65328 | 2910 | (uint (x * y) = uint x * uint y)" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2911 | for x y :: "'a::len word" | 
| 71997 | 2912 | by (metis mod_pos_pos_trivial uint_lt2p uint_mult_ge0 uint_word_ariths(3)) | 
| 37660 | 2913 | |
| 65328 | 2914 | lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y" | 
| 72292 | 2915 | by (metis diff_ge_0_iff_ge of_nat_0_le_iff uint_nat uint_sub_lt2p uint_word_of_int unique_euclidean_semiring_numeral_class.mod_less word_sub_wi) | 
| 65328 | 2916 | |
| 2917 | lemma uint_add_le: "uint (x + y) \<le> uint x + uint y" | |
| 71997 | 2918 | unfolding uint_word_ariths by (simp add: zmod_le_nonneg_dividend) | 
| 37660 | 2919 | |
| 65328 | 2920 | lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y" | 
| 72488 | 2921 | unfolding uint_word_ariths | 
| 2922 | by (simp flip: take_bit_eq_mod add: take_bit_int_greater_eq_self_iff) | |
| 2923 | ||
| 2924 | lemma int_mod_ge: \<open>a \<le> a mod n\<close> if \<open>a < n\<close> \<open>0 < n\<close> | |
| 2925 | for a n :: int | |
| 76231 | 2926 | using that order.trans [of a 0 \<open>a mod n\<close>] by (cases \<open>a < 0\<close>) auto | 
| 72488 | 2927 | |
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2928 | lemma mod_add_if_z: | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2929 | "\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow> | 
| 65328 | 2930 | (x + y) mod z = (if x + y < z then x + y else x + y - z)" | 
| 2931 | for x y z :: int | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2932 | apply (simp add: not_less) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2933 | by (metis (no_types) add_strict_mono diff_ge_0_iff_ge diff_less_eq minus_mod_self2 mod_pos_pos_trivial) | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2934 | |
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2935 | lemma uint_plus_if': | 
| 65328 | 2936 | "uint (a + b) = | 
| 70185 | 2937 |     (if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b
 | 
| 2938 |      else uint a + uint b - 2 ^ LENGTH('a))"
 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2939 | for a b :: "'a::len word" | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2940 | using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) | 
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2941 | |
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2942 | lemma mod_sub_if_z: | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2943 | "\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow> | 
| 65328 | 2944 | (x - y) mod z = (if y \<le> x then x - y else x - y + z)" | 
| 2945 | for x y z :: int | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2946 | using mod_pos_pos_trivial [of "x - y + z" z] by (auto simp add: not_le) | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2947 | |
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2948 | lemma uint_sub_if': | 
| 65328 | 2949 | "uint (a - b) = | 
| 2950 | (if uint b \<le> uint a then uint a - uint b | |
| 70185 | 2951 |      else uint a - uint b + 2 ^ LENGTH('a))"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2952 | for a b :: "'a::len word" | 
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2953 | using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) | 
| 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 2954 | |
| 37660 | 2955 | lemma word_of_int_inverse: | 
| 70185 | 2956 |   "word_of_int r = a \<Longrightarrow> 0 \<le> r \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> uint a = r"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 2957 | for a :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 2958 | by transfer (simp add: take_bit_int_eq_self) | 
| 37660 | 2959 | |
| 74592 | 2960 | lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)"
 | 
| 2961 | for x :: "'a::len word" | |
| 2962 | by (auto simp add: unsigned_of_nat take_bit_nat_eq_self) | |
| 2963 | ||
| 2964 | lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)"
 | |
| 2965 | for x :: "'a::len word" | |
| 2966 | by (auto simp add: unsigned_of_nat take_bit_nat_eq_self) | |
| 2967 | ||
| 2968 | lemma un_ui_le: | |
| 2969 | \<open>unat a \<le> unat b \<longleftrightarrow> uint a \<le> uint b\<close> | |
| 2970 | by transfer (simp add: nat_le_iff) | |
| 2971 | ||
| 2972 | lemma unat_plus_if': | |
| 2973 | \<open>unat (a + b) = | |
| 2974 |     (if unat a + unat b < 2 ^ LENGTH('a)
 | |
| 2975 | then unat a + unat b | |
| 2976 |     else unat a + unat b - 2 ^ LENGTH('a))\<close> for a b :: \<open>'a::len word\<close>
 | |
| 2977 | apply (auto simp add: not_less le_iff_add) | |
| 2978 | apply (metis (mono_tags, lifting) of_nat_add of_nat_unat take_bit_nat_eq_self_iff unsigned_less unsigned_of_nat unsigned_word_eqI) | |
| 2979 | apply (smt (verit, ccfv_SIG) dbl_simps(3) dbl_simps(5) numerals(1) of_nat_0_le_iff of_nat_add of_nat_eq_iff of_nat_numeral of_nat_power of_nat_unat uint_plus_if' unsigned_1) | |
| 2980 | done | |
| 2981 | ||
| 2982 | lemma unat_sub_if_size: | |
| 2983 | "unat (x - y) = | |
| 2984 | (if unat y \<le> unat x | |
| 2985 | then unat x - unat y | |
| 2986 | else unat x + 2 ^ size x - unat y)" | |
| 2987 | proof - | |
| 2988 |   { assume xy: "\<not> uint y \<le> uint x"
 | |
| 2989 |     have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x + 2 ^ LENGTH('a) - uint y)"
 | |
| 2990 | by simp | |
| 2991 |     also have "... = nat (uint x + 2 ^ LENGTH('a)) - nat (uint y)"
 | |
| 2992 | by (simp add: nat_diff_distrib') | |
| 2993 |     also have "... = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)"
 | |
| 2994 | by (metis nat_add_distrib nat_eq_numeral_power_cancel_iff order_less_imp_le unsigned_0 unsigned_greater_eq unsigned_less) | |
| 2995 |     finally have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)" .
 | |
| 2996 | } | |
| 2997 | then show ?thesis | |
| 2998 | by (simp add: word_size) (metis nat_diff_distrib' uint_sub_if' un_ui_le unat_eq_nat_uint unsigned_greater_eq) | |
| 2999 | qed | |
| 3000 | ||
| 3001 | lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size] | |
| 3002 | ||
| 37660 | 3003 | lemma uint_split: | 
| 70185 | 3004 |   "P (uint x) = (\<forall>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<longrightarrow> P i)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3005 | for x :: "'a::len word" | 
| 72262 | 3006 | by transfer (auto simp add: take_bit_eq_mod) | 
| 37660 | 3007 | |
| 3008 | lemma uint_split_asm: | |
| 70185 | 3009 |   "P (uint x) = (\<nexists>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<and> \<not> P i)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3010 | for x :: "'a::len word" | 
| 74496 | 3011 | by (auto simp add: unsigned_of_int take_bit_int_eq_self) | 
| 37660 | 3012 | |
| 74592 | 3013 | |
| 3014 | subsection \<open>Some proof tool support\<close> | |
| 3015 | ||
| 3016 | \<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close> | |
| 3017 | lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d" | |
| 3018 | by auto | |
| 3019 | ||
| 3020 | lemmas unat_splits = unat_split unat_split_asm | |
| 3021 | ||
| 3022 | lemmas unat_arith_simps = | |
| 3023 | word_le_nat_alt word_less_nat_alt | |
| 3024 | word_unat_eq_iff | |
| 3025 | unat_sub_if' unat_plus_if' unat_div unat_mod | |
| 3026 | ||
| 37660 | 3027 | lemmas uint_splits = uint_split uint_split_asm | 
| 3028 | ||
| 65268 | 3029 | lemmas uint_arith_simps = | 
| 37660 | 3030 | word_le_def word_less_alt | 
| 72292 | 3031 | word_uint_eq_iff | 
| 37660 | 3032 | uint_sub_if' uint_plus_if' | 
| 3033 | ||
| 74592 | 3034 | \<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close> | 
| 3035 | ML \<open> | |
| 3036 | val unat_arith_simpset = | |
| 3037 |   @{context} (* TODO: completely explicitly determined simpset *)
 | |
| 3038 |   |> fold Simplifier.add_simp @{thms unat_arith_simps}
 | |
| 3039 |   |> fold Splitter.add_split @{thms if_split_asm}
 | |
| 3040 |   |> fold Simplifier.add_cong @{thms power_False_cong}
 | |
| 3041 | |> simpset_of | |
| 3042 | ||
| 3043 | fun unat_arith_tacs ctxt = | |
| 3044 | let | |
| 3045 | fun arith_tac' n t = | |
| 3046 | Arith_Data.arith_tac ctxt n t | |
| 3047 | handle Cooper.COOPER _ => Seq.empty; | |
| 3048 | in | |
| 3049 | [ clarify_tac ctxt 1, | |
| 3050 | full_simp_tac (put_simpset unat_arith_simpset ctxt) 1, | |
| 3051 | ALLGOALS (full_simp_tac | |
| 3052 | (put_simpset HOL_ss ctxt | |
| 3053 |           |> fold Splitter.add_split @{thms unat_splits}
 | |
| 3054 |           |> fold Simplifier.add_cong @{thms power_False_cong})),
 | |
| 3055 |       rewrite_goals_tac ctxt @{thms word_size},
 | |
| 3056 | ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN | |
| 3057 | REPEAT (eresolve_tac ctxt [conjE] n) THEN | |
| 3058 |                          REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)),
 | |
| 3059 | TRYALL arith_tac' ] | |
| 3060 | end | |
| 3061 | ||
| 3062 | fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt)) | |
| 3063 | \<close> | |
| 3064 | ||
| 3065 | method_setup unat_arith = | |
| 3066 | \<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close> | |
| 3067 | "solving word arithmetic via natural numbers and arith" | |
| 37660 | 3068 | |
| 67408 | 3069 | \<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close> | 
| 61799 | 3070 | ML \<open> | 
| 72292 | 3071 | val uint_arith_simpset = | 
| 74592 | 3072 |   @{context} (* TODO: completely explicitly determined simpset *)
 | 
| 72292 | 3073 |   |> fold Simplifier.add_simp @{thms uint_arith_simps}
 | 
| 3074 |   |> fold Splitter.add_split @{thms if_split_asm}
 | |
| 3075 |   |> fold Simplifier.add_cong @{thms power_False_cong}
 | |
| 3076 | |> simpset_of; | |
| 3077 | ||
| 65268 | 3078 | fun uint_arith_tacs ctxt = | 
| 37660 | 3079 | let | 
| 3080 | fun arith_tac' n t = | |
| 59657 
2441a80fb6c1
eliminated unused arith "verbose" flag -- tools that need options can use the context;
 wenzelm parents: 
59498diff
changeset | 3081 | Arith_Data.arith_tac ctxt n t | 
| 37660 | 3082 | handle Cooper.COOPER _ => Seq.empty; | 
| 65268 | 3083 | in | 
| 42793 | 3084 | [ clarify_tac ctxt 1, | 
| 72292 | 3085 | full_simp_tac (put_simpset uint_arith_simpset ctxt) 1, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51375diff
changeset | 3086 | ALLGOALS (full_simp_tac | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51375diff
changeset | 3087 | (put_simpset HOL_ss ctxt | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51375diff
changeset | 3088 |           |> fold Splitter.add_split @{thms uint_splits}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51375diff
changeset | 3089 |           |> fold Simplifier.add_cong @{thms power_False_cong})),
 | 
| 65268 | 3090 |       rewrite_goals_tac ctxt @{thms word_size},
 | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
59487diff
changeset | 3091 | ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN | 
| 60754 | 3092 | REPEAT (eresolve_tac ctxt [conjE] n) THEN | 
| 65268 | 3093 |                          REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n
 | 
| 3094 | THEN assume_tac ctxt n | |
| 58963 
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
 wenzelm parents: 
58874diff
changeset | 3095 | THEN assume_tac ctxt n)), | 
| 37660 | 3096 | TRYALL arith_tac' ] | 
| 3097 | end | |
| 3098 | ||
| 3099 | fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt)) | |
| 61799 | 3100 | \<close> | 
| 37660 | 3101 | |
| 65268 | 3102 | method_setup uint_arith = | 
| 61799 | 3103 | \<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close> | 
| 37660 | 3104 | "solving word arithmetic via integers and arith" | 
| 3105 | ||
| 3106 | ||
| 61799 | 3107 | subsection \<open>More on overflows and monotonicity\<close> | 
| 37660 | 3108 | |
| 65328 | 3109 | lemma no_plus_overflow_uint_size: "x \<le> x + y \<longleftrightarrow> uint x + uint y < 2 ^ size x" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3110 | for x y :: "'a::len word" | 
| 74592 | 3111 | by (auto simp add: word_size word_le_def uint_add_lem uint_sub_lem) | 
| 37660 | 3112 | |
| 3113 | lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size] | |
| 3114 | ||
| 65328 | 3115 | lemma no_ulen_sub: "x \<ge> x - y \<longleftrightarrow> uint y \<le> uint x" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3116 | for x y :: "'a::len word" | 
| 74592 | 3117 | by (auto simp add: word_size word_le_def uint_add_lem uint_sub_lem) | 
| 37660 | 3118 | |
| 70185 | 3119 | lemma no_olen_add': "x \<le> y + x \<longleftrightarrow> uint y + uint x < 2 ^ LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3120 | for x y :: "'a::len word" | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 3121 | by (simp add: ac_simps no_olen_add) | 
| 37660 | 3122 | |
| 45604 | 3123 | lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]] | 
| 3124 | ||
| 3125 | lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem] | |
| 3126 | lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1] | |
| 3127 | lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem] | |
| 37660 | 3128 | lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def] | 
| 3129 | lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def] | |
| 45604 | 3130 | lemmas word_sub_le = word_sub_le_iff [THEN iffD2] | 
| 37660 | 3131 | |
| 65328 | 3132 | lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1" | 
| 3133 | for x :: "'a::len word" | |
| 74592 | 3134 | by transfer (simp add: take_bit_decr_eq) | 
| 37660 | 3135 | |
| 65328 | 3136 | lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1" | 
| 3137 | for x :: "'a::len word" | |
| 74592 | 3138 | by transfer (simp add: int_one_le_iff_zero_less less_le) | 
| 37660 | 3139 | |
| 65328 | 3140 | lemma sub_wrap_lt: "x < x - z \<longleftrightarrow> x < z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3141 | for x z :: "'a::len word" | 
| 74592 | 3142 | by (simp add: word_less_def uint_sub_lem) | 
| 3143 | (meson linorder_not_le uint_minus_simple_iff uint_sub_lem word_less_iff_unsigned) | |
| 3144 | ||
| 65328 | 3145 | lemma sub_wrap: "x \<le> x - z \<longleftrightarrow> z = 0 \<or> x < z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3146 | for x z :: "'a::len word" | 
| 74592 | 3147 | by (simp add: le_less sub_wrap_lt ac_simps) | 
| 37660 | 3148 | |
| 65328 | 3149 | lemma plus_minus_not_NULL_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> c \<noteq> 0 \<Longrightarrow> x + c \<noteq> 0" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3150 | for x ab c :: "'a::len word" | 
| 37660 | 3151 | by uint_arith | 
| 3152 | ||
| 65328 | 3153 | lemma plus_minus_no_overflow_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> x \<le> x + c" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3154 | for x ab c :: "'a::len word" | 
| 37660 | 3155 | by uint_arith | 
| 3156 | ||
| 65328 | 3157 | lemma le_minus': "a + c \<le> b \<Longrightarrow> a \<le> a + c \<Longrightarrow> c \<le> b - a" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3158 | for a b c :: "'a::len word" | 
| 37660 | 3159 | by uint_arith | 
| 3160 | ||
| 65328 | 3161 | lemma le_plus': "a \<le> b \<Longrightarrow> c \<le> b - a \<Longrightarrow> a + c \<le> b" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3162 | for a b c :: "'a::len word" | 
| 37660 | 3163 | by uint_arith | 
| 3164 | ||
| 3165 | lemmas le_plus = le_plus' [rotated] | |
| 3166 | ||
| 46011 | 3167 | lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *) | 
| 37660 | 3168 | |
| 65328 | 3169 | lemma word_plus_mono_right: "y \<le> z \<Longrightarrow> x \<le> x + z \<Longrightarrow> x + y \<le> x + z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3170 | for x y z :: "'a::len word" | 
| 37660 | 3171 | by uint_arith | 
| 3172 | ||
| 65328 | 3173 | lemma word_less_minus_cancel: "y - x < z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y < z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3174 | for x y z :: "'a::len word" | 
| 37660 | 3175 | by uint_arith | 
| 3176 | ||
| 65328 | 3177 | lemma word_less_minus_mono_left: "y < z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x < z - x" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3178 | for x y z :: "'a::len word" | 
| 37660 | 3179 | by uint_arith | 
| 3180 | ||
| 65328 | 3181 | lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d" | 
| 3182 | for a b c d :: "'a::len word" | |
| 37660 | 3183 | by uint_arith | 
| 3184 | ||
| 65328 | 3185 | lemma word_le_minus_cancel: "y - x \<le> z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y \<le> z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3186 | for x y z :: "'a::len word" | 
| 37660 | 3187 | by uint_arith | 
| 3188 | ||
| 65328 | 3189 | lemma word_le_minus_mono_left: "y \<le> z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x \<le> z - x" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3190 | for x y z :: "'a::len word" | 
| 37660 | 3191 | by uint_arith | 
| 3192 | ||
| 65268 | 3193 | lemma word_le_minus_mono: | 
| 65328 | 3194 | "a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d" | 
| 3195 | for a b c d :: "'a::len word" | |
| 37660 | 3196 | by uint_arith | 
| 3197 | ||
| 65328 | 3198 | lemma plus_le_left_cancel_wrap: "x + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3199 | for x y y' :: "'a::len word" | 
| 37660 | 3200 | by uint_arith | 
| 3201 | ||
| 65328 | 3202 | lemma plus_le_left_cancel_nowrap: "x \<le> x + y' \<Longrightarrow> x \<le> x + y \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3203 | for x y y' :: "'a::len word" | 
| 37660 | 3204 | by uint_arith | 
| 3205 | ||
| 65328 | 3206 | lemma word_plus_mono_right2: "a \<le> a + b \<Longrightarrow> c \<le> b \<Longrightarrow> a \<le> a + c" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3207 | for a b c :: "'a::len word" | 
| 65328 | 3208 | by uint_arith | 
| 3209 | ||
| 3210 | lemma word_less_add_right: "x < y - z \<Longrightarrow> z \<le> y \<Longrightarrow> x + z < y" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3211 | for x y z :: "'a::len word" | 
| 37660 | 3212 | by uint_arith | 
| 3213 | ||
| 65328 | 3214 | lemma word_less_sub_right: "x < y + z \<Longrightarrow> y \<le> x \<Longrightarrow> x - y < z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3215 | for x y z :: "'a::len word" | 
| 37660 | 3216 | by uint_arith | 
| 3217 | ||
| 65328 | 3218 | lemma word_le_plus_either: "x \<le> y \<or> x \<le> z \<Longrightarrow> y \<le> y + z \<Longrightarrow> x \<le> y + z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3219 | for x y z :: "'a::len word" | 
| 37660 | 3220 | by uint_arith | 
| 3221 | ||
| 65328 | 3222 | lemma word_less_nowrapI: "x < z - k \<Longrightarrow> k \<le> z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3223 | for x z k :: "'a::len word" | 
| 37660 | 3224 | by uint_arith | 
| 3225 | ||
| 65328 | 3226 | lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m" | 
| 3227 | for i m :: "'a::len word" | |
| 37660 | 3228 | by uint_arith | 
| 3229 | ||
| 65328 | 3230 | lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m" | 
| 3231 | for i m :: "'a::len word" | |
| 37660 | 3232 | by uint_arith | 
| 3233 | ||
| 3234 | lemma udvd_incr_lem: | |
| 65268 | 3235 | "up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow> | 
| 65328 | 3236 | uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq" | 
| 71997 | 3237 | by auto (metis int_distrib(1) linorder_not_less mult.left_neutral mult_right_mono uint_nonnegative zless_imp_add1_zle) | 
| 37660 | 3238 | |
| 65268 | 3239 | lemma udvd_incr': | 
| 3240 | "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> | |
| 65328 | 3241 | uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3242 | unfolding word_less_alt word_le_def | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3243 | by (metis (full_types) order_trans udvd_incr_lem uint_add_le) | 
| 37660 | 3244 | |
| 65268 | 3245 | lemma udvd_decr': | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3246 | assumes "p < q" "uint p = ua + n * uint K" "uint q = ua + n' * uint K" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3247 | shows "uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3248 | proof - | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3249 | have "\<And>w wa. uint (w::'a word) \<le> uint wa + uint (w - wa)" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3250 | by (metis (no_types) add_diff_cancel_left' diff_add_cancel uint_add_le) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3251 | moreover have "uint K + uint p \<le> uint q" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3252 | using assms by (metis (no_types) add_diff_cancel_left' diff_add_cancel udvd_incr_lem word_less_def) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3253 | ultimately show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3254 | by (meson add_le_cancel_left order_trans word_less_eq_iff_unsigned) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3255 | qed | 
| 37660 | 3256 | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 3257 | lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left] | 
| 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 3258 | lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left] | 
| 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 3259 | lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left] | 
| 37660 | 3260 | |
| 65328 | 3261 | lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3262 | unfolding udvd_unfold_int | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3263 | by (meson udvd_decr0) | 
| 37660 | 3264 | |
| 65268 | 3265 | lemma udvd_incr2_K: | 
| 65328 | 3266 | "p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow> | 
| 3267 | 0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s" | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3268 | unfolding udvd_unfold_int | 
| 62390 | 3269 | apply (simp add: uint_arith_simps split: if_split_asm) | 
| 73932 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73853diff
changeset | 3270 | apply (metis (no_types, opaque_lifting) le_add_diff_inverse le_less_trans udvd_incr_lem) | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3271 | using uint_lt2p [of s] by simp | 
| 37660 | 3272 | |
| 3273 | ||
| 61799 | 3274 | subsection \<open>Arithmetic type class instantiations\<close> | 
| 37660 | 3275 | |
| 3276 | lemmas word_le_0_iff [simp] = | |
| 70749 
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
 paulson <lp15@cam.ac.uk> parents: 
70342diff
changeset | 3277 | word_zero_le [THEN leD, THEN antisym_conv1] | 
| 37660 | 3278 | |
| 65328 | 3279 | lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)" | 
| 72262 | 3280 | by simp | 
| 37660 | 3281 | |
| 67408 | 3282 | text \<open> | 
| 3283 | note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>, | |
| 3284 | which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close> | |
| 3285 | \<close> | |
| 46603 | 3286 | lemma iszero_word_no [simp]: | 
| 65268 | 3287 | "iszero (numeral bin :: 'a::len word) = | 
| 72128 | 3288 |     iszero (take_bit LENGTH('a) (numeral bin :: int))"
 | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3289 | by (metis iszero_def uint_0_iff uint_bintrunc) | 
| 65268 | 3290 | |
| 61799 | 3291 | text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close> | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3292 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3293 | lemmas word_eq_numeral_iff_iszero [simp] = | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3294 | eq_numeral_iff_iszero [where 'a="'a::len word"] | 
| 46603 | 3295 | |
| 37660 | 3296 | |
| 61799 | 3297 | subsection \<open>Word and nat\<close> | 
| 37660 | 3298 | |
| 70185 | 3299 | lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)"
 | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3300 | by (metis of_nat_unat ucast_id unsigned_less) | 
| 37660 | 3301 | |
| 70185 | 3302 | lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))"
 | 
| 65328 | 3303 | for w :: "'a::len word" | 
| 68157 | 3304 |   using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric]
 | 
| 74496 | 3305 | by (auto simp flip: take_bit_eq_mod simp add: unsigned_of_nat) | 
| 37660 | 3306 | |
| 65328 | 3307 | lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)" | 
| 37660 | 3308 | unfolding word_size by (rule of_nat_eq) | 
| 3309 | ||
| 70185 | 3310 | lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))"
 | 
| 37660 | 3311 | by (simp add: of_nat_eq) | 
| 3312 | ||
| 70185 | 3313 | lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)"
 | 
| 45805 | 3314 | by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]) | 
| 37660 | 3315 | |
| 65328 | 3316 | lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k" | 
| 37660 | 3317 | by (cases k) auto | 
| 3318 | ||
| 70185 | 3319 | lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)"
 | 
| 65328 | 3320 | by (auto simp add : of_nat_0) | 
| 3321 | ||
| 3322 | lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)" | |
| 37660 | 3323 | by simp | 
| 3324 | ||
| 65328 | 3325 | lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)" | 
| 72262 | 3326 | by (simp add: wi_hom_mult) | 
| 37660 | 3327 | |
| 65328 | 3328 | lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)" | 
| 72262 | 3329 | by transfer (simp add: ac_simps) | 
| 37660 | 3330 | |
| 3331 | lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0" | |
| 45995 
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
 huffman parents: 
45958diff
changeset | 3332 | by simp | 
| 37660 | 3333 | |
| 3334 | lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)" | |
| 45995 
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
 huffman parents: 
45958diff
changeset | 3335 | by simp | 
| 37660 | 3336 | |
| 65268 | 3337 | lemmas Abs_fnat_homs = | 
| 3338 | Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc | |
| 37660 | 3339 | Abs_fnat_hom_0 Abs_fnat_hom_1 | 
| 3340 | ||
| 65328 | 3341 | lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)" | 
| 3342 | by simp | |
| 3343 | ||
| 3344 | lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)" | |
| 37660 | 3345 | by simp | 
| 3346 | ||
| 65328 | 3347 | lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))" | 
| 37660 | 3348 | by (subst Abs_fnat_hom_Suc [symmetric]) simp | 
| 3349 | ||
| 65328 | 3350 | lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)" | 
| 72262 | 3351 | by (metis of_int_of_nat_eq of_nat_unat of_nat_div word_div_def) | 
| 3352 | ||
| 65328 | 3353 | lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)" | 
| 72262 | 3354 | by (metis of_int_of_nat_eq of_nat_mod of_nat_unat word_mod_def) | 
| 37660 | 3355 | |
| 3356 | lemmas word_arith_nat_defs = | |
| 3357 | word_arith_nat_add word_arith_nat_mult | |
| 3358 | word_arith_nat_Suc Abs_fnat_hom_0 | |
| 3359 | Abs_fnat_hom_1 word_arith_nat_div | |
| 65268 | 3360 | word_arith_nat_mod | 
| 37660 | 3361 | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 3362 | lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y" | 
| 72292 | 3363 | by (fact arg_cong) | 
| 3364 | ||
| 3365 | lemma unat_of_nat: | |
| 3366 |   \<open>unat (word_of_nat x :: 'a::len word) = x mod 2 ^ LENGTH('a)\<close>
 | |
| 3367 | by transfer (simp flip: take_bit_eq_mod add: nat_take_bit_eq) | |
| 65268 | 3368 | |
| 37660 | 3369 | lemmas unat_word_ariths = word_arith_nat_defs | 
| 45604 | 3370 | [THEN trans [OF unat_cong unat_of_nat]] | 
| 37660 | 3371 | |
| 3372 | lemmas word_sub_less_iff = word_sub_le_iff | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 3373 | [unfolded linorder_not_less [symmetric] Not_eq_iff] | 
| 37660 | 3374 | |
| 65268 | 3375 | lemma unat_add_lem: | 
| 70185 | 3376 |   "unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y"
 | 
| 65328 | 3377 | for x y :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3378 | by (metis mod_less unat_word_ariths(1) unsigned_less) | 
| 37660 | 3379 | |
| 65268 | 3380 | lemma unat_mult_lem: | 
| 70185 | 3381 |   "unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y"
 | 
| 65363 | 3382 | for x y :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3383 | by (metis mod_less unat_word_ariths(2) unsigned_less) | 
| 71997 | 3384 | |
| 65328 | 3385 | lemma le_no_overflow: "x \<le> b \<Longrightarrow> a \<le> a + b \<Longrightarrow> x \<le> a + b" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3386 | for a b x :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3387 | using word_le_plus_either by blast | 
| 37660 | 3388 | |
| 71997 | 3389 | lemma uint_div: | 
| 3390 | \<open>uint (x div y) = uint x div uint y\<close> | |
| 72262 | 3391 | by (fact uint_div_distrib) | 
| 71997 | 3392 | |
| 3393 | lemma uint_mod: | |
| 3394 | \<open>uint (x mod y) = uint x mod uint y\<close> | |
| 72262 | 3395 | by (fact uint_mod_distrib) | 
| 71997 | 3396 | |
| 65328 | 3397 | lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x" | 
| 3398 | for x y :: "'a::len word" | |
| 37660 | 3399 | unfolding word_size by unat_arith | 
| 3400 | ||
| 65328 | 3401 | lemmas no_olen_add_nat = | 
| 3402 | no_plus_overflow_unat_size [unfolded word_size] | |
| 3403 | ||
| 3404 | lemmas unat_plus_simple = | |
| 3405 | trans [OF no_olen_add_nat unat_add_lem] | |
| 3406 | ||
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3407 | lemma word_div_mult: "\<lbrakk>0 < y; unat x * unat y < 2 ^ LENGTH('a)\<rbrakk> \<Longrightarrow> x * y div y = x"
 | 
| 65328 | 3408 | for x y :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3409 | by (simp add: unat_eq_zero unat_mult_lem word_arith_nat_div) | 
| 37660 | 3410 | |
| 70185 | 3411 | lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)"
 | 
| 65328 | 3412 | for i k x :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3413 | by unat_arith (meson le_less_trans less_mult_imp_div_less not_le unsigned_less) | 
| 37660 | 3414 | |
| 3415 | lemmas div_lt'' = order_less_imp_le [THEN div_lt'] | |
| 3416 | ||
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3417 | lemma div_lt_mult: "\<lbrakk>i < k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x < k" | 
| 65328 | 3418 | for i k x :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3419 | by (metis div_le_mono div_lt'' not_le unat_div word_div_mult word_less_iff_unsigned) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3420 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3421 | lemma div_le_mult: "\<lbrakk>i \<le> k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x \<le> k" | 
| 65328 | 3422 | for i k x :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3423 | by (metis div_lt' less_mult_imp_div_less not_less unat_arith_simps(2) unat_div unat_mult_lem) | 
| 37660 | 3424 | |
| 70185 | 3425 | lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)"
 | 
| 65328 | 3426 | for i k x :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3427 | unfolding uint_nat | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3428 | by (metis div_lt' int_ops(7) of_nat_unat uint_mult_lem unat_mult_lem) | 
| 37660 | 3429 | |
| 3430 | lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint'] | |
| 3431 | ||
| 70185 | 3432 | lemma word_le_exists': "x \<le> y \<Longrightarrow> \<exists>z. y = x + z \<and> uint x + uint z < 2 ^ LENGTH('a)"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3433 | for x y z :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3434 | by (metis add.commute diff_add_cancel no_olen_add) | 
| 71997 | 3435 | |
| 37660 | 3436 | lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab] | 
| 3437 | ||
| 3438 | lemmas plus_minus_no_overflow = | |
| 3439 | order_less_imp_le [THEN plus_minus_no_overflow_ab] | |
| 65268 | 3440 | |
| 37660 | 3441 | lemmas mcs = word_less_minus_cancel word_less_minus_mono_left | 
| 3442 | word_le_minus_cancel word_le_minus_mono_left | |
| 3443 | ||
| 45604 | 3444 | lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x | 
| 3445 | lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x | |
| 3446 | lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x | |
| 37660 | 3447 | |
| 72292 | 3448 | lemma le_unat_uoi: | 
| 3449 | \<open>y \<le> unat z \<Longrightarrow> unat (word_of_nat y :: 'a word) = y\<close> | |
| 3450 | for z :: \<open>'a::len word\<close> | |
| 3451 | by transfer (simp add: nat_take_bit_eq take_bit_nat_eq_self_iff le_less_trans) | |
| 37660 | 3452 | |
| 66808 
1907167b6038
elementary definition of division on natural numbers
 haftmann parents: 
66453diff
changeset | 3453 | lemmas thd = times_div_less_eq_dividend | 
| 37660 | 3454 | |
| 71997 | 3455 | lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend | 
| 37660 | 3456 | |
| 65328 | 3457 | lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n" | 
| 3458 | for n b :: "'a::len word" | |
| 71997 | 3459 | by (fact div_mult_mod_eq) | 
| 37660 | 3460 | |
| 65328 | 3461 | lemma word_div_mult_le: "a div b * b \<le> a" | 
| 3462 | for a b :: "'a::len word" | |
| 71997 | 3463 | by (metis div_le_mult mult_not_zero order.not_eq_order_implies_strict order_refl word_zero_le) | 
| 37660 | 3464 | |
| 65328 | 3465 | lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n" | 
| 3466 | for m n :: "'a::len word" | |
| 71997 | 3467 | by (simp add: unat_arith_simps) | 
| 3468 | ||
| 65328 | 3469 | lemma word_of_int_power_hom: "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)" | 
| 45995 
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
 huffman parents: 
45958diff
changeset | 3470 | by (induct n) (simp_all add: wi_hom_mult [symmetric]) | 
| 37660 | 3471 | |
| 65328 | 3472 | lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)" | 
| 37660 | 3473 | by (simp add : word_of_int_power_hom [symmetric]) | 
| 3474 | ||
| 70183 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3475 | lemma unatSuc: "1 + n \<noteq> 0 \<Longrightarrow> unat (1 + n) = Suc (unat n)" | 
| 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3476 | for n :: "'a::len word" | 
| 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3477 | by unat_arith | 
| 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3478 | |
| 37660 | 3479 | |
| 61799 | 3480 | subsection \<open>Cardinality, finiteness of set of words\<close> | 
| 37660 | 3481 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3482 | lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close>
 | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3483 | unfolding inj_on_def | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3484 | by (metis atLeastLessThan_iff word_of_int_inverse) | 
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 3485 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3486 | lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close>
 | 
| 72488 | 3487 | apply transfer | 
| 3488 | apply (auto simp add: image_iff) | |
| 3489 | apply (metis take_bit_int_eq_self_iff) | |
| 3490 | done | |
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 3491 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3492 | lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close>
 | 
| 72488 | 3493 | by (auto simp add: image_iff) (metis atLeastLessThan_iff linorder_not_le uint_split) | 
| 45809 
2bee94cbae72
finite class instance for word type; remove unused lemmas
 huffman parents: 
45808diff
changeset | 3494 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3495 | lemma card_word: "CARD('a word) = 2 ^ LENGTH('a::len)"
 | 
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 3496 | by (simp add: UNIV_eq card_image inj_on_word_of_int) | 
| 37660 | 3497 | |
| 70183 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3498 | lemma card_word_size: "CARD('a word) = 2 ^ size x"
 | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3499 | for x :: "'a::len word" | 
| 65328 | 3500 | unfolding word_size by (rule card_word) | 
| 37660 | 3501 | |
| 74097 | 3502 | end | 
| 3503 | ||
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3504 | instance word :: (len) finite | 
| 71948 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 3505 | by standard (simp add: UNIV_eq) | 
| 
6ede899d26d3
fundamental construction of word type following existing transfer rules
 haftmann parents: 
71947diff
changeset | 3506 | |
| 37660 | 3507 | |
| 61799 | 3508 | subsection \<open>Bitwise Operations on Words\<close> | 
| 37660 | 3509 | |
| 74097 | 3510 | context | 
| 3511 | includes bit_operations_syntax | |
| 3512 | begin | |
| 3513 | ||
| 46011 | 3514 | lemma word_wi_log_defs: | 
| 71149 | 3515 | "NOT (word_of_int a) = word_of_int (NOT a)" | 
| 46011 | 3516 | "word_of_int a AND word_of_int b = word_of_int (a AND b)" | 
| 3517 | "word_of_int a OR word_of_int b = word_of_int (a OR b)" | |
| 3518 | "word_of_int a XOR word_of_int b = word_of_int (a XOR b)" | |
| 47374 
9475d524bafb
set up and use lift_definition for word operations
 huffman parents: 
47372diff
changeset | 3519 | by (transfer, rule refl)+ | 
| 47372 | 3520 | |
| 46011 | 3521 | lemma word_no_log_defs [simp]: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3522 | "NOT (numeral a) = word_of_int (NOT (numeral a))" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3523 | "NOT (- numeral a) = word_of_int (NOT (- numeral a))" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3524 | "numeral a AND numeral b = word_of_int (numeral a AND numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3525 | "numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3526 | "- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3527 | "- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3528 | "numeral a OR numeral b = word_of_int (numeral a OR numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3529 | "numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3530 | "- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3531 | "- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3532 | "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3533 | "numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3534 | "- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3535 | "- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)" | 
| 47372 | 3536 | by (transfer, rule refl)+ | 
| 37660 | 3537 | |
| 61799 | 3538 | text \<open>Special cases for when one of the arguments equals 1.\<close> | 
| 46064 
88ef116e0522
add simp rules for bitwise word operations with 1
 huffman parents: 
46057diff
changeset | 3539 | |
| 
88ef116e0522
add simp rules for bitwise word operations with 1
 huffman parents: 
46057diff
changeset | 3540 | lemma word_bitwise_1_simps [simp]: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3541 | "NOT (1::'a::len word) = -2" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3542 | "1 AND numeral b = word_of_int (1 AND numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3543 | "1 AND - numeral b = word_of_int (1 AND - numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3544 | "numeral a AND 1 = word_of_int (numeral a AND 1)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3545 | "- numeral a AND 1 = word_of_int (- numeral a AND 1)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3546 | "1 OR numeral b = word_of_int (1 OR numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3547 | "1 OR - numeral b = word_of_int (1 OR - numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3548 | "numeral a OR 1 = word_of_int (numeral a OR 1)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3549 | "- numeral a OR 1 = word_of_int (- numeral a OR 1)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3550 | "1 XOR numeral b = word_of_int (1 XOR numeral b)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3551 | "1 XOR - numeral b = word_of_int (1 XOR - numeral b)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3552 | "numeral a XOR 1 = word_of_int (numeral a XOR 1)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54225diff
changeset | 3553 | "- numeral a XOR 1 = word_of_int (- numeral a XOR 1)" | 
| 74496 | 3554 | apply (simp_all add: word_uint_eq_iff unsigned_not_eq unsigned_and_eq unsigned_or_eq | 
| 3555 | unsigned_xor_eq of_nat_take_bit ac_simps unsigned_of_int) | |
| 74163 | 3556 | apply (simp_all add: minus_numeral_eq_not_sub_one) | 
| 3557 | apply (simp_all only: sub_one_eq_not_neg bit.xor_compl_right take_bit_xor bit.double_compl) | |
| 3558 | apply simp_all | |
| 3559 | done | |
| 46064 
88ef116e0522
add simp rules for bitwise word operations with 1
 huffman parents: 
46057diff
changeset | 3560 | |
| 61799 | 3561 | text \<open>Special cases for when one of the arguments equals -1.\<close> | 
| 56979 | 3562 | |
| 3563 | lemma word_bitwise_m1_simps [simp]: | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3564 | "NOT (-1::'a::len word) = 0" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3565 | "(-1::'a::len word) AND x = x" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3566 | "x AND (-1::'a::len word) = x" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3567 | "(-1::'a::len word) OR x = -1" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3568 | "x OR (-1::'a::len word) = -1" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3569 | " (-1::'a::len word) XOR x = NOT x" | 
| 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3570 | "x XOR (-1::'a::len word) = NOT x" | 
| 56979 | 3571 | by (transfer, simp)+ | 
| 3572 | ||
| 74163 | 3573 | lemma word_of_int_not_numeral_eq [simp]: | 
| 3574 | \<open>(word_of_int (NOT (numeral bin)) :: 'a::len word) = - numeral bin - 1\<close> | |
| 3575 | by transfer (simp add: not_eq_complement) | |
| 3576 | ||
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3577 | lemma uint_and: | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3578 | \<open>uint (x AND y) = uint x AND uint y\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3579 | by transfer simp | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3580 | |
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3581 | lemma uint_or: | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3582 | \<open>uint (x OR y) = uint x OR uint y\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3583 | by transfer simp | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3584 | |
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3585 | lemma uint_xor: | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3586 | \<open>uint (x XOR y) = uint x XOR uint y\<close> | 
| 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3587 | by transfer simp | 
| 47372 | 3588 | |
| 67408 | 3589 | \<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close> | 
| 65268 | 3590 | lemmas bwsimps = | 
| 46013 | 3591 | wi_hom_add | 
| 37660 | 3592 | word_wi_log_defs | 
| 3593 | ||
| 3594 | lemma word_bw_assocs: | |
| 3595 | "(x AND y) AND z = x AND y AND z" | |
| 3596 | "(x OR y) OR z = x OR y OR z" | |
| 3597 | "(x XOR y) XOR z = x XOR y XOR z" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3598 | for x :: "'a::len word" | 
| 72508 | 3599 | by (fact ac_simps)+ | 
| 65268 | 3600 | |
| 37660 | 3601 | lemma word_bw_comms: | 
| 3602 | "x AND y = y AND x" | |
| 3603 | "x OR y = y OR x" | |
| 3604 | "x XOR y = y XOR x" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3605 | for x :: "'a::len word" | 
| 72508 | 3606 | by (fact ac_simps)+ | 
| 65268 | 3607 | |
| 37660 | 3608 | lemma word_bw_lcs: | 
| 3609 | "y AND x AND z = x AND y AND z" | |
| 3610 | "y OR x OR z = x OR y OR z" | |
| 3611 | "y XOR x XOR z = x XOR y XOR z" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3612 | for x :: "'a::len word" | 
| 72508 | 3613 | by (fact ac_simps)+ | 
| 37660 | 3614 | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3615 | lemma word_log_esimps: | 
| 37660 | 3616 | "x AND 0 = 0" | 
| 3617 | "x AND -1 = x" | |
| 3618 | "x OR 0 = x" | |
| 3619 | "x OR -1 = -1" | |
| 3620 | "x XOR 0 = x" | |
| 3621 | "x XOR -1 = NOT x" | |
| 3622 | "0 AND x = 0" | |
| 3623 | "-1 AND x = x" | |
| 3624 | "0 OR x = x" | |
| 3625 | "-1 OR x = -1" | |
| 3626 | "0 XOR x = x" | |
| 3627 | "-1 XOR x = NOT x" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3628 | for x :: "'a::len word" | 
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3629 | by simp_all | 
| 37660 | 3630 | |
| 3631 | lemma word_not_dist: | |
| 3632 | "NOT (x OR y) = NOT x AND NOT y" | |
| 3633 | "NOT (x AND y) = NOT x OR NOT y" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3634 | for x :: "'a::len word" | 
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3635 | by simp_all | 
| 37660 | 3636 | |
| 3637 | lemma word_bw_same: | |
| 3638 | "x AND x = x" | |
| 3639 | "x OR x = x" | |
| 3640 | "x XOR x = 0" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3641 | for x :: "'a::len word" | 
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3642 | by simp_all | 
| 37660 | 3643 | |
| 3644 | lemma word_ao_absorbs [simp]: | |
| 3645 | "x AND (y OR x) = x" | |
| 3646 | "x OR y AND x = x" | |
| 3647 | "x AND (x OR y) = x" | |
| 3648 | "y AND x OR x = x" | |
| 3649 | "(y OR x) AND x = x" | |
| 3650 | "x OR x AND y = x" | |
| 3651 | "(x OR y) AND x = x" | |
| 3652 | "x AND y OR x = x" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3653 | for x :: "'a::len word" | 
| 72508 | 3654 | by (auto intro: bit_eqI simp add: bit_and_iff bit_or_iff) | 
| 37660 | 3655 | |
| 71149 | 3656 | lemma word_not_not [simp]: "NOT (NOT x) = x" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3657 | for x :: "'a::len word" | 
| 72508 | 3658 | by (fact bit.double_compl) | 
| 37660 | 3659 | |
| 65328 | 3660 | lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3661 | for x :: "'a::len word" | 
| 72508 | 3662 | by (fact bit.conj_disj_distrib2) | 
| 37660 | 3663 | |
| 65328 | 3664 | lemma word_oa_dist: "x AND y OR z = (x OR z) AND (y OR z)" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3665 | for x :: "'a::len word" | 
| 72508 | 3666 | by (fact bit.disj_conj_distrib2) | 
| 3667 | ||
| 65328 | 3668 | lemma word_add_not [simp]: "x + NOT x = -1" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3669 | for x :: "'a::len word" | 
| 72508 | 3670 | by (simp add: not_eq_complement) | 
| 3671 | ||
| 65328 | 3672 | lemma word_plus_and_or [simp]: "(x AND y) + (x OR y) = x + y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3673 | for x :: "'a::len word" | 
| 47372 | 3674 | by transfer (simp add: plus_and_or) | 
| 37660 | 3675 | |
| 65328 | 3676 | lemma leoa: "w = x OR y \<Longrightarrow> y = w AND y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3677 | for x :: "'a::len word" | 
| 65328 | 3678 | by auto | 
| 3679 | ||
| 3680 | lemma leao: "w' = x' AND y' \<Longrightarrow> x' = x' OR w'" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3681 | for x' :: "'a::len word" | 
| 65328 | 3682 | by auto | 
| 3683 | ||
| 3684 | lemma word_ao_equiv: "w = w OR w' \<longleftrightarrow> w' = w AND w'" | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3685 | for w w' :: "'a::len word" | 
| 48196 | 3686 | by (auto intro: leoa leao) | 
| 37660 | 3687 | |
| 65328 | 3688 | lemma le_word_or2: "x \<le> x OR y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 3689 | for x y :: "'a::len word" | 
| 72488 | 3690 | by (simp add: or_greater_eq uint_or word_le_def) | 
| 37660 | 3691 | |
| 71997 | 3692 | lemmas le_word_or1 = xtrans(3) [OF word_bw_comms (2) le_word_or2] | 
| 3693 | lemmas word_and_le1 = xtrans(3) [OF word_ao_absorbs (4) [symmetric] le_word_or2] | |
| 3694 | lemmas word_and_le2 = xtrans(3) [OF word_ao_absorbs (8) [symmetric] le_word_or2] | |
| 37660 | 3695 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 3696 | lemma bit_horner_sum_bit_word_iff [bit_simps]: | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3697 | \<open>bit (horner_sum of_bool (2 :: 'a::len word) bs) n | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3698 |     \<longleftrightarrow> n < min LENGTH('a) (length bs) \<and> bs ! n\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3699 | by transfer (simp add: bit_horner_sum_bit_iff) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3700 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3701 | definition word_reverse :: \<open>'a::len word \<Rightarrow> 'a word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3702 |   where \<open>word_reverse w = horner_sum of_bool 2 (rev (map (bit w) [0..<LENGTH('a)]))\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3703 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 3704 | lemma bit_word_reverse_iff [bit_simps]: | 
| 71990 | 3705 |   \<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close>
 | 
| 3706 | for w :: \<open>'a::len word\<close> | |
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3707 |   by (cases \<open>n < LENGTH('a)\<close>)
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3708 | (simp_all add: word_reverse_def bit_horner_sum_bit_word_iff rev_nth) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3709 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3710 | lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3711 | by (rule bit_word_eqI) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3712 | (auto simp add: bit_word_reverse_iff bit_imp_le_length Suc_diff_Suc) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3713 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3714 | lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3715 | by (metis word_rev_rev) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3716 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3717 | lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3718 | by simp | 
| 37660 | 3719 | |
| 65328 | 3720 | lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3721 |   by (cases \<open>n < LENGTH('a)\<close>; transfer; force)
 | 
| 37660 | 3722 | |
| 65268 | 3723 | lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n" | 
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
64243diff
changeset | 3724 | by (induct n) (simp_all add: wi_hom_syms) | 
| 37660 | 3725 | |
| 3726 | ||
| 61799 | 3727 | subsubsection \<open>shift functions in terms of lists of bools\<close> | 
| 37660 | 3728 | |
| 71997 | 3729 | lemma drop_bit_word_numeral [simp]: | 
| 3730 | \<open>drop_bit (numeral n) (numeral k) = | |
| 3731 |     (word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
 | |
| 3732 | by transfer simp | |
| 3733 | ||
| 74498 | 3734 | lemma drop_bit_word_Suc_numeral [simp]: | 
| 3735 | \<open>drop_bit (Suc n) (numeral k) = | |
| 3736 |     (word_of_int (drop_bit (Suc n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
 | |
| 3737 | by transfer simp | |
| 3738 | ||
| 3739 | lemma drop_bit_word_minus_numeral [simp]: | |
| 3740 | \<open>drop_bit (numeral n) (- numeral k) = | |
| 3741 |     (word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3742 | by transfer simp | |
| 3743 | ||
| 3744 | lemma drop_bit_word_Suc_minus_numeral [simp]: | |
| 3745 | \<open>drop_bit (Suc n) (- numeral k) = | |
| 3746 |     (word_of_int (drop_bit (Suc n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3747 | by transfer simp | |
| 3748 | ||
| 73853 | 3749 | lemma signed_drop_bit_word_numeral [simp]: | 
| 3750 | \<open>signed_drop_bit (numeral n) (numeral k) = | |
| 3751 |     (word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (numeral k))) :: 'a::len word)\<close>
 | |
| 3752 | by transfer simp | |
| 3753 | ||
| 74498 | 3754 | lemma signed_drop_bit_word_Suc_numeral [simp]: | 
| 3755 | \<open>signed_drop_bit (Suc n) (numeral k) = | |
| 3756 |     (word_of_int (drop_bit (Suc n) (signed_take_bit (LENGTH('a) - 1) (numeral k))) :: 'a::len word)\<close>
 | |
| 3757 | by transfer simp | |
| 3758 | ||
| 3759 | lemma signed_drop_bit_word_minus_numeral [simp]: | |
| 3760 | \<open>signed_drop_bit (numeral n) (- numeral k) = | |
| 3761 |     (word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3762 | by transfer simp | |
| 3763 | ||
| 3764 | lemma signed_drop_bit_word_Suc_minus_numeral [simp]: | |
| 3765 | \<open>signed_drop_bit (Suc n) (- numeral k) = | |
| 3766 |     (word_of_int (drop_bit (Suc n) (signed_take_bit (LENGTH('a) - 1) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3767 | by transfer simp | |
| 3768 | ||
| 3769 | lemma take_bit_word_numeral [simp]: | |
| 3770 | \<open>take_bit (numeral n) (numeral k) = | |
| 3771 |     (word_of_int (take_bit (min LENGTH('a) (numeral n)) (numeral k)) :: 'a::len word)\<close>
 | |
| 3772 | by transfer rule | |
| 3773 | ||
| 3774 | lemma take_bit_word_Suc_numeral [simp]: | |
| 3775 | \<open>take_bit (Suc n) (numeral k) = | |
| 3776 |     (word_of_int (take_bit (min LENGTH('a) (Suc n)) (numeral k)) :: 'a::len word)\<close>
 | |
| 3777 | by transfer rule | |
| 3778 | ||
| 3779 | lemma take_bit_word_minus_numeral [simp]: | |
| 3780 | \<open>take_bit (numeral n) (- numeral k) = | |
| 3781 |     (word_of_int (take_bit (min LENGTH('a) (numeral n)) (- numeral k)) :: 'a::len word)\<close>
 | |
| 3782 | by transfer rule | |
| 3783 | ||
| 3784 | lemma take_bit_word_Suc_minus_numeral [simp]: | |
| 3785 | \<open>take_bit (Suc n) (- numeral k) = | |
| 3786 |     (word_of_int (take_bit (min LENGTH('a) (Suc n)) (- numeral k)) :: 'a::len word)\<close>
 | |
| 3787 | by transfer rule | |
| 3788 | ||
| 3789 | lemma signed_take_bit_word_numeral [simp]: | |
| 3790 | \<open>signed_take_bit (numeral n) (numeral k) = | |
| 3791 |     (word_of_int (signed_take_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
 | |
| 3792 | by transfer rule | |
| 3793 | ||
| 3794 | lemma signed_take_bit_word_Suc_numeral [simp]: | |
| 3795 | \<open>signed_take_bit (Suc n) (numeral k) = | |
| 3796 |     (word_of_int (signed_take_bit (Suc n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
 | |
| 3797 | by transfer rule | |
| 3798 | ||
| 3799 | lemma signed_take_bit_word_minus_numeral [simp]: | |
| 3800 | \<open>signed_take_bit (numeral n) (- numeral k) = | |
| 3801 |     (word_of_int (signed_take_bit (numeral n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3802 | by transfer rule | |
| 3803 | ||
| 3804 | lemma signed_take_bit_word_Suc_minus_numeral [simp]: | |
| 3805 | \<open>signed_take_bit (Suc n) (- numeral k) = | |
| 3806 |     (word_of_int (signed_take_bit (Suc n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
 | |
| 3807 | by transfer rule | |
| 3808 | ||
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3809 | lemma False_map2_or: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<or>) xs ys = ys"
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3810 | by (induction xs arbitrary: ys) (auto simp: length_Suc_conv) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3811 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3812 | lemma align_lem_or: | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3813 | assumes "length xs = n + m" "length ys = n + m" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3814 | and "drop m xs = replicate n False" "take m ys = replicate m False" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3815 | shows "map2 (\<or>) xs ys = take m xs @ drop m ys" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3816 | using assms | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3817 | proof (induction xs arbitrary: ys m) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3818 | case (Cons a xs) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3819 | then show ?case | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3820 | by (cases m) (auto simp: length_Suc_conv False_map2_or) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3821 | qed auto | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3822 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3823 | lemma False_map2_and: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<and>) xs ys = xs"
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3824 | by (induction xs arbitrary: ys) (auto simp: length_Suc_conv) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3825 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3826 | lemma align_lem_and: | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3827 | assumes "length xs = n + m" "length ys = n + m" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3828 | and "drop m xs = replicate n False" "take m ys = replicate m False" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3829 | shows "map2 (\<and>) xs ys = replicate (n + m) False" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3830 | using assms | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3831 | proof (induction xs arbitrary: ys m) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3832 | case (Cons a xs) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3833 | then show ?case | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3834 | by (cases m) (auto simp: length_Suc_conv set_replicate_conv_if False_map2_and) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3835 | qed auto | 
| 37660 | 3836 | |
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 3837 | |
| 61799 | 3838 | subsubsection \<open>Mask\<close> | 
| 37660 | 3839 | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 3840 | lemma minus_1_eq_mask: | 
| 72082 | 3841 |   \<open>- 1 = (mask LENGTH('a) :: 'a::len word)\<close>
 | 
| 74101 | 3842 | by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff) | 
| 72079 | 3843 | |
| 3844 | lemma mask_eq_decr_exp: | |
| 72082 | 3845 | \<open>mask n = 2 ^ n - (1 :: 'a::len word)\<close> | 
| 3846 | by (fact mask_eq_exp_minus_1) | |
| 71953 | 3847 | |
| 3848 | lemma mask_Suc_rec: | |
| 72082 | 3849 | \<open>mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\<close> | 
| 3850 | by (simp add: mask_eq_exp_minus_1) | |
| 71953 | 3851 | |
| 3852 | context | |
| 3853 | begin | |
| 3854 | ||
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 3855 | qualified lemma bit_mask_iff [bit_simps]: | 
| 71990 | 3856 |   \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close>
 | 
| 74101 | 3857 | by (simp add: bit_mask_iff not_le) | 
| 71953 | 3858 | |
| 3859 | end | |
| 3860 | ||
| 72128 | 3861 | lemma mask_bin: "mask n = word_of_int (take_bit n (- 1))" | 
| 74592 | 3862 | by transfer simp | 
| 37660 | 3863 | |
| 72128 | 3864 | lemma and_mask_bintr: "w AND mask n = word_of_int (take_bit n (uint w))" | 
| 72488 | 3865 | by transfer (simp add: ac_simps take_bit_eq_mask) | 
| 37660 | 3866 | |
| 72128 | 3867 | lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (take_bit n i)" | 
| 74496 | 3868 | by (simp add: take_bit_eq_mask of_int_and_eq of_int_mask_eq) | 
| 46023 
fad87bb608fc
restate some lemmas to respect int/bin distinction
 huffman parents: 
46022diff
changeset | 3869 | |
| 65328 | 3870 | lemma and_mask_wi': | 
| 72128 | 3871 |   "word_of_int i AND mask n = (word_of_int (take_bit (min LENGTH('a) n) i) :: 'a::len word)"
 | 
| 72488 | 3872 | by (auto simp add: and_mask_wi min_def wi_bintr) | 
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
64243diff
changeset | 3873 | |
| 72128 | 3874 | lemma and_mask_no: "numeral i AND mask n = word_of_int (take_bit n (numeral i))" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 3875 | unfolding word_numeral_alt by (rule and_mask_wi) | 
| 37660 | 3876 | |
| 45811 | 3877 | lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)" | 
| 72128 | 3878 | by (simp only: and_mask_bintr take_bit_eq_mod) | 
| 37660 | 3879 | |
| 72130 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 3880 | lemma uint_mask_eq: | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 3881 |   \<open>uint (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close>
 | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 3882 | by transfer simp | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 3883 | |
| 37660 | 3884 | lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3885 | by (metis take_bit_eq_mask take_bit_int_less_exp unsigned_take_bit_eq) | 
| 37660 | 3886 | |
| 65363 | 3887 | lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n" | 
| 72292 | 3888 | apply (auto simp flip: take_bit_eq_mask) | 
| 3889 | apply (metis take_bit_int_eq_self_iff uint_take_bit_eq) | |
| 3890 | apply (simp add: take_bit_int_eq_self unsigned_take_bit_eq word_uint_eqI) | |
| 37660 | 3891 | done | 
| 3892 | ||
| 65328 | 3893 | lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0" | 
| 72262 | 3894 | by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 uint_0_iff) | 
| 37660 | 3895 | |
| 65328 | 3896 | lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0" | 
| 72262 | 3897 | by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 unat_0_iff uint_0_iff) | 
| 37660 | 3898 | |
| 65328 | 3899 | lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)" | 
| 3900 | for w :: "'a::len word" | |
| 72262 | 3901 | by transfer simp | 
| 37660 | 3902 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3903 | lemma less_mask_eq: | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3904 | fixes x :: "'a::len word" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3905 | assumes "x < 2 ^ n" shows "x AND mask n = x" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3906 | by (metis (no_types) assms lt2p_lem mask_eq_iff not_less word_2p_lem word_size) | 
| 37660 | 3907 | |
| 45604 | 3908 | lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]] | 
| 3909 | ||
| 3910 | lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size] | |
| 37660 | 3911 | |
| 72082 | 3912 | lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2 ^ n" | 
| 3913 | for x :: \<open>'a::len word\<close> | |
| 37660 | 3914 | unfolding word_size by (erule and_mask_less') | 
| 3915 | ||
| 65328 | 3916 | lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n" | 
| 3917 | for c x :: "'a::len word" | |
| 3918 | by (auto simp: word_mod_def uint_2p and_mask_mod_2p) | |
| 37660 | 3919 | |
| 3920 | lemma mask_eqs: | |
| 3921 | "(a AND mask n) + b AND mask n = a + b AND mask n" | |
| 3922 | "a + (b AND mask n) AND mask n = a + b AND mask n" | |
| 3923 | "(a AND mask n) - b AND mask n = a - b AND mask n" | |
| 3924 | "a - (b AND mask n) AND mask n = a - b AND mask n" | |
| 3925 | "a * (b AND mask n) AND mask n = a * b AND mask n" | |
| 3926 | "(b AND mask n) * a AND mask n = b * a AND mask n" | |
| 3927 | "(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n" | |
| 3928 | "(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n" | |
| 3929 | "(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n" | |
| 3930 | "- (a AND mask n) AND mask n = - a AND mask n" | |
| 3931 | "word_succ (a AND mask n) AND mask n = word_succ a AND mask n" | |
| 3932 | "word_pred (a AND mask n) AND mask n = word_pred a AND mask n" | |
| 3933 | using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b] | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3934 | unfolding take_bit_eq_mask [symmetric] | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3935 | by (transfer; simp add: take_bit_eq_mod mod_simps)+ | 
| 65328 | 3936 | |
| 3937 | lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n" | |
| 72082 | 3938 | for x :: \<open>'a::len word\<close> | 
| 37660 | 3939 | using word_of_int_Ex [where x=x] | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3940 | unfolding take_bit_eq_mask [symmetric] | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3941 | by (transfer; simp add: take_bit_eq_mod mod_simps)+ | 
| 37660 | 3942 | |
| 70183 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3943 | lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)"
 | 
| 74592 | 3944 | by transfer simp | 
| 70183 
3ea80c950023
incorporated various material from the AFP into the distribution
 haftmann parents: 
70175diff
changeset | 3945 | |
| 37660 | 3946 | |
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3947 | subsubsection \<open>Slices\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3948 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3949 | definition slice1 :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3950 |   where \<open>slice1 n w = (if n < LENGTH('a)
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3951 |     then ucast (drop_bit (LENGTH('a) - n) w)
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3952 |     else push_bit (n - LENGTH('a)) (ucast w))\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3953 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 3954 | lemma bit_slice1_iff [bit_simps]: | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3955 |   \<open>bit (slice1 m w :: 'b::len word) n \<longleftrightarrow> m - LENGTH('a) \<le> n \<and> n < min LENGTH('b) m
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3956 |     \<and> bit w (n + (LENGTH('a) - m) - (m - LENGTH('a)))\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3957 | for w :: \<open>'a::len word\<close> | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3958 | by (auto simp add: slice1_def bit_ucast_iff bit_drop_bit_eq bit_push_bit_iff not_less not_le ac_simps | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3959 | dest: bit_imp_le_length) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3960 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3961 | definition slice :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3962 |   where \<open>slice n = slice1 (LENGTH('a) - n)\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3963 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 3964 | lemma bit_slice_iff [bit_simps]: | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3965 |   \<open>bit (slice m w :: 'b::len word) n \<longleftrightarrow> n < min LENGTH('b) (LENGTH('a) - m) \<and> bit w (n + LENGTH('a) - (LENGTH('a) - m))\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3966 | for w :: \<open>'a::len word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3967 | by (simp add: slice_def word_size bit_slice1_iff) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3968 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3969 | lemma slice1_0 [simp] : "slice1 n 0 = 0" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3970 | unfolding slice1_def by simp | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3971 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3972 | lemma slice_0 [simp] : "slice n 0 = 0" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3973 | unfolding slice_def by auto | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3974 | |
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3975 | lemma ucast_slice1: "ucast w = slice1 (size w) w" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3976 | unfolding slice1_def by (simp add: size_word.rep_eq) | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3977 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3978 | lemma ucast_slice: "ucast w = slice 0 w" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3979 | by (simp add: slice_def slice1_def) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3980 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3981 | lemma slice_id: "slice 0 t = t" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3982 | by (simp only: ucast_slice [symmetric] ucast_id) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3983 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3984 | lemma rev_slice1: | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3985 | \<open>slice1 n (word_reverse w :: 'b::len word) = word_reverse (slice1 k w :: 'a::len word)\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3986 |   if \<open>n + k = LENGTH('a) + LENGTH('b)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3987 | proof (rule bit_word_eqI) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3988 | fix m | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3989 |   assume *: \<open>m < LENGTH('a)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3990 |   from that have **: \<open>LENGTH('b) = n + k - LENGTH('a)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3991 | by simp | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3992 | show \<open>bit (slice1 n (word_reverse w :: 'b word) :: 'a word) m \<longleftrightarrow> bit (word_reverse (slice1 k w :: 'a word)) m\<close> | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3993 | unfolding bit_slice1_iff bit_word_reverse_iff | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3994 | using * ** | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 3995 |     by (cases \<open>n \<le> LENGTH('a)\<close>; cases \<open>k \<le> LENGTH('a)\<close>) auto
 | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 3996 | qed | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3997 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3998 | lemma rev_slice: | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 3999 |   "n + k + LENGTH('a::len) = LENGTH('b::len) \<Longrightarrow>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4000 | slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4001 | unfolding slice_def word_size | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4002 | by (simp add: rev_slice1) | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4003 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4004 | |
| 61799 | 4005 | subsubsection \<open>Revcast\<close> | 
| 37660 | 4006 | |
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4007 | definition revcast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4008 |   where \<open>revcast = slice1 LENGTH('b)\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4009 | |
| 72611 
c7bc3e70a8c7
official collection for bit projection simplifications
 haftmann parents: 
72515diff
changeset | 4010 | lemma bit_revcast_iff [bit_simps]: | 
| 72027 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4011 |   \<open>bit (revcast w :: 'b::len word) n \<longleftrightarrow> LENGTH('b) - LENGTH('a) \<le> n \<and> n < LENGTH('b)
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4012 |     \<and> bit w (n + (LENGTH('a) - LENGTH('b)) - (LENGTH('b) - LENGTH('a)))\<close>
 | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4013 | for w :: \<open>'a::len word\<close> | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4014 | by (simp add: revcast_def bit_slice1_iff) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4015 | |
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4016 | lemma revcast_slice1 [OF refl]: "rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc" | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4017 | by (simp add: revcast_def word_size) | 
| 
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
 haftmann parents: 
72010diff
changeset | 4018 | |
| 65268 | 4019 | lemma revcast_rev_ucast [OF refl refl refl]: | 
| 4020 | "cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow> | |
| 37660 | 4021 | rc = word_reverse uc" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4022 | by (metis rev_slice1 revcast_slice1 ucast_slice1 word_size) | 
| 37660 | 4023 | |
| 45811 | 4024 | lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))" | 
| 4025 | using revcast_rev_ucast [of "word_reverse w"] by simp | |
| 4026 | ||
| 4027 | lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))" | |
| 4028 | by (fact revcast_rev_ucast [THEN word_rev_gal']) | |
| 4029 | ||
| 4030 | lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)" | |
| 4031 | by (fact revcast_ucast [THEN word_rev_gal']) | |
| 37660 | 4032 | |
| 4033 | ||
| 65328 | 4034 | text "linking revcast and cast via shift" | 
| 37660 | 4035 | |
| 4036 | lemmas wsst_TYs = source_size target_size word_size | |
| 4037 | ||
| 65268 | 4038 | lemmas sym_notr = | 
| 37660 | 4039 | not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]] | 
| 4040 | ||
| 4041 | ||
| 61799 | 4042 | subsection \<open>Split and cat\<close> | 
| 37660 | 4043 | |
| 40827 
abbc05c20e24
code preprocessor setup for numerals on word type;
 haftmann parents: 
39910diff
changeset | 4044 | lemmas word_split_bin' = word_split_def | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4045 | lemmas word_cat_bin' = word_cat_eq | 
| 37660 | 4046 | |
| 65268 | 4047 | \<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>, | 
| 61799 | 4048 | result to the length given by the result type\<close> | 
| 37660 | 4049 | |
| 4050 | lemma word_cat_id: "word_cat a b = b" | |
| 72488 | 4051 | by transfer (simp add: take_bit_concat_bit_eq) | 
| 65336 | 4052 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4053 | lemma word_cat_split_alt: "\<lbrakk>size w \<le> size u + size v; word_split w = (u,v)\<rbrakk> \<Longrightarrow> word_cat u v = w" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4054 | unfolding word_split_def | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4055 | by (rule bit_word_eqI) (auto simp add: bit_word_cat_iff not_less word_size bit_ucast_iff bit_drop_bit_eq) | 
| 37660 | 4056 | |
| 45604 | 4057 | lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]] | 
| 37660 | 4058 | |
| 4059 | ||
| 61799 | 4060 | subsubsection \<open>Split and slice\<close> | 
| 37660 | 4061 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4062 | lemma split_slices: | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4063 | assumes "word_split w = (u, v)" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4064 | shows "u = slice (size v) w \<and> v = slice 0 w" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4065 | unfolding word_size | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4066 | proof (intro conjI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4067 |   have \<section>: "\<And>n. \<lbrakk>ucast (drop_bit LENGTH('b) w) = u; LENGTH('c) < LENGTH('b)\<rbrakk> \<Longrightarrow> \<not> bit u n"
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4068 | by (metis bit_take_bit_iff bit_word_of_int_iff diff_is_0_eq' drop_bit_take_bit less_imp_le less_nat_zero_code of_int_uint unsigned_drop_bit_eq) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4069 |   show "u = slice LENGTH('b) w"
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4070 | proof (rule bit_word_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4071 |     show "bit u n = bit ((slice LENGTH('b) w)::'a word) n" if "n < LENGTH('a)" for n
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4072 | using assms bit_imp_le_length | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4073 | unfolding word_split_def bit_slice_iff | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4074 | by (fastforce simp add: \<section> ac_simps word_size bit_ucast_iff bit_drop_bit_eq) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4075 | qed | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4076 | show "v = slice 0 w" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4077 | by (metis Pair_inject assms ucast_slice word_split_bin') | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4078 | qed | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4079 | |
| 37660 | 4080 | |
| 45816 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
 huffman parents: 
45811diff
changeset | 4081 | lemma slice_cat1 [OF refl]: | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4082 | "\<lbrakk>wc = word_cat a b; size a + size b \<le> size wc\<rbrakk> \<Longrightarrow> slice (size b) wc = a" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4083 | by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size) | 
| 37660 | 4084 | |
| 4085 | lemmas slice_cat2 = trans [OF slice_id word_cat_id] | |
| 4086 | ||
| 4087 | lemma cat_slices: | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4088 | "\<lbrakk>a = slice n c; b = slice 0 c; n = size b; size c \<le> size a + size b\<rbrakk> \<Longrightarrow> word_cat a b = c" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4089 | by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size) | 
| 37660 | 4090 | |
| 4091 | lemma word_split_cat_alt: | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4092 | assumes "w = word_cat u v" and size: "size u + size v \<le> size w" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4093 | shows "word_split w = (u,v)" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4094 | proof - | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4095 |   have "ucast ((drop_bit LENGTH('c) (word_cat u v))::'a word) = u" "ucast ((word_cat u v)::'a word) = v"
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4096 | using assms | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4097 | by (auto simp add: word_size bit_ucast_iff bit_drop_bit_eq bit_word_cat_iff intro: bit_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4098 | then show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4099 | by (simp add: assms(1) word_split_bin') | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4100 | qed | 
| 37660 | 4101 | |
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4102 | lemma horner_sum_uint_exp_Cons_eq: | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4103 |   \<open>horner_sum uint (2 ^ LENGTH('a)) (w # ws) =
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4104 |     concat_bit LENGTH('a) (uint w) (horner_sum uint (2 ^ LENGTH('a)) ws)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4105 | for ws :: \<open>'a::len word list\<close> | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4106 | by (simp add: bintr_uint concat_bit_eq push_bit_eq_mult) | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4107 | |
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4108 | lemma bit_horner_sum_uint_exp_iff: | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4109 |   \<open>bit (horner_sum uint (2 ^ LENGTH('a)) ws) n \<longleftrightarrow>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4110 |     n div LENGTH('a) < length ws \<and> bit (ws ! (n div LENGTH('a))) (n mod LENGTH('a))\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4111 | for ws :: \<open>'a::len word list\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4112 | proof (induction ws arbitrary: n) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4113 | case Nil | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4114 | then show ?case | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4115 | by simp | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4116 | next | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4117 | case (Cons w ws) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4118 | then show ?case | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4119 |     by (cases \<open>n \<ge> LENGTH('a)\<close>)
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4120 | (simp_all only: horner_sum_uint_exp_Cons_eq, simp_all add: bit_concat_bit_iff le_div_geq le_mod_geq bit_uint_iff Cons) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4121 | qed | 
| 37660 | 4122 | |
| 4123 | ||
| 61799 | 4124 | subsection \<open>Rotation\<close> | 
| 37660 | 4125 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4126 | lemma word_rotr_word_rotr_eq: \<open>word_rotr m (word_rotr n w) = word_rotr (m + n) w\<close> | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4127 | by (rule bit_word_eqI) (simp add: bit_word_rotr_iff ac_simps mod_add_right_eq) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4128 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4129 | lemma word_rot_lem: "\<lbrakk>l + k = d + k mod l; n < l\<rbrakk> \<Longrightarrow> ((d + n) mod l) = n" for l::nat | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4130 | by (metis (no_types, lifting) add.commute add.right_neutral add_diff_cancel_left' mod_if mod_mult_div_eq mod_mult_self2 mod_self) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4131 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4132 | lemma word_rot_rl [simp]: \<open>word_rotl k (word_rotr k v) = v\<close> | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4133 | proof (rule bit_word_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4134 |   show "bit (word_rotl k (word_rotr k v)) n = bit v n" if "n < LENGTH('a)" for n
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4135 | using that | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4136 | by (auto simp: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4137 | qed | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4138 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4139 | lemma word_rot_lr [simp]: \<open>word_rotr k (word_rotl k v) = v\<close> | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4140 | proof (rule bit_word_eqI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4141 |   show "bit (word_rotr k (word_rotl k v)) n = bit v n" if "n < LENGTH('a)" for n
 | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4142 | using that | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4143 | by (auto simp add: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4144 | qed | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4145 | |
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4146 | lemma word_rot_gal: | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4147 | \<open>word_rotr n v = w \<longleftrightarrow> word_rotl n w = v\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4148 | by auto | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4149 | |
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4150 | lemma word_rot_gal': | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4151 | \<open>w = word_rotr n v \<longleftrightarrow> v = word_rotl n w\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4152 | by auto | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4153 | |
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4154 | lemma word_rotr_rev: | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4155 | \<open>word_rotr n w = word_reverse (word_rotl n (word_reverse w))\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4156 | proof (rule bit_word_eqI) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4157 | fix m | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4158 |   assume \<open>m < LENGTH('a)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4159 | moreover have \<open>1 + | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4160 |     ((int m + int n mod int LENGTH('a)) mod int LENGTH('a) +
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4161 |      ((int LENGTH('a) * 2) mod int LENGTH('a) - (1 + (int m + int n mod int LENGTH('a)))) mod int LENGTH('a)) =
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4162 |     int LENGTH('a)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4163 |     apply (cases \<open>(1 + (int m + int n mod int LENGTH('a))) mod
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4164 |          int LENGTH('a) = 0\<close>)
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4165 |     using zmod_zminus1_eq_if [of \<open>1 + (int m + int n mod int LENGTH('a))\<close> \<open>int LENGTH('a)\<close>]
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4166 | apply simp_all | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4167 | apply (auto simp add: algebra_simps) | 
| 73932 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73853diff
changeset | 4168 | apply (metis (mono_tags, opaque_lifting) Abs_fnat_hom_add mod_Suc mod_mult_self2_is_0 of_nat_Suc of_nat_mod semiring_char_0_class.of_nat_neq_0) | 
| 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73853diff
changeset | 4169 | apply (metis (no_types, opaque_lifting) Abs_fnat_hom_add less_not_refl mod_Suc of_nat_Suc of_nat_gt_0 of_nat_mod) | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4170 | done | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4171 |   then have \<open>int ((m + n) mod LENGTH('a)) =
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4172 |     int (LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a)))\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4173 |     using \<open>m < LENGTH('a)\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4174 | by (simp only: of_nat_mod mod_simps) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4175 | (simp add: of_nat_diff of_nat_mod Suc_le_eq add_less_mono algebra_simps mod_simps) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4176 |   then have \<open>(m + n) mod LENGTH('a) =
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4177 |     LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a))\<close>
 | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4178 | by simp | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4179 | ultimately show \<open>bit (word_rotr n w) m \<longleftrightarrow> bit (word_reverse (word_rotl n (word_reverse w))) m\<close> | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4180 | by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff bit_word_reverse_iff) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4181 | qed | 
| 65268 | 4182 | |
| 37660 | 4183 | lemma word_roti_0 [simp]: "word_roti 0 w = w" | 
| 72079 | 4184 | by transfer simp | 
| 37660 | 4185 | |
| 65336 | 4186 | lemma word_roti_add: "word_roti (m + n) w = word_roti m (word_roti n w)" | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4187 | by (rule bit_word_eqI) | 
| 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4188 | (simp add: bit_word_roti_iff nat_less_iff mod_simps ac_simps) | 
| 65268 | 4189 | |
| 67118 | 4190 | lemma word_roti_conv_mod': | 
| 4191 | "word_roti n w = word_roti (n mod int (size w)) w" | |
| 72079 | 4192 | by transfer simp | 
| 37660 | 4193 | |
| 4194 | lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size] | |
| 4195 | ||
| 74097 | 4196 | end | 
| 4197 | ||
| 37660 | 4198 | |
| 61799 | 4199 | subsubsection \<open>"Word rotation commutes with bit-wise operations\<close> | 
| 37660 | 4200 | |
| 67408 | 4201 | \<comment> \<open>using locale to not pollute lemma namespace\<close> | 
| 65268 | 4202 | locale word_rotate | 
| 37660 | 4203 | begin | 
| 4204 | ||
| 74097 | 4205 | context | 
| 4206 | includes bit_operations_syntax | |
| 4207 | begin | |
| 4208 | ||
| 37660 | 4209 | lemma word_rot_logs: | 
| 71149 | 4210 | "word_rotl n (NOT v) = NOT (word_rotl n v)" | 
| 4211 | "word_rotr n (NOT v) = NOT (word_rotr n v)" | |
| 37660 | 4212 | "word_rotl n (x AND y) = word_rotl n x AND word_rotl n y" | 
| 4213 | "word_rotr n (x AND y) = word_rotr n x AND word_rotr n y" | |
| 4214 | "word_rotl n (x OR y) = word_rotl n x OR word_rotl n y" | |
| 4215 | "word_rotr n (x OR y) = word_rotr n x OR word_rotr n y" | |
| 4216 | "word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y" | |
| 65268 | 4217 | "word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4218 | by (rule bit_word_eqI, auto simp add: bit_word_rotl_iff bit_word_rotr_iff bit_and_iff bit_or_iff bit_xor_iff bit_not_iff algebra_simps not_le)+ | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4219 | |
| 37660 | 4220 | end | 
| 4221 | ||
| 74097 | 4222 | end | 
| 4223 | ||
| 37660 | 4224 | lemmas word_rot_logs = word_rotate.word_rot_logs | 
| 4225 | ||
| 65336 | 4226 | lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 \<and> word_rotl i 0 = 0" | 
| 72088 
a36db1c8238e
separation of reversed bit lists from other material
 haftmann parents: 
72083diff
changeset | 4227 | by transfer simp_all | 
| 37660 | 4228 | |
| 4229 | lemma word_roti_0' [simp] : "word_roti n 0 = 0" | |
| 72079 | 4230 | by transfer simp | 
| 37660 | 4231 | |
| 72079 | 4232 | declare word_roti_eq_word_rotr_word_rotl [simp] | 
| 37660 | 4233 | |
| 4234 | ||
| 61799 | 4235 | subsection \<open>Maximum machine word\<close> | 
| 37660 | 4236 | |
| 74097 | 4237 | context | 
| 4238 | includes bit_operations_syntax | |
| 4239 | begin | |
| 4240 | ||
| 37660 | 4241 | lemma word_int_cases: | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4242 | fixes x :: "'a::len word" | 
| 70185 | 4243 |   obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)"
 | 
| 72292 | 4244 | by (rule that [of \<open>uint x\<close>]) simp_all | 
| 37660 | 4245 | |
| 4246 | lemma word_nat_cases [cases type: word]: | |
| 65336 | 4247 | fixes x :: "'a::len word" | 
| 70185 | 4248 |   obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
 | 
| 72292 | 4249 | by (rule that [of \<open>unat x\<close>]) simp_all | 
| 37660 | 4250 | |
| 73788 | 4251 | lemma max_word_max [intro!]: | 
| 4252 | \<open>n \<le> - 1\<close> for n :: \<open>'a::len word\<close> | |
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 4253 | by (fact word_order.extremum) | 
| 65268 | 4254 | |
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4255 | lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
 | 
| 72292 | 4256 | by simp | 
| 37660 | 4257 | |
| 70185 | 4258 | lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0"
 | 
| 71957 
3e162c63371a
build bit operations on word on library theory on bit operations
 haftmann parents: 
71955diff
changeset | 4259 | by (fact word_exp_length_eq_0) | 
| 37660 | 4260 | |
| 73788 | 4261 | lemma max_word_wrap: | 
| 4262 | \<open>x + 1 = 0 \<Longrightarrow> x = - 1\<close> for x :: \<open>'a::len word\<close> | |
| 71946 | 4263 | by (simp add: eq_neg_iff_add_eq_0) | 
| 4264 | ||
| 73788 | 4265 | lemma word_and_max: | 
| 4266 | \<open>x AND - 1 = x\<close> for x :: \<open>'a::len word\<close> | |
| 71946 | 4267 | by (fact word_log_esimps) | 
| 4268 | ||
| 73788 | 4269 | lemma word_or_max: | 
| 4270 | \<open>x OR - 1 = - 1\<close> for x :: \<open>'a::len word\<close> | |
| 71946 | 4271 | by (fact word_log_esimps) | 
| 37660 | 4272 | |
| 65336 | 4273 | lemma word_ao_dist2: "x AND (y OR z) = x AND y OR x AND z" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4274 | for x y z :: "'a::len word" | 
| 72508 | 4275 | by (fact bit.conj_disj_distrib) | 
| 37660 | 4276 | |
| 65336 | 4277 | lemma word_oa_dist2: "x OR y AND z = (x OR y) AND (x OR z)" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4278 | for x y z :: "'a::len word" | 
| 72508 | 4279 | by (fact bit.disj_conj_distrib) | 
| 37660 | 4280 | |
| 65336 | 4281 | lemma word_and_not [simp]: "x AND NOT x = 0" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4282 | for x :: "'a::len word" | 
| 72508 | 4283 | by (fact bit.conj_cancel_right) | 
| 37660 | 4284 | |
| 73788 | 4285 | lemma word_or_not [simp]: | 
| 4286 | \<open>x OR NOT x = - 1\<close> for x :: \<open>'a::len word\<close> | |
| 72508 | 4287 | by (fact bit.disj_cancel_right) | 
| 37660 | 4288 | |
| 65336 | 4289 | lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y" | 
| 71954 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
 haftmann parents: 
71953diff
changeset | 4290 | for x y :: "'a::len word" | 
| 72508 | 4291 | by (fact bit.xor_def) | 
| 37660 | 4292 | |
| 65336 | 4293 | lemma uint_lt_0 [simp]: "uint x < 0 = False" | 
| 37660 | 4294 | by (simp add: linorder_not_less) | 
| 4295 | ||
| 65336 | 4296 | lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0" | 
| 4297 | for x :: "'a::len word" | |
| 37660 | 4298 | by (simp add: word_less_nat_alt unat_0_iff) | 
| 4299 | ||
| 4300 | lemma uint_plus_if_size: | |
| 65268 | 4301 | "uint (x + y) = | 
| 65336 | 4302 | (if uint x + uint y < 2^size x | 
| 4303 | then uint x + uint y | |
| 4304 | else uint x + uint y - 2^size x)" | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4305 | by (simp add: take_bit_eq_mod word_size uint_word_of_int_eq uint_plus_if') | 
| 37660 | 4306 | |
| 4307 | lemma unat_plus_if_size: | |
| 65363 | 4308 | "unat (x + y) = | 
| 65336 | 4309 | (if unat x + unat y < 2^size x | 
| 4310 | then unat x + unat y | |
| 4311 | else unat x + unat y - 2^size x)" | |
| 65363 | 4312 | for x y :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4313 | by (simp add: size_word.rep_eq unat_arith_simps) | 
| 37660 | 4314 | |
| 65336 | 4315 | lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w" | 
| 4316 | for w :: "'a::len word" | |
| 72262 | 4317 | by (fact word_coorder.not_eq_extremum) | 
| 65336 | 4318 | |
| 4319 | lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c" | |
| 4320 | for c :: "'a::len word" | |
| 55818 | 4321 | by (fact unat_div) | 
| 37660 | 4322 | |
| 4323 | lemma uint_sub_if_size: | |
| 65268 | 4324 | "uint (x - y) = | 
| 65336 | 4325 | (if uint y \<le> uint x | 
| 4326 | then uint x - uint y | |
| 4327 | else uint x - uint y + 2^size x)" | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4328 | by (simp add: size_word.rep_eq uint_sub_if') | 
| 65336 | 4329 | |
| 72130 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 4330 | lemma unat_sub: | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 4331 | \<open>unat (a - b) = unat a - unat b\<close> | 
| 
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
 haftmann parents: 
72128diff
changeset | 4332 | if \<open>b \<le> a\<close> | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4333 | by (meson that unat_sub_if_size word_le_nat_alt) | 
| 37660 | 4334 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 4335 | lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 4336 | lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w | 
| 65268 | 4337 | |
| 70185 | 4338 | lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)"
 | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4339 | by simp | 
| 72292 | 4340 | |
| 4341 | lemma word_of_int_inj: | |
| 4342 | \<open>(word_of_int x :: 'a::len word) = word_of_int y \<longleftrightarrow> x = y\<close> | |
| 4343 |   if \<open>0 \<le> x \<and> x < 2 ^ LENGTH('a)\<close> \<open>0 \<le> y \<and> y < 2 ^ LENGTH('a)\<close>
 | |
| 4344 | using that by (transfer fixing: x y) (simp add: take_bit_int_eq_self) | |
| 37660 | 4345 | |
| 65336 | 4346 | lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y" | 
| 4347 | for x y :: "'z::len word" | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46962diff
changeset | 4348 | by (auto simp add: order_class.le_less) | 
| 37660 | 4349 | |
| 4350 | lemma mod_plus_cong: | |
| 65336 | 4351 | fixes b b' :: int | 
| 4352 | assumes 1: "b = b'" | |
| 4353 | and 2: "x mod b' = x' mod b'" | |
| 4354 | and 3: "y mod b' = y' mod b'" | |
| 4355 | and 4: "x' + y' = z'" | |
| 37660 | 4356 | shows "(x + y) mod b = z' mod b'" | 
| 4357 | proof - | |
| 4358 | from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'" | |
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
64243diff
changeset | 4359 | by (simp add: mod_add_eq) | 
| 37660 | 4360 | also have "\<dots> = (x' + y') mod b'" | 
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
64243diff
changeset | 4361 | by (simp add: mod_add_eq) | 
| 65336 | 4362 | finally show ?thesis | 
| 4363 | by (simp add: 4) | |
| 37660 | 4364 | qed | 
| 4365 | ||
| 4366 | lemma mod_minus_cong: | |
| 65336 | 4367 | fixes b b' :: int | 
| 4368 | assumes "b = b'" | |
| 4369 | and "x mod b' = x' mod b'" | |
| 4370 | and "y mod b' = y' mod b'" | |
| 4371 | and "x' - y' = z'" | |
| 37660 | 4372 | shows "(x - y) mod b = z' mod b'" | 
| 65336 | 4373 | using assms [symmetric] by (auto intro: mod_diff_cong) | 
| 4374 | ||
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4375 | lemma word_induct_less [case_names zero less]: | 
| 72262 | 4376 | \<open>P m\<close> if zero: \<open>P 0\<close> and less: \<open>\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close> | 
| 4377 | for m :: \<open>'a::len word\<close> | |
| 4378 | proof - | |
| 4379 | define q where \<open>q = unat m\<close> | |
| 4380 | with less have \<open>\<And>n. n < word_of_nat q \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close> | |
| 4381 | by simp | |
| 4382 | then have \<open>P (word_of_nat q :: 'a word)\<close> | |
| 4383 | proof (induction q) | |
| 4384 | case 0 | |
| 4385 | show ?case | |
| 4386 | by (simp add: zero) | |
| 4387 | next | |
| 4388 | case (Suc q) | |
| 4389 | show ?case | |
| 4390 | proof (cases \<open>1 + word_of_nat q = (0 :: 'a word)\<close>) | |
| 4391 | case True | |
| 4392 | then show ?thesis | |
| 4393 | by (simp add: zero) | |
| 4394 | next | |
| 4395 | case False | |
| 4396 | then have *: \<open>word_of_nat q < (word_of_nat (Suc q) :: 'a word)\<close> | |
| 4397 | by (simp add: unatSuc word_less_nat_alt) | |
| 4398 | then have **: \<open>n < (1 + word_of_nat q :: 'a word) \<longleftrightarrow> n \<le> (word_of_nat q :: 'a word)\<close> for n | |
| 4399 | by (metis (no_types, lifting) add.commute inc_le le_less_trans not_less of_nat_Suc) | |
| 4400 | have \<open>P (word_of_nat q)\<close> | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4401 | by (simp add: "**" Suc.IH Suc.prems) | 
| 72262 | 4402 | with * have \<open>P (1 + word_of_nat q)\<close> | 
| 4403 | by (rule Suc.prems) | |
| 4404 | then show ?thesis | |
| 4405 | by simp | |
| 4406 | qed | |
| 4407 | qed | |
| 4408 | with \<open>q = unat m\<close> show ?thesis | |
| 4409 | by simp | |
| 4410 | qed | |
| 65268 | 4411 | |
| 65363 | 4412 | lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m" | 
| 65336 | 4413 | for P :: "'a::len word \<Rightarrow> bool" | 
| 72262 | 4414 | by (rule word_induct_less) | 
| 65336 | 4415 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4416 | lemma word_induct2 [case_names zero suc, induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n" | 
| 65336 | 4417 | for P :: "'b::len word \<Rightarrow> bool" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4418 | by (induction rule: word_induct_less; force) | 
| 37660 | 4419 | |
| 55816 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
 haftmann parents: 
55415diff
changeset | 4420 | |
| 61799 | 4421 | subsection \<open>Recursion combinator for words\<close> | 
| 46010 | 4422 | |
| 54848 | 4423 | definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
 | 
| 65336 | 4424 | where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)" | 
| 37660 | 4425 | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4426 | lemma word_rec_0 [simp]: "word_rec z s 0 = z" | 
| 37660 | 4427 | by (simp add: word_rec_def) | 
| 4428 | ||
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4429 | lemma word_rec_Suc [simp]: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)" | 
| 65363 | 4430 | for n :: "'a::len word" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4431 | by (simp add: unatSuc word_rec_def) | 
| 37660 | 4432 | |
| 65363 | 4433 | lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4434 | by (metis add.commute diff_add_cancel word_rec_Suc) | 
| 37660 | 4435 | |
| 65336 | 4436 | lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n" | 
| 74101 | 4437 | by (induct n) simp_all | 
| 37660 | 4438 | |
| 67399 | 4439 | lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n" | 
| 74101 | 4440 | by (induct n) simp_all | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4441 | |
| 65268 | 4442 | lemma word_rec_twice: | 
| 67399 | 4443 | "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m" | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4444 | proof (induction n arbitrary: z f) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4445 | case zero | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4446 | then show ?case | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4447 | by (metis diff_0_right word_le_0_iff word_rec_0) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4448 | next | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4449 | case (suc n z f) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4450 | show ?case | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4451 | proof (cases "1 + (n - m) = 0") | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4452 | case True | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4453 | then show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4454 | by (simp add: add_diff_eq) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4455 | next | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4456 | case False | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4457 | then have eq: "1 + n - m = 1 + (n - m)" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4458 | by simp | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4459 | with False have "m \<le> n" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4460 | by (metis "suc.prems" add.commute dual_order.antisym eq_iff_diff_eq_0 inc_le leI) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4461 | with False "suc.hyps" show ?thesis | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4462 | using suc.IH [of "f 0 z" "f \<circ> (+) 1"] | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4463 | by (simp add: word_rec_in2 eq add.assoc o_def) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4464 | qed | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4465 | qed | 
| 37660 | 4466 | |
| 4467 | lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z" | |
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4468 | by (induct n) auto | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4469 | |
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4470 | lemma word_rec_id_eq: "(\<And>m. m < n \<Longrightarrow> f m = id) \<Longrightarrow> word_rec z f n = z" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4471 | by (induction n) (auto simp add: unatSuc unat_arith_simps(2)) | 
| 37660 | 4472 | |
| 65268 | 4473 | lemma word_rec_max: | 
| 72735 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4474 | assumes "\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4475 | shows "word_rec z f (- 1) = word_rec z f n" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4476 | proof - | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4477 | have \<section>: "\<And>m. \<lbrakk>m < - 1 - n\<rbrakk> \<Longrightarrow> (f \<circ> (+) n) m = id" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4478 | using assms | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4479 | by (metis (mono_tags, lifting) add.commute add_diff_cancel_left' comp_apply less_le olen_add_eqv plus_minus_no_overflow word_n1_ge) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4480 | have "word_rec z f (- 1) = word_rec (word_rec z f (- 1 - (- 1 - n))) (f \<circ> (+) (- 1 - (- 1 - n))) (- 1 - n)" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4481 | by (meson word_n1_ge word_rec_twice) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4482 | also have "... = word_rec z f n" | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4483 | by (metis (no_types, lifting) \<section> diff_add_cancel minus_diff_eq uminus_add_conv_diff word_rec_id_eq) | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4484 | finally show ?thesis . | 
| 
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
 paulson <lp15@cam.ac.uk> parents: 
72611diff
changeset | 4485 | qed | 
| 65336 | 4486 | |
| 74097 | 4487 | end | 
| 4488 | ||
| 72512 | 4489 | |
| 74592 | 4490 | subsection \<open>Tool support\<close> | 
| 72489 | 4491 | |
| 69605 | 4492 | ML_file \<open>Tools/smt_word.ML\<close> | 
| 36899 
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
 boehmes parents: 
35049diff
changeset | 4493 | |
| 41060 
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
 boehmes parents: 
40827diff
changeset | 4494 | end |