src/HOL/Word/WordBitwise.thy
changeset 24408 058c5613a86f
parent 24401 d9d2aa843a3b
child 24465 70f0214b3ecc
equal deleted inserted replaced
24407:61b10ffb2549 24408:058c5613a86f
    36 lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
    36 lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
    37   by (simp add: word_and_def number_of_is_id word_no_wi [symmetric]
    37   by (simp add: word_and_def number_of_is_id word_no_wi [symmetric]
    38                 bin_trunc_ao(1) [symmetric]) 
    38                 bin_trunc_ao(1) [symmetric]) 
    39 
    39 
    40 lemma word_ops_nth_size:
    40 lemma word_ops_nth_size:
    41   "n < size (x::'a::len0 word) ==> 
    41   "n < size (x::'a word) ==> 
    42     (x OR y) !! n = (x !! n | y !! n) & 
    42     (x OR y) !! n = (x !! n | y !! n) & 
    43     (x AND y) !! n = (x !! n & y !! n) & 
    43     (x AND y) !! n = (x !! n & y !! n) & 
    44     (x XOR y) !! n = (x !! n ~= y !! n) & 
    44     (x XOR y) !! n = (x !! n ~= y !! n) & 
    45     (NOT x) !! n = (~ x !! n)"
    45     (NOT x) !! n = (~ x !! n)"
    46   unfolding word_size word_no_wi word_test_bit_def word_log_defs
    46   unfolding word_size word_no_wi word_test_bit_def word_log_defs
    47   by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
    47   by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
    48 
    48 
    49 lemma word_ao_nth:
    49 lemma word_ao_nth:
    50   fixes x :: "'a::len0 word"
    50   fixes x :: "'a word"
    51   shows "(x OR y) !! n = (x !! n | y !! n) & 
    51   shows "(x OR y) !! n = (x !! n | y !! n) & 
    52          (x AND y) !! n = (x !! n & y !! n)"
    52          (x AND y) !! n = (x !! n & y !! n)"
    53   apply (cases "n < size x")
    53   apply (cases "n < size x")
    54    apply (drule_tac y = "y" in word_ops_nth_size)
    54    apply (drule_tac y = "y" in word_ops_nth_size)
    55    apply simp
    55    apply simp
    64   word_0_wi_Pls
    64   word_0_wi_Pls
    65   word_m1_wi_Min
    65   word_m1_wi_Min
    66   word_wi_log_defs
    66   word_wi_log_defs
    67 
    67 
    68 lemma word_bw_assocs:
    68 lemma word_bw_assocs:
    69   fixes x :: "'a::len0 word"
    69   fixes x :: "'a word"
    70   shows
    70   shows
    71   "(x AND y) AND z = x AND y AND z"
    71   "(x AND y) AND z = x AND y AND z"
    72   "(x OR y) OR z = x OR y OR z"
    72   "(x OR y) OR z = x OR y OR z"
    73   "(x XOR y) XOR z = x XOR y XOR z"
    73   "(x XOR y) XOR z = x XOR y XOR z"
    74   using word_of_int_Ex [where x=x] 
    74   using word_of_int_Ex [where x=x] 
    75         word_of_int_Ex [where x=y] 
    75         word_of_int_Ex [where x=y] 
    76         word_of_int_Ex [where x=z]
    76         word_of_int_Ex [where x=z]
    77   by (auto simp: bwsimps bbw_assocs)
    77   by (auto simp: bwsimps bbw_assocs)
    78   
    78   
    79 lemma word_bw_comms:
    79 lemma word_bw_comms:
    80   fixes x :: "'a::len0 word"
    80   fixes x :: "'a word"
    81   shows
    81   shows
    82   "x AND y = y AND x"
    82   "x AND y = y AND x"
    83   "x OR y = y OR x"
    83   "x OR y = y OR x"
    84   "x XOR y = y XOR x"
    84   "x XOR y = y XOR x"
    85   using word_of_int_Ex [where x=x] 
    85   using word_of_int_Ex [where x=x] 
    86         word_of_int_Ex [where x=y] 
    86         word_of_int_Ex [where x=y] 
    87   by (auto simp: bwsimps bin_ops_comm)
    87   by (auto simp: bwsimps bin_ops_comm)
    88   
    88   
    89 lemma word_bw_lcs:
    89 lemma word_bw_lcs:
    90   fixes x :: "'a::len0 word"
    90   fixes x :: "'a word"
    91   shows
    91   shows
    92   "y AND x AND z = x AND y AND z"
    92   "y AND x AND z = x AND y AND z"
    93   "y OR x OR z = x OR y OR z"
    93   "y OR x OR z = x OR y OR z"
    94   "y XOR x XOR z = x XOR y XOR z"
    94   "y XOR x XOR z = x XOR y XOR z"
    95   using word_of_int_Ex [where x=x] 
    95   using word_of_int_Ex [where x=x] 
    96         word_of_int_Ex [where x=y] 
    96         word_of_int_Ex [where x=y] 
    97         word_of_int_Ex [where x=z]
    97         word_of_int_Ex [where x=z]
    98   by (auto simp: bwsimps)
    98   by (auto simp: bwsimps)
    99 
    99 
   100 lemma word_log_esimps [simp]:
   100 lemma word_log_esimps [simp]:
   101   fixes x :: "'a::len0 word"
   101   fixes x :: "'a word"
   102   shows
   102   shows
   103   "x AND 0 = 0"
   103   "x AND 0 = 0"
   104   "x AND -1 = x"
   104   "x AND -1 = x"
   105   "x OR 0 = x"
   105   "x OR 0 = x"
   106   "x OR -1 = -1"
   106   "x OR -1 = -1"
   114   "-1 XOR x = NOT x"
   114   "-1 XOR x = NOT x"
   115   using word_of_int_Ex [where x=x] 
   115   using word_of_int_Ex [where x=x] 
   116   by (auto simp: bwsimps)
   116   by (auto simp: bwsimps)
   117 
   117 
   118 lemma word_not_dist:
   118 lemma word_not_dist:
   119   fixes x :: "'a::len0 word"
   119   fixes x :: "'a word"
   120   shows
   120   shows
   121   "NOT (x OR y) = NOT x AND NOT y"
   121   "NOT (x OR y) = NOT x AND NOT y"
   122   "NOT (x AND y) = NOT x OR NOT y"
   122   "NOT (x AND y) = NOT x OR NOT y"
   123   using word_of_int_Ex [where x=x] 
   123   using word_of_int_Ex [where x=x] 
   124         word_of_int_Ex [where x=y] 
   124         word_of_int_Ex [where x=y] 
   125   by (auto simp: bwsimps bbw_not_dist)
   125   by (auto simp: bwsimps bbw_not_dist)
   126 
   126 
   127 lemma word_bw_same:
   127 lemma word_bw_same:
   128   fixes x :: "'a::len0 word"
   128   fixes x :: "'a word"
   129   shows
   129   shows
   130   "x AND x = x"
   130   "x AND x = x"
   131   "x OR x = x"
   131   "x OR x = x"
   132   "x XOR x = 0"
   132   "x XOR x = 0"
   133   using word_of_int_Ex [where x=x] 
   133   using word_of_int_Ex [where x=x] 
   134   by (auto simp: bwsimps)
   134   by (auto simp: bwsimps)
   135 
   135 
   136 lemma word_ao_absorbs [simp]:
   136 lemma word_ao_absorbs [simp]:
   137   fixes x :: "'a::len0 word"
   137   fixes x :: "'a word"
   138   shows
   138   shows
   139   "x AND (y OR x) = x"
   139   "x AND (y OR x) = x"
   140   "x OR y AND x = x"
   140   "x OR y AND x = x"
   141   "x AND (x OR y) = x"
   141   "x AND (x OR y) = x"
   142   "y AND x OR x = x"
   142   "y AND x OR x = x"
   147   using word_of_int_Ex [where x=x] 
   147   using word_of_int_Ex [where x=x] 
   148         word_of_int_Ex [where x=y] 
   148         word_of_int_Ex [where x=y] 
   149   by (auto simp: bwsimps)
   149   by (auto simp: bwsimps)
   150 
   150 
   151 lemma word_not_not [simp]:
   151 lemma word_not_not [simp]:
   152   "NOT NOT (x::'a::len0 word) = x"
   152   "NOT NOT (x::'a word) = x"
   153   using word_of_int_Ex [where x=x] 
   153   using word_of_int_Ex [where x=x] 
   154   by (auto simp: bwsimps)
   154   by (auto simp: bwsimps)
   155 
   155 
   156 lemma word_ao_dist:
   156 lemma word_ao_dist:
   157   fixes x :: "'a::len0 word"
   157   fixes x :: "'a word"
   158   shows "(x OR y) AND z = x AND z OR y AND z"
   158   shows "(x OR y) AND z = x AND z OR y AND z"
   159   using word_of_int_Ex [where x=x] 
   159   using word_of_int_Ex [where x=x] 
   160         word_of_int_Ex [where x=y] 
   160         word_of_int_Ex [where x=y] 
   161         word_of_int_Ex [where x=z]   
   161         word_of_int_Ex [where x=z]   
   162   by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
   162   by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
   163 
   163 
   164 lemma word_oa_dist:
   164 lemma word_oa_dist:
   165   fixes x :: "'a::len0 word"
   165   fixes x :: "'a word"
   166   shows "x AND y OR z = (x OR z) AND (y OR z)"
   166   shows "x AND y OR z = (x OR z) AND (y OR z)"
   167   using word_of_int_Ex [where x=x] 
   167   using word_of_int_Ex [where x=x] 
   168         word_of_int_Ex [where x=y] 
   168         word_of_int_Ex [where x=y] 
   169         word_of_int_Ex [where x=z]   
   169         word_of_int_Ex [where x=z]   
   170   by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
   170   by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
   171 
   171 
   172 lemma word_add_not [simp]: 
   172 lemma word_add_not [simp]: 
   173   fixes x :: "'a::len0 word"
   173   fixes x :: "'a word"
   174   shows "x + NOT x = -1"
   174   shows "x + NOT x = -1"
   175   using word_of_int_Ex [where x=x] 
   175   using word_of_int_Ex [where x=x] 
   176   by (auto simp: bwsimps bin_add_not)
   176   by (auto simp: bwsimps bin_add_not)
   177 
   177 
   178 lemma word_plus_and_or [simp]:
   178 lemma word_plus_and_or [simp]:
   179   fixes x :: "'a::len0 word"
   179   fixes x :: "'a word"
   180   shows "(x AND y) + (x OR y) = x + y"
   180   shows "(x AND y) + (x OR y) = x + y"
   181   using word_of_int_Ex [where x=x] 
   181   using word_of_int_Ex [where x=x] 
   182         word_of_int_Ex [where x=y] 
   182         word_of_int_Ex [where x=y] 
   183   by (auto simp: bwsimps plus_and_or)
   183   by (auto simp: bwsimps plus_and_or)
   184 
   184 
   185 lemma leoa:   
   185 lemma leoa:   
   186   fixes x :: "'a::len0 word"
   186   fixes x :: "'a word"
   187   shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
   187   shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
   188 lemma leao: 
   188 lemma leao: 
   189   fixes x' :: "'a::len0 word"
   189   fixes x' :: "'a word"
   190   shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto 
   190   shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto 
   191 
   191 
   192 lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
   192 lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
   193 
   193 
   194 lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
   194 lemma le_word_or2: "x <= x OR (y::'a word)"
   195   unfolding word_le_def uint_or
   195   unfolding word_le_def uint_or
   196   by (auto intro: le_int_or) 
   196   by (auto intro: le_int_or) 
   197 
   197 
   198 lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
   198 lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
   199 lemmas word_and_le1 =
   199 lemmas word_and_le1 =
   200   xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
   200   xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
   201 lemmas word_and_le2 =
   201 lemmas word_and_le2 =
   202   xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
   202   xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
   203 
   203 
   204 lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
   204 lemma word_lsb_alt: "lsb (w::'a word) = test_bit w 0"
   205   by (auto simp: word_test_bit_def word_lsb_def)
   205   by (auto simp: word_test_bit_def word_lsb_def)
   206 
   206 
   207 lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
   207 lemma word_lsb_1_0: "lsb (1::'a::finite word) & ~ lsb (0::'b word)"
   208   unfolding word_lsb_def word_1_no word_0_no by auto
   208   unfolding word_lsb_def word_1_no word_0_no by auto
   209 
   209 
   210 lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
   210 lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
   211   unfolding word_lsb_def bin_last_mod by auto
   211   unfolding word_lsb_def bin_last_mod by auto
   212 
   212 
   213 lemma word_msb_sint: "msb w = (sint w < 0)" 
   213 lemma word_msb_sint: "msb w = (sint w < 0)" 
   214   unfolding word_msb_def
   214   unfolding word_msb_def
   215   by (simp add : sign_Min_lt_0 number_of_is_id)
   215   by (simp add : sign_Min_lt_0 number_of_is_id)
   216   
   216   
   217 lemma word_msb_no': 
   217 lemma word_msb_no': 
   218   "w = number_of bin ==> msb (w::'a::len word) = bin_nth bin (size w - 1)"
   218   "w = number_of bin ==> msb (w::'a::finite word) = bin_nth bin (size w - 1)"
   219   unfolding word_msb_def word_number_of_def
   219   unfolding word_msb_def word_number_of_def
   220   by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
   220   by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
   221 
   221 
   222 lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
   222 lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
   223 
   223 
   224 lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)"
   224 lemma word_msb_nth': "msb (w::'a::finite word) = bin_nth (uint w) (size w - 1)"
   225   apply (unfold word_size)
   225   apply (unfold word_size)
   226   apply (rule trans [OF _ word_msb_no])
   226   apply (rule trans [OF _ word_msb_no])
   227   apply (simp add : word_number_of_def)
   227   apply (simp add : word_number_of_def)
   228   done
   228   done
   229 
   229 
   230 lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
   230 lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
   231 
   231 
   232 lemma word_set_nth:
   232 lemma word_set_nth:
   233   "set_bit w n (test_bit w n) = (w::'a::len0 word)"
   233   "set_bit w n (test_bit w n) = (w::'a word)"
   234   unfolding word_test_bit_def word_set_bit_def by auto
   234   unfolding word_test_bit_def word_set_bit_def by auto
   235 
   235 
   236 lemma test_bit_set: 
   236 lemma test_bit_set: 
   237   fixes w :: "'a::len0 word"
   237   fixes w :: "'a word"
   238   shows "(set_bit w n x) !! n = (n < size w & x)"
   238   shows "(set_bit w n x) !! n = (n < size w & x)"
   239   unfolding word_size word_test_bit_def word_set_bit_def
   239   unfolding word_size word_test_bit_def word_set_bit_def
   240   by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
   240   by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
   241 
   241 
   242 lemma test_bit_set_gen: 
   242 lemma test_bit_set_gen: 
   243   fixes w :: "'a::len0 word"
   243   fixes w :: "'a word"
   244   shows "test_bit (set_bit w n x) m = 
   244   shows "test_bit (set_bit w n x) m = 
   245          (if m = n then n < size w & x else test_bit w m)"
   245          (if m = n then n < size w & x else test_bit w m)"
   246   apply (unfold word_size word_test_bit_def word_set_bit_def)
   246   apply (unfold word_size word_test_bit_def word_set_bit_def)
   247   apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
   247   apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
   248   apply (auto elim!: test_bit_size [unfolded word_size]
   248   apply (auto elim!: test_bit_size [unfolded word_size]
   249               simp add: word_test_bit_def [symmetric])
   249               simp add: word_test_bit_def [symmetric])
   250   done
   250   done
   251 
   251 
   252 lemma msb_nth':
   252 lemma msb_nth':
   253   fixes w :: "'a::len word"
   253   fixes w :: "'a::finite word"
   254   shows "msb w = w !! (size w - 1)"
   254   shows "msb w = w !! (size w - 1)"
   255   unfolding word_msb_nth' word_test_bit_def by simp
   255   unfolding word_msb_nth' word_test_bit_def by simp
   256 
   256 
   257 lemmas msb_nth = msb_nth' [unfolded word_size]
   257 lemmas msb_nth = msb_nth' [unfolded word_size]
   258 
   258 
   259 lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN
   259 lemmas msb0 = zero_less_card_finite [THEN diff_Suc_less, THEN
   260   word_ops_nth_size [unfolded word_size], standard]
   260   word_ops_nth_size [unfolded word_size], standard]
   261 lemmas msb1 = msb0 [where i = 0]
   261 lemmas msb1 = msb0 [where i = 0]
   262 lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
   262 lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
   263 
   263 
   264 lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
   264 lemmas lsb0 = zero_less_card_finite [THEN word_ops_nth_size [unfolded word_size], standard]
   265 lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
   265 lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
   266 
   266 
   267 lemma word_set_set_same: 
   267 lemma word_set_set_same: 
   268   fixes w :: "'a::len0 word"
   268   fixes w :: "'a word"
   269   shows "set_bit (set_bit w n x) n y = set_bit w n y" 
   269   shows "set_bit (set_bit w n x) n y = set_bit w n y" 
   270   by (rule word_eqI) (simp add : test_bit_set_gen word_size)
   270   by (rule word_eqI) (simp add : test_bit_set_gen word_size)
   271     
   271     
   272 lemma word_set_set_diff: 
   272 lemma word_set_set_diff: 
   273   fixes w :: "'a::len0 word"
   273   fixes w :: "'a word"
   274   assumes "m ~= n"
   274   assumes "m ~= n"
   275   shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" 
   275   shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" 
   276   by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
   276   by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
   277     
   277     
   278 lemma test_bit_no': 
   278 lemma test_bit_no': 
   279   fixes w :: "'a::len0 word"
   279   fixes w :: "'a word"
   280   shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
   280   shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
   281   unfolding word_test_bit_def word_number_of_def word_size
   281   unfolding word_test_bit_def word_number_of_def word_size
   282   by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
   282   by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
   283 
   283 
   284 lemmas test_bit_no = 
   284 lemmas test_bit_no = 
   285   refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
   285   refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
   286 
   286 
   287 lemma nth_0: "~ (0::'a::len0 word) !! n"
   287 lemma nth_0: "~ (0::'a word) !! n"
   288   unfolding test_bit_no word_0_no by auto
   288   unfolding test_bit_no word_0_no by auto
   289 
   289 
   290 lemma nth_sint: 
   290 lemma nth_sint: 
   291   fixes w :: "'a::len word"
   291   fixes w :: "'a::finite word"
   292   defines "l \<equiv> len_of TYPE ('a)"
   292   defines "l \<equiv> CARD('a)"
   293   shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
   293   shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
   294   unfolding sint_uint l_def
   294   unfolding sint_uint l_def
   295   by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
   295   by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
   296 
   296 
   297 lemma word_lsb_no: 
   297 lemma word_lsb_no: 
   298   "lsb (number_of bin :: 'a :: len word) = (bin_last bin = bit.B1)"
   298   "lsb (number_of bin :: 'a :: finite word) = (bin_last bin = bit.B1)"
   299   unfolding word_lsb_alt test_bit_no by auto
   299   unfolding word_lsb_alt test_bit_no by auto
   300 
   300 
   301 lemma word_set_no: 
   301 lemma word_set_no: 
   302   "set_bit (number_of bin::'a::len0 word) n b = 
   302   "set_bit (number_of bin::'a word) n b = 
   303     number_of (bin_sc n (if b then bit.B1 else bit.B0) bin)"
   303     number_of (bin_sc n (if b then bit.B1 else bit.B0) bin)"
   304   apply (unfold word_set_bit_def word_number_of_def [symmetric])
   304   apply (unfold word_set_bit_def word_number_of_def [symmetric])
   305   apply (rule word_eqI)
   305   apply (rule word_eqI)
   306   apply (clarsimp simp: word_size bin_nth_sc_gen number_of_is_id 
   306   apply (clarsimp simp: word_size bin_nth_sc_gen number_of_is_id 
   307                         test_bit_no nth_bintr)
   307                         test_bit_no nth_bintr)
   310 lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
   310 lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
   311   simplified if_simps, THEN eq_reflection, standard]
   311   simplified if_simps, THEN eq_reflection, standard]
   312 lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
   312 lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
   313   simplified if_simps, THEN eq_reflection, standard]
   313   simplified if_simps, THEN eq_reflection, standard]
   314 
   314 
   315 lemma word_msb_n1: "msb (-1::'a::len word)"
   315 lemma word_msb_n1: "msb (-1::'a::finite word)"
   316   unfolding word_msb_def sint_sbintrunc number_of_is_id bin_sign_lem
   316   unfolding word_msb_def sint_sbintrunc number_of_is_id bin_sign_lem
   317   by (rule bin_nth_Min)
   317   by (rule bin_nth_Min)
   318 
   318 
   319 declare word_set_set_same [simp] word_set_nth [simp]
   319 declare word_set_set_same [simp] word_set_nth [simp]
   320   test_bit_no [simp] word_set_no [simp] nth_0 [simp]
   320   test_bit_no [simp] word_set_no [simp] nth_0 [simp]
   321   setBit_no [simp] clearBit_no [simp]
   321   setBit_no [simp] clearBit_no [simp]
   322   word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
   322   word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
   323 
   323 
   324 lemma word_set_nth_iff: 
   324 lemma word_set_nth_iff: 
   325   "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
   325   "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a word))"
   326   apply (rule iffI)
   326   apply (rule iffI)
   327    apply (rule disjCI)
   327    apply (rule disjCI)
   328    apply (drule word_eqD)
   328    apply (drule word_eqD)
   329    apply (erule sym [THEN trans])
   329    apply (erule sym [THEN trans])
   330    apply (simp add: test_bit_set)
   330    apply (simp add: test_bit_set)
   336   apply force
   336   apply force
   337   done
   337   done
   338 
   338 
   339 lemma test_bit_2p': 
   339 lemma test_bit_2p': 
   340   "w = word_of_int (2 ^ n) ==> 
   340   "w = word_of_int (2 ^ n) ==> 
   341     w !! m = (m = n & m < size (w :: 'a :: len word))"
   341     w !! m = (m = n & m < size (w :: 'a :: finite word))"
   342   unfolding word_test_bit_def word_size
   342   unfolding word_test_bit_def word_size
   343   by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
   343   by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
   344 
   344 
   345 lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
   345 lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
   346 
   346 
   347 lemmas nth_w2p = test_bit_2p [unfolded of_int_number_of_eq
   347 lemmas nth_w2p = test_bit_2p [unfolded of_int_number_of_eq
   348   word_of_int [symmetric] of_int_power]
   348   word_of_int [symmetric] of_int_power]
   349 
   349 
   350 lemma uint_2p: 
   350 lemma uint_2p: 
   351   "(0::'a::len word) < 2 ^ n ==> uint (2 ^ n::'a::len word) = 2 ^ n"
   351   "(0::'a::finite word) < 2 ^ n ==> uint (2 ^ n::'a::finite word) = 2 ^ n"
   352   apply (unfold word_arith_power_alt)
   352   apply (unfold word_arith_power_alt)
   353   apply (case_tac "len_of TYPE ('a)")
   353   apply (case_tac "CARD('a)")
   354    apply clarsimp
   354    apply clarsimp
   355   apply (case_tac "nat")
   355   apply (case_tac "nat")
   356    apply clarsimp
   356    apply clarsimp
   357    apply (case_tac "n")
   357    apply (case_tac "n")
   358     apply (clarsimp simp add : word_1_wi [symmetric])
   358     apply (clarsimp simp add : word_1_wi [symmetric])
   360   apply (drule word_gt_0 [THEN iffD1])
   360   apply (drule word_gt_0 [THEN iffD1])
   361   apply (safe intro!: word_eqI bin_nth_lem ext)
   361   apply (safe intro!: word_eqI bin_nth_lem ext)
   362      apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
   362      apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
   363   done
   363   done
   364 
   364 
   365 lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n" 
   365 lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: finite word) = 2 ^ n" 
   366   apply (unfold word_arith_power_alt)
   366   apply (unfold word_arith_power_alt)
   367   apply (case_tac "len_of TYPE ('a)")
   367   apply (case_tac "CARD('a)")
   368    apply clarsimp
   368    apply clarsimp
   369   apply (case_tac "nat")
   369   apply (case_tac "nat")
   370    apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
   370    apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
   371    apply (rule box_equals) 
   371    apply (rule box_equals) 
   372      apply (rule_tac [2] bintr_ariths (1))+ 
   372      apply (rule_tac [2] bintr_ariths (1))+ 
   373    apply (clarsimp simp add : number_of_is_id)
   373    apply (clarsimp simp add : number_of_is_id)
   374   apply simp 
   374   apply simp 
   375   done
   375   done
   376 
   376 
   377 lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: len word)" 
   377 lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: finite word)" 
   378   apply (rule xtr3) 
   378   apply (rule xtr3) 
   379   apply (rule_tac [2] y = "x" in le_word_or2)
   379   apply (rule_tac [2] y = "x" in le_word_or2)
   380   apply (rule word_eqI)
   380   apply (rule word_eqI)
   381   apply (auto simp add: word_ao_nth nth_w2p word_size)
   381   apply (auto simp add: word_ao_nth nth_w2p word_size)
   382   done
   382   done
   383 
   383 
   384 lemma word_clr_le: 
   384 lemma word_clr_le: 
   385   fixes w :: "'a::len0 word"
   385   fixes w :: "'a word"
   386   shows "w >= set_bit w n False"
   386   shows "w >= set_bit w n False"
   387   apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
   387   apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
   388   apply simp
   388   apply simp
   389   apply (rule order_trans)
   389   apply (rule order_trans)
   390    apply (rule bintr_bin_clr_le)
   390    apply (rule bintr_bin_clr_le)
   391   apply simp
   391   apply simp
   392   done
   392   done
   393 
   393 
   394 lemma word_set_ge: 
   394 lemma word_set_ge: 
   395   fixes w :: "'a::len word"
   395   fixes w :: "'a::finite word"
   396   shows "w <= set_bit w n True"
   396   shows "w <= set_bit w n True"
   397   apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
   397   apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
   398   apply simp
   398   apply simp
   399   apply (rule order_trans [OF _ bintr_bin_set_ge])
   399   apply (rule order_trans [OF _ bintr_bin_set_ge])
   400   apply simp
   400   apply simp