| author | wenzelm | 
| Mon, 01 May 2017 20:28:27 +0200 | |
| changeset 65672 | 3848e278c278 | 
| parent 65363 | 5eb619751b14 | 
| child 67120 | 491fd7f0b5df | 
| permissions | -rw-r--r-- | 
| 65363 | 1 | (* Title: HOL/Word/Bits_Int.thy | 
| 2 | Author: Jeremy Dawson and Gerwin Klein, NICTA | |
| 24333 | 3 | |
| 65363 | 4 | Definitions and basic theorems for bit-wise logical operations | 
| 5 | for integers expressed using Pls, Min, BIT, | |
| 6 | and converting them to and from lists of bools. | |
| 7 | *) | |
| 24333 | 8 | |
| 61799 | 9 | section \<open>Bitwise Operations on Binary Integers\<close> | 
| 24350 | 10 | |
| 54854 
3324a0078636
prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
 haftmann parents: 
54848diff
changeset | 11 | theory Bits_Int | 
| 65363 | 12 | imports Bits Bit_Representation | 
| 24333 | 13 | begin | 
| 14 | ||
| 61799 | 15 | subsection \<open>Logical operations\<close> | 
| 24353 | 16 | |
| 17 | text "bit-wise logical operations on the int type" | |
| 18 | ||
| 25762 | 19 | instantiation int :: bit | 
| 20 | begin | |
| 21 | ||
| 65363 | 22 | definition int_not_def: "bitNOT = (\<lambda>x::int. - x - 1)" | 
| 46019 | 23 | |
| 65363 | 24 | function bitAND_int | 
| 25 | where "bitAND_int x y = | |
| 26 | (if x = 0 then 0 else if x = -1 then y | |
| 27 | else (bin_rest x AND bin_rest y) BIT (bin_last x \<and> bin_last y))" | |
| 46019 | 28 | by pat_completeness simp | 
| 25762 | 29 | |
| 46019 | 30 | termination | 
| 65363 | 31 | by (relation "measure (nat \<circ> abs \<circ> fst)", simp_all add: bin_rest_def) | 
| 46019 | 32 | |
| 33 | declare bitAND_int.simps [simp del] | |
| 25762 | 34 | |
| 46019 | 35 | definition int_or_def: | 
| 36 | "bitOR = (\<lambda>x y::int. NOT (NOT x AND NOT y))" | |
| 25762 | 37 | |
| 46019 | 38 | definition int_xor_def: | 
| 39 | "bitXOR = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))" | |
| 25762 | 40 | |
| 41 | instance .. | |
| 42 | ||
| 43 | end | |
| 24353 | 44 | |
| 61799 | 45 | subsubsection \<open>Basic simplification rules\<close> | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 46 | |
| 46016 | 47 | lemma int_not_BIT [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 48 | "NOT (w BIT b) = (NOT w) BIT (\<not> b)" | 
| 46016 | 49 | unfolding int_not_def Bit_def by (cases b, simp_all) | 
| 50 | ||
| 24333 | 51 | lemma int_not_simps [simp]: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 52 | "NOT (0::int) = -1" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 53 | "NOT (1::int) = -2" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 54 | "NOT (- 1::int) = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 55 | "NOT (numeral w::int) = - numeral (w + Num.One)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 56 | "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 57 | "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 58 | unfolding int_not_def by simp_all | 
| 24333 | 59 | |
| 46017 | 60 | lemma int_not_not [simp]: "NOT (NOT (x::int)) = x" | 
| 61 | unfolding int_not_def by simp | |
| 62 | ||
| 46019 | 63 | lemma int_and_0 [simp]: "(0::int) AND x = 0" | 
| 64 | by (simp add: bitAND_int.simps) | |
| 65 | ||
| 66 | lemma int_and_m1 [simp]: "(-1::int) AND x = x" | |
| 67 | by (simp add: bitAND_int.simps) | |
| 68 | ||
| 65363 | 69 | lemma int_and_Bits [simp]: | 
| 70 | "(x BIT b) AND (y BIT c) = (x AND y) BIT (b \<and> c)" | |
| 46019 | 71 | by (subst bitAND_int.simps, simp add: Bit_eq_0_iff Bit_eq_m1_iff) | 
| 46017 | 72 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 73 | lemma int_or_zero [simp]: "(0::int) OR x = x" | 
| 46017 | 74 | unfolding int_or_def by simp | 
| 46018 | 75 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 76 | lemma int_or_minus1 [simp]: "(-1::int) OR x = -1" | 
| 46017 | 77 | unfolding int_or_def by simp | 
| 78 | ||
| 65363 | 79 | lemma int_or_Bits [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 80 | "(x BIT b) OR (y BIT c) = (x OR y) BIT (b \<or> c)" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 81 | unfolding int_or_def by simp | 
| 24333 | 82 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 83 | lemma int_xor_zero [simp]: "(0::int) XOR x = x" | 
| 46018 | 84 | unfolding int_xor_def by simp | 
| 85 | ||
| 65363 | 86 | lemma int_xor_Bits [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 87 | "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 88 | unfolding int_xor_def by auto | 
| 46018 | 89 | |
| 61799 | 90 | subsubsection \<open>Binary destructors\<close> | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 91 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 92 | lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)" | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 93 | by (cases x rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 94 | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 95 | lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 96 | by (cases x rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 97 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 98 | lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y" | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 99 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 100 | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 101 | lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 102 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 103 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 104 | lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y" | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 105 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 106 | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 107 | lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 108 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 109 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 110 | lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y" | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 111 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 112 | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 113 | lemma bin_last_XOR [simp]: "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 114 | by (cases x rule: bin_exhaust, cases y rule: bin_exhaust, simp) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 115 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 116 | lemma bin_nth_ops: | 
| 65363 | 117 | "!!x y. bin_nth (x AND y) n = (bin_nth x n & bin_nth y n)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 118 | "!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)" | 
| 65363 | 119 | "!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 120 | "!!x. bin_nth (NOT x) n = (~ bin_nth x n)" | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 121 | by (induct n) auto | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 122 | |
| 61799 | 123 | subsubsection \<open>Derived properties\<close> | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 124 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 125 | lemma int_xor_minus1 [simp]: "(-1::int) XOR x = NOT x" | 
| 46018 | 126 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 127 | ||
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 128 | lemma int_xor_extra_simps [simp]: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 129 | "w XOR (0::int) = w" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 130 | "w XOR (-1::int) = NOT w" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 131 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 132 | |
| 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 133 | lemma int_or_extra_simps [simp]: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 134 | "w OR (0::int) = w" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 135 | "w OR (-1::int) = -1" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 136 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 137 | |
| 37667 | 138 | lemma int_and_extra_simps [simp]: | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 139 | "w AND (0::int) = 0" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 140 | "w AND (-1::int) = w" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 141 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 142 | |
| 143 | (* commutativity of the above *) | |
| 144 | lemma bin_ops_comm: | |
| 145 | shows | |
| 24353 | 146 | int_and_comm: "!!y::int. x AND y = y AND x" and | 
| 147 | int_or_comm: "!!y::int. x OR y = y OR x" and | |
| 148 | int_xor_comm: "!!y::int. x XOR y = y XOR x" | |
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 149 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 150 | |
| 151 | lemma bin_ops_same [simp]: | |
| 65363 | 152 | "(x::int) AND x = x" | 
| 153 | "(x::int) OR x = x" | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 154 | "(x::int) XOR x = 0" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 155 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 156 | |
| 65363 | 157 | lemmas bin_log_esimps = | 
| 24333 | 158 | int_and_extra_simps int_or_extra_simps int_xor_extra_simps | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 159 | int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1 | 
| 24333 | 160 | |
| 161 | (* basic properties of logical (bit-wise) operations *) | |
| 162 | ||
| 65363 | 163 | lemma bbw_ao_absorb: | 
| 24353 | 164 | "!!y::int. x AND (y OR x) = x & x OR (y AND x) = x" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 165 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 166 | |
| 167 | lemma bbw_ao_absorbs_other: | |
| 24353 | 168 | "x AND (x OR y) = x \<and> (y AND x) OR x = (x::int)" | 
| 169 | "(y OR x) AND x = x \<and> x OR (x AND y) = (x::int)" | |
| 170 | "(x OR y) AND x = x \<and> (x AND y) OR x = (x::int)" | |
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 171 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24353 | 172 | |
| 24333 | 173 | lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other | 
| 174 | ||
| 175 | lemma int_xor_not: | |
| 65363 | 176 | "!!y::int. (NOT x) XOR y = NOT (x XOR y) & | 
| 24353 | 177 | x XOR (NOT y) = NOT (x XOR y)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 178 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 179 | |
| 180 | lemma int_and_assoc: | |
| 24353 | 181 | "(x AND y) AND (z::int) = x AND (y AND z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 182 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 183 | |
| 184 | lemma int_or_assoc: | |
| 24353 | 185 | "(x OR y) OR (z::int) = x OR (y OR z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 186 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 187 | |
| 188 | lemma int_xor_assoc: | |
| 24353 | 189 | "(x XOR y) XOR (z::int) = x XOR (y XOR z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 190 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 191 | |
| 192 | lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc | |
| 193 | ||
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 194 | (* BH: Why are these declared as simp rules??? *) | 
| 65363 | 195 | lemma bbw_lcs [simp]: | 
| 24353 | 196 | "(y::int) AND (x AND z) = x AND (y AND z)" | 
| 197 | "(y::int) OR (x OR z) = x OR (y OR z)" | |
| 65363 | 198 | "(y::int) XOR (x XOR z) = x XOR (y XOR z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 199 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 200 | |
| 65363 | 201 | lemma bbw_not_dist: | 
| 202 | "!!y::int. NOT (x OR y) = (NOT x) AND (NOT y)" | |
| 24353 | 203 | "!!y::int. NOT (x AND y) = (NOT x) OR (NOT y)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 204 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 205 | |
| 65363 | 206 | lemma bbw_oa_dist: | 
| 207 | "!!y z::int. (x AND y) OR z = | |
| 24353 | 208 | (x OR z) AND (y OR z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 209 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 210 | |
| 65363 | 211 | lemma bbw_ao_dist: | 
| 212 | "!!y z::int. (x OR y) AND z = | |
| 24353 | 213 | (x AND z) OR (y AND z)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 214 | by (auto simp add: bin_eq_iff bin_nth_ops) | 
| 24333 | 215 | |
| 24367 
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
 huffman parents: 
24366diff
changeset | 216 | (* | 
| 
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
 huffman parents: 
24366diff
changeset | 217 | Why were these declared simp??? | 
| 65363 | 218 | declare bin_ops_comm [simp] bbw_assocs [simp] | 
| 24367 
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
 huffman parents: 
24366diff
changeset | 219 | *) | 
| 24333 | 220 | |
| 61799 | 221 | subsubsection \<open>Simplification with numerals\<close> | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 222 | |
| 61799 | 223 | text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by | 
| 224 | other simp rules.\<close> | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 225 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 226 | lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y" | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 227 | by (metis (mono_tags) BIT_eq_iff bin_ex_rl bin_last_BIT bin_rest_BIT) | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 228 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 229 | lemma bin_rest_neg_numeral_BitM [simp]: | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 230 | "bin_rest (- numeral (Num.BitM w)) = - numeral w" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 231 | by (simp only: BIT_bin_simps [symmetric] bin_rest_BIT) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 232 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 233 | lemma bin_last_neg_numeral_BitM [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 234 | "bin_last (- numeral (Num.BitM w))" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 235 | by (simp only: BIT_bin_simps [symmetric] bin_last_BIT) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 236 | |
| 61799 | 237 | text \<open>FIXME: The rule sets below are very large (24 rules for each | 
| 238 | operator). Is there a simpler way to do this?\<close> | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 239 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 240 | lemma int_and_numerals [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 241 | "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 242 | "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 243 | "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 244 | "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 245 | "numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 246 | "numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 247 | "numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 248 | "numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 249 | "- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (- numeral x AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 250 | "- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (- numeral x AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 251 | "- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 252 | "- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 253 | "- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (- numeral x AND - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 254 | "- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (- numeral x AND - numeral (y + Num.One)) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 255 | "- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 256 | "- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND - numeral (y + Num.One)) BIT True" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 257 | "(1::int) AND numeral (Num.Bit0 y) = 0" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 258 | "(1::int) AND numeral (Num.Bit1 y) = 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 259 | "(1::int) AND - numeral (Num.Bit0 y) = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 260 | "(1::int) AND - numeral (Num.Bit1 y) = 1" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 261 | "numeral (Num.Bit0 x) AND (1::int) = 0" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 262 | "numeral (Num.Bit1 x) AND (1::int) = 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 263 | "- numeral (Num.Bit0 x) AND (1::int) = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 264 | "- numeral (Num.Bit1 x) AND (1::int) = 1" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 265 | by (rule bin_rl_eqI, simp, simp)+ | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 266 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 267 | lemma int_or_numerals [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 268 | "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 269 | "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 270 | "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 271 | "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 272 | "numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 273 | "numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 274 | "numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 275 | "numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 276 | "- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (- numeral x OR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 277 | "- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (- numeral x OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 278 | "- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 279 | "- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 280 | "- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (- numeral x OR - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 281 | "- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (- numeral x OR - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 282 | "- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR - numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 283 | "- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR - numeral (y + Num.One)) BIT True" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 284 | "(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 285 | "(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 286 | "(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 287 | "(1::int) OR - numeral (Num.Bit1 y) = - numeral (Num.Bit1 y)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 288 | "numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 289 | "numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 290 | "- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 291 | "- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 292 | by (rule bin_rl_eqI, simp, simp)+ | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 293 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 294 | lemma int_xor_numerals [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 295 | "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 296 | "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 297 | "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 298 | "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 299 | "numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 300 | "numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 301 | "numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 302 | "numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 303 | "- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (- numeral x XOR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 304 | "- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (- numeral x XOR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 305 | "- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 306 | "- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 307 | "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (- numeral x XOR - numeral y) BIT False" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 308 | "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (- numeral x XOR - numeral (y + Num.One)) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 309 | "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR - numeral y) BIT True" | 
| 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 310 | "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR - numeral (y + Num.One)) BIT False" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 311 | "(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 312 | "(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 313 | "(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 314 | "(1::int) XOR - numeral (Num.Bit1 y) = - numeral (Num.Bit0 (y + Num.One))" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 315 | "numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 316 | "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 317 | "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54427diff
changeset | 318 | "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 319 | by (rule bin_rl_eqI, simp, simp)+ | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 320 | |
| 61799 | 321 | subsubsection \<open>Interactions with arithmetic\<close> | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 322 | |
| 24333 | 323 | lemma plus_and_or [rule_format]: | 
| 24353 | 324 | "ALL y::int. (x AND y) + (x OR y) = x + y" | 
| 24333 | 325 | apply (induct x rule: bin_induct) | 
| 326 | apply clarsimp | |
| 327 | apply clarsimp | |
| 328 | apply clarsimp | |
| 329 | apply (case_tac y rule: bin_exhaust) | |
| 330 | apply clarsimp | |
| 331 | apply (unfold Bit_def) | |
| 332 | apply clarsimp | |
| 333 | apply (erule_tac x = "x" in allE) | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 334 | apply simp | 
| 24333 | 335 | done | 
| 336 | ||
| 337 | lemma le_int_or: | |
| 46604 
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
 huffman parents: 
46023diff
changeset | 338 | "bin_sign (y::int) = 0 ==> x <= x OR y" | 
| 37667 | 339 | apply (induct y arbitrary: x rule: bin_induct) | 
| 24333 | 340 | apply clarsimp | 
| 341 | apply clarsimp | |
| 342 | apply (case_tac x rule: bin_exhaust) | |
| 343 | apply (case_tac b) | |
| 344 | apply (case_tac [!] bit) | |
| 46604 
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
 huffman parents: 
46023diff
changeset | 345 | apply (auto simp: le_Bits) | 
| 24333 | 346 | done | 
| 347 | ||
| 348 | lemmas int_and_le = | |
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 349 | xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] | 
| 24333 | 350 | |
| 24364 | 351 | (* interaction between bit-wise and arithmetic *) | 
| 352 | (* good example of bin_induction *) | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 353 | lemma bin_add_not: "x + NOT x = (-1::int)" | 
| 24364 | 354 | apply (induct x rule: bin_induct) | 
| 355 | apply clarsimp | |
| 356 | apply clarsimp | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 357 | apply (case_tac bit, auto) | 
| 24364 | 358 | done | 
| 359 | ||
| 61799 | 360 | subsubsection \<open>Truncating results of bit-wise operations\<close> | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 361 | |
| 65363 | 362 | lemma bin_trunc_ao: | 
| 363 | "!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" | |
| 24364 | 364 | "!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 365 | by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) | 
| 24364 | 366 | |
| 65363 | 367 | lemma bin_trunc_xor: | 
| 368 | "!!x y. bintrunc n (bintrunc n x XOR bintrunc n y) = | |
| 24364 | 369 | bintrunc n (x XOR y)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 370 | by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) | 
| 24364 | 371 | |
| 65363 | 372 | lemma bin_trunc_not: | 
| 24364 | 373 | "!!x. bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)" | 
| 45543 
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
 huffman parents: 
45529diff
changeset | 374 | by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) | 
| 24364 | 375 | |
| 376 | (* want theorems of the form of bin_trunc_xor *) | |
| 377 | lemma bintr_bintr_i: | |
| 378 | "x = bintrunc n y ==> bintrunc n x = bintrunc n y" | |
| 379 | by auto | |
| 380 | ||
| 381 | lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i] | |
| 382 | lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i] | |
| 383 | ||
| 61799 | 384 | subsection \<open>Setting and clearing bits\<close> | 
| 24364 | 385 | |
| 54874 | 386 | (** nth bit, set/clear **) | 
| 387 | ||
| 26558 | 388 | primrec | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 389 | bin_sc :: "nat => bool => int => int" | 
| 26558 | 390 | where | 
| 391 | Z: "bin_sc 0 b w = bin_rest w BIT b" | |
| 392 | | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w" | |
| 24364 | 393 | |
| 65363 | 394 | lemma bin_nth_sc [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 395 | "bin_nth (bin_sc n b w) n \<longleftrightarrow> b" | 
| 45955 | 396 | by (induct n arbitrary: w) auto | 
| 24333 | 397 | |
| 65363 | 398 | lemma bin_sc_sc_same [simp]: | 
| 45955 | 399 | "bin_sc n c (bin_sc n b w) = bin_sc n c w" | 
| 400 | by (induct n arbitrary: w) auto | |
| 24333 | 401 | |
| 402 | lemma bin_sc_sc_diff: | |
| 65363 | 403 | "m ~= n ==> | 
| 24333 | 404 | bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)" | 
| 45955 | 405 | apply (induct n arbitrary: w m) | 
| 24333 | 406 | apply (case_tac [!] m) | 
| 407 | apply auto | |
| 408 | done | |
| 409 | ||
| 65363 | 410 | lemma bin_nth_sc_gen: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 411 | "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)" | 
| 45955 | 412 | by (induct n arbitrary: w m) (case_tac [!] m, auto) | 
| 65363 | 413 | |
| 24333 | 414 | lemma bin_sc_nth [simp]: | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 415 | "(bin_sc n (bin_nth w n) w) = w" | 
| 45955 | 416 | by (induct n arbitrary: w) auto | 
| 24333 | 417 | |
| 418 | lemma bin_sign_sc [simp]: | |
| 45955 | 419 | "bin_sign (bin_sc n b w) = bin_sign w" | 
| 420 | by (induct n arbitrary: w) auto | |
| 65363 | 421 | |
| 422 | lemma bin_sc_bintr [simp]: | |
| 45955 | 423 | "bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)" | 
| 424 | apply (induct n arbitrary: w m) | |
| 24333 | 425 | apply (case_tac [!] w rule: bin_exhaust) | 
| 426 | apply (case_tac [!] m, auto) | |
| 427 | done | |
| 428 | ||
| 429 | lemma bin_clr_le: | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 430 | "bin_sc n False w <= w" | 
| 45955 | 431 | apply (induct n arbitrary: w) | 
| 24333 | 432 | apply (case_tac [!] w rule: bin_exhaust) | 
| 46605 | 433 | apply (auto simp: le_Bits) | 
| 24333 | 434 | done | 
| 435 | ||
| 436 | lemma bin_set_ge: | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 437 | "bin_sc n True w >= w" | 
| 45955 | 438 | apply (induct n arbitrary: w) | 
| 24333 | 439 | apply (case_tac [!] w rule: bin_exhaust) | 
| 46605 | 440 | apply (auto simp: le_Bits) | 
| 24333 | 441 | done | 
| 442 | ||
| 443 | lemma bintr_bin_clr_le: | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 444 | "bintrunc n (bin_sc m False w) <= bintrunc n w" | 
| 45955 | 445 | apply (induct n arbitrary: w m) | 
| 24333 | 446 | apply simp | 
| 447 | apply (case_tac w rule: bin_exhaust) | |
| 448 | apply (case_tac m) | |
| 46605 | 449 | apply (auto simp: le_Bits) | 
| 24333 | 450 | done | 
| 451 | ||
| 452 | lemma bintr_bin_set_ge: | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 453 | "bintrunc n (bin_sc m True w) >= bintrunc n w" | 
| 45955 | 454 | apply (induct n arbitrary: w m) | 
| 24333 | 455 | apply simp | 
| 456 | apply (case_tac w rule: bin_exhaust) | |
| 457 | apply (case_tac m) | |
| 46605 | 458 | apply (auto simp: le_Bits) | 
| 24333 | 459 | done | 
| 460 | ||
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 461 | lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0" | 
| 46608 
37e383cc7831
make uses of constant bin_sc respect int/bin distinction
 huffman parents: 
46605diff
changeset | 462 | by (induct n) auto | 
| 24333 | 463 | |
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
54874diff
changeset | 464 | lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1" | 
| 46608 
37e383cc7831
make uses of constant bin_sc respect int/bin distinction
 huffman parents: 
46605diff
changeset | 465 | by (induct n) auto | 
| 65363 | 466 | |
| 24333 | 467 | lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP | 
| 468 | ||
| 469 | lemma bin_sc_minus: | |
| 470 | "0 < n ==> bin_sc (Suc (n - 1)) b w = bin_sc n b w" | |
| 471 | by auto | |
| 472 | ||
| 65363 | 473 | lemmas bin_sc_Suc_minus = | 
| 45604 | 474 | trans [OF bin_sc_minus [symmetric] bin_sc.Suc] | 
| 24333 | 475 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 476 | lemma bin_sc_numeral [simp]: | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46610diff
changeset | 477 | "bin_sc (numeral k) b w = | 
| 47219 
172c031ad743
restate various simp rules for word operations using pred_numeral
 huffman parents: 
47108diff
changeset | 478 | bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w" | 
| 
172c031ad743
restate various simp rules for word operations using pred_numeral
 huffman parents: 
47108diff
changeset | 479 | by (simp add: numeral_eq_Suc) | 
| 24333 | 480 | |
| 24465 | 481 | |
| 61799 | 482 | subsection \<open>Splitting and concatenation\<close> | 
| 24333 | 483 | |
| 54848 | 484 | definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int" | 
| 485 | where | |
| 37667 | 486 | "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0" | 
| 487 | ||
| 54848 | 488 | fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" | 
| 489 | where | |
| 26558 | 490 | "bin_rsplit_aux n m c bs = | 
| 24364 | 491 | (if m = 0 | n = 0 then bs else | 
| 65363 | 492 | let (a, b) = bin_split n c | 
| 26558 | 493 | in bin_rsplit_aux n (m - n) a (b # bs))" | 
| 24364 | 494 | |
| 54848 | 495 | definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" | 
| 496 | where | |
| 26558 | 497 | "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []" | 
| 498 | ||
| 54848 | 499 | fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" | 
| 500 | where | |
| 26558 | 501 | "bin_rsplitl_aux n m c bs = | 
| 24364 | 502 | (if m = 0 | n = 0 then bs else | 
| 65363 | 503 | let (a, b) = bin_split (min m n) c | 
| 26558 | 504 | in bin_rsplitl_aux n (m - n) a (b # bs))" | 
| 24364 | 505 | |
| 54848 | 506 | definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" | 
| 507 | where | |
| 26558 | 508 | "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []" | 
| 509 | ||
| 24364 | 510 | declare bin_rsplit_aux.simps [simp del] | 
| 511 | declare bin_rsplitl_aux.simps [simp del] | |
| 512 | ||
| 65363 | 513 | lemma bin_sign_cat: | 
| 45955 | 514 | "bin_sign (bin_cat x n y) = bin_sign x" | 
| 515 | by (induct n arbitrary: y) auto | |
| 24364 | 516 | |
| 517 | lemma bin_cat_Suc_Bit: | |
| 518 | "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b" | |
| 519 | by auto | |
| 520 | ||
| 65363 | 521 | lemma bin_nth_cat: | 
| 522 | "bin_nth (bin_cat x k y) n = | |
| 24364 | 523 | (if n < k then bin_nth y n else bin_nth x (n - k))" | 
| 45955 | 524 | apply (induct k arbitrary: n y) | 
| 24364 | 525 | apply clarsimp | 
| 526 | apply (case_tac n, auto) | |
| 24333 | 527 | done | 
| 528 | ||
| 24364 | 529 | lemma bin_nth_split: | 
| 65363 | 530 | "bin_split n c = (a, b) ==> | 
| 531 | (ALL k. bin_nth a k = bin_nth c (n + k)) & | |
| 24364 | 532 | (ALL k. bin_nth b k = (k < n & bin_nth c k))" | 
| 45955 | 533 | apply (induct n arbitrary: b c) | 
| 24364 | 534 | apply clarsimp | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 535 | apply (clarsimp simp: Let_def split: prod.split_asm) | 
| 24364 | 536 | apply (case_tac k) | 
| 537 | apply auto | |
| 538 | done | |
| 539 | ||
| 65363 | 540 | lemma bin_cat_assoc: | 
| 541 | "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" | |
| 45955 | 542 | by (induct n arbitrary: z) auto | 
| 24364 | 543 | |
| 45955 | 544 | lemma bin_cat_assoc_sym: | 
| 545 | "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z" | |
| 546 | apply (induct n arbitrary: z m, clarsimp) | |
| 24364 | 547 | apply (case_tac m, auto) | 
| 24333 | 548 | done | 
| 549 | ||
| 45956 | 550 | lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w" | 
| 46001 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
 huffman parents: 
45956diff
changeset | 551 | by (induct n arbitrary: w) auto | 
| 45956 | 552 | |
| 65363 | 553 | lemma bintr_cat1: | 
| 45955 | 554 | "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" | 
| 555 | by (induct n arbitrary: b) auto | |
| 65363 | 556 | |
| 557 | lemma bintr_cat: "bintrunc m (bin_cat a n b) = | |
| 24364 | 558 | bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" | 
| 559 | by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) | |
| 65363 | 560 | |
| 561 | lemma bintr_cat_same [simp]: | |
| 24364 | 562 | "bintrunc n (bin_cat a n b) = bintrunc n b" | 
| 563 | by (auto simp add : bintr_cat) | |
| 564 | ||
| 65363 | 565 | lemma cat_bintr [simp]: | 
| 45955 | 566 | "bin_cat a n (bintrunc n b) = bin_cat a n b" | 
| 567 | by (induct n arbitrary: b) auto | |
| 24364 | 568 | |
| 65363 | 569 | lemma split_bintrunc: | 
| 45955 | 570 | "bin_split n c = (a, b) ==> b = bintrunc n c" | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 571 | by (induct n arbitrary: b c) (auto simp: Let_def split: prod.split_asm) | 
| 24364 | 572 | |
| 573 | lemma bin_cat_split: | |
| 45955 | 574 | "bin_split n w = (u, v) ==> w = bin_cat u n v" | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 575 | by (induct n arbitrary: v w) (auto simp: Let_def split: prod.split_asm) | 
| 24364 | 576 | |
| 577 | lemma bin_split_cat: | |
| 45955 | 578 | "bin_split n (bin_cat v n w) = (v, bintrunc n w)" | 
| 579 | by (induct n arbitrary: w) auto | |
| 24364 | 580 | |
| 45956 | 581 | lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)" | 
| 46001 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
 huffman parents: 
45956diff
changeset | 582 | by (induct n) auto | 
| 45956 | 583 | |
| 46610 
0c3a5e28f425
make uses of bin_split respect int/bin distinction
 huffman parents: 
46609diff
changeset | 584 | lemma bin_split_minus1 [simp]: | 
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
54874diff
changeset | 585 | "bin_split n (- 1) = (- 1, bintrunc n (- 1))" | 
| 46610 
0c3a5e28f425
make uses of bin_split respect int/bin distinction
 huffman parents: 
46609diff
changeset | 586 | by (induct n) auto | 
| 24364 | 587 | |
| 588 | lemma bin_split_trunc: | |
| 65363 | 589 | "bin_split (min m n) c = (a, b) ==> | 
| 24364 | 590 | bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" | 
| 45955 | 591 | apply (induct n arbitrary: m b c, clarsimp) | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 592 | apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) | 
| 24364 | 593 | apply (case_tac m) | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 594 | apply (auto simp: Let_def split: prod.split_asm) | 
| 24333 | 595 | done | 
| 596 | ||
| 24364 | 597 | lemma bin_split_trunc1: | 
| 65363 | 598 | "bin_split n c = (a, b) ==> | 
| 24364 | 599 | bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" | 
| 45955 | 600 | apply (induct n arbitrary: m b c, clarsimp) | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 601 | apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) | 
| 24364 | 602 | apply (case_tac m) | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 603 | apply (auto simp: Let_def split: prod.split_asm) | 
| 24364 | 604 | done | 
| 24333 | 605 | |
| 24364 | 606 | lemma bin_cat_num: | 
| 45955 | 607 | "bin_cat a n b = a * 2 ^ n + bintrunc n b" | 
| 608 | apply (induct n arbitrary: b, clarsimp) | |
| 46001 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
 huffman parents: 
45956diff
changeset | 609 | apply (simp add: Bit_def) | 
| 24364 | 610 | done | 
| 611 | ||
| 612 | lemma bin_split_num: | |
| 45955 | 613 | "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" | 
| 46610 
0c3a5e28f425
make uses of bin_split respect int/bin distinction
 huffman parents: 
46609diff
changeset | 614 | apply (induct n arbitrary: b, simp) | 
| 45529 
0e1037d4e049
remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
 huffman parents: 
45475diff
changeset | 615 | apply (simp add: bin_rest_def zdiv_zmult2_eq) | 
| 24364 | 616 | apply (case_tac b rule: bin_exhaust) | 
| 617 | apply simp | |
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 618 | apply (simp add: Bit_def mod_mult_mult1 p1mod22k) | 
| 45955 | 619 | done | 
| 24364 | 620 | |
| 61799 | 621 | subsection \<open>Miscellaneous lemmas\<close> | 
| 24333 | 622 | |
| 65363 | 623 | lemma nth_2p_bin: | 
| 45955 | 624 | "bin_nth (2 ^ n) m = (m = n)" | 
| 625 | apply (induct n arbitrary: m) | |
| 24333 | 626 | apply clarsimp | 
| 627 | apply safe | |
| 65363 | 628 | apply (case_tac m) | 
| 24333 | 629 | apply (auto simp: Bit_B0_2t [symmetric]) | 
| 630 | done | |
| 631 | ||
| 632 | (* for use when simplifying with bin_nth_Bit *) | |
| 633 | ||
| 634 | lemma ex_eq_or: | |
| 635 | "(EX m. n = Suc m & (m = k | P m)) = (n = Suc k | (EX m. n = Suc m & P m))" | |
| 636 | by auto | |
| 637 | ||
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 638 | lemma power_BIT: "2 ^ (Suc n) - 1 = (2 ^ n - 1) BIT True" | 
| 54427 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 639 | unfolding Bit_B1 | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 640 | by (induct n) simp_all | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 641 | |
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 642 | lemma mod_BIT: | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 643 | "bin BIT bit mod 2 ^ Suc n = (bin mod 2 ^ n) BIT bit" | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 644 | proof - | 
| 64593 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 645 | have "2 * (bin mod 2 ^ n) + 1 = (2 * bin mod 2 ^ Suc n) + 1" | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 646 | by (simp add: mod_mult_mult1) | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 647 | also have "\<dots> = ((2 * bin mod 2 ^ Suc n) + 1) mod 2 ^ Suc n" | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 648 | by (simp add: ac_simps p1mod22k') | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 649 | also have "\<dots> = (2 * bin + 1) mod 2 ^ Suc n" | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 650 | by (simp only: mod_simps) | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 651 | finally show ?thesis | 
| 
50c715579715
reoriented congruence rules in non-explosive direction
 haftmann parents: 
61799diff
changeset | 652 | by (auto simp add: Bit_def) | 
| 54427 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 653 | qed | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 654 | |
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 655 | lemma AND_mod: | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 656 | fixes x :: int | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 657 | shows "x AND 2 ^ n - 1 = x mod 2 ^ n" | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 658 | proof (induct x arbitrary: n rule: bin_induct) | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 659 | case 1 | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 660 | then show ?case | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 661 | by simp | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 662 | next | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 663 | case 2 | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 664 | then show ?case | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 665 | by (simp, simp add: m1mod2k) | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 666 | next | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 667 | case (3 bin bit) | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 668 | show ?case | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 669 | proof (cases n) | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 670 | case 0 | 
| 54847 
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
 haftmann parents: 
54489diff
changeset | 671 | then show ?thesis by simp | 
| 54427 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 672 | next | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 673 | case (Suc m) | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 674 | with 3 show ?thesis | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 675 | by (simp only: power_BIT mod_BIT int_and_Bits) simp | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 676 | qed | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 677 | qed | 
| 
783861a66a60
separated comparision on bit operations into separate theory
 haftmann parents: 
54224diff
changeset | 678 | |
| 24333 | 679 | end | 
| 53062 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
 haftmann parents: 
47219diff
changeset | 680 |