82118
|
1 |
(* Title: HOL/ex/Word_Msb.thy
|
|
2 |
Author: Florian Haftmann, TU Muenchen
|
|
3 |
*)
|
|
4 |
|
|
5 |
section \<open>An attempt for msb on word\<close>
|
|
6 |
|
|
7 |
|
|
8 |
theory Word_Msb
|
|
9 |
imports "HOL-Library.Word"
|
75655
|
10 |
begin
|
|
11 |
|
|
12 |
class word = ring_bit_operations +
|
|
13 |
fixes word_length :: \<open>'a itself \<Rightarrow> nat\<close>
|
|
14 |
assumes word_length_positive [simp]: \<open>0 < word_length TYPE('a)\<close>
|
75712
|
15 |
and possible_bit_msb: \<open>possible_bit TYPE('a) (word_length TYPE('a) - Suc 0)\<close>
|
|
16 |
and not_possible_bit_length: \<open>\<not> possible_bit TYPE('a) (word_length TYPE('a))\<close>
|
75655
|
17 |
begin
|
|
18 |
|
|
19 |
lemma word_length_not_0 [simp]:
|
|
20 |
\<open>word_length TYPE('a) \<noteq> 0\<close>
|
|
21 |
using word_length_positive
|
82118
|
22 |
by simp
|
75655
|
23 |
|
75712
|
24 |
lemma possible_bit_iff_less_word_length:
|
|
25 |
\<open>possible_bit TYPE('a) n \<longleftrightarrow> n < word_length TYPE('a)\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
|
|
26 |
proof
|
|
27 |
assume \<open>?P\<close>
|
|
28 |
show ?Q
|
|
29 |
proof (rule ccontr)
|
|
30 |
assume \<open>\<not> n < word_length TYPE('a)\<close>
|
|
31 |
then have \<open>word_length TYPE('a) \<le> n\<close>
|
|
32 |
by simp
|
|
33 |
with \<open>?P\<close> have \<open>possible_bit TYPE('a) (word_length TYPE('a))\<close>
|
|
34 |
by (rule possible_bit_less_imp)
|
|
35 |
with not_possible_bit_length show False ..
|
|
36 |
qed
|
|
37 |
next
|
|
38 |
assume \<open>?Q\<close>
|
|
39 |
then have \<open>n \<le> word_length TYPE('a) - Suc 0\<close>
|
|
40 |
by simp
|
|
41 |
with possible_bit_msb show ?P
|
|
42 |
by (rule possible_bit_less_imp)
|
|
43 |
qed
|
|
44 |
|
75655
|
45 |
end
|
|
46 |
|
|
47 |
instantiation word :: (len) word
|
|
48 |
begin
|
|
49 |
|
|
50 |
definition word_length_word :: \<open>'a word itself \<Rightarrow> nat\<close>
|
|
51 |
where [simp, code_unfold]: \<open>word_length_word _ = LENGTH('a)\<close>
|
|
52 |
|
|
53 |
instance
|
|
54 |
by standard simp_all
|
|
55 |
|
|
56 |
end
|
|
57 |
|
|
58 |
context word
|
|
59 |
begin
|
|
60 |
|
|
61 |
context
|
|
62 |
includes bit_operations_syntax
|
|
63 |
begin
|
|
64 |
|
|
65 |
definition msb :: \<open>'a \<Rightarrow> bool\<close>
|
|
66 |
where \<open>msb w = bit w (word_length TYPE('a) - Suc 0)\<close>
|
|
67 |
|
|
68 |
lemma not_msb_0 [simp]:
|
|
69 |
\<open>\<not> msb 0\<close>
|
|
70 |
by (simp add: msb_def)
|
|
71 |
|
|
72 |
lemma msb_minus_1 [simp]:
|
|
73 |
\<open>msb (- 1)\<close>
|
75712
|
74 |
by (simp add: msb_def possible_bit_iff_less_word_length)
|
75655
|
75 |
|
|
76 |
lemma msb_1_iff [simp]:
|
|
77 |
\<open>msb 1 \<longleftrightarrow> word_length TYPE('a) = 1\<close>
|
|
78 |
by (auto simp add: msb_def bit_simps le_less)
|
|
79 |
|
|
80 |
lemma msb_minus_iff [simp]:
|
|
81 |
\<open>msb (- w) \<longleftrightarrow> \<not> msb (w - 1)\<close>
|
75712
|
82 |
by (simp add: msb_def bit_simps possible_bit_iff_less_word_length)
|
75655
|
83 |
|
|
84 |
lemma msb_not_iff [simp]:
|
|
85 |
\<open>msb (NOT w) \<longleftrightarrow> \<not> msb w\<close>
|
75712
|
86 |
by (simp add: msb_def bit_simps possible_bit_iff_less_word_length)
|
75655
|
87 |
|
|
88 |
lemma msb_and_iff [simp]:
|
|
89 |
\<open>msb (v AND w) \<longleftrightarrow> msb v \<and> msb w\<close>
|
|
90 |
by (simp add: msb_def bit_simps)
|
|
91 |
|
|
92 |
lemma msb_or_iff [simp]:
|
|
93 |
\<open>msb (v OR w) \<longleftrightarrow> msb v \<or> msb w\<close>
|
|
94 |
by (simp add: msb_def bit_simps)
|
|
95 |
|
|
96 |
lemma msb_xor_iff [simp]:
|
|
97 |
\<open>msb (v XOR w) \<longleftrightarrow> \<not> (msb v \<longleftrightarrow> msb w)\<close>
|
|
98 |
by (simp add: msb_def bit_simps)
|
|
99 |
|
82118
|
100 |
lemma msb_exp_iff [simp]:
|
75655
|
101 |
\<open>msb (2 ^ n) \<longleftrightarrow> n = word_length TYPE('a) - Suc 0\<close>
|
82118
|
102 |
by (auto simp add: msb_def bit_simps possible_bit_iff_less_word_length)
|
75655
|
103 |
|
|
104 |
lemma msb_mask_iff [simp]:
|
|
105 |
\<open>msb (mask n) \<longleftrightarrow> word_length TYPE('a) \<le> n\<close>
|
75712
|
106 |
by (simp add: msb_def bit_simps less_diff_conv2 Suc_le_eq less_Suc_eq_le possible_bit_iff_less_word_length)
|
75655
|
107 |
|
|
108 |
lemma msb_set_bit_iff [simp]:
|
|
109 |
\<open>msb (set_bit n w) \<longleftrightarrow> n = word_length TYPE('a) - Suc 0 \<or> msb w\<close>
|
|
110 |
by (simp add: set_bit_eq_or ac_simps)
|
|
111 |
|
|
112 |
lemma msb_unset_bit_iff [simp]:
|
|
113 |
\<open>msb (unset_bit n w) \<longleftrightarrow> n \<noteq> word_length TYPE('a) - Suc 0 \<and> msb w\<close>
|
|
114 |
by (simp add: unset_bit_eq_and_not ac_simps)
|
|
115 |
|
|
116 |
lemma msb_flip_bit_iff [simp]:
|
|
117 |
\<open>msb (flip_bit n w) \<longleftrightarrow> (n \<noteq> word_length TYPE('a) - Suc 0 \<longleftrightarrow> msb w)\<close>
|
|
118 |
by (auto simp add: flip_bit_eq_xor)
|
|
119 |
|
|
120 |
lemma msb_push_bit_iff:
|
|
121 |
\<open>msb (push_bit n w) \<longleftrightarrow> n < word_length TYPE('a) \<and> bit w (word_length TYPE('a) - Suc n)\<close>
|
75712
|
122 |
by (simp add: msb_def bit_simps le_diff_conv2 Suc_le_eq possible_bit_iff_less_word_length)
|
75655
|
123 |
|
75712
|
124 |
lemma msb_drop_bit_iff [simp]:
|
75655
|
125 |
\<open>msb (drop_bit n w) \<longleftrightarrow> n = 0 \<and> msb w\<close>
|
75712
|
126 |
by (cases n)
|
|
127 |
(auto simp add: msb_def bit_simps possible_bit_iff_less_word_length intro!: impossible_bit)
|
75655
|
128 |
|
|
129 |
lemma msb_take_bit_iff [simp]:
|
|
130 |
\<open>msb (take_bit n w) \<longleftrightarrow> word_length TYPE('a) \<le> n \<and> msb w\<close>
|
|
131 |
by (simp add: take_bit_eq_mask ac_simps)
|
|
132 |
|
75712
|
133 |
lemma msb_signed_take_bit_iff:
|
|
134 |
\<open>msb (signed_take_bit n w) \<longleftrightarrow> bit w (min n (word_length TYPE('a) - Suc 0))\<close>
|
|
135 |
by (simp add: msb_def bit_simps possible_bit_iff_less_word_length)
|
75655
|
136 |
|
75713
|
137 |
definition signed_drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
|
|
138 |
where \<open>signed_drop_bit n w = drop_bit n w
|
|
139 |
OR (of_bool (bit w (word_length TYPE('a) - Suc 0)) * NOT (mask (word_length TYPE('a) - Suc n)))\<close>
|
|
140 |
|
|
141 |
lemma msb_signed_drop_bit_iff [simp]:
|
|
142 |
\<open>msb (signed_drop_bit n w) \<longleftrightarrow> msb w\<close>
|
|
143 |
by (simp add: signed_drop_bit_def bit_simps not_le not_less)
|
|
144 |
(simp add: msb_def)
|
|
145 |
|
75655
|
146 |
end
|
|
147 |
|
|
148 |
end
|
|
149 |
|
|
150 |
end
|