3 |
3 |
4 section \<open>Proof(s) of concept for algebraically founded lists of bits\<close> |
4 section \<open>Proof(s) of concept for algebraically founded lists of bits\<close> |
5 |
5 |
6 theory Bit_Lists |
6 theory Bit_Lists |
7 imports |
7 imports |
8 Word |
8 Word "HOL-Library.More_List" |
9 begin |
9 begin |
|
10 |
|
11 lemma hd_zip: |
|
12 \<open>hd (zip xs ys) = (hd xs, hd ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> |
|
13 using that by (cases xs; cases ys) simp_all |
|
14 |
|
15 lemma last_zip: |
|
16 \<open>last (zip xs ys) = (last xs, last ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> |
|
17 and \<open>length xs = length ys\<close> |
|
18 using that by (cases xs rule: rev_cases; cases ys rule: rev_cases) simp_all |
|
19 |
10 |
20 |
11 subsection \<open>Fragments of algebraic bit representations\<close> |
21 subsection \<open>Fragments of algebraic bit representations\<close> |
12 |
22 |
13 context comm_semiring_1 |
23 context comm_semiring_1 |
14 begin |
24 begin |
34 + push_bit (length bs) (unsigned_of_bits cs)" |
44 + push_bit (length bs) (unsigned_of_bits cs)" |
35 by (induction bs) (simp_all add: push_bit_double, |
45 by (induction bs) (simp_all add: push_bit_double, |
36 simp_all add: algebra_simps) |
46 simp_all add: algebra_simps) |
37 |
47 |
38 lemma unsigned_of_bits_take [simp]: |
48 lemma unsigned_of_bits_take [simp]: |
39 "unsigned_of_bits (take n bs) = Parity.take_bit n (unsigned_of_bits bs)" |
49 "unsigned_of_bits (take n bs) = take_bit n (unsigned_of_bits bs)" |
40 proof (induction bs arbitrary: n) |
50 proof (induction bs arbitrary: n) |
41 case Nil |
51 case Nil |
42 then show ?case |
52 then show ?case |
43 by simp |
53 by simp |
44 next |
54 next |
47 by (cases n) (simp_all add: ac_simps) |
57 by (cases n) (simp_all add: ac_simps) |
48 qed |
58 qed |
49 |
59 |
50 lemma unsigned_of_bits_drop [simp]: |
60 lemma unsigned_of_bits_drop [simp]: |
51 "unsigned_of_bits (drop n bs) = Parity.drop_bit n (unsigned_of_bits bs)" |
61 "unsigned_of_bits (drop n bs) = Parity.drop_bit n (unsigned_of_bits bs)" |
|
62 proof (induction bs arbitrary: n) |
|
63 case Nil |
|
64 then show ?case |
|
65 by simp |
|
66 next |
|
67 case (Cons b bs) |
|
68 then show ?case |
|
69 by (cases n) simp_all |
|
70 qed |
|
71 |
|
72 lemma bit_unsigned_of_bits_iff: |
|
73 \<open>bit (unsigned_of_bits bs) n \<longleftrightarrow> nth_default False bs n\<close> |
52 proof (induction bs arbitrary: n) |
74 proof (induction bs arbitrary: n) |
53 case Nil |
75 case Nil |
54 then show ?case |
76 then show ?case |
55 by simp |
77 by simp |
56 next |
78 next |
123 by (induction n rule: nat_bit_induct) simp_all |
145 by (induction n rule: nat_bit_induct) simp_all |
124 qed |
146 qed |
125 |
147 |
126 end |
148 end |
127 |
149 |
|
150 lemma bit_of_bits_nat_iff: |
|
151 \<open>bit (of_bits bs :: nat) n \<longleftrightarrow> nth_default False bs n\<close> |
|
152 by (simp add: bit_unsigned_of_bits_iff) |
|
153 |
128 lemma bits_of_Suc_0 [simp]: |
154 lemma bits_of_Suc_0 [simp]: |
129 "bits_of (Suc 0) = [True]" |
155 "bits_of (Suc 0) = [True]" |
130 by simp |
156 by simp |
131 |
157 |
132 lemma bits_of_1_nat [simp]: |
158 lemma bits_of_1_nat [simp]: |
207 by (simp_all add: add_One) |
233 by (simp_all add: add_One) |
208 have "- (2 * k) div 2 = - k" "(- (2 * k) - 1) div 2 = - k - 1" |
234 have "- (2 * k) div 2 = - k" "(- (2 * k) - 1) div 2 = - k - 1" |
209 by simp_all |
235 by simp_all |
210 with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1 |
236 with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1 |
211 by (simp_all add: *) |
237 by (simp_all add: *) |
|
238 qed |
|
239 |
|
240 lemma bit_of_bits_int_iff: |
|
241 \<open>bit (of_bits bs :: int) n \<longleftrightarrow> nth_default (bs \<noteq> [] \<and> last bs) bs n\<close> |
|
242 proof (induction bs arbitrary: n) |
|
243 case Nil |
|
244 then show ?case |
|
245 by simp |
|
246 next |
|
247 case (Cons b bs) |
|
248 then show ?case |
|
249 by (cases n; cases b; cases bs) simp_all |
212 qed |
250 qed |
213 |
251 |
214 lemma of_bits_append [simp]: |
252 lemma of_bits_append [simp]: |
215 "of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)" |
253 "of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)" |
216 if "bs \<noteq> []" "\<not> last bs" |
254 if "bs \<noteq> []" "\<not> last bs" |
289 and drop_bit_eq: "n < length (bits_of a) \<Longrightarrow> drop_bit n a = of_bits (drop n (bits_of a))" |
327 and drop_bit_eq: "n < length (bits_of a) \<Longrightarrow> drop_bit n a = of_bits (drop n (bits_of a))" |
290 |
328 |
291 class ring_bit_representation = ring_bit_operations + semiring_bit_representation + |
329 class ring_bit_representation = ring_bit_operations + semiring_bit_representation + |
292 assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of" |
330 assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of" |
293 |
331 |
294 |
|
295 context zip_nat |
332 context zip_nat |
296 begin |
333 begin |
297 |
334 |
298 lemma of_bits: |
335 lemma of_bits: |
299 "of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)" |
336 "of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)" |
300 if "length bs = length cs" for bs cs |
337 if "length bs = length cs" for bs cs |
301 using that proof (induction bs cs rule: list_induct2) |
338 by (simp add: Parity.bit_eq_iff bit_unsigned_of_bits_iff bit_eq_iff) |
302 case Nil |
339 (simp add: that end_of_bits nth_default_map2 [of _ _ _ False False]) |
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 |
|
311 |
|
312 end |
340 end |
313 |
341 |
314 instance nat :: semiring_bit_representation |
342 instance nat :: semiring_bit_representation |
315 apply standard |
343 apply standard |
316 apply (simp_all only: and_nat.of_bits or_nat.of_bits xor_nat.of_bits) |
344 apply (simp_all only: and_nat.of_bits or_nat.of_bits xor_nat.of_bits) |
319 |
347 |
320 context zip_int |
348 context zip_int |
321 begin |
349 begin |
322 |
350 |
323 lemma of_bits: |
351 lemma of_bits: |
324 "of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)" |
352 \<open>of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)\<close> |
325 if "length bs = length cs" and "\<not> False \<^bold>* False" for bs cs |
353 if \<open>length bs = length cs\<close> and \<open>\<not> False \<^bold>* False\<close> for bs cs |
326 using \<open>length bs = length cs\<close> proof (induction bs cs rule: list_induct2) |
354 proof (cases \<open>bs = []\<close>) |
327 case Nil |
355 case True |
328 then show ?case |
356 moreover have \<open>cs = []\<close> |
329 using \<open>\<not> False \<^bold>* False\<close> by (auto simp add: False_False_imp_True_True split: bool.splits) |
357 using True that by simp |
330 next |
358 ultimately show ?thesis |
331 case (Cons b bs c cs) |
359 by (simp add: Parity.bit_eq_iff bit_eq_iff that) |
332 show ?case |
360 next |
333 proof (cases "bs = []") |
361 case False |
334 case True |
362 moreover have \<open>cs \<noteq> []\<close> |
335 with Cons show ?thesis |
363 using False that by auto |
336 using \<open>\<not> False \<^bold>* False\<close> by (cases b; cases c) |
364 ultimately show ?thesis |
337 (auto simp add: ac_simps split: bool.splits) |
365 apply (simp add: Parity.bit_eq_iff bit_of_bits_int_iff bit_eq_iff zip_eq_Nil_iff last_map last_zip that) |
338 next |
366 apply (simp add: that nth_default_map2 [of _ _ _ \<open>last bs\<close> \<open>last cs\<close>]) |
339 case False |
367 done |
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 |
368 qed |
351 |
369 |
352 end |
370 end |
353 |
371 |
354 instance int :: ring_bit_representation |
372 instance int :: ring_bit_representation |