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