src/HOL/ex/Bit_Operations.thy
author haftmann
Tue Feb 11 19:03:57 2020 +0100 (7 weeks ago ago)
changeset 71654 d45495e897f4
parent 71638 745e518d3d0b
child 71749 b612edee9b0c
permissions -rw-r--r--
more instances
     1 (*  Author:  Florian Haftmann, TUM
     2 *)
     3 
     4 section \<open>Proof of concept for purely algebraically founded lists of bits\<close>
     5 
     6 theory Bit_Operations
     7   imports
     8     "HOL-Library.Boolean_Algebra"
     9     Main
    10 begin
    11 
    12 subsection \<open>Bit operations in suitable algebraic structures\<close>
    13 
    14 class semiring_bit_operations = semiring_bit_shifts +
    15   fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>AND\<close> 64)
    16     and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>OR\<close>  59)
    17     and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>XOR\<close> 59)
    18   assumes bit_and_iff: \<open>\<And>n. bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
    19     and bit_or_iff: \<open>\<And>n. bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
    20     and bit_xor_iff: \<open>\<And>n. bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
    21 begin
    22 
    23 text \<open>
    24   We want the bitwise operations to bind slightly weaker
    25   than \<open>+\<close> and \<open>-\<close>.
    26   For the sake of code generation
    27   the operations \<^const>\<open>and\<close>, \<^const>\<open>or\<close> and \<^const>\<open>xor\<close>
    28   are specified as definitional class operations.
    29 \<close>
    30 
    31 lemma stable_imp_drop_eq:
    32   \<open>drop_bit n a = a\<close> if \<open>a div 2 = a\<close>
    33   by (induction n) (simp_all add: that)
    34 
    35 sublocale "and": semilattice \<open>(AND)\<close>
    36   by standard (auto simp add: bit_eq_iff bit_and_iff)
    37 
    38 sublocale or: semilattice_neutr \<open>(OR)\<close> 0
    39   by standard (auto simp add: bit_eq_iff bit_or_iff)
    40 
    41 sublocale xor: comm_monoid \<open>(XOR)\<close> 0
    42   by standard (auto simp add: bit_eq_iff bit_xor_iff)
    43 
    44 lemma zero_and_eq [simp]:
    45   "0 AND a = 0"
    46   by (simp add: bit_eq_iff bit_and_iff)
    47 
    48 lemma and_zero_eq [simp]:
    49   "a AND 0 = 0"
    50   by (simp add: bit_eq_iff bit_and_iff)
    51 
    52 lemma one_and_eq [simp]:
    53   "1 AND a = of_bool (odd a)"
    54   by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff)
    55 
    56 lemma and_one_eq [simp]:
    57   "a AND 1 = of_bool (odd a)"
    58   using one_and_eq [of a] by (simp add: ac_simps)
    59 
    60 lemma one_or_eq [simp]:
    61   "1 OR a = a + of_bool (even a)"
    62   by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff)
    63 
    64 lemma or_one_eq [simp]:
    65   "a OR 1 = a + of_bool (even a)"
    66   using one_or_eq [of a] by (simp add: ac_simps)
    67 
    68 lemma one_xor_eq [simp]:
    69   "1 XOR a = a + of_bool (even a) - of_bool (odd a)"
    70   by (simp add: bit_eq_iff bit_xor_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff odd_bit_iff_bit_pred elim: oddE)
    71 
    72 lemma xor_one_eq [simp]:
    73   "a XOR 1 = a + of_bool (even a) - of_bool (odd a)"
    74   using one_xor_eq [of a] by (simp add: ac_simps)
    75 
    76 lemma take_bit_and [simp]:
    77   \<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close>
    78   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff)
    79 
    80 lemma take_bit_or [simp]:
    81   \<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close>
    82   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff)
    83 
    84 lemma take_bit_xor [simp]:
    85   \<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close>
    86   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff)
    87 
    88 end
    89 
    90 class ring_bit_operations = semiring_bit_operations + ring_parity +
    91   fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
    92   assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
    93   assumes minus_eq_not_minus_1: \<open>- a = NOT (a - 1)\<close>
    94 begin
    95 
    96 text \<open>
    97   For the sake of code generation \<^const>\<open>not\<close> is specified as
    98   definitional class operation.  Note that \<^const>\<open>not\<close> has no
    99   sensible definition for unlimited but only positive bit strings
   100   (type \<^typ>\<open>nat\<close>).
   101 \<close>
   102 
   103 lemma bits_minus_1_mod_2_eq [simp]:
   104   \<open>(- 1) mod 2 = 1\<close>
   105   by (simp add: mod_2_eq_odd)
   106 
   107 lemma not_eq_complement:
   108   \<open>NOT a = - a - 1\<close>
   109   using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp
   110 
   111 lemma minus_eq_not_plus_1:
   112   \<open>- a = NOT a + 1\<close>
   113   using not_eq_complement [of a] by simp
   114 
   115 lemma bit_minus_iff:
   116   \<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close>
   117   by (simp add: minus_eq_not_minus_1 bit_not_iff)
   118 
   119 lemma even_not_iff [simp]:
   120   "even (NOT a) \<longleftrightarrow> odd a"
   121   using bit_not_iff [of a 0] by auto
   122 
   123 lemma bit_not_exp_iff:
   124   \<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close>
   125   by (auto simp add: bit_not_iff bit_exp_iff)
   126 
   127 lemma bit_minus_1_iff [simp]:
   128   \<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close>
   129   by (simp add: bit_minus_iff)
   130 
   131 lemma bit_minus_exp_iff:
   132   \<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close>
   133   oops
   134 
   135 lemma bit_minus_2_iff [simp]:
   136   \<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close>
   137   by (simp add: bit_minus_iff bit_1_iff)
   138 
   139 lemma not_one [simp]:
   140   "NOT 1 = - 2"
   141   by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff)
   142 
   143 sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close>
   144   apply standard
   145   apply (simp add: bit_eq_iff bit_and_iff)
   146   apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff)
   147   done
   148 
   149 sublocale bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
   150   rewrites \<open>bit.xor = (XOR)\<close>
   151 proof -
   152   interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
   153     apply standard
   154          apply (simp_all add: bit_eq_iff bit_and_iff bit_or_iff bit_not_iff)
   155       apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff)
   156     done
   157   show \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close>
   158     by standard
   159   show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close>
   160     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)
   161          apply (simp_all add: bit_exp_iff, simp_all add: bit_def)
   162         apply (metis local.bit_exp_iff local.bits_div_by_0)
   163        apply (metis local.bit_exp_iff local.bits_div_by_0)
   164     done
   165 qed
   166 
   167 lemma push_bit_minus:
   168   \<open>push_bit n (- a) = - push_bit n a\<close>
   169   by (simp add: push_bit_eq_mult)
   170 
   171 lemma take_bit_not_take_bit:
   172   \<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close>
   173   by (auto simp add: bit_eq_iff bit_take_bit_iff bit_not_iff)
   174 
   175 lemma take_bit_not_iff:
   176   "take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b"
   177   apply (simp add: bit_eq_iff bit_not_iff bit_take_bit_iff)
   178   apply (simp add: bit_exp_iff)
   179   apply (use local.exp_eq_0_imp_not_bit in blast)
   180   done
   181 
   182 definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
   183   where \<open>set_bit n a = a OR 2 ^ n\<close>
   184 
   185 definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
   186   where \<open>unset_bit n a = a AND NOT (2 ^ n)\<close>
   187 
   188 definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
   189   where \<open>flip_bit n a = a XOR 2 ^ n\<close>
   190 
   191 lemma bit_set_bit_iff:
   192   \<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close>
   193   by (auto simp add: set_bit_def bit_or_iff bit_exp_iff)
   194 
   195 lemma even_set_bit_iff:
   196   \<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close>
   197   using bit_set_bit_iff [of m a 0] by auto
   198 
   199 lemma bit_unset_bit_iff:
   200   \<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close>
   201   by (auto simp add: unset_bit_def bit_and_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit)
   202 
   203 lemma even_unset_bit_iff:
   204   \<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close>
   205   using bit_unset_bit_iff [of m a 0] by auto
   206 
   207 lemma bit_flip_bit_iff:
   208   \<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close>
   209   by (auto simp add: flip_bit_def bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit)
   210 
   211 lemma even_flip_bit_iff:
   212   \<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close>
   213   using bit_flip_bit_iff [of m a 0] by auto
   214 
   215 lemma set_bit_0 [simp]:
   216   \<open>set_bit 0 a = 1 + 2 * (a div 2)\<close>
   217 proof (rule bit_eqI)
   218   fix m
   219   assume *: \<open>2 ^ m \<noteq> 0\<close>
   220   then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close>
   221     by (simp add: bit_set_bit_iff bit_double_iff even_bit_succ_iff)
   222       (cases m, simp_all)
   223 qed
   224 
   225 lemma set_bit_Suc [simp]:
   226   \<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close>
   227 proof (rule bit_eqI)
   228   fix m
   229   assume *: \<open>2 ^ m \<noteq> 0\<close>
   230   show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close>
   231   proof (cases m)
   232     case 0
   233     then show ?thesis
   234       by (simp add: even_set_bit_iff)
   235   next
   236     case (Suc m)
   237     with * have \<open>2 ^ m \<noteq> 0\<close>
   238       using mult_2 by auto
   239     show ?thesis
   240       by (cases a rule: parity_cases)
   241         (simp_all add: bit_set_bit_iff bit_double_iff even_bit_succ_iff *,
   242         simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close>)
   243   qed
   244 qed
   245 
   246 lemma unset_bit_0 [simp]:
   247   \<open>unset_bit 0 a = 2 * (a div 2)\<close>
   248 proof (rule bit_eqI)
   249   fix m
   250   assume *: \<open>2 ^ m \<noteq> 0\<close>
   251   then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close>
   252     by (simp add: bit_unset_bit_iff bit_double_iff)
   253       (cases m, simp_all)
   254 qed
   255 
   256 lemma unset_bit_Suc [simp]:
   257   \<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close>
   258 proof (rule bit_eqI)
   259   fix m
   260   assume *: \<open>2 ^ m \<noteq> 0\<close>
   261   then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close>
   262   proof (cases m)
   263     case 0
   264     then show ?thesis
   265       by (simp add: even_unset_bit_iff)
   266   next
   267     case (Suc m)
   268     show ?thesis
   269       by (cases a rule: parity_cases)
   270         (simp_all add: bit_unset_bit_iff bit_double_iff even_bit_succ_iff *,
   271          simp_all add: Suc)
   272   qed
   273 qed
   274 
   275 lemma flip_bit_0 [simp]:
   276   \<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close>
   277 proof (rule bit_eqI)
   278   fix m
   279   assume *: \<open>2 ^ m \<noteq> 0\<close>
   280   then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close>
   281     by (simp add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff)
   282       (cases m, simp_all)
   283 qed
   284 
   285 lemma flip_bit_Suc [simp]:
   286   \<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close>
   287 proof (rule bit_eqI)
   288   fix m
   289   assume *: \<open>2 ^ m \<noteq> 0\<close>
   290   show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close>
   291   proof (cases m)
   292     case 0
   293     then show ?thesis
   294       by (simp add: even_flip_bit_iff)
   295   next
   296     case (Suc m)
   297     with * have \<open>2 ^ m \<noteq> 0\<close>
   298       using mult_2 by auto
   299     show ?thesis
   300       by (cases a rule: parity_cases)
   301         (simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff,
   302         simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close>)
   303   qed
   304 qed
   305 
   306 end
   307 
   308 
   309 subsubsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
   310 
   311 locale zip_nat = single: abel_semigroup f
   312     for f :: "bool \<Rightarrow> bool \<Rightarrow> bool"  (infixl \<open>\<^bold>*\<close> 70) +
   313   assumes end_of_bits: \<open>\<not> False \<^bold>* False\<close>
   314 begin
   315 
   316 function F :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>  (infixl \<open>\<^bold>\<times>\<close> 70)
   317   where \<open>m \<^bold>\<times> n = (if m = 0 \<and> n = 0 then 0
   318     else of_bool (odd m \<^bold>* odd n) + 2 * ((m div 2) \<^bold>\<times> (n div 2)))\<close>
   319   by auto
   320 
   321 termination
   322   by (relation "measure (case_prod (+))") auto
   323 
   324 declare F.simps [simp del]
   325 
   326 lemma rec:
   327   "m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2"
   328 proof (cases \<open>m = 0 \<and> n = 0\<close>)
   329   case True
   330   then have \<open>m \<^bold>\<times> n = 0\<close>
   331     using True by (simp add: F.simps [of 0 0])
   332   moreover have \<open>(m div 2) \<^bold>\<times> (n div 2) = m \<^bold>\<times> n\<close>
   333     using True by simp
   334   ultimately show ?thesis
   335     using True by (simp add: end_of_bits)
   336 next
   337   case False
   338   then show ?thesis
   339     by (auto simp add: ac_simps F.simps [of m n])
   340 qed
   341 
   342 lemma bit_eq_iff:
   343   \<open>bit (m \<^bold>\<times> n) q \<longleftrightarrow> bit m q \<^bold>* bit n q\<close>
   344 proof (induction q arbitrary: m n)
   345   case 0
   346   then show ?case
   347     by (simp add: rec [of m n])
   348 next
   349   case (Suc n)
   350   then show ?case
   351     by (simp add: rec [of m n])
   352 qed
   353 
   354 sublocale abel_semigroup F
   355   by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps)
   356 
   357 end
   358 
   359 instantiation nat :: semiring_bit_operations
   360 begin
   361 
   362 global_interpretation and_nat: zip_nat \<open>(\<and>)\<close>
   363   defines and_nat = and_nat.F
   364   by standard auto
   365 
   366 global_interpretation and_nat: semilattice \<open>(AND) :: nat \<Rightarrow> nat \<Rightarrow> nat\<close>
   367 proof (rule semilattice.intro, fact and_nat.abel_semigroup_axioms, standard)
   368   show \<open>n AND n = n\<close> for n :: nat
   369     by (simp add: bit_eq_iff and_nat.bit_eq_iff)
   370 qed
   371 
   372 global_interpretation or_nat: zip_nat \<open>(\<or>)\<close>
   373   defines or_nat = or_nat.F
   374   by standard auto
   375 
   376 global_interpretation or_nat: semilattice \<open>(OR) :: nat \<Rightarrow> nat \<Rightarrow> nat\<close>
   377 proof (rule semilattice.intro, fact or_nat.abel_semigroup_axioms, standard)
   378   show \<open>n OR n = n\<close> for n :: nat
   379     by (simp add: bit_eq_iff or_nat.bit_eq_iff)
   380 qed
   381 
   382 global_interpretation xor_nat: zip_nat \<open>(\<noteq>)\<close>
   383   defines xor_nat = xor_nat.F
   384   by standard auto
   385 
   386 instance proof
   387   fix m n q :: nat
   388   show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close>
   389     by (fact and_nat.bit_eq_iff)
   390   show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close>
   391     by (fact or_nat.bit_eq_iff)
   392   show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close>
   393     by (fact xor_nat.bit_eq_iff)
   394 qed
   395 
   396 end
   397 
   398 lemma Suc_0_and_eq [simp]:
   399   \<open>Suc 0 AND n = of_bool (odd n)\<close>
   400   using one_and_eq [of n] by simp
   401 
   402 lemma and_Suc_0_eq [simp]:
   403   \<open>n AND Suc 0 = of_bool (odd n)\<close>
   404   using and_one_eq [of n] by simp
   405 
   406 lemma Suc_0_or_eq [simp]:
   407   \<open>Suc 0 OR n = n + of_bool (even n)\<close>
   408   using one_or_eq [of n] by simp
   409 
   410 lemma or_Suc_0_eq [simp]:
   411   \<open>n OR Suc 0 = n + of_bool (even n)\<close>
   412   using or_one_eq [of n] by simp
   413 
   414 lemma Suc_0_xor_eq [simp]:
   415   \<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close>
   416   using one_xor_eq [of n] by simp
   417 
   418 lemma xor_Suc_0_eq [simp]:
   419   \<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close>
   420   using xor_one_eq [of n] by simp
   421 
   422 
   423 subsubsection \<open>Instance \<^typ>\<open>int\<close>\<close>
   424 
   425 locale zip_int = single: abel_semigroup f
   426   for f :: \<open>bool \<Rightarrow> bool \<Rightarrow> bool\<close>  (infixl \<open>\<^bold>*\<close> 70)
   427 begin
   428 
   429 function F :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close>  (infixl \<open>\<^bold>\<times>\<close> 70)
   430   where \<open>k \<^bold>\<times> l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
   431     then - of_bool (odd k \<^bold>* odd l)
   432     else of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\<times> (l div 2)))\<close>
   433   by auto
   434 
   435 termination
   436   by (relation "measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))") auto
   437 
   438 declare F.simps [simp del]
   439 
   440 lemma rec:
   441   \<open>k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\<times> (l div 2))\<close>
   442 proof (cases \<open>k \<in> {0, - 1} \<and> l \<in> {0, - 1}\<close>)
   443   case True
   444   then have \<open>(k div 2) \<^bold>\<times> (l div 2) = k \<^bold>\<times> l\<close>
   445     by auto
   446   moreover have \<open>of_bool (odd k \<^bold>* odd l) = - (k \<^bold>\<times> l)\<close>
   447     using True by (simp add: F.simps [of k l])
   448   ultimately show ?thesis by simp
   449 next
   450   case False
   451   then show ?thesis
   452     by (auto simp add: ac_simps F.simps [of k l])
   453 qed
   454 
   455 lemma bit_eq_iff:
   456   \<open>bit (k \<^bold>\<times> l) n \<longleftrightarrow> bit k n \<^bold>* bit l n\<close>
   457 proof (induction n arbitrary: k l)
   458   case 0
   459   then show ?case
   460     by (simp add: rec [of k l])
   461 next
   462   case (Suc n)
   463   then show ?case
   464     by (simp add: rec [of k l])
   465 qed
   466 
   467 sublocale abel_semigroup F
   468   by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps)
   469 
   470 end
   471 
   472 instantiation int :: ring_bit_operations
   473 begin
   474 
   475 global_interpretation and_int: zip_int "(\<and>)"
   476   defines and_int = and_int.F
   477   by standard
   478 
   479 global_interpretation and_int: semilattice "(AND) :: int \<Rightarrow> int \<Rightarrow> int"
   480 proof (rule semilattice.intro, fact and_int.abel_semigroup_axioms, standard)
   481   show "k AND k = k" for k :: int
   482     by (simp add: bit_eq_iff and_int.bit_eq_iff)
   483 qed
   484 
   485 global_interpretation or_int: zip_int "(\<or>)"
   486   defines or_int = or_int.F
   487   by standard
   488 
   489 global_interpretation or_int: semilattice "(OR) :: int \<Rightarrow> int \<Rightarrow> int"
   490 proof (rule semilattice.intro, fact or_int.abel_semigroup_axioms, standard)
   491   show "k OR k = k" for k :: int
   492     by (simp add: bit_eq_iff or_int.bit_eq_iff)
   493 qed
   494 
   495 global_interpretation xor_int: zip_int "(\<noteq>)"
   496   defines xor_int = xor_int.F
   497   by standard
   498 
   499 definition not_int :: \<open>int \<Rightarrow> int\<close>
   500   where \<open>not_int k = - k - 1\<close>
   501 
   502 lemma not_int_rec:
   503   "NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int
   504   by (auto simp add: not_int_def elim: oddE)
   505 
   506 lemma even_not_iff_int:
   507   \<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int
   508   by (simp add: not_int_def)
   509 
   510 lemma not_int_div_2:
   511   \<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int
   512   by (simp add: not_int_def)
   513 
   514 lemma bit_not_iff_int:
   515   \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close>
   516     for k :: int
   517   by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int)
   518 
   519 instance proof
   520   fix k l :: int and n :: nat
   521   show \<open>- k = NOT (k - 1)\<close>
   522     by (simp add: not_int_def)
   523   show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close>
   524     by (fact and_int.bit_eq_iff)
   525   show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close>
   526     by (fact or_int.bit_eq_iff)
   527   show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close>
   528     by (fact xor_int.bit_eq_iff)
   529 qed (simp_all add: bit_not_iff_int)
   530 
   531 end
   532 
   533 
   534 subsubsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close>
   535 
   536 unbundle integer.lifting natural.lifting
   537 
   538 context
   539   includes lifting_syntax
   540 begin
   541 
   542 lemma transfer_rule_bit_integer [transfer_rule]:
   543   \<open>((pcr_integer :: int \<Rightarrow> integer \<Rightarrow> bool) ===> (=)) bit bit\<close>
   544   by (unfold bit_def) transfer_prover
   545 
   546 lemma transfer_rule_bit_natural [transfer_rule]:
   547   \<open>((pcr_natural :: nat \<Rightarrow> natural \<Rightarrow> bool) ===> (=)) bit bit\<close>
   548   by (unfold bit_def) transfer_prover
   549 
   550 end
   551 
   552 instantiation integer :: ring_bit_operations
   553 begin
   554 
   555 lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close>
   556   is not .
   557 
   558 lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
   559   is \<open>and\<close> .
   560 
   561 lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
   562   is or .
   563 
   564 lift_definition xor_integer ::  \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
   565   is xor .
   566 
   567 instance proof
   568   fix k l :: \<open>integer\<close> and n :: nat
   569   show \<open>- k = NOT (k - 1)\<close>
   570     by transfer (simp add: minus_eq_not_minus_1)
   571   show \<open>bit (NOT k) n \<longleftrightarrow> (2 :: integer) ^ n \<noteq> 0 \<and> \<not> bit k n\<close>
   572     by transfer (fact bit_not_iff)
   573   show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close>
   574     by transfer (fact and_int.bit_eq_iff)
   575   show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close>
   576     by transfer (fact or_int.bit_eq_iff)
   577   show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close>
   578     by transfer (fact xor_int.bit_eq_iff)
   579 qed
   580 
   581 end
   582 
   583 instantiation natural :: semiring_bit_operations
   584 begin
   585 
   586 lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
   587   is \<open>and\<close> .
   588 
   589 lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
   590   is or .
   591 
   592 lift_definition xor_natural ::  \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
   593   is xor .
   594 
   595 instance proof
   596   fix m n :: \<open>natural\<close> and q :: nat
   597   show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close>
   598     by transfer (fact and_nat.bit_eq_iff)
   599   show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close>
   600     by transfer (fact or_nat.bit_eq_iff)
   601   show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close>
   602     by transfer (fact xor_nat.bit_eq_iff)
   603 qed
   604 
   605 end
   606 
   607 lifting_update integer.lifting
   608 lifting_forget integer.lifting
   609 
   610 lifting_update natural.lifting
   611 lifting_forget natural.lifting
   612 
   613 end