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