34 by (cases b) (simp_all add: Bit_def) |
34 by (cases b) (simp_all add: Bit_def) |
35 |
35 |
36 lemma power_BIT: "2 ^ Suc n - 1 = (2 ^ n - 1) BIT True" |
36 lemma power_BIT: "2 ^ Suc n - 1 = (2 ^ n - 1) BIT True" |
37 by (simp add: Bit_B1) |
37 by (simp add: Bit_B1) |
38 |
38 |
39 definition bin_last :: "int \<Rightarrow> bool" |
39 abbreviation (input) bin_last :: "int \<Rightarrow> bool" |
40 where "bin_last w \<longleftrightarrow> w mod 2 = 1" |
40 where "bin_last \<equiv> odd" |
41 |
41 |
42 lemma bin_last_odd: "bin_last = odd" |
42 lemma bin_last_def: |
43 by (rule ext) (simp add: bin_last_def even_iff_mod_2_eq_zero) |
43 "bin_last w \<longleftrightarrow> w mod 2 = 1" |
|
44 by (fact odd_iff_mod_2_eq_one) |
44 |
45 |
45 definition bin_rest :: "int \<Rightarrow> int" |
46 definition bin_rest :: "int \<Rightarrow> int" |
46 where "bin_rest w = w div 2" |
47 where "bin_rest w = w div 2" |
47 |
48 |
48 lemma bin_rl_simp [simp]: "bin_rest w BIT bin_last w = w" |
49 lemma bin_rl_simp [simp]: "bin_rest w BIT bin_last w = w" |
51 |
52 |
52 lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x" |
53 lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x" |
53 unfolding bin_rest_def Bit_def |
54 unfolding bin_rest_def Bit_def |
54 by (cases b) simp_all |
55 by (cases b) simp_all |
55 |
56 |
|
57 lemma even_BIT [simp]: "even (x BIT b) \<longleftrightarrow> \<not> b" |
|
58 by (simp add: Bit_def) |
|
59 |
56 lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b" |
60 lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b" |
57 unfolding bin_last_def Bit_def |
61 by simp |
58 by (cases b) simp_all |
|
59 |
62 |
60 lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c" |
63 lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c" |
61 by (auto simp: Bit_def) arith+ |
64 by (auto simp: Bit_def) arith+ |
62 |
65 |
63 lemma BIT_bin_simps [simp]: |
66 lemma BIT_bin_simps [simp]: |
232 by (cases n) auto |
235 by (cases n) auto |
233 |
236 |
234 lemma bin_to_bl_aux_Bit0_minus_simp [simp]: |
237 lemma bin_to_bl_aux_Bit0_minus_simp [simp]: |
235 "0 < n \<Longrightarrow> |
238 "0 < n \<Longrightarrow> |
236 bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)" |
239 bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)" |
237 by (cases n) auto |
240 by (cases n) |
|
241 (simp_all only: bin_to_bl_aux.simps bin_last_numeral_simps, simp) |
238 |
242 |
239 lemma bin_to_bl_aux_Bit1_minus_simp [simp]: |
243 lemma bin_to_bl_aux_Bit1_minus_simp [simp]: |
240 "0 < n \<Longrightarrow> |
244 "0 < n \<Longrightarrow> |
241 bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)" |
245 bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)" |
242 by (cases n) auto |
246 by (cases n) |
|
247 (simp_all only: bin_to_bl_aux.simps bin_last_numeral_simps, simp) |
243 |
248 |
244 lemma bl_to_bin_aux_append: "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)" |
249 lemma bl_to_bin_aux_append: "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)" |
245 by (induct bs arbitrary: w) auto |
250 by (induct bs arbitrary: w) auto |
246 |
251 |
247 lemma bin_to_bl_aux_append: "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)" |
252 lemma bin_to_bl_aux_append: "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)" |
406 done |
411 done |
407 |
412 |
408 lemma bin_nth_numeral_unfold: |
413 lemma bin_nth_numeral_unfold: |
409 "bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)" |
414 "bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)" |
410 "bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))" |
415 "bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))" |
411 by(case_tac [!] n) simp_all |
416 by (cases n; simp)+ |
412 |
417 |
413 |
418 |
414 subsection \<open>Truncating\<close> |
419 subsection \<open>Truncating\<close> |
415 |
420 |
416 definition bin_sign :: "int \<Rightarrow> int" |
421 definition bin_sign :: "int \<Rightarrow> int" |
443 |
448 |
444 lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n" |
449 lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n" |
445 proof (induction n arbitrary: w) |
450 proof (induction n arbitrary: w) |
446 case 0 |
451 case 0 |
447 then show ?case |
452 then show ?case |
448 by (auto simp add: bin_last_odd odd_iff_mod_2_eq_one) |
453 by (auto simp add: odd_iff_mod_2_eq_one) |
449 next |
454 next |
450 case (Suc n) |
455 case (Suc n) |
451 moreover have "((bin_rest w + 2 ^ n) mod (2 * 2 ^ n) - 2 ^ n) BIT bin_last w = |
456 moreover have "((bin_rest w + 2 ^ n) mod (2 * 2 ^ n) - 2 ^ n) BIT bin_last w = |
452 (w + 2 * 2 ^ n) mod (4 * 2 ^ n) - 2 * 2 ^ n" |
457 (w + 2 * 2 ^ n) mod (4 * 2 ^ n) - 2 * 2 ^ n" |
453 proof (cases w rule: parity_cases) |
458 proof (cases w rule: parity_cases) |
454 case even |
459 case even |
455 then show ?thesis |
460 then show ?thesis |
456 by (simp add: bin_last_odd bin_rest_def Bit_B0_2t mult_mod_right) |
461 by (simp add: bin_rest_def Bit_B0_2t mult_mod_right) |
457 next |
462 next |
458 case odd |
463 case odd |
459 then have "2 * (w div 2) = w - 1" |
464 then have "2 * (w div 2) = w - 1" |
460 using minus_mod_eq_mult_div [of w 2] by simp |
465 using minus_mod_eq_mult_div [of w 2] by simp |
461 moreover have "(2 * 2 ^ n + w - 1) mod (2 * 2 * 2 ^ n) + 1 = (2 * 2 ^ n + w) mod (2 * 2 * 2 ^ n)" |
466 moreover have "(2 * 2 ^ n + w - 1) mod (2 * 2 * 2 ^ n) + 1 = (2 * 2 ^ n + w) mod (2 * 2 * 2 ^ n)" |
462 using odd emep1 [of "2 * 2 ^ n + w - 1" "2 * 2 * 2 ^ n"] by simp |
467 using odd emep1 [of "2 * 2 ^ n + w - 1" "2 * 2 * 2 ^ n"] by simp |
463 ultimately show ?thesis |
468 ultimately show ?thesis |
464 using odd by (simp add: bin_last_odd bin_rest_def Bit_B1_2t mult_mod_right) (simp add: algebra_simps) |
469 using odd by (simp add: bin_rest_def Bit_B1_2t mult_mod_right) (simp add: algebra_simps) |
465 qed |
470 qed |
466 ultimately show ?case |
471 ultimately show ?case |
467 by simp |
472 by simp |
468 qed |
473 qed |
469 |
474 |
1493 |
1498 |
1494 text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close> |
1499 text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close> |
1495 |
1500 |
1496 lemma bin_rest_neg_numeral_BitM [simp]: |
1501 lemma bin_rest_neg_numeral_BitM [simp]: |
1497 "bin_rest (- numeral (Num.BitM w)) = - numeral w" |
1502 "bin_rest (- numeral (Num.BitM w)) = - numeral w" |
1498 by (simp only: BIT_bin_simps [symmetric] bin_rest_BIT) |
1503 by (simp flip: BIT_bin_simps) |
1499 |
1504 |
1500 lemma bin_last_neg_numeral_BitM [simp]: |
1505 lemma bin_last_neg_numeral_BitM [simp]: |
1501 "bin_last (- numeral (Num.BitM w))" |
1506 "bin_last (- numeral (Num.BitM w))" |
1502 by (simp only: BIT_bin_simps [symmetric] bin_last_BIT) |
1507 by simp |
1503 |
1508 |
1504 (* FIXME: The rule sets below are very large (24 rules for each |
1509 (* FIXME: The rule sets below are very large (24 rules for each |
1505 operator). Is there a simpler way to do this? *) |
1510 operator). Is there a simpler way to do this? *) |
1506 |
1511 |
1507 lemma int_and_numerals [simp]: |
1512 lemma int_and_numerals [simp]: |
1926 |
1931 |
1927 lemma int_xor_ge0 [simp]: fixes x y :: int shows |
1932 lemma int_xor_ge0 [simp]: fixes x y :: int shows |
1928 "x XOR y \<ge> 0 \<longleftrightarrow> ((x \<ge> 0) \<longleftrightarrow> (y \<ge> 0))" |
1933 "x XOR y \<ge> 0 \<longleftrightarrow> ((x \<ge> 0) \<longleftrightarrow> (y \<ge> 0))" |
1929 by (metis int_xor_lt0 linorder_not_le) |
1934 by (metis int_xor_lt0 linorder_not_le) |
1930 |
1935 |
|
1936 lemma even_conv_AND: |
|
1937 \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int |
|
1938 proof - |
|
1939 obtain x b where \<open>i = x BIT b\<close> |
|
1940 by (cases i rule: bin_exhaust) |
|
1941 then have "i AND 1 = 0 BIT b" |
|
1942 by (simp add: BIT_special_simps(2) [symmetric] del: BIT_special_simps(2)) |
|
1943 then show ?thesis |
|
1944 using \<open>i = x BIT b\<close> by (cases b) simp_all |
|
1945 qed |
|
1946 |
1931 lemma bin_last_conv_AND: |
1947 lemma bin_last_conv_AND: |
1932 "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0" |
1948 "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0" |
1933 proof - |
1949 by (simp add: even_conv_AND) |
1934 obtain x b where "i = x BIT b" by(cases i rule: bin_exhaust) |
|
1935 hence "i AND 1 = 0 BIT b" |
|
1936 by(simp add: BIT_special_simps(2)[symmetric] del: BIT_special_simps(2)) |
|
1937 thus ?thesis using \<open>i = x BIT b\<close> by(cases b) simp_all |
|
1938 qed |
|
1939 |
1950 |
1940 lemma bitval_bin_last: |
1951 lemma bitval_bin_last: |
1941 "of_bool (bin_last i) = i AND 1" |
1952 "of_bool (bin_last i) = i AND 1" |
1942 proof - |
1953 proof - |
1943 obtain x b where "i = x BIT b" by(cases i rule: bin_exhaust) |
1954 obtain x b where "i = x BIT b" by(cases i rule: bin_exhaust) |
1979 "lsb (- Numeral1 :: int) = True" |
1990 "lsb (- Numeral1 :: int) = True" |
1980 "lsb (numeral (num.Bit0 w) :: int) = False" |
1991 "lsb (numeral (num.Bit0 w) :: int) = False" |
1981 "lsb (numeral (num.Bit1 w) :: int) = True" |
1992 "lsb (numeral (num.Bit1 w) :: int) = True" |
1982 "lsb (- numeral (num.Bit0 w) :: int) = False" |
1993 "lsb (- numeral (num.Bit0 w) :: int) = False" |
1983 "lsb (- numeral (num.Bit1 w) :: int) = True" |
1994 "lsb (- numeral (num.Bit1 w) :: int) = True" |
1984 by(simp_all add: lsb_int_def) |
1995 by (simp_all add: lsb_int_def) |
1985 |
1996 |
1986 lemma int_set_bit_0 [simp]: fixes x :: int shows |
1997 lemma int_set_bit_0 [simp]: fixes x :: int shows |
1987 "set_bit x 0 b = bin_rest x BIT b" |
1998 "set_bit x 0 b = bin_rest x BIT b" |
1988 by(auto simp add: set_bit_int_def intro: bin_rl_eqI) |
1999 by(auto simp add: set_bit_int_def intro: bin_rl_eqI) |
1989 |
2000 |
2142 shows "bin_nth (x - y) m = bin_nth x m" |
2153 shows "bin_nth (x - y) m = bin_nth x m" |
2143 using sign m x unfolding y |
2154 using sign m x unfolding y |
2144 proof(induction m arbitrary: x y n) |
2155 proof(induction m arbitrary: x y n) |
2145 case 0 |
2156 case 0 |
2146 thus ?case |
2157 thus ?case |
2147 by(simp add: bin_last_def shiftl_int_def) (metis (hide_lams, no_types) mod_diff_right_eq mod_self neq0_conv numeral_One power_eq_0_iff power_mod diff_zero zero_neq_numeral) |
2158 by (simp add: bin_last_def shiftl_int_def) |
2148 next |
2159 next |
2149 case (Suc m) |
2160 case (Suc m) |
2150 from \<open>Suc m < n\<close> obtain n' where [simp]: "n = Suc n'" by(cases n) auto |
2161 from \<open>Suc m < n\<close> obtain n' where [simp]: "n = Suc n'" by(cases n) auto |
2151 obtain x' b where [simp]: "x = x' BIT b" by(cases x rule: bin_exhaust) |
2162 obtain x' b where [simp]: "x = x' BIT b" by(cases x rule: bin_exhaust) |
2152 from \<open>bin_sign x = 0\<close> have "bin_sign x' = 0" by simp |
2163 from \<open>bin_sign x = 0\<close> have "bin_sign x' = 0" by simp |
2158 by(cases b)(simp_all add: Bit_def bin_rest_def shiftl_int_def) |
2169 by(cases b)(simp_all add: Bit_def bin_rest_def shiftl_int_def) |
2159 qed |
2170 qed |
2160 |
2171 |
2161 lemma bin_clr_conv_NAND: |
2172 lemma bin_clr_conv_NAND: |
2162 "bin_sc n False i = i AND NOT (1 << n)" |
2173 "bin_sc n False i = i AND NOT (1 << n)" |
2163 by(induct n arbitrary: i)(auto intro: bin_rl_eqI) |
2174 by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+ |
2164 |
2175 |
2165 lemma bin_set_conv_OR: |
2176 lemma bin_set_conv_OR: |
2166 "bin_sc n True i = i OR (1 << n)" |
2177 "bin_sc n True i = i OR (1 << n)" |
2167 by(induct n arbitrary: i)(auto intro: bin_rl_eqI) |
2178 by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+ |
2168 |
2179 |
2169 lemma msb_conv_bin_sign: "msb x \<longleftrightarrow> bin_sign x = -1" |
2180 lemma msb_conv_bin_sign: "msb x \<longleftrightarrow> bin_sign x = -1" |
2170 by(simp add: bin_sign_def not_le msb_int_def) |
2181 by(simp add: bin_sign_def not_le msb_int_def) |
2171 |
2182 |
2172 lemma msb_BIT [simp]: "msb (x BIT b) = msb x" |
2183 lemma msb_BIT [simp]: "msb (x BIT b) = msb x" |