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