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