src/HOL/Library/Bit_Operations.thy
author haftmann
Mon, 10 Aug 2020 15:34:55 +0000
changeset 72130 9e5862223442
parent 72083 3ec876181527
child 72187 e4aecb0c7296
permissions -rw-r--r--
dedicated symbols for code generation, to pave way for generic conversions from and to word
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     1
(*  Author:  Florian Haftmann, TUM
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     2
*)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     3
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
     4
section \<open>Bit operations in suitable algebraic structures\<close>
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     5
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     6
theory Bit_Operations
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     7
  imports
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
     8
    "HOL-Library.Boolean_Algebra"
71095
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
     9
    Main
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    10
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    11
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    12
lemma nat_take_bit_eq:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    13
  \<open>nat (take_bit n k) = take_bit n (nat k)\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    14
  if \<open>k \<ge> 0\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    15
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    16
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    17
lemma take_bit_eq_self:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    18
  \<open>take_bit m n = n\<close> if \<open>n < 2 ^ m\<close> for n :: nat
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    19
  using that by (simp add: take_bit_eq_mod)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    20
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    21
lemma horner_sum_of_bool_2_less:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    22
  \<open>(horner_sum of_bool 2 bs :: int) < 2 ^ length bs\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    23
proof -
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    24
  have \<open>(\<Sum>n = 0..<length bs. of_bool (bs ! n) * (2::int) ^ n) \<le> (\<Sum>n = 0..<length bs. 2 ^ n)\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    25
    by (rule sum_mono) simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    26
  also have \<open>\<dots> = 2 ^ length bs - 1\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    27
    by (induction bs) simp_all
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    28
  finally show ?thesis
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    29
    by (simp add: horner_sum_eq_sum)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    30
qed
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    31
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72083
diff changeset
    32
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
    33
subsection \<open>Bit operations\<close>
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    34
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    35
class semiring_bit_operations = semiring_bit_shifts +
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    36
  fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>AND\<close> 64)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    37
    and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>OR\<close>  59)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    38
    and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>XOR\<close> 59)
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
    39
    and mask :: \<open>nat \<Rightarrow> 'a\<close>
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
    40
  assumes bit_and_iff: \<open>\<And>n. bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
    41
    and bit_or_iff: \<open>\<And>n. bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
    42
    and bit_xor_iff: \<open>\<And>n. bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
    43
    and mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close>
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    44
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    45
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    46
text \<open>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    47
  We want the bitwise operations to bind slightly weaker
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
    48
  than \<open>+\<close> and \<open>-\<close>.
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    49
  For the sake of code generation
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    50
  the operations \<^const>\<open>and\<close>, \<^const>\<open>or\<close> and \<^const>\<open>xor\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    51
  are specified as definitional class operations.
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    52
\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
    53
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    54
sublocale "and": semilattice \<open>(AND)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    55
  by standard (auto simp add: bit_eq_iff bit_and_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    56
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    57
sublocale or: semilattice_neutr \<open>(OR)\<close> 0
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    58
  by standard (auto simp add: bit_eq_iff bit_or_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    59
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    60
sublocale xor: comm_monoid \<open>(XOR)\<close> 0
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    61
  by standard (auto simp add: bit_eq_iff bit_xor_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    62
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    63
lemma even_and_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    64
  \<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    65
  using bit_and_iff [of a b 0] by auto
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    66
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    67
lemma even_or_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    68
  \<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    69
  using bit_or_iff [of a b 0] by auto
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    70
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    71
lemma even_xor_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    72
  \<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    73
  using bit_xor_iff [of a b 0] by auto
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
    74
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    75
lemma zero_and_eq [simp]:
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    76
  "0 AND a = 0"
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    77
  by (simp add: bit_eq_iff bit_and_iff)
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    78
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    79
lemma and_zero_eq [simp]:
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    80
  "a AND 0 = 0"
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    81
  by (simp add: bit_eq_iff bit_and_iff)
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    82
71921
a238074c5a9d avoid overaggressive default simp rules
haftmann
parents: 71823
diff changeset
    83
lemma one_and_eq:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
    84
  "1 AND a = a mod 2"
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    85
  by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff)
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    86
71921
a238074c5a9d avoid overaggressive default simp rules
haftmann
parents: 71823
diff changeset
    87
lemma and_one_eq:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
    88
  "a AND 1 = a mod 2"
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    89
  using one_and_eq [of a] by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    90
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
    91
lemma one_or_eq:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    92
  "1 OR a = a + of_bool (even a)"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    93
  by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff)
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    94
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
    95
lemma or_one_eq:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    96
  "a OR 1 = a + of_bool (even a)"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
    97
  using one_or_eq [of a] by (simp add: ac_simps)
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
    98
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
    99
lemma one_xor_eq:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   100
  "1 XOR a = a + of_bool (even a) - of_bool (odd a)"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   101
  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)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   102
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
   103
