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