| author | wenzelm | 
| Sat, 29 Mar 2008 19:13:58 +0100 | |
| changeset 26479 | 3a2efce3e992 | 
| parent 25919 | 8b1c0d434824 | 
| child 26514 | eff55c0a6d34 | 
| permissions | -rw-r--r-- | 
| 24333 | 1 | (* | 
| 2 | ID: $Id$ | |
| 3 | Author: Jeremy Dawson and Gerwin Klein, NICTA | |
| 4 | ||
| 5 | Basic definition of word type and basic theorems following from | |
| 6 | the definition of the word type | |
| 7 | *) | |
| 8 | ||
| 24350 | 9 | header {* Definition of Word Type *}
 | 
| 24333 | 10 | |
| 24465 | 11 | theory WordDefinition imports Size BinBoolList TdThs begin | 
| 24333 | 12 | |
| 13 | typedef (open word) 'a word | |
| 24465 | 14 |   = "{(0::int) ..< 2^len_of TYPE('a::len0)}" by auto
 | 
| 24333 | 15 | |
| 25762 | 16 | instantiation word :: (len0) size | 
| 17 | begin | |
| 18 | ||
| 19 | definition | |
| 20 |   word_size: "size (w :: 'a word) = len_of TYPE('a)"
 | |
| 21 | ||
| 22 | instance .. | |
| 23 | ||
| 24 | end | |
| 25 | ||
| 26 | definition | |
| 27 |   -- {* representation of words using unsigned or signed bins, 
 | |
| 28 | only difference in these is the type class *} | |
| 29 | word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" | |
| 30 | where | |
| 31 |   "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 
 | |
| 24333 | 32 | |
| 33 | ||
| 24350 | 34 | subsection "Type conversions and casting" | 
| 24333 | 35 | |
| 36 | constdefs | |
| 37 |   -- {* uint and sint cast a word to an integer,
 | |
| 38 | uint treats the word as unsigned, | |
| 39 | sint treats the most-significant-bit as a sign bit *} | |
| 24465 | 40 | uint :: "'a :: len0 word => int" | 
| 24333 | 41 | "uint w == Rep_word w" | 
| 24465 | 42 | sint :: "'a :: len word => int" | 
| 43 |   sint_uint: "sint w == sbintrunc (len_of TYPE ('a) - 1) (uint w)"
 | |
| 44 | unat :: "'a :: len0 word => nat" | |
| 24333 | 45 | "unat w == nat (uint w)" | 
| 46 | ||
| 47 | -- "the sets of integers representing the words" | |
| 48 | uints :: "nat => int set" | |
| 49 | "uints n == range (bintrunc n)" | |
| 50 | sints :: "nat => int set" | |
| 51 | "sints n == range (sbintrunc (n - 1))" | |
| 52 | unats :: "nat => nat set" | |
| 53 |   "unats n == {i. i < 2 ^ n}"
 | |
| 54 | norm_sint :: "nat => int => int" | |
| 55 | "norm_sint n w == (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)" | |
| 56 | ||
| 24465 | 57 | -- "cast a word to a different length" | 
| 58 | scast :: "'a :: len word => 'b :: len word" | |
| 59 | "scast w == word_of_int (sint w)" | |
| 60 | ucast :: "'a :: len0 word => 'b :: len0 word" | |
| 61 | "ucast w == word_of_int (uint w)" | |
| 62 | ||
| 63 | -- "whether a cast (or other) function is to a longer or shorter length" | |
| 64 |   source_size :: "('a :: len0 word => 'b) => nat"
 | |
| 65 | "source_size c == let arb = arbitrary ; x = c arb in size arb" | |
| 66 |   target_size :: "('a => 'b :: len0 word) => nat"
 | |
| 67 | "target_size c == size (c arbitrary)" | |
| 68 |   is_up :: "('a :: len0 word => 'b :: len0 word) => bool"
 | |
| 69 | "is_up c == source_size c <= target_size c" | |
| 70 |   is_down :: "('a :: len0 word => 'b :: len0 word) => bool"
 | |