lemma xor_one_eq:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   104
  "a XOR 1 = a + of_bool (even a) - of_bool (odd a)"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   105
  using one_xor_eq [of a] by (simp add: ac_simps)
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
   106
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   107
lemma take_bit_and [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   108
  \<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   109
  by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   110
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   111
lemma take_bit_or [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   112
  \<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   113
  by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   114
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   115
lemma take_bit_xor [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   116
  \<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   117
  by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   118
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   119
lemma bit_mask_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   120
  \<open>bit (mask m) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   121
  by (simp add: mask_eq_exp_minus_1 bit_mask_iff)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   122
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   123
lemma even_mask_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   124
  \<open>even (mask n) \<longleftrightarrow> n = 0\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   125
  using bit_mask_iff [of n 0] by auto
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   126
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   127
lemma mask_0 [simp]:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   128
  \<open>mask 0 = 0\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   129
  by (simp add: mask_eq_exp_minus_1)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   130
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   131
lemma mask_Suc_0 [simp]:
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   132
  \<open>mask (Suc 0) = 1\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   133
  by (simp add: mask_eq_exp_minus_1 add_implies_diff sym)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   134
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   135
lemma mask_Suc_exp:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   136
  \<open>mask (Suc n) = 2 ^ n OR mask n\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   137
  by (rule bit_eqI)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   138
    (auto simp add: bit_or_iff bit_mask_iff bit_exp_iff not_less le_less_Suc_eq)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   139
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   140
lemma mask_Suc_double:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   141
  \<open>mask (Suc n) = 1 OR 2 * mask n\<close>
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   142
proof (rule bit_eqI)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   143
  fix q
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   144
  assume \<open>2 ^ q \<noteq> 0\<close>
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   145
  show \<open>bit (mask (Suc n)) q \<longleftrightarrow> bit (1 OR 2 * mask n) q\<close>
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   146
    by (cases q)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   147
      (simp_all add: even_mask_iff even_or_iff bit_or_iff bit_mask_iff bit_exp_iff bit_double_iff not_less le_less_Suc_eq bit_1_iff, auto simp add: mult_2)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   148
qed
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   149
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   150
lemma mask_numeral:
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   151
  \<open>mask (numeral n) = 1 + 2 * mask (pred_numeral n)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   152
  by (simp add: numeral_eq_Suc mask_Suc_double one_or_eq ac_simps)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   153
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
   154
lemma take_bit_eq_mask:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   155
  \<open>take_bit n a = a AND mask n\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   156
  by (rule bit_eqI)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   157
    (auto simp add: bit_take_bit_iff bit_and_iff bit_mask_iff)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
   158
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   159
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   160
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   161
class ring_bit_operations = semiring_bit_operations + ring_parity +
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   162
  fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   163
  assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   164
  assumes minus_eq_not_minus_1: \<open>- a = NOT (a - 1)\<close>
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   165
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   166
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   167
text \<open>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   168
  For the sake of code generation \<^const>\<open>not\<close> is specified as
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   169
  definitional class operation.  Note that \<^const>\<open>not\<close> has no
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   170
  sensible definition for unlimited but only positive bit strings
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   171
  (type \<^typ>\<open>nat\<close>).
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   172
\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   173
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   174
lemma bits_minus_1_mod_2_eq [simp]:
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   175
  \<open>(- 1) mod 2 = 1\<close>
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   176
  by (simp add: mod_2_eq_odd)
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   177
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   178
lemma not_eq_complement:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   179
  \<open>NOT a = - a - 1\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   180
  using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   181
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   182
lemma minus_eq_not_plus_1:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   183
  \<open>- a = NOT a + 1\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   184
  using not_eq_complement [of a] by simp
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   185
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   186
lemma bit_minus_iff:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   187
  \<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   188
  by (simp add: minus_eq_not_minus_1 bit_not_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   189
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   190
lemma even_not_iff [simp]:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   191
  "even (NOT a) \<longleftrightarrow> odd a"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   192
  using bit_not_iff [of a 0] by auto
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   193
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   194
lemma bit_not_exp_iff:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   195
  \<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   196
  by (auto simp add: bit_not_iff bit_exp_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   197
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   198
lemma bit_minus_1_iff [simp]:
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   199
  \<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close>
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   200
  by (simp add: bit_minus_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   201
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   202
lemma bit_minus_exp_iff:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   203
  \<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   204
  oops
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   205
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   206
lemma bit_minus_2_iff [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   207
  \<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   208
  by (simp add: bit_minus_iff bit_1_iff)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   209
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   210
lemma not_one [simp]:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   211
  "NOT 1 = - 2"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   212
  by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   213
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   214
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   215
  apply standard
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   216
  apply (simp add: bit_eq_iff bit_and_iff)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   217
  apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   218
  done
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   219
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   220
sublocale bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   221
  rewrites \<open>bit.xor = (XOR)\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   222
proof -
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   223
  interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   224
    apply standard
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
   225
         apply (simp_all add: bit_eq_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
   226
       apply (auto simp add: bit_and_iff bit_or_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   227
    done
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   228
  show \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   229
    by standard
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   230
  show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
   231
    apply (simp add: fun_eq_iff bit_eq_iff bit.xor_def)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
   232
    apply (auto simp add: bit_and_iff bit_or_iff bit_not_iff bit_xor_iff exp_eq_0_imp_not_bit)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   233
    done
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   234
qed
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   235
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   236
lemma and_eq_not_not_or:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   237
  \<open>a AND b = NOT (NOT a OR NOT b)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   238
  by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   239
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   240
lemma or_eq_not_not_and:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   241
  \<open>a OR b = NOT (NOT a AND NOT b)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   242
  by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   243
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   244
lemma not_add_distrib:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   245
  \<open>NOT (a + b) = NOT a - b\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   246
  by (simp add: not_eq_complement algebra_simps)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   247
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   248
lemma not_diff_distrib:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   249
  \<open>NOT (a - b) = NOT a + b\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   250
  using not_add_distrib [of a \<open>- b\<close>] by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   251
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
   252
lemma push_bit_minus:
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
   253
  \<open>push_bit n (- a) = - push_bit n a\<close>
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
   254
  by (simp add: push_bit_eq_mult)
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
   255
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   256
lemma take_bit_not_take_bit:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   257
  \<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   258
  by (auto simp add: bit_eq_iff bit_take_bit_iff bit_not_iff)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   259
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   260
lemma take_bit_not_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   261
  "take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b"
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   262
  apply (simp add: bit_eq_iff bit_not_iff bit_take_bit_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   263
  apply (simp add: bit_exp_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   264
  apply (use local.exp_eq_0_imp_not_bit in blast)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   265
  done
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   266
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   267
lemma mask_eq_take_bit_minus_one:
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   268
  \<open>mask n = take_bit n (- 1)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   269
  by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute)
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   270
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   271
lemma take_bit_minus_one_eq_mask:
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   272
  \<open>take_bit n (- 1) = mask n\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   273
  by (simp add: mask_eq_take_bit_minus_one)
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   274
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   275
lemma minus_exp_eq_not_mask:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   276
  \<open>- (2 ^ n) = NOT (mask n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   277
  by (rule bit_eqI) (simp add: bit_minus_iff bit_not_iff flip: mask_eq_exp_minus_1)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   278
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   279
lemma push_bit_minus_one_eq_not_mask:
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   280
  \<open>push_bit n (- 1) = NOT (mask n)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   281
  by (simp add: push_bit_eq_mult minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   282
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   283
lemma take_bit_not_mask_eq_0:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   284
  \<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   285
  by (rule bit_eqI) (use that in \<open>simp add: bit_take_bit_iff bit_not_iff bit_mask_iff\<close>)
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
   286
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   287
lemma take_bit_mask [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   288
  \<open>take_bit m (mask n) = mask (min m n)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   289
  by (simp add: mask_eq_take_bit_minus_one)
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   290
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   291
definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   292
  where \<open>set_bit n a = a OR push_bit n 1\<close>
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   293
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   294
definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   295
  where \<open>unset_bit n a = a AND NOT (push_bit n 1)\<close>
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   296
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   297
definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   298
  where \<open>flip_bit n a = a XOR push_bit n 1\<close>
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   299
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   300
lemma bit_set_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   301
  \<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   302
  by (auto simp add: set_bit_def push_bit_of_1 bit_or_iff bit_exp_iff)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   303
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   304
lemma even_set_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   305
  \<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   306
  using bit_set_bit_iff [of m a 0] by auto
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   307
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   308
lemma bit_unset_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   309
  \<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   310
  by (auto simp add: unset_bit_def push_bit_of_1 bit_and_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   311
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   312
lemma even_unset_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   313
  \<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   314
  using bit_unset_bit_iff [of m a 0] by auto
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   315
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   316
lemma bit_flip_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   317
  \<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71986
diff changeset
   318
  by (auto simp add: flip_bit_def push_bit_of_1 bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   319
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   320
lemma even_flip_bit_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   321
  \<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   322
  using bit_flip_bit_iff [of m a 0] by auto
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   323
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   324
lemma set_bit_0 [simp]:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   325
  \<open>set_bit 0 a = 1 + 2 * (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   326
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   327
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   328
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   329
  then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   330
    by (simp add: bit_set_bit_iff bit_double_iff even_bit_succ_iff)
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   331
      (cases m, simp_all add: bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   332
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   333
71821
541e68d1a964 less aggressive default simp rules
haftmann
parents: 71804
diff changeset
   334
lemma set_bit_Suc:
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   335
  \<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   336
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   337
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   338
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   339
  show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   340
  proof (cases m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   341
    case 0
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   342
    then show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   343
      by (simp add: even_set_bit_iff)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   344
  next
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   345
    case (Suc m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   346
    with * have \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   347
      using mult_2 by auto
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   348
    show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   349
      by (cases a rule: parity_cases)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   350
        (simp_all add: bit_set_bit_iff bit_double_iff even_bit_succ_iff *,
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   351
        simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   352
  qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   353
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   354
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   355
lemma unset_bit_0 [simp]:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   356
  \<open>unset_bit 0 a = 2 * (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   357
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   358
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   359
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   360
  then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   361
    by (simp add: bit_unset_bit_iff bit_double_iff)
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   362
      (cases m, simp_all add: bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   363
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   364
71821
541e68d1a964 less aggressive default simp rules
haftmann
parents: 71804
diff changeset
   365
lemma unset_bit_Suc:
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   366
  \<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   367
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   368
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   369
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   370
  then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   371
  proof (cases m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   372
    case 0
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   373
    then show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   374
      by (simp add: even_unset_bit_iff)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   375
  next
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   376
    case (Suc m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   377
    show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   378
      by (cases a rule: parity_cases)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   379
        (simp_all add: bit_unset_bit_iff bit_double_iff even_bit_succ_iff *,
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   380
         simp_all add: Suc bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   381
  qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   382
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   383
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   384
lemma flip_bit_0 [simp]:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   385
  \<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   386
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   387
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   388
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   389
  then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   390
    by (simp add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff)
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   391
      (cases m, simp_all add: bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   392
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   393
71821
541e68d1a964 less aggressive default simp rules
haftmann
parents: 71804
diff changeset
   394
lemma flip_bit_Suc:
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   395
  \<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   396
proof (rule bit_eqI)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   397
  fix m
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   398
  assume *: \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   399
  show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   400
  proof (cases m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   401
    case 0
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   402
    then show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   403
      by (simp add: even_flip_bit_iff)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   404
  next
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   405
    case (Suc m)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   406
    with * have \<open>2 ^ m \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   407
      using mult_2 by auto
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   408
    show ?thesis
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   409
      by (cases a rule: parity_cases)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   410
        (simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff,
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   411
        simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   412
  qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   413
qed
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   414
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   415
lemma flip_bit_eq_if:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   416
  \<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   417
  by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   418
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   419
lemma take_bit_set_bit_eq:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   420
  \<open>take_bit n (set_bit m a) = (if n \<le> m then take_bit n a else set_bit m (take_bit n a))\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   421
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   422
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   423
lemma take_bit_unset_bit_eq:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   424
  \<open>take_bit n (unset_bit m a) = (if n \<le> m then take_bit n a else unset_bit m (take_bit n a))\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   425
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   426
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   427
lemma take_bit_flip_bit_eq:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   428
  \<open>take_bit n (flip_bit m a) = (if n \<le> m then take_bit n a else flip_bit m (take_bit n a))\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   429
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   430
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   431
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   432
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   433
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
   434
subsection \<open>Instance \<^typ>\<open>int\<close>\<close>
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   435
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   436
instantiation int :: ring_bit_operations
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   437
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   438
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   439
definition not_int :: \<open>int \<Rightarrow> int\<close>
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   440
  where \<open>not_int k = - k - 1\<close>
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   441
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   442
lemma not_int_rec:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   443
  "NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   444
  by (auto simp add: not_int_def elim: oddE)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   445
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   446
lemma even_not_iff_int:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   447
  \<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   448
  by (simp add: not_int_def)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   449
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   450
lemma not_int_div_2:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   451
  \<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int
572ab9e64e18 simplified logical constructions
haftmann
parents: 71419
diff changeset
   452
  by (simp add: not_int_def)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   453
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   454
lemma bit_not_int_iff:
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   455
  \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close>
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   456
    for k :: int
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71442
diff changeset
   457
  by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int bit_Suc)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   458
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   459
function and_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   460
  where \<open>(k::int) AND l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   461
    then - of_bool (odd k \<and> odd l)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   462
    else of_bool (odd k \<and> odd l) + 2 * ((k div 2) AND (l div 2)))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   463
  by auto
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   464
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   465
termination
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   466
  by (relation \<open>measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>) auto
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   467
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   468
declare and_int.simps [simp del]
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   469
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   470
lemma and_int_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   471
  \<open>k AND l = of_bool (odd k \<and> odd l) + 2 * ((k div 2) AND (l div 2))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   472
    for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   473
proof (cases \<open>k \<in> {0, - 1} \<and> l \<in> {0, - 1}\<close>)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   474
  case True
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   475
  then show ?thesis
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   476
    by auto (simp_all add: and_int.simps)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   477
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   478
  case False
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   479
  then show ?thesis
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   480
    by (auto simp add: ac_simps and_int.simps [of k l])
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   481
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   482
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   483
lemma bit_and_int_iff:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   484
  \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   485
proof (induction n arbitrary: k l)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   486
  case 0
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   487
  then show ?case
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   488
    by (simp add: and_int_rec [of k l])
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   489
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   490
  case (Suc n)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   491
  then show ?case
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   492
    by (simp add: and_int_rec [of k l] bit_Suc)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   493
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   494
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   495
lemma even_and_iff_int:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   496
  \<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   497
  using bit_and_int_iff [of k l 0] by auto
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   498
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   499
definition or_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   500
  where \<open>k OR l = NOT (NOT k AND NOT l)\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   501
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   502
lemma or_int_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   503
  \<open>k OR l = of_bool (odd k \<or> odd l) + 2 * ((k div 2) OR (l div 2))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   504
  for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   505
  using and_int_rec [of \<open>NOT k\<close> \<open>NOT l\<close>]
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   506
  by (simp add: or_int_def even_not_iff_int not_int_div_2)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   507
    (simp add: not_int_def)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   508
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   509
lemma bit_or_int_iff:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   510
  \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   511
  by (simp add: or_int_def bit_not_int_iff bit_and_int_iff)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   512
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   513
definition xor_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   514
  where \<open>k XOR l = k AND NOT l OR NOT k AND l\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   515
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   516
lemma xor_int_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   517
  \<open>k XOR l = of_bool (odd k \<noteq> odd l) + 2 * ((k div 2) XOR (l div 2))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   518
  for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   519
  by (simp add: xor_int_def or_int_rec [of \<open>k AND NOT l\<close> \<open>NOT k AND l\<close>] even_and_iff_int even_not_iff_int)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   520
    (simp add: and_int_rec [of \<open>NOT k\<close> \<open>l\<close>] and_int_rec [of \<open>k\<close> \<open>NOT l\<close>] not_int_div_2)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   521
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   522
lemma bit_xor_int_iff:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   523
  \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   524
  by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   525
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   526
definition mask_int :: \<open>nat \<Rightarrow> int\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   527
  where \<open>mask n = (2 :: int) ^ n - 1\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   528
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   529
instance proof
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   530
  fix k l :: int and n :: nat
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   531
  show \<open>- k = NOT (k - 1)\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
   532
    by (simp add: not_int_def)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   533
  show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   534
    by (fact bit_and_int_iff)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   535
  show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   536
    by (fact bit_or_int_iff)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
   537
  show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   538
    by (fact bit_xor_int_iff)
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
   539
qed (simp_all add: bit_not_int_iff mask_int_def)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   540
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   541
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   542
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   543
lemma disjunctive_add:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   544
  \<open>k + l = k OR l\<close> if \<open>\<And>n. \<not> bit k n \<or> \<not> bit l n\<close> for k l :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   545
  \<comment> \<open>TODO: may integrate (indirectly) into \<^class>\<open>semiring_bits\<close> premises\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   546
proof (rule bit_eqI)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   547
  fix n
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   548
  from that have \<open>bit (k + l) n \<longleftrightarrow> bit k n \<or> bit l n\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   549
  proof (induction n arbitrary: k l)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   550
    case 0
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   551
    from this [of 0] show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   552
      by auto
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   553
  next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   554
    case (Suc n)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   555
    have \<open>bit ((k + l) div 2) n \<longleftrightarrow> bit (k div 2 + l div 2) n\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   556
      using Suc.prems [of 0] div_add1_eq [of k l] by auto
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   557
    also have \<open>bit (k div 2 + l div 2) n \<longleftrightarrow> bit (k div 2) n \<or> bit (l div 2) n\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   558
      by (rule Suc.IH) (use Suc.prems in \<open>simp flip: bit_Suc\<close>)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   559
    finally show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   560
      by (simp add: bit_Suc)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   561
  qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   562
  also have \<open>\<dots> \<longleftrightarrow> bit (k OR l) n\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   563
    by (simp add: bit_or_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   564
  finally show \<open>bit (k + l) n \<longleftrightarrow> bit (k OR l) n\<close> .
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   565
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   566
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   567
lemma disjunctive_diff:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   568
  \<open>k - l = k AND NOT l\<close> if \<open>\<And>n. bit l n \<Longrightarrow> bit k n\<close> for k l :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   569
proof -
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   570
  have \<open>NOT k + l = NOT k OR l\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   571
    by (rule disjunctive_add) (auto simp add: bit_not_iff dest: that)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   572
  then have \<open>NOT (NOT k + l) = NOT (NOT k OR l)\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   573
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   574
  then show ?thesis
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   575
    by (simp add: not_add_distrib)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   576
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   577
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   578
lemma mask_nonnegative_int [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   579
  \<open>mask n \<ge> (0::int)\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   580
  by (simp add: mask_eq_exp_minus_1)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   581
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   582
lemma not_mask_negative_int [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   583
  \<open>\<not> mask n < (0::int)\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   584
  by (simp add: not_less)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   585
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   586
lemma not_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   587
  \<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   588
  by (simp add: not_int_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   589
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   590
lemma not_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   591
  \<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   592
  by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   593
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   594
lemma and_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   595
  \<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   596
proof (induction k arbitrary: l rule: int_bit_induct)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   597
  case zero
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   598
  then show ?case
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   599
    by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   600
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   601
  case minus
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   602
  then show ?case
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   603
    by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   604
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   605
  case (even k)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   606
  then show ?case
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   607
    using and_int_rec [of \<open>k * 2\<close> l] by (simp add: pos_imp_zdiv_nonneg_iff)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   608
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   609
  case (odd k)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   610
  from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   611
    by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   612
  then have \<open>0 \<le> (1 + k * 2) div 2 AND l div 2 \<longleftrightarrow> 0 \<le> (1 + k * 2) div 2\<or> 0 \<le> l div 2\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   613
    by simp
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   614
  with and_int_rec [of \<open>1 + k * 2\<close> l]
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   615
  show ?case
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   616
    by auto
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   617
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   618
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   619
lemma and_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   620
  \<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   621
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   622
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   623
lemma and_less_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   624
  \<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   625
using that proof (induction k arbitrary: l rule: int_bit_induct)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   626
  case zero
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   627
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   628
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   629
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   630
  case minus
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   631
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   632
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   633
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   634
  case (even k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   635
  from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   636
  show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   637
    by (simp add: and_int_rec [of _ l])
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   638
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   639
  case (odd k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   640
  from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   641
  show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   642
    by (simp add: and_int_rec [of _ l])
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   643
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   644
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   645
lemma or_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   646
  \<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   647
  by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   648
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   649
lemma or_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   650
  \<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   651
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   652
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   653
lemma or_greater_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   654
  \<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   655
using that proof (induction k arbitrary: l rule: int_bit_induct)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   656
  case zero
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   657
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   658
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   659
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   660
  case minus
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   661
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   662
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   663
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   664
  case (even k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   665
  from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   666
  show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   667
    by (simp add: or_int_rec [of _ l])
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   668
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   669
  case (odd k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   670
  from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   671
  show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   672
    by (simp add: or_int_rec [of _ l])
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   673
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   674
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   675
lemma xor_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   676
  \<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   677
  by (simp only: bit.xor_def or_nonnegative_int_iff) auto
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   678
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   679
lemma xor_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   680
  \<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   681
  by (subst Not_eq_iff [symmetric]) (auto simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   682
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   683
lemma set_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   684
  \<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   685
  by (simp add: set_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   686
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   687
lemma set_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   688
  \<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   689
  by (simp add: set_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   690
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   691
lemma unset_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   692
  \<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   693
  by (simp add: unset_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   694
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   695
lemma unset_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   696
  \<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   697
  by (simp add: unset_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   698
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   699
lemma flip_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   700
  \<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   701
  by (simp add: flip_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   702
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   703
lemma flip_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   704
  \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   705
  by (simp add: flip_bit_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
   706
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   707
lemma set_bit_greater_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   708
  \<open>set_bit n k \<ge> k\<close> for k :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   709
  by (simp add: set_bit_def or_greater_eq)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   710
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   711
lemma unset_bit_less_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   712
  \<open>unset_bit n k \<le> k\<close> for k :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   713
  by (simp add: unset_bit_def and_less_eq)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   714
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   715
lemma set_bit_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   716
  \<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   717
proof (rule bit_eqI)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   718
  fix m
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   719
  show \<open>bit (set_bit n k) m \<longleftrightarrow> bit (k + of_bool (\<not> bit k n) * 2 ^ n) m\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   720
  proof (cases \<open>m = n\<close>)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   721
    case True
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   722
    then show ?thesis
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   723
      apply (simp add: bit_set_bit_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   724
      apply (simp add: bit_iff_odd div_plus_div_distrib_dvd_right)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   725
      done
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   726
  next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   727
    case False
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   728
    then show ?thesis
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   729
      apply (clarsimp simp add: bit_set_bit_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   730
      apply (subst disjunctive_add)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   731
      apply (clarsimp simp add: bit_exp_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   732
      apply (clarsimp simp add: bit_or_iff bit_exp_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   733
      done
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   734
  qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   735
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   736
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   737
lemma unset_bit_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   738
  \<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   739
proof (rule bit_eqI)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   740
  fix m
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   741
  show \<open>bit (unset_bit n k) m \<longleftrightarrow> bit (k - of_bool (bit k n) * 2 ^ n) m\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   742
  proof (cases \<open>m = n\<close>)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   743
    case True
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   744
    then show ?thesis
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   745
      apply (simp add: bit_unset_bit_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   746
      apply (simp add: bit_iff_odd)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   747
      using div_plus_div_distrib_dvd_right [of \<open>2 ^ n\<close> \<open>- (2 ^ n)\<close> k]
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   748
      apply (simp add: dvd_neg_div)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   749
      done
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   750
  next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   751
    case False
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   752
    then show ?thesis
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   753
      apply (clarsimp simp add: bit_unset_bit_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   754
      apply (subst disjunctive_diff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   755
      apply (clarsimp simp add: bit_exp_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   756
      apply (clarsimp simp add: bit_and_iff bit_not_iff bit_exp_iff)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   757
      done
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   758
  qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   759
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
   760
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
   761
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   762
subsection \<open>Bit concatenation\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   763
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   764
definition concat_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   765
  where \<open>concat_bit n k l = (k AND mask n) OR push_bit n l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   766
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   767
lemma bit_concat_bit_iff:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   768
  \<open>bit (concat_bit m k l) n \<longleftrightarrow> n < m \<and> bit k n \<or> m \<le> n \<and> bit l (n - m)\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   769
  by (simp add: concat_bit_def bit_or_iff bit_and_iff bit_mask_iff bit_push_bit_iff ac_simps)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   770
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   771
lemma concat_bit_eq:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   772
  \<open>concat_bit n k l = take_bit n k + push_bit n l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   773
  by (simp add: concat_bit_def take_bit_eq_mask
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   774
    bit_and_iff bit_mask_iff bit_push_bit_iff disjunctive_add)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   775
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   776
lemma concat_bit_0 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   777
  \<open>concat_bit 0 k l = l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   778
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   779
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   780
lemma concat_bit_Suc:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   781
  \<open>concat_bit (Suc n) k l = k mod 2 + 2 * concat_bit n (k div 2) l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   782
  by (simp add: concat_bit_eq take_bit_Suc push_bit_double)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   783
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   784
lemma concat_bit_of_zero_1 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   785
  \<open>concat_bit n 0 l = push_bit n l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   786
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   787
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   788
lemma concat_bit_of_zero_2 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   789
  \<open>concat_bit n k 0 = take_bit n k\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   790
  by (simp add: concat_bit_def take_bit_eq_mask)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   791
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   792
lemma concat_bit_nonnegative_iff [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   793
  \<open>concat_bit n k l \<ge> 0 \<longleftrightarrow> l \<ge> 0\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   794
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   795
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   796
lemma concat_bit_negative_iff [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   797
  \<open>concat_bit n k l < 0 \<longleftrightarrow> l < 0\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   798
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   799
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   800
lemma concat_bit_assoc:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   801
  \<open>concat_bit n k (concat_bit m l r) = concat_bit (m + n) (concat_bit n k l) r\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   802
  by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   803
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   804
lemma concat_bit_assoc_sym:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   805
  \<open>concat_bit m (concat_bit n k l) r = concat_bit (min m n) k (concat_bit (m - n) l r)\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   806
  by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps min_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   807
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   808
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   809
subsection \<open>Taking bit with sign propagation\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   810
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   811
definition signed_take_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   812
  where \<open>signed_take_bit n k = concat_bit n k (- of_bool (bit k n))\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   813
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   814
lemma signed_take_bit_eq:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   815
  \<open>signed_take_bit n k = take_bit n k\<close> if \<open>\<not> bit k n\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   816
  using that by (simp add: signed_take_bit_def)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   817
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   818
lemma signed_take_bit_eq_or:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   819
  \<open>signed_take_bit n k = take_bit n k OR NOT (mask n)\<close> if \<open>bit k n\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   820
  using that by (simp add: signed_take_bit_def concat_bit_def take_bit_eq_mask push_bit_minus_one_eq_not_mask)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   821
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   822
lemma signed_take_bit_0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   823
  \<open>signed_take_bit 0 k = - (k mod 2)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   824
  by (simp add: signed_take_bit_def odd_iff_mod_2_eq_one)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   825
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   826
lemma mask_half_int:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   827
  \<open>mask n div 2 = (mask (n - 1) :: int)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   828
  by (cases n) (simp_all add: mask_eq_exp_minus_1 algebra_simps)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   829
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   830
lemma signed_take_bit_Suc:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   831
  \<open>signed_take_bit (Suc n) k = k mod 2 + 2 * signed_take_bit n (k div 2)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   832
  by (unfold signed_take_bit_def or_int_rec [of \<open>take_bit (Suc n) k\<close>])
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   833
    (simp add: bit_Suc concat_bit_Suc even_or_iff even_mask_iff odd_iff_mod_2_eq_one not_int_div_2 mask_half_int)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   834
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   835
lemma signed_take_bit_rec:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   836
  \<open>signed_take_bit n k = (if n = 0 then - (k mod 2) else k mod 2 + 2 * signed_take_bit (n - 1) (k div 2))\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   837
  by (cases n) (simp_all add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   838
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   839
lemma bit_signed_take_bit_iff:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   840
  \<open>bit (signed_take_bit m k) n = bit k (min m n)\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   841
  by (simp add: signed_take_bit_def bit_or_iff bit_concat_bit_iff bit_not_iff bit_mask_iff min_def)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   842
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   843
text \<open>Modulus centered around 0\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   844
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   845
lemma signed_take_bit_eq_take_bit_minus:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   846
  \<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   847
proof (cases \<open>bit k n\<close>)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   848
  case True
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   849
  have \<open>signed_take_bit n k = take_bit (Suc n) k OR NOT (mask (Suc n))\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   850
    by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff less_Suc_eq True)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   851
  then have \<open>signed_take_bit n k = take_bit (Suc n) k + NOT (mask (Suc n))\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   852
    by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   853
  with True show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   854
    by (simp flip: minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   855
next
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   856
  case False
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   857
  then show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   858
    by (simp add: bit_eq_iff bit_take_bit_iff bit_signed_take_bit_iff min_def)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   859
      (auto intro: bit_eqI simp add: less_Suc_eq)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   860
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   861
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   862
lemma signed_take_bit_eq_take_bit_shift:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   863
  \<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   864
proof -
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   865
  have *: \<open>take_bit n k OR 2 ^ n = take_bit n k + 2 ^ n\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   866
    by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   867
  have \<open>take_bit n k - 2 ^ n = take_bit n k + NOT (mask n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   868
    by (simp add: minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   869
  also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   870
    by (rule disjunctive_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   871
      (simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   872
  finally have **: \<open>take_bit n k - 2 ^ n = take_bit n k OR NOT (mask n)\<close> .
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   873
  have \<open>take_bit (Suc n) (k + 2 ^ n) = take_bit (Suc n) (take_bit (Suc n) k + take_bit (Suc n) (2 ^ n))\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   874
    by (simp only: take_bit_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   875
  also have \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   876
    by (simp add: take_bit_Suc_from_most)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   877
  finally have \<open>take_bit (Suc n) (k + 2 ^ n) = take_bit (Suc n) (2 ^ (n + of_bool (bit k n)) + take_bit n k)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   878
    by (simp add: ac_simps)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   879
  also have \<open>2 ^ (n + of_bool (bit k n)) + take_bit n k = 2 ^ (n + of_bool (bit k n)) OR take_bit n k\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   880
    by (rule disjunctive_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   881
      (auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   882
  finally show ?thesis
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   883
    using * **
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   884
    by (simp add: signed_take_bit_def concat_bit_Suc min_def ac_simps)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   885
      (simp add: concat_bit_def take_bit_eq_mask push_bit_minus_one_eq_not_mask ac_simps)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   886
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   887
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   888
lemma signed_take_bit_of_0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   889
  \<open>signed_take_bit n 0 = 0\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   890
  by (simp add: signed_take_bit_def)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   891
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   892
lemma signed_take_bit_of_minus_1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   893
  \<open>signed_take_bit n (- 1) = - 1\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   894
  by (simp add: signed_take_bit_def concat_bit_def push_bit_minus_one_eq_not_mask take_bit_minus_one_eq_mask)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   895
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   896
lemma signed_take_bit_eq_iff_take_bit_eq:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   897
  \<open>signed_take_bit n k = signed_take_bit n l \<longleftrightarrow> take_bit (Suc n) k = take_bit (Suc n) l\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   898
proof (cases \<open>bit k n \<longleftrightarrow> bit l n\<close>)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   899
  case True
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   900
  moreover have \<open>take_bit n k OR NOT (mask n) = take_bit n k - 2 ^ n\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   901
    for k :: int
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   902
    by (auto simp add: disjunctive_add [symmetric] bit_not_iff bit_mask_iff bit_take_bit_iff minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   903
  ultimately show ?thesis
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   904
    by (simp add: signed_take_bit_def take_bit_Suc_from_most concat_bit_eq)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   905
next
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   906
  case False
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   907
  then have \<open>signed_take_bit n k \<noteq> signed_take_bit n l\<close> and \<open>take_bit (Suc n) k \<noteq> take_bit (Suc n) l\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   908
    by (auto simp add: bit_eq_iff bit_take_bit_iff bit_signed_take_bit_iff min_def)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   909
  then show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   910
    by simp
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   911
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   912
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   913
lemma signed_take_bit_signed_take_bit [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   914
  \<open>signed_take_bit m (signed_take_bit n k) = signed_take_bit (min m n) k\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   915
  by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_or_iff bit_not_iff bit_mask_iff bit_take_bit_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   916
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   917
lemma take_bit_signed_take_bit:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   918
  \<open>take_bit m (signed_take_bit n k) = take_bit m k\<close> if \<open>m \<le> Suc n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   919
  using that by (rule le_SucE; intro bit_eqI)
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
   920
   (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   921
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   922
lemma signed_take_bit_take_bit:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   923
  \<open>signed_take_bit m (take_bit n k) = (if n \<le> m then take_bit n else signed_take_bit m) k\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   924
  by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   925
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   926
lemma signed_take_bit_nonnegative_iff [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   927
  \<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   928
  by (simp add: signed_take_bit_def not_less concat_bit_def)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   929
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   930
lemma signed_take_bit_negative_iff [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   931
  \<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
   932
  by (simp add: signed_take_bit_def not_less concat_bit_def)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   933
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   934
lemma signed_take_bit_greater_eq:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   935
  \<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   936
  using that take_bit_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>]
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   937
  by (simp add: signed_take_bit_eq_take_bit_shift)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   938
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   939
lemma signed_take_bit_less_eq:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   940
  \<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   941
  using that take_bit_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>]
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   942
  by (simp add: signed_take_bit_eq_take_bit_shift)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   943
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   944
lemma signed_take_bit_Suc_1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   945
  \<open>signed_take_bit (Suc n) 1 = 1\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   946
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   947
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   948
lemma signed_take_bit_Suc_bit0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   949
  \<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * 2\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   950
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   951
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   952
lemma signed_take_bit_Suc_bit1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   953
  \<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + 1\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   954
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   955
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   956
lemma signed_take_bit_Suc_minus_bit0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   957
  \<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * 2\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   958
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   959
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   960
lemma signed_take_bit_Suc_minus_bit1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   961
  \<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + 1\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   962
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   963
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   964
lemma signed_take_bit_numeral_bit0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   965
  \<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   966
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   967
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   968
lemma signed_take_bit_numeral_bit1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   969
  \<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + 1\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   970
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   971
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   972
lemma signed_take_bit_numeral_minus_bit0 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   973
  \<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * 2\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   974
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   975
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   976
lemma signed_take_bit_numeral_minus_bit1 [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   977
  \<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + 1\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   978
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   979
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   980
lemma signed_take_bit_code [code]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   981
  \<open>signed_take_bit n k =
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
   982
  (let l = take_bit (Suc n) k
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   983
   in if bit l n then l - (push_bit n 2) else l)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   984
proof -
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
   985
  have *: \<open>(take_bit (Suc n) k) - 2 * 2 ^ n = take_bit (Suc n) k OR NOT (mask (Suc n))\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   986
    apply (subst disjunctive_add [symmetric])
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
   987
    apply (simp_all add: bit_and_iff bit_mask_iff bit_not_iff bit_take_bit_iff)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   988
    apply (simp flip: minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   989
    done
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   990
  show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   991
    by (rule bit_eqI)
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
   992
     (auto simp add: Let_def bit_and_iff bit_signed_take_bit_iff push_bit_eq_mult min_def not_le
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
   993
       bit_mask_iff bit_exp_iff less_Suc_eq * bit_or_iff bit_take_bit_iff bit_not_iff)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   994
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   995
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
   996
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
   997
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   998
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
   999
instantiation nat :: semiring_bit_operations
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1000
begin
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1001
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1002
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1003
  where \<open>m AND n = nat (int m AND int n)\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1004
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1005
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1006
  where \<open>m OR n = nat (int m OR int n)\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1007
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1008
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1009
  where \<open>m XOR n = nat (int m XOR int n)\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1010
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1011
definition mask_nat :: \<open>nat \<Rightarrow> nat\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1012
  where \<open>mask n = (2 :: nat) ^ n - 1\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1013
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1014
instance proof
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1015
  fix m n q :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1016
  show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1017
    by (auto simp add: and_nat_def bit_and_iff less_le bit_eq_iff)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1018
  show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1019
    by (auto simp add: or_nat_def bit_or_iff less_le bit_eq_iff)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1020
  show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1021
    by (auto simp add: xor_nat_def bit_xor_iff less_le bit_eq_iff)
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1022
qed (simp add: mask_nat_def)
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1023
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1024
end
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1025
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1026
lemma and_nat_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1027
  \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1028
  by (simp add: and_nat_def and_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1029
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1030
lemma or_nat_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1031
  \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1032
  by (simp add: or_nat_def or_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1033
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1034
lemma xor_nat_rec:
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1035
  \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1036
  by (simp add: xor_nat_def xor_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1037
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1038
lemma Suc_0_and_eq [simp]:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1039
  \<open>Suc 0 AND n = n mod 2\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1040
  using one_and_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1041
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1042
lemma and_Suc_0_eq [simp]:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1043
  \<open>n AND Suc 0 = n mod 2\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1044
  using and_one_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1045
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1046
lemma Suc_0_or_eq:
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1047
  \<open>Suc 0 OR n = n + of_bool (even n)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1048
  using one_or_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1049
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1050
lemma or_Suc_0_eq:
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1051
  \<open>n OR Suc 0 = n + of_bool (even n)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1052
  using or_one_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1053
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1054
lemma Suc_0_xor_eq:
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1055
  \<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1056
  using one_xor_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1057
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71821
diff changeset
  1058
lemma xor_Suc_0_eq:
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1059
  \<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1060
  using xor_one_eq [of n] by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1061
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1062
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
  1063
subsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close>
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1064
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1065
unbundle integer.lifting natural.lifting
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1066
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1067
instantiation integer :: ring_bit_operations
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1068
begin
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1069
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1070
lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1071
  is not .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1072
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1073
lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1074
  is \<open>and\<close> .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1075
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1076
lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1077
  is or .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1078
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1079
lift_definition xor_integer ::  \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1080
  is xor .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1081
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1082
lift_definition mask_integer :: \<open>nat \<Rightarrow> integer\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1083
  is mask .
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1084
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1085
instance by (standard; transfer)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1086
  (simp_all add: minus_eq_not_minus_1 mask_eq_exp_minus_1
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1087
    bit_not_iff bit_and_iff bit_or_iff bit_xor_iff)
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1088
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1089
end
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1090
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1091
lemma [code]:
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1092
  \<open>mask n = 2 ^ n - (1::integer)\<close>
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1093
  by (simp add: mask_eq_exp_minus_1)
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1094
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1095
instantiation natural :: semiring_bit_operations
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1096
begin
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1097
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1098
lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1099
  is \<open>and\<close> .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1100
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1101
lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1102
  is or .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1103
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1104
lift_definition xor_natural ::  \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close>
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1105
  is xor .
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1106
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1107
lift_definition mask_natural :: \<open>nat \<Rightarrow> natural\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1108
  is mask .
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1109
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1110
instance by (standard; transfer)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1111
  (simp_all add: mask_eq_exp_minus_1 bit_and_iff bit_or_iff bit_xor_iff)
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1112
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1113
end
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1114
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1115
lemma [code]:
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1116
  \<open>integer_of_natural (mask n) = mask n\<close>
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1117
  by transfer (simp add: mask_eq_exp_minus_1 of_nat_diff)
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1118
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1119
lifting_update integer.lifting
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1120
lifting_forget integer.lifting
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1121
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1122
lifting_update natural.lifting
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1123
lifting_forget natural.lifting
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1124
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1125
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1126
subsection \<open>Key ideas of bit operations\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1127
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1128
text \<open>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1129
  When formalizing bit operations, it is tempting to represent
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1130
  bit values as explicit lists over a binary type. This however
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1131
  is a bad idea, mainly due to the inherent ambiguities in
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1132
  representation concerning repeating leading bits.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1133
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1134
  Hence this approach avoids such explicit lists altogether
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1135
  following an algebraic path:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1136
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1137
  \<^item> Bit values are represented by numeric types: idealized
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1138
    unbounded bit values can be represented by type \<^typ>\<open>int\<close>,
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1139
    bounded bit values by quotient types over \<^typ>\<open>int\<close>.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1140
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1141
  \<^item> (A special case are idealized unbounded bit values ending
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1142
    in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1143
    only support a restricted set of operations).
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1144
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1145
  \<^item> From this idea follows that
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1146
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1147
      \<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1148
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1149
      \<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1150
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1151
  \<^item> Concerning bounded bit values, iterated shifts to the left
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1152
    may result in eliminating all bits by shifting them all
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1153
    beyond the boundary.  The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1154
    represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1155
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
  1156
  \<^item> The projection on a single bit is then @{thm bit_iff_odd [where ?'a = int, no_vars]}.
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1157
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1158
  \<^item> This leads to the most fundamental properties of bit values:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1159
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1160
      \<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1161
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1162
      \<^item> Induction rule: @{thm bits_induct [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1163
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1164
  \<^item> Typical operations are characterized as follows:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1165
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1166
      \<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1167
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
  1168
      \<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]}
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1169
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1170
      \<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1171
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1172
      \<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1173
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1174
      \<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1175
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1176
      \<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1177
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1178
      \<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1179
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1180
      \<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1181
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1182
      \<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1183
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1184
      \<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1185
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1186
      \<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1187
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1188
      \<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]}
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1189
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1190
      \<^item> Bit concatenation: @{thm concat_bit_def [no_vars]}
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1191
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1192
      \<^item> Truncation centered towards \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]}
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1193
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1194
      \<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]}
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1195
\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  1196
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  1197
end