author | haftmann |
Wed, 02 Apr 2008 15:58:36 +0200 | |
changeset 26514 | eff55c0a6d34 |
parent 26086 | 3c243098b64a |
child 26558 | 7fcc10088e72 |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
ID: $Id$ |
|
3 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
|
4 |
||
5 |
definition and basic theorems for bit-wise logical operations |
|
6 |
for integers expressed using Pls, Min, BIT, |
|
7 |
and converting them to and from lists of bools |
|
8 |
*) |
|
9 |
||
24350 | 10 |
header {* Bitwise Operations on Binary Integers *} |
11 |
||
24353 | 12 |
theory BinOperations imports BinGeneral BitSyntax |
24333 | 13 |
|
14 |
begin |
|
15 |
||
24364 | 16 |
subsection {* Logical operations *} |
24353 | 17 |
|
18 |
text "bit-wise logical operations on the int type" |
|
19 |
||
25762 | 20 |
instantiation int :: bit |
21 |
begin |
|
22 |
||
23 |
definition |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
24 |
int_not_def: "bitNOT = bin_rec Int.Min Int.Pls |
24353 | 25 |
(\<lambda>w b s. s BIT (NOT b))" |
25762 | 26 |
|
27 |
definition |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
28 |
int_and_def: "bitAND = bin_rec (\<lambda>x. Int.Pls) (\<lambda>y. y) |
24353 | 29 |
(\<lambda>w b s y. s (bin_rest y) BIT (b AND bin_last y))" |
25762 | 30 |
|
31 |
definition |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
32 |
int_or_def: "bitOR = bin_rec (\<lambda>x. x) (\<lambda>y. Int.Min) |
24353 | 33 |
(\<lambda>w b s y. s (bin_rest y) BIT (b OR bin_last y))" |
25762 | 34 |
|
35 |
definition |
|
36 |
int_xor_def: "bitXOR = bin_rec (\<lambda>x. x) bitNOT |
|
24353 | 37 |
(\<lambda>w b s y. s (bin_rest y) BIT (b XOR bin_last y))" |
25762 | 38 |
|
39 |
instance .. |
|
40 |
||
41 |
end |
|
24353 | 42 |
|
24333 | 43 |
lemma int_not_simps [simp]: |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
44 |
"NOT Int.Pls = Int.Min" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
45 |
"NOT Int.Min = Int.Pls" |
24353 | 46 |
"NOT (w BIT b) = (NOT w) BIT (NOT b)" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
47 |
"NOT (Int.Bit0 w) = Int.Bit1 (NOT w)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
48 |
"NOT (Int.Bit1 w) = Int.Bit0 (NOT w)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
49 |
unfolding int_not_def by (simp_all add: bin_rec_simps) |
24333 | 50 |
|
51 |
lemma int_xor_Pls [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
52 |
"Int.Pls XOR x = x" |
24465 | 53 |
unfolding int_xor_def by (simp add: bin_rec_PM) |
24333 | 54 |
|
55 |
lemma int_xor_Min [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
56 |
"Int.Min XOR x = NOT x" |
24465 | 57 |
unfolding int_xor_def by (simp add: bin_rec_PM) |
24333 | 58 |
|
59 |
lemma int_xor_Bits [simp]: |
|
24353 | 60 |
"(x BIT b) XOR (y BIT c) = (x XOR y) BIT (b XOR c)" |
24333 | 61 |
apply (unfold int_xor_def) |
62 |
apply (rule bin_rec_simps (1) [THEN fun_cong, THEN trans]) |
|
63 |
apply (rule ext, simp) |
|
64 |
prefer 2 |
|
65 |
apply simp |
|
66 |
apply (rule ext) |
|
67 |
apply (simp add: int_not_simps [symmetric]) |
|
68 |
done |
|
69 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
70 |
lemma int_xor_Bits2 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
71 |
"(Int.Bit0 x) XOR (Int.Bit0 y) = Int.Bit0 (x XOR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
72 |
"(Int.Bit0 x) XOR (Int.Bit1 y) = Int.Bit1 (x XOR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
73 |
"(Int.Bit1 x) XOR (Int.Bit0 y) = Int.Bit1 (x XOR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
74 |
"(Int.Bit1 x) XOR (Int.Bit1 y) = Int.Bit0 (x XOR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
75 |
unfolding BIT_simps [symmetric] int_xor_Bits by simp_all |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
76 |
|
24333 | 77 |
lemma int_xor_x_simps': |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
78 |
"w XOR (Int.Pls BIT bit.B0) = w" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
79 |
"w XOR (Int.Min BIT bit.B1) = NOT w" |
24333 | 80 |
apply (induct w rule: bin_induct) |
81 |
apply simp_all[4] |
|
82 |
apply (unfold int_xor_Bits) |
|
83 |
apply clarsimp+ |
|
84 |
done |
|
85 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
86 |
lemma int_xor_extra_simps [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
87 |
"w XOR Int.Pls = w" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
88 |
"w XOR Int.Min = NOT w" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
89 |
using int_xor_x_simps' by simp_all |
24333 | 90 |
|
91 |
lemma int_or_Pls [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
92 |
"Int.Pls OR x = x" |
24465 | 93 |
by (unfold int_or_def) (simp add: bin_rec_PM) |
24333 | 94 |
|
95 |
lemma int_or_Min [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
96 |
"Int.Min OR x = Int.Min" |
24465 | 97 |
by (unfold int_or_def) (simp add: bin_rec_PM) |
24333 | 98 |
|
99 |
lemma int_or_Bits [simp]: |
|
24353 | 100 |
"(x BIT b) OR (y BIT c) = (x OR y) BIT (b OR c)" |
24333 | 101 |
unfolding int_or_def by (simp add: bin_rec_simps) |
102 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
103 |
lemma int_or_Bits2 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
104 |
"(Int.Bit0 x) OR (Int.Bit0 y) = Int.Bit0 (x OR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
105 |
"(Int.Bit0 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
106 |
"(Int.Bit1 x) OR (Int.Bit0 y) = Int.Bit1 (x OR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
107 |
"(Int.Bit1 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
108 |
unfolding BIT_simps [symmetric] int_or_Bits by simp_all |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
109 |
|
24333 | 110 |
lemma int_or_x_simps': |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
111 |
"w OR (Int.Pls BIT bit.B0) = w" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
112 |
"w OR (Int.Min BIT bit.B1) = Int.Min" |
24333 | 113 |
apply (induct w rule: bin_induct) |
114 |
apply simp_all[4] |
|
115 |
apply (unfold int_or_Bits) |
|
116 |
apply clarsimp+ |
|
117 |
done |
|
118 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
119 |
lemma int_or_extra_simps [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
120 |
"w OR Int.Pls = w" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
121 |
"w OR Int.Min = Int.Min" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
122 |
using int_or_x_simps' by simp_all |
24333 | 123 |
|
124 |
lemma int_and_Pls [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
125 |
"Int.Pls AND x = Int.Pls" |
24465 | 126 |
unfolding int_and_def by (simp add: bin_rec_PM) |
24333 | 127 |
|
24353 | 128 |
lemma int_and_Min [simp]: |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
129 |
"Int.Min AND x = x" |
24465 | 130 |
unfolding int_and_def by (simp add: bin_rec_PM) |
24333 | 131 |
|
132 |
lemma int_and_Bits [simp]: |
|
24353 | 133 |
"(x BIT b) AND (y BIT c) = (x AND y) BIT (b AND c)" |
24333 | 134 |
unfolding int_and_def by (simp add: bin_rec_simps) |
135 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
136 |
lemma int_and_Bits2 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
137 |
"(Int.Bit0 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
138 |
"(Int.Bit0 x) AND (Int.Bit1 y) = Int.Bit0 (x AND y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
139 |
"(Int.Bit1 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
140 |
"(Int.Bit1 x) AND (Int.Bit1 y) = Int.Bit1 (x AND y)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
141 |
unfolding BIT_simps [symmetric] int_and_Bits by simp_all |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
142 |
|
24333 | 143 |
lemma int_and_x_simps': |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
144 |
"w AND (Int.Pls BIT bit.B0) = Int.Pls" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
145 |
"w AND (Int.Min BIT bit.B1) = w" |
24333 | 146 |
apply (induct w rule: bin_induct) |
147 |
apply simp_all[4] |
|
148 |
apply (unfold int_and_Bits) |
|
149 |
apply clarsimp+ |
|
150 |
done |
|
151 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
152 |
lemma int_and_extra_simps [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
153 |
"w AND Int.Pls = Int.Pls" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
154 |
"w AND Int.Min = w" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
155 |
using int_and_x_simps' by simp_all |
24333 | 156 |
|
157 |
(* commutativity of the above *) |
|
158 |
lemma bin_ops_comm: |
|
159 |
shows |
|
24353 | 160 |
int_and_comm: "!!y::int. x AND y = y AND x" and |
161 |
int_or_comm: "!!y::int. x OR y = y OR x" and |
|
162 |
int_xor_comm: "!!y::int. x XOR y = y XOR x" |
|
24333 | 163 |
apply (induct x rule: bin_induct) |
164 |
apply simp_all[6] |
|
165 |
apply (case_tac y rule: bin_exhaust, simp add: bit_ops_comm)+ |
|
166 |
done |
|
167 |
||
168 |
lemma bin_ops_same [simp]: |
|
24353 | 169 |
"(x::int) AND x = x" |
170 |
"(x::int) OR x = x" |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
171 |
"(x::int) XOR x = Int.Pls" |
24333 | 172 |
by (induct x rule: bin_induct) auto |
173 |
||
24353 | 174 |
lemma int_not_not [simp]: "NOT (NOT (x::int)) = x" |
24333 | 175 |
by (induct x rule: bin_induct) auto |
176 |
||
177 |
lemmas bin_log_esimps = |
|
178 |
int_and_extra_simps int_or_extra_simps int_xor_extra_simps |
|
179 |
int_and_Pls int_and_Min int_or_Pls int_or_Min int_xor_Pls int_xor_Min |
|
180 |
||
181 |
(* basic properties of logical (bit-wise) operations *) |
|
182 |
||
183 |
lemma bbw_ao_absorb: |
|
24353 | 184 |
"!!y::int. x AND (y OR x) = x & x OR (y AND x) = x" |
24333 | 185 |
apply (induct x rule: bin_induct) |
186 |
apply auto |
|
187 |
apply (case_tac [!] y rule: bin_exhaust) |
|
188 |
apply auto |
|
189 |
apply (case_tac [!] bit) |
|
190 |
apply auto |
|
191 |
done |
|
192 |
||
193 |
lemma bbw_ao_absorbs_other: |
|
24353 | 194 |
"x AND (x OR y) = x \<and> (y AND x) OR x = (x::int)" |
195 |
"(y OR x) AND x = x \<and> x OR (x AND y) = (x::int)" |
|
196 |
"(x OR y) AND x = x \<and> (x AND y) OR x = (x::int)" |
|
24333 | 197 |
apply (auto simp: bbw_ao_absorb int_or_comm) |
198 |
apply (subst int_or_comm) |
|
199 |
apply (simp add: bbw_ao_absorb) |
|
200 |
apply (subst int_and_comm) |
|
201 |
apply (subst int_or_comm) |
|
202 |
apply (simp add: bbw_ao_absorb) |
|
203 |
apply (subst int_and_comm) |
|
204 |
apply (simp add: bbw_ao_absorb) |
|
205 |
done |
|
24353 | 206 |
|
24333 | 207 |
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other |
208 |
||
209 |
lemma int_xor_not: |
|
24353 | 210 |
"!!y::int. (NOT x) XOR y = NOT (x XOR y) & |
211 |
x XOR (NOT y) = NOT (x XOR y)" |
|
24333 | 212 |
apply (induct x rule: bin_induct) |
213 |
apply auto |
|
214 |
apply (case_tac y rule: bin_exhaust, auto, |
|
215 |
case_tac b, auto)+ |
|
216 |
done |
|
217 |
||
218 |
lemma bbw_assocs': |
|
24353 | 219 |
"!!y z::int. (x AND y) AND z = x AND (y AND z) & |
220 |
(x OR y) OR z = x OR (y OR z) & |
|
221 |
(x XOR y) XOR z = x XOR (y XOR z)" |
|
24333 | 222 |
apply (induct x rule: bin_induct) |
223 |
apply (auto simp: int_xor_not) |
|
224 |
apply (case_tac [!] y rule: bin_exhaust) |
|
225 |
apply (case_tac [!] z rule: bin_exhaust) |
|
226 |
apply (case_tac [!] bit) |
|
227 |
apply (case_tac [!] b) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
228 |
apply (auto simp del: BIT_simps) |
24333 | 229 |
done |
230 |
||
231 |
lemma int_and_assoc: |
|
24353 | 232 |
"(x AND y) AND (z::int) = x AND (y AND z)" |
24333 | 233 |
by (simp add: bbw_assocs') |
234 |
||
235 |
lemma int_or_assoc: |
|
24353 | 236 |
"(x OR y) OR (z::int) = x OR (y OR z)" |
24333 | 237 |
by (simp add: bbw_assocs') |
238 |
||
239 |
lemma int_xor_assoc: |
|
24353 | 240 |
"(x XOR y) XOR (z::int) = x XOR (y XOR z)" |
24333 | 241 |
by (simp add: bbw_assocs') |
242 |
||
243 |
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc |
|
244 |
||
245 |
lemma bbw_lcs [simp]: |
|
24353 | 246 |
"(y::int) AND (x AND z) = x AND (y AND z)" |
247 |
"(y::int) OR (x OR z) = x OR (y OR z)" |
|
248 |
"(y::int) XOR (x XOR z) = x XOR (y XOR z)" |
|
24333 | 249 |
apply (auto simp: bbw_assocs [symmetric]) |
250 |
apply (auto simp: bin_ops_comm) |
|
251 |
done |
|
252 |
||
253 |
lemma bbw_not_dist: |
|
24353 | 254 |
"!!y::int. NOT (x OR y) = (NOT x) AND (NOT y)" |
255 |
"!!y::int. NOT (x AND y) = (NOT x) OR (NOT y)" |
|
24333 | 256 |
apply (induct x rule: bin_induct) |
257 |
apply auto |
|
258 |
apply (case_tac [!] y rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
259 |
apply (case_tac [!] bit, auto simp del: BIT_simps) |
24333 | 260 |
done |
261 |
||
262 |
lemma bbw_oa_dist: |
|
24353 | 263 |
"!!y z::int. (x AND y) OR z = |
264 |
(x OR z) AND (y OR z)" |
|
24333 | 265 |
apply (induct x rule: bin_induct) |
266 |
apply auto |
|
267 |
apply (case_tac y rule: bin_exhaust) |
|
268 |
apply (case_tac z rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
269 |
apply (case_tac ba, auto simp del: BIT_simps) |
24333 | 270 |
done |
271 |
||
272 |
lemma bbw_ao_dist: |
|
24353 | 273 |
"!!y z::int. (x OR y) AND z = |
274 |
(x AND z) OR (y AND z)" |
|
24333 | 275 |
apply (induct x rule: bin_induct) |
276 |
apply auto |
|
277 |
apply (case_tac y rule: bin_exhaust) |
|
278 |
apply (case_tac z rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
279 |
apply (case_tac ba, auto simp del: BIT_simps) |
24333 | 280 |
done |
281 |
||
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
282 |
(* |
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
283 |
Why were these declared simp??? |
24333 | 284 |
declare bin_ops_comm [simp] bbw_assocs [simp] |
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
285 |
*) |
24333 | 286 |
|
287 |
lemma plus_and_or [rule_format]: |
|
24353 | 288 |
"ALL y::int. (x AND y) + (x OR y) = x + y" |
24333 | 289 |
apply (induct x rule: bin_induct) |
290 |
apply clarsimp |
|
291 |
apply clarsimp |
|
292 |
apply clarsimp |
|
293 |
apply (case_tac y rule: bin_exhaust) |
|
294 |
apply clarsimp |
|
295 |
apply (unfold Bit_def) |
|
296 |
apply clarsimp |
|
297 |
apply (erule_tac x = "x" in allE) |
|
298 |
apply (simp split: bit.split) |
|
299 |
done |
|
300 |
||
301 |
lemma le_int_or: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
302 |
"!!x. bin_sign y = Int.Pls ==> x <= x OR y" |
24333 | 303 |
apply (induct y rule: bin_induct) |
304 |
apply clarsimp |
|
305 |
apply clarsimp |
|
306 |
apply (case_tac x rule: bin_exhaust) |
|
307 |
apply (case_tac b) |
|
308 |
apply (case_tac [!] bit) |
|
26514 | 309 |
apply (auto simp: less_eq_int_code) |
24333 | 310 |
done |
311 |
||
312 |
lemmas int_and_le = |
|
313 |
xtr3 [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] ; |
|
314 |
||
24364 | 315 |
lemma bin_nth_ops: |
316 |
"!!x y. bin_nth (x AND y) n = (bin_nth x n & bin_nth y n)" |
|
317 |
"!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)" |
|
318 |
"!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" |
|
319 |
"!!x. bin_nth (NOT x) n = (~ bin_nth x n)" |
|
320 |
apply (induct n) |
|
321 |
apply safe |
|
322 |
apply (case_tac [!] x rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
323 |
apply (simp_all del: BIT_simps) |
24364 | 324 |
apply (case_tac [!] y rule: bin_exhaust) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
325 |
apply (simp_all del: BIT_simps) |
24364 | 326 |
apply (auto dest: not_B1_is_B0 intro: B1_ass_B0) |
327 |
done |
|
328 |
||
329 |
(* interaction between bit-wise and arithmetic *) |
|
330 |
(* good example of bin_induction *) |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
331 |
lemma bin_add_not: "x + NOT x = Int.Min" |
24364 | 332 |
apply (induct x rule: bin_induct) |
333 |
apply clarsimp |
|
334 |
apply clarsimp |
|
335 |
apply (case_tac bit, auto) |
|
336 |
done |
|
337 |
||
338 |
(* truncating results of bit-wise operations *) |
|
339 |
lemma bin_trunc_ao: |
|
340 |
"!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" |
|
341 |
"!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)" |
|
342 |
apply (induct n) |
|
343 |
apply auto |
|
344 |
apply (case_tac [!] x rule: bin_exhaust) |
|
345 |
apply (case_tac [!] y rule: bin_exhaust) |
|
346 |
apply auto |
|
347 |
done |
|
348 |
||
349 |
lemma bin_trunc_xor: |
|
350 |
"!!x y. bintrunc n (bintrunc n x XOR bintrunc n y) = |
|
351 |
bintrunc n (x XOR y)" |
|
352 |
apply (induct n) |
|
353 |
apply auto |
|
354 |
apply (case_tac [!] x rule: bin_exhaust) |
|
355 |
apply (case_tac [!] y rule: bin_exhaust) |
|
356 |
apply auto |
|
357 |
done |
|
358 |
||
359 |
lemma bin_trunc_not: |
|
360 |
"!!x. bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)" |
|
361 |
apply (induct n) |
|
362 |
apply auto |
|
363 |
apply (case_tac [!] x rule: bin_exhaust) |
|
364 |
apply auto |
|
365 |
done |
|
366 |
||
367 |
(* want theorems of the form of bin_trunc_xor *) |
|
368 |
lemma bintr_bintr_i: |
|
369 |
"x = bintrunc n y ==> bintrunc n x = bintrunc n y" |
|
370 |
by auto |
|
371 |
||
372 |
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i] |
|
373 |
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i] |
|
374 |
||
375 |
subsection {* Setting and clearing bits *} |
|
376 |
||
377 |
consts |
|
378 |
bin_sc :: "nat => bit => int => int" |
|
379 |
||
380 |
primrec |
|
381 |
Z : "bin_sc 0 b w = bin_rest w BIT b" |
|
382 |
Suc : |
|
383 |
"bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w" |
|
384 |
||
24333 | 385 |
(** nth bit, set/clear **) |
386 |
||
387 |
lemma bin_nth_sc [simp]: |
|
388 |
"!!w. bin_nth (bin_sc n b w) n = (b = bit.B1)" |
|
389 |
by (induct n) auto |
|
390 |
||
391 |
lemma bin_sc_sc_same [simp]: |
|
392 |
"!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w" |
|
393 |
by (induct n) auto |
|
394 |
||
395 |
lemma bin_sc_sc_diff: |
|
396 |
"!!w m. m ~= n ==> |
|
397 |
bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)" |
|
398 |
apply (induct n) |
|
399 |
apply (case_tac [!] m) |
|
400 |
apply auto |
|
401 |
done |
|
402 |
||
403 |
lemma bin_nth_sc_gen: |
|
404 |
"!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = bit.B1 else bin_nth w m)" |
|
405 |
by (induct n) (case_tac [!] m, auto) |
|
406 |
||
407 |
lemma bin_sc_nth [simp]: |
|
408 |
"!!w. (bin_sc n (If (bin_nth w n) bit.B1 bit.B0) w) = w" |
|
24465 | 409 |
by (induct n) auto |
24333 | 410 |
|
411 |
lemma bin_sign_sc [simp]: |
|
412 |
"!!w. bin_sign (bin_sc n b w) = bin_sign w" |
|
413 |
by (induct n) auto |
|
414 |
||
415 |
lemma bin_sc_bintr [simp]: |
|
416 |
"!!w m. bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)" |
|
417 |
apply (induct n) |
|
418 |
apply (case_tac [!] w rule: bin_exhaust) |
|
419 |
apply (case_tac [!] m, auto) |
|
420 |
done |
|
421 |
||
422 |
lemma bin_clr_le: |
|
423 |
"!!w. bin_sc n bit.B0 w <= w" |
|
424 |
apply (induct n) |
|
425 |
apply (case_tac [!] w rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
426 |
apply (auto simp del: BIT_simps) |
24333 | 427 |
apply (unfold Bit_def) |
428 |
apply (simp_all split: bit.split) |
|
429 |
done |
|
430 |
||
431 |
lemma bin_set_ge: |
|
432 |
"!!w. bin_sc n bit.B1 w >= w" |
|
433 |
apply (induct n) |
|
434 |
apply (case_tac [!] w rule: bin_exhaust) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
435 |
apply (auto simp del: BIT_simps) |
24333 | 436 |
apply (unfold Bit_def) |
437 |
apply (simp_all split: bit.split) |
|
438 |
done |
|
439 |
||
440 |
lemma bintr_bin_clr_le: |
|
441 |
"!!w m. bintrunc n (bin_sc m bit.B0 w) <= bintrunc n w" |
|
442 |
apply (induct n) |
|
443 |
apply simp |
|
444 |
apply (case_tac w rule: bin_exhaust) |
|
445 |
apply (case_tac m) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
446 |
apply (auto simp del: BIT_simps) |
24333 | 447 |
apply (unfold Bit_def) |
448 |
apply (simp_all split: bit.split) |
|
449 |
done |
|
450 |
||
451 |
lemma bintr_bin_set_ge: |
|
452 |
"!!w m. bintrunc n (bin_sc m bit.B1 w) >= bintrunc n w" |
|
453 |
apply (induct n) |
|
454 |
apply simp |
|
455 |
apply (case_tac w rule: bin_exhaust) |
|
456 |
apply (case_tac m) |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
457 |
apply (auto simp del: BIT_simps) |
24333 | 458 |
apply (unfold Bit_def) |
459 |
apply (simp_all split: bit.split) |
|
460 |
done |
|
461 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
462 |
lemma bin_sc_FP [simp]: "bin_sc n bit.B0 Int.Pls = Int.Pls" |
24333 | 463 |
by (induct n) auto |
464 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
465 |
lemma bin_sc_TM [simp]: "bin_sc n bit.B1 Int.Min = Int.Min" |
24333 | 466 |
by (induct n) auto |
467 |
||
468 |
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP |
|
469 |
||
470 |
lemma bin_sc_minus: |
|
471 |
"0 < n ==> bin_sc (Suc (n - 1)) b w = bin_sc n b w" |
|
472 |
by auto |
|
473 |
||
474 |
lemmas bin_sc_Suc_minus = |
|
475 |
trans [OF bin_sc_minus [symmetric] bin_sc.Suc, standard] |
|
476 |
||
477 |
lemmas bin_sc_Suc_pred [simp] = |
|
478 |
bin_sc_Suc_minus [of "number_of bin", simplified nobm1, standard] |
|
479 |
||
24465 | 480 |
subsection {* Operations on lists of booleans *} |
481 |
||
482 |
consts |
|
483 |
bin_to_bl :: "nat => int => bool list" |
|
484 |
bin_to_bl_aux :: "nat => int => bool list => bool list" |
|
485 |
bl_to_bin :: "bool list => int" |
|
486 |
bl_to_bin_aux :: "int => bool list => int" |
|
487 |
||
488 |
bl_of_nth :: "nat => (nat => bool) => bool list" |
|
489 |
||
490 |
primrec |
|
491 |
Nil : "bl_to_bin_aux w [] = w" |
|
492 |
Cons : "bl_to_bin_aux w (b # bs) = |
|
493 |
bl_to_bin_aux (w BIT (if b then bit.B1 else bit.B0)) bs" |
|
494 |
||
495 |
primrec |
|
496 |
Z : "bin_to_bl_aux 0 w bl = bl" |
|
497 |
Suc : "bin_to_bl_aux (Suc n) w bl = |
|
498 |
bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)" |
|
499 |
||
500 |
defs |
|
501 |
bin_to_bl_def : "bin_to_bl n w == bin_to_bl_aux n w []" |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
502 |
bl_to_bin_def : "bl_to_bin bs == bl_to_bin_aux Int.Pls bs" |
24465 | 503 |
|
504 |
primrec |
|
505 |
Suc : "bl_of_nth (Suc n) f = f n # bl_of_nth n f" |
|
506 |
Z : "bl_of_nth 0 f = []" |
|
507 |
||
508 |
consts |
|
509 |
takefill :: "'a => nat => 'a list => 'a list" |
|
510 |
app2 :: "('a => 'b => 'c) => 'a list => 'b list => 'c list" |
|
511 |
||
512 |
-- "takefill - like take but if argument list too short," |
|
513 |
-- "extends result to get requested length" |
|
514 |
primrec |
|
515 |
Z : "takefill fill 0 xs = []" |
|
516 |
Suc : "takefill fill (Suc n) xs = ( |
|
517 |
case xs of [] => fill # takefill fill n xs |
|
518 |
| y # ys => y # takefill fill n ys)" |
|
519 |
||
520 |
defs |
|
521 |
app2_def : "app2 f as bs == map (split f) (zip as bs)" |
|
522 |
||
24364 | 523 |
subsection {* Splitting and concatenation *} |
24333 | 524 |
|
24364 | 525 |
-- "rcat and rsplit" |
526 |
consts |
|
527 |
bin_rcat :: "nat => int list => int" |
|
528 |
bin_rsplit_aux :: "nat * int list * nat * int => int list" |
|
529 |
bin_rsplit :: "nat => (nat * int) => int list" |
|
530 |
bin_rsplitl_aux :: "nat * int list * nat * int => int list" |
|
531 |
bin_rsplitl :: "nat => (nat * int) => int list" |
|
532 |
||
533 |
recdef bin_rsplit_aux "measure (fst o snd o snd)" |
|
534 |
"bin_rsplit_aux (n, bs, (m, c)) = |
|
535 |
(if m = 0 | n = 0 then bs else |
|
536 |
let (a, b) = bin_split n c |
|
537 |
in bin_rsplit_aux (n, b # bs, (m - n, a)))" |
|
538 |
||
539 |
recdef bin_rsplitl_aux "measure (fst o snd o snd)" |
|
540 |
"bin_rsplitl_aux (n, bs, (m, c)) = |
|
541 |
(if m = 0 | n = 0 then bs else |
|
542 |
let (a, b) = bin_split (min m n) c |
|
543 |
in bin_rsplitl_aux (n, b # bs, (m - n, a)))" |
|
544 |
||
545 |
defs |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
546 |
bin_rcat_def : "bin_rcat n bs == foldl (%u v. bin_cat u n v) Int.Pls bs" |
24364 | 547 |
bin_rsplit_def : "bin_rsplit n w == bin_rsplit_aux (n, [], w)" |
548 |
bin_rsplitl_def : "bin_rsplitl n w == bin_rsplitl_aux (n, [], w)" |
|
549 |
||
550 |
||
551 |
(* potential for looping *) |
|
552 |
declare bin_rsplit_aux.simps [simp del] |
|
553 |
declare bin_rsplitl_aux.simps [simp del] |
|
554 |
||
555 |
lemma bin_sign_cat: |
|
556 |
"!!y. bin_sign (bin_cat x n y) = bin_sign x" |
|
557 |
by (induct n) auto |
|
558 |
||
559 |
lemma bin_cat_Suc_Bit: |
|
560 |
"bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b" |
|
561 |
by auto |
|
562 |
||
563 |
lemma bin_nth_cat: |
|
564 |
"!!n y. bin_nth (bin_cat x k y) n = |
|
565 |
(if n < k then bin_nth y n else bin_nth x (n - k))" |
|
566 |
apply (induct k) |
|
567 |
apply clarsimp |
|
568 |
apply (case_tac n, auto) |
|
24333 | 569 |
done |
570 |
||
24364 | 571 |
lemma bin_nth_split: |
572 |
"!!b c. bin_split n c = (a, b) ==> |
|
573 |
(ALL k. bin_nth a k = bin_nth c (n + k)) & |
|
574 |
(ALL k. bin_nth b k = (k < n & bin_nth c k))" |
|
24333 | 575 |
apply (induct n) |
24364 | 576 |
apply clarsimp |
577 |
apply (clarsimp simp: Let_def split: ls_splits) |
|
578 |
apply (case_tac k) |
|
579 |
apply auto |
|
580 |
done |
|
581 |
||
582 |
lemma bin_cat_assoc: |
|
583 |
"!!z. bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" |
|
584 |
by (induct n) auto |
|
585 |
||
586 |
lemma bin_cat_assoc_sym: "!!z m. |
|
587 |
bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z" |
|
588 |
apply (induct n, clarsimp) |
|
589 |
apply (case_tac m, auto) |
|
24333 | 590 |
done |
591 |
||
24364 | 592 |
lemma bin_cat_Pls [simp]: |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
593 |
"!!w. bin_cat Int.Pls n w = bintrunc n w" |
24364 | 594 |
by (induct n) auto |
595 |
||
596 |
lemma bintr_cat1: |
|
597 |
"!!b. bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" |
|
598 |
by (induct n) auto |
|
599 |
||
600 |
lemma bintr_cat: "bintrunc m (bin_cat a n b) = |
|
601 |
bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" |
|
602 |
by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) |
|
603 |
||
604 |
lemma bintr_cat_same [simp]: |
|
605 |
"bintrunc n (bin_cat a n b) = bintrunc n b" |
|
606 |
by (auto simp add : bintr_cat) |
|
607 |
||
608 |
lemma cat_bintr [simp]: |
|
609 |
"!!b. bin_cat a n (bintrunc n b) = bin_cat a n b" |
|
610 |
by (induct n) auto |
|
611 |
||
612 |
lemma split_bintrunc: |
|
613 |
"!!b c. bin_split n c = (a, b) ==> b = bintrunc n c" |
|
614 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
615 |
||
616 |
lemma bin_cat_split: |
|
617 |
"!!v w. bin_split n w = (u, v) ==> w = bin_cat u n v" |
|
618 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
619 |
||
620 |
lemma bin_split_cat: |
|
621 |
"!!w. bin_split n (bin_cat v n w) = (v, bintrunc n w)" |
|
622 |
by (induct n) auto |
|
623 |
||
624 |
lemma bin_split_Pls [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
625 |
"bin_split n Int.Pls = (Int.Pls, Int.Pls)" |
24364 | 626 |
by (induct n) (auto simp: Let_def split: ls_splits) |
627 |
||
628 |
lemma bin_split_Min [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25762
diff
changeset
|
629 |
"bin_split n Int.Min = (Int.Min, bintrunc n Int.Min)" |
24364 | 630 |
by (induct n) (auto simp: Let_def split: ls_splits) |
631 |
||
632 |
lemma bin_split_trunc: |
|
633 |
"!!m b c. bin_split (min m n) c = (a, b) ==> |
|
634 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" |
|
635 |
apply (induct n, clarsimp) |
|
636 |
apply (simp add: bin_rest_trunc Let_def split: ls_splits) |
|
637 |
apply (case_tac m) |
|
638 |
apply (auto simp: Let_def split: ls_splits) |
|
24333 | 639 |
done |
640 |
||
24364 | 641 |
lemma bin_split_trunc1: |
642 |
"!!m b c. bin_split n c = (a, b) ==> |
|
643 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" |
|
644 |
apply (induct n, clarsimp) |
|
645 |
apply (simp add: bin_rest_trunc Let_def split: ls_splits) |
|
646 |
apply (case_tac m) |
|
647 |
apply (auto simp: Let_def split: ls_splits) |
|
648 |
done |
|
24333 | 649 |
|
24364 | 650 |
lemma bin_cat_num: |
651 |
"!!b. bin_cat a n b = a * 2 ^ n + bintrunc n b" |
|
652 |
apply (induct n, clarsimp) |
|
653 |
apply (simp add: Bit_def cong: number_of_False_cong) |
|
654 |
done |
|
655 |
||
656 |
lemma bin_split_num: |
|
657 |
"!!b. bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" |
|
658 |
apply (induct n, clarsimp) |
|
659 |
apply (simp add: bin_rest_div zdiv_zmult2_eq) |
|
660 |
apply (case_tac b rule: bin_exhaust) |
|
661 |
apply simp |
|
662 |
apply (simp add: Bit_def zmod_zmult_zmult1 p1mod22k |
|
663 |
split: bit.split |
|
664 |
cong: number_of_False_cong) |
|
665 |
done |
|
666 |
||
667 |
subsection {* Miscellaneous lemmas *} |
|
24333 | 668 |
|
669 |
lemma nth_2p_bin: |
|
670 |
"!!m. bin_nth (2 ^ n) m = (m = n)" |
|
671 |
apply (induct n) |
|
672 |
apply clarsimp |
|
673 |
apply safe |
|
674 |
apply (case_tac m) |
|
675 |
apply (auto simp: trans [OF numeral_1_eq_1 [symmetric] number_of_eq]) |
|
676 |
apply (case_tac m) |
|
677 |
apply (auto simp: Bit_B0_2t [symmetric]) |
|
678 |
done |
|
679 |
||
680 |
(* for use when simplifying with bin_nth_Bit *) |
|
681 |
||
682 |
lemma ex_eq_or: |
|
683 |
"(EX m. n = Suc m & (m = k | P m)) = (n = Suc k | (EX m. n = Suc m & P m))" |
|
684 |
by auto |
|
685 |
||
686 |
end |
|
687 |