author | nipkow |
Sun, 21 Oct 2007 14:53:44 +0200 | |
changeset 25134 | 3d4953e88449 |
parent 24465 | 70f0214b3ecc |
child 25349 | 0d46bea01741 |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
ID: $Id$ |
|
3 |
Author: Jeremy Dawson, NICTA |
|
4 |
||
5 |
contains basic definition to do with integers |
|
6 |
expressed using Pls, Min, BIT and important resulting theorems, |
|
7 |
in particular, bin_rec and related work |
|
8 |
*) |
|
9 |
||
24350 | 10 |
header {* Basic Definitions for Binary Integers *} |
11 |
||
24465 | 12 |
theory BinGeneral imports Num_Lemmas |
24333 | 13 |
|
14 |
begin |
|
15 |
||
24465 | 16 |
subsection {* Recursion combinator for binary integers *} |
24384
0002537695df
move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents:
24383
diff
changeset
|
17 |
|
24465 | 18 |
lemma brlem: "(bin = Numeral.Min) = (- bin + Numeral.pred 0 = 0)" |
19 |
unfolding Min_def pred_def by arith |
|
24350 | 20 |
|
24333 | 21 |
function |
22 |
bin_rec' :: "int * 'a * 'a * (int => bit => 'a => 'a) => 'a" |
|
23 |
where |
|
24 |
"bin_rec' (bin, f1, f2, f3) = (if bin = Numeral.Pls then f1 |
|
25 |
else if bin = Numeral.Min then f2 |
|
24465 | 26 |
else case bin_rl bin of (w, b) => f3 w b (bin_rec' (w, f1, f2, f3)))" |
24333 | 27 |
by pat_completeness auto |
28 |
||
29 |
termination |
|
24465 | 30 |
apply (relation "measure (nat o abs o fst)") |
31 |
apply simp |
|
32 |
apply (simp add: Pls_def brlem) |
|
33 |
apply (clarsimp simp: bin_rl_char pred_def) |
|
34 |
apply (frule thin_rl [THEN refl [THEN bin_abs_lem [rule_format]]]) |
|
35 |
apply (unfold Pls_def Min_def number_of_eq) |
|
36 |
prefer 2 |
|
37 |
apply (erule asm_rl) |
|
38 |
apply auto |
|
39 |
done |
|
24333 | 40 |
|
41 |
constdefs |
|
42 |
bin_rec :: "'a => 'a => (int => bit => 'a => 'a) => int => 'a" |
|
43 |
"bin_rec f1 f2 f3 bin == bin_rec' (bin, f1, f2, f3)" |
|
44 |
||
24465 | 45 |
lemma bin_rec_PM: |
46 |
"f = bin_rec f1 f2 f3 ==> f Numeral.Pls = f1 & f Numeral.Min = f2" |
|
47 |
apply safe |
|
48 |
apply (unfold bin_rec_def) |
|
49 |
apply (auto intro: bin_rec'.simps [THEN trans]) |
|
50 |
done |
|
24333 | 51 |
|
24465 | 52 |
lemmas bin_rec_Pls = refl [THEN bin_rec_PM, THEN conjunct1, standard] |
53 |
lemmas bin_rec_Min = refl [THEN bin_rec_PM, THEN conjunct2, standard] |
|
24333 | 54 |
|
55 |
lemma bin_rec_Bit: |
|
56 |
"f = bin_rec f1 f2 f3 ==> f3 Numeral.Pls bit.B0 f1 = f1 ==> |
|
57 |
f3 Numeral.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)" |
|
58 |
apply clarify |
|
59 |
apply (unfold bin_rec_def) |
|
60 |
apply (rule bin_rec'.simps [THEN trans]) |
|
61 |
apply auto |
|
24465 | 62 |
apply (unfold Pls_def Min_def Bit_def) |
63 |
apply (cases b, auto)+ |
|
24333 | 64 |
done |
65 |
||
66 |
lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min |
|
67 |
||
24465 | 68 |
subsection {* Destructors for binary integers *} |
24364 | 69 |
|
70 |
consts |
|
24465 | 71 |
-- "corresponding operations analysing bins" |
72 |
bin_last :: "int => bit" |
|
73 |
bin_rest :: "int => int" |
|
24364 | 74 |
bin_sign :: "int => int" |
24465 | 75 |
bin_nth :: "int => nat => bool" |
76 |
||
77 |
primrec |
|
78 |
Z : "bin_nth w 0 = (bin_last w = bit.B1)" |
|
79 |
Suc : "bin_nth w (Suc n) = bin_nth (bin_rest w) n" |
|
24364 | 80 |
|
81 |
defs |
|
24465 | 82 |
bin_rest_def : "bin_rest w == fst (bin_rl w)" |
83 |
bin_last_def : "bin_last w == snd (bin_rl w)" |
|
24364 | 84 |
bin_sign_def : "bin_sign == bin_rec Numeral.Pls Numeral.Min (%w b s. s)" |
85 |
||
24465 | 86 |
lemma bin_rl: "bin_rl w = (bin_rest w, bin_last w)" |
87 |
unfolding bin_rest_def bin_last_def by auto |
|
88 |
||
89 |
lemmas bin_rl_simp [simp] = iffD1 [OF bin_rl_char bin_rl] |
|
24364 | 90 |
|
24465 | 91 |
lemma bin_rest_simps [simp]: |
92 |
"bin_rest Numeral.Pls = Numeral.Pls" |
|
93 |
"bin_rest Numeral.Min = Numeral.Min" |
|
94 |
"bin_rest (w BIT b) = w" |
|
95 |
unfolding bin_rest_def by auto |
|
96 |
||
97 |
lemma bin_last_simps [simp]: |
|
98 |
"bin_last Numeral.Pls = bit.B0" |
|
99 |
"bin_last Numeral.Min = bit.B1" |
|
100 |
"bin_last (w BIT b) = b" |
|
101 |
unfolding bin_last_def by auto |
|
24333 | 102 |
|
103 |
lemma bin_sign_simps [simp]: |
|
104 |
"bin_sign Numeral.Pls = Numeral.Pls" |
|
105 |
"bin_sign Numeral.Min = Numeral.Min" |
|
106 |
"bin_sign (w BIT b) = bin_sign w" |
|
107 |
unfolding bin_sign_def by (auto simp: bin_rec_simps) |
|
108 |
||
109 |
lemma bin_r_l_extras [simp]: |
|
110 |
"bin_last 0 = bit.B0" |
|
111 |
"bin_last (- 1) = bit.B1" |
|
112 |
"bin_last -1 = bit.B1" |
|
113 |
"bin_last 1 = bit.B1" |
|
114 |
"bin_rest 1 = 0" |
|
115 |
"bin_rest 0 = 0" |
|
116 |
"bin_rest (- 1) = - 1" |
|
117 |
"bin_rest -1 = -1" |
|
118 |
apply (unfold number_of_Min) |
|
119 |
apply (unfold Pls_def [symmetric] Min_def [symmetric]) |
|
120 |
apply (unfold numeral_1_eq_1 [symmetric]) |
|
121 |
apply (auto simp: number_of_eq) |
|
122 |
done |
|
123 |
||
124 |
lemma bin_last_mod: |
|
125 |
"bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)" |
|
24465 | 126 |
apply (case_tac w rule: bin_exhaust) |
127 |
apply (case_tac b) |
|
128 |
apply auto |
|
24333 | 129 |
done |
130 |
||
131 |
lemma bin_rest_div: |
|
132 |
"bin_rest w = w div 2" |
|
24465 | 133 |
apply (case_tac w rule: bin_exhaust) |
134 |
apply (rule trans) |
|
135 |
apply clarsimp |
|
136 |
apply (rule refl) |
|
137 |
apply (drule trans) |
|
138 |
apply (rule Bit_def) |
|
139 |
apply (simp add: z1pdiv2 split: bit.split) |
|
24333 | 140 |
done |
141 |
||
142 |
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
|
143 |
unfolding bin_rest_div [symmetric] by auto |
|
144 |
||
145 |
lemma bin_nth_lem [rule_format]: |
|
146 |
"ALL y. bin_nth x = bin_nth y --> x = y" |
|
147 |
apply (induct x rule: bin_induct) |
|
148 |
apply safe |
|
149 |
apply (erule rev_mp) |
|
150 |
apply (induct_tac y rule: bin_induct) |
|
151 |
apply safe |
|
152 |
apply (drule_tac x=0 in fun_cong, force) |
|
153 |
apply (erule notE, rule ext, |
|
154 |
drule_tac x="Suc x" in fun_cong, force) |
|
155 |
apply (drule_tac x=0 in fun_cong, force) |
|
156 |
apply (erule rev_mp) |
|
157 |
apply (induct_tac y rule: bin_induct) |
|
158 |
apply safe |
|
159 |
apply (drule_tac x=0 in fun_cong, force) |
|
160 |
apply (erule notE, rule ext, |
|
161 |
drule_tac x="Suc x" in fun_cong, force) |
|
162 |
apply (drule_tac x=0 in fun_cong, force) |
|
163 |
apply (case_tac y rule: bin_exhaust) |
|
164 |
apply clarify |
|
165 |
apply (erule allE) |
|
166 |
apply (erule impE) |
|
167 |
prefer 2 |
|
168 |
apply (erule BIT_eqI) |
|
169 |
apply (drule_tac x=0 in fun_cong, force) |
|
170 |
apply (rule ext) |
|
171 |
apply (drule_tac x="Suc ?x" in fun_cong, force) |
|
172 |
done |
|
173 |
||
174 |
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)" |
|
175 |
by (auto elim: bin_nth_lem) |
|
176 |
||
177 |
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard] |
|
178 |
||
179 |
lemma bin_nth_Pls [simp]: "~ bin_nth Numeral.Pls n" |
|
180 |
by (induct n) auto |
|
181 |
||
182 |
lemma bin_nth_Min [simp]: "bin_nth Numeral.Min n" |
|
183 |
by (induct n) auto |
|
184 |
||
185 |
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = bit.B1)" |
|
186 |
by auto |
|
187 |
||
188 |
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n" |
|
189 |
by auto |
|
190 |
||
191 |
lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)" |
|
192 |
by (cases n) auto |
|
193 |
||
194 |
lemmas bin_nth_0 = bin_nth.simps(1) |
|
195 |
lemmas bin_nth_Suc = bin_nth.simps(2) |
|
196 |
||
197 |
lemmas bin_nth_simps = |
|
198 |
bin_nth_0 bin_nth_Suc bin_nth_Pls bin_nth_Min bin_nth_minus |
|
199 |
||
24364 | 200 |
lemma bin_sign_rest [simp]: |
201 |
"bin_sign (bin_rest w) = (bin_sign w)" |
|
202 |
by (case_tac w rule: bin_exhaust) auto |
|
203 |
||
204 |
subsection {* Truncating binary integers *} |
|
205 |
||
206 |
consts |
|
207 |
bintrunc :: "nat => int => int" |
|
208 |
primrec |
|
209 |
Z : "bintrunc 0 bin = Numeral.Pls" |
|
210 |
Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)" |
|
211 |
||
212 |
consts |
|
213 |
sbintrunc :: "nat => int => int" |
|
214 |
primrec |
|
215 |
Z : "sbintrunc 0 bin = |
|
216 |
(case bin_last bin of bit.B1 => Numeral.Min | bit.B0 => Numeral.Pls)" |
|
217 |
Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" |
|
218 |
||
24333 | 219 |
lemma sign_bintr: |
220 |
"!!w. bin_sign (bintrunc n w) = Numeral.Pls" |
|
221 |
by (induct n) auto |
|
222 |
||
223 |
lemma bintrunc_mod2p: |
|
224 |
"!!w. bintrunc n w = (w mod 2 ^ n :: int)" |
|
225 |
apply (induct n, clarsimp) |
|
226 |
apply (simp add: bin_last_mod bin_rest_div Bit_def zmod_zmult2_eq |
|
227 |
cong: number_of_False_cong) |
|
228 |
done |
|
229 |
||
230 |
lemma sbintrunc_mod2p: |
|
231 |
"!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n :: int)" |
|
232 |
apply (induct n) |
|
233 |
apply clarsimp |
|
234 |
apply (subst zmod_zadd_left_eq) |
|
235 |
apply (simp add: bin_last_mod) |
|
236 |
apply (simp add: number_of_eq) |
|
237 |
apply clarsimp |
|
238 |
apply (simp add: bin_last_mod bin_rest_div Bit_def |
|
239 |
cong: number_of_False_cong) |
|
240 |
apply (clarsimp simp: zmod_zmult_zmult1 [symmetric] |
|
241 |
zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]]) |
|
242 |
apply (rule trans [symmetric, OF _ emep1]) |
|
243 |
apply auto |
|
244 |
apply (auto simp: even_def) |
|
245 |
done |
|
246 |
||
24465 | 247 |
subsection "Simplifications for (s)bintrunc" |
248 |
||
249 |
lemma bit_bool: |
|
250 |
"(b = (b' = bit.B1)) = (b' = (if b then bit.B1 else bit.B0))" |
|
251 |
by (cases b') auto |
|
252 |
||
253 |
lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric] |
|
24333 | 254 |
|
255 |
lemma bin_sign_lem: |
|
256 |
"!!bin. (bin_sign (sbintrunc n bin) = Numeral.Min) = bin_nth bin n" |
|
257 |
apply (induct n) |
|
258 |
apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+ |
|
259 |
done |
|
260 |
||
261 |
lemma nth_bintr: |
|
262 |
"!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)" |
|
263 |
apply (induct n) |
|
264 |
apply (case_tac m, auto)[1] |
|
265 |
apply (case_tac m, auto)[1] |
|
266 |
done |
|
267 |
||
268 |
lemma nth_sbintr: |
|
269 |
"!!w m. bin_nth (sbintrunc m w) n = |
|
270 |
(if n < m then bin_nth w n else bin_nth w m)" |
|
271 |
apply (induct n) |
|
272 |
apply (case_tac m, simp_all split: bit.splits)[1] |
|
273 |
apply (case_tac m, simp_all split: bit.splits)[1] |
|
274 |
done |
|
275 |
||
276 |
lemma bin_nth_Bit: |
|
277 |
"bin_nth (w BIT b) n = (n = 0 & b = bit.B1 | (EX m. n = Suc m & bin_nth w m))" |
|
278 |
by (cases n) auto |
|
279 |
||
280 |
lemma bintrunc_bintrunc_l: |
|
281 |
"n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)" |
|
282 |
by (rule bin_eqI) (auto simp add : nth_bintr) |
|
283 |
||
284 |
lemma sbintrunc_sbintrunc_l: |
|
285 |
"n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)" |
|
286 |
by (rule bin_eqI) (auto simp: nth_sbintr min_def) |
|
287 |
||
288 |
lemma bintrunc_bintrunc_ge: |
|
289 |
"n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)" |
|
290 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
291 |
||
292 |
lemma bintrunc_bintrunc_min [simp]: |
|
293 |
"bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
|
294 |
apply (unfold min_def) |
|
295 |
apply (rule bin_eqI) |
|
296 |
apply (auto simp: nth_bintr) |
|
297 |
done |
|
298 |
||
299 |
lemma sbintrunc_sbintrunc_min [simp]: |
|
300 |
"sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
|
301 |
apply (unfold min_def) |
|
302 |
apply (rule bin_eqI) |
|
303 |
apply (auto simp: nth_sbintr) |
|
304 |
done |
|
305 |
||
306 |
lemmas bintrunc_Pls = |
|
307 |
bintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps] |
|
308 |
||
309 |
lemmas bintrunc_Min [simp] = |
|
310 |
bintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps] |
|
311 |
||
312 |
lemmas bintrunc_BIT [simp] = |
|
313 |
bintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps] |
|
314 |
||
315 |
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT |
|
316 |
||
317 |
lemmas sbintrunc_Suc_Pls = |
|
318 |
sbintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps] |
|
319 |
||
320 |
lemmas sbintrunc_Suc_Min = |
|
321 |
sbintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps] |
|
322 |
||
323 |
lemmas sbintrunc_Suc_BIT [simp] = |
|
324 |
sbintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps] |
|
325 |
||
326 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT |
|
327 |
||
328 |
lemmas sbintrunc_Pls = |
|
329 |
sbintrunc.Z [where bin="Numeral.Pls", |
|
330 |
simplified bin_last_simps bin_rest_simps bit.simps] |
|
331 |
||
332 |
lemmas sbintrunc_Min = |
|
333 |
sbintrunc.Z [where bin="Numeral.Min", |
|
334 |
simplified bin_last_simps bin_rest_simps bit.simps] |
|
335 |
||
336 |
lemmas sbintrunc_0_BIT_B0 [simp] = |
|
337 |
sbintrunc.Z [where bin="?w BIT bit.B0", |
|
338 |
simplified bin_last_simps bin_rest_simps bit.simps] |
|
339 |
||
340 |
lemmas sbintrunc_0_BIT_B1 [simp] = |
|
341 |
sbintrunc.Z [where bin="?w BIT bit.B1", |
|
342 |
simplified bin_last_simps bin_rest_simps bit.simps] |
|
343 |
||
344 |
lemmas sbintrunc_0_simps = |
|
345 |
sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1 |
|
346 |
||
347 |
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs |
|
348 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
349 |
||
350 |
lemma bintrunc_minus: |
|
351 |
"0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w" |
|
352 |
by auto |
|
353 |
||
354 |
lemma sbintrunc_minus: |
|
355 |
"0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
356 |
by auto |
|
357 |
||
358 |
lemmas bintrunc_minus_simps = |
|
359 |
bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard] |
|
360 |
lemmas sbintrunc_minus_simps = |
|
361 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard] |
|
362 |
||
363 |
lemma bintrunc_n_Pls [simp]: |
|
364 |
"bintrunc n Numeral.Pls = Numeral.Pls" |
|
365 |
by (induct n) auto |
|
366 |
||
367 |
lemma sbintrunc_n_PM [simp]: |
|
368 |
"sbintrunc n Numeral.Pls = Numeral.Pls" |
|
369 |
"sbintrunc n Numeral.Min = Numeral.Min" |
|
370 |
by (induct n) auto |
|
371 |
||
372 |
lemmas thobini1 = arg_cong [where f = "%w. w BIT ?b"] |
|
373 |
||
374 |
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1] |
|
375 |
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1] |
|
376 |
||
377 |
lemmas bmsts = bintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard] |
|
378 |
lemmas bintrunc_Pls_minus_I = bmsts(1) |
|
379 |
lemmas bintrunc_Min_minus_I = bmsts(2) |
|
380 |
lemmas bintrunc_BIT_minus_I = bmsts(3) |
|
381 |
||
382 |
lemma bintrunc_0_Min: "bintrunc 0 Numeral.Min = Numeral.Pls" |
|
383 |
by auto |
|
384 |
lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Numeral.Pls" |
|
385 |
by auto |
|
386 |
||
387 |
lemma bintrunc_Suc_lem: |
|
388 |
"bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y" |
|
389 |
by auto |
|
390 |
||
391 |
lemmas bintrunc_Suc_Ialts = |
|
392 |
bintrunc_Min_I bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard] |
|
393 |
||
394 |
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1] |
|
395 |
||
396 |
lemmas sbintrunc_Suc_Is = |
|
397 |
sbintrunc_Sucs [THEN thobini1 [THEN [2] trans], standard] |
|
398 |
||
399 |
lemmas sbintrunc_Suc_minus_Is = |
|
400 |
sbintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard] |
|
401 |
||
402 |
lemma sbintrunc_Suc_lem: |
|
403 |
"sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y" |
|
404 |
by auto |
|
405 |
||
406 |
lemmas sbintrunc_Suc_Ialts = |
|
407 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard] |
|
408 |
||
409 |
lemma sbintrunc_bintrunc_lt: |
|
410 |
"m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w" |
|
411 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
|
412 |
||
413 |
lemma bintrunc_sbintrunc_le: |
|
414 |
"m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w" |
|
415 |
apply (rule bin_eqI) |
|
416 |
apply (auto simp: nth_sbintr nth_bintr) |
|
417 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
418 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
419 |
done |
|
420 |
||
421 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
422 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
423 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
424 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
|
425 |
||
426 |
lemma bintrunc_sbintrunc' [simp]: |
|
427 |
"0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
|
428 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
429 |
||
430 |
lemma sbintrunc_bintrunc' [simp]: |
|
431 |
"0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
|
432 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
433 |
||
434 |
lemma bin_sbin_eq_iff: |
|
435 |
"bintrunc (Suc n) x = bintrunc (Suc n) y <-> |
|
436 |
sbintrunc n x = sbintrunc n y" |
|
437 |
apply (rule iffI) |
|
438 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
439 |
apply simp |
|
440 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
441 |
apply simp |
|
442 |
done |
|
443 |
||
444 |
lemma bin_sbin_eq_iff': |
|
445 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> |
|
446 |
sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
|
447 |
by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc) |
|
448 |
||
449 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
450 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
451 |
||
452 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
453 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
454 |
||
455 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
456 |
tends to get applied where it's not wanted in developing the theories, |
|
457 |
we get a version for when the word length is given literally *) |
|
458 |
||
459 |
lemmas nat_non0_gr = |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24465
diff
changeset
|
460 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl, standard] |
24333 | 461 |
|
462 |
lemmas bintrunc_pred_simps [simp] = |
|
463 |
bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] |
|
464 |
||
465 |
lemmas sbintrunc_pred_simps [simp] = |
|
466 |
sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] |
|
467 |
||
468 |
lemma no_bintr_alt: |
|
469 |
"number_of (bintrunc n w) = w mod 2 ^ n" |
|
470 |
by (simp add: number_of_eq bintrunc_mod2p) |
|
471 |
||
472 |
lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)" |
|
473 |
by (rule ext) (rule bintrunc_mod2p) |
|
474 |
||
475 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}" |
|
476 |
apply (unfold no_bintr_alt1) |
|
477 |
apply (auto simp add: image_iff) |
|
478 |
apply (rule exI) |
|
479 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
480 |
done |
|
481 |
||
482 |
lemma no_bintr: |
|
483 |
"number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)" |
|
484 |
by (simp add : bintrunc_mod2p number_of_eq) |
|
485 |
||
486 |
lemma no_sbintr_alt2: |
|
487 |
"sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
488 |
by (rule ext) (simp add : sbintrunc_mod2p) |
|
489 |
||
490 |
lemma no_sbintr: |
|
491 |
"number_of (sbintrunc n w) = |
|
492 |
((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
493 |
by (simp add : no_sbintr_alt2 number_of_eq) |
|
494 |
||
495 |
lemma range_sbintrunc: |
|
496 |
"range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}" |
|
497 |
apply (unfold no_sbintr_alt2) |
|
498 |
apply (auto simp add: image_iff eq_diff_eq) |
|
499 |
apply (rule exI) |
|
500 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
501 |
done |
|
502 |
||
503 |
lemmas sb_inc_lem = int_mod_ge' |
|
504 |
[where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", |
|
505 |
simplified zless2p, OF _ TrueI] |
|
506 |
||
507 |
lemmas sb_inc_lem' = |
|
508 |
iffD1 [OF less_diff_eq, THEN sb_inc_lem, simplified OrderedGroup.diff_0] |
|
509 |
||
510 |
lemma sbintrunc_inc: |
|
511 |
"x < - (2 ^ n) ==> x + 2 ^ (Suc n) <= sbintrunc n x" |
|
512 |
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp |
|
513 |
||
514 |
lemmas sb_dec_lem = int_mod_le' |
|
515 |
[where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", |
|
516 |
simplified zless2p, OF _ TrueI, simplified] |
|
517 |
||
518 |
lemmas sb_dec_lem' = iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified] |
|
519 |
||
520 |
lemma sbintrunc_dec: |
|
521 |
"x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x" |
|
522 |
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp |
|
523 |
||
524 |
lemmas zmod_uminus' = zmod_uminus [where b="?c"] |
|
525 |
lemmas zpower_zmod' = zpower_zmod [where m="?c" and y="?k"] |
|
526 |
||
527 |
lemmas brdmod1s' [symmetric] = |
|
528 |
zmod_zadd_left_eq zmod_zadd_right_eq |
|
529 |
zmod_zsub_left_eq zmod_zsub_right_eq |
|
530 |
zmod_zmult1_eq zmod_zmult1_eq_rev |
|
531 |
||
532 |
lemmas brdmods' [symmetric] = |
|
533 |
zpower_zmod' [symmetric] |
|
534 |
trans [OF zmod_zadd_left_eq zmod_zadd_right_eq] |
|
535 |
trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] |
|
536 |
trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] |
|
537 |
zmod_uminus' [symmetric] |
|
538 |
zmod_zadd_left_eq [where b = "1"] |
|
539 |
zmod_zsub_left_eq [where b = "1"] |
|
540 |
||
541 |
lemmas bintr_arith1s = |
|
542 |
brdmod1s' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p] |
|
543 |
lemmas bintr_ariths = |
|
544 |
brdmods' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p] |
|
545 |
||
24364 | 546 |
lemmas m2pths [OF zless2p, standard] = pos_mod_sign pos_mod_bound |
547 |
||
24333 | 548 |
lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)" |
549 |
by (simp add : no_bintr m2pths) |
|
550 |
||
551 |
lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)" |
|
552 |
by (simp add : no_bintr m2pths) |
|
553 |
||
554 |
lemma bintr_Min: |
|
555 |
"number_of (bintrunc n Numeral.Min) = (2 ^ n :: int) - 1" |
|
556 |
by (simp add : no_bintr m1mod2k) |
|
557 |
||
558 |
lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)" |
|
559 |
by (simp add : no_sbintr m2pths) |
|
560 |
||
561 |
lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)" |
|
562 |
by (simp add : no_sbintr m2pths) |
|
563 |
||
564 |
lemma bintrunc_Suc: |
|
565 |
"bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin" |
|
566 |
by (case_tac bin rule: bin_exhaust) auto |
|
567 |
||
568 |
lemma sign_Pls_ge_0: |
|
569 |
"(bin_sign bin = Numeral.Pls) = (number_of bin >= (0 :: int))" |
|
570 |
by (induct bin rule: bin_induct) auto |
|
571 |
||
572 |
lemma sign_Min_lt_0: |
|
573 |
"(bin_sign bin = Numeral.Min) = (number_of bin < (0 :: int))" |
|
574 |
by (induct bin rule: bin_induct) auto |
|
575 |
||
576 |
lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] |
|
577 |
||
578 |
lemma bin_rest_trunc: |
|
579 |
"!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)" |
|
580 |
by (induct n) auto |
|
581 |
||
582 |
lemma bin_rest_power_trunc [rule_format] : |
|
583 |
"(bin_rest ^ k) (bintrunc n bin) = |
|
584 |
bintrunc (n - k) ((bin_rest ^ k) bin)" |
|
585 |
by (induct k) (auto simp: bin_rest_trunc) |
|
586 |
||
587 |
lemma bin_rest_trunc_i: |
|
588 |
"bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
|
589 |
by auto |
|
590 |
||
591 |
lemma bin_rest_strunc: |
|
592 |
"!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
|
593 |
by (induct n) auto |
|
594 |
||
595 |
lemma bintrunc_rest [simp]: |
|
596 |
"!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
|
597 |
apply (induct n, simp) |
|
598 |
apply (case_tac bin rule: bin_exhaust) |
|
599 |
apply (auto simp: bintrunc_bintrunc_l) |
|
600 |
done |
|
601 |
||
602 |
lemma sbintrunc_rest [simp]: |
|
603 |
"!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
|
604 |
apply (induct n, simp) |
|
605 |
apply (case_tac bin rule: bin_exhaust) |
|
606 |
apply (auto simp: bintrunc_bintrunc_l split: bit.splits) |
|
607 |
done |
|
608 |
||
609 |
lemma bintrunc_rest': |
|
610 |
"bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n" |
|
611 |
by (rule ext) auto |
|
612 |
||
613 |
lemma sbintrunc_rest' : |
|
614 |
"sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n" |
|
615 |
by (rule ext) auto |
|
616 |
||
617 |
lemma rco_lem: |
|
618 |
"f o g o f = g o f ==> f o (g o f) ^ n = g ^ n o f" |
|
619 |
apply (rule ext) |
|
620 |
apply (induct_tac n) |
|
621 |
apply (simp_all (no_asm)) |
|
622 |
apply (drule fun_cong) |
|
623 |
apply (unfold o_def) |
|
624 |
apply (erule trans) |
|
625 |
apply simp |
|
626 |
done |
|
627 |
||
628 |
lemma rco_alt: "(f o g) ^ n o f = f o (g o f) ^ n" |
|
629 |
apply (rule ext) |
|
630 |
apply (induct n) |
|
631 |
apply (simp_all add: o_def) |
|
632 |
done |
|
633 |
||
634 |
lemmas rco_bintr = bintrunc_rest' |
|
635 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
636 |
lemmas rco_sbintr = sbintrunc_rest' |
|
637 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
638 |
||
24364 | 639 |
subsection {* Splitting and concatenation *} |
640 |
||
641 |
consts |
|
642 |
bin_split :: "nat => int => int * int" |
|
643 |
primrec |
|
644 |
Z : "bin_split 0 w = (w, Numeral.Pls)" |
|
645 |
Suc : "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) |
|
646 |
in (w1, w2 BIT bin_last w))" |
|
647 |
||
648 |
consts |
|
649 |
bin_cat :: "int => nat => int => int" |
|
650 |
primrec |
|
651 |
Z : "bin_cat w 0 v = w" |
|
652 |
Suc : "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v" |
|
653 |
||
654 |
subsection {* Miscellaneous lemmas *} |
|
655 |
||
656 |
lemmas funpow_minus_simp = |
|
657 |
trans [OF gen_minus [where f = "power f"] funpow_Suc, standard] |
|
658 |
||
659 |
lemmas funpow_pred_simp [simp] = |
|
660 |
funpow_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
661 |
||
662 |
lemmas replicate_minus_simp = |
|
663 |
trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc, |
|
664 |
standard] |
|
665 |
||
666 |
lemmas replicate_pred_simp [simp] = |
|
667 |
replicate_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
668 |
||
669 |
lemmas power_Suc_no [simp] = power_Suc [of "number_of ?a"] |
|
670 |
||
671 |
lemmas power_minus_simp = |
|
672 |
trans [OF gen_minus [where f = "power f"] power_Suc, standard] |
|
673 |
||
674 |
lemmas power_pred_simp = |
|
675 |
power_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
676 |
lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of ?f"] |
|
677 |
||
678 |
lemma list_exhaust_size_gt0: |
|
679 |
assumes y: "\<And>a list. y = a # list \<Longrightarrow> P" |
|
680 |
shows "0 < length y \<Longrightarrow> P" |
|
681 |
apply (cases y, simp) |
|
682 |
apply (rule y) |
|
683 |
apply fastsimp |
|
684 |
done |
|
685 |
||
686 |
lemma list_exhaust_size_eq0: |
|
687 |
assumes y: "y = [] \<Longrightarrow> P" |
|
688 |
shows "length y = 0 \<Longrightarrow> P" |
|
689 |
apply (cases y) |
|
690 |
apply (rule y, simp) |
|
691 |
apply simp |
|
692 |
done |
|
693 |
||
694 |
lemma size_Cons_lem_eq: |
|
695 |
"y = xa # list ==> size y = Suc k ==> size list = k" |
|
696 |
by auto |
|
697 |
||
698 |
lemma size_Cons_lem_eq_bin: |
|
699 |
"y = xa # list ==> size y = number_of (Numeral.succ k) ==> |
|
700 |
size list = number_of k" |
|
701 |
by (auto simp: pred_def succ_def split add : split_if_asm) |
|
702 |
||
24333 | 703 |
lemmas ls_splits = |
704 |
prod.split split_split prod.split_asm split_split_asm split_if_asm |
|
705 |
||
706 |
lemma not_B1_is_B0: "y \<noteq> bit.B1 \<Longrightarrow> y = bit.B0" |
|
707 |
by (cases y) auto |
|
708 |
||
709 |
lemma B1_ass_B0: |
|
710 |
assumes y: "y = bit.B0 \<Longrightarrow> y = bit.B1" |
|
711 |
shows "y = bit.B1" |
|
712 |
apply (rule classical) |
|
713 |
apply (drule not_B1_is_B0) |
|
714 |
apply (erule y) |
|
715 |
done |
|
716 |
||
717 |
-- "simplifications for specific word lengths" |
|
718 |
lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc' |
|
719 |
||
720 |
lemmas s2n_ths = n2s_ths [symmetric] |
|
721 |
||
722 |
||
723 |
end |