src/HOL/Word/Word.thy
changeset 45545 26aebb8ac9c1
parent 45544 c0304794e9e4
child 45546 6dd3e88de4c2
equal deleted inserted replaced
45544:c0304794e9e4 45545:26aebb8ac9c1
    28 lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)"
    28 lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)"
    29   by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse)
    29   by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse)
    30 
    30 
    31 code_datatype word_of_int
    31 code_datatype word_of_int
    32 
    32 
       
    33 subsection {* Random instance *}
       
    34 
    33 notation fcomp (infixl "\<circ>>" 60)
    35 notation fcomp (infixl "\<circ>>" 60)
    34 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    36 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    35 
    37 
    36 instantiation word :: ("{len0, typerep}") random
    38 instantiation word :: ("{len0, typerep}") random
    37 begin
    39 begin
   116 syntax
   118 syntax
   117   of_int :: "int => 'a"
   119   of_int :: "int => 'a"
   118 translations
   120 translations
   119   "case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x"
   121   "case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x"
   120 
   122 
   121 
   123 subsection {* Type-definition locale instantiations *}
   122 subsection  "Arithmetic operations"
       
   123 
       
   124 instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, ord}"
       
   125 begin
       
   126 
       
   127 definition
       
   128   word_0_wi: "0 = word_of_int 0"
       
   129 
       
   130 definition
       
   131   word_1_wi: "1 = word_of_int 1"
       
   132 
       
   133 definition
       
   134   word_add_def: "a + b = word_of_int (uint a + uint b)"
       
   135 
       
   136 definition
       
   137   word_sub_wi: "a - b = word_of_int (uint a - uint b)"
       
   138 
       
   139 definition
       
   140   word_minus_def: "- a = word_of_int (- uint a)"
       
   141 
       
   142 definition
       
   143   word_mult_def: "a * b = word_of_int (uint a * uint b)"
       
   144 
       
   145 definition
       
   146   word_div_def: "a div b = word_of_int (uint a div uint b)"
       
   147 
       
   148 definition
       
   149   word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
       
   150 
       
   151 definition
       
   152   word_number_of_def: "number_of w = word_of_int w"
       
   153 
       
   154 definition
       
   155   word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
       
   156 
       
   157 definition
       
   158   word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
       
   159 
       
   160 instance ..
       
   161 
       
   162 end
       
   163 
       
   164 definition
       
   165   word_succ :: "'a :: len0 word => 'a word"
       
   166 where
       
   167   "word_succ a = word_of_int (Int.succ (uint a))"
       
   168 
       
   169 definition
       
   170   word_pred :: "'a :: len0 word => 'a word"
       
   171 where
       
   172   "word_pred a = word_of_int (Int.pred (uint a))"
       
   173 
       
   174 definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where
       
   175   "a udvd b = (EX n>=0. uint b = n * uint a)"
       
   176 
       
   177 definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where
       
   178   "a <=s b = (sint a <= sint b)"
       
   179 
       
   180 definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where
       
   181   "(x <s y) = (x <=s y & x ~= y)"
       
   182 
       
   183 
       
   184 
       
   185 subsection "Bit-wise operations"
       
   186 
       
   187 instantiation word :: (len0) bits
       
   188 begin
       
   189 
       
   190 definition
       
   191   word_and_def: 
       
   192   "(a::'a word) AND b = word_of_int (uint a AND uint b)"
       
   193 
       
   194 definition
       
   195   word_or_def:  
       
   196   "(a::'a word) OR b = word_of_int (uint a OR uint b)"
       
   197 
       
   198 definition
       
   199   word_xor_def: 
       
   200   "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
       
   201 
       
   202 definition
       
   203   word_not_def: 
       
   204   "NOT (a::'a word) = word_of_int (NOT (uint a))"
       
   205 
       
   206 definition
       
   207   word_test_bit_def: "test_bit a = bin_nth (uint a)"
       
   208 
       
   209 definition
       
   210   word_set_bit_def: "set_bit a n x =
       
   211    word_of_int (bin_sc n (If x 1 0) (uint a))"
       
   212 
       
   213 definition
       
   214   word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)"
       
   215 
       
   216 definition
       
   217   word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1"
       
   218 
       
   219 definition shiftl1 :: "'a word \<Rightarrow> 'a word" where
       
   220   "shiftl1 w = word_of_int (uint w BIT 0)"
       
   221 
       
   222 definition shiftr1 :: "'a word \<Rightarrow> 'a word" where
       
   223   -- "shift right as unsigned or as signed, ie logical or arithmetic"
       
   224   "shiftr1 w = word_of_int (bin_rest (uint w))"
       
   225 
       
   226 definition
       
   227   shiftl_def: "w << n = (shiftl1 ^^ n) w"
       
   228 
       
   229 definition
       
   230   shiftr_def: "w >> n = (shiftr1 ^^ n) w"
       
   231 
       
   232 instance ..
       
   233 
       
   234 end
       
   235 
       
   236 instantiation word :: (len) bitss
       
   237 begin
       
   238 
       
   239 definition
       
   240   word_msb_def: 
       
   241   "msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min"
       
   242 
       
   243 instance ..
       
   244 
       
   245 end
       
   246 
       
   247 lemma [code]:
       
   248   "msb a \<longleftrightarrow> bin_sign (sint a) = (- 1 :: int)"
       
   249   by (simp only: word_msb_def Min_def)
       
   250 
       
   251 definition setBit :: "'a :: len0 word => nat => 'a word" where 
       
   252   "setBit w n = set_bit w n True"
       
   253 
       
   254 definition clearBit :: "'a :: len0 word => nat => 'a word" where
       
   255   "clearBit w n = set_bit w n False"
       
   256 
       
   257 
       
   258 subsection "Shift operations"
       
   259 
       
   260 definition sshiftr1 :: "'a :: len word => 'a word" where 
       
   261   "sshiftr1 w = word_of_int (bin_rest (sint w))"
       
   262 
       
   263 definition bshiftr1 :: "bool => 'a :: len word => 'a word" where
       
   264   "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
       
   265 
       
   266 definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where
       
   267   "w >>> n = (sshiftr1 ^^ n) w"
       
   268 
       
   269 definition mask :: "nat => 'a::len word" where
       
   270   "mask n = (1 << n) - 1"
       
   271 
       
   272 definition revcast :: "'a :: len0 word => 'b :: len0 word" where
       
   273   "revcast w =  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
       
   274 
       
   275 definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where
       
   276   "slice1 n w = of_bl (takefill False n (to_bl w))"
       
   277 
       
   278 definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where
       
   279   "slice n w = slice1 (size w - n) w"
       
   280 
       
   281 
       
   282 subsection "Rotation"
       
   283 
       
   284 definition rotater1 :: "'a list => 'a list" where
       
   285   "rotater1 ys = 
       
   286     (case ys of [] => [] | x # xs => last ys # butlast ys)"
       
   287 
       
   288 definition rotater :: "nat => 'a list => 'a list" where
       
   289   "rotater n = rotater1 ^^ n"
       
   290 
       
   291 definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where
       
   292   "word_rotr n w = of_bl (rotater n (to_bl w))"
       
   293 
       
   294 definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where
       
   295   "word_rotl n w = of_bl (rotate n (to_bl w))"
       
   296 
       
   297 definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where
       
   298   "word_roti i w = (if i >= 0 then word_rotr (nat i) w
       
   299                     else word_rotl (nat (- i)) w)"
       
   300 
       
   301 
       
   302 subsection "Split and cat operations"
       
   303 
       
   304 definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where
       
   305   "word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
       
   306 
       
   307 definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where
       
   308   "word_split a = 
       
   309    (case bin_split (len_of TYPE ('c)) (uint a) of 
       
   310      (u, v) => (word_of_int u, word_of_int v))"
       
   311 
       
   312 definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where
       
   313   "word_rcat ws = 
       
   314   word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
       
   315 
       
   316 definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where
       
   317   "word_rsplit w = 
       
   318   map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
       
   319 
       
   320 definition max_word :: "'a::len word" -- "Largest representable machine integer." where
       
   321   "max_word = word_of_int (2 ^ len_of TYPE('a) - 1)"
       
   322 
       
   323 primrec of_bool :: "bool \<Rightarrow> 'a::len word" where
       
   324   "of_bool False = 0"
       
   325 | "of_bool True = 1"
       
   326 
       
   327 
       
   328 lemmas of_nth_def = word_set_bits_def
       
   329 
   124 
   330 lemmas word_size_gt_0 [iff] = 
   125 lemmas word_size_gt_0 [iff] = 
   331   xtr1 [OF word_size len_gt_0, standard]
   126   xtr1 [OF word_size len_gt_0, standard]
   332 lemmas lens_gt_0 = word_size_gt_0 len_gt_0
   127 lemmas lens_gt_0 = word_size_gt_0 len_gt_0
   333 lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
   128 lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
   380   td_ext "uint::'a::len0 word \<Rightarrow> int" 
   175   td_ext "uint::'a::len0 word \<Rightarrow> int" 
   381          word_of_int 
   176          word_of_int 
   382          "uints (len_of TYPE('a::len0))"
   177          "uints (len_of TYPE('a::len0))"
   383          "bintrunc (len_of TYPE('a::len0))"
   178          "bintrunc (len_of TYPE('a::len0))"
   384   by (rule td_ext_ubin)
   179   by (rule td_ext_ubin)
       
   180 
       
   181 lemma split_word_all:
       
   182   "(\<And>x::'a::len0 word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
       
   183 proof
       
   184   fix x :: "'a word"
       
   185   assume "\<And>x. PROP P (word_of_int x)"
       
   186   hence "PROP P (word_of_int (uint x))" .
       
   187   thus "PROP P x" by simp
       
   188 qed
       
   189 
       
   190 subsection  "Arithmetic operations"
       
   191 
       
   192 definition
       
   193   word_succ :: "'a :: len0 word => 'a word"
       
   194 where
       
   195   "word_succ a = word_of_int (Int.succ (uint a))"
       
   196 
       
   197 definition
       
   198   word_pred :: "'a :: len0 word => 'a word"
       
   199 where
       
   200   "word_pred a = word_of_int (Int.pred (uint a))"
       
   201 
       
   202 instantiation word :: (len0) "{number, Divides.div, ord, comm_monoid_mult, comm_ring}"
       
   203 begin
       
   204 
       
   205 definition
       
   206   word_0_wi: "0 = word_of_int 0"
       
   207 
       
   208 definition
       
   209   word_1_wi: "1 = word_of_int 1"
       
   210 
       
   211 definition
       
   212   word_add_def: "a + b = word_of_int (uint a + uint b)"
       
   213 
       
   214 definition
       
   215   word_sub_wi: "a - b = word_of_int (uint a - uint b)"
       
   216 
       
   217 definition
       
   218   word_minus_def: "- a = word_of_int (- uint a)"
       
   219 
       
   220 definition
       
   221   word_mult_def: "a * b = word_of_int (uint a * uint b)"
       
   222 
       
   223 definition
       
   224   word_div_def: "a div b = word_of_int (uint a div uint b)"
       
   225 
       
   226 definition
       
   227   word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
       
   228 
       
   229 definition
       
   230   word_number_of_def: "number_of w = word_of_int w"
       
   231 
       
   232 definition
       
   233   word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
       
   234 
       
   235 definition
       
   236   word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
       
   237 
       
   238 lemmas word_arith_wis = 
       
   239   word_add_def word_mult_def word_minus_def 
       
   240   word_succ_def word_pred_def word_0_wi word_1_wi
       
   241 
       
   242 lemmas arths = 
       
   243   bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
       
   244                 folded word_ubin.eq_norm, standard]
       
   245 
       
   246 lemma wi_homs: 
       
   247   shows
       
   248   wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
       
   249   wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
       
   250   wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
       
   251   wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and
       
   252   wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)"
       
   253   by (auto simp: word_arith_wis arths)
       
   254 
       
   255 lemmas wi_hom_syms = wi_homs [symmetric]
       
   256 
       
   257 lemma word_sub_def: "a - b = a + - (b :: 'a :: len0 word)"
       
   258   unfolding word_sub_wi diff_minus
       
   259   by (simp only : word_uint.Rep_inverse wi_hom_syms)
       
   260     
       
   261 lemmas word_diff_minus = word_sub_def [standard]
       
   262 
       
   263 lemma word_of_int_sub_hom:
       
   264   "(word_of_int a) - word_of_int b = word_of_int (a - b)"
       
   265   unfolding word_sub_def diff_minus by (simp only : wi_homs)
       
   266 
       
   267 lemmas new_word_of_int_homs = 
       
   268   word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
       
   269 
       
   270 lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
       
   271 
       
   272 lemmas word_of_int_hom_syms =
       
   273   new_word_of_int_hom_syms [unfolded succ_def pred_def]
       
   274 
       
   275 lemmas word_of_int_homs =
       
   276   new_word_of_int_homs [unfolded succ_def pred_def]
       
   277 
       
   278 lemmas word_of_int_add_hom = word_of_int_homs (2)
       
   279 lemmas word_of_int_mult_hom = word_of_int_homs (3)
       
   280 lemmas word_of_int_minus_hom = word_of_int_homs (4)
       
   281 lemmas word_of_int_succ_hom = word_of_int_homs (5)
       
   282 lemmas word_of_int_pred_hom = word_of_int_homs (6)
       
   283 lemmas word_of_int_0_hom = word_of_int_homs (7)
       
   284 lemmas word_of_int_1_hom = word_of_int_homs (8)
       
   285 
       
   286 instance
       
   287   by default (auto simp: split_word_all word_of_int_homs algebra_simps)
       
   288 
       
   289 end
       
   290 
       
   291 lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1"
       
   292   unfolding word_arith_wis
       
   293   by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
       
   294 
       
   295 lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
       
   296 
       
   297 instance word :: (len) comm_ring_1
       
   298   by (intro_classes) (simp add: lenw1_zero_neq_one)
       
   299 
       
   300 lemma word_of_nat: "of_nat n = word_of_int (int n)"
       
   301   by (induct n) (auto simp add : word_of_int_hom_syms)
       
   302 
       
   303 lemma word_of_int: "of_int = word_of_int"
       
   304   apply (rule ext)
       
   305   apply (case_tac x rule: int_diff_cases)
       
   306   apply (simp add: word_of_nat word_of_int_sub_hom)
       
   307   done
       
   308 
       
   309 lemma word_number_of_eq: 
       
   310   "number_of w = (of_int w :: 'a :: len word)"
       
   311   unfolding word_number_of_def word_of_int by auto
       
   312 
       
   313 instance word :: (len) number_ring
       
   314   by (intro_classes) (simp add : word_number_of_eq)
       
   315 
       
   316 definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where
       
   317   "a udvd b = (EX n>=0. uint b = n * uint a)"
       
   318 
       
   319 definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where
       
   320   "a <=s b = (sint a <= sint b)"
       
   321 
       
   322 definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where
       
   323   "(x <s y) = (x <=s y & x ~= y)"
       
   324 
       
   325 
       
   326 subsection "Bit-wise operations"
       
   327 
       
   328 instantiation word :: (len0) bits
       
   329 begin
       
   330 
       
   331 definition
       
   332   word_and_def: 
       
   333   "(a::'a word) AND b = word_of_int (uint a AND uint b)"
       
   334 
       
   335 definition
       
   336   word_or_def:  
       
   337   "(a::'a word) OR b = word_of_int (uint a OR uint b)"
       
   338 
       
   339 definition
       
   340   word_xor_def: 
       
   341   "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
       
   342 
       
   343 definition
       
   344   word_not_def: 
       
   345   "NOT (a::'a word) = word_of_int (NOT (uint a))"
       
   346 
       
   347 definition
       
   348   word_test_bit_def: "test_bit a = bin_nth (uint a)"
       
   349 
       
   350 definition
       
   351   word_set_bit_def: "set_bit a n x =
       
   352    word_of_int (bin_sc n (If x 1 0) (uint a))"
       
   353 
       
   354 definition
       
   355   word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)"
       
   356 
       
   357 definition
       
   358   word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1"
       
   359 
       
   360 definition shiftl1 :: "'a word \<Rightarrow> 'a word" where
       
   361   "shiftl1 w = word_of_int (uint w BIT 0)"
       
   362 
       
   363 definition shiftr1 :: "'a word \<Rightarrow> 'a word" where
       
   364   -- "shift right as unsigned or as signed, ie logical or arithmetic"
       
   365   "shiftr1 w = word_of_int (bin_rest (uint w))"
       
   366 
       
   367 definition
       
   368   shiftl_def: "w << n = (shiftl1 ^^ n) w"
       
   369 
       
   370 definition
       
   371   shiftr_def: "w >> n = (shiftr1 ^^ n) w"
       
   372 
       
   373 instance ..
       
   374 
       
   375 end
       
   376 
       
   377 instantiation word :: (len) bitss
       
   378 begin
       
   379 
       
   380 definition
       
   381   word_msb_def: 
       
   382   "msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min"
       
   383 
       
   384 instance ..
       
   385 
       
   386 end
       
   387 
       
   388 lemma [code]:
       
   389   "msb a \<longleftrightarrow> bin_sign (sint a) = (- 1 :: int)"
       
   390   by (simp only: word_msb_def Min_def)
       
   391 
       
   392 definition setBit :: "'a :: len0 word => nat => 'a word" where 
       
   393   "setBit w n = set_bit w n True"
       
   394 
       
   395 definition clearBit :: "'a :: len0 word => nat => 'a word" where
       
   396   "clearBit w n = set_bit w n False"
       
   397 
       
   398 
       
   399 subsection "Shift operations"
       
   400 
       
   401 definition sshiftr1 :: "'a :: len word => 'a word" where 
       
   402   "sshiftr1 w = word_of_int (bin_rest (sint w))"
       
   403 
       
   404 definition bshiftr1 :: "bool => 'a :: len word => 'a word" where
       
   405   "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
       
   406 
       
   407 definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where
       
   408   "w >>> n = (sshiftr1 ^^ n) w"
       
   409 
       
   410 definition mask :: "nat => 'a::len word" where
       
   411   "mask n = (1 << n) - 1"
       
   412 
       
   413 definition revcast :: "'a :: len0 word => 'b :: len0 word" where
       
   414   "revcast w =  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
       
   415 
       
   416 definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where
       
   417   "slice1 n w = of_bl (takefill False n (to_bl w))"
       
   418 
       
   419 definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where
       
   420   "slice n w = slice1 (size w - n) w"
       
   421 
       
   422 
       
   423 subsection "Rotation"
       
   424 
       
   425 definition rotater1 :: "'a list => 'a list" where
       
   426   "rotater1 ys = 
       
   427     (case ys of [] => [] | x # xs => last ys # butlast ys)"
       
   428 
       
   429 definition rotater :: "nat => 'a list => 'a list" where
       
   430   "rotater n = rotater1 ^^ n"
       
   431 
       
   432 definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where
       
   433   "word_rotr n w = of_bl (rotater n (to_bl w))"
       
   434 
       
   435 definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where
       
   436   "word_rotl n w = of_bl (rotate n (to_bl w))"
       
   437 
       
   438 definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where
       
   439   "word_roti i w = (if i >= 0 then word_rotr (nat i) w
       
   440                     else word_rotl (nat (- i)) w)"
       
   441 
       
   442 
       
   443 subsection "Split and cat operations"
       
   444 
       
   445 definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where
       
   446   "word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
       
   447 
       
   448 definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where
       
   449   "word_split a = 
       
   450    (case bin_split (len_of TYPE ('c)) (uint a) of 
       
   451      (u, v) => (word_of_int u, word_of_int v))"
       
   452 
       
   453 definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where
       
   454   "word_rcat ws = 
       
   455   word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
       
   456 
       
   457 definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where
       
   458   "word_rsplit w = 
       
   459   map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
       
   460 
       
   461 definition max_word :: "'a::len word" -- "Largest representable machine integer." where
       
   462   "max_word = word_of_int (2 ^ len_of TYPE('a) - 1)"
       
   463 
       
   464 primrec of_bool :: "bool \<Rightarrow> 'a::len word" where
       
   465   "of_bool False = 0"
       
   466 | "of_bool True = 1"
       
   467 
       
   468 
       
   469 lemmas of_nth_def = word_set_bits_def
   385 
   470 
   386 lemma sint_sbintrunc': 
   471 lemma sint_sbintrunc': 
   387   "sint (word_of_int bin :: 'a word) = 
   472   "sint (word_of_int bin :: 'a word) = 
   388     (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
   473     (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
   389   unfolding sint_uint 
   474   unfolding sint_uint 
   982 qed (unfold word_sle_def word_sless_def, auto)
  1067 qed (unfold word_sle_def word_sless_def, auto)
   983 
  1068 
   984 interpretation signed: linorder "word_sle" "word_sless"
  1069 interpretation signed: linorder "word_sle" "word_sless"
   985   by (rule signed_linorder)
  1070   by (rule signed_linorder)
   986 
  1071 
   987 lemmas word_arith_wis = 
       
   988   word_add_def word_mult_def word_minus_def 
       
   989   word_succ_def word_pred_def word_0_wi word_1_wi
       
   990 
       
   991 lemma udvdI: 
  1072 lemma udvdI: 
   992   "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
  1073   "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
   993   by (auto simp: udvd_def)
  1074   by (auto simp: udvd_def)
   994 
  1075 
   995 lemmas word_div_no [simp] = 
  1076 lemmas word_div_no [simp] = 
  1114 
  1195 
  1115 lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
  1196 lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
  1116   unfolding ucast_def word_1_wi
  1197   unfolding ucast_def word_1_wi
  1117   by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
  1198   by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
  1118 
  1199 
  1119 (* abstraction preserves the operations
       
  1120   (the definitions tell this for bins in range uint) *)
       
  1121 
       
  1122 lemmas arths = 
       
  1123   bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
       
  1124                 folded word_ubin.eq_norm, standard]
       
  1125 
       
  1126 lemma wi_homs: 
       
  1127   shows
       
  1128   wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
       
  1129   wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
       
  1130   wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
       
  1131   wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and
       
  1132   wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)"
       
  1133   by (auto simp: word_arith_wis arths)
       
  1134 
       
  1135 lemmas wi_hom_syms = wi_homs [symmetric]
       
  1136 
       
  1137 lemma word_sub_def: "a - b = a + - (b :: 'a :: len0 word)"
       
  1138   unfolding word_sub_wi diff_minus
       
  1139   by (simp only : word_uint.Rep_inverse wi_hom_syms)
       
  1140     
       
  1141 lemmas word_diff_minus = word_sub_def [standard]
       
  1142 
       
  1143 lemma word_of_int_sub_hom:
       
  1144   "(word_of_int a) - word_of_int b = word_of_int (a - b)"
       
  1145   unfolding word_sub_def diff_minus by (simp only : wi_homs)
       
  1146 
       
  1147 lemmas new_word_of_int_homs = 
       
  1148   word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
       
  1149 
       
  1150 lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
       
  1151 
       
  1152 lemmas word_of_int_hom_syms =
       
  1153   new_word_of_int_hom_syms [unfolded succ_def pred_def]
       
  1154 
       
  1155 lemmas word_of_int_homs =
       
  1156   new_word_of_int_homs [unfolded succ_def pred_def]
       
  1157 
       
  1158 lemmas word_of_int_add_hom = word_of_int_homs (2)
       
  1159 lemmas word_of_int_mult_hom = word_of_int_homs (3)
       
  1160 lemmas word_of_int_minus_hom = word_of_int_homs (4)
       
  1161 lemmas word_of_int_succ_hom = word_of_int_homs (5)
       
  1162 lemmas word_of_int_pred_hom = word_of_int_homs (6)
       
  1163 lemmas word_of_int_0_hom = word_of_int_homs (7)
       
  1164 lemmas word_of_int_1_hom = word_of_int_homs (8)
       
  1165 
       
  1166 (* now, to get the weaker results analogous to word_div/mod_def *)
  1200 (* now, to get the weaker results analogous to word_div/mod_def *)
  1167 
  1201 
  1168 lemmas word_arith_alts = 
  1202 lemmas word_arith_alts = 
  1169   word_sub_wi [unfolded succ_def pred_def, standard]
  1203   word_sub_wi [unfolded succ_def pred_def, standard]
  1170   word_arith_wis [unfolded succ_def pred_def, standard]
  1204   word_arith_wis [unfolded succ_def pred_def, standard]
  1171 
  1205 
       
  1206 (* FIXME: Lots of duplicate lemmas *)
  1172 lemmas word_sub_alt = word_arith_alts (1)
  1207 lemmas word_sub_alt = word_arith_alts (1)
  1173 lemmas word_add_alt = word_arith_alts (2)
  1208 lemmas word_add_alt = word_arith_alts (2)
  1174 lemmas word_mult_alt = word_arith_alts (3)
  1209 lemmas word_mult_alt = word_arith_alts (3)
  1175 lemmas word_minus_alt = word_arith_alts (4)
  1210 lemmas word_minus_alt = word_arith_alts (4)
  1176 lemmas word_succ_alt = word_arith_alts (5)
  1211 lemmas word_succ_alt = word_arith_alts (5)
  1232 (* alternative approach to lifting arithmetic equalities *)
  1267 (* alternative approach to lifting arithmetic equalities *)
  1233 lemma word_of_int_Ex:
  1268 lemma word_of_int_Ex:
  1234   "\<exists>y. x = word_of_int y"
  1269   "\<exists>y. x = word_of_int y"
  1235   by (rule_tac x="uint x" in exI) simp
  1270   by (rule_tac x="uint x" in exI) simp
  1236 
  1271 
       
  1272 (* FIXME: redundant theorems *)
  1237 lemma word_arith_eqs:
  1273 lemma word_arith_eqs:
  1238   fixes a :: "'a::len0 word"
  1274   fixes a :: "'a::len0 word"
  1239   fixes b :: "'a::len0 word"
  1275   fixes b :: "'a::len0 word"
  1240   shows
  1276   shows
  1241   word_add_0: "0 + a = a" and
  1277   word_add_0: "0 + a = a" and
  1285 
  1321 
  1286 lemma word_zero_le [simp] :
  1322 lemma word_zero_le [simp] :
  1287   "0 <= (y :: 'a :: len0 word)"
  1323   "0 <= (y :: 'a :: len0 word)"
  1288   unfolding word_le_def by auto
  1324   unfolding word_le_def by auto
  1289   
  1325   
  1290 instance word :: (len0) semigroup_add
       
  1291   by intro_classes (simp add: word_add_assoc)
       
  1292 
       
  1293 instance word :: (len0) linorder
  1326 instance word :: (len0) linorder
  1294   by intro_classes (auto simp: word_less_def word_le_def)
  1327   by intro_classes (auto simp: word_less_def word_le_def)
  1295 
       
  1296 instance word :: (len0) ring
       
  1297   by intro_classes
       
  1298      (auto simp: word_arith_eqs word_diff_minus 
       
  1299                  word_diff_self [unfolded word_diff_minus])
       
  1300 
  1328 
  1301 lemma word_m1_ge [simp] : "word_pred 0 >= y"
  1329 lemma word_m1_ge [simp] : "word_pred 0 >= y"
  1302   unfolding word_le_def
  1330   unfolding word_le_def
  1303   by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
  1331   by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
  1304 
  1332 
  1349 
  1377 
  1350 lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
  1378 lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
  1351   unfolding dvd_def udvd_nat_alt by force
  1379   unfolding dvd_def udvd_nat_alt by force
  1352 
  1380 
  1353 lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
  1381 lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
  1354 
       
  1355 lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1"
       
  1356   unfolding word_arith_wis
       
  1357   by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
       
  1358 
       
  1359 lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
       
  1360 
  1382 
  1361 lemma no_no [simp] : "number_of (number_of b) = number_of b"
  1383 lemma no_no [simp] : "number_of (number_of b) = number_of b"
  1362   by (simp add: number_of_eq)
  1384   by (simp add: number_of_eq)
  1363 
  1385 
  1364 lemma unat_minus_one: "x ~= 0 \<Longrightarrow> unat (x - 1) = unat x - 1"
  1386 lemma unat_minus_one: "x ~= 0 \<Longrightarrow> unat (x - 1) = unat x - 1"
  1725                  word_pred_rbl word_mult_rbl word_add_rbl)
  1747                  word_pred_rbl word_mult_rbl word_add_rbl)
  1726 
  1748 
  1727 
  1749 
  1728 subsection "Arithmetic type class instantiations"
  1750 subsection "Arithmetic type class instantiations"
  1729 
  1751 
  1730 instance word :: (len0) comm_monoid_add ..
       
  1731 
       
  1732 instance word :: (len0) comm_monoid_mult
       
  1733   apply (intro_classes)
       
  1734    apply (simp add: word_mult_commute)
       
  1735   apply (simp add: word_mult_1)
       
  1736   done
       
  1737 
       
  1738 instance word :: (len0) comm_semiring 
       
  1739   by (intro_classes) (simp add : word_left_distrib)
       
  1740 
       
  1741 instance word :: (len0) ab_group_add ..
       
  1742 
       
  1743 instance word :: (len0) comm_ring ..
       
  1744 
       
  1745 instance word :: (len) comm_semiring_1 
       
  1746   by (intro_classes) (simp add: lenw1_zero_neq_one)
       
  1747 
       
  1748 instance word :: (len) comm_ring_1 ..
       
  1749 
       
  1750 instance word :: (len0) comm_semiring_0 ..
       
  1751 
       
  1752 instance word :: (len0) order ..
       
  1753 
       
  1754 (* note that iszero_def is only for class comm_semiring_1_cancel,
  1752 (* note that iszero_def is only for class comm_semiring_1_cancel,
  1755    which requires word length >= 1, ie 'a :: len word *) 
  1753    which requires word length >= 1, ie 'a :: len word *) 
  1756 lemma zero_bintrunc:
  1754 lemma zero_bintrunc:
  1757   "iszero (number_of x :: 'a :: len word) = 
  1755   "iszero (number_of x :: 'a :: len word) = 
  1758     (bintrunc (len_of TYPE('a)) x = Int.Pls)"
  1756     (bintrunc (len_of TYPE('a)) x = Int.Pls)"
  1762   done
  1760   done
  1763 
  1761 
  1764 lemmas word_le_0_iff [simp] =
  1762 lemmas word_le_0_iff [simp] =
  1765   word_zero_le [THEN leD, THEN linorder_antisym_conv1]
  1763   word_zero_le [THEN leD, THEN linorder_antisym_conv1]
  1766 
  1764 
  1767 lemma word_of_nat: "of_nat n = word_of_int (int n)"
       
  1768   by (induct n) (auto simp add : word_of_int_hom_syms)
       
  1769 
       
  1770 lemma word_of_int: "of_int = word_of_int"
       
  1771   apply (rule ext)
       
  1772   apply (case_tac x rule: int_diff_cases)
       
  1773   apply (simp add: word_of_nat word_of_int_sub_hom)
       
  1774   done
       
  1775 
       
  1776 lemma word_of_int_nat: 
  1765 lemma word_of_int_nat: 
  1777   "0 <= x \<Longrightarrow> word_of_int x = of_nat (nat x)"
  1766   "0 <= x \<Longrightarrow> word_of_int x = of_nat (nat x)"
  1778   by (simp add: of_nat_nat word_of_int)
  1767   by (simp add: of_nat_nat word_of_int)
  1779 
       
  1780 lemma word_number_of_eq: 
       
  1781   "number_of w = (of_int w :: 'a :: len word)"
       
  1782   unfolding word_number_of_def word_of_int by auto
       
  1783 
       
  1784 instance word :: (len) number_ring
       
  1785   by (intro_classes) (simp add : word_number_of_eq)
       
  1786 
  1768 
  1787 lemma iszero_word_no [simp] : 
  1769 lemma iszero_word_no [simp] : 
  1788   "iszero (number_of bin :: 'a :: len word) = 
  1770   "iszero (number_of bin :: 'a :: len word) = 
  1789     iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
  1771     iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
  1790   apply (simp add: zero_bintrunc number_of_is_id)
  1772   apply (simp add: zero_bintrunc number_of_is_id)