src/HOL/Word/WordBitwise.thy
changeset 24401 d9d2aa843a3b
parent 24397 eaf37b780683
child 24408 058c5613a86f
equal deleted inserted replaced
24400:199bb6d451e5 24401:d9d2aa843a3b
     5   contains theorems to do with bit-wise (logical) operations on words
     5   contains theorems to do with bit-wise (logical) operations on words
     6 *)
     6 *)
     7 
     7 
     8 header {* Bitwise Operations on Words *}
     8 header {* Bitwise Operations on Words *}
     9 
     9 
    10 theory WordBitwise imports WordArith WordBoolList begin
    10 theory WordBitwise imports WordArith begin
    11 
    11 
    12 lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
    12 lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
    13   
    13   
    14 (* following definitions require both arithmetic and bit-wise word operations *)
    14 (* following definitions require both arithmetic and bit-wise word operations *)
    15 
    15 
   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 bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" 
       
   205   unfolding to_bl_def word_log_defs
       
   206   by (simp add: bl_not_bin number_of_is_id word_no_wi [symmetric])
       
   207 
       
   208 lemma bl_word_xor: "to_bl (v XOR w) = app2 op ~= (to_bl v) (to_bl w)" 
       
   209   unfolding to_bl_def word_log_defs bl_xor_bin
       
   210   by (simp add: number_of_is_id word_no_wi [symmetric])
       
   211 
       
   212 lemma bl_word_or: "to_bl (v OR w) = app2 op | (to_bl v) (to_bl w)" 
       
   213   unfolding to_bl_def word_log_defs
       
   214   by (simp add: bl_or_bin number_of_is_id word_no_wi [symmetric])
       
   215 
       
   216 lemma bl_word_and: "to_bl (v AND w) = app2 op & (to_bl v) (to_bl w)" 
       
   217   unfolding to_bl_def word_log_defs
       
   218   by (simp add: bl_and_bin number_of_is_id word_no_wi [symmetric])
       
   219 
       
   220 lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
   204 lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
   221   by (auto simp: word_test_bit_def word_lsb_def)
   205   by (auto simp: word_test_bit_def word_lsb_def)
   222 
   206 
   223 lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
   207 lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
   224   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
   225 
       
   226 lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
       
   227   apply (unfold word_lsb_def uint_bl bin_to_bl_def) 
       
   228   apply (rule_tac bin="uint w" in bin_exhaust)
       
   229   apply (cases "size w")
       
   230    apply auto
       
   231    apply (auto simp add: bin_to_bl_aux_alt)
       
   232   done
       
   233 
   209 
   234 lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
   210 lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
   235   unfolding word_lsb_def bin_last_mod by auto
   211   unfolding word_lsb_def bin_last_mod by auto
   236 
   212 
   237 lemma word_msb_sint: "msb w = (sint w < 0)" 
   213 lemma word_msb_sint: "msb w = (sint w < 0)" 
   251   apply (simp add : word_number_of_def)
   227   apply (simp add : word_number_of_def)
   252   done
   228   done
   253 
   229 
   254 lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
   230 lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
   255 
   231 
   256 lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
       
   257   apply (unfold word_msb_nth uint_bl)
       
   258   apply (subst hd_conv_nth)
       
   259   apply (rule length_greater_0_conv [THEN iffD1])
       
   260    apply simp
       
   261   apply (simp add : nth_bin_to_bl word_size)
       
   262   done
       
   263 
       
   264 lemma word_set_nth:
   232 lemma word_set_nth:
   265   "set_bit w n (test_bit w n) = (w::'a::len0 word)"
   233   "set_bit w n (test_bit w n) = (w::'a::len0 word)"
   266   unfolding word_test_bit_def word_set_bit_def by auto
   234   unfolding word_test_bit_def word_set_bit_def by auto
   267 
       
   268 lemma bin_nth_uint':
       
   269   "bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
       
   270   apply (unfold word_size)
       
   271   apply (safe elim!: bin_nth_uint_imp)
       
   272    apply (frule bin_nth_uint_imp)
       
   273    apply (fast dest!: bin_nth_bl)+
       
   274   done
       
   275 
       
   276 lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
       
   277 
       
   278 lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
       
   279   unfolding to_bl_def word_test_bit_def word_size
       
   280   by (rule bin_nth_uint)
       
   281 
       
   282 lemma to_bl_nth: "n < size w ==> to_bl w ! n = w !! (size w - Suc n)"
       
   283   apply (unfold test_bit_bl)
       
   284   apply clarsimp
       
   285   apply (rule trans)
       
   286    apply (rule nth_rev_alt)
       
   287    apply (auto simp add: word_size)
       
   288   done
       
   289 
   235 
   290 lemma test_bit_set: 
   236 lemma test_bit_set: 
   291   fixes w :: "'a::len0 word"
   237   fixes w :: "'a::len0 word"
   292   shows "(set_bit w n x) !! n = (n < size w & x)"
   238   shows "(set_bit w n x) !! n = (n < size w & x)"
   293   unfolding word_size word_test_bit_def word_set_bit_def
   239   unfolding word_size word_test_bit_def word_set_bit_def
   301   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)
   302   apply (auto elim!: test_bit_size [unfolded word_size]
   248   apply (auto elim!: test_bit_size [unfolded word_size]
   303               simp add: word_test_bit_def [symmetric])
   249               simp add: word_test_bit_def [symmetric])
   304   done
   250   done
   305 
   251 
   306 lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
       
   307   unfolding of_bl_def bl_to_bin_rep_F by auto
       
   308   
       
   309 lemma msb_nth':
   252 lemma msb_nth':
   310   fixes w :: "'a::len word"
   253   fixes w :: "'a::len word"
   311   shows "msb w = w !! (size w - 1)"
   254   shows "msb w = w !! (size w - 1)"
   312   unfolding word_msb_nth' word_test_bit_def by simp
   255   unfolding word_msb_nth' word_test_bit_def by simp
   313 
   256 
   318 lemmas msb1 = msb0 [where i = 0]
   261 lemmas msb1 = msb0 [where i = 0]
   319 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]]
   320 
   263 
   321 lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
   264 lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
   322 lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
   265 lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
   323 
       
   324 lemma td_ext_nth':
       
   325   "n = size (w::'a::len0 word) ==> ofn = set_bits ==> [w, ofn g] = l ==> 
       
   326     td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
       
   327   apply (unfold word_size td_ext_def')
       
   328   apply safe
       
   329      apply (rule_tac [3] ext)
       
   330      apply (rule_tac [4] ext)
       
   331      apply (unfold word_size of_nth_def test_bit_bl)
       
   332      apply safe
       
   333        defer
       
   334        apply (clarsimp simp: word_bl.Abs_inverse)+
       
   335   apply (rule word_bl.Rep_inverse')
       
   336   apply (rule sym [THEN trans])
       
   337   apply (rule bl_of_nth_nth)
       
   338   apply simp
       
   339   apply (rule bl_of_nth_inj)
       
   340   apply (clarsimp simp add : test_bit_bl word_size)
       
   341   done
       
   342 
       
   343 lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size]
       
   344 
       
   345 interpretation test_bit:
       
   346   td_ext ["op !! :: 'a::len0 word => nat => bool"
       
   347           set_bits
       
   348           "{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
       
   349           "(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"]
       
   350   by (rule td_ext_nth)
       
   351 
       
   352 declare test_bit.Rep' [simp del]
       
   353 declare test_bit.Rep' [rule del]
       
   354 
       
   355 lemmas td_nth = test_bit.td_thm
       
   356 
   266 
   357 lemma word_set_set_same: 
   267 lemma word_set_set_same: 
   358   fixes w :: "'a::len0 word"
   268   fixes w :: "'a::len0 word"
   359   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" 
   360   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)
   400 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],
   401   simplified if_simps, THEN eq_reflection, standard]
   311   simplified if_simps, THEN eq_reflection, standard]
   402 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],
   403   simplified if_simps, THEN eq_reflection, standard]
   313   simplified if_simps, THEN eq_reflection, standard]
   404 
   314 
   405 lemma to_bl_n1: 
       
   406   "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True"
       
   407   apply (rule word_bl.Abs_inverse')
       
   408    apply simp
       
   409   apply (rule word_eqI)
       
   410   apply (clarsimp simp add: word_size test_bit_no)
       
   411   apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
       
   412   done
       
   413 
       
   414 lemma word_msb_n1: "msb (-1::'a::len word)"
   315 lemma word_msb_n1: "msb (-1::'a::len word)"
   415   unfolding word_msb_alt word_msb_alt to_bl_n1 by simp
   316   unfolding word_msb_def sint_sbintrunc number_of_is_id bin_sign_lem
       
   317   by (rule bin_nth_Min)
   416 
   318 
   417 declare word_set_set_same [simp] word_set_nth [simp]
   319 declare word_set_set_same [simp] word_set_nth [simp]
   418   test_bit_no [simp] word_set_no [simp] nth_0 [simp]
   320   test_bit_no [simp] word_set_no [simp] nth_0 [simp]
   419   setBit_no [simp] clearBit_no [simp]
   321   setBit_no [simp] clearBit_no [simp]
   420   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]