| 71 | "is_down c == target_size c <= source_size c" | |
| 72 | ||
| 73 | constdefs | |
| 74 | of_bl :: "bool list => 'a :: len0 word" | |
| 75 | "of_bl bl == word_of_int (bl_to_bin bl)" | |
| 76 | to_bl :: "'a :: len0 word => bool list" | |
| 77 | "to_bl w == | |
| 78 |   bin_to_bl (len_of TYPE ('a)) (uint w)"
 | |
| 79 | ||
| 80 | word_reverse :: "'a :: len0 word => 'a word" | |
| 81 | "word_reverse w == of_bl (rev (to_bl w))" | |
| 82 | ||
| 24333 | 83 | constdefs | 
| 24465 | 84 |   word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b"
 | 
| 24333 | 85 | "word_int_case f w == f (uint w)" | 
| 86 | ||
| 87 | syntax | |
| 88 | of_int :: "int => 'a" | |
| 89 | translations | |
| 90 | "case x of of_int y => b" == "word_int_case (%y. b) x" | |
| 91 | ||
| 92 | ||
| 24350 | 93 | subsection "Arithmetic operations" | 
| 24333 | 94 | |
| 25762 | 95 | instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, power, ord, bit}"
 | 
| 96 | begin | |
| 97 | ||
| 98 | definition | |
| 99 | word_0_wi: "0 = word_of_int 0" | |
| 100 | ||
| 101 | definition | |
| 102 | word_1_wi: "1 = word_of_int 1" | |
| 103 | ||
| 104 | definition | |
| 105 | word_add_def: "a + b = word_of_int (uint a + uint b)" | |
| 106 | ||
| 107 | definition | |
| 108 | word_sub_wi: "a - b = word_of_int (uint a - uint b)" | |
| 109 | ||
| 110 | definition | |
| 111 | word_minus_def: "- a = word_of_int (- uint a)" | |
| 112 | ||
| 113 | definition | |
| 114 | word_mult_def: "a * b = word_of_int (uint a * uint b)" | |
| 115 | ||
| 116 | definition | |
| 117 | word_div_def: "a div b = word_of_int (uint a div uint b)" | |
| 118 | ||
| 119 | definition | |
| 120 | word_mod_def: "a mod b = word_of_int (uint a mod uint b)" | |
| 121 | ||
| 122 | primrec power_word where | |
| 123 | "(a\<Colon>'a word) ^ 0 = 1" | |
| 124 | | "(a\<Colon>'a word) ^ Suc n = a * a ^ n" | |
| 125 | ||
| 126 | definition | |
| 127 | word_number_of_def: "number_of w = word_of_int w" | |
| 128 | ||
| 129 | definition | |
| 130 | word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" | |
| 24415 | 131 | |
| 25762 | 132 | definition | 
| 133 | word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)" | |
| 134 | ||
| 135 | definition | |
| 136 | word_and_def: | |
| 137 | "(a::'a word) AND b = word_of_int (uint a AND uint b)" | |
| 138 | ||
| 139 | definition | |
| 140 | word_or_def: | |
| 141 | "(a::'a word) OR b = word_of_int (uint a OR uint b)" | |
| 142 | ||
| 143 | definition | |
| 144 | word_xor_def: | |
| 145 | "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" | |
| 146 | ||
| 147 | definition | |
| 148 | word_not_def: | |
| 149 | "NOT (a::'a word) = word_of_int (NOT (uint a))" | |
| 150 | ||
| 151 | instance .. | |
| 152 | ||
| 153 | end | |
| 154 | ||
| 155 | abbreviation | |
| 156 | word_power :: "'a\<Colon>len0 word \<Rightarrow> nat \<Rightarrow> 'a word" | |
| 157 | where | |
| 158 | "word_power == op ^" | |
| 159 | ||
| 160 | definition | |
| 161 | word_succ :: "'a :: len0 word => 'a word" | |
| 162 | where | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 163 | "word_succ a = word_of_int (Int.succ (uint a))" | 
| 25762 | 164 | |
| 165 | definition | |
| 166 | word_pred :: "'a :: len0 word => 'a word" | |
| 167 | where | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 168 | "word_pred a = word_of_int (Int.pred (uint a))" | 
| 24333 | 169 | |
| 170 | constdefs | |
| 24465 | 171 | udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) | 
| 172 | "a udvd b == EX n>=0. uint b = n * uint a" | |
| 173 | ||
| 174 |   word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50)
 | |
| 175 | "a <=s b == sint a <= sint b" | |
| 176 | ||
| 177 |   word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50)
 | |
| 178 | "(x <s y) == (x <=s y & x ~= y)" | |
| 179 | ||
| 24333 | 180 | |
| 24350 | 181 | subsection "Bit-wise operations" | 
| 24333 | 182 | |
| 183 | defs (overloaded) | |
| 184 | word_test_bit_def: | |
| 24465 | 185 | "test_bit (a::'a::len0 word) == bin_nth (uint a)" | 
| 24333 | 186 | |
| 187 | word_set_bit_def: | |
| 24465 | 188 | "set_bit (a::'a::len0 word) n x == | 
| 24333 | 189 | word_of_int (bin_sc n (If x bit.B1 bit.B0) (uint a))" | 
| 190 | ||
| 24465 | 191 | word_set_bits_def: | 
| 192 |   "(BITS n. f n)::'a::len0 word == of_bl (bl_of_nth (len_of TYPE ('a)) f)"
 | |
| 193 | ||
| 24333 | 194 | word_lsb_def: | 
| 24465 | 195 | "lsb (a::'a::len0 word) == bin_last (uint a) = bit.B1" | 
| 24333 | 196 | |
| 197 | word_msb_def: | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 198 | "msb (a::'a::len word) == bin_sign (sint a) = Int.Min" | 
| 24333 | 199 | |
| 200 | ||
| 201 | constdefs | |
| 24465 | 202 | setBit :: "'a :: len0 word => nat => 'a word" | 
| 24333 | 203 | "setBit w n == set_bit w n True" | 
| 204 | ||
| 24465 | 205 | clearBit :: "'a :: len0 word => nat => 'a word" | 
| 24333 | 206 | "clearBit w n == set_bit w n False" | 
| 207 | ||
| 208 | ||
| 24465 | 209 | subsection "Shift operations" | 
| 210 | ||
| 211 | constdefs | |
| 212 | shiftl1 :: "'a :: len0 word => 'a word" | |
| 213 | "shiftl1 w == word_of_int (uint w BIT bit.B0)" | |
| 214 | ||
| 215 | -- "shift right as unsigned or as signed, ie logical or arithmetic" | |
| 216 | shiftr1 :: "'a :: len0 word => 'a word" | |
| 217 | "shiftr1 w == word_of_int (bin_rest (uint w))" | |
| 218 | ||
| 219 | sshiftr1 :: "'a :: len word => 'a word" | |
| 220 | "sshiftr1 w == word_of_int (bin_rest (sint w))" | |
| 221 | ||
| 222 | bshiftr1 :: "bool => 'a :: len word => 'a word" | |
| 223 | "bshiftr1 b w == of_bl (b # butlast (to_bl w))" | |
| 224 | ||
| 225 | sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) | |
| 226 | "w >>> n == (sshiftr1 ^ n) w" | |
| 227 | ||
| 228 | mask :: "nat => 'a::len word" | |
| 229 | "mask n == (1 << n) - 1" | |
| 230 | ||
| 231 | revcast :: "'a :: len0 word => 'b :: len0 word" | |
| 232 |   "revcast w ==  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
 | |
