6 Copyright 1996 University of Twente |
6 Copyright 1996 University of Twente |
7 *) |
7 *) |
8 |
8 |
9 header{*Arithmetic on Binary Integers*} |
9 header{*Arithmetic on Binary Integers*} |
10 |
10 |
11 theory Bin = Int + Numeral: |
11 theory Bin = IntDef + Numeral: |
12 |
12 |
13 text{*The sign @{term Pls} stands for an infinite string of leading Falses.*} |
13 text{*The sign @{term Pls} stands for an infinite string of leading Falses.*} |
14 text{*The sign @{term Min} stands for an infinite string of leading Trues.*} |
14 text{*The sign @{term Min} stands for an infinite string of leading Trues.*} |
15 |
15 |
16 text{*A number can have multiple representations, namely leading Falses with |
16 text{*A number can have multiple representations, namely leading Falses with |
256 |
256 |
257 (** Equals (=) **) |
257 (** Equals (=) **) |
258 |
258 |
259 lemma eq_number_of_eq: |
259 lemma eq_number_of_eq: |
260 "((number_of x::int) = number_of y) = |
260 "((number_of x::int) = number_of y) = |
261 iszero (number_of (bin_add x (bin_minus y)))" |
261 iszero (number_of (bin_add x (bin_minus y)) :: int)" |
262 apply (unfold iszero_def) |
262 apply (unfold iszero_def) |
263 apply (simp add: compare_rls number_of_add number_of_minus) |
263 apply (simp add: compare_rls number_of_add number_of_minus) |
264 done |
264 done |
265 |
265 |
266 lemma iszero_number_of_Pls: "iszero ((number_of bin.Pls)::int)" |
266 lemma iszero_number_of_Pls: "iszero ((number_of bin.Pls)::int)" |
270 apply (unfold iszero_def) |
270 apply (unfold iszero_def) |
271 apply (simp add: eq_commute) |
271 apply (simp add: eq_commute) |
272 done |
272 done |
273 |
273 |
274 lemma iszero_number_of_BIT: |
274 lemma iszero_number_of_BIT: |
275 "iszero (number_of (w BIT x)) = (~x & iszero (number_of w::int))" |
275 "iszero (number_of (w BIT x)::int) = (~x & iszero (number_of w::int))" |
276 apply (unfold iszero_def) |
276 apply (unfold iszero_def) |
277 apply (cases "(number_of w)::int" rule: int_cases) |
277 apply (cases "(number_of w)::int" rule: int_cases) |
278 apply (simp_all (no_asm_simp) add: compare_rls zero_reorient |
278 apply (simp_all (no_asm_simp) add: compare_rls zero_reorient |
279 zminus_zadd_distrib [symmetric] int_Suc0_eq_1 [symmetric] zadd_int) |
279 zminus_zadd_distrib [symmetric] int_Suc0_eq_1 [symmetric] zadd_int) |
280 done |
280 done |
281 |
281 |
282 lemma iszero_number_of_0: "iszero (number_of (w BIT False)) = iszero (number_of w::int)" |
282 lemma iszero_number_of_0: |
|
283 "iszero (number_of (w BIT False)::int) = iszero (number_of w::int)" |
283 by (simp only: iszero_number_of_BIT simp_thms) |
284 by (simp only: iszero_number_of_BIT simp_thms) |
284 |
285 |
285 lemma iszero_number_of_1: "~ iszero (number_of (w BIT True)::int)" |
286 lemma iszero_number_of_1: "~ iszero (number_of (w BIT True)::int)" |
286 by (simp only: iszero_number_of_BIT simp_thms) |
287 by (simp only: iszero_number_of_BIT simp_thms) |
287 |
288 |
289 |
290 |
290 (** Less-than (<) **) |
291 (** Less-than (<) **) |
291 |
292 |
292 lemma less_number_of_eq_neg: |
293 lemma less_number_of_eq_neg: |
293 "((number_of x::int) < number_of y) |
294 "((number_of x::int) < number_of y) |
294 = neg (number_of (bin_add x (bin_minus y)))" |
295 = neg (number_of (bin_add x (bin_minus y)) ::int )" |
295 |
296 by (simp add: neg_def number_of_add number_of_minus compare_rls) |
296 apply (unfold zless_def zdiff_def) |
297 |
297 apply (simp add: bin_mult_simps) |
|
298 done |
|
299 |
298 |
300 (*But if Numeral0 is rewritten to 0 then this rule can't be applied: |
299 (*But if Numeral0 is rewritten to 0 then this rule can't be applied: |
301 Numeral0 IS (number_of Pls) *) |
300 Numeral0 IS (number_of Pls) *) |
302 lemma not_neg_number_of_Pls: "~ neg (number_of bin.Pls)" |
301 lemma not_neg_number_of_Pls: "~ neg (number_of bin.Pls ::int)" |
303 by (simp add: neg_eq_less_0) |
302 by (simp add: neg_def) |
304 |
303 |
305 lemma neg_number_of_Min: "neg (number_of bin.Min)" |
304 lemma neg_number_of_Min: "neg (number_of bin.Min ::int)" |
306 by (simp add: neg_eq_less_0 int_0_less_1) |
305 by (simp add: neg_def int_0_less_1) |
307 |
306 |
308 lemma neg_number_of_BIT: "neg (number_of (w BIT x)) = neg (number_of w)" |
307 lemma neg_number_of_BIT: |
|
308 "neg (number_of (w BIT x)::int) = neg (number_of w ::int)" |
309 apply simp |
309 apply simp |
310 apply (cases "(number_of w)::int" rule: int_cases) |
310 apply (cases "(number_of w)::int" rule: int_cases) |
311 apply (simp_all (no_asm_simp) add: int_Suc0_eq_1 [symmetric] zadd_int neg_eq_less_0 zdiff_def [symmetric] compare_rls) |
311 apply (simp_all (no_asm_simp) add: int_Suc0_eq_1 [symmetric] zadd_int neg_def zdiff_def [symmetric] compare_rls) |
312 done |
312 done |
313 |
313 |
314 |
314 |
315 (** Less-than-or-equals (\<le>) **) |
315 (** Less-than-or-equals (\<le>) **) |
316 |
316 |
324 (** Absolute value (abs) **) |
324 (** Absolute value (abs) **) |
325 |
325 |
326 lemma zabs_number_of: |
326 lemma zabs_number_of: |
327 "abs(number_of x::int) = |
327 "abs(number_of x::int) = |
328 (if number_of x < (0::int) then -number_of x else number_of x)" |
328 (if number_of x < (0::int) then -number_of x else number_of x)" |
329 apply (unfold zabs_def) |
329 by (simp add: zabs_def) |
330 apply (rule refl) |
|
331 done |
|
332 |
330 |
333 (*0 and 1 require special rewrites because they aren't numerals*) |
331 (*0 and 1 require special rewrites because they aren't numerals*) |
334 lemma zabs_0: "abs (0::int) = 0" |
332 lemma zabs_0: "abs (0::int) = 0" |
335 by (simp add: zabs_def) |
333 by (simp add: zabs_def) |
336 |
334 |