| author | wenzelm |
| Mon, 02 Dec 2019 16:15:27 +0100 | |
| changeset 71216 | e64c249d3d98 |
| parent 71095 | 038727567817 |
| child 71420 | 572ab9e64e18 |
| permissions | -rw-r--r-- |
| 67909 | 1 |
(* Author: Florian Haftmann, TUM |
2 |
*) |
|
3 |
||
| 70912 | 4 |
section \<open>Proof(s) of concept for algebraically founded lists of bits\<close> |
| 67909 | 5 |
|
6 |
theory Bit_Lists |
|
| 70926 | 7 |
imports |
| 71095 | 8 |
Word |
| 67909 | 9 |
begin |
10 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
11 |
subsection \<open>Fragments of algebraic bit representations\<close> |
| 70912 | 12 |
|
| 67909 | 13 |
context comm_semiring_1 |
14 |
begin |
|
| 70912 | 15 |
|
16 |
primrec radix_value :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'a"
|
|
17 |
where "radix_value f b [] = 0" |
|
18 |
| "radix_value f b (a # as) = f a + radix_value f b as * b" |
|
19 |
||
20 |
abbreviation (input) unsigned_of_bits :: "bool list \<Rightarrow> 'a" |
|
21 |
where "unsigned_of_bits \<equiv> radix_value of_bool 2" |
|
22 |
||
23 |
lemma unsigned_of_bits_replicate_False [simp]: |
|
24 |
"unsigned_of_bits (replicate n False) = 0" |
|
25 |
by (induction n) simp_all |
|
26 |
||
27 |
end |
|
28 |
||
| 71094 | 29 |
context unique_euclidean_semiring_with_bit_shifts |
| 70912 | 30 |
begin |
| 67909 | 31 |
|
| 70912 | 32 |
lemma unsigned_of_bits_append [simp]: |
33 |
"unsigned_of_bits (bs @ cs) = unsigned_of_bits bs |
|
34 |
+ push_bit (length bs) (unsigned_of_bits cs)" |
|
35 |
by (induction bs) (simp_all add: push_bit_double, |
|
36 |
simp_all add: algebra_simps) |
|
37 |
||
38 |
lemma unsigned_of_bits_take [simp]: |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
39 |
"unsigned_of_bits (take n bs) = Parity.take_bit n (unsigned_of_bits bs)" |
| 70912 | 40 |
proof (induction bs arbitrary: n) |
41 |
case Nil |
|
42 |
then show ?case |
|
43 |
by simp |
|
44 |
next |
|
45 |
case (Cons b bs) |
|
46 |
then show ?case |
|
47 |
by (cases n) (simp_all add: ac_simps) |
|
48 |
qed |
|
49 |
||
50 |
lemma unsigned_of_bits_drop [simp]: |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
51 |
"unsigned_of_bits (drop n bs) = Parity.drop_bit n (unsigned_of_bits bs)" |
| 70912 | 52 |
proof (induction bs arbitrary: n) |
53 |
case Nil |
|
54 |
then show ?case |
|
55 |
by simp |
|
56 |
next |
|
57 |
case (Cons b bs) |
|
58 |
then show ?case |
|
59 |
by (cases n) simp_all |
|
60 |
qed |
|
61 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
62 |
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
63 |
where |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
64 |
"n_bits_of 0 a = []" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
65 |
| "n_bits_of (Suc n) a = odd a # n_bits_of n (a div 2)" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
66 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
67 |
lemma n_bits_of_eq_iff: |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
68 |
"n_bits_of n a = n_bits_of n b \<longleftrightarrow> Parity.take_bit n a = Parity.take_bit n b" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
69 |
apply (induction n arbitrary: a b) |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
70 |
apply (auto elim!: evenE oddE) |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
71 |
apply (metis dvd_triv_right even_plus_one_iff) |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
72 |
apply (metis dvd_triv_right even_plus_one_iff) |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
73 |
done |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
74 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
75 |
lemma take_n_bits_of [simp]: |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
76 |
"take m (n_bits_of n a) = n_bits_of (min m n) a" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
77 |
proof - |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
78 |
define q and v and w where "q = min m n" and "v = m - q" and "w = n - q" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
79 |
then have "v = 0 \<or> w = 0" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
80 |
by auto |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
81 |
then have "take (q + v) (n_bits_of (q + w) a) = n_bits_of q a" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
82 |
by (induction q arbitrary: a) auto |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
83 |
with q_def v_def w_def show ?thesis |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
84 |
by simp |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
85 |
qed |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
86 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
87 |
lemma unsigned_of_bits_n_bits_of [simp]: |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
88 |
"unsigned_of_bits (n_bits_of n a) = Parity.take_bit n a" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
89 |
by (induction n arbitrary: a) (simp_all add: ac_simps) |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
90 |
|
| 70912 | 91 |
end |
92 |
||
93 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
94 |
subsection \<open>Syntactic bit representation\<close> |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
95 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
96 |
class bit_representation = |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
97 |
fixes bits_of :: "'a \<Rightarrow> bool list" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
98 |
and of_bits :: "bool list \<Rightarrow> 'a" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
99 |
assumes of_bits_of [simp]: "of_bits (bits_of a) = a" |
| 70912 | 100 |
|
101 |
text \<open>Unclear whether a \<^typ>\<open>bool\<close> instantiation is needed or not\<close> |
|
102 |
||
103 |
instantiation nat :: bit_representation |
|
104 |
begin |
|
105 |
||
106 |
fun bits_of_nat :: "nat \<Rightarrow> bool list" |
|
107 |
where "bits_of (n::nat) = |
|
108 |
(if n = 0 then [] else odd n # bits_of (n div 2))" |
|
109 |
||
110 |
lemma bits_of_nat_simps [simp]: |
|
111 |
"bits_of (0::nat) = []" |
|
112 |
"n > 0 \<Longrightarrow> bits_of n = odd n # bits_of (n div 2)" for n :: nat |
|
113 |
by simp_all |
|
114 |
||
115 |
declare bits_of_nat.simps [simp del] |
|
116 |
||
117 |
definition of_bits_nat :: "bool list \<Rightarrow> nat" |
|
118 |
where [simp]: "of_bits_nat = unsigned_of_bits" |
|
119 |
\<comment> \<open>remove simp\<close> |
|
120 |
||
121 |
instance proof |
|
122 |
show "of_bits (bits_of n) = n" for n :: nat |
|
123 |
by (induction n rule: nat_bit_induct) simp_all |
|
124 |
qed |
|
| 67909 | 125 |
|
126 |
end |
|
127 |
||
| 70912 | 128 |
lemma bits_of_Suc_0 [simp]: |
129 |
"bits_of (Suc 0) = [True]" |
|
130 |
by simp |
|
131 |
||
132 |
lemma bits_of_1_nat [simp]: |
|
133 |
"bits_of (1 :: nat) = [True]" |
|
134 |
by simp |
|
135 |
||
136 |
lemma bits_of_nat_numeral_simps [simp]: |
|
137 |
"bits_of (numeral Num.One :: nat) = [True]" (is ?One) |
|
138 |
"bits_of (numeral (Num.Bit0 n) :: nat) = False # bits_of (numeral n :: nat)" (is ?Bit0) |
|
139 |
"bits_of (numeral (Num.Bit1 n) :: nat) = True # bits_of (numeral n :: nat)" (is ?Bit1) |
|
140 |
proof - |
|
141 |
show ?One |
|
142 |
by simp |
|
143 |
define m :: nat where "m = numeral n" |
|
144 |
then have "m > 0" and *: "numeral n = m" "numeral (Num.Bit0 n) = 2 * m" "numeral (Num.Bit1 n) = Suc (2 * m)" |
|
145 |
by simp_all |
|
146 |
from \<open>m > 0\<close> show ?Bit0 ?Bit1 |
|
147 |
by (simp_all add: *) |
|
148 |
qed |
|
149 |
||
150 |
lemma unsigned_of_bits_of_nat [simp]: |
|
151 |
"unsigned_of_bits (bits_of n) = n" for n :: nat |
|
152 |
using of_bits_of [of n] by simp |
|
153 |
||
154 |
instantiation int :: bit_representation |
|
| 67909 | 155 |
begin |
156 |
||
| 70912 | 157 |
fun bits_of_int :: "int \<Rightarrow> bool list" |
158 |
where "bits_of_int k = odd k # |
|
159 |
(if k = 0 \<or> k = - 1 then [] else bits_of_int (k div 2))" |
|
160 |
||
161 |
lemma bits_of_int_simps [simp]: |
|
162 |
"bits_of (0 :: int) = [False]" |
|
163 |
"bits_of (- 1 :: int) = [True]" |
|
164 |
"k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> bits_of k = odd k # bits_of (k div 2)" for k :: int |
|
165 |
by simp_all |
|
166 |
||
167 |
lemma bits_of_not_Nil [simp]: |
|
168 |
"bits_of k \<noteq> []" for k :: int |
|
169 |
by simp |
|
170 |
||
171 |
declare bits_of_int.simps [simp del] |
|
172 |
||
173 |
definition of_bits_int :: "bool list \<Rightarrow> int" |
|
174 |
where "of_bits_int bs = (if bs = [] \<or> \<not> last bs then unsigned_of_bits bs |
|
175 |
else unsigned_of_bits bs - 2 ^ length bs)" |
|
176 |
||
177 |
lemma of_bits_int_simps [simp]: |
|
178 |
"of_bits [] = (0 :: int)" |
|
179 |
"of_bits [False] = (0 :: int)" |
|
180 |
"of_bits [True] = (- 1 :: int)" |
|
181 |
"of_bits (bs @ [b]) = (unsigned_of_bits bs :: int) - (2 ^ length bs) * of_bool b" |
|
182 |
"of_bits (False # bs) = 2 * (of_bits bs :: int)" |
|
183 |
"bs \<noteq> [] \<Longrightarrow> of_bits (True # bs) = 1 + 2 * (of_bits bs :: int)" |
|
184 |
by (simp_all add: of_bits_int_def push_bit_of_1) |
|
185 |
||
186 |
instance proof |
|
187 |
show "of_bits (bits_of k) = k" for k :: int |
|
188 |
by (induction k rule: int_bit_induct) simp_all |
|
189 |
qed |
|
190 |
||
191 |
lemma bits_of_1_int [simp]: |
|
192 |
"bits_of (1 :: int) = [True, False]" |
|
193 |
by simp |
|
194 |
||
195 |
lemma bits_of_int_numeral_simps [simp]: |
|
196 |
"bits_of (numeral Num.One :: int) = [True, False]" (is ?One) |
|
197 |
"bits_of (numeral (Num.Bit0 n) :: int) = False # bits_of (numeral n :: int)" (is ?Bit0) |
|
198 |
"bits_of (numeral (Num.Bit1 n) :: int) = True # bits_of (numeral n :: int)" (is ?Bit1) |
|
199 |
"bits_of (- numeral (Num.Bit0 n) :: int) = False # bits_of (- numeral n :: int)" (is ?nBit0) |
|
200 |
"bits_of (- numeral (Num.Bit1 n) :: int) = True # bits_of (- numeral (Num.inc n) :: int)" (is ?nBit1) |
|
201 |
proof - |
|
202 |
show ?One |
|
203 |
by simp |
|
204 |
define k :: int where "k = numeral n" |
|
205 |
then have "k > 0" and *: "numeral n = k" "numeral (Num.Bit0 n) = 2 * k" "numeral (Num.Bit1 n) = 2 * k + 1" |
|
206 |
"numeral (Num.inc n) = k + 1" |
|
207 |
by (simp_all add: add_One) |
|
208 |
have "- (2 * k) div 2 = - k" "(- (2 * k) - 1) div 2 = - k - 1" |
|
209 |
by simp_all |
|
210 |
with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1 |
|
211 |
by (simp_all add: *) |
|
212 |
qed |
|
213 |
||
214 |
lemma of_bits_append [simp]: |
|
215 |
"of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)" |
|
216 |
if "bs \<noteq> []" "\<not> last bs" |
|
217 |
using that proof (induction bs rule: list_nonempty_induct) |
|
218 |
case (single b) |
|
219 |
then show ?case |
|
220 |
by simp |
|
221 |
next |
|
222 |
case (cons b bs) |
|
223 |
then show ?case |
|
224 |
by (cases b) (simp_all add: push_bit_double) |
|
225 |
qed |
|
226 |
||
227 |
lemma of_bits_replicate_False [simp]: |
|
228 |
"of_bits (replicate n False) = (0 :: int)" |
|
229 |
by (auto simp add: of_bits_int_def) |
|
230 |
||
231 |
lemma of_bits_drop [simp]: |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
232 |
"of_bits (drop n bs) = Parity.drop_bit n (of_bits bs :: int)" |
| 70912 | 233 |
if "n < length bs" |
234 |
using that proof (induction bs arbitrary: n) |
|
235 |
case Nil |
|
236 |
then show ?case |
|
237 |
by simp |
|
238 |
next |
|
239 |
case (Cons b bs) |
|
240 |
show ?case |
|
241 |
proof (cases n) |
|
242 |
case 0 |
|
243 |
then show ?thesis |
|
244 |
by simp |
|
245 |
next |
|
246 |
case (Suc n) |
|
247 |
with Cons.prems have "bs \<noteq> []" |
|
248 |
by auto |
|
249 |
with Suc Cons.IH [of n] Cons.prems show ?thesis |
|
250 |
by (cases b) simp_all |
|
251 |
qed |
|
252 |
qed |
|
| 67909 | 253 |
|
254 |
end |
|
255 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
256 |
lemma unsigned_of_bits_eq_of_bits: |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
257 |
"unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
258 |
by (simp add: of_bits_int_def) |
| 70912 | 259 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
260 |
instantiation word :: (len) bit_representation |
| 67909 | 261 |
begin |
262 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
263 |
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
264 |
is "n_bits_of LENGTH('a)"
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
265 |
by (simp add: n_bits_of_eq_iff) |
| 70912 | 266 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
267 |
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
268 |
is unsigned_of_bits . |
| 70912 | 269 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
270 |
instance proof |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
271 |
fix a :: "'a word" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
272 |
show "of_bits (bits_of a) = a" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
273 |
by transfer simp |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
274 |
qed |
| 70912 | 275 |
|
276 |
end |
|
277 |
||
278 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
279 |
subsection \<open>Bit representations with bit operations\<close> |
| 67909 | 280 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
281 |
class semiring_bit_representation = semiring_bit_operations + bit_representation + |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
282 |
assumes and_eq: "length bs = length cs \<Longrightarrow> |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
283 |
of_bits bs AND of_bits cs = of_bits (map2 (\<and>) bs cs)" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
284 |
and or_eq: "length bs = length cs \<Longrightarrow> |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
285 |
of_bits bs OR of_bits cs = of_bits (map2 (\<or>) bs cs)" |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
286 |
and xor_eq: "length bs = length cs \<Longrightarrow> |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
287 |
of_bits bs XOR of_bits cs = of_bits (map2 (\<noteq>) bs cs)" |
| 71094 | 288 |
and push_bit_eq: "push_bit n a = of_bits (replicate n False @ bits_of a)" |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
289 |
and drop_bit_eq: "n < length (bits_of a) \<Longrightarrow> drop_bit n a = of_bits (drop n (bits_of a))" |
| 67909 | 290 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
291 |
class ring_bit_representation = ring_bit_operations + semiring_bit_representation + |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
292 |
assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of" |
| 67909 | 293 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
294 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
295 |
context zip_nat |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
296 |
begin |
| 70912 | 297 |
|
298 |
lemma of_bits: |
|
299 |
"of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)" |
|
300 |
if "length bs = length cs" for bs cs |
|
301 |
using that proof (induction bs cs rule: list_induct2) |
|
302 |
case Nil |
|
303 |
then show ?case |
|
304 |
by simp |
|
305 |
next |
|
306 |
case (Cons b bs c cs) |
|
307 |
then show ?case |
|
308 |
by (cases "of_bits bs = (0::nat) \<or> of_bits cs = (0::nat)") |
|
309 |
(auto simp add: ac_simps end_of_bits rec [of "Suc 0"]) |
|
310 |
qed |
|
| 67909 | 311 |
|
312 |
end |
|
313 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
314 |
instance nat :: semiring_bit_representation |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
315 |
apply standard |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
316 |
apply (simp_all only: and_nat.of_bits or_nat.of_bits xor_nat.of_bits) |
| 71094 | 317 |
apply simp_all |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
318 |
done |
| 70912 | 319 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
320 |
context zip_int |
| 70912 | 321 |
begin |
322 |
||
323 |
lemma of_bits: |
|
324 |
"of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)" |
|
325 |
if "length bs = length cs" and "\<not> False \<^bold>* False" for bs cs |
|
326 |
using \<open>length bs = length cs\<close> proof (induction bs cs rule: list_induct2) |
|
327 |
case Nil |
|
328 |
then show ?case |
|
329 |
using \<open>\<not> False \<^bold>* False\<close> by (auto simp add: False_False_imp_True_True split: bool.splits) |
|
330 |
next |
|
331 |
case (Cons b bs c cs) |
|
332 |
show ?case |
|
333 |
proof (cases "bs = []") |
|
334 |
case True |
|
335 |
with Cons show ?thesis |
|
336 |
using \<open>\<not> False \<^bold>* False\<close> by (cases b; cases c) |
|
337 |
(auto simp add: ac_simps split: bool.splits) |
|
338 |
next |
|
339 |
case False |
|
340 |
with Cons.hyps have "cs \<noteq> []" |
|
341 |
by auto |
|
342 |
with \<open>bs \<noteq> []\<close> have "map2 (\<^bold>*) bs cs \<noteq> []" |
|
343 |
by (simp add: zip_eq_Nil_iff) |
|
344 |
from \<open>bs \<noteq> []\<close> \<open>cs \<noteq> []\<close> \<open>map2 (\<^bold>*) bs cs \<noteq> []\<close> Cons show ?thesis |
|
345 |
by (cases b; cases c) |
|
346 |
(auto simp add: \<open>\<not> False \<^bold>* False\<close> ac_simps |
|
347 |
rec [of "of_bits bs * 2"] rec [of "of_bits cs * 2"] |
|
348 |
rec [of "1 + of_bits bs * 2"]) |
|
349 |
qed |
|
350 |
qed |
|
| 67909 | 351 |
|
352 |
end |
|
353 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
354 |
instance int :: ring_bit_representation |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
70926
diff
changeset
|
355 |
proof |
| 70912 | 356 |
show "(not :: int \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of" |
357 |
proof (rule sym, rule ext) |
|
358 |
fix k :: int |
|
359 |
show "(of_bits \<circ> map Not \<circ> bits_of) k = NOT k" |
|
360 |
by (induction k rule: int_bit_induct) (simp_all add: not_int_def) |
|
361 |
qed |
|
| 71094 | 362 |
show "push_bit n k = of_bits (replicate n False @ bits_of k)" |
| 70912 | 363 |
for k :: int and n :: nat |
| 71094 | 364 |
by (cases "n = 0") simp_all |
365 |
qed (simp_all add: and_int.of_bits or_int.of_bits xor_int.of_bits) |
|
| 67909 | 366 |
|
367 |
end |