| 233 | ||
| 234 | slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" | |
| 235 | "slice1 n w == of_bl (takefill False n (to_bl w))" | |
| 236 | ||
| 237 | slice :: "nat => 'a :: len0 word => 'b :: len0 word" | |
| 238 | "slice n w == slice1 (size w - n) w" | |
| 239 | ||
| 240 | ||
| 241 | defs (overloaded) | |
| 242 | shiftl_def: "(w::'a::len0 word) << n == (shiftl1 ^ n) w" | |
| 243 | shiftr_def: "(w::'a::len0 word) >> n == (shiftr1 ^ n) w" | |
| 244 | ||
| 245 | ||
| 246 | subsection "Rotation" | |
| 247 | ||
| 248 | constdefs | |
| 249 | rotater1 :: "'a list => 'a list" | |
| 250 | "rotater1 ys == | |
| 251 | case ys of [] => [] | x # xs => last ys # butlast ys" | |
| 252 | ||
| 253 | rotater :: "nat => 'a list => 'a list" | |
| 254 | "rotater n == rotater1 ^ n" | |
| 255 | ||
| 256 | word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" | |
| 257 | "word_rotr n w == of_bl (rotater n (to_bl w))" | |
| 258 | ||
| 259 | word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" | |
| 260 | "word_rotl n w == of_bl (rotate n (to_bl w))" | |
| 261 | ||
| 262 | word_roti :: "int => 'a :: len0 word => 'a :: len0 word" | |
| 263 | "word_roti i w == if i >= 0 then word_rotr (nat i) w | |
| 264 | else word_rotl (nat (- i)) w" | |
| 265 | ||
| 266 | ||
| 267 | subsection "Split and cat operations" | |
| 268 | ||
| 269 | constdefs | |
| 270 | word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" | |
| 271 |   "word_cat a b == word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
 | |
| 272 | ||
| 273 |   word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)"
 | |
| 274 | "word_split a == | |
| 275 |    case bin_split (len_of TYPE ('c)) (uint a) of 
 | |
| 276 | (u, v) => (word_of_int u, word_of_int v)" | |
| 277 | ||
| 278 | word_rcat :: "'a :: len0 word list => 'b :: len0 word" | |
| 279 | "word_rcat ws == | |
| 280 |   word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
 | |
| 281 | ||
| 282 | word_rsplit :: "'a :: len0 word => 'b :: len word list" | |
| 283 | "word_rsplit w == | |
| 284 |   map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
 | |
| 285 | ||
| 24333 | 286 | constdefs | 
| 287 | -- "Largest representable machine integer." | |
| 24465 | 288 | max_word :: "'a::len word" | 
| 289 |   "max_word \<equiv> word_of_int (2^len_of TYPE('a) - 1)"
 | |
| 24333 | 290 | |
| 291 | consts | |
| 24465 | 292 | of_bool :: "bool \<Rightarrow> 'a::len word" | 
| 24333 | 293 | primrec | 
| 294 | "of_bool False = 0" | |
| 295 | "of_bool True = 1" | |
| 296 | ||
| 297 | ||
| 298 | ||
| 24465 | 299 | lemmas of_nth_def = word_set_bits_def | 
| 300 | ||
| 24333 | 301 | lemmas word_size_gt_0 [iff] = | 
| 25762 | 302 | xtr1 [OF word_size len_gt_0, standard] | 
| 24465 | 303 | lemmas lens_gt_0 = word_size_gt_0 len_gt_0 | 
| 24333 | 304 | lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard] | 
| 305 | ||
| 306 | lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
 | |
| 307 | by (simp add: uints_def range_bintrunc) | |
| 308 | ||
| 309 | lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
 | |
| 310 | by (simp add: sints_def range_sbintrunc) | |
| 311 | ||
| 312 | lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded | |
| 313 | atLeast_def lessThan_def Collect_conj_eq [symmetric]] | |
| 314 | ||
| 315 | lemma mod_in_reps: "m > 0 ==> y mod m : {0::int ..< m}"
 | |
| 316 | unfolding atLeastLessThan_alt by auto | |
| 317 | ||
| 318 | lemma | |
| 319 | Rep_word_0:"0 <= Rep_word x" and | |
| 24465 | 320 |   Rep_word_lt: "Rep_word (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
 | 
| 24333 | 321 | by (auto simp: Rep_word [simplified]) | 
| 322 | ||
| 323 | lemma Rep_word_mod_same: | |
| 24465 | 324 |   "Rep_word x mod 2 ^ len_of TYPE('a) = Rep_word (x::'a::len0 word)"
 | 
| 24333 | 325 | by (simp add: int_mod_eq Rep_word_lt Rep_word_0) | 
| 326 | ||
| 327 | lemma td_ext_uint: | |
| 24465 | 328 |   "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
 | 
