src/HOL/ex/Bit_Operations.thy
changeset 71186 3d35e12999ba
parent 71181 8331063570d6
child 71195 d50a718ccf35
equal deleted inserted replaced
71185:8a0e25d93a95 71186:3d35e12999ba
     7   imports
     7   imports
     8     "HOL-Library.Boolean_Algebra"
     8     "HOL-Library.Boolean_Algebra"
     9     Main
     9     Main
    10 begin
    10 begin
    11 
    11 
       
    12 lemma minus_1_div_exp_eq_int [simp]:
       
    13   \<open>- 1 div (2 :: int) ^ n = - 1\<close>
       
    14   for n :: nat
       
    15   by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>)
       
    16 
       
    17 context semiring_bits
       
    18 begin
       
    19 
       
    20 lemma bits_div_by_0 [simp]:
       
    21   \<open>a div 0 = 0\<close>
       
    22   by (metis local.add_cancel_right_right local.bit_mod_div_trivial local.mod_mult_div_eq local.mult_not_zero)
       
    23 
       
    24 lemma bit_0_eq [simp]:
       
    25   \<open>bit 0 = bot\<close>
       
    26   by (simp add: fun_eq_iff bit_def)
       
    27 
       
    28 end
       
    29 
       
    30 context semiring_bit_shifts
       
    31 begin
       
    32 
       
    33 end
       
    34 
       
    35 
    12 subsection \<open>Bit operations in suitable algebraic structures\<close>
    36 subsection \<open>Bit operations in suitable algebraic structures\<close>
    13 
    37 
    14 class semiring_bit_operations = semiring_bit_shifts +
    38 class semiring_bit_operations = semiring_bit_shifts +
    15   fixes "and" :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "AND" 64)
    39   fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr "AND" 64)
    16     and or :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "OR"  59)
    40     and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr "OR"  59)
    17     and xor :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "XOR" 59)
    41     and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr "XOR" 59)
       
    42   assumes bit_and_iff: \<open>\<And>n. bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
       
    43     and bit_or_iff: \<open>\<And>n. bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
       
    44     and bit_xor_iff: \<open>\<And>n. bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
    18 begin
    45 begin
    19 
    46 
    20 text \<open>
    47 text \<open>
    21   We want the bitwise operations to bind slightly weaker
    48   We want the bitwise operations to bind slightly weaker
    22   than \<open>+\<close> and \<open>-\<close>.
    49   than \<open>+\<close> and \<open>-\<close>.
    38 definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
    65 definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
    39   where \<open>flip_bit n = map_bit n Not\<close>
    66   where \<open>flip_bit n = map_bit n Not\<close>
    40 
    67 
    41 text \<open>
    68 text \<open>
    42   Having 
    69   Having 
    43   <^const>\<open>set_bit\<close>, \<^const>\<open>unset_bit\<close> and \<^const>\<open>flip_bit\<close> as separate
    70   \<^const>\<open>set_bit\<close>, \<^const>\<open>unset_bit\<close> and \<^const>\<open>flip_bit\<close> as separate
    44   operations allows to implement them using bit masks later.
    71   operations allows to implement them using bit masks later.
    45 \<close>
    72 \<close>
    46 
    73 
    47 lemma stable_imp_drop_eq:
    74 lemma stable_imp_drop_eq:
    48   \<open>drop_bit n a = a\<close> if \<open>a div 2 = a\<close>
    75   \<open>drop_bit n a = a\<close> if \<open>a div 2 = a\<close>
    83 
   110 
    84 end
   111 end
    85 
   112 
    86 class ring_bit_operations = semiring_bit_operations + ring_parity +
   113 class ring_bit_operations = semiring_bit_operations + ring_parity +
    87   fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
   114   fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
    88   assumes boolean_algebra: \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close>
   115   assumes bits_even_minus_1_div_exp_iff [simp]: \<open>even (- 1 div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0\<close>
    89     and boolean_algebra_xor_eq: \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close>
   116   assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
    90 begin
   117 begin
       
   118 
       
   119 lemma bits_minus_1_mod_2_eq [simp]:
       
   120   \<open>(- 1) mod 2 = 1\<close>
       
   121   by (simp add: mod_2_eq_odd)
       
   122 
       
   123 lemma bit_minus_1_iff [simp]:
       
   124   \<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close>
       
   125   by (simp add: bit_def)
    91 
   126 
    92 sublocale bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
   127 sublocale bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
    93   rewrites \<open>bit.xor = (XOR)\<close>
   128   rewrites \<open>bit.xor = (XOR)\<close>
    94 proof -
   129 proof -
    95   interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
   130   interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
    96     by (fact boolean_algebra)
   131     apply standard
       
   132              apply (auto simp add: bit_eq_iff bit_and_iff bit_or_iff bit_not_iff)
       
   133      apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   134     apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   135     done
    97   show \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close>
   136   show \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close>
    98     by standard
   137     by standard
    99   show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close>  
   138   show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close> 
   100     by (fact boolean_algebra_xor_eq)
   139     apply (auto simp add: fun_eq_iff bit.xor_def bit_eq_iff bit_and_iff bit_or_iff bit_not_iff bit_xor_iff)
       
   140          apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   141         apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   142        apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   143       apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   144      apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   145     apply (metis local.bit_def local.bit_exp_iff local.bits_div_by_0)
       
   146     done
   101 qed
   147 qed
   102 
   148 
   103 text \<open>
   149 text \<open>
   104   For the sake of code generation \<^const>\<open>not\<close> is specified as
   150   For the sake of code generation \<^const>\<open>not\<close> is specified as
   105   definitional class operation.  Note that \<^const>\<open>not\<close> has no
   151   definitional class operation.  Note that \<^const>\<open>not\<close> has no
   263 
   309 
   264 lemma xor_zero_nat_eq [simp]:
   310 lemma xor_zero_nat_eq [simp]:
   265   "n XOR 0 = n" for n :: nat
   311   "n XOR 0 = n" for n :: nat
   266   by simp
   312   by simp
   267 
   313 
   268 instance ..
   314 instance proof
       
   315   fix m n q :: nat
       
   316   show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close>
       
   317   proof (rule sym, induction q arbitrary: m n)
       
   318     case 0
       
   319     then show ?case
       
   320       by (simp add: and_nat.even_iff)
       
   321   next
       
   322     case (Suc q)
       
   323     with and_nat.rec [of m n] show ?case
       
   324       by simp
       
   325   qed
       
   326   show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close>
       
   327   proof (rule sym, induction q arbitrary: m n)
       
   328     case 0
       
   329     then show ?case
       
   330       by (simp add: or_nat.even_iff)
       
   331   next
       
   332     case (Suc q)
       
   333     with or_nat.rec [of m n] show ?case
       
   334       by simp
       
   335   qed
       
   336   show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close>
       
   337   proof (rule sym, induction q arbitrary: m n)
       
   338     case 0
       
   339     then show ?case
       
   340       by (simp add: xor_nat.even_iff)
       
   341   next
       
   342     case (Suc q)
       
   343     with xor_nat.rec [of m n] show ?case
       
   344       by simp
       
   345   qed
       
   346 qed
   269 
   347 
   270 end
   348 end
   271 
   349 
   272 global_interpretation or_nat: semilattice_neutr "(OR)" "0 :: nat"
   350 global_interpretation or_nat: semilattice_neutr "(OR)" "0 :: nat"
   273   by standard simp
   351   by standard simp
   623   "NOT 1 = (- 2 :: int)"
   701   "NOT 1 = (- 2 :: int)"
   624   by simp
   702   by simp
   625 
   703 
   626 lemma even_not_iff [simp]:
   704 lemma even_not_iff [simp]:
   627   "even (NOT k) \<longleftrightarrow> odd k"
   705   "even (NOT k) \<longleftrightarrow> odd k"
   628   for k :: int
   706     for k :: int
   629   by (simp add: not_int_def)
   707   by (simp add: not_int_def)
   630 
   708 
       
   709 lemma bit_not_iff_int:
       
   710   \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close>
       
   711     for k :: int
       
   712     by (induction n arbitrary: k)
       
   713       (simp_all add: not_int_def flip: complement_div_2)
       
   714 
       
   715 
   631 instance proof
   716 instance proof
   632   interpret bit_int: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: int"
   717   fix k l :: int and n :: nat
   633   proof
   718   show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close>
   634     show "k AND (l OR r) = k AND l OR k AND r"
   719   proof (rule sym, induction n arbitrary: k l)
   635       for k l r :: int
   720     case 0
   636     proof (induction k arbitrary: l r rule: int_bit_induct)
   721     then show ?case
   637       case zero
   722       by (simp add: and_int.even_iff)
   638       show ?case
   723   next
   639         by simp
   724     case (Suc n)
   640     next
   725     with and_int.rec [of k l] show ?case
   641       case minus
   726       by simp
   642       show ?case
   727   qed
   643         by simp
   728   show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close>
   644     next
   729   proof (rule sym, induction n arbitrary: k l)
   645       case (even k)
   730     case 0
   646       then show ?case by (simp add: and_int.rec [of "k * 2"]
   731     then show ?case
   647         or_int.rec [of "(k AND l div 2) * 2"] or_int.rec [of l])
   732       by (simp add: or_int.even_iff)
   648     next
   733   next
   649       case (odd k)
   734     case (Suc n)
   650       then show ?case by (simp add: and_int.rec [of "1 + k * 2"]
   735     with or_int.rec [of k l] show ?case
   651         or_int.rec [of "(k AND l div 2) * 2"] or_int.rec [of "1 + (k AND l div 2) * 2"] or_int.rec [of l])
   736       by simp
   652     qed
   737   qed
   653     show "k OR l AND r = (k OR l) AND (k OR r)"
   738   show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close>
   654       for k l r :: int
   739   proof (rule sym, induction n arbitrary: k l)
   655     proof (induction k arbitrary: l r rule: int_bit_induct)
   740     case 0
   656       case zero
   741     then show ?case
   657       then show ?case
   742       by (simp add: xor_int.even_iff)
   658         by simp
   743   next
   659     next
   744     case (Suc n)
   660       case minus
   745     with xor_int.rec [of k l] show ?case
   661       then show ?case
   746       by simp
   662         by simp
   747   qed
   663     next
   748 qed (simp_all add: bit_not_iff_int)
   664       case (even k)
       
   665       then show ?case by (simp add: or_int.rec [of "k * 2"]
       
   666         and_int.rec [of "(k OR l div 2) * 2"] and_int.rec [of "1 + (k OR l div 2) * 2"] and_int.rec [of l])
       
   667     next
       
   668       case (odd k)
       
   669       then show ?case by (simp add: or_int.rec [of "1 + k * 2"]
       
   670         and_int.rec [of "1 + (k OR l div 2) * 2"] and_int.rec [of l])
       
   671     qed
       
   672     show "k AND NOT k = 0" for k :: int
       
   673       by (induction k rule: int_bit_induct)
       
   674         (simp_all add: not_int_def complement_half minus_diff_commute [of 1])
       
   675     show "k OR NOT k = - 1" for k :: int
       
   676       by (induction k rule: int_bit_induct)
       
   677         (simp_all add: not_int_def complement_half minus_diff_commute [of 1])
       
   678   qed (simp_all add: and_int.assoc or_int.assoc,
       
   679     simp_all add: and_int.commute or_int.commute)
       
   680   show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: int)"
       
   681     by (fact bit_int.boolean_algebra_axioms)
       
   682   show "bit_int.xor = ((XOR) :: int \<Rightarrow> _)"
       
   683   proof (rule ext)+
       
   684     fix k l :: int
       
   685     have "k XOR l = k AND NOT l OR NOT k AND l"
       
   686     proof (induction k arbitrary: l rule: int_bit_induct)
       
   687       case zero
       
   688       show ?case
       
   689         by simp
       
   690     next
       
   691       case minus
       
   692       show ?case
       
   693         by (simp add: not_int_def)
       
   694     next
       
   695       case (even k)
       
   696       then show ?case
       
   697         by (simp add: xor_int.rec [of "k * 2"] and_int.rec [of "k * 2"]
       
   698           or_int.rec [of _ "1 + 2 * NOT k AND l"] not_div_2)
       
   699           (simp add: and_int.rec [of _ l])
       
   700     next
       
   701       case (odd k)
       
   702       then show ?case
       
   703         by (simp add: xor_int.rec [of "1 + k * 2"] and_int.rec [of "1 + k * 2"]
       
   704           or_int.rec [of _ "2 * NOT k AND l"] not_div_2)
       
   705           (simp add: and_int.rec [of _ l])
       
   706     qed
       
   707     then show "bit_int.xor k l = k XOR l"
       
   708       by (simp add: bit_int.xor_def)
       
   709   qed
       
   710 qed
       
   711 
   749 
   712 end
   750 end
   713 
   751 
   714 lemma one_and_int_eq [simp]:
   752 lemma one_and_int_eq [simp]:
   715   "1 AND k = k mod 2" for k :: int
   753   "1 AND k = k mod 2" for k :: int
   741   by (simp add: Parity.take_bit_eq_mod mod_eq_dvd_iff dvd_diff_commute)
   779   by (simp add: Parity.take_bit_eq_mod mod_eq_dvd_iff dvd_diff_commute)
   742 
   780 
   743 lemma take_bit_not_iff:
   781 lemma take_bit_not_iff:
   744   "Parity.take_bit n (NOT k) = Parity.take_bit n (NOT l) \<longleftrightarrow> Parity.take_bit n k = Parity.take_bit n l"
   782   "Parity.take_bit n (NOT k) = Parity.take_bit n (NOT l) \<longleftrightarrow> Parity.take_bit n k = Parity.take_bit n l"
   745   for k l :: int
   783   for k l :: int
   746   by (simp add: not_int_def take_bit_complement_iff)
   784   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_not_iff_int)
   747 
   785 
   748 lemma take_bit_and [simp]:
   786 lemma take_bit_and [simp]:
   749   "Parity.take_bit n (k AND l) = Parity.take_bit n k AND Parity.take_bit n l"
   787   "Parity.take_bit n (k AND l) = Parity.take_bit n k AND Parity.take_bit n l"
   750   for k l :: int
   788   for k l :: int
   751   apply (induction n arbitrary: k l)
   789   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff)
   752    apply simp
       
   753   apply (subst and_int.rec)
       
   754   apply (subst (2) and_int.rec)
       
   755   apply simp
       
   756   done
       
   757 
   790 
   758 lemma take_bit_or [simp]:
   791 lemma take_bit_or [simp]:
   759   "Parity.take_bit n (k OR l) = Parity.take_bit n k OR Parity.take_bit n l"
   792   "Parity.take_bit n (k OR l) = Parity.take_bit n k OR Parity.take_bit n l"
   760   for k l :: int
   793   for k l :: int
   761   apply (induction n arbitrary: k l)
   794   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff)
   762    apply simp
       
   763   apply (subst or_int.rec)
       
   764   apply (subst (2) or_int.rec)
       
   765   apply simp
       
   766   done
       
   767 
   795 
   768 lemma take_bit_xor [simp]:
   796 lemma take_bit_xor [simp]:
   769   "Parity.take_bit n (k XOR l) = Parity.take_bit n k XOR Parity.take_bit n l"
   797   "Parity.take_bit n (k XOR l) = Parity.take_bit n k XOR Parity.take_bit n l"
   770   for k l :: int
   798   for k l :: int
   771   apply (induction n arbitrary: k l)
   799   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff)
   772    apply simp
       
   773   apply (subst xor_int.rec)
       
   774   apply (subst (2) xor_int.rec)
       
   775   apply simp
       
   776   done
       
   777 
   800 
   778 end
   801 end