author | haftmann |
Tue, 16 Apr 2019 19:50:07 +0000 | |
changeset 70171 | 3173d7878274 |
parent 67961 | 9c31678d2139 |
child 70348 | bde161c740ca |
permissions | -rw-r--r-- |
64015 | 1 |
(* Author: Florian Haftmann, TUM |
2 |
*) |
|
3 |
||
4 |
section \<open>Proof of concept for algebraically founded bit word types\<close> |
|
5 |
||
6 |
theory Word_Type |
|
7 |
imports |
|
8 |
Main |
|
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
64593
diff
changeset
|
9 |
"HOL-Library.Type_Length" |
64015 | 10 |
begin |
11 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
12 |
lemma take_bit_uminus: |
70171 | 13 |
"take_bit n (- (take_bit n k)) = take_bit n (- k)" for k :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
14 |
by (simp add: take_bit_eq_mod mod_minus_eq) |
64015 | 15 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
16 |
lemma take_bit_minus: |
70171 | 17 |
"take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)" for k l :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
18 |
by (simp add: take_bit_eq_mod mod_diff_eq) |
64015 | 19 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
20 |
lemma take_bit_nonnegative [simp]: |
70171 | 21 |
"take_bit n k \<ge> 0" for k :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
22 |
by (simp add: take_bit_eq_mod) |
64015 | 23 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
24 |
definition signed_take_bit :: "nat \<Rightarrow> int \<Rightarrow> int" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
25 |
where signed_take_bit_eq_take_bit: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
26 |
"signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n" |
64015 | 27 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
28 |
lemma signed_take_bit_eq_take_bit': |
70171 | 29 |
"signed_take_bit (n - Suc 0) k = take_bit n (k + 2 ^ (n - 1)) - 2 ^ (n - 1)" if "n > 0" |
30 |
using that by (simp add: signed_take_bit_eq_take_bit) |
|
64015 | 31 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
32 |
lemma signed_take_bit_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
33 |
"signed_take_bit 0 k = - (k mod 2)" |
64015 | 34 |
proof (cases "even k") |
35 |
case True |
|
36 |
then have "odd (k + 1)" |
|
37 |
by simp |
|
38 |
then have "(k + 1) mod 2 = 1" |
|
39 |
by (simp add: even_iff_mod_2_eq_zero) |
|
40 |
with True show ?thesis |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
41 |
by (simp add: signed_take_bit_eq_take_bit) |
64015 | 42 |
next |
43 |
case False |
|
44 |
then show ?thesis |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
45 |
by (simp add: signed_take_bit_eq_take_bit odd_iff_mod_2_eq_one) |
64015 | 46 |
qed |
47 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
48 |
lemma signed_take_bit_Suc [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
49 |
"signed_take_bit (Suc n) k = signed_take_bit n (k div 2) * 2 + k mod 2" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
50 |
by (simp add: odd_iff_mod_2_eq_one signed_take_bit_eq_take_bit algebra_simps) |
64015 | 51 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
52 |
lemma signed_take_bit_of_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
53 |
"signed_take_bit n 0 = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
54 |
by (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod) |
64015 | 55 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
56 |
lemma signed_take_bit_of_minus_1 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
57 |
"signed_take_bit n (- 1) = - 1" |
64015 | 58 |
by (induct n) simp_all |
59 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
60 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
70171 | 61 |
"signed_take_bit (n - Suc 0) k = signed_take_bit (n - Suc 0) l \<longleftrightarrow> take_bit n k = take_bit n l" (is "?P \<longleftrightarrow> ?Q") |
62 |
if "n > 0" |
|
64015 | 63 |
proof - |
70171 | 64 |
from that obtain m where m: "n = Suc m" |
64015 | 65 |
by (cases n) auto |
66 |
show ?thesis |
|
67 |
proof |
|
68 |
assume ?Q |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
69 |
have "take_bit (Suc m) (k + 2 ^ m) = |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
70 |
take_bit (Suc m) (take_bit (Suc m) k + take_bit (Suc m) (2 ^ m))" |
67961 | 71 |
by (simp only: take_bit_add) |
64015 | 72 |
also have "\<dots> = |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
73 |
take_bit (Suc m) (take_bit (Suc m) l + take_bit (Suc m) (2 ^ m))" |
64015 | 74 |
by (simp only: \<open>?Q\<close> m [symmetric]) |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
75 |
also have "\<dots> = take_bit (Suc m) (l + 2 ^ m)" |
67961 | 76 |
by (simp only: take_bit_add) |
64015 | 77 |
finally show ?P |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
78 |
by (simp only: signed_take_bit_eq_take_bit m) simp |
64015 | 79 |
next |
80 |
assume ?P |
|
70171 | 81 |
with that have "(k + 2 ^ (n - Suc 0)) mod 2 ^ n = (l + 2 ^ (n - Suc 0)) mod 2 ^ n" |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
82 |
by (simp add: signed_take_bit_eq_take_bit' take_bit_eq_mod) |
64015 | 83 |
then have "(i + (k + 2 ^ (n - Suc 0))) mod 2 ^ n = (i + (l + 2 ^ (n - Suc 0))) mod 2 ^ n" for i |
84 |
by (metis mod_add_eq) |
|
85 |
then have "k mod 2 ^ n = l mod 2 ^ n" |
|
86 |
by (metis add_diff_cancel_right' uminus_add_conv_diff) |
|
87 |
then show ?Q |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
88 |
by (simp add: take_bit_eq_mod) |
64015 | 89 |
qed |
90 |
qed |
|
91 |
||
92 |
||
93 |
subsection \<open>Bit strings as quotient type\<close> |
|
94 |
||
95 |
subsubsection \<open>Basic properties\<close> |
|
96 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
97 |
quotient_type (overloaded) 'a word = int / "\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len0) l" |
64015 | 98 |
by (auto intro!: equivpI reflpI sympI transpI) |
99 |
||
100 |
instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}" |
|
101 |
begin |
|
102 |
||
103 |
lift_definition zero_word :: "'a word" |
|
104 |
is 0 |
|
105 |
. |
|
106 |
||
107 |
lift_definition one_word :: "'a word" |
|
108 |
is 1 |
|
109 |
. |
|
110 |
||
111 |
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
112 |
is plus |
|
67961 | 113 |
by (subst take_bit_add [symmetric]) (simp add: take_bit_add) |
64015 | 114 |
|
115 |
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" |
|
116 |
is uminus |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
117 |
by (subst take_bit_uminus [symmetric]) (simp add: take_bit_uminus) |
64015 | 118 |
|
119 |
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
120 |
is minus |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
121 |
by (subst take_bit_minus [symmetric]) (simp add: take_bit_minus) |
64015 | 122 |
|
123 |
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
124 |
is times |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
125 |
by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) |
64015 | 126 |
|
127 |
instance |
|
128 |
by standard (transfer; simp add: algebra_simps)+ |
|
129 |
||
130 |
end |
|
131 |
||
132 |
instance word :: (len) comm_ring_1 |
|
133 |
by standard (transfer; simp)+ |
|
134 |
||
135 |
||
136 |
subsubsection \<open>Conversions\<close> |
|
137 |
||
138 |
lemma [transfer_rule]: |
|
139 |
"rel_fun HOL.eq pcr_word int of_nat" |
|
140 |
proof - |
|
141 |
note transfer_rule_of_nat [transfer_rule] |
|
142 |
show ?thesis by transfer_prover |
|
143 |
qed |
|
144 |
||
145 |
lemma [transfer_rule]: |
|
146 |
"rel_fun HOL.eq pcr_word (\<lambda>k. k) of_int" |
|
147 |
proof - |
|
148 |
note transfer_rule_of_int [transfer_rule] |
|
149 |
have "rel_fun HOL.eq pcr_word (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> 'a word)" |
|
150 |
by transfer_prover |
|
151 |
then show ?thesis by (simp add: id_def) |
|
152 |
qed |
|
153 |
||
154 |
context semiring_1 |
|
155 |
begin |
|
156 |
||
157 |
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
158 |
is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)" |
64015 | 159 |
by simp |
160 |
||
161 |
lemma unsigned_0 [simp]: |
|
162 |
"unsigned 0 = 0" |
|
163 |
by transfer simp |
|
164 |
||
165 |
end |
|
166 |
||
167 |
context semiring_char_0 |
|
168 |
begin |
|
169 |
||
170 |
lemma word_eq_iff_unsigned: |
|
171 |
"a = b \<longleftrightarrow> unsigned a = unsigned b" |
|
172 |
by safe (transfer; simp add: eq_nat_nat_iff) |
|
173 |
||
174 |
end |
|
175 |
||
176 |
context ring_1 |
|
177 |
begin |
|
178 |
||
179 |
lift_definition signed :: "'b::len word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
180 |
is "of_int \<circ> signed_take_bit (LENGTH('b) - 1)" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
181 |
by (simp add: signed_take_bit_eq_iff_take_bit_eq [symmetric]) |
64015 | 182 |
|
183 |
lemma signed_0 [simp]: |
|
184 |
"signed 0 = 0" |
|
185 |
by transfer simp |
|
186 |
||
187 |
end |
|
188 |
||
189 |
lemma unsigned_of_nat [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
190 |
"unsigned (of_nat n :: 'a word) = take_bit LENGTH('a::len) n" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
191 |
by transfer (simp add: nat_eq_iff take_bit_eq_mod zmod_int) |
64015 | 192 |
|
193 |
lemma of_nat_unsigned [simp]: |
|
194 |
"of_nat (unsigned a) = a" |
|
195 |
by transfer simp |
|
196 |
||
197 |
lemma of_int_unsigned [simp]: |
|
198 |
"of_int (unsigned a) = a" |
|
199 |
by transfer simp |
|
200 |
||
201 |
context ring_char_0 |
|
202 |
begin |
|
203 |
||
204 |
lemma word_eq_iff_signed: |
|
205 |
"a = b \<longleftrightarrow> signed a = signed b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
206 |
by safe (transfer; auto simp add: signed_take_bit_eq_iff_take_bit_eq) |
64015 | 207 |
|
208 |
end |
|
209 |
||
210 |
lemma signed_of_int [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
211 |
"signed (of_int k :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) k" |
64015 | 212 |
by transfer simp |
213 |
||
214 |
lemma of_int_signed [simp]: |
|
215 |
"of_int (signed a) = a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
216 |
by transfer (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod mod_simps) |
64015 | 217 |
|
218 |
||
219 |
subsubsection \<open>Properties\<close> |
|
220 |
||
221 |
||
222 |
subsubsection \<open>Division\<close> |
|
223 |
||
224 |
instantiation word :: (len0) modulo |
|
225 |
begin |
|
226 |
||
227 |
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
228 |
is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b" |
64015 | 229 |
by simp |
230 |
||
231 |
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
232 |
is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b" |
64015 | 233 |
by simp |
234 |
||
235 |
instance .. |
|
236 |
||
237 |
end |
|
238 |
||
239 |
||
240 |
subsubsection \<open>Orderings\<close> |
|
241 |
||
242 |
instantiation word :: (len0) linorder |
|
243 |
begin |
|
244 |
||
245 |
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
246 |
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" |
64015 | 247 |
by simp |
248 |
||
249 |
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
250 |
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" |
64015 | 251 |
by simp |
252 |
||
253 |
instance |
|
254 |
by standard (transfer; auto)+ |
|
255 |
||
256 |
end |
|
257 |
||
258 |
context linordered_semidom |
|
259 |
begin |
|
260 |
||
261 |
lemma word_less_eq_iff_unsigned: |
|
262 |
"a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" |
|
263 |
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) |
|
264 |
||
265 |
lemma word_less_iff_unsigned: |
|
266 |
"a < b \<longleftrightarrow> unsigned a < unsigned b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
267 |
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) |
64015 | 268 |
|
269 |
end |
|
270 |
||
271 |
end |