| 329 |     (%w::int. w mod 2 ^ len_of TYPE('a))"
 | |
| 24333 | 330 | apply (unfold td_ext_def') | 
| 331 | apply (simp add: uints_num uint_def word_of_int_def bintrunc_mod2p) | |
| 332 | apply (simp add: Rep_word_mod_same Rep_word_0 Rep_word_lt | |
| 333 | word.Rep_word_inverse word.Abs_word_inverse int_mod_lem) | |
| 334 | done | |
| 335 | ||
| 336 | lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard] | |
| 337 | ||
| 338 | interpretation word_uint: | |
| 24465 | 339 | td_ext ["uint::'a::len0 word \<Rightarrow> int" | 
| 24333 | 340 | word_of_int | 
| 24465 | 341 |           "uints (len_of TYPE('a::len0))"
 | 
| 342 |           "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"]
 | |
| 24333 | 343 | by (rule td_ext_uint) | 
| 344 | ||
| 345 | lemmas td_uint = word_uint.td_thm | |
| 346 | ||
| 347 | lemmas td_ext_ubin = td_ext_uint | |
| 24465 | 348 | [simplified len_gt_0 no_bintr_alt1 [symmetric]] | 
| 24333 | 349 | |
| 350 | interpretation word_ubin: | |
| 24465 | 351 | td_ext ["uint::'a::len0 word \<Rightarrow> int" | 
| 24333 | 352 | word_of_int | 
| 24465 | 353 |           "uints (len_of TYPE('a::len0))"
 | 
| 354 |           "bintrunc (len_of TYPE('a::len0))"]
 | |
| 24333 | 355 | by (rule td_ext_ubin) | 
| 356 | ||
| 357 | lemma sint_sbintrunc': | |
| 358 | "sint (word_of_int bin :: 'a word) = | |
| 24465 | 359 |     (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
 | 
| 24333 | 360 | unfolding sint_uint | 
| 361 | by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) | |
| 362 | ||
| 363 | lemma uint_sint: | |
| 24465 | 364 |   "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
 | 
| 24333 | 365 | unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) | 
| 366 | ||
| 367 | lemma bintr_uint': | |
| 368 | "n >= size w ==> bintrunc n (uint w) = uint w" | |
| 369 | apply (unfold word_size) | |
| 370 | apply (subst word_ubin.norm_Rep [symmetric]) | |
| 371 | apply (simp only: bintrunc_bintrunc_min word_size min_def) | |
| 372 | apply simp | |
| 373 | done | |
| 374 | ||
| 375 | lemma wi_bintr': | |
| 376 | "wb = word_of_int bin ==> n >= size wb ==> | |
| 377 | word_of_int (bintrunc n bin) = wb" | |
| 378 | unfolding word_size | |
| 379 | by (clarsimp simp add : word_ubin.norm_eq_iff [symmetric] min_def) | |
| 380 | ||
| 381 | lemmas bintr_uint = bintr_uint' [unfolded word_size] | |
| 382 | lemmas wi_bintr = wi_bintr' [unfolded word_size] | |
| 383 | ||
| 384 | lemma td_ext_sbin: | |
| 24465 | 385 |   "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 
 | 
| 386 |     (sbintrunc (len_of TYPE('a) - 1))"
 | |
| 24333 | 387 | apply (unfold td_ext_def' sint_uint) | 
| 388 | apply (simp add : word_ubin.eq_norm) | |
| 24465 | 389 |   apply (cases "len_of TYPE('a)")
 | 
| 24333 | 390 | apply (auto simp add : sints_def) | 
| 391 | apply (rule sym [THEN trans]) | |
| 392 | apply (rule word_ubin.Abs_norm) | |
| 393 | apply (simp only: bintrunc_sbintrunc) | |
| 394 | apply (drule sym) | |
| 395 | apply simp | |
| 396 | done | |
| 397 | ||
| 398 | lemmas td_ext_sint = td_ext_sbin | |
| 24465 | 399 | [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] | 
| 24333 | 400 | |
| 401 | (* We do sint before sbin, before sint is the user version | |
| 402 | and interpretations do not produce thm duplicates. I.e. | |
| 403 | we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD, | |
| 404 | because the latter is the same thm as the former *) | |
| 405 | interpretation word_sint: | |
| 24465 | 406 | td_ext ["sint ::'a::len word => int" | 
| 24333 | 407 | word_of_int | 
| 24465 | 408 |           "sints (len_of TYPE('a::len))"
 | 
| 409 |           "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
 | |
| 410 |                2 ^ (len_of TYPE('a::len) - 1)"]
 | |
| 24333 | 411 | by (rule td_ext_sint) | 
| 412 | ||
| 413 | interpretation word_sbin: | |
| 24465 | 414 | td_ext ["sint ::'a::len word => int" | 
| 24333 | 415 | word_of_int | 
| 24465 | 416 |           "sints (len_of TYPE('a::len))"
 | 
| 417 |           "sbintrunc (len_of TYPE('a::len) - 1)"]
 | |
| 24333 | 418 | by (rule td_ext_sbin) | 
| 419 | ||
| 420 | lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard] | |
| 421 | ||
| 422 | lemmas td_sint = word_sint.td | |
| 423 | ||
| 424 | lemma word_number_of_alt: "number_of b == word_of_int (number_of b)" | |
| 425 | unfolding word_number_of_def by (simp add: number_of_eq) | |
| 426 | ||
| 427 | lemma word_no_wi: "number_of = word_of_int" | |
| 428 | by (auto simp: word_number_of_def intro: ext) | |
| 429 | ||
| 24465 | 430 | lemma to_bl_def': | 
| 431 | "(to_bl :: 'a :: len0 word => bool list) = | |
| 432 |     bin_to_bl (len_of TYPE('a)) o uint"
 | |
| 433 | by (auto simp: to_bl_def intro: ext) | |
| 434 | ||
| 25349 
0d46bea01741
eliminated illegal schematic variables in where/of;
 wenzelm parents: 
25149diff
changeset | 435 | lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w", standard] | 
| 24465 | 436 | |
| 24333 | 437 | lemmas uints_mod = uints_def [unfolded no_bintr_alt1] | 
| 438 | ||
| 439 | lemma uint_bintrunc: "uint (number_of bin :: 'a word) = | |
| 24465 | 440 |     number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
 | 
| 24333 | 441 | unfolding word_number_of_def number_of_eq | 
| 442 | by (auto intro: word_ubin.eq_norm) | |
| 443 | ||
| 444 | lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = | |
| 24465 | 445 |     number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
 | 
| 24333 | 446 | unfolding word_number_of_def number_of_eq | 
| 25149 | 447 | by (subst word_sbin.eq_norm) simp | 
| 24333 | 448 | |
| 449 | lemma unat_bintrunc: | |
| 24465 | 450 | "unat (number_of bin :: 'a :: len0 word) = | 
| 451 |     number_of (bintrunc (len_of TYPE('a)) bin)"
 | |
| 24333 | 452 | unfolding unat_def nat_number_of_def | 
| 453 | by (simp only: uint_bintrunc) | |
| 454 | ||
| 455 | (* WARNING - these may not always be helpful *) | |
| 456 | declare | |
| 457 | uint_bintrunc [simp] | |
| 458 | sint_sbintrunc [simp] | |
| 459 | unat_bintrunc [simp] | |
| 460 | ||
| 24465 | 461 | lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w" | 
| 24333 | 462 | apply (unfold word_size) | 
| 463 | apply (rule word_uint.Rep_eqD) | |
| 464 | apply (rule box_equals) | |
| 465 | defer | |
| 466 | apply (rule word_ubin.norm_Rep)+ | |
| 467 | apply simp | |
| 468 | done | |
| 469 | ||
| 470 | lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq] | |
| 471 | lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq] | |
| 472 | lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard] | |
| 473 | lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard] | |
| 474 | lemmas sint_ge = sint_lem [THEN conjunct1, standard] | |
| 475 | lemmas sint_lt = sint_lem [THEN conjunct2, standard] | |
| 476 | ||
| 477 | lemma sign_uint_Pls [simp]: | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 478 | "bin_sign (uint x) = Int.Pls" | 
| 24333 | 479 | by (simp add: sign_Pls_ge_0 number_of_eq) | 
| 480 | ||
| 481 | lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard] | |
| 482 | lemmas uint_m2p_not_non_neg = | |
| 483 | iffD2 [OF linorder_not_le uint_m2p_neg, standard] | |
| 484 | ||
| 485 | lemma lt2p_lem: | |
| 24465 | 486 |   "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n"
 | 
| 24333 | 487 | by (rule xtr8 [OF _ uint_lt2p]) simp | 
| 488 | ||
| 489 | lemmas uint_le_0_iff [simp] = | |
| 490 | uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard] | |
| 491 | ||
| 492 | lemma uint_nat: "uint w == int (unat w)" | |
| 493 | unfolding unat_def by auto | |
| 494 | ||
| 495 | lemma uint_number_of: | |
| 24465 | 496 |   "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
 | 
| 24333 | 497 | unfolding word_number_of_alt | 
| 498 | by (simp only: int_word_uint) | |
| 499 | ||
| 500 | lemma unat_number_of: | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 501 | "bin_sign b = Int.Pls ==> | 
| 24465 | 502 |   unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
 | 
| 24333 | 503 | apply (unfold unat_def) | 
| 504 | apply (clarsimp simp only: uint_number_of) | |
| 505 | apply (rule nat_mod_distrib [THEN trans]) | |
| 506 | apply (erule sign_Pls_ge_0 [THEN iffD1]) | |
| 507 | apply (simp_all add: nat_power_eq) | |
| 508 | done | |
| 509 | ||
| 24465 | 510 | lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + | 
| 511 |     2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
 | |
| 512 |     2 ^ (len_of TYPE('a) - 1)"
 | |
| 24333 | 513 | unfolding word_number_of_alt by (rule int_word_sint) | 
| 514 | ||
| 515 | lemma word_of_int_bin [simp] : | |
| 24465 | 516 | "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" | 
| 24333 | 517 | unfolding word_number_of_alt by auto | 
| 518 | ||
| 519 | lemma word_int_case_wi: | |
| 520 | "word_int_case f (word_of_int i :: 'b word) = | |
| 24465 | 521 |     f (i mod 2 ^ len_of TYPE('b::len0))"
 | 
| 24333 | 522 | unfolding word_int_case_def by (simp add: word_uint.eq_norm) | 
| 523 | ||
| 524 | lemma word_int_split: | |
| 525 | "P (word_int_case f x) = | |
| 24465 | 526 | (ALL i. x = (word_of_int i :: 'b :: len0 word) & | 
| 527 |       0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
 | |
| 24333 | 528 | unfolding word_int_case_def | 
| 529 | by (auto simp: word_uint.eq_norm int_mod_eq') | |
| 530 | ||
| 531 | lemma word_int_split_asm: | |
| 532 | "P (word_int_case f x) = | |
| 24465 | 533 | (~ (EX n. x = (word_of_int n :: 'b::len0 word) & | 
| 534 |       0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
 | |
| 24333 | 535 | unfolding word_int_case_def | 
| 536 | by (auto simp: word_uint.eq_norm int_mod_eq') | |
| 537 | ||
| 538 | lemmas uint_range' = | |
| 539 | word_uint.Rep [unfolded uints_num mem_Collect_eq, standard] | |
| 540 | lemmas sint_range' = word_sint.Rep [unfolded One_nat_def | |
| 541 | sints_num mem_Collect_eq, standard] | |
| 542 | ||
| 543 | lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w" | |
| 544 | unfolding word_size by (rule uint_range') | |
| 545 | ||
| 546 | lemma sint_range_size: | |
| 547 | "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)" | |
| 548 | unfolding word_size by (rule sint_range') | |
| 549 | ||
| 550 | lemmas sint_above_size = sint_range_size | |
| 551 | [THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard] | |
| 552 | ||
| 553 | lemmas sint_below_size = sint_range_size | |
| 554 | [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard] | |
| 555 | ||
| 24465 | 556 | lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)" | 
| 24333 | 557 | unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) | 
| 558 | ||
| 24465 | 559 | lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w" | 
| 24333 | 560 | apply (unfold word_test_bit_def) | 
| 561 | apply (subst word_ubin.norm_Rep [symmetric]) | |
| 562 | apply (simp only: nth_bintr word_size) | |
| 563 | apply fast | |
| 564 | done | |
| 565 | ||
| 566 | lemma word_eqI [rule_format] : | |
| 24465 | 567 | fixes u :: "'a::len0 word" | 
| 24333 | 568 | shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v" | 
| 569 | apply (rule test_bit_eq_iff [THEN iffD1]) | |
| 570 | apply (rule ext) | |
| 571 | apply (erule allE) | |
| 572 | apply (erule impCE) | |
| 573 | prefer 2 | |
| 574 | apply assumption | |
| 575 | apply (auto dest!: test_bit_size simp add: word_size) | |
| 576 | done | |
| 577 | ||
| 578 | lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard] | |
| 579 | ||
| 580 | lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)" | |
| 581 | unfolding word_test_bit_def word_size | |
| 582 | by (simp add: nth_bintr [symmetric]) | |
| 583 | ||
| 584 | lemmas test_bit_bin = test_bit_bin' [unfolded word_size] | |
| 585 | ||
| 586 | lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w" | |
| 587 | apply (unfold word_size) | |
| 588 | apply (rule impI) | |
| 589 | apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) | |
| 590 | apply (subst word_ubin.norm_Rep) | |
| 591 | apply assumption | |
| 592 | done | |
| 593 | ||
| 594 | lemma bin_nth_sint': | |
| 595 | "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)" | |
| 596 | apply (rule impI) | |
| 597 | apply (subst word_sbin.norm_Rep [symmetric]) | |
| 598 | apply (simp add : nth_sbintr word_size) | |
| 599 | apply auto | |
| 600 | done | |
| 601 | ||
| 602 | lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size] | |
| 603 | lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size] | |
| 604 | ||
| 24465 | 605 | (* type definitions theorem for in terms of equivalent bool list *) | 
| 606 | lemma td_bl: | |
| 607 | "type_definition (to_bl :: 'a::len0 word => bool list) | |
| 608 | of_bl | |
| 609 |                    {bl. length bl = len_of TYPE('a)}"
 | |
| 610 | apply (unfold type_definition_def of_bl_def to_bl_def) | |
| 611 | apply (simp add: word_ubin.eq_norm) | |
| 612 | apply safe | |
| 613 | apply (drule sym) | |
| 614 | apply simp | |
| 615 | done | |
| 616 | ||
| 617 | interpretation word_bl: | |
| 618 | type_definition ["to_bl :: 'a::len0 word => bool list" | |
| 619 | of_bl | |
| 620 |                    "{bl. length bl = len_of TYPE('a::len0)}"]
 | |
| 621 | by (rule td_bl) | |
| 622 | ||
| 623 | lemma word_size_bl: "size w == size (to_bl w)" | |
| 624 | unfolding word_size by auto | |
| 625 | ||
| 626 | lemma to_bl_use_of_bl: | |
| 627 | "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))" | |
| 628 | by (fastsimp elim!: word_bl.Abs_inverse [simplified]) | |
| 629 | ||
| 630 | lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)" | |
| 631 | unfolding word_reverse_def by (simp add: word_bl.Abs_inverse) | |
| 632 | ||
| 633 | lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" | |
| 634 | unfolding word_reverse_def by (simp add : word_bl.Abs_inverse) | |
| 635 | ||
| 636 | lemma word_rev_gal: "word_reverse w = u ==> word_reverse u = w" | |
| 637 | by auto | |
| 638 | ||
| 639 | lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard] | |
| 640 | ||
| 641 | lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard] | |
| 642 | lemmas bl_not_Nil [iff] = | |
| 643 | length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard] | |
| 644 | lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0] | |
| 645 | ||
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25762diff
changeset | 646 | lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)" | 
| 24465 | 647 | apply (unfold to_bl_def sint_uint) | 
| 648 | apply (rule trans [OF _ bl_sbin_sign]) | |
| 649 | apply simp | |
| 650 | done | |
| 651 | ||
| 652 | lemma of_bl_drop': | |
| 653 |   "lend = length bl - len_of TYPE ('a :: len0) ==> 
 | |
| 654 | of_bl (drop lend bl) = (of_bl bl :: 'a word)" | |
| 655 | apply (unfold of_bl_def) | |
| 656 | apply (clarsimp simp add : trunc_bl2bin [symmetric]) | |
| 657 | done | |
| 658 | ||
| 659 | lemmas of_bl_no = of_bl_def [folded word_number_of_def] | |
| 660 | ||
| 661 | lemma test_bit_of_bl: | |
| 662 |   "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)"
 | |
| 663 | apply (unfold of_bl_def word_test_bit_def) | |
| 664 | apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl) | |
| 665 | done | |
| 666 | ||
| 667 | lemma no_of_bl: | |
| 668 |   "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
 | |
| 669 | unfolding word_size of_bl_no by (simp add : word_number_of_def) | |
| 670 | ||
| 671 | lemma uint_bl: "to_bl w == bin_to_bl (size w) (uint w)" | |
| 672 | unfolding word_size to_bl_def by auto | |
| 673 | ||
| 674 | lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w" | |
| 675 | unfolding uint_bl by (simp add : word_size) | |
| 676 | ||
| 677 | lemma to_bl_of_bin: | |
| 678 |   "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
 | |
| 679 | unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) | |
| 680 | ||
| 681 | lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def] | |
| 682 | ||
| 683 | lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" | |
| 684 | unfolding uint_bl by (simp add : word_size) | |
| 685 | ||
| 686 | lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard] | |
| 24333 | 687 | |
| 688 | lemmas num_AB_u [simp] = word_uint.Rep_inverse | |
| 689 | [unfolded o_def word_number_of_def [symmetric], standard] | |
| 690 | lemmas num_AB_s [simp] = word_sint.Rep_inverse | |
| 691 | [unfolded o_def word_number_of_def [symmetric], standard] | |
| 692 | ||
| 693 | (* naturals *) | |
| 694 | lemma uints_unats: "uints n = int ` unats n" | |
| 695 | apply (unfold unats_def uints_num) | |
| 696 | apply safe | |
| 697 | apply (rule_tac image_eqI) | |
| 698 | apply (erule_tac nat_0_le [symmetric]) | |
| 699 | apply auto | |
| 700 | apply (erule_tac nat_less_iff [THEN iffD2]) | |
| 701 | apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1]) | |
| 702 | apply (auto simp add : nat_power_eq int_power) | |
| 703 | done | |
| 704 | ||
| 705 | lemma unats_uints: "unats n = nat ` uints n" | |
| 25349 
0d46bea01741
eliminated illegal schematic variables in where/of;
 wenzelm parents: 
25149diff
changeset | 706 | by (auto simp add : uints_unats image_iff) | 
| 24333 | 707 | |
| 708 | lemmas bintr_num = word_ubin.norm_eq_iff | |
| 709 | [symmetric, folded word_number_of_def, standard] | |
| 710 | lemmas sbintr_num = word_sbin.norm_eq_iff | |
| 711 | [symmetric, folded word_number_of_def, standard] | |
| 712 | ||
| 713 | lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard] | |
| 714 | lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard]; | |
| 715 | ||
| 716 | (* don't add these to simpset, since may want bintrunc n w to be simplified; | |
| 717 | may want these in reverse, but loop as simp rules, so use following *) | |
| 718 | ||
| 719 | lemma num_of_bintr': | |
| 24465 | 720 |   "bintrunc (len_of TYPE('a :: len0)) a = b ==> 
 | 
