24333
|
1 |
(*
|
|
2 |
ID: $Id$
|
|
3 |
Author: Jeremy Dawson and Gerwin Klein, NICTA
|
|
4 |
|
|
5 |
contains arithmetic theorems for word, instantiations to
|
|
6 |
arithmetic type classes and tactics for reducing word arithmetic
|
|
7 |
to linear arithmetic on int or nat
|
|
8 |
*)
|
|
9 |
|
|
10 |
theory WordArith imports WordDefinition begin
|
|
11 |
|
|
12 |
|
|
13 |
lemma word_less_alt: "(a < b) = (uint a < uint b)"
|
|
14 |
unfolding word_less_def word_le_def
|
|
15 |
by (auto simp del: word_uint.Rep_inject
|
|
16 |
simp: word_uint.Rep_inject [symmetric])
|
|
17 |
|
|
18 |
lemma signed_linorder: "linorder word_sle word_sless"
|
|
19 |
apply unfold_locales
|
|
20 |
apply (unfold word_sle_def word_sless_def)
|
|
21 |
by auto
|
|
22 |
|
|
23 |
interpretation signed: linorder ["word_sle" "word_sless"]
|
|
24 |
by (rule signed_linorder)
|
|
25 |
|
|
26 |
lemmas word_arith_wis [THEN meta_eq_to_obj_eq] =
|
|
27 |
word_add_def word_mult_def word_minus_def
|
|
28 |
word_succ_def word_pred_def word_0_wi word_1_wi
|
|
29 |
|
|
30 |
lemma udvdI:
|
|
31 |
"0 \<le> n ==> uint b = n * uint a ==> a udvd b"
|
|
32 |
by (auto simp: udvd_def)
|
|
33 |
|
|
34 |
lemmas word_div_no [simp] =
|
|
35 |
word_div_def [of "number_of ?a" "number_of ?b"]
|
|
36 |
|
|
37 |
lemmas word_mod_no [simp] =
|
|
38 |
word_mod_def [of "number_of ?a" "number_of ?b"]
|
|
39 |
|
|
40 |
lemmas word_less_no [simp] =
|
|
41 |
word_less_def [of "number_of ?a" "number_of ?b"]
|
|
42 |
|
|
43 |
lemmas word_le_no [simp] =
|
|
44 |
word_le_def [of "number_of ?a" "number_of ?b"]
|
|
45 |
|
|
46 |
lemmas word_sless_no [simp] =
|
|
47 |
word_sless_def [of "number_of ?a" "number_of ?b"]
|
|
48 |
|
|
49 |
lemmas word_sle_no [simp] =
|
|
50 |
word_sle_def [of "number_of ?a" "number_of ?b"]
|
|
51 |
|
|
52 |
(* following two are available in class number_ring,
|
|
53 |
but convenient to have them here here;
|
|
54 |
note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
|
|
55 |
are in the default simpset, so to use the automatic simplifications for
|
|
56 |
(eg) sint (number_of bin) on sint 1, must do
|
|
57 |
(simp add: word_1_no del: numeral_1_eq_1)
|
|
58 |
*)
|
|
59 |
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def]
|
|
60 |
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi]
|
|
61 |
|
|
62 |
lemma int_one_bin: "(1 :: int) == (Numeral.Pls BIT bit.B1)"
|
|
63 |
unfolding Pls_def Bit_def by auto
|
|
64 |
|
|
65 |
lemma word_1_no:
|
|
66 |
"(1 :: 'a :: len0 word) == number_of (Numeral.Pls BIT bit.B1)"
|
|
67 |
unfolding word_1_wi word_number_of_def int_one_bin by auto
|
|
68 |
|
|
69 |
lemma word_m1_wi: "-1 == word_of_int -1"
|
|
70 |
by (rule word_number_of_alt)
|
|
71 |
|
|
72 |
lemma word_m1_wi_Min: "-1 = word_of_int Numeral.Min"
|
|
73 |
by (simp add: word_m1_wi number_of_eq)
|
|
74 |
|
|
75 |
lemma word_0_bl: "of_bl [] = 0"
|
|
76 |
unfolding word_0_wi of_bl_def by (simp add : Pls_def)
|
|
77 |
|
|
78 |
lemma word_1_bl: "of_bl [True] = 1"
|
|
79 |
unfolding word_1_wi of_bl_def
|
|
80 |
by (simp add : bl_to_bin_def Bit_def Pls_def)
|
|
81 |
|
|
82 |
lemma uint_0 [simp] : "(uint 0 = 0)"
|
|
83 |
unfolding word_0_wi
|
|
84 |
by (simp add: word_ubin.eq_norm Pls_def [symmetric])
|
|
85 |
|
|
86 |
lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0"
|
|
87 |
by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def)
|
|
88 |
|
|
89 |
lemma to_bl_0:
|
|
90 |
"to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
|
|
91 |
unfolding uint_bl
|
|
92 |
by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric])
|
|
93 |
|
|
94 |
lemma uint_0_iff: "(uint x = 0) = (x = 0)"
|
|
95 |
by (auto intro!: word_uint.Rep_eqD)
|
|
96 |
|
|
97 |
lemma unat_0_iff: "(unat x = 0) = (x = 0)"
|
|
98 |
unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
|
|
99 |
|
|
100 |
lemma unat_0 [simp]: "unat 0 = 0"
|
|
101 |
unfolding unat_def by auto
|
|
102 |
|
|
103 |
lemma size_0_same': "size w = 0 ==> w = (v :: 'a :: len0 word)"
|
|
104 |
apply (unfold word_size)
|
|
105 |
apply (rule box_equals)
|
|
106 |
defer
|
|
107 |
apply (rule word_uint.Rep_inverse)+
|
|
108 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1])
|
|
109 |
apply simp
|
|
110 |
done
|
|
111 |
|
|
112 |
lemmas size_0_same = size_0_same' [folded word_size]
|
|
113 |
|
|
114 |
lemmas unat_eq_0 = unat_0_iff
|
|
115 |
lemmas unat_eq_zero = unat_0_iff
|
|
116 |
|
|
117 |
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
|
|
118 |
by (simp add : unat_0_iff [symmetric])
|
|
119 |
|
|
120 |
lemma ucast_0 [simp] : "ucast 0 = 0"
|
|
121 |
unfolding ucast_def
|
|
122 |
by simp (simp add: word_0_wi)
|
|
123 |
|
|
124 |
lemma sint_0 [simp] : "sint 0 = 0"
|
|
125 |
unfolding sint_uint
|
|
126 |
by (simp add: Pls_def [symmetric])
|
|
127 |
|
|
128 |
lemma scast_0 [simp] : "scast 0 = 0"
|
|
129 |
apply (unfold scast_def)
|
|
130 |
apply simp
|
|
131 |
apply (simp add: word_0_wi)
|
|
132 |
done
|
|
133 |
|
|
134 |
lemma sint_n1 [simp] : "sint -1 = -1"
|
|
135 |
apply (unfold word_m1_wi_Min)
|
|
136 |
apply (simp add: word_sbin.eq_norm)
|
|
137 |
apply (unfold Min_def number_of_eq)
|
|
138 |
apply simp
|
|
139 |
done
|
|
140 |
|
|
141 |
lemma scast_n1 [simp] : "scast -1 = -1"
|
|
142 |
apply (unfold scast_def sint_n1)
|
|
143 |
apply (unfold word_number_of_alt)
|
|
144 |
apply (rule refl)
|
|
145 |
done
|
|
146 |
|
|
147 |
lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1"
|
|
148 |
unfolding word_1_wi
|
|
149 |
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
|
|
150 |
|
|
151 |
lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1"
|
|
152 |
by (unfold unat_def uint_1) auto
|
|
153 |
|
|
154 |
lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
|
|
155 |
unfolding ucast_def word_1_wi
|
|
156 |
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
|
|
157 |
|
|
158 |
(* abstraction preserves the operations
|
|
159 |
(the definitions tell this for bins in range uint) *)
|
|
160 |
|
|
161 |
lemmas arths =
|
|
162 |
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
|
|
163 |
folded word_ubin.eq_norm, standard]
|
|
164 |
|
|
165 |
lemma wi_homs:
|
|
166 |
shows
|
|
167 |
wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
|
|
168 |
wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
|
|
169 |
wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
|
|
170 |
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Numeral.succ a)" and
|
|
171 |
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Numeral.pred a)"
|
|
172 |
by (auto simp: word_arith_wis arths)
|
|
173 |
|
|
174 |
lemmas wi_hom_syms = wi_homs [symmetric]
|
|
175 |
|
|
176 |
lemma word_sub_def: "a - b == a + - (b :: 'a :: len0 word)"
|
|
177 |
unfolding word_sub_wi diff_def
|
|
178 |
by (simp only : word_uint.Rep_inverse wi_hom_syms)
|
|
179 |
|
|
180 |
lemmas word_diff_minus = word_sub_def [THEN meta_eq_to_obj_eq, standard]
|
|
181 |
|
|
182 |
lemma word_of_int_sub_hom:
|
|
183 |
"(word_of_int a) - word_of_int b = word_of_int (a - b)"
|
|
184 |
unfolding word_sub_def diff_def by (simp only : wi_homs)
|
|
185 |
|
|
186 |
lemmas new_word_of_int_homs =
|
|
187 |
word_of_int_sub_hom wi_homs word_0_wi word_1_wi
|
|
188 |
|
|
189 |
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
|
|
190 |
|
|
191 |
lemmas word_of_int_hom_syms =
|
|
192 |
new_word_of_int_hom_syms [unfolded succ_def pred_def]
|
|
193 |
|
|
194 |
lemmas word_of_int_homs =
|
|
195 |
new_word_of_int_homs [unfolded succ_def pred_def]
|
|
196 |
|
|
197 |
lemmas word_of_int_add_hom = word_of_int_homs (2)
|
|
198 |
lemmas word_of_int_mult_hom = word_of_int_homs (3)
|
|
199 |
lemmas word_of_int_minus_hom = word_of_int_homs (4)
|
|
200 |
lemmas word_of_int_succ_hom = word_of_int_homs (5)
|
|
201 |
lemmas word_of_int_pred_hom = word_of_int_homs (6)
|
|
202 |
lemmas word_of_int_0_hom = word_of_int_homs (7)
|
|
203 |
lemmas word_of_int_1_hom = word_of_int_homs (8)
|
|
204 |
|
|
205 |
(* now, to get the weaker results analogous to word_div/mod_def *)
|
|
206 |
|
|
207 |
lemmas word_arith_alts =
|
|
208 |
word_sub_wi [unfolded succ_def pred_def, THEN meta_eq_to_obj_eq, standard]
|
|
209 |
word_arith_wis [unfolded succ_def pred_def, standard]
|
|
210 |
|
|
211 |
lemmas word_sub_alt = word_arith_alts (1)
|
|
212 |
lemmas word_add_alt = word_arith_alts (2)
|
|
213 |
lemmas word_mult_alt = word_arith_alts (3)
|
|
214 |
lemmas word_minus_alt = word_arith_alts (4)
|
|
215 |
lemmas word_succ_alt = word_arith_alts (5)
|
|
216 |
lemmas word_pred_alt = word_arith_alts (6)
|
|
217 |
lemmas word_0_alt = word_arith_alts (7)
|
|
218 |
lemmas word_1_alt = word_arith_alts (8)
|
|
219 |
|
|
220 |
section "Transferring goals from words to ints"
|
|
221 |
|
|
222 |
lemma word_ths:
|
|
223 |
shows
|
|
224 |
word_succ_p1: "word_succ a = a + 1" and
|
|
225 |
word_pred_m1: "word_pred a = a - 1" and
|
|
226 |
word_pred_succ: "word_pred (word_succ a) = a" and
|
|
227 |
word_succ_pred: "word_succ (word_pred a) = a" and
|
|
228 |
word_mult_succ: "word_succ a * b = b + a * b"
|
|
229 |
by (rule word_uint.Abs_cases [of b],
|
|
230 |
rule word_uint.Abs_cases [of a],
|
|
231 |
simp add: pred_def succ_def add_commute mult_commute
|
|
232 |
ring_distribs new_word_of_int_homs)+
|
|
233 |
|
|
234 |
lemmas uint_cong = arg_cong [where f = uint]
|
|
235 |
|
|
236 |
lemmas uint_word_ariths =
|
|
237 |
word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard]
|
|
238 |
|
|
239 |
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
|
|
240 |
|
|
241 |
(* similar expressions for sint (arith operations) *)
|
|
242 |
lemmas sint_word_ariths = uint_word_arith_bintrs
|
|
243 |
[THEN uint_sint [symmetric, THEN trans],
|
|
244 |
unfolded uint_sint bintr_arith1s bintr_ariths
|
|
245 |
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard]
|
|
246 |
|
|
247 |
lemmas uint_div_alt = word_div_def
|
|
248 |
[THEN meta_eq_to_obj_eq [THEN trans [OF uint_cong int_word_uint]], standard]
|
|
249 |
lemmas uint_mod_alt = word_mod_def
|
|
250 |
[THEN meta_eq_to_obj_eq [THEN trans [OF uint_cong int_word_uint]], standard]
|
|
251 |
|
|
252 |
lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
|
|
253 |
unfolding word_pred_def number_of_eq
|
|
254 |
by (simp add : pred_def word_no_wi)
|
|
255 |
|
|
256 |
lemma word_pred_0_Min: "word_pred 0 = word_of_int Numeral.Min"
|
|
257 |
by (simp add: word_pred_0_n1 number_of_eq)
|
|
258 |
|
|
259 |
lemma word_m1_Min: "- 1 = word_of_int Numeral.Min"
|
|
260 |
unfolding Min_def by (simp only: word_of_int_hom_syms)
|
|
261 |
|
|
262 |
lemma succ_pred_no [simp]:
|
|
263 |
"word_succ (number_of bin) = number_of (Numeral.succ bin) &
|
|
264 |
word_pred (number_of bin) = number_of (Numeral.pred bin)"
|
|
265 |
unfolding word_number_of_def by (simp add : new_word_of_int_homs)
|
|
266 |
|
|
267 |
lemma word_sp_01 [simp] :
|
|
268 |
"word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
|
|
269 |
by (unfold word_0_no word_1_no) auto
|
|
270 |
|
|
271 |
(* alternative approach to lifting arithmetic equalities *)
|
|
272 |
lemma word_of_int_Ex:
|
|
273 |
"\<exists>y. x = word_of_int y"
|
|
274 |
by (rule_tac x="uint x" in exI) simp
|
|
275 |
|
|
276 |
lemma word_arith_eqs:
|
|
277 |
fixes a :: "'a::len0 word"
|
|
278 |
fixes b :: "'a::len0 word"
|
|
279 |
shows
|
|
280 |
word_add_0: "0 + a = a" and
|
|
281 |
word_add_0_right: "a + 0 = a" and
|
|
282 |
word_mult_1: "1 * a = a" and
|
|
283 |
word_mult_1_right: "a * 1 = a" and
|
|
284 |
word_add_commute: "a + b = b + a" and
|
|
285 |
word_add_assoc: "a + b + c = a + (b + c)" and
|
|
286 |
word_add_left_commute: "a + (b + c) = b + (a + c)" and
|
|
287 |
word_mult_commute: "a * b = b * a" and
|
|
288 |
word_mult_assoc: "a * b * c = a * (b * c)" and
|
|
289 |
word_mult_left_commute: "a * (b * c) = b * (a * c)" and
|
|
290 |
word_left_distrib: "(a + b) * c = a * c + b * c" and
|
|
291 |
word_right_distrib: "a * (b + c) = a * b + a * c" and
|
|
292 |
word_left_minus: "- a + a = 0" and
|
|
293 |
word_diff_0_right: "a - 0 = a" and
|
|
294 |
word_diff_self: "a - a = 0"
|
|
295 |
using word_of_int_Ex [of a]
|
|
296 |
word_of_int_Ex [of b]
|
|
297 |
word_of_int_Ex [of c]
|
|
298 |
by (auto simp: word_of_int_hom_syms [symmetric]
|
|
299 |
zadd_0_right add_commute add_assoc add_left_commute
|
|
300 |
mult_commute mult_assoc mult_left_commute
|
|
301 |
plus_times.left_distrib plus_times.right_distrib)
|
|
302 |
|
|
303 |
lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute
|
|
304 |
lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute
|
|
305 |
|
|
306 |
lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac
|
|
307 |
lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac
|
|
308 |
|
|
309 |
|
|
310 |
section "Order on fixed-length words"
|
|
311 |
|
|
312 |
lemma word_order_trans: "x <= y ==> y <= z ==> x <= (z :: 'a :: len0 word)"
|
|
313 |
unfolding word_le_def by auto
|
|
314 |
|
|
315 |
lemma word_order_refl: "z <= (z :: 'a :: len0 word)"
|
|
316 |
unfolding word_le_def by auto
|
|
317 |
|
|
318 |
lemma word_order_antisym: "x <= y ==> y <= x ==> x = (y :: 'a :: len0 word)"
|
|
319 |
unfolding word_le_def by (auto intro!: word_uint.Rep_eqD)
|
|
320 |
|
|
321 |
lemma word_order_linear:
|
|
322 |
"y <= x | x <= (y :: 'a :: len0 word)"
|
|
323 |
unfolding word_le_def by auto
|
|
324 |
|
|
325 |
lemma word_zero_le [simp] :
|
|
326 |
"0 <= (y :: 'a :: len0 word)"
|
|
327 |
unfolding word_le_def by auto
|
|
328 |
|
|
329 |
instance word :: (len0) semigroup_add
|
|
330 |
by intro_classes (simp add: word_add_assoc)
|
|
331 |
|
|
332 |
instance word :: (len0) linorder
|
|
333 |
by intro_classes (auto simp: word_less_def word_le_def)
|
|
334 |
|
|
335 |
instance word :: (len0) ring
|
|
336 |
by intro_classes
|
|
337 |
(auto simp: word_arith_eqs word_diff_minus
|
|
338 |
word_diff_self [unfolded word_diff_minus])
|
|
339 |
|
|
340 |
lemma word_m1_ge [simp] : "word_pred 0 >= y"
|
|
341 |
unfolding word_le_def
|
|
342 |
by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
|
|
343 |
|
|
344 |
lemmas word_n1_ge [simp] = word_m1_ge [simplified word_sp_01]
|
|
345 |
|
|
346 |
lemmas word_not_simps [simp] =
|
|
347 |
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
|
|
348 |
|
|
349 |
lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
|
|
350 |
unfolding word_less_def by auto
|
|
351 |
|
|
352 |
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of ?y"]
|
|
353 |
|
|
354 |
lemma word_sless_alt: "(a <s b) == (sint a < sint b)"
|
|
355 |
unfolding word_sle_def word_sless_def
|
|
356 |
by (auto simp add : less_eq_less.less_le)
|
|
357 |
|
|
358 |
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
|
|
359 |
unfolding unat_def word_le_def
|
|
360 |
by (rule nat_le_eq_zle [symmetric]) simp
|
|
361 |
|
|
362 |
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
|
|
363 |
unfolding unat_def word_less_alt
|
|
364 |
by (rule nat_less_eq_zless [symmetric]) simp
|
|
365 |
|
|
366 |
lemma wi_less:
|
|
367 |
"(word_of_int n < (word_of_int m :: 'a :: len0 word)) =
|
|
368 |
(n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
|
|
369 |
unfolding word_less_alt by (simp add: word_uint.eq_norm)
|
|
370 |
|
|
371 |
lemma wi_le:
|
|
372 |
"(word_of_int n <= (word_of_int m :: 'a :: len0 word)) =
|
|
373 |
(n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
|
|
374 |
unfolding word_le_def by (simp add: word_uint.eq_norm)
|
|
375 |
|
|
376 |
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
|
|
377 |
apply (unfold udvd_def)
|
|
378 |
apply safe
|
|
379 |
apply (simp add: unat_def nat_mult_distrib)
|
|
380 |
apply (simp add: uint_nat int_mult)
|
|
381 |
apply (rule exI)
|
|
382 |
apply safe
|
|
383 |
prefer 2
|
|
384 |
apply (erule notE)
|
|
385 |
apply (rule refl)
|
|
386 |
apply force
|
|
387 |
done
|
|
388 |
|
|
389 |
lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
|
|
390 |
unfolding dvd_def udvd_nat_alt by force
|
|
391 |
|
|
392 |
lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
|
|
393 |
|
|
394 |
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) ==> (0 :: 'a word) ~= 1";
|
|
395 |
unfolding word_arith_wis
|
|
396 |
by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
|
|
397 |
|
|
398 |
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
|
|
399 |
|
|
400 |
lemma no_no [simp] : "number_of (number_of b) = number_of b"
|
|
401 |
by (simp add: number_of_eq)
|
|
402 |
|
|
403 |
lemma unat_minus_one: "x ~= 0 ==> unat (x - 1) = unat x - 1"
|
|
404 |
apply (unfold unat_def)
|
|
405 |
apply (simp only: int_word_uint word_arith_alts rdmods)
|
|
406 |
apply (subgoal_tac "uint x >= 1")
|
|
407 |
prefer 2
|
|
408 |
apply (drule contrapos_nn)
|
|
409 |
apply (erule word_uint.Rep_inverse' [symmetric])
|
|
410 |
apply (insert uint_ge_0 [of x])[1]
|
|
411 |
apply arith
|
|
412 |
apply (rule box_equals)
|
|
413 |
apply (rule nat_diff_distrib)
|
|
414 |
prefer 2
|
|
415 |
apply assumption
|
|
416 |
apply simp
|
|
417 |
apply (subst mod_pos_pos_trivial)
|
|
418 |
apply arith
|
|
419 |
apply (insert uint_lt2p [of x])[1]
|
|
420 |
apply arith
|
|
421 |
apply (rule refl)
|
|
422 |
apply simp
|
|
423 |
done
|
|
424 |
|
|
425 |
lemma measure_unat: "p ~= 0 ==> unat (p - 1) < unat p"
|
|
426 |
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
|
|
427 |
|
|
428 |
lemmas uint_add_ge0 [simp] =
|
|
429 |
add_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
|
|
430 |
lemmas uint_mult_ge0 [simp] =
|
|
431 |
mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
|
|
432 |
|
|
433 |
lemma uint_sub_lt2p [simp]:
|
|
434 |
"uint (x :: 'a :: len0 word) - uint (y :: 'b :: len0 word) <
|
|
435 |
2 ^ len_of TYPE('a)"
|
|
436 |
using uint_ge_0 [of y] uint_lt2p [of x] by arith
|
|
437 |
|
|
438 |
|
|
439 |
section "Conditions for the addition (etc) of two words to overflow"
|
|
440 |
|
|
441 |
lemma uint_add_lem:
|
|
442 |
"(uint x + uint y < 2 ^ len_of TYPE('a)) =
|
|
443 |
(uint (x + y :: 'a :: len0 word) = uint x + uint y)"
|
|
444 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
|
|
445 |
|
|
446 |
lemma uint_mult_lem:
|
|
447 |
"(uint x * uint y < 2 ^ len_of TYPE('a)) =
|
|
448 |
(uint (x * y :: 'a :: len0 word) = uint x * uint y)"
|
|
449 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
|
|
450 |
|
|
451 |
lemma uint_sub_lem:
|
|
452 |
"(uint x >= uint y) = (uint (x - y) = uint x - uint y)"
|
|
453 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
|
|
454 |
|
|
455 |
lemma uint_add_le: "uint (x + y) <= uint x + uint y"
|
|
456 |
unfolding uint_word_ariths by (auto simp: mod_add_if_z)
|
|
457 |
|
|
458 |
lemma uint_sub_ge: "uint (x - y) >= uint x - uint y"
|
|
459 |
unfolding uint_word_ariths by (auto simp: mod_sub_if_z)
|
|
460 |
|
|
461 |
lemmas uint_sub_if' =
|
|
462 |
trans [OF uint_word_ariths(1) mod_sub_if_z, simplified, standard]
|
|
463 |
lemmas uint_plus_if' =
|
|
464 |
trans [OF uint_word_ariths(2) mod_add_if_z, simplified, standard]
|
|
465 |
|
|
466 |
|
|
467 |
section {* Definition of uint\_arith *}
|
|
468 |
|
|
469 |
lemma word_of_int_inverse:
|
|
470 |
"word_of_int r = a ==> 0 <= r ==> r < 2 ^ len_of TYPE('a) ==>
|
|
471 |
uint (a::'a::len0 word) = r"
|
|
472 |
apply (erule word_uint.Abs_inverse' [rotated])
|
|
473 |
apply (simp add: uints_num)
|
|
474 |
done
|
|
475 |
|
|
476 |
lemma uint_split:
|
|
477 |
fixes x::"'a::len0 word"
|
|
478 |
shows "P (uint x) =
|
|
479 |
(ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)"
|
|
480 |
apply (fold word_int_case_def)
|
|
481 |
apply (auto dest!: word_of_int_inverse simp: int_word_uint int_mod_eq'
|
|
482 |
split: word_int_split)
|
|
483 |
done
|
|
484 |
|
|
485 |
lemma uint_split_asm:
|
|
486 |
fixes x::"'a::len0 word"
|
|
487 |
shows "P (uint x) =
|
|
488 |
(~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))"
|
|
489 |
by (auto dest!: word_of_int_inverse
|
|
490 |
simp: int_word_uint int_mod_eq'
|
|
491 |
split: uint_split)
|
|
492 |
|
|
493 |
lemmas uint_splits = uint_split uint_split_asm
|
|
494 |
|
|
495 |
lemmas uint_arith_simps =
|
|
496 |
word_le_def word_less_alt
|
|
497 |
word_uint.Rep_inject [symmetric]
|
|
498 |
uint_sub_if' uint_plus_if'
|
|
499 |
|
|
500 |
(* use this to stop, eg, 2 ^ len_of TYPE (32) being simplified *)
|
|
501 |
lemma power_False_cong: "False ==> a ^ b = c ^ d"
|
|
502 |
by auto
|
|
503 |
|
|
504 |
(* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *)
|
|
505 |
ML {*
|
|
506 |
fun uint_arith_ss_of ss =
|
|
507 |
ss addsimps @{thms uint_arith_simps}
|
|
508 |
delsimps @{thms word_uint.Rep_inject}
|
|
509 |
addsplits @{thms split_if_asm}
|
|
510 |
addcongs @{thms power_False_cong}
|
|
511 |
|
|
512 |
fun uint_arith_tacs ctxt =
|
|
513 |
let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty
|
|
514 |
in
|
|
515 |
[ CLASET' clarify_tac 1,
|
|
516 |
SIMPSET' (full_simp_tac o uint_arith_ss_of) 1,
|
|
517 |
ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms uint_splits}
|
|
518 |
addcongs @{thms power_False_cong})),
|
|
519 |
rewrite_goals_tac @{thms word_size},
|
|
520 |
ALLGOALS (fn n => REPEAT (resolve_tac [allI, impI] n) THEN
|
|
521 |
REPEAT (etac conjE n) THEN
|
|
522 |
REPEAT (dtac @{thm word_of_int_inverse} n
|
|
523 |
THEN atac n
|
|
524 |
THEN atac n)),
|
|
525 |
TRYALL arith_tac' ]
|
|
526 |
end
|
|
527 |
|
|
528 |
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
|
|
529 |
*}
|
|
530 |
|
|
531 |
method_setup uint_arith =
|
|
532 |
"Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (uint_arith_tac ctxt 1))"
|
|
533 |
"solving word arithmetic via integers and arith"
|
|
534 |
|
|
535 |
|
|
536 |
section "More on overflows and monotonicity"
|
|
537 |
|
|
538 |
lemma no_plus_overflow_uint_size:
|
|
539 |
"((x :: 'a :: len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)"
|
|
540 |
unfolding word_size by uint_arith
|
|
541 |
|
|
542 |
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
|
|
543 |
|
|
544 |
lemma no_ulen_sub: "((x :: 'a :: len0 word) >= x - y) = (uint y <= uint x)"
|
|
545 |
by uint_arith
|
|
546 |
|
|
547 |
lemma no_olen_add':
|
|
548 |
fixes x :: "'a::len0 word"
|
|
549 |
shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))"
|
|
550 |
by (simp add: word_add_ac add_ac no_olen_add)
|
|
551 |
|
|
552 |
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric], standard]
|
|
553 |
|
|
554 |
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem, standard]
|
|
555 |
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1, standard]
|
|
556 |
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem, standard]
|
|
557 |
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
|
|
558 |
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
|
|
559 |
lemmas word_sub_le = word_sub_le_iff [THEN iffD2, standard]
|
|
560 |
|
|
561 |
lemma word_less_sub1:
|
|
562 |
"(x :: 'a :: len word) ~= 0 ==> (1 < x) = (0 < x - 1)"
|
|
563 |
by uint_arith
|
|
564 |
|
|
565 |
lemma word_le_sub1:
|
|
566 |
"(x :: 'a :: len word) ~= 0 ==> (1 <= x) = (0 <= x - 1)"
|
|
567 |
by uint_arith
|
|
568 |
|
|
569 |
lemma sub_wrap_lt:
|
|
570 |
"((x :: 'a :: len0 word) < x - z) = (x < z)"
|
|
571 |
by uint_arith
|
|
572 |
|
|
573 |
lemma sub_wrap:
|
|
574 |
"((x :: 'a :: len0 word) <= x - z) = (z = 0 | x < z)"
|
|
575 |
by uint_arith
|
|
576 |
|
|
577 |
lemma plus_minus_not_NULL_ab:
|
|
578 |
"(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> c ~= 0 ==> x + c ~= 0"
|
|
579 |
by uint_arith
|
|
580 |
|
|
581 |
lemma plus_minus_no_overflow_ab:
|
|
582 |
"(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> x <= x + c"
|
|
583 |
by uint_arith
|
|
584 |
|
|
585 |
lemma le_minus':
|
|
586 |
"(a :: 'a :: len0 word) + c <= b ==> a <= a + c ==> c <= b - a"
|
|
587 |
by uint_arith
|
|
588 |
|
|
589 |
lemma le_plus':
|
|
590 |
"(a :: 'a :: len0 word) <= b ==> c <= b - a ==> a + c <= b"
|
|
591 |
by uint_arith
|
|
592 |
|
|
593 |
lemmas le_plus = le_plus' [rotated]
|
|
594 |
|
|
595 |
lemmas le_minus = leD [THEN thin_rl, THEN le_minus', standard]
|
|
596 |
|
|
597 |
lemma word_plus_mono_right:
|
|
598 |
"(y :: 'a :: len0 word) <= z ==> x <= x + z ==> x + y <= x + z"
|
|
599 |
by uint_arith
|
|
600 |
|
|
601 |
lemma word_less_minus_cancel:
|
|
602 |
"y - x < z - x ==> x <= z ==> (y :: 'a :: len0 word) < z"
|
|
603 |
by uint_arith
|
|
604 |
|
|
605 |
lemma word_less_minus_mono_left:
|
|
606 |
"(y :: 'a :: len0 word) < z ==> x <= y ==> y - x < z - x"
|
|
607 |
by uint_arith
|
|
608 |
|
|
609 |
lemma word_less_minus_mono:
|
|
610 |
"a < c ==> d < b ==> a - b < a ==> c - d < c
|
|
611 |
==> a - b < c - (d::'a::len word)"
|
|
612 |
by uint_arith
|
|
613 |
|
|
614 |
lemma word_le_minus_cancel:
|
|
615 |
"y - x <= z - x ==> x <= z ==> (y :: 'a :: len0 word) <= z"
|
|
616 |
by uint_arith
|
|
617 |
|
|
618 |
lemma word_le_minus_mono_left:
|
|
619 |
"(y :: 'a :: len0 word) <= z ==> x <= y ==> y - x <= z - x"
|
|
620 |
by uint_arith
|
|
621 |
|
|
622 |
lemma word_le_minus_mono:
|
|
623 |
"a <= c ==> d <= b ==> a - b <= a ==> c - d <= c
|
|
624 |
==> a - b <= c - (d::'a::len word)"
|
|
625 |
by uint_arith
|
|
626 |
|
|
627 |
lemma plus_le_left_cancel_wrap:
|
|
628 |
"(x :: 'a :: len0 word) + y' < x ==> x + y < x ==> (x + y' < x + y) = (y' < y)"
|
|
629 |
by uint_arith
|
|
630 |
|
|
631 |
lemma plus_le_left_cancel_nowrap:
|
|
632 |
"(x :: 'a :: len0 word) <= x + y' ==> x <= x + y ==>
|
|
633 |
(x + y' < x + y) = (y' < y)"
|
|
634 |
by uint_arith
|
|
635 |
|
|
636 |
lemma word_plus_mono_right2:
|
|
637 |
"(a :: 'a :: len0 word) <= a + b ==> c <= b ==> a <= a + c"
|
|
638 |
by uint_arith
|
|
639 |
|
|
640 |
lemma word_less_add_right:
|
|
641 |
"(x :: 'a :: len0 word) < y - z ==> z <= y ==> x + z < y"
|
|
642 |
by uint_arith
|
|
643 |
|
|
644 |
lemma word_less_sub_right:
|
|
645 |
"(x :: 'a :: len0 word) < y + z ==> y <= x ==> x - y < z"
|
|
646 |
by uint_arith
|
|
647 |
|
|
648 |
lemma word_le_plus_either:
|
|
649 |
"(x :: 'a :: len0 word) <= y | x <= z ==> y <= y + z ==> x <= y + z"
|
|
650 |
by uint_arith
|
|
651 |
|
|
652 |
lemma word_less_nowrapI:
|
|
653 |
"(x :: 'a :: len0 word) < z - k ==> k <= z ==> 0 < k ==> x < x + k"
|
|
654 |
by uint_arith
|
|
655 |
|
|
656 |
lemma inc_le: "(i :: 'a :: len word) < m ==> i + 1 <= m"
|
|
657 |
by uint_arith
|
|
658 |
|
|
659 |
lemma inc_i:
|
|
660 |
"(1 :: 'a :: len word) <= i ==> i < m ==> 1 <= (i + 1) & i + 1 <= m"
|
|
661 |
by uint_arith
|
|
662 |
|
|
663 |
lemma udvd_incr_lem:
|
|
664 |
"up < uq ==> up = ua + n * uint K ==>
|
|
665 |
uq = ua + n' * uint K ==> up + uint K <= uq"
|
|
666 |
apply clarsimp
|
|
667 |
apply (drule less_le_mult)
|
|
668 |
apply safe
|
|
669 |
done
|
|
670 |
|
|
671 |
lemma udvd_incr':
|
|
672 |
"p < q ==> uint p = ua + n * uint K ==>
|
|
673 |
uint q = ua + n' * uint K ==> p + K <= q"
|
|
674 |
apply (unfold word_less_alt word_le_def)
|
|
675 |
apply (drule (2) udvd_incr_lem)
|
|
676 |
apply (erule uint_add_le [THEN order_trans])
|
|
677 |
done
|
|
678 |
|
|
679 |
lemma udvd_decr':
|
|
680 |
"p < q ==> uint p = ua + n * uint K ==>
|
|
681 |
uint q = ua + n' * uint K ==> p <= q - K"
|
|
682 |
apply (unfold word_less_alt word_le_def)
|
|
683 |
apply (drule (2) udvd_incr_lem)
|
|
684 |
apply (drule le_diff_eq [THEN iffD2])
|
|
685 |
apply (erule order_trans)
|
|
686 |
apply (rule uint_sub_ge)
|
|
687 |
done
|
|
688 |
|
|
689 |
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, simplified]
|
|
690 |
lemmas udvd_incr0 = udvd_incr' [where ua=0, simplified]
|
|
691 |
lemmas udvd_decr0 = udvd_decr' [where ua=0, simplified]
|
|
692 |
|
|
693 |
lemma udvd_minus_le':
|
|
694 |
"xy < k ==> z udvd xy ==> z udvd k ==> xy <= k - z"
|
|
695 |
apply (unfold udvd_def)
|
|
696 |
apply clarify
|
|
697 |
apply (erule (2) udvd_decr0)
|
|
698 |
done
|
|
699 |
|
|
700 |
lemma udvd_incr2_K:
|
|
701 |
"p < a + s ==> a <= a + s ==> K udvd s ==> K udvd p - a ==> a <= p ==>
|
|
702 |
0 < K ==> p <= p + K & p + K <= a + s"
|
|
703 |
apply (unfold udvd_def)
|
|
704 |
apply clarify
|
|
705 |
apply (simp add: uint_arith_simps split: split_if_asm)
|
|
706 |
prefer 2
|
|
707 |
apply (insert uint_range' [of s])[1]
|
|
708 |
apply arith
|
|
709 |
apply (drule add_commute [THEN xtr1])
|
|
710 |
apply (simp add: diff_less_eq [symmetric])
|
|
711 |
apply (drule less_le_mult)
|
|
712 |
apply arith
|
|
713 |
apply simp
|
|
714 |
done
|
|
715 |
|
|
716 |
(* links with rbl operations *)
|
|
717 |
lemma word_succ_rbl:
|
|
718 |
"to_bl w = bl ==> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))"
|
|
719 |
apply (unfold word_succ_def)
|
|
720 |
apply clarify
|
|
721 |
apply (simp add: to_bl_of_bin)
|
|
722 |
apply (simp add: to_bl_def rbl_succ)
|
|
723 |
done
|
|
724 |
|
|
725 |
lemma word_pred_rbl:
|
|
726 |
"to_bl w = bl ==> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))"
|
|
727 |
apply (unfold word_pred_def)
|
|
728 |
apply clarify
|
|
729 |
apply (simp add: to_bl_of_bin)
|
|
730 |
apply (simp add: to_bl_def rbl_pred)
|
|
731 |
done
|
|
732 |
|
|
733 |
lemma word_add_rbl:
|
|
734 |
"to_bl v = vbl ==> to_bl w = wbl ==>
|
|
735 |
to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))"
|
|
736 |
apply (unfold word_add_def)
|
|
737 |
apply clarify
|
|
738 |
apply (simp add: to_bl_of_bin)
|
|
739 |
apply (simp add: to_bl_def rbl_add)
|
|
740 |
done
|
|
741 |
|
|
742 |
lemma word_mult_rbl:
|
|
743 |
"to_bl v = vbl ==> to_bl w = wbl ==>
|
|
744 |
to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))"
|
|
745 |
apply (unfold word_mult_def)
|
|
746 |
apply clarify
|
|
747 |
apply (simp add: to_bl_of_bin)
|
|
748 |
apply (simp add: to_bl_def rbl_mult)
|
|
749 |
done
|
|
750 |
|
|
751 |
lemma rtb_rbl_ariths:
|
|
752 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
|
|
753 |
|
|
754 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
|
|
755 |
|
|
756 |
"[| rev (to_bl v) = ys; rev (to_bl w) = xs |]
|
|
757 |
==> rev (to_bl (v * w)) = rbl_mult ys xs"
|
|
758 |
|
|
759 |
"[| rev (to_bl v) = ys; rev (to_bl w) = xs |]
|
|
760 |
==> rev (to_bl (v + w)) = rbl_add ys xs"
|
|
761 |
by (auto simp: rev_swap [symmetric] word_succ_rbl
|
|
762 |
word_pred_rbl word_mult_rbl word_add_rbl)
|
|
763 |
|
|
764 |
|
|
765 |
section "Arithmetic type class instantiations"
|
|
766 |
|
|
767 |
instance word :: (len0) comm_monoid_add ..
|
|
768 |
|
|
769 |
instance word :: (len0) comm_monoid_mult
|
|
770 |
apply (intro_classes)
|
|
771 |
apply (simp add: word_mult_commute)
|
|
772 |
apply (simp add: word_mult_1)
|
|
773 |
done
|
|
774 |
|
|
775 |
instance word :: (len0) comm_semiring
|
|
776 |
by (intro_classes) (simp add : word_left_distrib)
|
|
777 |
|
|
778 |
instance word :: (len0) ab_group_add ..
|
|
779 |
|
|
780 |
instance word :: (len0) comm_ring ..
|
|
781 |
|
|
782 |
instance word :: (len) comm_semiring_1
|
|
783 |
by (intro_classes) (simp add: lenw1_zero_neq_one)
|
|
784 |
|
|
785 |
instance word :: (len) comm_ring_1 ..
|
|
786 |
|
|
787 |
instance word :: (len0) comm_semiring_0 ..
|
|
788 |
|
|
789 |
instance word :: (len0) order ..
|
|
790 |
|
|
791 |
instance word :: (len) recpower
|
|
792 |
by (intro_classes) (simp_all add: word_pow)
|
|
793 |
|
|
794 |
(* note that iszero_def is only for class comm_semiring_1_cancel,
|
|
795 |
which requires word length >= 1, ie 'a :: len word *)
|
|
796 |
lemma zero_bintrunc:
|
|
797 |
"iszero (number_of x :: 'a :: len word) =
|
|
798 |
(bintrunc (len_of TYPE('a)) x = Numeral.Pls)"
|
|
799 |
apply (unfold iszero_def word_0_wi word_no_wi)
|
|
800 |
apply (rule word_ubin.norm_eq_iff [symmetric, THEN trans])
|
|
801 |
apply (simp add : Pls_def [symmetric])
|
|
802 |
done
|
|
803 |
|
|
804 |
lemmas word_le_0_iff [simp] =
|
|
805 |
word_zero_le [THEN leD, THEN linorder_antisym_conv1]
|
|
806 |
|
|
807 |
lemma word_of_nat: "of_nat n = word_of_int (int n)"
|
|
808 |
by (induct n) (auto simp add : word_of_int_hom_syms)
|
|
809 |
|
|
810 |
lemma word_of_int: "of_int = word_of_int"
|
|
811 |
apply (rule ext)
|
|
812 |
apply (unfold of_int_def)
|
|
813 |
apply (rule contentsI)
|
|
814 |
apply safe
|
|
815 |
apply (simp_all add: word_of_nat word_of_int_homs)
|
|
816 |
defer
|
|
817 |
apply (rule Rep_Integ_ne [THEN nonemptyE])
|
|
818 |
apply (rule bexI)
|
|
819 |
prefer 2
|
|
820 |
apply assumption
|
|
821 |
apply (auto simp add: RI_eq_diff)
|
|
822 |
done
|
|
823 |
|
|
824 |
lemma word_of_int_nat:
|
|
825 |
"0 <= x ==> word_of_int x = of_nat (nat x)"
|
|
826 |
by (simp add: of_nat_nat word_of_int)
|
|
827 |
|
|
828 |
lemma word_number_of_eq:
|
|
829 |
"number_of w = (of_int w :: 'a :: len word)"
|
|
830 |
unfolding word_number_of_def word_of_int by auto
|
|
831 |
|
|
832 |
instance word :: (len) number_ring
|
|
833 |
by (intro_classes) (simp add : word_number_of_eq)
|
|
834 |
|
|
835 |
lemma iszero_word_no [simp] :
|
|
836 |
"iszero (number_of bin :: 'a :: len word) =
|
|
837 |
iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
|
|
838 |
apply (simp add: zero_bintrunc int_number_of)
|
|
839 |
apply (unfold iszero_def Pls_def)
|
|
840 |
apply (rule refl)
|
|
841 |
done
|
|
842 |
|
|
843 |
|
|
844 |
section "Word and nat"
|
|
845 |
|
|
846 |
lemma td_ext_unat':
|
|
847 |
"n = len_of TYPE ('a :: len) ==>
|
|
848 |
td_ext (unat :: 'a word => nat) of_nat
|
|
849 |
(unats n) (%i. i mod 2 ^ n)"
|
|
850 |
apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
|
|
851 |
apply (auto intro!: imageI simp add : word_of_int_hom_syms)
|
|
852 |
apply (erule word_uint.Abs_inverse [THEN arg_cong])
|
|
853 |
apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
|
|
854 |
done
|
|
855 |
|
|
856 |
lemmas td_ext_unat = refl [THEN td_ext_unat']
|
|
857 |
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm, standard]
|
|
858 |
|
|
859 |
interpretation word_unat:
|
|
860 |
td_ext ["unat::'a::len word => nat"
|
|
861 |
of_nat
|
|
862 |
"unats (len_of TYPE('a::len))"
|
|
863 |
"%i. i mod 2 ^ len_of TYPE('a::len)"]
|
|
864 |
by (rule td_ext_unat)
|
|
865 |
|
|
866 |
lemmas td_unat = word_unat.td_thm
|
|
867 |
|
|
868 |
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
|
|
869 |
|
|
870 |
lemma unat_le: "y <= unat (z :: 'a :: len word) ==> y : unats (len_of TYPE ('a))"
|
|
871 |
apply (unfold unats_def)
|
|
872 |
apply clarsimp
|
|
873 |
apply (rule xtrans, rule unat_lt2p, assumption)
|
|
874 |
done
|
|
875 |
|
|
876 |
lemma word_nchotomy:
|
|
877 |
"ALL w. EX n. (w :: 'a :: len word) = of_nat n & n < 2 ^ len_of TYPE ('a)"
|
|
878 |
apply (rule allI)
|
|
879 |
apply (rule word_unat.Abs_cases)
|
|
880 |
apply (unfold unats_def)
|
|
881 |
apply auto
|
|
882 |
done
|
|
883 |
|
|
884 |
lemma of_nat_eq:
|
|
885 |
fixes w :: "'a::len word"
|
|
886 |
shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))"
|
|
887 |
apply (rule trans)
|
|
888 |
apply (rule word_unat.inverse_norm)
|
|
889 |
apply (rule iffI)
|
|
890 |
apply (rule mod_eqD)
|
|
891 |
apply simp
|
|
892 |
apply clarsimp
|
|
893 |
done
|
|
894 |
|
|
895 |
lemma of_nat_eq_size:
|
|
896 |
"(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)"
|
|
897 |
unfolding word_size by (rule of_nat_eq)
|
|
898 |
|
|
899 |
lemma of_nat_0:
|
|
900 |
"(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))"
|
|
901 |
by (simp add: of_nat_eq)
|
|
902 |
|
|
903 |
lemmas of_nat_2p = mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]
|
|
904 |
|
|
905 |
lemma of_nat_gt_0: "of_nat k ~= 0 ==> 0 < k"
|
|
906 |
by (cases k) auto
|
|
907 |
|
|
908 |
lemma of_nat_neq_0:
|
|
909 |
"0 < k ==> k < 2 ^ len_of TYPE ('a :: len) ==> of_nat k ~= (0 :: 'a word)"
|
|
910 |
by (clarsimp simp add : of_nat_0)
|
|
911 |
|
|
912 |
lemma Abs_fnat_hom_add:
|
|
913 |
"of_nat a + of_nat b = of_nat (a + b)"
|
|
914 |
by simp
|
|
915 |
|
|
916 |
lemma Abs_fnat_hom_mult:
|
|
917 |
"of_nat a * of_nat b = (of_nat (a * b) :: 'a :: len word)"
|
|
918 |
by (simp add: word_of_nat word_of_int_mult_hom zmult_int)
|
|
919 |
|
|
920 |
lemma Abs_fnat_hom_Suc:
|
|
921 |
"word_succ (of_nat a) = of_nat (Suc a)"
|
|
922 |
by (simp add: word_of_nat word_of_int_succ_hom add_ac)
|
|
923 |
|
|
924 |
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
|
|
925 |
by (simp add: word_of_nat word_0_wi)
|
|
926 |
|
|
927 |
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
|
|
928 |
by (simp add: word_of_nat word_1_wi)
|
|
929 |
|
|
930 |
lemmas Abs_fnat_homs =
|
|
931 |
Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc
|
|
932 |
Abs_fnat_hom_0 Abs_fnat_hom_1
|
|
933 |
|
|
934 |
lemma word_arith_nat_add:
|
|
935 |
"a + b = of_nat (unat a + unat b)"
|
|
936 |
by simp
|
|
937 |
|
|
938 |
lemma word_arith_nat_mult:
|
|
939 |
"a * b = of_nat (unat a * unat b)"
|
|
940 |
by (simp add: Abs_fnat_hom_mult [symmetric])
|
|
941 |
|
|
942 |
lemma word_arith_nat_Suc:
|
|
943 |
"word_succ a = of_nat (Suc (unat a))"
|
|
944 |
by (subst Abs_fnat_hom_Suc [symmetric]) simp
|
|
945 |
|
|
946 |
lemma word_arith_nat_div:
|
|
947 |
"a div b = of_nat (unat a div unat b)"
|
|
948 |
by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
|
|
949 |
|
|
950 |
lemma word_arith_nat_mod:
|
|
951 |
"a mod b = of_nat (unat a mod unat b)"
|
|
952 |
by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
|
|
953 |
|
|
954 |
lemmas word_arith_nat_defs =
|
|
955 |
word_arith_nat_add word_arith_nat_mult
|
|
956 |
word_arith_nat_Suc Abs_fnat_hom_0
|
|
957 |
Abs_fnat_hom_1 word_arith_nat_div
|
|
958 |
word_arith_nat_mod
|
|
959 |
|
|
960 |
lemmas unat_cong = arg_cong [where f = "unat"]
|
|
961 |
|
|
962 |
lemmas unat_word_ariths = word_arith_nat_defs
|
|
963 |
[THEN trans [OF unat_cong unat_of_nat], standard]
|
|
964 |
|
|
965 |
lemmas word_sub_less_iff = word_sub_le_iff
|
|
966 |
[simplified linorder_not_less [symmetric], simplified]
|
|
967 |
|
|
968 |
lemma unat_add_lem:
|
|
969 |
"(unat x + unat y < 2 ^ len_of TYPE('a)) =
|
|
970 |
(unat (x + y :: 'a :: len word) = unat x + unat y)"
|
|
971 |
unfolding unat_word_ariths
|
|
972 |
by (auto intro!: trans [OF _ nat_mod_lem])
|
|
973 |
|
|
974 |
lemma unat_mult_lem:
|
|
975 |
"(unat x * unat y < 2 ^ len_of TYPE('a)) =
|
|
976 |
(unat (x * y :: 'a :: len word) = unat x * unat y)"
|
|
977 |
unfolding unat_word_ariths
|
|
978 |
by (auto intro!: trans [OF _ nat_mod_lem])
|
|
979 |
|
|
980 |
lemmas unat_plus_if' =
|
|
981 |
trans [OF unat_word_ariths(1) mod_nat_add, simplified, standard]
|
|
982 |
|
|
983 |
lemma le_no_overflow:
|
|
984 |
"x <= b ==> a <= a + b ==> x <= a + (b :: 'a :: len0 word)"
|
|
985 |
apply (erule order_trans)
|
|
986 |
apply (erule olen_add_eqv [THEN iffD1])
|
|
987 |
done
|
|
988 |
|
|
989 |
lemmas un_ui_le = trans
|
|
990 |
[OF word_le_nat_alt [symmetric]
|
|
991 |
word_le_def [THEN meta_eq_to_obj_eq],
|
|
992 |
standard]
|
|
993 |
|
|
994 |
lemma unat_sub_if_size:
|
|
995 |
"unat (x - y) = (if unat y <= unat x
|
|
996 |
then unat x - unat y
|
|
997 |
else unat x + 2 ^ size x - unat y)"
|
|
998 |
apply (unfold word_size)
|
|
999 |
apply (simp add: un_ui_le)
|
|
1000 |
apply (auto simp add: unat_def uint_sub_if')
|
|
1001 |
apply (rule nat_diff_distrib)
|
|
1002 |
prefer 3
|
|
1003 |
apply (simp add: group_simps)
|
|
1004 |
apply (rule nat_diff_distrib [THEN trans])
|
|
1005 |
prefer 3
|
|
1006 |
apply (subst nat_add_distrib)
|
|
1007 |
prefer 3
|
|
1008 |
apply (simp add: nat_power_eq)
|
|
1009 |
apply auto
|
|
1010 |
apply uint_arith
|
|
1011 |
done
|
|
1012 |
|
|
1013 |
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
|
|
1014 |
|
|
1015 |
lemma unat_div: "unat ((x :: 'a :: len word) div y) = unat x div unat y"
|
|
1016 |
apply (simp add : unat_word_ariths)
|
|
1017 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
|
|
1018 |
apply (rule div_le_dividend)
|
|
1019 |
done
|
|
1020 |
|
|
1021 |
lemma unat_mod: "unat ((x :: 'a :: len word) mod y) = unat x mod unat y"
|
|
1022 |
apply (clarsimp simp add : unat_word_ariths)
|
|
1023 |
apply (cases "unat y")
|
|
1024 |
prefer 2
|
|
1025 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
|
|
1026 |
apply (rule mod_le_divisor)
|
|
1027 |
apply auto
|
|
1028 |
done
|
|
1029 |
|
|
1030 |
lemma uint_div: "uint ((x :: 'a :: len word) div y) = uint x div uint y"
|
|
1031 |
unfolding uint_nat by (simp add : unat_div zdiv_int)
|
|
1032 |
|
|
1033 |
lemma uint_mod: "uint ((x :: 'a :: len word) mod y) = uint x mod uint y"
|
|
1034 |
unfolding uint_nat by (simp add : unat_mod zmod_int)
|
|
1035 |
|
|
1036 |
|
|
1037 |
section {* Definition of unat\_arith tactic *}
|
|
1038 |
|
|
1039 |
lemma unat_split:
|
|
1040 |
fixes x::"'a::len word"
|
|
1041 |
shows "P (unat x) =
|
|
1042 |
(ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)"
|
|
1043 |
by (auto simp: unat_of_nat)
|
|
1044 |
|
|
1045 |
lemma unat_split_asm:
|
|
1046 |
fixes x::"'a::len word"
|
|
1047 |
shows "P (unat x) =
|
|
1048 |
(~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))"
|
|
1049 |
by (auto simp: unat_of_nat)
|
|
1050 |
|
|
1051 |
lemmas of_nat_inverse =
|
|
1052 |
word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
|
|
1053 |
|
|
1054 |
lemmas unat_splits = unat_split unat_split_asm
|
|
1055 |
|
|
1056 |
lemmas unat_arith_simps =
|
|
1057 |
word_le_nat_alt word_less_nat_alt
|
|
1058 |
word_unat.Rep_inject [symmetric]
|
|
1059 |
unat_sub_if' unat_plus_if' unat_div unat_mod
|
|
1060 |
|
|
1061 |
(* unat_arith_tac: tactic to reduce word arithmetic to nat,
|
|
1062 |
try to solve via arith *)
|
|
1063 |
ML {*
|
|
1064 |
fun unat_arith_ss_of ss =
|
|
1065 |
ss addsimps @{thms unat_arith_simps}
|
|
1066 |
delsimps @{thms word_unat.Rep_inject}
|
|
1067 |
addsplits @{thms split_if_asm}
|
|
1068 |
addcongs @{thms power_False_cong}
|
|
1069 |
|
|
1070 |
fun unat_arith_tacs ctxt =
|
|
1071 |
let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty
|
|
1072 |
in
|
|
1073 |
[ CLASET' clarify_tac 1,
|
|
1074 |
SIMPSET' (full_simp_tac o unat_arith_ss_of) 1,
|
|
1075 |
ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms unat_splits}
|
|
1076 |
addcongs @{thms power_False_cong})),
|
|
1077 |
rewrite_goals_tac @{thms word_size},
|
|
1078 |
ALLGOALS (fn n => REPEAT (resolve_tac [allI, impI] n) THEN
|
|
1079 |
REPEAT (etac conjE n) THEN
|
|
1080 |
REPEAT (dtac @{thm of_nat_inverse} n THEN atac n)),
|
|
1081 |
TRYALL arith_tac' ]
|
|
1082 |
end
|
|
1083 |
|
|
1084 |
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
|
|
1085 |
*}
|
|
1086 |
|
|
1087 |
method_setup unat_arith =
|
|
1088 |
"Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (unat_arith_tac ctxt 1))"
|
|
1089 |
"solving word arithmetic via natural numbers and arith"
|
|
1090 |
|
|
1091 |
lemma no_plus_overflow_unat_size:
|
|
1092 |
"((x :: 'a :: len word) <= x + y) = (unat x + unat y < 2 ^ size x)"
|
|
1093 |
unfolding word_size by unat_arith
|
|
1094 |
|
|
1095 |
lemma unat_sub: "b <= a ==> unat (a - b) = unat a - unat (b :: 'a :: len word)"
|
|
1096 |
by unat_arith
|
|
1097 |
|
|
1098 |
lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size]
|
|
1099 |
|
|
1100 |
lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem, standard]
|
|
1101 |
|
|
1102 |
lemma word_div_mult:
|
|
1103 |
"(0 :: 'a :: len word) < y ==> unat x * unat y < 2 ^ len_of TYPE('a) ==>
|
|
1104 |
x * y div y = x"
|
|
1105 |
apply unat_arith
|
|
1106 |
apply clarsimp
|
|
1107 |
apply (subst unat_mult_lem [THEN iffD1])
|
|
1108 |
apply auto
|
|
1109 |
done
|
|
1110 |
|
|
1111 |
lemma div_lt': "(i :: 'a :: len word) <= k div x ==>
|
|
1112 |
unat i * unat x < 2 ^ len_of TYPE('a)"
|
|
1113 |
apply unat_arith
|
|
1114 |
apply clarsimp
|
|
1115 |
apply (drule mult_le_mono1)
|
|
1116 |
apply (erule order_le_less_trans)
|
|
1117 |
apply (rule xtr7 [OF unat_lt2p div_mult_le])
|
|
1118 |
done
|
|
1119 |
|
|
1120 |
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
|
|
1121 |
|
|
1122 |
lemma div_lt_mult: "(i :: 'a :: len word) < k div x ==> 0 < x ==> i * x < k"
|
|
1123 |
apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
|
|
1124 |
apply (simp add: unat_arith_simps)
|
|
1125 |
apply (drule (1) mult_less_mono1)
|
|
1126 |
apply (erule order_less_le_trans)
|
|
1127 |
apply (rule div_mult_le)
|
|
1128 |
done
|
|
1129 |
|
|
1130 |
lemma div_le_mult:
|
|
1131 |
"(i :: 'a :: len word) <= k div x ==> 0 < x ==> i * x <= k"
|
|
1132 |
apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
|
|
1133 |
apply (simp add: unat_arith_simps)
|
|
1134 |
apply (drule mult_le_mono1)
|
|
1135 |
apply (erule order_trans)
|
|
1136 |
apply (rule div_mult_le)
|
|
1137 |
done
|
|
1138 |
|
|
1139 |
lemma div_lt_uint':
|
|
1140 |
"(i :: 'a :: len word) <= k div x ==> uint i * uint x < 2 ^ len_of TYPE('a)"
|
|
1141 |
apply (unfold uint_nat)
|
|
1142 |
apply (drule div_lt')
|
|
1143 |
apply (simp add: zmult_int zless_nat_eq_int_zless [symmetric]
|
|
1144 |
nat_power_eq)
|
|
1145 |
done
|
|
1146 |
|
|
1147 |
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
|
|
1148 |
|
|
1149 |
lemma word_le_exists':
|
|
1150 |
"(x :: 'a :: len0 word) <= y ==>
|
|
1151 |
(EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))"
|
|
1152 |
apply (rule exI)
|
|
1153 |
apply (rule conjI)
|
|
1154 |
apply (rule zadd_diff_inverse)
|
|
1155 |
apply uint_arith
|
|
1156 |
done
|
|
1157 |
|
|
1158 |
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
|
|
1159 |
|
|
1160 |
lemmas plus_minus_no_overflow =
|
|
1161 |
order_less_imp_le [THEN plus_minus_no_overflow_ab]
|
|
1162 |
|
|
1163 |
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
|
|
1164 |
word_le_minus_cancel word_le_minus_mono_left
|
|
1165 |
|
|
1166 |
lemmas word_l_diffs = mcs [where y = "?w + ?x", unfolded add_diff_cancel]
|
|
1167 |
lemmas word_diff_ls = mcs [where z = "?w + ?x", unfolded add_diff_cancel]
|
|
1168 |
lemmas word_plus_mcs = word_diff_ls
|
|
1169 |
[where y = "?v + ?x", unfolded add_diff_cancel]
|
|
1170 |
|
|
1171 |
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
|
|
1172 |
|
|
1173 |
lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1]
|
|
1174 |
|
|
1175 |
lemma thd1:
|
|
1176 |
"a div b * b \<le> (a::nat)"
|
|
1177 |
using gt_or_eq_0 [of b]
|
|
1178 |
apply (rule disjE)
|
|
1179 |
apply (erule xtr4 [OF thd mult_commute])
|
|
1180 |
apply clarsimp
|
|
1181 |
done
|
|
1182 |
|
|
1183 |
lemmas uno_simps [THEN le_unat_uoi, standard] =
|
|
1184 |
mod_le_divisor div_le_dividend thd1
|
|
1185 |
|
|
1186 |
lemma word_mod_div_equality:
|
|
1187 |
"(n div b) * b + (n mod b) = (n :: 'a :: len word)"
|
|
1188 |
apply (unfold word_less_nat_alt word_arith_nat_defs)
|
|
1189 |
apply (cut_tac y="unat b" in gt_or_eq_0)
|
|
1190 |
apply (erule disjE)
|
|
1191 |
apply (simp add: mod_div_equality uno_simps)
|
|
1192 |
apply simp
|
|
1193 |
done
|
|
1194 |
|
|
1195 |
lemma word_div_mult_le: "a div b * b <= (a::'a::len word)"
|
|
1196 |
apply (unfold word_le_nat_alt word_arith_nat_defs)
|
|
1197 |
apply (cut_tac y="unat b" in gt_or_eq_0)
|
|
1198 |
apply (erule disjE)
|
|
1199 |
apply (simp add: div_mult_le uno_simps)
|
|
1200 |
apply simp
|
|
1201 |
done
|
|
1202 |
|
|
1203 |
lemma word_mod_less_divisor: "0 < n ==> m mod n < (n :: 'a :: len word)"
|
|
1204 |
apply (simp only: word_less_nat_alt word_arith_nat_defs)
|
|
1205 |
apply (clarsimp simp add : uno_simps)
|
|
1206 |
done
|
|
1207 |
|
|
1208 |
lemma word_of_int_power_hom:
|
|
1209 |
"word_of_int a ^ n = (word_of_int (a ^ n) :: 'a :: len word)"
|
|
1210 |
by (induct n) (simp_all add : word_of_int_hom_syms power_Suc)
|
|
1211 |
|
|
1212 |
lemma word_arith_power_alt:
|
|
1213 |
"a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)"
|
|
1214 |
by (simp add : word_of_int_power_hom [symmetric])
|
|
1215 |
|
|
1216 |
lemma of_bl_length_less:
|
|
1217 |
"length x = k ==> k < len_of TYPE('a) ==> (of_bl x :: 'a :: len word) < 2 ^ k"
|
|
1218 |
apply (unfold of_bl_no [unfolded word_number_of_def]
|
|
1219 |
word_less_alt word_number_of_alt)
|
|
1220 |
apply safe
|
|
1221 |
apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm
|
|
1222 |
del: word_of_int_bin)
|
|
1223 |
apply (simp add: mod_pos_pos_trivial)
|
|
1224 |
apply (subst mod_pos_pos_trivial)
|
|
1225 |
apply (rule bl_to_bin_ge0)
|
|
1226 |
apply (rule order_less_trans)
|
|
1227 |
apply (rule bl_to_bin_lt2p)
|
|
1228 |
apply simp
|
|
1229 |
apply (rule bl_to_bin_lt2p)
|
|
1230 |
done
|
|
1231 |
|
|
1232 |
|
|
1233 |
section "Cardinality, finiteness of set of words"
|
|
1234 |
|
|
1235 |
lemmas card_lessThan' = card_lessThan [unfolded lessThan_def]
|
|
1236 |
|
|
1237 |
lemmas card_eq = word_unat.Abs_inj_on [THEN card_image,
|
|
1238 |
unfolded word_unat.image, unfolded unats_def, standard]
|
|
1239 |
|
|
1240 |
lemmas card_word = trans [OF card_eq card_lessThan', standard]
|
|
1241 |
|
|
1242 |
lemma finite_word_UNIV: "finite (UNIV :: 'a :: len word set)"
|
|
1243 |
apply (rule contrapos_np)
|
|
1244 |
prefer 2
|
|
1245 |
apply (erule card_infinite)
|
|
1246 |
apply (simp add : card_word)
|
|
1247 |
done
|
|
1248 |
|
|
1249 |
lemma card_word_size:
|
|
1250 |
"card (UNIV :: 'a :: len word set) = (2 ^ size (x :: 'a word))"
|
|
1251 |
unfolding word_size by (rule card_word)
|
|
1252 |
|
|
1253 |
end
|
|
1254 |
|