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