src/HOL/Bit_Operations.thy
author paulson <lp15@cam.ac.uk>
Tue, 16 Jan 2024 13:40:09 +0000
changeset 79492 c1b0f64eb865
parent 79489 1e19abf373ac
child 79531 22a137a6de44
permissions -rw-r--r--
A few new results (mostly brought in from other developments)
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
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
     7
  imports Presburger Groups_List
79117
7476818dfd5d generalized
haftmann
parents: 79116
diff changeset
     8
begin                 
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
     9
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    10
subsection \<open>Abstract bit structures\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    11
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    12
class semiring_bits = semiring_parity +
79480
c7cb1bf6efa0 consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents: 79117
diff changeset
    13
  assumes bit_induct [case_names stable rec]:
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    14
    \<open>(\<And>a. a div 2 = a \<Longrightarrow> P a)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    15
     \<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a))
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    16
        \<Longrightarrow> P a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    17
  assumes bits_div_0 [simp]: \<open>0 div a = 0\<close>
79481
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    18
    and bits_div_by_0 [simp]: \<open>a div 0 = 0\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    19
    and bits_div_by_1 [simp]: \<open>a div 1 = a\<close>
79488
62d8c6c08fb2 consolidated lemma name
haftmann
parents: 79481
diff changeset
    20
    and even_half_succ_eq [simp]: \<open>even a \<Longrightarrow> (1 + a) div 2 = a div 2\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    21
    and even_mask_div_iff: \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0 \<or> m \<le> n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    22
    and exp_div_exp_eq: \<open>2 ^ m div 2 ^ n = of_bool (2 ^ m \<noteq> 0 \<and> m \<ge> n) * 2 ^ (m - n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    23
    and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    24
    and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    25
    and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    26
    and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    27
    and even_mult_exp_div_exp_iff: \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    28
  fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    29
  assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    30
begin
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    31
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    32
text \<open>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    33
  Having \<^const>\<open>bit\<close> as definitional class operation
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    34
  takes into account that specific instances can be implemented
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    35
  differently wrt. code generation.
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    36
\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    37
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    38
lemma bits_1_div_2 [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    39
  \<open>1 div 2 = 0\<close>
79488
62d8c6c08fb2 consolidated lemma name
haftmann
parents: 79481
diff changeset
    40
  using even_half_succ_eq [of 0] by simp
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    41
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    42
lemma bits_1_div_exp [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    43
  \<open>1 div 2 ^ n = of_bool (n = 0)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    44
  using div_exp_eq [of 1 1] by (cases n) simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    45
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    46
lemma even_succ_div_exp [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    47
  \<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    48
proof (cases n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    49
  case 0
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    50
  with that show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    51
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    52
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    53
  case (Suc n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    54
  with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    55
  proof (induction n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    56
    case 0
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    57
    then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    58
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    59
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    60
    case (Suc n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    61
    then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    62
      using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    63
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    64
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    65
  with Suc show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    66
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    67
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    68
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    69
lemma even_succ_mod_exp [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    70
  \<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
    71
  using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] div_mult_mod_eq [of a \<open>2 ^ n\<close>] that
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
    72
  by simp (metis (full_types) add.left_commute add_left_imp_eq)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    73
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    74
lemma bits_mod_by_1 [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    75
  \<open>a mod 1 = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    76
  using div_mult_mod_eq [of a 1] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    77
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    78
lemma bits_mod_0 [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    79
  \<open>0 mod a = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    80
  using div_mult_mod_eq [of 0 a] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    81
79481
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    82
lemma mod_exp_div_exp_eq_0 [simp]:
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    83
  \<open>a mod 2 ^ n div 2 ^ n = 0\<close>
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    84
proof (induction n arbitrary: a)
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    85
  case 0
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    86
  then show ?case
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    87
    by simp
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    88
next
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    89
  case (Suc n)
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    90
  then have \<open>a div 2 ^ 1 mod 2 ^ n div 2 ^ n = 0\<close> .
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    91
  then show ?case
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    92
    using div_exp_eq [of _ 1 n] div_exp_mod_exp_eq [of a 1 n]
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    93
    by simp
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    94
qed
8205977e9e2c simplified specification of type class
haftmann
parents: 79480
diff changeset
    95
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
    96
lemma bit_0:
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    97
  \<open>bit a 0 \<longleftrightarrow> odd a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    98
  by (simp add: bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
    99
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   100
lemma bit_Suc:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   101
  \<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   102
  using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   103
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   104
lemma bit_rec:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   105
  \<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   106
  by (cases n) (simp_all add: bit_Suc bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   107
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   108
lemma bit_0_eq [simp]:
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
   109
  \<open>bit 0 = \<bottom>\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   110
  by (simp add: fun_eq_iff bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   111
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   112
context
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   113
  fixes a
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   114
  assumes stable: \<open>a div 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   115
begin
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   116
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   117
lemma bits_stable_imp_add_self:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   118
  \<open>a + a mod 2 = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   119
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   120
  have \<open>a div 2 * 2 + a mod 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   121
    by (fact div_mult_mod_eq)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   122
  then have \<open>a * 2 + a mod 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   123
    by (simp add: stable)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   124
  then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   125
    by (simp add: mult_2_right ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   126
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   127
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   128
lemma stable_imp_bit_iff_odd:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   129
  \<open>bit a n \<longleftrightarrow> odd a\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   130
  by (induction n) (simp_all add: stable bit_Suc bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   131
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   132
end
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   133
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   134
lemma bit_iff_idd_imp_stable:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   135
  \<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close>
79480
c7cb1bf6efa0 consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents: 79117
diff changeset
   136
using that proof (induction a rule: bit_induct)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   137
  case (stable a)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   138
  then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   139
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   140
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   141
  case (rec a b)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   142
  from rec.prems [of 1] have [simp]: \<open>b = odd a\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   143
    by (simp add: rec.hyps bit_Suc bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   144
  from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   145
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   146
  have \<open>bit a n \<longleftrightarrow> odd a\<close> for n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   147
    using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   148
  then have \<open>a div 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   149
    by (rule rec.IH)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   150
  then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   151
    by (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   152
  also have \<open>\<dots> = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   153
    using mult_div_mod_eq [of 2 a]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   154
    by (simp add: of_bool_odd_eq_mod_2)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   155
  finally show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   156
    using \<open>a div 2 = a\<close> by (simp add: hyp)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   157
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   158
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   159
lemma exp_eq_0_imp_not_bit:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   160
  \<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   161
  using that by (simp add: bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   162
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   163
definition possible_bit :: \<open>'a itself \<Rightarrow> nat \<Rightarrow> bool\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   164
  where \<open>possible_bit TYPE('a) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close>
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
   165
  \<comment> \<open>This auxiliary avoids non-termination with extensionality.\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   166
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   167
lemma possible_bit_0 [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   168
  \<open>possible_bit TYPE('a) 0\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   169
  by (simp add: possible_bit_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   170
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   171
lemma fold_possible_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   172
  \<open>2 ^ n = 0 \<longleftrightarrow> \<not> possible_bit TYPE('a) n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   173
  by (simp add: possible_bit_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   174
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   175
lemma bit_imp_possible_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   176
  \<open>possible_bit TYPE('a) n\<close> if \<open>bit a n\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   177
  using that by (auto simp add: possible_bit_def exp_eq_0_imp_not_bit)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   178
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   179
lemma impossible_bit:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   180
  \<open>\<not> bit a n\<close> if \<open>\<not> possible_bit TYPE('a) n\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   181
  using that by (blast dest: bit_imp_possible_bit)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   182
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   183
lemma possible_bit_less_imp:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   184
  \<open>possible_bit TYPE('a) j\<close> if \<open>possible_bit TYPE('a) i\<close> \<open>j \<le> i\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   185
  using power_add [of 2 j \<open>i - j\<close>] that mult_not_zero [of \<open>2 ^ j\<close> \<open>2 ^ (i - j)\<close>]
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   186
  by (simp add: possible_bit_def)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   187
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   188
lemma possible_bit_min [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   189
  \<open>possible_bit TYPE('a) (min i j) \<longleftrightarrow> possible_bit TYPE('a) i \<or> possible_bit TYPE('a) j\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   190
  by (auto simp add: min_def elim: possible_bit_less_imp)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   191
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   192
lemma bit_eqI:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   193
  \<open>a = b\<close> if \<open>\<And>n. possible_bit TYPE('a) n \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   194
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   195
  have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   196
  proof (cases \<open>2 ^ n = 0\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   197
    case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   198
    then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   199
      by (simp add: exp_eq_0_imp_not_bit)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   200
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   201
    case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   202
    then show ?thesis
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   203
      by (rule that[unfolded possible_bit_def])
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   204
  qed
79480
c7cb1bf6efa0 consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents: 79117
diff changeset
   205
  then show ?thesis proof (induction a arbitrary: b rule: bit_induct)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   206
    case (stable a)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   207
    from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   208
      by (simp add: bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   209
    have \<open>b div 2 = b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   210
    proof (rule bit_iff_idd_imp_stable)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   211
      fix n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   212
      from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   213
        by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   214
      also have \<open>bit a n \<longleftrightarrow> odd a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   215
        using stable by (simp add: stable_imp_bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   216
      finally show \<open>bit b n \<longleftrightarrow> odd b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   217
        by (simp add: **)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   218
    qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   219
    from ** have \<open>a mod 2 = b mod 2\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   220
      by (simp add: mod2_eq_if)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   221
    then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   222
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   223
    then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   224
      by (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   225
    with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   226
      by (simp add: bits_stable_imp_add_self)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   227
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   228
    case (rec a p)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   229
    from rec.prems [of 0] have [simp]: \<open>p = odd b\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   230
      by (simp add: bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   231
    from rec.hyps have \<open>bit a n \<longleftrightarrow> bit (b div 2) n\<close> for n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   232
      using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   233
    then have \<open>a = b div 2\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   234
      by (rule rec.IH)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   235
    then have \<open>2 * a = 2 * (b div 2)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   236
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   237
    then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   238
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   239
    also have \<open>\<dots> = b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   240
      by (fact mod_mult_div_eq)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   241
    finally show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   242
      by (auto simp add: mod2_eq_if)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   243
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   244
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   245
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   246
lemma bit_eq_iff:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   247
  \<open>a = b \<longleftrightarrow> (\<forall>n. possible_bit TYPE('a) n \<longrightarrow> bit a n \<longleftrightarrow> bit b n)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   248
  by (auto intro: bit_eqI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   249
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   250
named_theorems bit_simps \<open>Simplification rules for \<^const>\<open>bit\<close>\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   251
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   252
lemma bit_exp_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   253
  \<open>bit (2 ^ m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> m = n\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   254
  by (auto simp add: bit_iff_odd exp_div_exp_eq possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   255
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   256
lemma bit_1_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   257
  \<open>bit 1 n \<longleftrightarrow> n = 0\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   258
  using bit_exp_iff [of 0 n]
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   259
  by auto
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   260
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   261
lemma bit_2_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   262
  \<open>bit 2 n \<longleftrightarrow> possible_bit TYPE('a) 1 \<and> n = 1\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   263
  using bit_exp_iff [of 1 n] by auto
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   264
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   265
lemma even_bit_succ_iff:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   266
  \<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   267
  using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   268
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   269
lemma bit_double_iff [bit_simps]:
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   270
  \<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> possible_bit TYPE('a) n\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   271
  using even_mult_exp_div_exp_iff [of a 1 n]
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   272
  by (cases n, auto simp add: bit_iff_odd ac_simps possible_bit_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   273
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   274
lemma odd_bit_iff_bit_pred:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   275
  \<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   276
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   277
  from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   278
  moreover have \<open>bit (2 * b) n \<or> n = 0 \<longleftrightarrow> bit (1 + 2 * b) n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   279
    using even_bit_succ_iff by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   280
  ultimately show ?thesis by (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   281
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   282
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   283
lemma bit_eq_rec:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   284
  \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   285
proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   286
  assume ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   287
  then show ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   288
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   289
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   290
  assume ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   291
  then have \<open>even a \<longleftrightarrow> even b\<close> and \<open>a div 2 = b div 2\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   292
    by simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   293
  show ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   294
  proof (rule bit_eqI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   295
    fix n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   296
    show \<open>bit a n \<longleftrightarrow> bit b n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   297
    proof (cases n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   298
      case 0
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   299
      with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   300
        by (simp add: bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   301
    next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   302
      case (Suc n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   303
      moreover from \<open>a div 2 = b div 2\<close> have \<open>bit (a div 2) n = bit (b div 2) n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   304
        by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   305
      ultimately show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   306
        by (simp add: bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   307
    qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   308
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   309
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   310
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   311
lemma bit_mod_2_iff [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   312
  \<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   313
  by (cases a rule: parity_cases) (simp_all add: bit_iff_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   314
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   315
lemma bit_mask_sub_iff:
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   316
  \<open>bit (2 ^ m - 1) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n < m\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   317
  by (simp add: bit_iff_odd even_mask_div_iff not_le possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   318
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   319
lemma exp_add_not_zero_imp:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   320
  \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> if \<open>2 ^ (m + n) \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   321
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   322
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   323
  proof (rule notI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   324
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   325
    then have \<open>2 ^ (m + n) = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   326
      by (rule disjE) (simp_all add: power_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   327
    with that show False ..
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   328
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   329
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   330
    by simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   331
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   332
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   333
lemma bit_disjunctive_add_iff:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   334
  \<open>bit (a + b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   335
  if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   336
proof (cases \<open>2 ^ n = 0\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   337
  case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   338
  then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   339
    by (simp add: exp_eq_0_imp_not_bit)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   340
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   341
  case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   342
  with that show ?thesis proof (induction n arbitrary: a b)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   343
    case 0
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   344
    from "0.prems"(1) [of 0] show ?case
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   345
      by (auto simp add: bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   346
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   347
    case (Suc n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   348
    from Suc.prems(1) [of 0] have even: \<open>even a \<or> even b\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   349
      by (auto simp add: bit_0)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   350
    have bit: \<open>\<not> bit (a div 2) n \<or> \<not> bit (b div 2) n\<close> for n
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   351
      using Suc.prems(1) [of \<open>Suc n\<close>] by (simp add: bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   352
    from Suc.prems(2) have \<open>2 * 2 ^ n \<noteq> 0\<close> \<open>2 ^ n \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   353
      by (auto simp add: mult_2)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   354
    have \<open>a + b = (a div 2 * 2 + a mod 2) + (b div 2 * 2 + b mod 2)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   355
      using div_mult_mod_eq [of a 2] div_mult_mod_eq [of b 2] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   356
    also have \<open>\<dots> = of_bool (odd a \<or> odd b) + 2 * (a div 2 + b div 2)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   357
      using even by (auto simp add: algebra_simps mod2_eq_if)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   358
    finally have \<open>bit ((a + b) div 2) n \<longleftrightarrow> bit (a div 2 + b div 2) n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   359
      using \<open>2 * 2 ^ n \<noteq> 0\<close> by simp (simp_all flip: bit_Suc add: bit_double_iff possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   360
    also have \<open>\<dots> \<longleftrightarrow> bit (a div 2) n \<or> bit (b div 2) n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   361
      using bit \<open>2 ^ n \<noteq> 0\<close> by (rule Suc.IH)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   362
    finally show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   363
      by (simp add: bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   364
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   365
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   366
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   367
lemma
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   368
  exp_add_not_zero_imp_left: \<open>2 ^ m \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   369
  and exp_add_not_zero_imp_right: \<open>2 ^ n \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   370
  if \<open>2 ^ (m + n) \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   371
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   372
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   373
  proof (rule notI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   374
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   375
    then have \<open>2 ^ (m + n) = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   376
      by (rule disjE) (simp_all add: power_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   377
    with that show False ..
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   378
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   379
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   380
    by simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   381
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   382
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   383
lemma exp_not_zero_imp_exp_diff_not_zero:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   384
  \<open>2 ^ (n - m) \<noteq> 0\<close> if \<open>2 ^ n \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   385
proof (cases \<open>m \<le> n\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   386
  case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   387
  moreover define q where \<open>q = n - m\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   388
  ultimately have \<open>n = m + q\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   389
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   390
  with that show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   391
    by (simp add: exp_add_not_zero_imp_right)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   392
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   393
  case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   394
  with that show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   395
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   396
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   397
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   398
lemma bit_of_bool_iff [bit_simps]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   399
  \<open>bit (of_bool b) n \<longleftrightarrow> b \<and> n = 0\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   400
  by (simp add: bit_1_iff)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   401
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   402
end
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   403
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   404
lemma nat_bit_induct [case_names zero even odd]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   405
  \<open>P n\<close> if zero: \<open>P 0\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   406
    and even: \<open>\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   407
    and odd: \<open>\<And>n. P n \<Longrightarrow> P (Suc (2 * n))\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   408
proof (induction n rule: less_induct)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   409
  case (less n)
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   410
  show \<open>P n\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   411
  proof (cases \<open>n = 0\<close>)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   412
    case True with zero show ?thesis by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   413
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   414
    case False
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   415
    with less have hyp: \<open>P (n div 2)\<close> by simp
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   416
    show ?thesis
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   417
    proof (cases \<open>even n\<close>)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   418
      case True
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   419
      then have \<open>n \<noteq> 1\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   420
        by auto
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   421
      with \<open>n \<noteq> 0\<close> have \<open>n div 2 > 0\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   422
        by simp
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   423
      with \<open>even n\<close> hyp even [of \<open>n div 2\<close>] show ?thesis
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   424
        by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   425
    next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   426
      case False
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   427
      with hyp odd [of \<open>n div 2\<close>] show ?thesis
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   428
        by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   429
    qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   430
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   431
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   432
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   433
instantiation nat :: semiring_bits
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   434
begin
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   435
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   436
definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   437
  where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   438
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   439
instance
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   440
proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   441
  show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   442
    and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   443
    for P and n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   444
  proof (induction n rule: nat_bit_induct)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   445
    case zero
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   446
    from stable [of 0] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   447
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   448
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   449
    case (even n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   450
    with rec [of n False] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   451
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   452
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   453
    case (odd n)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   454
    with rec [of n True] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   455
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   456
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   457
  show \<open>q mod 2 ^ m mod 2 ^ n = q mod 2 ^ min m n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   458
    for q m n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   459
    apply (auto simp add: less_iff_Suc_add power_add mod_mod_cancel split: split_min_lin)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   460
    apply (metis div_mult2_eq mod_div_trivial mod_eq_self_iff_div_eq_0 mod_mult_self2_is_0 power_commutes)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   461
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   462
  show \<open>(q * 2 ^ m) mod (2 ^ n) = (q mod 2 ^ (n - m)) * 2 ^ m\<close> if \<open>m \<le> n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   463
    for q m n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   464
    using that
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   465
    apply (auto simp add: mod_mod_cancel div_mult2_eq power_add mod_mult2_eq le_iff_add split: split_min_lin)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   466
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   467
  show \<open>even ((2 ^ m - (1::nat)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::nat) \<or> m \<le> n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   468
    for m n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   469
    using even_mask_div_iff' [where ?'a = nat, of m n] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   470
  show \<open>even (q * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::nat) ^ n = 0 \<or> m \<le> n \<and> even (q div 2 ^ (n - m))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   471
    for m n q r :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   472
    apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   473
    apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   474
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   475
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff bit_nat_def)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   476
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   477
end
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   478
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   479
lemma possible_bit_nat [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   480
  \<open>possible_bit TYPE(nat) n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   481
  by (simp add: possible_bit_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   482
79069
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
   483
lemma bit_Suc_0_iff [bit_simps]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
   484
  \<open>bit (Suc 0) n \<longleftrightarrow> n = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
   485
  using bit_1_iff [of n, where ?'a = nat] by simp
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
   486
74497
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   487
lemma not_bit_Suc_0_Suc [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   488
  \<open>\<not> bit (Suc 0) (Suc n)\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   489
  by (simp add: bit_Suc)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   490
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   491
lemma not_bit_Suc_0_numeral [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   492
  \<open>\<not> bit (Suc 0) (numeral n)\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   493
  by (simp add: numeral_eq_Suc)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
   494
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   495
context semiring_bits
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   496
begin
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   497
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   498
lemma bit_of_nat_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   499
  \<open>bit (of_nat m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit m n\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   500
proof (cases \<open>(2::'a) ^ n = 0\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   501
  case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   502
  then show ?thesis
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   503
    by (simp add: exp_eq_0_imp_not_bit possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   504
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   505
  case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   506
  then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   507
  proof (induction m arbitrary: n rule: nat_bit_induct)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   508
    case zero
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   509
    then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   510
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   511
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   512
    case (even m)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   513
    then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   514
      by (cases n)
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   515
        (auto simp add: bit_double_iff Bit_Operations.bit_double_iff possible_bit_def bit_0 dest: mult_not_zero)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   516
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   517
    case (odd m)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   518
    then show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   519
      by (cases n)
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   520
        (auto simp add: bit_double_iff even_bit_succ_iff possible_bit_def
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   521
          Bit_Operations.bit_Suc Bit_Operations.bit_0 dest: mult_not_zero)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   522
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   523
  with False show ?thesis
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   524
    by (simp add: possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   525
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   526
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   527
end
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   528
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   529
lemma int_bit_induct [case_names zero minus even odd]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   530
  \<open>P k\<close> if zero_int: \<open>P 0\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   531
    and minus_int: \<open>P (- 1)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   532
    and even_int: \<open>\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   533
    and odd_int: \<open>\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))\<close> for k :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   534
proof (cases \<open>k \<ge> 0\<close>)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   535
  case True
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   536
  define n where \<open>n = nat k\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   537
  with True have \<open>k = int n\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   538
    by simp
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   539
  then show \<open>P k\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   540
  proof (induction n arbitrary: k rule: nat_bit_induct)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   541
    case zero
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   542
    then show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   543
      by (simp add: zero_int)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   544
  next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   545
    case (even n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   546
    have \<open>P (int n * 2)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   547
      by (rule even_int) (use even in simp_all)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   548
    with even show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   549
      by (simp add: ac_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   550
  next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   551
    case (odd n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   552
    have \<open>P (1 + (int n * 2))\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   553
      by (rule odd_int) (use odd in simp_all)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   554
    with odd show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   555
      by (simp add: ac_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   556
  qed
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   557
next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   558
  case False
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   559
  define n where \<open>n = nat (- k - 1)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   560
  with False have \<open>k = - int n - 1\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   561
    by simp
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   562
  then show \<open>P k\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   563
  proof (induction n arbitrary: k rule: nat_bit_induct)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   564
    case zero
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   565
    then show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   566
      by (simp add: minus_int)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   567
  next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   568
    case (even n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   569
    have \<open>P (1 + (- int (Suc n) * 2))\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   570
      by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   571
    also have \<open>\<dots> = - int (2 * n) - 1\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   572
      by (simp add: algebra_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   573
    finally show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   574
      using even.prems by simp
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   575
  next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   576
    case (odd n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   577
    have \<open>P (- int (Suc n) * 2)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   578
      by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   579
    also have \<open>\<dots> = - int (Suc (2 * n)) - 1\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   580
      by (simp add: algebra_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   581
    finally show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   582
      using odd.prems by simp
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   583
  qed
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   584
qed
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   585
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   586
instantiation int :: semiring_bits
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   587
begin
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   588
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   589
definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   590
  where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   591
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   592
instance
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   593
proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   594
  show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   595
    and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   596
    for P and k :: int
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   597
  proof (induction k rule: int_bit_induct)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   598
    case zero
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   599
    from stable [of 0] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   600
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   601
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   602
    case minus
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   603
    from stable [of \<open>- 1\<close>] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   604
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   605
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   606
    case (even k)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   607
    with rec [of k False] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   608
      by (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   609
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   610
    case (odd k)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   611
    with rec [of k True] show ?case
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   612
      by (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   613
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   614
  show \<open>(2::int) ^ m div 2 ^ n = of_bool ((2::int) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   615
    for m n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   616
  proof (cases \<open>m < n\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   617
    case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   618
    then have \<open>n = m + (n - m)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   619
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   620
    then have \<open>(2::int) ^ m div 2 ^ n = (2::int) ^ m div 2 ^ (m + (n - m))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   621
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   622
    also have \<open>\<dots> = (2::int) ^ m div (2 ^ m * 2 ^ (n - m))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   623
      by (simp add: power_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   624
    also have \<open>\<dots> = (2::int) ^ m div 2 ^ m div 2 ^ (n - m)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   625
      by (simp add: zdiv_zmult2_eq)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   626
    finally show ?thesis using \<open>m < n\<close> by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   627
  next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   628
    case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   629
    then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   630
      by (simp add: power_diff)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   631
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   632
  show \<open>k mod 2 ^ m mod 2 ^ n = k mod 2 ^ min m n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   633
    for m n :: nat and k :: int
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   634
    using mod_exp_eq [of \<open>nat k\<close> m n]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   635
    apply (auto simp add: mod_mod_cancel zdiv_zmult2_eq power_add zmod_zmult2_eq le_iff_add split: split_min_lin)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   636
     apply (auto simp add: less_iff_Suc_add mod_mod_cancel power_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   637
    apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>])
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   638
    apply (subst zmod_zmult2_eq) apply simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   639
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   640
  show \<open>(k * 2 ^ m) mod (2 ^ n) = (k mod 2 ^ (n - m)) * 2 ^ m\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   641
    if \<open>m \<le> n\<close> for m n :: nat and k :: int
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   642
    using that
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   643
    apply (auto simp add: power_add zmod_zmult2_eq le_iff_add split: split_min_lin)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   644
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   645
  show \<open>even ((2 ^ m - (1::int)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::int) \<or> m \<le> n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   646
    for m n :: nat
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   647
    using even_mask_div_iff' [where ?'a = int, of m n] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   648
  show \<open>even (k * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::int) ^ n = 0 \<or> m \<le> n \<and> even (k div 2 ^ (n - m))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   649
    for m n :: nat and k l :: int
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   650
    apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   651
    apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2))
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   652
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   653
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le bit_int_def)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   654
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   655
end
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   656
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   657
lemma possible_bit_int [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   658
  \<open>possible_bit TYPE(int) n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   659
  by (simp add: possible_bit_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   660
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   661
lemma bit_nat_iff [bit_simps]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   662
  \<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   663
proof (cases \<open>k \<ge> 0\<close>)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   664
  case True
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   665
  moreover define m where \<open>m = nat k\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   666
  ultimately have \<open>k = int m\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   667
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   668
  then show ?thesis
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   669
    by (simp add: bit_simps)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   670
next
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   671
  case False
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   672
  then show ?thesis
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   673
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   674
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   675
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   676
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   677
subsection \<open>Bit operations\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   678
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   679
class semiring_bit_operations = semiring_bits +
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   680
  fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>AND\<close> 64)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   681
    and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>OR\<close> 59)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   682
    and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close>  (infixr \<open>XOR\<close> 59)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   683
    and mask :: \<open>nat \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   684
    and set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   685
    and unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   686
    and flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   687
    and push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   688
    and drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   689
    and take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   690
  assumes and_rec: \<open>a AND b = of_bool (odd a \<and> odd b) + 2 * ((a div 2) AND (b div 2))\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   691
    and or_rec: \<open>a OR b = of_bool (odd a \<or> odd b) + 2 * ((a div 2) OR (b div 2))\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   692
    and xor_rec: \<open>a XOR b = of_bool (odd a \<noteq> odd b) + 2 * ((a div 2) XOR (b div 2))\<close>
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   693
    and mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   694
    and set_bit_eq_or: \<open>set_bit n a = a OR push_bit n 1\<close>
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
   695
    and unset_bit_eq_or_xor: \<open>unset_bit n a = (a OR push_bit n 1) XOR push_bit n 1\<close>
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   696
    and flip_bit_eq_xor: \<open>flip_bit n a = a XOR push_bit n 1\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   697
    and push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   698
    and drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   699
    and take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ 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
   700
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
   701
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   702
text \<open>
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   703
  We want the bitwise operations to bind slightly weaker
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   704
  than \<open>+\<close> and \<open>-\<close>.
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   705
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   706
  Logically, \<^const>\<open>push_bit\<close>,
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   707
  \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   708
  as separate operations makes proofs easier, otherwise proof automation
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   709
  would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   710
  algebraic relationships between those operations.
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   711
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
   712
  For the sake of code generation operations
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   713
  are specified as definitional class operations,
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   714
  taking into account that specific instances of these can be implemented
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   715
  differently wrt. code generation.
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   716
\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   717
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   718
lemma bit_and_iff [bit_simps]:
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   719
  \<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   720
proof (induction n arbitrary: a b)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   721
  case 0
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   722
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   723
    by (simp add: bit_0 and_rec [of a b] even_bit_succ_iff)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   724
next
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   725
  case (Suc n)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   726
  from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>]
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   727
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   728
    by (simp add: and_rec [of a b] bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   729
      (auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   730
qed
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   731
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   732
lemma bit_or_iff [bit_simps]:
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   733
  \<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   734
proof (induction n arbitrary: a b)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   735
  case 0
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   736
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   737
    by (simp add: bit_0 or_rec [of a b] even_bit_succ_iff)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   738
next
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   739
  case (Suc n)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   740
  from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>]
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   741
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   742
    by (simp add: or_rec [of a b] bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   743
      (auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   744
qed
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   745
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   746
lemma bit_xor_iff [bit_simps]:
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   747
  \<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   748
proof (induction n arbitrary: a b)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   749
  case 0
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   750
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   751
    by (simp add: bit_0 xor_rec [of a b] even_bit_succ_iff)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   752
next
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   753
  case (Suc n)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   754
  from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>]
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   755
  show ?case
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   756
    by (simp add: xor_rec [of a b] bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   757
      (auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   758
qed
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   759
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   760
sublocale "and": semilattice \<open>(AND)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   761
  by standard (auto simp add: bit_eq_iff bit_and_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   762
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   763
sublocale or: semilattice_neutr \<open>(OR)\<close> 0
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   764
  by standard (auto simp add: bit_eq_iff bit_or_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   765
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   766
sublocale xor: comm_monoid \<open>(XOR)\<close> 0
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   767
  by standard (auto simp add: bit_eq_iff bit_xor_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   768
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   769
lemma even_and_iff:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   770
  \<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   771
  using bit_and_iff [of a b 0] by (auto simp add: bit_0)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   772
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   773
lemma even_or_iff:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   774
  \<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   775
  using bit_or_iff [of a b 0] by (auto simp add: bit_0)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   776
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   777
lemma even_xor_iff:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   778
  \<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   779
  using bit_xor_iff [of a b 0] by (auto simp add: bit_0)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   780
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   781
lemma zero_and_eq [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   782
  \<open>0 AND a = 0\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   783
  by (simp add: bit_eq_iff bit_and_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   784
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   785
lemma and_zero_eq [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   786
  \<open>a AND 0 = 0\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   787
  by (simp add: bit_eq_iff bit_and_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   788
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   789
lemma one_and_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   790
  \<open>1 AND a = a mod 2\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   791
  by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff bit_0)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   792
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   793
lemma and_one_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   794
  \<open>a AND 1 = a mod 2\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   795
  using one_and_eq [of a] by (simp add: ac_simps)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   796
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   797
lemma one_or_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   798
  \<open>1 OR a = a + of_bool (even a)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   799
  by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff)
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   800
    (auto simp add: bit_1_iff bit_0)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   801
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   802
lemma or_one_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   803
  \<open>a OR 1 = a + of_bool (even a)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   804
  using one_or_eq [of a] by (simp add: ac_simps)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   805
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   806
lemma one_xor_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   807
  \<open>1 XOR a = a + of_bool (even a) - of_bool (odd a)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   808
  by (simp add: bit_eq_iff bit_xor_iff add.commute [of _ 1] even_bit_succ_iff)
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
   809
    (auto simp add: bit_1_iff odd_bit_iff_bit_pred bit_0 elim: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   810
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   811
lemma xor_one_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   812
  \<open>a XOR 1 = a + of_bool (even a) - of_bool (odd a)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   813
  using one_xor_eq [of a] by (simp add: ac_simps)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   814
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
   815
lemma xor_self_eq [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
   816
  \<open>a XOR a = 0\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
   817
  by (rule bit_eqI) (simp add: bit_simps)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
   818
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   819
lemma bit_iff_odd_drop_bit:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   820
  \<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   821
  by (simp add: bit_iff_odd drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   822
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   823
lemma even_drop_bit_iff_not_bit:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   824
  \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   825
  by (simp add: bit_iff_odd_drop_bit)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   826
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   827
lemma div_push_bit_of_1_eq_drop_bit:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   828
  \<open>a div push_bit n 1 = drop_bit n a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   829
  by (simp add: push_bit_eq_mult drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   830
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   831
lemma bits_ident:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   832
  \<open>push_bit n (drop_bit n a) + take_bit n a = a\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   833
  using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   834
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   835
lemma push_bit_push_bit [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   836
  \<open>push_bit m (push_bit n a) = push_bit (m + n) a\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   837
  by (simp add: push_bit_eq_mult power_add ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   838
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   839
lemma push_bit_0_id [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   840
  \<open>push_bit 0 = id\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   841
  by (simp add: fun_eq_iff push_bit_eq_mult)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   842
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   843
lemma push_bit_of_0 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   844
  \<open>push_bit n 0 = 0\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   845
  by (simp add: push_bit_eq_mult)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   846
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
   847
lemma push_bit_of_1 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   848
  \<open>push_bit n 1 = 2 ^ n\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   849
  by (simp add: push_bit_eq_mult)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   850
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   851
lemma push_bit_Suc [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   852
  \<open>push_bit (Suc n) a = push_bit n (a * 2)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   853
  by (simp add: push_bit_eq_mult ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   854
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   855
lemma push_bit_double:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   856
  \<open>push_bit n (a * 2) = push_bit n a * 2\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   857
  by (simp add: push_bit_eq_mult ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   858
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   859
lemma push_bit_add:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   860
  \<open>push_bit n (a + b) = push_bit n a + push_bit n b\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   861
  by (simp add: push_bit_eq_mult algebra_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   862
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   863
lemma push_bit_numeral [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   864
  \<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   865
  by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   866
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   867
lemma take_bit_0 [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   868
  "take_bit 0 a = 0"
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   869
  by (simp add: take_bit_eq_mod)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   870
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   871
lemma take_bit_Suc:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   872
  \<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + a mod 2\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   873
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   874
  have \<open>take_bit (Suc n) (a div 2 * 2 + of_bool (odd a)) = take_bit n (a div 2) * 2 + of_bool (odd a)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   875
    using even_succ_mod_exp [of \<open>2 * (a div 2)\<close> \<open>Suc n\<close>]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   876
      mult_exp_mod_exp_eq [of 1 \<open>Suc n\<close> \<open>a div 2\<close>]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   877
    by (auto simp add: take_bit_eq_mod ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   878
  then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   879
    using div_mult_mod_eq [of a 2] by (simp add: mod_2_eq_odd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   880
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   881
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   882
lemma take_bit_rec:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   883
  \<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + a mod 2)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   884
  by (cases n) (simp_all add: take_bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   885
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   886
lemma take_bit_Suc_0 [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   887
  \<open>take_bit (Suc 0) a = a mod 2\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   888
  by (simp add: take_bit_eq_mod)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   889
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   890
lemma take_bit_of_0 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   891
  \<open>take_bit n 0 = 0\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   892
  by (simp add: take_bit_eq_mod)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   893
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   894
lemma take_bit_of_1 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   895
  \<open>take_bit n 1 = of_bool (n > 0)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   896
  by (cases n) (simp_all add: take_bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   897
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   898
lemma drop_bit_of_0 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   899
  \<open>drop_bit n 0 = 0\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   900
  by (simp add: drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   901
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   902
lemma drop_bit_of_1 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   903
  \<open>drop_bit n 1 = of_bool (n = 0)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   904
  by (simp add: drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   905
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   906
lemma drop_bit_0 [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   907
  \<open>drop_bit 0 = id\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   908
  by (simp add: fun_eq_iff drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   909
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   910
lemma drop_bit_Suc:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   911
  \<open>drop_bit (Suc n) a = drop_bit n (a div 2)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   912
  using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   913
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   914
lemma drop_bit_rec:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   915
  \<open>drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   916
  by (cases n) (simp_all add: drop_bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   917
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   918
lemma drop_bit_half:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   919
  \<open>drop_bit n (a div 2) = drop_bit n a div 2\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   920
  by (induction n arbitrary: a) (simp_all add: drop_bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   921
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   922
lemma drop_bit_of_bool [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   923
  \<open>drop_bit n (of_bool b) = of_bool (n = 0 \<and> b)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   924
  by (cases n) simp_all
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   925
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   926
lemma even_take_bit_eq [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   927
  \<open>even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   928
  by (simp add: take_bit_rec [of n a])
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   929
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   930
lemma take_bit_take_bit [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   931
  \<open>take_bit m (take_bit n a) = take_bit (min m n) a\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   932
  by (simp add: take_bit_eq_mod mod_exp_eq ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   933
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   934
lemma drop_bit_drop_bit [simp]:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   935
  \<open>drop_bit m (drop_bit n a) = drop_bit (m + n) a\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   936
  by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   937
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   938
lemma push_bit_take_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   939
  \<open>push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   940
  apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   941
  using mult_exp_mod_exp_eq [of m \<open>m + n\<close> a] apply (simp add: ac_simps power_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   942
  done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   943
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   944
lemma take_bit_push_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   945
  \<open>take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   946
proof (cases \<open>m \<le> n\<close>)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   947
  case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   948
  then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   949
    apply (simp add:)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   950
    apply (simp_all add: push_bit_eq_mult take_bit_eq_mod)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   951
    apply (auto dest!: le_Suc_ex simp add: power_add ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   952
    using mult_exp_mod_exp_eq [of m m \<open>a * 2 ^ n\<close> for n]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   953
    apply (simp add: ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   954
    done
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   955
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   956
  case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   957
  then show ?thesis
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   958
    using push_bit_take_bit [of n \<open>m - n\<close> a]
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   959
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   960
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   961
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   962
lemma take_bit_drop_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   963
  \<open>take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   964
  by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   965
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   966
lemma drop_bit_take_bit:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
   967
  \<open>drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   968
proof (cases "m \<le> n")
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   969
  case True
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   970
  then show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   971
    using take_bit_drop_bit [of "n - m" m a] by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   972
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   973
  case False
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   974
  then obtain q where \<open>m = n + q\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   975
    by (auto simp add: not_le dest: less_imp_Suc_add)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   976
  then have \<open>drop_bit m (take_bit n a) = 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   977
    using div_exp_eq [of \<open>a mod 2 ^ n\<close> n q]
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   978
    by (simp add: take_bit_eq_mod drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   979
  with False show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   980
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   981
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   982
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   983
lemma even_push_bit_iff [simp]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   984
  \<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   985
  by (simp add: push_bit_eq_mult) auto
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   986
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   987
lemma bit_push_bit_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   988
  \<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> possible_bit TYPE('a) n \<and> bit a (n - m)\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
   989
  by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff possible_bit_def)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   990
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   991
lemma bit_drop_bit_eq [bit_simps]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   992
  \<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   993
  by (simp add: bit_iff_odd fun_eq_iff ac_simps flip: drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   994
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   995
lemma bit_take_bit_iff [bit_simps]:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   996
  \<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   997
  by (simp add: bit_iff_odd drop_bit_take_bit not_le flip: drop_bit_eq_div)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   998
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
   999
lemma stable_imp_drop_bit_eq:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1000
  \<open>drop_bit n a = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1001
  if \<open>a div 2 = a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1002
  by (induction n) (simp_all add: that drop_bit_Suc)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1003
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1004
lemma stable_imp_take_bit_eq:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1005
  \<open>take_bit n a = (if even a then 0 else 2 ^ n - 1)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1006
    if \<open>a div 2 = a\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1007
proof (rule bit_eqI[unfolded possible_bit_def])
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1008
  fix m
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1009
  assume \<open>2 ^ m \<noteq> 0\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1010
  with that show \<open>bit (take_bit n a) m \<longleftrightarrow> bit (if even a then 0 else 2 ^ n - 1) m\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1011
    by (simp add: bit_take_bit_iff bit_mask_sub_iff possible_bit_def stable_imp_bit_iff_odd)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1012
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1013
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1014
lemma exp_dvdE:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1015
  assumes \<open>2 ^ n dvd a\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1016
  obtains b where \<open>a = push_bit n b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1017
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1018
  from assms obtain b where \<open>a = 2 ^ n * b\<close> ..
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1019
  then have \<open>a = push_bit n b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1020
    by (simp add: push_bit_eq_mult ac_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1021
  with that show thesis .
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1022
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1023
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1024
lemma take_bit_eq_0_iff:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1025
  \<open>take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1026
proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1027
  assume ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1028
  then show ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1029
    by (simp add: take_bit_eq_mod mod_0_imp_dvd)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1030
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1031
  assume ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1032
  then obtain b where \<open>a = push_bit n b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1033
    by (rule exp_dvdE)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1034
  then show ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1035
    by (simp add: take_bit_push_bit)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1036
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1037
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1038
lemma take_bit_tightened:
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1039
  \<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1040
proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1041
  from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1042
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1043
  then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1044
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1045
  with that show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1046
    by (simp add: min_def)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1047
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1048
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1049
lemma take_bit_eq_self_iff_drop_bit_eq_0:
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1050
  \<open>take_bit n a = a \<longleftrightarrow> drop_bit n a = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1051
proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1052
  assume ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1053
  show ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1054
  proof (rule bit_eqI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1055
    fix m
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1056
    from \<open>?P\<close> have \<open>a = take_bit n a\<close> ..
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1057
    also have \<open>\<not> bit (take_bit n a) (n + m)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1058
      unfolding bit_simps
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1059
      by (simp add: bit_simps)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1060
    finally show \<open>bit (drop_bit n a) m \<longleftrightarrow> bit 0 m\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1061
      by (simp add: bit_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1062
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1063
next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1064
  assume ?Q
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1065
  show ?P
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1066
  proof (rule bit_eqI)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1067
    fix m
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1068
    from \<open>?Q\<close> have \<open>\<not> bit (drop_bit n a) (m - n)\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1069
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1070
    then have \<open> \<not> bit a (n + (m - n))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1071
      by (simp add: bit_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1072
    then show \<open>bit (take_bit n a) m \<longleftrightarrow> bit a m\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1073
      by (cases \<open>m < n\<close>) (auto simp add: bit_simps)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1074
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1075
qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1076
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1077
lemma drop_bit_exp_eq:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1078
  \<open>drop_bit m (2 ^ n) = of_bool (m \<le> n \<and> possible_bit TYPE('a) n) * 2 ^ (n - m)\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1079
  by (auto simp add: bit_eq_iff bit_simps)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1080
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1081
lemma take_bit_and [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1082
  \<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1083
  by (auto simp add: bit_eq_iff bit_simps)
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1084
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1085
lemma take_bit_or [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1086
  \<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1087
  by (auto simp add: bit_eq_iff bit_simps)
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1088
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1089
lemma take_bit_xor [simp]:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1090
  \<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1091
  by (auto simp add: bit_eq_iff bit_simps)
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1092
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1093
lemma push_bit_and [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1094
  \<open>push_bit n (a AND b) = push_bit n a AND push_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1095
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1096
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1097
lemma push_bit_or [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1098
  \<open>push_bit n (a OR b) = push_bit n a OR push_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1099
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1100
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1101
lemma push_bit_xor [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1102
  \<open>push_bit n (a XOR b) = push_bit n a XOR push_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1103
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1104
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1105
lemma drop_bit_and [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1106
  \<open>drop_bit n (a AND b) = drop_bit n a AND drop_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1107
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1108
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1109
lemma drop_bit_or [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1110
  \<open>drop_bit n (a OR b) = drop_bit n a OR drop_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1111
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1112
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1113
lemma drop_bit_xor [simp]:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1114
  \<open>drop_bit n (a XOR b) = drop_bit n a XOR drop_bit n b\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1115
  by (auto simp add: bit_eq_iff bit_simps)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1116
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1117
lemma bit_mask_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1118
  \<open>bit (mask m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n < m\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1119
  by (simp add: mask_eq_exp_minus_1 bit_mask_sub_iff)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1120
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1121
lemma even_mask_iff:
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1122
  \<open>even (mask n) \<longleftrightarrow> n = 0\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1123
  using bit_mask_iff [of n 0] by (auto simp add: bit_0)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1124
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1125
lemma mask_0 [simp]:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1126
  \<open>mask 0 = 0\<close>
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1127
  by (simp add: mask_eq_exp_minus_1)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1128
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1129
lemma mask_Suc_0 [simp]:
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1130
  \<open>mask (Suc 0) = 1\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1131
  by (simp add: mask_eq_exp_minus_1 add_implies_diff sym)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1132
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1133
lemma mask_Suc_exp:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1134
  \<open>mask (Suc n) = 2 ^ n OR mask n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1135
  by (auto simp add: bit_eq_iff bit_simps)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1136
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1137
lemma mask_Suc_double:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1138
  \<open>mask (Suc n) = 1 OR 2 * mask n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1139
  by (auto simp add: bit_eq_iff bit_simps elim: possible_bit_less_imp)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1140
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1141
lemma mask_numeral:
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1142
  \<open>mask (numeral n) = 1 + 2 * mask (pred_numeral n)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1143
  by (simp add: numeral_eq_Suc mask_Suc_double one_or_eq ac_simps)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1144
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1145
lemma take_bit_of_mask [simp]:
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  1146
  \<open>take_bit m (mask n) = mask (min m n)\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  1147
  by (rule bit_eqI) (simp add: bit_simps)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  1148
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
  1149
lemma take_bit_eq_mask:
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1150
  \<open>take_bit n a = a AND mask n\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1151
  by (auto simp add: bit_eq_iff bit_simps)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71822
diff changeset
  1152
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1153
lemma or_eq_0_iff:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1154
  \<open>a OR b = 0 \<longleftrightarrow> a = 0 \<and> b = 0\<close>
72792
26492b600d78 tuned whitespace --- avoid TABs;
wenzelm
parents: 72611
diff changeset
  1155
  by (auto simp add: bit_eq_iff bit_or_iff)
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1156
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1157
lemma disjunctive_add:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1158
  \<open>a + b = a OR b\<close> if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1159
  by (rule bit_eqI) (use that in \<open>simp add: bit_disjunctive_add_iff bit_or_iff\<close>)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1160
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1161
lemma bit_iff_and_drop_bit_eq_1:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1162
  \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1163
  by (simp add: bit_iff_odd_drop_bit and_one_eq odd_iff_mod_2_eq_one)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1164
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1165
lemma bit_iff_and_push_bit_not_eq_0:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1166
  \<open>bit a n \<longleftrightarrow> a AND push_bit n 1 \<noteq> 0\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1167
  apply (cases \<open>2 ^ n = 0\<close>)
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1168
  apply (simp_all add: bit_eq_iff bit_and_iff bit_push_bit_iff exp_eq_0_imp_not_bit)
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1169
  apply (simp_all add: bit_exp_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1170
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72488
diff changeset
  1171
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1172
lemma bit_set_bit_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1173
  \<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> possible_bit TYPE('a) n)\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1174
  by (auto simp add: set_bit_eq_or bit_or_iff bit_exp_iff)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1175
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1176
lemma even_set_bit_iff:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1177
  \<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1178
  using bit_set_bit_iff [of m a 0] by (auto simp add: bit_0)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1179
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  1180
lemma bit_unset_bit_iff [bit_simps]:
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  1181
  \<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close>
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1182
  by (auto simp add: unset_bit_eq_or_xor bit_simps dest: bit_imp_possible_bit)
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  1183
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1184
lemma even_unset_bit_iff:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1185
  \<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1186
  using bit_unset_bit_iff [of m a 0] by (auto simp add: bit_0)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1187
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1188
lemma bit_flip_bit_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1189
  \<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> possible_bit TYPE('a) n\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1190
  by (auto simp add: bit_eq_iff bit_simps flip_bit_eq_xor bit_imp_possible_bit)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1191
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1192
lemma even_flip_bit_iff:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1193
  \<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1194
  using bit_flip_bit_iff [of m a 0] by (auto simp: possible_bit_def  bit_0)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1195
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1196
lemma and_exp_eq_0_iff_not_bit:
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1197
  \<open>a AND 2 ^ n = 0 \<longleftrightarrow> \<not> bit a n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1198
  using bit_imp_possible_bit[of a n]
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1199
  by (auto simp add: bit_eq_iff bit_simps)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1200
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1201
lemma bit_sum_mult_2_cases:
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1202
  assumes a: \<open>\<forall>j. \<not> bit a (Suc j)\<close>
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1203
  shows \<open>bit (a + 2 * b) n = (if n = 0 then odd a else bit (2 * b) n)\<close>
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1204
proof -
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1205
  from a have \<open>n = 0\<close> if \<open>bit a n\<close> for n using that
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1206
    by (cases n) simp_all
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1207
  then have \<open>a = 0 \<or> a = 1\<close>
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1208
    by (auto simp add: bit_eq_iff bit_1_iff)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1209
  then show ?thesis
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1210
    by (cases n) (auto simp add: bit_0 bit_double_iff even_bit_succ_iff)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1211
qed
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1212
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1213
lemma set_bit_0 [simp]:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1214
  \<open>set_bit 0 a = 1 + 2 * (a div 2)\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1215
  by (auto simp add: bit_eq_iff bit_simps even_bit_succ_iff simp flip: bit_Suc)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1216
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1217
lemma set_bit_Suc:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1218
  \<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1219
  by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1220
    elim: possible_bit_less_imp)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1221
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1222
lemma unset_bit_0 [simp]:
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1223
  \<open>unset_bit 0 a = 2 * (a div 2)\<close>
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1224
  by (auto simp add: bit_eq_iff bit_simps simp flip: bit_Suc)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1225
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1226
lemma unset_bit_Suc:
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1227
  \<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close>
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1228
  by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc)
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1229
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1230
lemma flip_bit_0 [simp]:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1231
  \<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1232
  by (auto simp add: bit_eq_iff bit_simps even_bit_succ_iff bit_0 simp flip: bit_Suc)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1233
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1234
lemma flip_bit_Suc:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1235
  \<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1236
  by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1237
    elim: possible_bit_less_imp)
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1238
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1239
lemma flip_bit_eq_if:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1240
  \<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1241
  by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1242
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1243
lemma take_bit_set_bit_eq:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1244
  \<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>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1245
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1246
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1247
lemma take_bit_unset_bit_eq:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1248
  \<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>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1249
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1250
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1251
lemma take_bit_flip_bit_eq:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1252
  \<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>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1253
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1254
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1255
lemma bit_1_0 [simp]:
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1256
  \<open>bit 1 0\<close>
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1257
  by (simp add: bit_0)
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  1258
74497
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1259
lemma not_bit_1_Suc [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1260
  \<open>\<not> bit 1 (Suc n)\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1261
  by (simp add: bit_Suc)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1262
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1263
lemma push_bit_Suc_numeral [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1264
  \<open>push_bit (Suc n) (numeral k) = push_bit n (numeral (Num.Bit0 k))\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1265
  by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1266
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1267
lemma mask_eq_0_iff [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1268
  \<open>mask n = 0 \<longleftrightarrow> n = 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1269
  by (cases n) (simp_all add: mask_Suc_double or_eq_0_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1270
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1271
lemma bit_horner_sum_bit_iff [bit_simps]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1272
  \<open>bit (horner_sum of_bool 2 bs) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n < length bs \<and> bs ! n\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1273
proof (induction bs arbitrary: n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1274
  case Nil
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1275
  then show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1276
    by simp
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1277
next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1278
  case (Cons b bs)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1279
  show ?case
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1280
  proof (cases n)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1281
    case 0
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1282
    then show ?thesis
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1283
      by (simp add: bit_0)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1284
  next
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1285
    case (Suc m)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1286
    with bit_rec [of _ n] Cons.prems Cons.IH [of m]
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1287
    show ?thesis
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1288
      by (simp add: bit_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1289
        (auto simp add: possible_bit_less_imp bit_simps simp flip: bit_Suc)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1290
  qed
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1291
qed
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1292
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1293
lemma horner_sum_bit_eq_take_bit:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1294
  \<open>horner_sum of_bool 2 (map (bit a) [0..<n]) = take_bit n a\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1295
  by (rule bit_eqI) (auto simp add: bit_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1296
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1297
lemma take_bit_horner_sum_bit_eq:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1298
  \<open>take_bit n (horner_sum of_bool 2 bs) = horner_sum of_bool 2 (take n bs)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1299
  by (auto simp add: bit_eq_iff bit_take_bit_iff bit_horner_sum_bit_iff)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1300
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1301
lemma take_bit_sum:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1302
  \<open>take_bit n a = (\<Sum>k = 0..<n. push_bit k (of_bool (bit a k)))\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1303
  by (simp flip: horner_sum_bit_eq_take_bit add: horner_sum_eq_sum push_bit_eq_mult)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1304
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1305
lemma set_bit_eq:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1306
  \<open>set_bit n a = a + of_bool (\<not> bit a n) * 2 ^ n\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1307
proof -
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1308
  have \<open>set_bit n a = a OR of_bool (\<not> bit a n) * 2 ^ n\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1309
    by (rule bit_eqI) (auto simp add: bit_simps)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1310
  then show ?thesis
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1311
    by (subst disjunctive_add) (auto simp add: bit_simps)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1312
qed
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1313
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1314
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1315
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1316
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
  1317
  fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1318
  assumes not_eq_complement: \<open>NOT a = - 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
  1319
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1320
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1321
text \<open>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1322
  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
  1323
  definitional class operation.  Note that \<^const>\<open>not\<close> has no
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1324
  sensible definition for unlimited but only positive bit strings
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1325
  (type \<^typ>\<open>nat\<close>).
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1326
\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1327
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1328
lemma bits_minus_1_mod_2_eq [simp]:
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1329
  \<open>(- 1) mod 2 = 1\<close>
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1330
  by (simp add: mod_2_eq_odd)
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1331
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1332
lemma minus_eq_not_plus_1:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1333
  \<open>- a = NOT a + 1\<close>
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1334
  using not_eq_complement [of a] by simp
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1335
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1336
lemma minus_eq_not_minus_1:
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1337
  \<open>- a = NOT (a - 1)\<close>
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1338
  using not_eq_complement [of \<open>a - 1\<close>] by simp (simp add: algebra_simps)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1339
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1340
lemma not_rec:
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1341
  \<open>NOT a = of_bool (even a) + 2 * NOT (a div 2)\<close>
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1342
  by (simp add: not_eq_complement algebra_simps mod_2_eq_odd flip: minus_mod_eq_mult_div)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1343
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1344
lemma even_not_iff [simp]:
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1345
  \<open>even (NOT a) \<longleftrightarrow> odd a\<close>
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1346
  by (simp add: not_eq_complement)
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1347
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1348
lemma bit_not_iff [bit_simps]:
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1349
  \<open>bit (NOT a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit a n\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1350
proof (cases \<open>possible_bit TYPE('a) n\<close>)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1351
  case False
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1352
  then show ?thesis
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1353
    by (auto dest: bit_imp_possible_bit)
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1354
next
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1355
  case True
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1356
  moreover have \<open>bit (NOT a) n \<longleftrightarrow> \<not> bit a n\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1357
  using \<open>possible_bit TYPE('a) n\<close> proof (induction n arbitrary: a)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1358
    case 0
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1359
    then show ?case
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1360
      by (simp add: bit_0)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1361
  next
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1362
    case (Suc n)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1363
    from Suc.prems Suc.IH [of \<open>a div 2\<close>]
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1364
    show ?case
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1365
      by (simp add: impossible_bit possible_bit_less_imp not_rec [of a] even_bit_succ_iff bit_double_iff flip: bit_Suc)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1366
  qed
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1367
  ultimately show ?thesis
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1368
    by simp
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1369
qed
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1370
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1371
lemma bit_not_exp_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1372
  \<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n \<noteq> m\<close>
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1373
  by (auto simp add: bit_not_iff bit_exp_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1374
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1375
lemma bit_minus_iff [bit_simps]:
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1376
  \<open>bit (- a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit (a - 1) n\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1377
  by (simp add: minus_eq_not_minus_1 bit_not_iff)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1378
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1379
lemma bit_minus_1_iff [simp]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1380
  \<open>bit (- 1) n \<longleftrightarrow> possible_bit TYPE('a) n\<close>
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1381
  by (simp add: bit_minus_iff)
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1382
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1383
lemma bit_minus_exp_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1384
  \<open>bit (- (2 ^ m)) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n \<ge> m\<close>
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1385
  by (auto simp add: bit_simps simp flip: mask_eq_exp_minus_1)
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1386
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1387
lemma bit_minus_2_iff [simp]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1388
  \<open>bit (- 2) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n > 0\<close>
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1389
  by (simp add: bit_minus_iff bit_1_iff)
71186
3d35e12999ba characterization of typical bit operations
haftmann
parents: 71181
diff changeset
  1390
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1391
lemma bit_not_iff_eq:
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1392
  \<open>bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1393
  by (simp add: bit_simps possible_bit_def)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79017
diff changeset
  1394
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  1395
lemma not_one_eq [simp]:
73969
ca2a35c0fe6e operations for symbolic computation of bit operations
haftmann
parents: 73871
diff changeset
  1396
  \<open>NOT 1 = - 2\<close>
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1397
  by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1398
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1399
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close>
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1400
  by standard (rule bit_eqI, simp add: bit_and_iff)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1401
74123
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1402
sublocale bit: abstract_boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close>
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1403
  by standard (auto simp add: bit_and_iff bit_or_iff bit_not_iff intro: bit_eqI)
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1404
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1405
sublocale bit: abstract_boolean_algebra_sym_diff \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> \<open>(XOR)\<close>
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1406
  apply standard
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1407
  apply (rule bit_eqI)
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1408
  apply (auto simp add: bit_simps)
7c5842b06114 clarified abstract and concrete boolean algebras
haftmann
parents: 74108
diff changeset
  1409
  done
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1410
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1411
lemma and_eq_not_not_or:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1412
  \<open>a AND b = NOT (NOT a OR NOT b)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1413
  by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1414
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1415
lemma or_eq_not_not_and:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1416
  \<open>a OR b = NOT (NOT a AND NOT b)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1417
  by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1418
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1419
lemma not_add_distrib:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1420
  \<open>NOT (a + b) = NOT a - b\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1421
  by (simp add: not_eq_complement algebra_simps)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1422
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1423
lemma not_diff_distrib:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1424
  \<open>NOT (a - b) = NOT a + b\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1425
  using not_add_distrib [of a \<open>- b\<close>] by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1426
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1427
lemma and_eq_minus_1_iff:
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1428
  \<open>a AND b = - 1 \<longleftrightarrow> a = - 1 \<and> b = - 1\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1429
  by (auto simp: bit_eq_iff bit_simps)
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1430
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1431
lemma disjunctive_diff:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1432
  \<open>a - b = a AND NOT b\<close> if \<open>\<And>n. bit b n \<Longrightarrow> bit a n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1433
proof -
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1434
  have \<open>NOT a + b = NOT a OR b\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1435
    by (rule disjunctive_add) (auto simp add: bit_not_iff dest: that)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1436
  then have \<open>NOT (NOT a + b) = NOT (NOT a OR b)\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1437
    by simp
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1438
  then show ?thesis
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1439
    by (simp add: not_add_distrib)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1440
qed
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1441
71412
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
  1442
lemma push_bit_minus:
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
  1443
  \<open>push_bit n (- a) = - push_bit n a\<close>
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
  1444
  by (simp add: push_bit_eq_mult)
96d126844adc more theorems
haftmann
parents: 71409
diff changeset
  1445
71409
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1446
lemma take_bit_not_take_bit:
0bb0cb558bf9 sketches of ideas still to come
haftmann
parents: 71195
diff changeset
  1447
  \<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
  1448
  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
  1449
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1450
lemma take_bit_not_iff:
73969
ca2a35c0fe6e operations for symbolic computation of bit operations
haftmann
parents: 73871
diff changeset
  1451
  \<open>take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b\<close>
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1452
  apply (simp add: bit_eq_iff)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1453
  apply (simp add: bit_not_iff bit_take_bit_iff bit_exp_iff)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1454
  apply (use exp_eq_0_imp_not_bit in blast)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1455
  done
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
  1456
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1457
lemma take_bit_not_eq_mask_diff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1458
  \<open>take_bit n (NOT a) = mask n - take_bit n a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1459
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1460
  have \<open>take_bit n (NOT a) = take_bit n (NOT (take_bit n a))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1461
    by (simp add: take_bit_not_take_bit)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1462
  also have \<open>\<dots> = mask n AND NOT (take_bit n a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1463
    by (simp add: take_bit_eq_mask ac_simps)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1464
  also have \<open>\<dots> = mask n - take_bit n a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1465
    by (subst disjunctive_diff)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1466
      (auto simp add: bit_take_bit_iff bit_mask_iff bit_imp_possible_bit)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1467
  finally show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1468
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1469
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1470
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
  1471
lemma mask_eq_take_bit_minus_one:
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
  1472
  \<open>mask n = take_bit n (- 1)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
  1473
  by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute)
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
  1474
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1475
lemma take_bit_minus_one_eq_mask [simp]:
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
  1476
  \<open>take_bit n (- 1) = mask n\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72028
diff changeset
  1477
  by (simp add: mask_eq_take_bit_minus_one)
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
  1478
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1479
lemma minus_exp_eq_not_mask:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1480
  \<open>- (2 ^ n) = NOT (mask n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1481
  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
  1482
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1483
lemma push_bit_minus_one_eq_not_mask [simp]:
71922
2c6a5c709f22 more theorems
haftmann
parents: 71921
diff changeset
  1484
  \<open>push_bit n (- 1) = NOT (mask n)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1485
  by (simp add: push_bit_eq_mult minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1486
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1487
lemma take_bit_not_mask_eq_0:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1488
  \<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  1489
  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
  1490
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1491
lemma unset_bit_eq_and_not:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1492
  \<open>unset_bit n a = a AND NOT (push_bit n 1)\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1493
  by (rule bit_eqI) (auto simp add: bit_simps)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
  1494
74497
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1495
lemma push_bit_Suc_minus_numeral [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1496
  \<open>push_bit (Suc n) (- numeral k) = push_bit n (- numeral (Num.Bit0 k))\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1497
  apply (simp only: numeral_Bit0)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1498
  apply simp
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1499
  apply (simp only: numeral_mult mult_2_right numeral_add)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1500
  done
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1501
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1502
lemma push_bit_minus_numeral [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1503
  \<open>push_bit (numeral l) (- numeral k) = push_bit (pred_numeral l) (- numeral (Num.Bit0 k))\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1504
  by (simp only: numeral_eq_Suc push_bit_Suc_minus_numeral)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  1505
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1506
lemma take_bit_Suc_minus_1_eq:
74498
27475e64a887 more complete simp rules
haftmann
parents: 74497
diff changeset
  1507
  \<open>take_bit (Suc n) (- 1) = 2 ^ Suc n - 1\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1508
  by (simp add: mask_eq_exp_minus_1)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1509
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1510
lemma take_bit_numeral_minus_1_eq:
74498
27475e64a887 more complete simp rules
haftmann
parents: 74497
diff changeset
  1511
  \<open>take_bit (numeral k) (- 1) = 2 ^ numeral k - 1\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1512
  by (simp add: mask_eq_exp_minus_1)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1513
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1514
lemma push_bit_mask_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1515
  \<open>push_bit m (mask n) = mask (n + m) AND NOT (mask m)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1516
  apply (rule bit_eqI)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1517
  apply (auto simp add: bit_simps not_less possible_bit_def)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1518
  apply (drule sym [of 0])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1519
  apply (simp only:)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1520
  using exp_not_zero_imp_exp_diff_not_zero apply (blast dest: exp_not_zero_imp_exp_diff_not_zero)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1521
  done
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1522
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1523
lemma slice_eq_mask:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1524
  \<open>push_bit n (take_bit m (drop_bit n a)) = a AND mask (m + n) AND NOT (mask n)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1525
  by (rule bit_eqI) (auto simp add: bit_simps)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1526
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1527
lemma push_bit_numeral_minus_1 [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1528
  \<open>push_bit (numeral n) (- 1) = - (2 ^ numeral n)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1529
  by (simp add: push_bit_eq_mult)
74498
27475e64a887 more complete simp rules
haftmann
parents: 74497
diff changeset
  1530
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1531
lemma unset_bit_eq:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1532
  \<open>unset_bit n a = a - of_bool (bit a n) * 2 ^ n\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1533
proof -
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1534
  have \<open>unset_bit n a = a AND NOT (of_bool (bit a n) * 2 ^ n)\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1535
    by (rule bit_eqI) (auto simp add: bit_simps)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1536
  then show ?thesis
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1537
    by (subst disjunctive_diff) (auto simp add: bit_simps simp flip: push_bit_eq_mult)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1538
qed
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1539
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1540
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1541
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1542
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1543
subsection \<open>Common algebraic structure\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1544
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1545
class linordered_euclidean_semiring_bit_operations =
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1546
  linordered_euclidean_semiring + semiring_bit_operations
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1547
begin
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1548
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1549
lemma possible_bit [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1550
  \<open>possible_bit TYPE('a) n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1551
  by (simp add: possible_bit_def)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1552
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1553
lemma take_bit_of_exp [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1554
  \<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1555
  by (simp add: take_bit_eq_mod exp_mod_exp)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1556
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1557
lemma take_bit_of_2 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1558
  \<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1559
  using take_bit_of_exp [of n 1] by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1560
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1561
lemma push_bit_eq_0_iff [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1562
  \<open>push_bit n a = 0 \<longleftrightarrow> a = 0\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1563
  by (simp add: push_bit_eq_mult)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1564
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1565
lemma take_bit_add:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1566
  \<open>take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1567
  by (simp add: take_bit_eq_mod mod_simps)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1568
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1569
lemma take_bit_of_1_eq_0_iff [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1570
  \<open>take_bit n 1 = 0 \<longleftrightarrow> n = 0\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1571
  by (simp add: take_bit_eq_mod)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1572
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1573
lemma drop_bit_Suc_bit0 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1574
  \<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1575
  by (simp add: drop_bit_Suc numeral_Bit0_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1576
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1577
lemma drop_bit_Suc_bit1 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1578
  \<open>drop_bit (Suc n) (numeral (Num.Bit1 k)) = drop_bit n (numeral k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1579
  by (simp add: drop_bit_Suc numeral_Bit1_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1580
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1581
lemma drop_bit_numeral_bit0 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1582
  \<open>drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1583
  by (simp add: drop_bit_rec numeral_Bit0_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1584
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1585
lemma drop_bit_numeral_bit1 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1586
  \<open>drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1587
  by (simp add: drop_bit_rec numeral_Bit1_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1588
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1589
lemma take_bit_Suc_1 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1590
  \<open>take_bit (Suc n) 1 = 1\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1591
  by (simp add: take_bit_Suc)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1592
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1593
lemma take_bit_Suc_bit0:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1594
  \<open>take_bit (Suc n) (numeral (Num.Bit0 k)) = take_bit n (numeral k) * 2\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1595
  by (simp add: take_bit_Suc numeral_Bit0_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1596
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1597
lemma take_bit_Suc_bit1:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1598
  \<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1599
  by (simp add: take_bit_Suc numeral_Bit1_div_2 mod_2_eq_odd)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1600
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1601
lemma take_bit_numeral_1 [simp]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1602
  \<open>take_bit (numeral l) 1 = 1\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1603
  by (simp add: take_bit_rec [of \<open>numeral l\<close> 1])
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1604
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1605
lemma take_bit_numeral_bit0:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1606
  \<open>take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1607
  by (simp add: take_bit_rec numeral_Bit0_div_2)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1608
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1609
lemma take_bit_numeral_bit1:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1610
  \<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1611
  by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1612
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1613
lemma bit_of_nat_iff_bit [bit_simps]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1614
  \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1615
proof -
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1616
  have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1617
    by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1618
  also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1619
    by (simp add: of_nat_div)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1620
  finally show ?thesis
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1621
    by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1622
qed
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1623
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1624
lemma drop_bit_mask_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1625
  \<open>drop_bit m (mask n) = mask (n - m)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1626
  by (rule bit_eqI) (auto simp add: bit_simps possible_bit_def)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1627
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1628
lemma bit_push_bit_iff':
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1629
  \<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> bit a (n - m)\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1630
  by (simp add: bit_simps)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1631
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1632
lemma mask_half:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1633
  \<open>mask n div 2 = mask (n - 1)\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1634
  by (cases n) (simp_all add: mask_Suc_double one_or_eq)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1635
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1636
lemma take_bit_Suc_from_most:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1637
  \<open>take_bit (Suc n) a = 2 ^ n * of_bool (bit a n) + take_bit n a\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1638
  using mod_mult2_eq' [of a \<open>2 ^ n\<close> 2]
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1639
  by (simp only: take_bit_eq_mod power_Suc2)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1640
    (simp_all add: bit_iff_odd odd_iff_mod_2_eq_one)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1641
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1642
lemma take_bit_nonnegative [simp]:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1643
  \<open>0 \<le> take_bit n a\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1644
  using horner_sum_nonnegative by (simp flip: horner_sum_bit_eq_take_bit)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1645
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1646
lemma not_take_bit_negative [simp]:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1647
  \<open>\<not> take_bit n a < 0\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1648
  by (simp add: not_less)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1649
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1650
lemma bit_imp_take_bit_positive:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1651
  \<open>0 < take_bit m a\<close> if \<open>n < m\<close> and \<open>bit a n\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1652
proof (rule ccontr)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1653
  assume \<open>\<not> 0 < take_bit m a\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1654
  then have \<open>take_bit m a = 0\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1655
    by (auto simp add: not_less intro: order_antisym)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1656
  then have \<open>bit (take_bit m a) n = bit 0 n\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1657
    by simp
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1658
  with that show False
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1659
    by (simp add: bit_take_bit_iff)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1660
qed
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1661
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1662
lemma take_bit_mult:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1663
  \<open>take_bit n (take_bit n a * take_bit n b) = take_bit n (a * b)\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1664
  by (simp add: take_bit_eq_mod mod_mult_eq)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1665
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1666
lemma drop_bit_push_bit:
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1667
  \<open>drop_bit m (push_bit n a) = drop_bit (m - n) (push_bit (n - m) a)\<close>
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1668
  by (cases \<open>m \<le> n\<close>)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1669
    (auto simp add: mult.left_commute [of _ \<open>2 ^ n\<close>] mult.commute [of _ \<open>2 ^ n\<close>] mult.assoc
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1670
    mult.commute [of a] drop_bit_eq_div push_bit_eq_mult not_le power_add Orderings.not_le dest!: le_Suc_ex less_imp_Suc_add)
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1671
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1672
end
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1673
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1674
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
  1675
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
  1676
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1677
locale fold2_bit_int =
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1678
  fixes f :: \<open>bool \<Rightarrow> bool \<Rightarrow> bool\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1679
begin
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1680
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1681
context
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1682
begin
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1683
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1684
function F :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1685
  where \<open>F k l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1686
    then - of_bool (f (odd k) (odd l))
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1687
    else of_bool (f (odd k) (odd l)) + 2 * (F (k div 2) (l div 2)))\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1688
  by auto
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1689
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1690
private termination proof (relation \<open>measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>)
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1691
  have less_eq: \<open>\<bar>k div 2\<bar> \<le> \<bar>k\<bar>\<close> for k :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1692
    by (cases k) (simp_all add: divide_int_def nat_add_distrib)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1693
  then have less: \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close> if \<open>k \<notin> {0, - 1}\<close> for k :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  1694
    using that by (auto simp add: less_le [of k])
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1695
  show \<open>wf (measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>)))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1696
    by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1697
  show \<open>((k div 2, l div 2), k, l) \<in> measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1698
    if \<open>\<not> (k \<in> {0, - 1} \<and> l \<in> {0, - 1})\<close> for k l
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1699
  proof -
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1700
    from that have *: \<open>k \<notin> {0, - 1} \<or> l \<notin> {0, - 1}\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1701
      by simp
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1702
    then have \<open>0 < \<bar>k\<bar> + \<bar>l\<bar>\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1703
      by auto
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1704
    moreover from * have \<open>\<bar>k div 2\<bar> + \<bar>l div 2\<bar> < \<bar>k\<bar> + \<bar>l\<bar>\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1705
    proof
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1706
      assume \<open>k \<notin> {0, - 1}\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1707
      then have \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1708
        by (rule less)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1709
      with less_eq [of l] show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1710
        by auto
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1711
    next
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1712
      assume \<open>l \<notin> {0, - 1}\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1713
      then have \<open>\<bar>l div 2\<bar> < \<bar>l\<bar>\<close>
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1714
        by (rule less)
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1715
      with less_eq [of k] show ?thesis
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1716
        by auto
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1717
    qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1718
    ultimately show ?thesis
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1719
      by (simp only: in_measure split_def fst_conv snd_conv nat_mono_iff)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1720
  qed
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1721
qed
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1722
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1723
declare F.simps [simp del]
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1724
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1725
lemma rec:
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1726
  \<open>F k l = of_bool (f (odd k) (odd l)) + 2 * (F (k div 2) (l div 2))\<close>
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1727
    for k l :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1728
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
  1729
  case True
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1730
  then show ?thesis
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1731
    by (auto simp add: F.simps [of 0] F.simps [of \<open>- 1\<close>])
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1732
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1733
  case False
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1734
  then show ?thesis
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1735
    by (auto simp add: ac_simps F.simps [of k l])
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1736
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1737
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1738
lemma bit_iff:
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1739
  \<open>bit (F k l) n \<longleftrightarrow> f (bit k n) (bit l n)\<close> for k l :: int
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1740
proof (induction n arbitrary: k l)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1741
  case 0
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1742
  then show ?case
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1743
    by (simp add: rec [of k l] bit_0)
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1744
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1745
  case (Suc n)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1746
  then show ?case
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1747
    by (simp add: rec [of k l] bit_Suc)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1748
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1749
79030
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1750
end
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1751
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1752
end
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1753
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1754
instantiation int :: ring_bit_operations
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1755
begin
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1756
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1757
definition not_int :: \<open>int \<Rightarrow> int\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1758
  where \<open>not_int k = - k - 1\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1759
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1760
global_interpretation and_int: fold2_bit_int \<open>(\<and>)\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1761
  defines and_int = and_int.F .
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1762
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1763
global_interpretation or_int: fold2_bit_int \<open>(\<or>)\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1764
  defines or_int = or_int.F .
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1765
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1766
global_interpretation xor_int: fold2_bit_int \<open>(\<noteq>)\<close>
bdea2b95817b more direct characterization of binary bit operations
haftmann
parents: 79018
diff changeset
  1767
  defines xor_int = xor_int.F .
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1768
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1769
definition mask_int :: \<open>nat \<Rightarrow> int\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1770
  where \<open>mask n = (2 :: int) ^ n - 1\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1771
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1772
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1773
  where \<open>push_bit_int n k = k * 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1774
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1775
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1776
  where \<open>drop_bit_int n k = k div 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1777
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1778
definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1779
  where \<open>take_bit_int n k = k mod 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1780
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1781
definition set_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1782
  where \<open>set_bit n k = k OR push_bit n 1\<close> for k :: int
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1783
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1784
definition unset_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1785
  where \<open>unset_bit n k = k AND NOT (push_bit n 1)\<close> for k :: int
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1786
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1787
definition flip_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1788
  where \<open>flip_bit n k = k XOR push_bit n 1\<close> for k :: int
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1789
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1790
lemma not_int_div_2:
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1791
  \<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1792
  by (simp add: not_int_def)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1793
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1794
lemma bit_not_int_iff:
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1795
  \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> for k :: int
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1796
proof (rule sym, induction n arbitrary: k)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1797
  case 0
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1798
  then show ?case
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1799
    by (simp add: bit_0 not_int_def)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1800
next
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1801
  case (Suc n)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1802
  then show ?case
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1803
    by (simp add: bit_Suc not_int_div_2)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1804
qed
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1805
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1806
instance proof
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1807
  fix k l :: int and m n :: nat
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1808
  show \<open>unset_bit n k = (k OR push_bit n 1) XOR push_bit n 1\<close>
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  1809
    by (rule bit_eqI)
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1810
      (auto simp add: unset_bit_int_def
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  1811
        and_int.bit_iff or_int.bit_iff xor_int.bit_iff bit_not_int_iff push_bit_int_def bit_simps)
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  1812
qed (fact and_int.rec or_int.rec xor_int.rec mask_int_def set_bit_int_def flip_bit_int_def
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  1813
  push_bit_int_def drop_bit_int_def take_bit_int_def 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
  1814
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1815
end
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff changeset
  1816
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1817
instance int :: linordered_euclidean_semiring_bit_operations ..
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1818
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1819
context ring_bit_operations
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1820
begin
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1821
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1822
lemma even_of_int_iff:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1823
  \<open>even (of_int k) \<longleftrightarrow> even k\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1824
  by (induction k rule: int_bit_induct) simp_all
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1825
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1826
lemma bit_of_int_iff [bit_simps]:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1827
  \<open>bit (of_int k) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit k n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1828
proof (cases \<open>possible_bit TYPE('a) n\<close>)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1829
  case False
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1830
  then show ?thesis
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1831
    by (simp add: impossible_bit)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1832
next
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1833
  case True
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1834
  then have \<open>bit (of_int k) n \<longleftrightarrow> bit k n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1835
  proof (induction k arbitrary: n rule: int_bit_induct)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1836
    case zero
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1837
    then show ?case
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1838
      by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1839
  next
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1840
    case minus
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1841
    then show ?case
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1842
      by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1843
  next
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1844
    case (even k)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1845
    then show ?case
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1846
      using bit_double_iff [of \<open>of_int k\<close> n] Bit_Operations.bit_double_iff [of k n]
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1847
      by (cases n) (auto simp add: ac_simps possible_bit_def dest: mult_not_zero)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1848
  next
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1849
    case (odd k)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1850
    then show ?case
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1851
      using bit_double_iff [of \<open>of_int k\<close> n]
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1852
      by (cases n)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1853
        (auto simp add: ac_simps bit_double_iff even_bit_succ_iff Bit_Operations.bit_0 Bit_Operations.bit_Suc
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1854
          possible_bit_def dest: mult_not_zero)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1855
  qed
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1856
  with True show ?thesis
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1857
    by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1858
qed
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1859
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1860
lemma push_bit_of_int:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1861
  \<open>push_bit n (of_int k) = of_int (push_bit n k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1862
  by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1863
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1864
lemma of_int_push_bit:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1865
  \<open>of_int (push_bit n k) = push_bit n (of_int k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1866
  by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1867
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1868
lemma take_bit_of_int:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1869
  \<open>take_bit n (of_int k) = of_int (take_bit n k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1870
  by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1871
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1872
lemma of_int_take_bit:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1873
  \<open>of_int (take_bit n k) = take_bit n (of_int k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1874
  by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1875
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1876
lemma of_int_not_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1877
  \<open>of_int (NOT k) = NOT (of_int k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1878
  by (rule bit_eqI) (simp add: bit_not_iff Bit_Operations.bit_not_iff bit_of_int_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1879
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1880
lemma of_int_not_numeral:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1881
  \<open>of_int (NOT (numeral k)) = NOT (numeral k)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1882
  by (simp add: local.of_int_not_eq)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1883
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1884
lemma of_int_and_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1885
  \<open>of_int (k AND l) = of_int k AND of_int l\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1886
  by (rule bit_eqI) (simp add: bit_of_int_iff bit_and_iff Bit_Operations.bit_and_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1887
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1888
lemma of_int_or_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1889
  \<open>of_int (k OR l) = of_int k OR of_int l\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1890
  by (rule bit_eqI) (simp add: bit_of_int_iff bit_or_iff Bit_Operations.bit_or_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1891
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1892
lemma of_int_xor_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1893
  \<open>of_int (k XOR l) = of_int k XOR of_int l\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1894
  by (rule bit_eqI) (simp add: bit_of_int_iff bit_xor_iff Bit_Operations.bit_xor_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1895
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1896
lemma of_int_mask_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1897
  \<open>of_int (mask n) = mask n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1898
  by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_int_or_eq)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1899
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1900
end
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  1901
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1902
lemma take_bit_int_less_exp [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1903
  \<open>take_bit n k < 2 ^ n\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1904
  by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1905
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1906
lemma take_bit_int_eq_self_iff:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1907
  \<open>take_bit n k = k \<longleftrightarrow> 0 \<le> k \<and> k < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1908
  for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1909
proof
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1910
  assume ?P
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1911
  moreover note take_bit_int_less_exp [of n k] take_bit_nonnegative [of n k]
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1912
  ultimately show ?Q
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1913
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1914
next
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1915
  assume ?Q
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1916
  then show ?P
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1917
    by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1918
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1919
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1920
lemma take_bit_int_eq_self:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1921
  \<open>take_bit n k = k\<close> if \<open>0 \<le> k\<close> \<open>k < 2 ^ n\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1922
  using that by (simp add: take_bit_int_eq_self_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1923
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1924
lemma mask_nonnegative_int [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1925
  \<open>mask n \<ge> (0::int)\<close>
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  1926
  by (simp add: mask_eq_exp_minus_1 add_le_imp_le_diff)
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1927
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1928
lemma not_mask_negative_int [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1929
  \<open>\<not> mask n < (0::int)\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1930
  by (simp add: not_less)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  1931
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1932
lemma not_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1933
  \<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1934
  by (simp add: not_int_def)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1935
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1936
lemma not_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1937
  \<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1938
  by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1939
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1940
lemma and_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1941
  \<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
  1942
proof (induction k arbitrary: l rule: int_bit_induct)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1943
  case zero
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1944
  then show ?case
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1945
    by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1946
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1947
  case minus
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1948
  then show ?case
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1949
    by simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1950
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1951
  case (even k)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1952
  then show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1953
    using and_int.rec [of \<open>k * 2\<close> l]
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1954
    by (simp add: pos_imp_zdiv_nonneg_iff zero_le_mult_iff)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1955
next
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1956
  case (odd k)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1957
  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
  1958
    by simp
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1959
  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>
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1960
    by simp
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1961
  with and_int.rec [of \<open>1 + k * 2\<close> l]
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1962
  show ?case
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1963
    by (auto simp add: zero_le_mult_iff not_le)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1964
qed
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1965
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1966
lemma and_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1967
  \<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1968
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1969
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1970
lemma and_less_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1971
  \<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
  1972
using that proof (induction k arbitrary: l rule: int_bit_induct)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1973
  case zero
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1974
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1975
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1976
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1977
  case minus
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1978
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1979
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1980
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1981
  case (even k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1982
  from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1983
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1984
    by (simp add: and_int.rec [of _ l])
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1985
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1986
  case (odd k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1987
  from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1988
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  1989
    by (simp add: and_int.rec [of _ l])
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1990
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  1991
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1992
lemma or_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1993
  \<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
  1994
  by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1995
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1996
lemma or_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1997
  \<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1998
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  1999
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2000
lemma or_greater_eq:
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2001
  \<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
  2002
using that proof (induction k arbitrary: l rule: int_bit_induct)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2003
  case zero
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2004
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2005
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2006
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2007
  case minus
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2008
  then show ?case
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2009
    by simp
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2010
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2011
  case (even k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2012
  from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2013
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2014
    by (simp add: or_int.rec [of _ l])
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2015
next
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2016
  case (odd k)
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2017
  from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2018
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2019
    by (simp add: or_int.rec [of _ l])
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2020
qed
febdd4eead56 more on single-bit operations
haftmann
parents: 71991
diff changeset
  2021
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2022
lemma xor_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2023
  \<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
  2024
  by (simp only: bit.xor_def or_nonnegative_int_iff) auto
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2025
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2026
lemma xor_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2027
  \<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2028
  by (subst Not_eq_iff [symmetric]) (auto simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2029
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2030
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2031
  \<open>x OR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2032
using that proof (induction x arbitrary: y n rule: int_bit_induct)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2033
  case zero
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2034
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2035
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2036
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2037
  case minus
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2038
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2039
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2040
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2041
  case (even x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2042
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2043
  show ?case
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2044
    by (cases n) (auto simp add: or_int.rec [of \<open>_ * 2\<close>] elim: oddE)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2045
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2046
  case (odd x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2047
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2048
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2049
    by (cases n) (auto simp add: or_int.rec [of \<open>1 + _ * 2\<close>], linarith)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2050
qed
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2051
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2052
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2053
  \<open>x XOR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2054
using that proof (induction x arbitrary: y n rule: int_bit_induct)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2055
  case zero
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2056
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2057
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2058
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2059
  case minus
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2060
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2061
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2062
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2063
  case (even x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2064
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2065
  show ?case
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2066
    by (cases n) (auto simp add: xor_int.rec [of \<open>_ * 2\<close>] elim: oddE)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2067
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2068
  case (odd x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2069
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2070
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2071
    by (cases n) (auto simp add: xor_int.rec [of \<open>1 + _ * 2\<close>])
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2072
qed
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2073
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2074
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2075
  \<open>0 \<le> x AND y\<close> if \<open>0 \<le> x\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2076
  using that by simp
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2077
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2078
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2079
  \<open>0 \<le> x OR y\<close> if \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2080
  using that by simp
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2081
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2082
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2083
  \<open>0 \<le> x XOR y\<close> if \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2084
  using that by simp
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2085
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2086
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2087
  \<open>x AND y \<le> x\<close> if \<open>0 \<le> x\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2088
using that proof (induction x arbitrary: y rule: int_bit_induct)
73535
0f33c7031ec9 new lemmas
haftmann
parents: 72830
diff changeset
  2089
  case (odd k)
0f33c7031ec9 new lemmas
haftmann
parents: 72830
diff changeset
  2090
  then have \<open>k AND y div 2 \<le> k\<close>
0f33c7031ec9 new lemmas
haftmann
parents: 72830
diff changeset
  2091
    by simp
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2092
  then show ?case
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2093
    by (simp add: and_int.rec [of \<open>1 + _ * 2\<close>])
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2094
qed (simp_all add: and_int.rec [of \<open>_ * 2\<close>])
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2095
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2096
lemma AND_upper1' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2097
  \<open>y AND x \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y z :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2098
  using _ \<open>y \<le> z\<close> by (rule order_trans) (use \<open>0 \<le> y\<close> in simp)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2099
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2100
lemma AND_upper1'' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2101
  \<open>y AND x < z\<close> if \<open>0 \<le> y\<close> \<open>y < z\<close> for x y z :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2102
  using _ \<open>y < z\<close> by (rule order_le_less_trans) (use \<open>0 \<le> y\<close> in simp)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2103
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2104
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2105
  \<open>x AND y \<le> y\<close> if \<open>0 \<le> y\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2106
  using that AND_upper1 [of y x] by (simp add: ac_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2107
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2108
lemma AND_upper2' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2109
  \<open>x AND y \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2110
  using that AND_upper1' [of y z x] by (simp add: ac_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2111
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2112
lemma AND_upper2'' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2113
  \<open>x AND y < z\<close> if \<open>0 \<le> y\<close> \<open>y < z\<close> for x y :: int
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2114
  using that AND_upper1'' [of y z x] by (simp add: ac_simps)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2115
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2116
lemma plus_and_or:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2117
  \<open>(x AND y) + (x OR y) = x + y\<close> for x y :: int
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2118
proof (induction x arbitrary: y rule: int_bit_induct)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2119
  case zero
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2120
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2121
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2122
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2123
  case minus
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2124
  then show ?case
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2125
    by simp
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2126
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2127
  case (even x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2128
  from even.IH [of \<open>y div 2\<close>]
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2129
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2130
    by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2131
next
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2132
  case (odd x)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2133
  from odd.IH [of \<open>y div 2\<close>]
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2134
  show ?case
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2135
    by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2136
qed
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  2137
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2138
lemma push_bit_minus_one:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2139
  \<open>push_bit n (- 1 :: int) = - (2 ^ n)\<close>
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2140
  by (simp add: push_bit_eq_mult)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2141
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2142
lemma minus_1_div_exp_eq_int:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2143
  \<open>- 1 div (2 :: int) ^ n = - 1\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2144
  by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2145
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2146
lemma drop_bit_minus_one [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2147
  \<open>drop_bit n (- 1 :: int) = - 1\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2148
  by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2149
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2150
lemma take_bit_minus:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2151
  \<open>take_bit n (- take_bit n k) = take_bit n (- k)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2152
    for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2153
  by (simp add: take_bit_eq_mod mod_minus_eq)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2154
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2155
lemma take_bit_diff:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2156
  \<open>take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2157
    for k l :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2158
  by (simp add: take_bit_eq_mod mod_diff_eq)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2159
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2160
lemma (in ring_1) of_nat_nat_take_bit_eq [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2161
  \<open>of_nat (nat (take_bit n k)) = of_int (take_bit n k)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2162
  by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2163
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2164
lemma take_bit_minus_small_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2165
  \<open>take_bit n (- k) = 2 ^ n - k\<close> if \<open>0 < k\<close> \<open>k \<le> 2 ^ n\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2166
proof -
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2167
  define m where \<open>m = nat k\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2168
  with that have \<open>k = int m\<close> and \<open>0 < m\<close> and \<open>m \<le> 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2169
    by simp_all
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2170
  have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2171
    using \<open>0 < m\<close> by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2172
  then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2173
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2174
  then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2175
    using \<open>m \<le> 2 ^ n\<close> by (simp only: of_nat_mod of_nat_diff) simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2176
  with \<open>k = int m\<close> have \<open>(2 ^ n - k) mod 2 ^ n = 2 ^ n - k\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2177
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2178
  then show ?thesis
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2179
    by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2180
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2181
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2182
lemma push_bit_nonnegative_int_iff [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2183
  \<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2184
  by (simp add: push_bit_eq_mult zero_le_mult_iff power_le_zero_eq)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2185
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2186
lemma push_bit_negative_int_iff [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2187
  \<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2188
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2189
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2190
lemma drop_bit_nonnegative_int_iff [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2191
  \<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2192
  by (induction n) (auto simp add: drop_bit_Suc drop_bit_half)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2193
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2194
lemma drop_bit_negative_int_iff [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2195
  \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2196
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2197
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2198
lemma set_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2199
  \<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2200
  by (simp add: set_bit_eq_or)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2201
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2202
lemma set_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2203
  \<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2204
  by (simp add: set_bit_eq_or)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2205
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2206
lemma unset_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2207
  \<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2208
  by (simp add: unset_bit_eq_and_not)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2209
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2210
lemma unset_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2211
  \<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2212
  by (simp add: unset_bit_eq_and_not)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2213
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2214
lemma flip_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2215
  \<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2216
  by (simp add: flip_bit_eq_xor)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2217
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2218
lemma flip_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2219
  \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2220
  by (simp add: flip_bit_eq_xor)
71802
ab3cecb836b5 more rules
haftmann
parents: 71800
diff changeset
  2221
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2222
lemma set_bit_greater_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2223
  \<open>set_bit n k \<ge> k\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2224
  by (simp add: set_bit_eq_or or_greater_eq)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2225
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2226
lemma unset_bit_less_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2227
  \<open>unset_bit n k \<le> k\<close> for k :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2228
  by (simp add: unset_bit_eq_and_not and_less_eq)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  2229
75651
f4116b7a6679 Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents: 75138
diff changeset
  2230
lemma and_int_unfold:
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2231
  \<open>k AND l = (if k = 0 \<or> l = 0 then 0 else if k = - 1 then l else if l = - 1 then k
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2232
    else (k mod 2) * (l mod 2) + 2 * ((k div 2) AND (l div 2)))\<close> for k l :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2233
  by (auto simp add: and_int.rec [of k l] zmult_eq_1_iff elim: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2234
75651
f4116b7a6679 Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents: 75138
diff changeset
  2235
lemma or_int_unfold:
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2236
  \<open>k OR l = (if k = - 1 \<or> l = - 1 then - 1 else if k = 0 then l else if l = 0 then k
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2237
    else max (k mod 2) (l mod 2) + 2 * ((k div 2) OR (l div 2)))\<close> for k l :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2238
  by (auto simp add: or_int.rec [of k l] elim: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2239
75651
f4116b7a6679 Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents: 75138
diff changeset
  2240
lemma xor_int_unfold:
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2241
  \<open>k XOR l = (if k = - 1 then NOT l else if l = - 1 then NOT k else if k = 0 then l else if l = 0 then k
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2242
    else \<bar>k mod 2 - l mod 2\<bar> + 2 * ((k div 2) XOR (l div 2)))\<close> for k l :: int
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2243
  by (auto simp add: xor_int.rec [of k l] not_int_def elim!: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2244
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2245
lemma bit_minus_int_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2246
  \<open>bit (- k) n \<longleftrightarrow> bit (NOT (k - 1)) n\<close> for k :: int
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2247
  by (simp add: bit_simps)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2248
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2249
lemma take_bit_incr_eq:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2250
  \<open>take_bit n (k + 1) = 1 + take_bit n k\<close> if \<open>take_bit n k \<noteq> 2 ^ n - 1\<close> for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2251
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2252
  from that have \<open>2 ^ n \<noteq> k mod 2 ^ n + 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2253
    by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2254
  moreover have \<open>k mod 2 ^ n < 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2255
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2256
  ultimately have *: \<open>k mod 2 ^ n + 1 < 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2257
    by linarith
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2258
  have \<open>(k + 1) mod 2 ^ n = (k mod 2 ^ n + 1) mod 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2259
    by (simp add: mod_simps)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2260
  also have \<open>\<dots> = k mod 2 ^ n + 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2261
    using * by (simp add: zmod_trivial_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2262
  finally have \<open>(k + 1) mod 2 ^ n = k mod 2 ^ n + 1\<close> .
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2263
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2264
    by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2265
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2266
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2267
lemma take_bit_decr_eq:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2268
  \<open>take_bit n (k - 1) = take_bit n k - 1\<close> if \<open>take_bit n k \<noteq> 0\<close> for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2269
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2270
  from that have \<open>k mod 2 ^ n \<noteq> 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2271
    by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2272
  moreover have \<open>k mod 2 ^ n \<ge> 0\<close> \<open>k mod 2 ^ n < 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2273
    by simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2274
  ultimately have *: \<open>k mod 2 ^ n > 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2275
    by linarith
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2276
  have \<open>(k - 1) mod 2 ^ n = (k mod 2 ^ n - 1) mod 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2277
    by (simp add: mod_simps)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2278
  also have \<open>\<dots> = k mod 2 ^ n - 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2279
    by (simp add: zmod_trivial_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2280
      (use \<open>k mod 2 ^ n < 2 ^ n\<close> * in linarith)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2281
  finally have \<open>(k - 1) mod 2 ^ n = k mod 2 ^ n - 1\<close> .
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2282
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2283
    by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2284
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2285
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2286
lemma take_bit_int_greater_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2287
  \<open>k + 2 ^ n \<le> take_bit n k\<close> if \<open>k < 0\<close> for k :: int
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2288
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2289
  have \<open>k + 2 ^ n \<le> take_bit n (k + 2 ^ n)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2290
  proof (cases \<open>k > - (2 ^ n)\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2291
    case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2292
    then have \<open>k + 2 ^ n \<le> 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2293
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2294
    also note take_bit_nonnegative
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2295
    finally show ?thesis .
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2296
  next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2297
    case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2298
    with that have \<open>0 \<le> k + 2 ^ n\<close> and \<open>k + 2 ^ n < 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2299
      by simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2300
    then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2301
      by (simp only: take_bit_eq_mod mod_pos_pos_trivial)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2302
  qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2303
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2304
    by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2305
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2306
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2307
lemma take_bit_int_less_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2308
  \<open>take_bit n k \<le> k - 2 ^ n\<close> if \<open>2 ^ n \<le> k\<close> and \<open>n > 0\<close> for k :: int
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2309
  using that zmod_le_nonneg_dividend [of \<open>k - 2 ^ n\<close> \<open>2 ^ n\<close>]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2310
  by (simp add: take_bit_eq_mod)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2311
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2312
lemma take_bit_int_less_eq_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2313
  \<open>take_bit n k \<le> k \<longleftrightarrow> 0 \<le> k\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2314
proof
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2315
  assume ?P
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2316
  show ?Q
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2317
  proof (rule ccontr)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2318
    assume \<open>\<not> 0 \<le> k\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2319
    then have \<open>k < 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2320
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2321
    with \<open>?P\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2322
    have \<open>take_bit n k < 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2323
      by (rule le_less_trans)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2324
    then show False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2325
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2326
  qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2327
next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2328
  assume ?Q
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2329
  then show ?P
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2330
    by (simp add: take_bit_eq_mod zmod_le_nonneg_dividend)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2331
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2332
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2333
lemma take_bit_int_less_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2334
  \<open>take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2335
  by (auto simp add: less_le take_bit_int_less_eq_self_iff take_bit_int_eq_self_iff
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2336
    intro: order_trans [of 0 \<open>2 ^ n\<close> k])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2337
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2338
lemma take_bit_int_greater_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2339
  \<open>k < take_bit n k \<longleftrightarrow> k < 0\<close> for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2340
  using take_bit_int_less_eq_self_iff [of n k] by auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2341
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2342
lemma take_bit_int_greater_eq_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2343
  \<open>k \<le> take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> for k :: int
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2344
  by (auto simp add: le_less take_bit_int_greater_self_iff take_bit_int_eq_self_iff
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2345
    dest: sym not_sym intro: less_trans [of k 0 \<open>2 ^ n\<close>])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2346
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2347
lemma take_bit_tightened_less_eq_int:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2348
  \<open>take_bit m k \<le> take_bit n k\<close> if \<open>m \<le> n\<close> for k :: int
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2349
proof -
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2350
  have \<open>take_bit m (take_bit n k) \<le> take_bit n k\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2351
    by (simp only: take_bit_int_less_eq_self_iff take_bit_nonnegative)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2352
  with that show ?thesis
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2353
    by simp
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2354
qed
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2355
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2356
lemma not_exp_less_eq_0_int [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2357
  \<open>\<not> 2 ^ n \<le> (0::int)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2358
  by (simp add: power_le_zero_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2359
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2360
lemma int_bit_bound:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2361
  fixes k :: int
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2362
  obtains n where \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2363
    and \<open>n > 0 \<Longrightarrow> bit k (n - 1) \<noteq> bit k n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2364
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2365
  obtain q where *: \<open>\<And>m. q \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2366
  proof (cases \<open>k \<ge> 0\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2367
    case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2368
    moreover from power_gt_expt [of 2 \<open>nat k\<close>]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2369
    have \<open>nat k < 2 ^ nat k\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2370
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2371
    then have \<open>int (nat k) < int (2 ^ nat k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2372
      by (simp only: of_nat_less_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2373
    ultimately have *: \<open>k div 2 ^ nat k = 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2374
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2375
    show thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2376
    proof (rule that [of \<open>nat k\<close>])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2377
      fix m
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2378
      assume \<open>nat k \<le> m\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2379
      then show \<open>bit k m \<longleftrightarrow> bit k (nat k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2380
        by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq dest!: le_Suc_ex)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2381
    qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2382
  next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2383
    case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2384
    moreover from power_gt_expt [of 2 \<open>nat (- k)\<close>]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2385
    have \<open>nat (- k) < 2 ^ nat (- k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2386
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2387
    then have \<open>int (nat (- k)) < int (2 ^ nat (- k))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2388
      by (simp only: of_nat_less_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2389
    ultimately have \<open>- k div - (2 ^ nat (- k)) = - 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2390
      by (subst div_pos_neg_trivial) simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2391
    then have *: \<open>k div 2 ^ nat (- k) = - 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2392
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2393
    show thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2394
    proof (rule that [of \<open>nat (- k)\<close>])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2395
      fix m
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2396
      assume \<open>nat (- k) \<le> m\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2397
      then show \<open>bit k m \<longleftrightarrow> bit k (nat (- k))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2398
        by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq minus_1_div_exp_eq_int dest!: le_Suc_ex)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2399
    qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2400
  qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2401
  show thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2402
  proof (cases \<open>\<forall>m. bit k m \<longleftrightarrow> bit k q\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2403
    case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2404
    then have \<open>bit k 0 \<longleftrightarrow> bit k q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2405
      by blast
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2406
    with True that [of 0] show thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2407
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2408
  next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2409
    case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2410
    then obtain r where **: \<open>bit k r \<noteq> bit k q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2411
      by blast
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2412
    have \<open>r < q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2413
      by (rule ccontr) (use * [of r] ** in simp)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2414
    define N where \<open>N = {n. n < q \<and> bit k n \<noteq> bit k q}\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2415
    moreover have \<open>finite N\<close> \<open>r \<in> N\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2416
      using ** N_def \<open>r < q\<close> by auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2417
    moreover define n where \<open>n = Suc (Max N)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2418
    ultimately have \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2419
      apply auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2420
         apply (metis (full_types, lifting) "*" Max_ge_iff Suc_n_not_le_n \<open>finite N\<close> all_not_in_conv mem_Collect_eq not_le)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2421
        apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2422
        apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2423
      apply (metis (full_types, lifting) "*" Max_ge_iff Suc_n_not_le_n \<open>finite N\<close> all_not_in_conv mem_Collect_eq not_le)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2424
      done
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2425
    have \<open>bit k (Max N) \<noteq> bit k n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2426
      by (metis (mono_tags, lifting) "*" Max_in N_def \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m = bit k n\<close> \<open>finite N\<close> \<open>r \<in> N\<close> empty_iff le_cases mem_Collect_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2427
    show thesis apply (rule that [of n])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2428
      using \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m = bit k n\<close> apply blast
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2429
      using \<open>bit k (Max N) \<noteq> bit k n\<close> n_def by auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2430
  qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2431
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2432
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2433
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2434
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2435
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2436
instantiation nat :: semiring_bit_operations
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2437
begin
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2438
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2439
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2440
  where \<open>m AND n = nat (int m AND int n)\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2441
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2442
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2443
  where \<open>m OR n = nat (int m OR int n)\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2444
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2445
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2446
  where \<open>m XOR n = nat (int m XOR int n)\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2447
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2448
definition mask_nat :: \<open>nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2449
  where \<open>mask n = (2 :: nat) ^ n - 1\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2450
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2451
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2452
  where \<open>push_bit_nat n m = m * 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2453
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2454
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2455
  where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2456
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2457
definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2458
  where \<open>take_bit_nat n m = m mod 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2459
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2460
definition set_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2461
  where \<open>set_bit m n = n OR push_bit m 1\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2462
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2463
definition unset_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  2464
  where \<open>unset_bit m n = (n OR push_bit m 1) XOR push_bit m 1\<close> for m n :: nat
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2465
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2466
definition flip_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2467
  where \<open>flip_bit m n = n XOR push_bit m 1\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2468
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2469
instance proof
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79030
diff changeset
  2470
  fix m n :: nat
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2471
  show \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * (m div 2 AND n div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2472
    by (simp add: and_nat_def and_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2473
  show \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * (m div 2 OR n div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2474
    by (simp add: or_nat_def or_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2475
  show \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * (m div 2 XOR n div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  2476
    by (simp add: xor_nat_def xor_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div)
79489
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  2477
qed (simp_all add: mask_nat_def set_bit_nat_def unset_bit_nat_def flip_bit_nat_def
1e19abf373ac streamlined type class specification
haftmann
parents: 79488
diff changeset
  2478
  push_bit_nat_def drop_bit_nat_def take_bit_nat_def)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2479
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2480
end
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2481
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2482
instance nat :: linordered_euclidean_semiring_bit_operations ..
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2483
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2484
context semiring_bit_operations
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2485
begin
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2486
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2487
lemma push_bit_of_nat:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2488
  \<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2489
  by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2490
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2491
lemma of_nat_push_bit:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2492
  \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2493
  by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2494
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2495
lemma take_bit_of_nat:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2496
  \<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2497
  by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2498
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2499
lemma of_nat_take_bit:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2500
  \<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2501
  by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2502
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2503
lemma of_nat_and_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2504
  \<open>of_nat (m AND n) = of_nat m AND of_nat n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2505
  by (rule bit_eqI) (simp add: bit_of_nat_iff bit_and_iff Bit_Operations.bit_and_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2506
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2507
lemma of_nat_or_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2508
  \<open>of_nat (m OR n) = of_nat m OR of_nat n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2509
  by (rule bit_eqI) (simp add: bit_of_nat_iff bit_or_iff Bit_Operations.bit_or_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2510
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2511
lemma of_nat_xor_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2512
  \<open>of_nat (m XOR n) = of_nat m XOR of_nat n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2513
  by (rule bit_eqI) (simp add: bit_of_nat_iff bit_xor_iff Bit_Operations.bit_xor_iff)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2514
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2515
lemma of_nat_mask_eq:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2516
  \<open>of_nat (mask n) = mask n\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2517
  by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_nat_or_eq)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2518
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2519
end
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2520
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2521
context linordered_euclidean_semiring_bit_operations
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2522
begin
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2523
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2524
lemma drop_bit_of_nat:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2525
  "drop_bit n (of_nat m) = of_nat (drop_bit n m)"
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2526
  by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div [of m "2 ^ n"])
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2527
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2528
lemma of_nat_drop_bit:
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2529
  \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close>
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2530
  by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2531
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2532
end
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2533
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2534
lemma take_bit_nat_less_exp [simp]:
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2535
  \<open>take_bit n m < 2 ^ n\<close> for n m :: nat
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2536
  by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2537
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2538
lemma take_bit_nat_eq_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2539
  \<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for n m :: nat
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2540
proof
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2541
  assume ?P
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2542
  moreover note take_bit_nat_less_exp [of n m]
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2543
  ultimately show ?Q
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2544
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2545
next
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2546
  assume ?Q
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2547
  then show ?P
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2548
    by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2549
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2550
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2551
lemma take_bit_nat_eq_self:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2552
  \<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for m n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2553
  using that by (simp add: take_bit_nat_eq_self_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2554
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2555
lemma take_bit_nat_less_eq_self [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2556
  \<open>take_bit n m \<le> m\<close> for n m :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2557
  by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2558
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2559
lemma take_bit_nat_less_self_iff:
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2560
  \<open>take_bit n m < m \<longleftrightarrow> 2 ^ n \<le> m\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for m n :: nat
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2561
proof
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2562
  assume ?P
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2563
  then have \<open>take_bit n m \<noteq> m\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2564
    by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2565
  then show \<open>?Q\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2566
    by (simp add: take_bit_nat_eq_self_iff)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2567
next
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2568
  have \<open>take_bit n m < 2 ^ n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2569
    by (fact take_bit_nat_less_exp)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2570
  also assume ?Q
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2571
  finally show ?P .
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2572
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2573
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2574
lemma Suc_0_and_eq [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2575
  \<open>Suc 0 AND n = n mod 2\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2576
  using one_and_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2577
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2578
lemma and_Suc_0_eq [simp]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2579
  \<open>n AND Suc 0 = n mod 2\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2580
  using and_one_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2581
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2582
lemma Suc_0_or_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2583
  \<open>Suc 0 OR n = n + of_bool (even n)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2584
  using one_or_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2585
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2586
lemma or_Suc_0_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2587
  \<open>n OR Suc 0 = n + of_bool (even n)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2588
  using or_one_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2589
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2590
lemma Suc_0_xor_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2591
  \<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2592
  using one_xor_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2593
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2594
lemma xor_Suc_0_eq:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2595
  \<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2596
  using xor_one_eq [of n] by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2597
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2598
lemma and_nat_unfold [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2599
  \<open>m AND n = (if m = 0 \<or> n = 0 then 0 else (m mod 2) * (n mod 2) + 2 * ((m div 2) AND (n div 2)))\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2600
    for m n :: nat
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2601
  by (auto simp add: and_rec [of m n] elim: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2602
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2603
lemma or_nat_unfold [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2604
  \<open>m OR n = (if m = 0 then n else if n = 0 then m
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2605
    else max (m mod 2) (n mod 2) + 2 * ((m div 2) OR (n div 2)))\<close> for m n :: nat
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2606
  by (auto simp add: or_rec [of m n] elim: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2607
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2608
lemma xor_nat_unfold [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2609
  \<open>m XOR n = (if m = 0 then n else if n = 0 then m
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2610
    else (m mod 2 + n mod 2) mod 2 + 2 * ((m div 2) XOR (n div 2)))\<close> for m n :: nat
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  2611
  by (auto simp add: xor_rec [of m n] elim!: oddE)
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2612
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2613
lemma [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2614
  \<open>unset_bit 0 m = 2 * (m div 2)\<close>
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2615
  \<open>unset_bit (Suc n) m = m mod 2 + 2 * unset_bit n (m div 2)\<close> for m n :: nat
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2616
  by (simp_all add: unset_bit_Suc)
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2617
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2618
lemma push_bit_of_Suc_0 [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2619
  \<open>push_bit n (Suc 0) = 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2620
  using push_bit_of_1 [where ?'a = nat] by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2621
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2622
lemma take_bit_of_Suc_0 [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2623
  \<open>take_bit n (Suc 0) = of_bool (0 < n)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2624
  using take_bit_of_1 [where ?'a = nat] by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2625
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2626
lemma drop_bit_of_Suc_0 [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2627
  \<open>drop_bit n (Suc 0) = of_bool (n = 0)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2628
  using drop_bit_of_1 [where ?'a = nat] by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2629
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2630
lemma Suc_mask_eq_exp:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2631
  \<open>Suc (mask n) = 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2632
  by (simp add: mask_eq_exp_minus_1)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2633
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2634
lemma less_eq_mask:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2635
  \<open>n \<le> mask n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2636
  by (simp add: mask_eq_exp_minus_1 le_diff_conv2)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2637
    (metis Suc_mask_eq_exp diff_Suc_1 diff_le_diff_pow diff_zero le_refl not_less_eq_eq power_0)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2638
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2639
lemma less_mask:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2640
  \<open>n < mask n\<close> if \<open>Suc 0 < n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2641
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2642
  define m where \<open>m = n - 2\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2643
  with that have *: \<open>n = m + 2\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2644
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2645
  have \<open>Suc (Suc (Suc m)) < 4 * 2 ^ m\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2646
    by (induction m) simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2647
  then have \<open>Suc (m + 2) < Suc (mask (m + 2))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2648
    by (simp add: Suc_mask_eq_exp)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2649
  then have \<open>m + 2 < mask (m + 2)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2650
    by (simp add: less_le)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2651
  with * show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2652
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2653
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2654
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2655
lemma mask_nat_less_exp [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2656
  \<open>(mask n :: nat) < 2 ^ n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2657
  by (simp add: mask_eq_exp_minus_1)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2658
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2659
lemma mask_nat_positive_iff [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2660
  \<open>(0::nat) < mask n \<longleftrightarrow> 0 < n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2661
proof (cases \<open>n = 0\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2662
  case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2663
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2664
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2665
next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2666
  case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2667
  then have \<open>0 < n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2668
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2669
  then have \<open>(0::nat) < mask n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2670
    using less_eq_mask [of n] by (rule order_less_le_trans)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2671
  with \<open>0 < n\<close> show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2672
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2673
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2674
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2675
lemma take_bit_tightened_less_eq_nat:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2676
  \<open>take_bit m q \<le> take_bit n q\<close> if \<open>m \<le> n\<close> for q :: nat
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2677
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2678
  have \<open>take_bit m (take_bit n q) \<le> take_bit n q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2679
    by (rule take_bit_nat_less_eq_self)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2680
  with that show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2681
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2682
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2683
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2684
lemma push_bit_nat_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2685
  \<open>push_bit n (nat k) = nat (push_bit n k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2686
  by (cases \<open>k \<ge> 0\<close>) (simp_all add: push_bit_eq_mult nat_mult_distrib not_le mult_nonneg_nonpos2)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2687
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2688
lemma drop_bit_nat_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2689
  \<open>drop_bit n (nat k) = nat (drop_bit n k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2690
  apply (cases \<open>k \<ge> 0\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2691
   apply (simp_all add: drop_bit_eq_div nat_div_distrib nat_power_eq not_le)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2692
  apply (simp add: divide_int_def)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2693
  done
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2694
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2695
lemma take_bit_nat_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2696
  \<open>take_bit n (nat k) = nat (take_bit n k)\<close> if \<open>k \<ge> 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2697
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2698
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2699
lemma nat_take_bit_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2700
  \<open>nat (take_bit n k) = take_bit n (nat k)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2701
  if \<open>k \<ge> 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2702
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2703
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2704
lemma nat_mask_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2705
  \<open>nat (mask n) = mask n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2706
  by (simp add: nat_eq_iff of_nat_mask_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2707
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  2708
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2709
subsection \<open>Symbolic computations on numeral expressions\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2710
75138
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2711
context semiring_bits
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2712
begin
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2713
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2714
lemma not_bit_numeral_Bit0_0 [simp]:
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2715
  \<open>\<not> bit (numeral (Num.Bit0 m)) 0\<close>
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2716
  by (simp add: bit_0)
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2717
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2718
lemma bit_numeral_Bit1_0 [simp]:
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2719
  \<open>bit (numeral (Num.Bit1 m)) 0\<close>
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2720
  by (simp add: bit_0)
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2721
75138
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2722
end
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2723
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2724
context ring_bit_operations
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2725
begin
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2726
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2727
lemma not_bit_minus_numeral_Bit0_0 [simp]:
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2728
  \<open>\<not> bit (- numeral (Num.Bit0 m)) 0\<close>
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2729
  by (simp add: bit_0)
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2730
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2731
lemma bit_minus_numeral_Bit1_0 [simp]:
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2732
  \<open>bit (- numeral (Num.Bit1 m)) 0\<close>
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2733
  by (simp add: bit_0)
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2734
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2735
end
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2736
78955
74147aa81dbb more specific name for type class
haftmann
parents: 78937
diff changeset
  2737
context linordered_euclidean_semiring_bit_operations
75138
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2738
begin
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2739
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2740
lemma bit_numeral_iff:
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2741
  \<open>bit (numeral m) n \<longleftrightarrow> bit (numeral m :: nat) n\<close>
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2742
  using bit_of_nat_iff_bit [of \<open>numeral m\<close> n] by simp
cd77ffb01e15 simp rules for negative numerals
haftmann
parents: 75086
diff changeset
  2743
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2744
lemma bit_numeral_Bit0_Suc_iff [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2745
  \<open>bit (numeral (Num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (numeral m) n\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2746
  by (simp add: bit_Suc numeral_Bit0_div_2)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2747
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2748
lemma bit_numeral_Bit1_Suc_iff [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2749
  \<open>bit (numeral (Num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (numeral m) n\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2750
  by (simp add: bit_Suc numeral_Bit1_div_2)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2751
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2752
lemma bit_numeral_rec:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2753
  \<open>bit (numeral (Num.Bit0 w)) n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc m \<Rightarrow> bit (numeral w) m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2754
  \<open>bit (numeral (Num.Bit1 w)) n \<longleftrightarrow> (case n of 0 \<Rightarrow> True | Suc m \<Rightarrow> bit (numeral w) m)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2755
  by (cases n; simp add: bit_0)+
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2756
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2757
lemma bit_numeral_simps [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2758
  \<open>\<not> bit 1 (numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2759
  \<open>bit (numeral (Num.Bit0 w)) (numeral n) \<longleftrightarrow> bit (numeral w) (pred_numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2760
  \<open>bit (numeral (Num.Bit1 w)) (numeral n) \<longleftrightarrow> bit (numeral w) (pred_numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2761
  by (simp_all add: bit_1_iff numeral_eq_Suc)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2762
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2763
lemma and_numerals [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2764
  \<open>1 AND numeral (Num.Bit0 y) = 0\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2765
  \<open>1 AND numeral (Num.Bit1 y) = 1\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2766
  \<open>numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = 2 * (numeral x AND numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2767
  \<open>numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = 2 * (numeral x AND numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2768
  \<open>numeral (Num.Bit0 x) AND 1 = 0\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2769
  \<open>numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = 2 * (numeral x AND numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2770
  \<open>numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + 2 * (numeral x AND numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2771
  \<open>numeral (Num.Bit1 x) AND 1 = 1\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2772
  by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2773
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2774
lemma or_numerals [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2775
  \<open>1 OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2776
  \<open>1 OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2777
  \<open>numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = 2 * (numeral x OR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2778
  \<open>numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + 2 * (numeral x OR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2779
  \<open>numeral (Num.Bit0 x) OR 1 = numeral (Num.Bit1 x)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2780
  \<open>numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + 2 * (numeral x OR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2781
  \<open>numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + 2 * (numeral x OR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2782
  \<open>numeral (Num.Bit1 x) OR 1 = numeral (Num.Bit1 x)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2783
  by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2784
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2785
lemma xor_numerals [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2786
  \<open>1 XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2787
  \<open>1 XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2788
  \<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = 2 * (numeral x XOR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2789
  \<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + 2 * (numeral x XOR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2790
  \<open>numeral (Num.Bit0 x) XOR 1 = numeral (Num.Bit1 x)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2791
  \<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + 2 * (numeral x XOR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2792
  \<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = 2 * (numeral x XOR numeral y)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2793
  \<open>numeral (Num.Bit1 x) XOR 1 = numeral (Num.Bit0 x)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2794
  by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2795
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2796
end
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2797
79017
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2798
lemma drop_bit_Suc_minus_bit0 [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2799
  \<open>drop_bit (Suc n) (- numeral (Num.Bit0 k)) = drop_bit n (- numeral k :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2800
  by (simp add: drop_bit_Suc numeral_Bit0_div_2)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2801
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2802
lemma drop_bit_Suc_minus_bit1 [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2803
  \<open>drop_bit (Suc n) (- numeral (Num.Bit1 k)) = drop_bit n (- numeral (Num.inc k) :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2804
  by (simp add: drop_bit_Suc numeral_Bit1_div_2 add_One)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2805
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2806
lemma drop_bit_numeral_minus_bit0 [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2807
  \<open>drop_bit (numeral l) (- numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (- numeral k :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2808
  by (simp add: numeral_eq_Suc numeral_Bit0_div_2)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2809
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2810
lemma drop_bit_numeral_minus_bit1 [simp]:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2811
  \<open>drop_bit (numeral l) (- numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (- numeral (Num.inc k) :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2812
  by (simp add: numeral_eq_Suc numeral_Bit1_div_2)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2813
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2814
lemma take_bit_Suc_minus_bit0:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2815
  \<open>take_bit (Suc n) (- numeral (Num.Bit0 k)) = take_bit n (- numeral k) * (2 :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2816
  by (simp add: take_bit_Suc numeral_Bit0_div_2)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2817
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2818
lemma take_bit_Suc_minus_bit1:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2819
  \<open>take_bit (Suc n) (- numeral (Num.Bit1 k)) = take_bit n (- numeral (Num.inc k)) * 2 + (1 :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2820
  by (simp add: take_bit_Suc numeral_Bit1_div_2 add_One)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2821
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2822
lemma take_bit_numeral_minus_bit0:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2823
  \<open>take_bit (numeral l) (- numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2824
  by (simp add: numeral_eq_Suc numeral_Bit0_div_2 take_bit_Suc_minus_bit0)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2825
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2826
lemma take_bit_numeral_minus_bit1:
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2827
  \<open>take_bit (numeral l) (- numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (- numeral (Num.inc k)) * 2 + (1 :: int)\<close>
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2828
  by (simp add: numeral_eq_Suc numeral_Bit1_div_2 take_bit_Suc_minus_bit1)
127ba61b2630 modernized, reordered, generalized
haftmann
parents: 79008
diff changeset
  2829
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2830
lemma and_nat_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2831
  \<open>Suc 0 AND numeral (Num.Bit0 y) = 0\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2832
  \<open>Suc 0 AND numeral (Num.Bit1 y) = 1\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2833
  \<open>numeral (Num.Bit0 x) AND Suc 0 = 0\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2834
  \<open>numeral (Num.Bit1 x) AND Suc 0 = 1\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2835
  by (simp_all only: and_numerals flip: One_nat_def)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2836
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2837
lemma or_nat_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2838
  \<open>Suc 0 OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2839
  \<open>Suc 0 OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2840
  \<open>numeral (Num.Bit0 x) OR Suc 0 = numeral (Num.Bit1 x)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2841
  \<open>numeral (Num.Bit1 x) OR Suc 0 = numeral (Num.Bit1 x)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2842
  by (simp_all only: or_numerals flip: One_nat_def)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2843
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2844
lemma xor_nat_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2845
  \<open>Suc 0 XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2846
  \<open>Suc 0 XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2847
  \<open>numeral (Num.Bit0 x) XOR Suc 0 = numeral (Num.Bit1 x)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2848
  \<open>numeral (Num.Bit1 x) XOR Suc 0 = numeral (Num.Bit0 x)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2849
  by (simp_all only: xor_numerals flip: One_nat_def)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2850
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2851
context ring_bit_operations
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2852
begin
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2853
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2854
lemma minus_numeral_inc_eq:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2855
  \<open>- numeral (Num.inc n) = NOT (numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2856
  by (simp add: not_eq_complement sub_inc_One_eq add_One)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2857
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2858
lemma sub_one_eq_not_neg:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2859
  \<open>Num.sub n num.One = NOT (- numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2860
  by (simp add: not_eq_complement)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2861
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2862
lemma minus_numeral_eq_not_sub_one:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2863
  \<open>- numeral n = NOT (Num.sub n num.One)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2864
  by (simp add: not_eq_complement)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2865
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2866
lemma not_numeral_eq [simp]:
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2867
  \<open>NOT (numeral n) = - numeral (Num.inc n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2868
  by (simp add: minus_numeral_inc_eq)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2869
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2870
lemma not_minus_numeral_eq [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2871
  \<open>NOT (- numeral n) = Num.sub n num.One\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2872
  by (simp add: sub_one_eq_not_neg)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2873
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2874
lemma minus_not_numeral_eq [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2875
  \<open>- (NOT (numeral n)) = numeral (Num.inc n)\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2876
  by simp
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2877
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2878
lemma not_numeral_BitM_eq:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2879
  \<open>NOT (numeral (Num.BitM n)) =  - numeral (num.Bit0 n)\<close>
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  2880
  by (simp add: inc_BitM_eq)
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2881
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2882
lemma not_numeral_Bit0_eq:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2883
  \<open>NOT (numeral (Num.Bit0 n)) =  - numeral (num.Bit1 n)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2884
  by simp
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2885
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2886
end
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2887
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2888
lemma bit_minus_numeral_int [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2889
  \<open>bit (- numeral (num.Bit0 w) :: int) (numeral n) \<longleftrightarrow> bit (- numeral w :: int) (pred_numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2890
  \<open>bit (- numeral (num.Bit1 w) :: int) (numeral n) \<longleftrightarrow> \<not> bit (numeral w :: int) (pred_numeral n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2891
  by (simp_all add: bit_minus_iff bit_not_iff numeral_eq_Suc bit_Suc add_One sub_inc_One_eq)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2892
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2893
lemma bit_minus_numeral_Bit0_Suc_iff [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2894
  \<open>bit (- numeral (num.Bit0 w) :: int) (Suc n) \<longleftrightarrow> bit (- numeral w :: int) n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2895
  by (simp add: bit_Suc)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2896
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2897
lemma bit_minus_numeral_Bit1_Suc_iff [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2898
  \<open>bit (- numeral (num.Bit1 w) :: int) (Suc n) \<longleftrightarrow> \<not> bit (numeral w :: int) n\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2899
  by (simp add: bit_Suc add_One flip: bit_not_int_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2900
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2901
lemma and_not_numerals:
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2902
  \<open>1 AND NOT 1 = (0 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2903
  \<open>1 AND NOT (numeral (Num.Bit0 n)) = (1 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2904
  \<open>1 AND NOT (numeral (Num.Bit1 n)) = (0 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2905
  \<open>numeral (Num.Bit0 m) AND NOT (1 :: int) = numeral (Num.Bit0 m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2906
  \<open>numeral (Num.Bit0 m) AND NOT (numeral (Num.Bit0 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2907
  \<open>numeral (Num.Bit0 m) AND NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2908
  \<open>numeral (Num.Bit1 m) AND NOT (1 :: int) = numeral (Num.Bit0 m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2909
  \<open>numeral (Num.Bit1 m) AND NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m AND NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2910
  \<open>numeral (Num.Bit1 m) AND NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2911
  by (simp_all add: bit_eq_iff) (auto simp add: bit_0 bit_simps bit_Suc bit_numeral_rec BitM_inc_eq sub_inc_One_eq split: nat.split)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2912
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2913
fun and_not_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2914
where
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2915
  \<open>and_not_num num.One num.One = None\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2916
| \<open>and_not_num num.One (num.Bit0 n) = Some num.One\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2917
| \<open>and_not_num num.One (num.Bit1 n) = None\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2918
| \<open>and_not_num (num.Bit0 m) num.One = Some (num.Bit0 m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2919
| \<open>and_not_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_not_num m n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2920
| \<open>and_not_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2921
| \<open>and_not_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2922
| \<open>and_not_num (num.Bit1 m) (num.Bit0 n) = (case and_not_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2923
| \<open>and_not_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2924
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2925
lemma int_numeral_and_not_num:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2926
  \<open>numeral m AND NOT (numeral n) = (case and_not_num m n of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2927
  by (induction m n rule: and_not_num.induct) (simp_all del: not_numeral_eq not_one_eq add: and_not_numerals split: option.splits)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2928
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2929
lemma int_numeral_not_and_num:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2930
  \<open>NOT (numeral m) AND numeral n = (case and_not_num n m of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2931
  using int_numeral_and_not_num [of n m] by (simp add: ac_simps)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2932
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2933
lemma and_not_num_eq_None_iff:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2934
  \<open>and_not_num m n = None \<longleftrightarrow> numeral m AND NOT (numeral n) = (0 :: int)\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2935
  by (simp del: not_numeral_eq add: int_numeral_and_not_num split: option.split)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2936
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2937
lemma and_not_num_eq_Some_iff:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2938
  \<open>and_not_num m n = Some q \<longleftrightarrow> numeral m AND NOT (numeral n) = (numeral q :: int)\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2939
  by (simp del: not_numeral_eq add: int_numeral_and_not_num split: option.split)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2940
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2941
lemma and_minus_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2942
  \<open>1 AND - (numeral (num.Bit0 n)) = (0::int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2943
  \<open>1 AND - (numeral (num.Bit1 n)) = (1::int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2944
  \<open>numeral m AND - (numeral (num.Bit0 n)) = (case and_not_num m (Num.BitM n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2945
  \<open>numeral m AND - (numeral (num.Bit1 n)) = (case and_not_num m (Num.Bit0 n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2946
  \<open>- (numeral (num.Bit0 n)) AND 1 = (0::int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2947
  \<open>- (numeral (num.Bit1 n)) AND 1 = (1::int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2948
  \<open>- (numeral (num.Bit0 n)) AND numeral m = (case and_not_num m (Num.BitM n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2949
  \<open>- (numeral (num.Bit1 n)) AND numeral m = (case and_not_num m (Num.Bit0 n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2950
  by (simp_all del: not_numeral_eq add: ac_simps
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2951
    and_not_numerals one_and_eq not_numeral_BitM_eq not_numeral_Bit0_eq and_not_num_eq_None_iff and_not_num_eq_Some_iff split: option.split)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2952
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2953
lemma and_minus_minus_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2954
  \<open>- (numeral m :: int) AND - (numeral n :: int) = NOT ((numeral m - 1) OR (numeral n - 1))\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2955
  by (simp add: minus_numeral_eq_not_sub_one)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2956
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2957
lemma or_not_numerals:
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2958
  \<open>1 OR NOT 1 = NOT (0 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2959
  \<open>1 OR NOT (numeral (Num.Bit0 n)) = NOT (numeral (Num.Bit0 n) :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2960
  \<open>1 OR NOT (numeral (Num.Bit1 n)) = NOT (numeral (Num.Bit0 n) :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2961
  \<open>numeral (Num.Bit0 m) OR NOT (1 :: int) = NOT (1 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2962
  \<open>numeral (Num.Bit0 m) OR NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2963
  \<open>numeral (Num.Bit0 m) OR NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m OR NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2964
  \<open>numeral (Num.Bit1 m) OR NOT (1 :: int) = NOT (0 :: int)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2965
  \<open>numeral (Num.Bit1 m) OR NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2966
  \<open>numeral (Num.Bit1 m) OR NOT (numeral (Num.Bit1 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2967
  by (simp_all add: bit_eq_iff)
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  2968
    (auto simp add: bit_0 bit_simps bit_Suc bit_numeral_rec sub_inc_One_eq split: nat.split)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2969
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2970
fun or_not_num_neg :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2971
where
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2972
  \<open>or_not_num_neg num.One num.One = num.One\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2973
| \<open>or_not_num_neg num.One (num.Bit0 m) = num.Bit1 m\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2974
| \<open>or_not_num_neg num.One (num.Bit1 m) = num.Bit1 m\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2975
| \<open>or_not_num_neg (num.Bit0 n) num.One = num.Bit0 num.One\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2976
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2977
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit1 m) = num.Bit0 (or_not_num_neg n m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2978
| \<open>or_not_num_neg (num.Bit1 n) num.One = num.One\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2979
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2980
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit1 m) = Num.BitM (or_not_num_neg n m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2981
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2982
lemma int_numeral_or_not_num_neg:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2983
  \<open>numeral m OR NOT (numeral n :: int) = - numeral (or_not_num_neg m n)\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2984
  by (induction m n rule: or_not_num_neg.induct) (simp_all del: not_numeral_eq not_one_eq add: or_not_numerals, simp_all)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2985
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2986
lemma int_numeral_not_or_num_neg:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2987
  \<open>NOT (numeral m) OR (numeral n :: int) = - numeral (or_not_num_neg n m)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2988
  using int_numeral_or_not_num_neg [of n m] by (simp add: ac_simps)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2989
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2990
lemma numeral_or_not_num_eq:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2991
  \<open>numeral (or_not_num_neg m n) = - (numeral m OR NOT (numeral n :: int))\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2992
  using int_numeral_or_not_num_neg [of m n] by simp
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  2993
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2994
lemma or_minus_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2995
  \<open>1 OR - (numeral (num.Bit0 n)) = - (numeral (or_not_num_neg num.One (Num.BitM n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2996
  \<open>1 OR - (numeral (num.Bit1 n)) = - (numeral (num.Bit1 n) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2997
  \<open>numeral m OR - (numeral (num.Bit0 n)) = - (numeral (or_not_num_neg m (Num.BitM n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2998
  \<open>numeral m OR - (numeral (num.Bit1 n)) = - (numeral (or_not_num_neg m (Num.Bit0 n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  2999
  \<open>- (numeral (num.Bit0 n)) OR 1 = - (numeral (or_not_num_neg num.One (Num.BitM n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3000
  \<open>- (numeral (num.Bit1 n)) OR 1 = - (numeral (num.Bit1 n) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3001
  \<open>- (numeral (num.Bit0 n)) OR numeral m = - (numeral (or_not_num_neg m (Num.BitM n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3002
  \<open>- (numeral (num.Bit1 n)) OR numeral m = - (numeral (or_not_num_neg m (Num.Bit0 n)) :: int)\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3003
  by (simp_all only: or.commute [of _ 1] or.commute [of _ \<open>numeral m\<close>]
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3004
    minus_numeral_eq_not_sub_one or_not_numerals
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3005
    numeral_or_not_num_eq arith_simps minus_minus numeral_One)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3006
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3007
lemma or_minus_minus_numerals [simp]:
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3008
  \<open>- (numeral m :: int) OR - (numeral n :: int) = NOT ((numeral m - 1) AND (numeral n - 1))\<close>
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3009
  by (simp add: minus_numeral_eq_not_sub_one)
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3010
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3011
lemma xor_minus_numerals [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3012
  \<open>- numeral n XOR k = NOT (neg_numeral_class.sub n num.One XOR k)\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3013
  \<open>k XOR - numeral n = NOT (k XOR (neg_numeral_class.sub n num.One))\<close> for k :: int
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3014
  by (simp_all add: minus_numeral_eq_not_sub_one)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3015
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3016
definition take_bit_num :: \<open>nat \<Rightarrow> num \<Rightarrow> num option\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3017
  where \<open>take_bit_num n m =
75651
f4116b7a6679 Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents: 75138
diff changeset
  3018
    (if take_bit n (numeral m :: nat) = 0 then None else Some (num_of_nat (take_bit n (numeral m :: nat))))\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3019
74618
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3020
lemma take_bit_num_simps:
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3021
  \<open>take_bit_num 0 m = None\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3022
  \<open>take_bit_num (Suc n) Num.One =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3023
    Some Num.One\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3024
  \<open>take_bit_num (Suc n) (Num.Bit0 m) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3025
    (case take_bit_num n m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3026
  \<open>take_bit_num (Suc n) (Num.Bit1 m) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3027
    Some (case take_bit_num n m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q)\<close>
74618
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3028
  \<open>take_bit_num (numeral r) Num.One =
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3029
    Some Num.One\<close>
74618
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3030
  \<open>take_bit_num (numeral r) (Num.Bit0 m) =
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3031
    (case take_bit_num (pred_numeral r) m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q))\<close>
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3032
  \<open>take_bit_num (numeral r) (Num.Bit1 m) =
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3033
    Some (case take_bit_num (pred_numeral r) m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q)\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3034
  by (auto simp add: take_bit_num_def ac_simps mult_2 num_of_nat_double
74618
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3035
    take_bit_Suc_bit0 take_bit_Suc_bit1 take_bit_numeral_bit0 take_bit_numeral_bit1)
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3036
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3037
lemma take_bit_num_code [code]:
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3038
  \<comment> \<open>Ocaml-style pattern matching is more robust wrt. different representations of \<^typ>\<open>nat\<close>\<close>
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3039
  \<open>take_bit_num n m = (case (n, m)
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3040
   of (0, _) \<Rightarrow> None
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3041
    | (Suc n, Num.One) \<Rightarrow> Some Num.One
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3042
    | (Suc n, Num.Bit0 m) \<Rightarrow> (case take_bit_num n m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q))
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3043
    | (Suc n, Num.Bit1 m) \<Rightarrow> Some (case take_bit_num n m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q))\<close>
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3044
  by (cases n; cases m) (simp_all add: take_bit_num_simps)
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3045
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3046
context semiring_bit_operations
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3047
begin
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3048
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3049
lemma take_bit_num_eq_None_imp:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3050
  \<open>take_bit m (numeral n) = 0\<close> if \<open>take_bit_num m n = None\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3051
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3052
  from that have \<open>take_bit m (numeral n :: nat) = 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3053
    by (simp add: take_bit_num_def split: if_splits)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3054
  then have \<open>of_nat (take_bit m (numeral n)) = of_nat 0\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3055
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3056
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3057
    by (simp add: of_nat_take_bit)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3058
qed
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3059
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3060
lemma take_bit_num_eq_Some_imp:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3061
  \<open>take_bit m (numeral n) = numeral q\<close> if \<open>take_bit_num m n = Some q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3062
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3063
  from that have \<open>take_bit m (numeral n :: nat) = numeral q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3064
    by (auto simp add: take_bit_num_def Num.numeral_num_of_nat_unfold split: if_splits)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3065
  then have \<open>of_nat (take_bit m (numeral n)) = of_nat (numeral q)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3066
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3067
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3068
    by (simp add: of_nat_take_bit)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3069
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3070
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3071
lemma take_bit_numeral_numeral:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3072
  \<open>take_bit (numeral m) (numeral n) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3073
    (case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> numeral q)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3074
  by (auto split: option.split dest: take_bit_num_eq_None_imp take_bit_num_eq_Some_imp)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3075
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3076
end
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3077
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3078
lemma take_bit_numeral_minus_numeral_int:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3079
  \<open>take_bit (numeral m) (- numeral n :: int) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3080
    (case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> take_bit (numeral m) (2 ^ numeral m - numeral q))\<close> (is \<open>?lhs = ?rhs\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3081
proof (cases \<open>take_bit_num (numeral m) n\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3082
  case None
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3083
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3084
    by (auto dest: take_bit_num_eq_None_imp [where ?'a = int] simp add: take_bit_eq_0_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3085
next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3086
  case (Some q)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3087
  then have q: \<open>take_bit (numeral m) (numeral n :: int) = numeral q\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3088
    by (auto dest: take_bit_num_eq_Some_imp)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3089
  let ?T = \<open>take_bit (numeral m) :: int \<Rightarrow> int\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3090
  have *: \<open>?T (2 ^ numeral m) = ?T (?T 0)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3091
    by (simp add: take_bit_eq_0_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3092
  have \<open>?lhs = ?T (0 - numeral n)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3093
    by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3094
  also have \<open>\<dots> = ?T (?T (?T 0) - ?T (?T (numeral n)))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3095
    by (simp only: take_bit_diff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3096
  also have \<open>\<dots> = ?T (2 ^ numeral m - ?T (numeral n))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3097
    by (simp only: take_bit_diff flip: *)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3098
  also have \<open>\<dots> = ?rhs\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3099
    by (simp add: q Some)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3100
  finally show ?thesis .
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3101
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3102
74618
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3103
declare take_bit_num_simps [simp]
43142ac556e6 moved generic implementation into HOL-Main
haftmann
parents: 74592
diff changeset
  3104
  take_bit_numeral_numeral [simp]
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3105
  take_bit_numeral_minus_numeral_int [simp]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3106
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74123
diff changeset
  3107
79069
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3108
subsection \<open>Symbolic computations for code generation\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3109
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3110
lemma bit_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3111
  \<open>bit (0::int)               n      \<longleftrightarrow> False\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3112
  \<open>bit (Int.Neg num.One)      n      \<longleftrightarrow> True\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3113
  \<open>bit (Int.Pos num.One)      0      \<longleftrightarrow> True\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3114
  \<open>bit (Int.Pos (num.Bit0 m)) 0      \<longleftrightarrow> False\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3115
  \<open>bit (Int.Pos (num.Bit1 m)) 0      \<longleftrightarrow> True\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3116
  \<open>bit (Int.Neg (num.Bit0 m)) 0      \<longleftrightarrow> False\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3117
  \<open>bit (Int.Neg (num.Bit1 m)) 0      \<longleftrightarrow> True\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3118
  \<open>bit (Int.Pos num.One)      (Suc n) \<longleftrightarrow> False\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3119
  \<open>bit (Int.Pos (num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (Int.Pos m) n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3120
  \<open>bit (Int.Pos (num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (Int.Pos m) n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3121
  \<open>bit (Int.Neg (num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (Int.Neg m) n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3122
  \<open>bit (Int.Neg (num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (Int.Neg (Num.inc m)) n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3123
  by (simp_all add: Num.add_One bit_0 bit_Suc)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3124
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3125
lemma not_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3126
  \<open>NOT (0 :: int) = - 1\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3127
  \<open>NOT (Int.Pos n) = Int.Neg (Num.inc n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3128
  \<open>NOT (Int.Neg n) = Num.sub n num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3129
  by (simp_all add: Num.add_One not_int_def)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3130
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3131
fun and_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3132
where
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3133
  \<open>and_num num.One num.One = Some num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3134
| \<open>and_num num.One (num.Bit0 n) = None\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3135
| \<open>and_num num.One (num.Bit1 n) = Some num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3136
| \<open>and_num (num.Bit0 m) num.One = None\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3137
| \<open>and_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3138
| \<open>and_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3139
| \<open>and_num (num.Bit1 m) num.One = Some num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3140
| \<open>and_num (num.Bit1 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3141
| \<open>and_num (num.Bit1 m) (num.Bit1 n) = (case and_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3142
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3143
context linordered_euclidean_semiring_bit_operations
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3144
begin
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3145
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3146
lemma numeral_and_num:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3147
  \<open>numeral m AND numeral n = (case and_num m n of None \<Rightarrow> 0 | Some n' \<Rightarrow> numeral n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3148
  by (induction m n rule: and_num.induct) (simp_all add: split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3149
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3150
lemma and_num_eq_None_iff:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3151
  \<open>and_num m n = None \<longleftrightarrow> numeral m AND numeral n = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3152
  by (simp add: numeral_and_num split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3153
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3154
lemma and_num_eq_Some_iff:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3155
  \<open>and_num m n = Some q \<longleftrightarrow> numeral m AND numeral n = numeral q\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3156
  by (simp add: numeral_and_num split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3157
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3158
end
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3159
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3160
lemma and_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3161
  fixes i j :: int shows
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3162
  \<open>0 AND j = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3163
  \<open>i AND 0 = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3164
  \<open>Int.Pos n AND Int.Pos m = (case and_num n m of None \<Rightarrow> 0 | Some n' \<Rightarrow> Int.Pos n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3165
  \<open>Int.Neg n AND Int.Neg m = NOT (Num.sub n num.One OR Num.sub m num.One)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3166
  \<open>Int.Pos n AND Int.Neg num.One = Int.Pos n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3167
  \<open>Int.Pos n AND Int.Neg (num.Bit0 m) = Num.sub (or_not_num_neg (Num.BitM m) n) num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3168
  \<open>Int.Pos n AND Int.Neg (num.Bit1 m) = Num.sub (or_not_num_neg (num.Bit0 m) n) num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3169
  \<open>Int.Neg num.One AND Int.Pos m = Int.Pos m\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3170
  \<open>Int.Neg (num.Bit0 n) AND Int.Pos m = Num.sub (or_not_num_neg (Num.BitM n) m) num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3171
  \<open>Int.Neg (num.Bit1 n) AND Int.Pos m = Num.sub (or_not_num_neg (num.Bit0 n) m) num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3172
  apply (auto simp add: and_num_eq_None_iff [where ?'a = int] and_num_eq_Some_iff [where ?'a = int]
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3173
    split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3174
     apply (simp_all only: sub_one_eq_not_neg numeral_or_not_num_eq minus_minus and_not_numerals
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3175
       bit.de_Morgan_disj bit.double_compl and_not_num_eq_None_iff and_not_num_eq_Some_iff ac_simps)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3176
  done
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3177
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3178
context linordered_euclidean_semiring_bit_operations
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3179
begin
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3180
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3181
fun or_num :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3182
where
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3183
  \<open>or_num num.One num.One = num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3184
| \<open>or_num num.One (num.Bit0 n) = num.Bit1 n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3185
| \<open>or_num num.One (num.Bit1 n) = num.Bit1 n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3186
| \<open>or_num (num.Bit0 m) num.One = num.Bit1 m\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3187
| \<open>or_num (num.Bit0 m) (num.Bit0 n) = num.Bit0 (or_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3188
| \<open>or_num (num.Bit0 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3189
| \<open>or_num (num.Bit1 m) num.One = num.Bit1 m\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3190
| \<open>or_num (num.Bit1 m) (num.Bit0 n) = num.Bit1 (or_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3191
| \<open>or_num (num.Bit1 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3192
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3193
lemma numeral_or_num:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3194
  \<open>numeral m OR numeral n = numeral (or_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3195
  by (induction m n rule: or_num.induct) simp_all
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3196
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3197
lemma numeral_or_num_eq:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3198
  \<open>numeral (or_num m n) = numeral m OR numeral n\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3199
  by (simp add: numeral_or_num)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3200
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3201
end
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3202
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3203
lemma or_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3204
  fixes i j :: int shows
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3205
  \<open>0 OR j = j\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3206
  \<open>i OR 0 = i\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3207
  \<open>Int.Pos n OR Int.Pos m = Int.Pos (or_num n m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3208
  \<open>Int.Neg n OR Int.Neg m = NOT (Num.sub n num.One AND Num.sub m num.One)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3209
  \<open>Int.Pos n OR Int.Neg num.One = Int.Neg num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3210
  \<open>Int.Pos n OR Int.Neg (num.Bit0 m) = (case and_not_num (Num.BitM m) n of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3211
  \<open>Int.Pos n OR Int.Neg (num.Bit1 m) = (case and_not_num (num.Bit0 m) n of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3212
  \<open>Int.Neg num.One OR Int.Pos m = Int.Neg num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3213
  \<open>Int.Neg (num.Bit0 n) OR Int.Pos m = (case and_not_num (Num.BitM n) m of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3214
  \<open>Int.Neg (num.Bit1 n) OR Int.Pos m = (case and_not_num (num.Bit0 n) m of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3215
  apply (auto simp add: numeral_or_num_eq split: option.splits)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3216
         apply (simp_all only: and_not_num_eq_None_iff and_not_num_eq_Some_iff and_not_numerals
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3217
           numeral_or_not_num_eq or_eq_not_not_and bit.double_compl ac_simps flip: numeral_eq_iff [where ?'a = int])
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3218
         apply simp_all
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3219
  done
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3220
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3221
fun xor_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3222
where
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3223
  \<open>xor_num num.One num.One = None\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3224
| \<open>xor_num num.One (num.Bit0 n) = Some (num.Bit1 n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3225
| \<open>xor_num num.One (num.Bit1 n) = Some (num.Bit0 n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3226
| \<open>xor_num (num.Bit0 m) num.One = Some (num.Bit1 m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3227
| \<open>xor_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (xor_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3228
| \<open>xor_num (num.Bit0 m) (num.Bit1 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3229
| \<open>xor_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3230
| \<open>xor_num (num.Bit1 m) (num.Bit0 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3231
| \<open>xor_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (xor_num m n)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3232
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3233
context linordered_euclidean_semiring_bit_operations
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3234
begin
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3235
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3236
lemma numeral_xor_num:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3237
  \<open>numeral m XOR numeral n = (case xor_num m n of None \<Rightarrow> 0 | Some n' \<Rightarrow> numeral n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3238
  by (induction m n rule: xor_num.induct) (simp_all split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3239
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3240
lemma xor_num_eq_None_iff:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3241
  \<open>xor_num m n = None \<longleftrightarrow> numeral m XOR numeral n = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3242
  by (simp add: numeral_xor_num split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3243
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3244
lemma xor_num_eq_Some_iff:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3245
  \<open>xor_num m n = Some q \<longleftrightarrow> numeral m XOR numeral n = numeral q\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3246
  by (simp add: numeral_xor_num split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3247
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3248
end
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3249
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3250
lemma xor_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3251
  fixes i j :: int shows
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3252
  \<open>0 XOR j = j\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3253
  \<open>i XOR 0 = i\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3254
  \<open>Int.Pos n XOR Int.Pos m = (case xor_num n m of None \<Rightarrow> 0 | Some n' \<Rightarrow> Int.Pos n')\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3255
  \<open>Int.Neg n XOR Int.Neg m = Num.sub n num.One XOR Num.sub m num.One\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3256
  \<open>Int.Neg n XOR Int.Pos m = NOT (Num.sub n num.One XOR Int.Pos m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3257
  \<open>Int.Pos n XOR Int.Neg m = NOT (Int.Pos n XOR Num.sub m num.One)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3258
  by (simp_all add: xor_num_eq_None_iff [where ?'a = int] xor_num_eq_Some_iff [where ?'a = int] split: option.split)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3259
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3260
lemma push_bit_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3261
  \<open>push_bit 0 i = i\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3262
  \<open>push_bit (Suc n) i = push_bit n (Int.dup i)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3263
  by (simp_all add: ac_simps)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3264
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3265
lemma drop_bit_int_code [code]:
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3266
  fixes i :: int shows
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3267
  \<open>drop_bit 0 i = i\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3268
  \<open>drop_bit (Suc n) 0 = (0 :: int)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3269
  \<open>drop_bit (Suc n) (Int.Pos num.One) = 0\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3270
  \<open>drop_bit (Suc n) (Int.Pos (num.Bit0 m)) = drop_bit n (Int.Pos m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3271
  \<open>drop_bit (Suc n) (Int.Pos (num.Bit1 m)) = drop_bit n (Int.Pos m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3272
  \<open>drop_bit (Suc n) (Int.Neg num.One) = - 1\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3273
  \<open>drop_bit (Suc n) (Int.Neg (num.Bit0 m)) = drop_bit n (Int.Neg m)\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3274
  \<open>drop_bit (Suc n) (Int.Neg (num.Bit1 m)) = drop_bit n (Int.Neg (Num.inc m))\<close>
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3275
  by (simp_all add: drop_bit_Suc add_One)
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3276
48ca09068adf grouped lemmas for symbolic computations
haftmann
parents: 79068
diff changeset
  3277
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  3278
subsection \<open>More properties\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  3279
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3280
lemma take_bit_eq_mask_iff:
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3281
  \<open>take_bit n k = mask n \<longleftrightarrow> take_bit n (k + 1) = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3282
  for k :: int
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3283
proof
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3284
  assume ?P
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3285
  then have \<open>take_bit n (take_bit n k + take_bit n 1) = 0\<close>
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  3286
    by (simp add: mask_eq_exp_minus_1 take_bit_eq_0_iff)
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3287
  then show ?Q
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3288
    by (simp only: take_bit_add)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3289
next
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3290
  assume ?Q
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3291
  then have \<open>take_bit n (k + 1) - 1 = - 1\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3292
    by simp
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3293
  then have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n (- 1)\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3294
    by simp
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3295
  moreover have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n k\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3296
    by (simp add: take_bit_eq_mod mod_simps)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3297
  ultimately show ?P
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3298
    by simp
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3299
qed
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3300
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3301
lemma take_bit_eq_mask_iff_exp_dvd:
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3302
  \<open>take_bit n k = mask n \<longleftrightarrow> 2 ^ n dvd k + 1\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3303
  for k :: int
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3304
  by (simp add: take_bit_eq_mask_iff flip: take_bit_eq_0_iff)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72792
diff changeset
  3305
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  3306
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3307
subsection \<open>Bit concatenation\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3308
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3309
definition concat_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int\<close>
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3310
  where \<open>concat_bit n k l = take_bit n k OR push_bit n l\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3311
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  3312
lemma bit_concat_bit_iff [bit_simps]:
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3313
  \<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>
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3314
  by (simp add: concat_bit_def bit_or_iff bit_and_iff bit_take_bit_iff bit_push_bit_iff ac_simps)
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3315
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3316
lemma concat_bit_eq:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3317
  \<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
  3318
  by (simp add: concat_bit_def take_bit_eq_mask
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3319
    bit_and_iff bit_mask_iff bit_push_bit_iff disjunctive_add)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3320
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3321
lemma concat_bit_0 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3322
  \<open>concat_bit 0 k l = l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3323
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3324
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3325
lemma concat_bit_Suc:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3326
  \<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
  3327
  by (simp add: concat_bit_eq take_bit_Suc push_bit_double)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3328
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3329
lemma concat_bit_of_zero_1 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3330
  \<open>concat_bit n 0 l = push_bit n l\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3331
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3332
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3333
lemma concat_bit_of_zero_2 [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3334
  \<open>concat_bit n k 0 = take_bit n k\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3335
  by (simp add: concat_bit_def take_bit_eq_mask)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3336
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3337
lemma concat_bit_nonnegative_iff [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3338
  \<open>concat_bit n k l \<ge> 0 \<longleftrightarrow> l \<ge> 0\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3339
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3340
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3341
lemma concat_bit_negative_iff [simp]:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3342
  \<open>concat_bit n k l < 0 \<longleftrightarrow> l < 0\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3343
  by (simp add: concat_bit_def)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3344
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3345
lemma concat_bit_assoc:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3346
  \<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
  3347
  by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3348
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3349
lemma concat_bit_assoc_sym:
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3350
  \<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
  3351
  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
  3352
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3353
lemma concat_bit_eq_iff:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3354
  \<open>concat_bit n k l = concat_bit n r s
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3355
    \<longleftrightarrow> take_bit n k = take_bit n r \<and> l = s\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3356
proof
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3357
  assume ?Q
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3358
  then show ?P
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3359
    by (simp add: concat_bit_def)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3360
next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3361
  assume ?P
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3362
  then have *: \<open>bit (concat_bit n k l) m = bit (concat_bit n r s) m\<close> for m
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3363
    by (simp add: bit_eq_iff)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3364
  have \<open>take_bit n k = take_bit n r\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3365
  proof (rule bit_eqI)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3366
    fix m
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3367
    from * [of m]
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3368
    show \<open>bit (take_bit n k) m \<longleftrightarrow> bit (take_bit n r) m\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3369
      by (auto simp add: bit_take_bit_iff bit_concat_bit_iff)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3370
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3371
  moreover have \<open>push_bit n l = push_bit n s\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3372
  proof (rule bit_eqI)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3373
    fix m
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3374
    from * [of m]
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3375
    show \<open>bit (push_bit n l) m \<longleftrightarrow> bit (push_bit n s) m\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3376
      by (auto simp add: bit_push_bit_iff bit_concat_bit_iff)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3377
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3378
  then have \<open>l = s\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3379
    by (simp add: push_bit_eq_mult)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3380
  ultimately show ?Q
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3381
    by (simp add: concat_bit_def)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3382
qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3383
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3384
lemma take_bit_concat_bit_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3385
  \<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3386
  by (rule bit_eqI)
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3387
    (auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def)
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3388
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  3389
lemma concat_bit_take_bit_eq:
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  3390
  \<open>concat_bit n (take_bit n b) = concat_bit n b\<close>
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  3391
  by (simp add: concat_bit_def [abs_def])
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72397
diff changeset
  3392
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3393
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3394
subsection \<open>Taking bits with sign propagation\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3395
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3396
context ring_bit_operations
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3397
begin
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3398
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3399
definition signed_take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3400
  where \<open>signed_take_bit n a = take_bit n a OR (of_bool (bit a n) * NOT (mask n))\<close>
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  3401
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3402
lemma signed_take_bit_eq_if_positive:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3403
  \<open>signed_take_bit n a = take_bit n a\<close> if \<open>\<not> bit a n\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3404
  using that by (simp add: signed_take_bit_def)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3405
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3406
lemma signed_take_bit_eq_if_negative:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3407
  \<open>signed_take_bit n a = take_bit n a OR NOT (mask n)\<close> if \<open>bit a n\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3408
  using that by (simp add: signed_take_bit_def)
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3409
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3410
lemma even_signed_take_bit_iff:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3411
  \<open>even (signed_take_bit m a) \<longleftrightarrow> even a\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  3412
  by (auto simp add: bit_0 signed_take_bit_def even_or_iff even_mask_iff bit_double_iff)
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3413
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  3414
lemma bit_signed_take_bit_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  3415
  \<open>bit (signed_take_bit m a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit a (min m n)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3416
  by (simp add: signed_take_bit_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff min_def not_le)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  3417
    (blast dest: bit_imp_possible_bit)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3418
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3419
lemma signed_take_bit_0 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3420
  \<open>signed_take_bit 0 a = - (a mod 2)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  3421
  by (simp add: bit_0 signed_take_bit_def odd_iff_mod_2_eq_one)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3422
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3423
lemma signed_take_bit_Suc:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3424
  \<open>signed_take_bit (Suc n) a = a mod 2 + 2 * signed_take_bit n (a div 2)\<close>
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74618
diff changeset
  3425
  by (simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 possible_bit_less_imp flip: bit_Suc min_Suc_Suc)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3426
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3427
lemma signed_take_bit_of_0 [simp]:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3428
  \<open>signed_take_bit n 0 = 0\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3429
  by (simp add: signed_take_bit_def)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3430
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3431
lemma signed_take_bit_of_minus_1 [simp]:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3432
  \<open>signed_take_bit n (- 1) = - 1\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3433
  by (simp add: signed_take_bit_def mask_eq_exp_minus_1 possible_bit_def)
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3434
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3435
lemma signed_take_bit_Suc_1 [simp]:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3436
  \<open>signed_take_bit (Suc n) 1 = 1\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3437
  by (simp add: signed_take_bit_Suc)
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3438
74497
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  3439
lemma signed_take_bit_numeral_of_1 [simp]:
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  3440
  \<open>signed_take_bit (numeral k) 1 = 1\<close>
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  3441
  by (simp add: bit_1_iff signed_take_bit_eq_if_positive)
9c04a82c3128 more complete simp rules
haftmann
parents: 74495
diff changeset
  3442
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3443
lemma signed_take_bit_rec:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3444
  \<open>signed_take_bit n a = (if n = 0 then - (a mod 2) else a mod 2 + 2 * signed_take_bit (n - 1) (a div 2))\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3445
  by (cases n) (simp_all add: signed_take_bit_Suc)
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3446
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3447
lemma signed_take_bit_eq_iff_take_bit_eq:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3448
  \<open>signed_take_bit n a = signed_take_bit n b \<longleftrightarrow> take_bit (Suc n) a = take_bit (Suc n) b\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3449
proof -
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3450
  have \<open>bit (signed_take_bit n a) = bit (signed_take_bit n b) \<longleftrightarrow> bit (take_bit (Suc n) a) = bit (take_bit (Suc n) b)\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3451
    by (simp add: fun_eq_iff bit_signed_take_bit_iff bit_take_bit_iff not_le less_Suc_eq_le min_def)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  3452
      (use bit_imp_possible_bit in fastforce)
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3453
  then show ?thesis
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  3454
    by (auto simp add: fun_eq_iff intro: bit_eqI)
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3455
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3456
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3457
lemma signed_take_bit_signed_take_bit [simp]:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3458
  \<open>signed_take_bit m (signed_take_bit n a) = signed_take_bit (min m n) a\<close>
74495
bc27c490aaac normalizing NOT (numeral _) (again)
haftmann
parents: 74391
diff changeset
  3459
  by (auto simp add: bit_eq_iff bit_simps ac_simps)
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3460
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3461
lemma signed_take_bit_take_bit:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3462
  \<open>signed_take_bit m (take_bit n a) = (if n \<le> m then take_bit n else signed_take_bit m) a\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3463
  by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff)
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3464
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3465
lemma take_bit_signed_take_bit:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3466
  \<open>take_bit m (signed_take_bit n a) = take_bit m a\<close> if \<open>m \<le> Suc n\<close>
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3467
  using that by (rule le_SucE; intro bit_eqI)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3468
   (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3469
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3470
end
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3471
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3472
text \<open>Modulus centered around 0\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3473
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3474
lemma signed_take_bit_eq_concat_bit:
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3475
  \<open>signed_take_bit n k = concat_bit n k (- of_bool (bit k n))\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3476
  by (simp add: concat_bit_def signed_take_bit_def)
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3477
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3478
lemma signed_take_bit_add:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3479
  \<open>signed_take_bit n (signed_take_bit n k + signed_take_bit n l) = signed_take_bit n (k + l)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3480
  for k l :: int
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3481
proof -
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3482
  have \<open>take_bit (Suc n)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3483
     (take_bit (Suc n) (signed_take_bit n k) +
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3484
      take_bit (Suc n) (signed_take_bit n l)) =
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3485
    take_bit (Suc n) (k + l)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3486
    by (simp add: take_bit_signed_take_bit take_bit_add)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3487
  then show ?thesis
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3488
    by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_add)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3489
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3490
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3491
lemma signed_take_bit_diff:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3492
  \<open>signed_take_bit n (signed_take_bit n k - signed_take_bit n l) = signed_take_bit n (k - l)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3493
  for k l :: int
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3494
proof -
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3495
  have \<open>take_bit (Suc n)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3496
     (take_bit (Suc n) (signed_take_bit n k) -
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3497
      take_bit (Suc n) (signed_take_bit n l)) =
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3498
    take_bit (Suc n) (k - l)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3499
    by (simp add: take_bit_signed_take_bit take_bit_diff)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3500
  then show ?thesis
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3501
    by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_diff)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3502
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3503
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3504
lemma signed_take_bit_minus:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3505
  \<open>signed_take_bit n (- signed_take_bit n k) = signed_take_bit n (- k)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3506
  for k :: int
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3507
proof -
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3508
  have \<open>take_bit (Suc n)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3509
     (- take_bit (Suc n) (signed_take_bit n k)) =
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3510
    take_bit (Suc n) (- k)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3511
    by (simp add: take_bit_signed_take_bit take_bit_minus)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3512
  then show ?thesis
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3513
    by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_minus)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3514
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3515
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3516
lemma signed_take_bit_mult:
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3517
  \<open>signed_take_bit n (signed_take_bit n k * signed_take_bit n l) = signed_take_bit n (k * l)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3518
  for k l :: int
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3519
proof -
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3520
  have \<open>take_bit (Suc n)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3521
     (take_bit (Suc n) (signed_take_bit n k) *
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3522
      take_bit (Suc n) (signed_take_bit n l)) =
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3523
    take_bit (Suc n) (k * l)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3524
    by (simp add: take_bit_signed_take_bit take_bit_mult)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3525
  then show ?thesis
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3526
    by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_mult)
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3527
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72130
diff changeset
  3528
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3529
lemma signed_take_bit_eq_take_bit_minus:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3530
  \<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3531
  for k :: int
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3532
proof (cases \<open>bit k n\<close>)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3533
  case True
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3534
  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
  3535
    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
  3536
  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
  3537
    by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3538
  with True show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3539
    by (simp flip: minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3540
next
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3541
  case False
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3542
  show ?thesis
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3543
    by (rule bit_eqI) (simp add: False bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3544
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3545
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3546
lemma signed_take_bit_eq_take_bit_shift:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3547
  \<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3548
  for k :: int
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3549
proof -
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3550
  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
  3551
    by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3552
  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
  3553
    by (simp add: minus_exp_eq_not_mask)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3554
  also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3555
    by (rule disjunctive_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3556
      (simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3557
  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
  3558
  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
  3559
    by (simp only: take_bit_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3560
  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
  3561
    by (simp add: take_bit_Suc_from_most)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3562
  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
  3563
    by (simp add: ac_simps)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3564
  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
  3565
    by (rule disjunctive_add)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3566
      (auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3567
  finally show ?thesis
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3568
    using * ** by (simp add: signed_take_bit_def concat_bit_Suc min_def ac_simps)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3569
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3570
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3571
lemma signed_take_bit_nonnegative_iff [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3572
  \<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3573
  for k :: int
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3574
  by (simp add: signed_take_bit_def not_less concat_bit_def)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3575
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3576
lemma signed_take_bit_negative_iff [simp]:
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3577
  \<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3578
  for k :: int
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3579
  by (simp add: signed_take_bit_def not_less concat_bit_def)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3580
73868
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3581
lemma signed_take_bit_int_greater_eq_minus_exp [simp]:
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3582
  \<open>- (2 ^ n) \<le> signed_take_bit n k\<close>
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3583
  for k :: int
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3584
  by (simp add: signed_take_bit_eq_take_bit_shift)
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3585
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3586
lemma signed_take_bit_int_less_exp [simp]:
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3587
  \<open>signed_take_bit n k < 2 ^ n\<close>
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3588
  for k :: int
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3589
  using take_bit_int_less_exp [of \<open>Suc n\<close>]
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3590
  by (simp add: signed_take_bit_eq_take_bit_shift)
465846b611d5 some word streamlining
haftmann
parents: 73816
diff changeset
  3591
72261
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3592
lemma signed_take_bit_int_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3593
  \<open>signed_take_bit n k = k \<longleftrightarrow> - (2 ^ n) \<le> k \<and> k < 2 ^ n\<close>
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3594
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3595
  by (auto simp add: signed_take_bit_eq_take_bit_shift take_bit_int_eq_self_iff algebra_simps)
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3596
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3597
lemma signed_take_bit_int_eq_self:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3598
  \<open>signed_take_bit n k = k\<close> if \<open>- (2 ^ n) \<le> k\<close> \<open>k < 2 ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3599
  for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3600
  using that by (simp add: signed_take_bit_int_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3601
72261
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3602
lemma signed_take_bit_int_less_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3603
  \<open>signed_take_bit n k \<le> k \<longleftrightarrow> - (2 ^ n) \<le> k\<close>
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3604
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3605
  by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_eq_self_iff algebra_simps)
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3606
    linarith
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3607
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3608
lemma signed_take_bit_int_less_self_iff:
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3609
  \<open>signed_take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close>
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3610
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3611
  by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_self_iff algebra_simps)
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3612
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3613
lemma signed_take_bit_int_greater_self_iff:
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3614
  \<open>k < signed_take_bit n k \<longleftrightarrow> k < - (2 ^ n)\<close>
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3615
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3616
  by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_self_iff algebra_simps)
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3617
    linarith
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3618
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3619
lemma signed_take_bit_int_greater_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3620
  \<open>k \<le> signed_take_bit n k \<longleftrightarrow> k < 2 ^ n\<close>
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3621
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3622
  by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_eq_self_iff algebra_simps)
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3623
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3624
lemma signed_take_bit_int_greater_eq:
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3625
  \<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3626
  for k :: int
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3627
  using that take_bit_int_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>]
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3628
  by (simp add: signed_take_bit_eq_take_bit_shift)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3629
72261
5193570b739a more lemmas
haftmann
parents: 72241
diff changeset
  3630
lemma signed_take_bit_int_less_eq:
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3631
  \<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close>
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3632
  for k :: int
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3633
  using that take_bit_int_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>]
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3634
  by (simp add: signed_take_bit_eq_take_bit_shift)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3635
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3636
lemma signed_take_bit_Suc_bit0 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3637
  \<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * (2 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3638
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3639
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3640
lemma signed_take_bit_Suc_bit1 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3641
  \<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + (1 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3642
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3643
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3644
lemma signed_take_bit_Suc_minus_bit0 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3645
  \<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * (2 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3646
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3647
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3648
lemma signed_take_bit_Suc_minus_bit1 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3649
  \<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + (1 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3650
  by (simp add: signed_take_bit_Suc)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3651
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3652
lemma signed_take_bit_numeral_bit0 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3653
  \<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * (2 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3654
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3655
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3656
lemma signed_take_bit_numeral_bit1 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3657
  \<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + (1 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3658
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3659
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3660
lemma signed_take_bit_numeral_minus_bit0 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3661
  \<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3662
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3663
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3664
lemma signed_take_bit_numeral_minus_bit1 [simp]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3665
  \<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + (1 :: int)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3666
  by (simp add: signed_take_bit_rec)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3667
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3668
lemma signed_take_bit_code [code]:
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3669
  \<open>signed_take_bit n a =
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3670
  (let l = take_bit (Suc n) a
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3671
   in if bit l n then l + push_bit (Suc n) (- 1) else l)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3672
proof -
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3673
  have *: \<open>take_bit (Suc n) a + push_bit n (- 2) =
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3674
    take_bit (Suc n) a OR NOT (mask (Suc n))\<close>
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3675
    by (auto simp add: bit_take_bit_iff bit_push_bit_iff bit_not_iff bit_mask_iff disjunctive_add
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3676
       simp flip: push_bit_minus_one_eq_not_mask)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3677
  show ?thesis
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3678
    by (rule bit_eqI)
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3679
      (auto simp add: Let_def * bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq bit_not_iff
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3680
        bit_mask_iff bit_or_iff simp del: push_bit_minus_one_eq_not_mask)
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3681
qed
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3682
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3683
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3684
subsection \<open>Key ideas of bit operations\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3685
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3686
text \<open>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3687
  When formalizing bit operations, it is tempting to represent
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3688
  bit values as explicit lists over a binary type. This however
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3689
  is a bad idea, mainly due to the inherent ambiguities in
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3690
  representation concerning repeating leading bits.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3691
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3692
  Hence this approach avoids such explicit lists altogether
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3693
  following an algebraic path:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3694
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3695
  \<^item> Bit values are represented by numeric types: idealized
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3696
    unbounded bit values can be represented by type \<^typ>\<open>int\<close>,
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3697
    bounded bit values by quotient types over \<^typ>\<open>int\<close>.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3698
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3699
  \<^item> (A special case are idealized unbounded bit values ending
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3700
    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
  3701
    only support a restricted set of operations).
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3702
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3703
  \<^item> From this idea follows that
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3704
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3705
      \<^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
  3706
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3707
      \<^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
  3708
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3709
  \<^item> Concerning bounded bit values, iterated shifts to the left
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3710
    may result in eliminating all bits by shifting them all
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3711
    beyond the boundary.  The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3712
    represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary.
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3713
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71956
diff changeset
  3714
  \<^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
  3715
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3716
  \<^item> This leads to the most fundamental properties of bit values:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3717
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3718
      \<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3719
79480
c7cb1bf6efa0 consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents: 79117
diff changeset
  3720
      \<^item> Induction rule: @{thm bit_induct [where ?'a = int, no_vars]}
71800
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3721
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3722
  \<^item> Typical operations are characterized as follows:
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3723
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3724
      \<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3725
71956
a4bffc0de967 bit operations as distinctive library theory
haftmann
parents: 71922
diff changeset
  3726
      \<^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
  3727
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3728
      \<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3729
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3730
      \<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3731
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3732
      \<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3733
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3734
      \<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3735
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3736
      \<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3737
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3738
      \<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3739
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3740
      \<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]}
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3741
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3742
      \<^item> Set a single bit: @{thm set_bit_eq_or [where ?'a = int, no_vars]}
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3743
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3744
      \<^item> Unset a single bit: @{thm unset_bit_eq_and_not [where ?'a = int, no_vars]}
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3745
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3746
      \<^item> Flip a single bit: @{thm flip_bit_eq_xor [where ?'a = int, no_vars]}
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3747
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3748
      \<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]}
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3749
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3750
      \<^item> Bit concatenation: @{thm concat_bit_def [no_vars]}
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3751
08f1e4cb735f concatentation of bit values
haftmann
parents: 72023
diff changeset
  3752
      \<^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
  3753
\<close>
35a951ed2e82 documentation of relevant ideas
haftmann
parents: 71535
diff changeset
  3754
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3755
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3756
subsection \<open>Lemma duplicates and other\<close>
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3757
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3758
context semiring_bit_operations
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3759
begin
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3760
79117
7476818dfd5d generalized
haftmann
parents: 79116
diff changeset
  3761
lemmas bits_one_mod_two_eq_one [no_atp] = one_mod_two_eq_one
7476818dfd5d generalized
haftmann
parents: 79116
diff changeset
  3762
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3763
lemmas set_bit_def [no_atp] = set_bit_eq_or
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3764
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3765
lemmas unset_bit_def [no_atp] = unset_bit_eq_and_not
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3766
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3767
lemmas flip_bit_def [no_atp] = flip_bit_eq_xor
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3768
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3769
end
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3770
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3771
lemma and_nat_rec [no_atp]:
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3772
  \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3773
  by (fact and_rec)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3774
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3775
lemma or_nat_rec [no_atp]:
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3776
  \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3777
  by (fact or_rec)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3778
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3779
lemma xor_nat_rec [no_atp]:
79070
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3780
  \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3781
  by (fact xor_rec)
a4775fe69f5d restructured
haftmann
parents: 79069
diff changeset
  3782
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3783
lemma bit_push_bit_iff_nat [no_atp]:
79071
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  3784
  \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  3785
  by (fact bit_push_bit_iff')
7ab8b3f1d84b generalized
haftmann
parents: 79070
diff changeset
  3786
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3787
lemma mask_half_int [no_atp]:
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3788
  \<open>mask n div 2 = (mask (n - 1) :: int)\<close>
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3789
  by (fact mask_half)
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3790
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3791
lemma not_int_rec [no_atp]:
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3792
  \<open>NOT k = of_bool (even k) + 2 * NOT (k div 2)\<close> for k :: int
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3793
  by (fact not_rec)
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3794
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3795
lemma even_not_iff_int [no_atp]:
79068
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3796
  \<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3797
  by (fact even_not_iff)
cb72e2c0c539 sorted out lemma duplicates
haftmann
parents: 79031
diff changeset
  3798
79072
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  3799
lemma bit_not_int_iff':
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  3800
  \<open>bit (- k - 1) n \<longleftrightarrow> \<not> bit k n\<close> for k :: int
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  3801
  by (simp flip: not_eq_complement add: bit_simps)
a91050cd5c93 de-duplicated specification of class ring_bit_operations
haftmann
parents: 79071
diff changeset
  3802
79116
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3803
lemmas and_int_rec [no_atp] = and_int.rec
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3804
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3805
lemma even_and_iff_int [no_atp]:
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3806
  \<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3807
  by (fact even_and_iff)
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3808
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3809
lemmas bit_and_int_iff [no_atp] = and_int.bit_iff
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3810
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3811
lemmas or_int_rec [no_atp] = or_int.rec
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3812
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3813
lemmas bit_or_int_iff [no_atp] = or_int.bit_iff
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3814
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3815
lemmas xor_int_rec [no_atp] = xor_int.rec
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3816
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3817
lemmas bit_xor_int_iff [no_atp] = xor_int.bit_iff
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3818
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3819
lemma drop_bit_push_bit_int [no_atp]:
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3820
  \<open>drop_bit m (push_bit n k) = drop_bit (m - n) (push_bit (n - m) k)\<close> for k :: int
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3821
  by (fact drop_bit_push_bit)
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3822
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3823
lemma bit_push_bit_iff_int [no_atp] :
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3824
  \<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3825
  by (fact bit_push_bit_iff')
b90bf6636260 explicit annotation of lemma duplicates
haftmann
parents: 79072
diff changeset
  3826
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3827
no_notation
74391
930047942f46 repaired slip
haftmann
parents: 74364
diff changeset
  3828
  not  (\<open>NOT\<close>)
74364
99add5178e51 NOT is part of syntax bundle also
haftmann
parents: 74309
diff changeset
  3829
    and "and"  (infixr \<open>AND\<close> 64)
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3830
    and or  (infixr \<open>OR\<close>  59)
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3831
    and xor  (infixr \<open>XOR\<close> 59)
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3832
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3833
bundle bit_operations_syntax
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  3834
begin
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3835
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3836
notation
74391
930047942f46 repaired slip
haftmann
parents: 74364
diff changeset
  3837
  not  (\<open>NOT\<close>)
74364
99add5178e51 NOT is part of syntax bundle also
haftmann
parents: 74309
diff changeset
  3838
    and "and"  (infixr \<open>AND\<close> 64)
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3839
    and or  (infixr \<open>OR\<close>  59)
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3840
    and xor  (infixr \<open>XOR\<close> 59)
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3841
71442
d45495e897f4 more instances
haftmann
parents: 71426
diff changeset
  3842
end
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3843
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73969
diff changeset
  3844
end