| 24333 | 721 | number_of a = (number_of b :: 'a word)" | 
| 722 | apply safe | |
| 723 | apply (rule_tac num_of_bintr [symmetric]) | |
| 724 | done | |
| 725 | ||
| 726 | lemma num_of_sbintr': | |
| 24465 | 727 |   "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> 
 | 
| 24333 | 728 | number_of a = (number_of b :: 'a word)" | 
| 729 | apply safe | |
| 730 | apply (rule_tac num_of_sbintr [symmetric]) | |
| 731 | done | |
| 732 | ||
| 733 | lemmas num_abs_bintr = sym [THEN trans, | |
| 25762 | 734 | OF num_of_bintr word_number_of_def, standard] | 
| 24333 | 735 | lemmas num_abs_sbintr = sym [THEN trans, | 
| 25762 | 736 | OF num_of_sbintr word_number_of_def, standard] | 
| 24465 | 737 | |
| 24333 | 738 | (** cast - note, no arg for new length, as it's determined by type of result, | 
| 739 | thus in "cast w = w, the type means cast to length of w! **) | |
| 740 | ||
| 741 | lemma ucast_id: "ucast w = w" | |
| 742 | unfolding ucast_def by auto | |
| 743 | ||
| 744 | lemma scast_id: "scast w = w" | |
| 745 | unfolding scast_def by auto | |
| 746 | ||
| 24465 | 747 | lemma ucast_bl: "ucast w == of_bl (to_bl w)" | 
| 748 | unfolding ucast_def of_bl_def uint_bl | |
| 749 | by (auto simp add : word_size) | |
| 750 | ||
| 24333 | 751 | lemma nth_ucast: | 
| 24465 | 752 |   "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
 | 
| 24333 | 753 | apply (unfold ucast_def test_bit_bin) | 
| 754 | apply (simp add: word_ubin.eq_norm nth_bintr word_size) | |
| 755 | apply (fast elim!: bin_nth_uint_imp) | |
| 756 | done | |
| 757 | ||
| 758 | (* for literal u(s)cast *) | |
| 759 | ||
| 760 | lemma ucast_bintr [simp]: | |
| 24465 | 761 | "ucast (number_of w ::'a::len0 word) = | 
| 762 |    number_of (bintrunc (len_of TYPE('a)) w)"
 | |
| 24333 | 763 | unfolding ucast_def by simp | 
| 764 | ||
| 765 | lemma scast_sbintr [simp]: | |
| 24465 | 766 | "scast (number_of w ::'a::len word) = | 
| 767 |    number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
 | |
| 24333 | 768 | unfolding scast_def by simp | 
| 769 | ||
| 770 | lemmas source_size = source_size_def [unfolded Let_def word_size] | |
| 771 | lemmas target_size = target_size_def [unfolded Let_def word_size] | |
| 772 | lemmas is_down = is_down_def [unfolded source_size target_size] | |
| 773 | lemmas is_up = is_up_def [unfolded source_size target_size] | |
| 774 | ||
| 775 | lemmas is_up_down = | |
| 776 | trans [OF is_up [THEN meta_eq_to_obj_eq] | |
| 777 | is_down [THEN meta_eq_to_obj_eq, symmetric], | |
| 778 | standard] | |
| 779 | ||
| 780 | lemma down_cast_same': "uc = ucast ==> is_down uc ==> uc = scast" | |
| 781 | apply (unfold is_down) | |
| 782 | apply safe | |
| 783 | apply (rule ext) | |
| 784 | apply (unfold ucast_def scast_def uint_sint) | |
| 785 | apply (rule word_ubin.norm_eq_iff [THEN iffD1]) | |
| 786 | apply simp | |
| 787 | done | |
| 788 | ||
| 24465 | 789 | lemma word_rev_tf': | 
| 790 | "r = to_bl (of_bl bl) ==> r = rev (takefill False (length r) (rev bl))" | |
| 791 | unfolding of_bl_def uint_bl | |
| 792 | by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size) | |
| 793 | ||
| 794 | lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard] | |
| 795 | ||
| 796 | lemmas word_rep_drop = word_rev_tf [simplified takefill_alt, | |
| 797 | simplified, simplified rev_take, simplified] | |
| 798 | ||
| 799 | lemma to_bl_ucast: | |
| 800 | "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = | |
| 801 |    replicate (len_of TYPE('a) - len_of TYPE('b)) False @
 | |
| 802 |    drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
 | |
| 803 | apply (unfold ucast_bl) | |
| 804 | apply (rule trans) | |
| 805 | apply (rule word_rep_drop) | |
| 806 | apply simp | |
| 807 | done | |
| 808 | ||
| 809 | lemma ucast_up_app': | |
| 810 | "uc = ucast ==> source_size uc + n = target_size uc ==> | |
| 811 | to_bl (uc w) = replicate n False @ (to_bl w)" | |
| 812 | apply (auto simp add : source_size target_size to_bl_ucast) | |
| 813 | apply (rule_tac f = "%n. replicate n False" in arg_cong) | |
| 814 | apply simp | |
| 815 | done | |
| 816 | ||
| 817 | lemma ucast_down_drop': | |
| 818 | "uc = ucast ==> source_size uc = target_size uc + n ==> | |
| 819 | to_bl (uc w) = drop n (to_bl w)" | |
| 820 | by (auto simp add : source_size target_size to_bl_ucast) | |
| 821 | ||
| 822 | lemma scast_down_drop': | |
| 823 | "sc = scast ==> source_size sc = target_size sc + n ==> | |
| 824 | to_bl (sc w) = drop n (to_bl w)" | |
| 825 | apply (subgoal_tac "sc = ucast") | |
| 826 | apply safe | |
| 827 | apply simp | |
| 828 | apply (erule refl [THEN ucast_down_drop']) | |
| 829 | apply (rule refl [THEN down_cast_same', symmetric]) | |
| 830 | apply (simp add : source_size target_size is_down) | |
| 831 | done | |
| 832 | ||
| 24333 | 833 | lemma sint_up_scast': | 
| 834 | "sc = scast ==> is_up sc ==> sint (sc w) = sint w" | |
| 835 | apply (unfold is_up) | |
| 836 | apply safe | |
| 837 | apply (simp add: scast_def word_sbin.eq_norm) | |
| 838 | apply (rule box_equals) | |
| 839 | prefer 3 | |
| 840 | apply (rule word_sbin.norm_Rep) | |
| 841 | apply (rule sbintrunc_sbintrunc_l) | |
| 842 | defer | |
| 843 | apply (subst word_sbin.norm_Rep) | |
| 844 | apply (rule refl) | |
| 845 | apply simp | |
| 846 | done | |
| 847 | ||
| 848 | lemma uint_up_ucast': | |
| 849 | "uc = ucast ==> is_up uc ==> uint (uc w) = uint w" | |
| 850 | apply (unfold is_up) | |
| 851 | apply safe | |
| 852 | apply (rule bin_eqI) | |
| 853 | apply (fold word_test_bit_def) | |
| 854 | apply (auto simp add: nth_ucast) | |
| 855 | apply (auto simp add: test_bit_bin) | |
| 856 | done | |
| 857 | ||
| 858 | lemmas down_cast_same = refl [THEN down_cast_same'] | |
| 24465 | 859 | lemmas ucast_up_app = refl [THEN ucast_up_app'] | 
| 860 | lemmas ucast_down_drop = refl [THEN ucast_down_drop'] | |
| 861 | lemmas scast_down_drop = refl [THEN scast_down_drop'] | |
| 24333 | 862 | lemmas uint_up_ucast = refl [THEN uint_up_ucast'] | 
| 863 | lemmas sint_up_scast = refl [THEN sint_up_scast'] | |
| 864 | ||
| 865 | lemma ucast_up_ucast': "uc = ucast ==> is_up uc ==> ucast (uc w) = ucast w" | |
| 866 | apply (simp (no_asm) add: ucast_def) | |
| 867 | apply (clarsimp simp add: uint_up_ucast) | |
| 868 | done | |
| 869 | ||
| 870 | lemma scast_up_scast': "sc = scast ==> is_up sc ==> scast (sc w) = scast w" | |
| 871 | apply (simp (no_asm) add: scast_def) | |
| 872 | apply (clarsimp simp add: sint_up_scast) | |
| 873 | done | |
| 874 | ||
| 24465 | 875 | lemma ucast_of_bl_up': | 
| 876 | "w = of_bl bl ==> size bl <= size w ==> ucast w = of_bl bl" | |
| 877 | by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) | |
| 878 | ||
| 24333 | 879 | lemmas ucast_up_ucast = refl [THEN ucast_up_ucast'] | 
| 880 | lemmas scast_up_scast = refl [THEN scast_up_scast'] | |
| 24465 | 881 | lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up'] | 
| 24333 | 882 | |
| 883 | lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] | |
| 884 | lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] | |
| 885 | ||
| 886 | lemmas isduu = is_up_down [where c = "ucast", THEN iffD2] | |
| 887 | lemmas isdus = is_up_down [where c = "scast", THEN iffD2] | |
| 888 | lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] | |
| 889 | lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] | |
| 890 | ||
| 891 | lemma up_ucast_surj: | |
| 24465 | 892 | "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> | 
| 24333 | 893 | surj (ucast :: 'a word => 'b word)" | 
| 894 | by (rule surjI, erule ucast_up_ucast_id) | |
| 895 | ||
| 896 | lemma up_scast_surj: | |
| 24465 | 897 | "is_up (scast :: 'b::len word => 'a::len word) ==> | 
| 24333 | 898 | surj (scast :: 'a word => 'b word)" | 
| 899 | by (rule surjI, erule scast_up_scast_id) | |
| 900 | ||
| 901 | lemma down_scast_inj: | |
| 24465 | 902 | "is_down (scast :: 'b::len word => 'a::len word) ==> | 
| 24333 | 903 | inj_on (ucast :: 'a word => 'b word) A" | 
| 904 | by (rule inj_on_inverseI, erule scast_down_scast_id) | |
| 905 | ||
| 906 | lemma down_ucast_inj: | |
| 24465 | 907 | "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> | 
| 24333 | 908 | inj_on (ucast :: 'a word => 'b word) A" | 
| 909 | by (rule inj_on_inverseI, erule ucast_down_ucast_id) | |
| 910 | ||
| 24465 | 911 | lemma of_bl_append_same: "of_bl (X @ to_bl w) = w" | 
| 912 | by (rule word_bl.Rep_eqD) (simp add: word_rep_drop) | |
| 24333 | 913 | |
| 914 | lemma ucast_down_no': | |
| 915 | "uc = ucast ==> is_down uc ==> uc (number_of bin) = number_of bin" | |
| 916 | apply (unfold word_number_of_def is_down) | |
| 917 | apply (clarsimp simp add: ucast_def word_ubin.eq_norm) | |
| 918 | apply (rule word_ubin.norm_eq_iff [THEN iffD1]) | |
| 919 | apply (erule bintrunc_bintrunc_ge) | |
| 920 | done | |
| 921 | ||
| 922 | lemmas ucast_down_no = ucast_down_no' [OF refl] | |
| 923 | ||
| 24465 | 924 | lemma ucast_down_bl': "uc = ucast ==> is_down uc ==> uc (of_bl bl) = of_bl bl" | 
| 925 | unfolding of_bl_no by clarify (erule ucast_down_no) | |
| 926 | ||
| 927 | lemmas ucast_down_bl = ucast_down_bl' [OF refl] | |
| 928 | ||
| 929 | lemmas slice_def' = slice_def [unfolded word_size] | |
| 930 | lemmas test_bit_def' = word_test_bit_def [THEN meta_eq_to_obj_eq, THEN fun_cong] | |
| 931 | ||
| 932 | lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def | |
| 933 | lemmas word_log_bin_defs = word_log_defs | |
| 934 | ||
| 24333 | 935 | end |