src/HOL/Word/Bits_Int.thy
changeset 61799 4cf66f21b764
parent 58874 7172c7ffb047
child 64593 50c715579715
equal deleted inserted replaced
61798:27f3c10b0b50 61799:4cf66f21b764
     4   Definitions and basic theorems for bit-wise logical operations 
     4   Definitions and basic theorems for bit-wise logical operations 
     5   for integers expressed using Pls, Min, BIT,
     5   for integers expressed using Pls, Min, BIT,
     6   and converting them to and from lists of bools.
     6   and converting them to and from lists of bools.
     7 *) 
     7 *) 
     8 
     8 
     9 section {* Bitwise Operations on Binary Integers *}
     9 section \<open>Bitwise Operations on Binary Integers\<close>
    10 
    10 
    11 theory Bits_Int
    11 theory Bits_Int
    12 imports Bits Bit_Representation
    12 imports Bits Bit_Representation
    13 begin
    13 begin
    14 
    14 
    15 subsection {* Logical operations *}
    15 subsection \<open>Logical operations\<close>
    16 
    16 
    17 text "bit-wise logical operations on the int type"
    17 text "bit-wise logical operations on the int type"
    18 
    18 
    19 instantiation int :: bit
    19 instantiation int :: bit
    20 begin
    20 begin
    41 
    41 
    42 instance ..
    42 instance ..
    43 
    43 
    44 end
    44 end
    45 
    45 
    46 subsubsection {* Basic simplification rules *}
    46 subsubsection \<open>Basic simplification rules\<close>
    47 
    47 
    48 lemma int_not_BIT [simp]:
    48 lemma int_not_BIT [simp]:
    49   "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
    49   "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
    50   unfolding int_not_def Bit_def by (cases b, simp_all)
    50   unfolding int_not_def Bit_def by (cases b, simp_all)
    51 
    51 
    86 
    86 
    87 lemma int_xor_Bits [simp]: 
    87 lemma int_xor_Bits [simp]: 
    88   "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
    88   "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
    89   unfolding int_xor_def by auto
    89   unfolding int_xor_def by auto
    90 
    90 
    91 subsubsection {* Binary destructors *}
    91 subsubsection \<open>Binary destructors\<close>
    92 
    92 
    93 lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
    93 lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
    94   by (cases x rule: bin_exhaust, simp)
    94   by (cases x rule: bin_exhaust, simp)
    95 
    95 
    96 lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
    96 lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
   119   "!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)"
   119   "!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)"
   120   "!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" 
   120   "!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" 
   121   "!!x. bin_nth (NOT x) n = (~ bin_nth x n)"
   121   "!!x. bin_nth (NOT x) n = (~ bin_nth x n)"
   122   by (induct n) auto
   122   by (induct n) auto
   123 
   123 
   124 subsubsection {* Derived properties *}
   124 subsubsection \<open>Derived properties\<close>
   125 
   125 
   126 lemma int_xor_minus1 [simp]: "(-1::int) XOR x = NOT x"
   126 lemma int_xor_minus1 [simp]: "(-1::int) XOR x = NOT x"
   127   by (auto simp add: bin_eq_iff bin_nth_ops)
   127   by (auto simp add: bin_eq_iff bin_nth_ops)
   128 
   128 
   129 lemma int_xor_extra_simps [simp]:
   129 lemma int_xor_extra_simps [simp]:
   217 (*
   217 (*
   218 Why were these declared simp???
   218 Why were these declared simp???
   219 declare bin_ops_comm [simp] bbw_assocs [simp] 
   219 declare bin_ops_comm [simp] bbw_assocs [simp] 
   220 *)
   220 *)
   221 
   221 
   222 subsubsection {* Simplification with numerals *}
   222 subsubsection \<open>Simplification with numerals\<close>
   223 
   223 
   224 text {* Cases for @{text "0"} and @{text "-1"} are already covered by
   224 text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by
   225   other simp rules. *}
   225   other simp rules.\<close>
   226 
   226 
   227 lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
   227 lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
   228   by (metis (mono_tags) BIT_eq_iff bin_ex_rl bin_last_BIT bin_rest_BIT)
   228   by (metis (mono_tags) BIT_eq_iff bin_ex_rl bin_last_BIT bin_rest_BIT)
   229 
   229 
   230 lemma bin_rest_neg_numeral_BitM [simp]:
   230 lemma bin_rest_neg_numeral_BitM [simp]:
   233 
   233 
   234 lemma bin_last_neg_numeral_BitM [simp]:
   234 lemma bin_last_neg_numeral_BitM [simp]:
   235   "bin_last (- numeral (Num.BitM w))"
   235   "bin_last (- numeral (Num.BitM w))"
   236   by (simp only: BIT_bin_simps [symmetric] bin_last_BIT)
   236   by (simp only: BIT_bin_simps [symmetric] bin_last_BIT)
   237 
   237 
   238 text {* FIXME: The rule sets below are very large (24 rules for each
   238 text \<open>FIXME: The rule sets below are very large (24 rules for each
   239   operator). Is there a simpler way to do this? *}
   239   operator). Is there a simpler way to do this?\<close>
   240 
   240 
   241 lemma int_and_numerals [simp]:
   241 lemma int_and_numerals [simp]:
   242   "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
   242   "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
   243   "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT False"
   243   "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT False"
   244   "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
   244   "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
   317   "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
   317   "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
   318   "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
   318   "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
   319   "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
   319   "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
   320   by (rule bin_rl_eqI, simp, simp)+
   320   by (rule bin_rl_eqI, simp, simp)+
   321 
   321 
   322 subsubsection {* Interactions with arithmetic *}
   322 subsubsection \<open>Interactions with arithmetic\<close>
   323 
   323 
   324 lemma plus_and_or [rule_format]:
   324 lemma plus_and_or [rule_format]:
   325   "ALL y::int. (x AND y) + (x OR y) = x + y"
   325   "ALL y::int. (x AND y) + (x OR y) = x + y"
   326   apply (induct x rule: bin_induct)
   326   apply (induct x rule: bin_induct)
   327     apply clarsimp
   327     apply clarsimp
   356     apply clarsimp
   356     apply clarsimp
   357    apply clarsimp
   357    apply clarsimp
   358   apply (case_tac bit, auto)
   358   apply (case_tac bit, auto)
   359   done
   359   done
   360 
   360 
   361 subsubsection {* Truncating results of bit-wise operations *}
   361 subsubsection \<open>Truncating results of bit-wise operations\<close>
   362 
   362 
   363 lemma bin_trunc_ao: 
   363 lemma bin_trunc_ao: 
   364   "!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" 
   364   "!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" 
   365   "!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)"
   365   "!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)"
   366   by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
   366   by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
   380   by auto
   380   by auto
   381 
   381 
   382 lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
   382 lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
   383 lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
   383 lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
   384 
   384 
   385 subsection {* Setting and clearing bits *}
   385 subsection \<open>Setting and clearing bits\<close>
   386 
   386 
   387 (** nth bit, set/clear **)
   387 (** nth bit, set/clear **)
   388 
   388 
   389 primrec
   389 primrec
   390   bin_sc :: "nat => bool => int => int"
   390   bin_sc :: "nat => bool => int => int"
   478   "bin_sc (numeral k) b w =
   478   "bin_sc (numeral k) b w =
   479     bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
   479     bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
   480   by (simp add: numeral_eq_Suc)
   480   by (simp add: numeral_eq_Suc)
   481 
   481 
   482 
   482 
   483 subsection {* Splitting and concatenation *}
   483 subsection \<open>Splitting and concatenation\<close>
   484 
   484 
   485 definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
   485 definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
   486 where
   486 where
   487   "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
   487   "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
   488 
   488 
   617   apply (case_tac b rule: bin_exhaust)
   617   apply (case_tac b rule: bin_exhaust)
   618   apply simp
   618   apply simp
   619   apply (simp add: Bit_def mod_mult_mult1 p1mod22k)
   619   apply (simp add: Bit_def mod_mult_mult1 p1mod22k)
   620   done
   620   done
   621 
   621 
   622 subsection {* Miscellaneous lemmas *}
   622 subsection \<open>Miscellaneous lemmas\<close>
   623 
   623 
   624 lemma nth_2p_bin: 
   624 lemma nth_2p_bin: 
   625   "bin_nth (2 ^ n) m = (m = n)"
   625   "bin_nth (2 ^ n) m = (m = n)"
   626   apply (induct n arbitrary: m)
   626   apply (induct n arbitrary: m)
   627    apply clarsimp
   627    apply clarsimp