diff -r 74440d820ba5 -r 127ba61b2630 src/HOL/Bit_Operations.thy --- a/src/HOL/Bit_Operations.thy Tue Nov 21 19:19:16 2023 +0000 +++ b/src/HOL/Bit_Operations.thy Tue Nov 21 19:19:16 2023 +0000 @@ -72,9 +72,8 @@ lemma even_succ_mod_exp [simp]: \(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\ if \even a\ and \n > 0\ - using div_mult_mod_eq [of \1 + a\ \2 ^ n\] that - apply simp - by (metis local.add.left_commute local.add_left_cancel local.div_mult_mod_eq) + using div_mult_mod_eq [of \1 + a\ \2 ^ n\] div_mult_mod_eq [of a \2 ^ n\] that + by simp (metis (full_types) add.left_commute add_left_imp_eq) lemma bits_mod_by_1 [simp]: \a mod 1 = 0\ @@ -155,33 +154,33 @@ \\ bit a n\ if \2 ^ n = 0\ using that by (simp add: bit_iff_odd) -definition - possible_bit :: "'a itself \ nat \ bool" - where - "possible_bit tyrep n = (2 ^ n \ (0 :: 'a))" - -lemma possible_bit_0[simp]: - "possible_bit ty 0" +definition possible_bit :: \'a itself \ nat \ bool\ + where \possible_bit TYPE('a) n \ 2 ^ n \ 0\ + +lemma possible_bit_0 [simp]: + \possible_bit TYPE('a) 0\ by (simp add: possible_bit_def) lemma fold_possible_bit: - "2 ^ n = (0 :: 'a) \ \ possible_bit TYPE('a) n" + \2 ^ n = 0 \ \ possible_bit TYPE('a) n\ by (simp add: possible_bit_def) -lemmas impossible_bit = exp_eq_0_imp_not_bit[simplified fold_possible_bit] - lemma bit_imp_possible_bit: - "bit a n \ possible_bit TYPE('a) n" - by (rule ccontr) (simp add: impossible_bit) + \possible_bit TYPE('a) n\ if \bit a n\ + using that by (auto simp add: possible_bit_def exp_eq_0_imp_not_bit) + +lemma impossible_bit: + \\ bit a n\ if \\ possible_bit TYPE('a) n\ + using that by (blast dest: bit_imp_possible_bit) lemma possible_bit_less_imp: - "possible_bit tyrep i \ j \ i \ possible_bit tyrep j" - using power_add[of "2 :: 'a" j "i - j"] - by (clarsimp simp: possible_bit_def eq_commute[where a=0]) - -lemma possible_bit_min[simp]: - "possible_bit tyrep (min i j) \ possible_bit tyrep i \ possible_bit tyrep j" - by (auto simp: min_def elim: possible_bit_less_imp) + \possible_bit TYPE('a) j\ if \possible_bit TYPE('a) i\ \j \ i\ + using power_add [of 2 j \i - j\] that mult_not_zero [of \2 ^ j\ \2 ^ (i - j)\] + by (simp add: possible_bit_def) + +lemma possible_bit_min [simp]: + \possible_bit TYPE('a) (min i j) \ possible_bit TYPE('a) i \ possible_bit TYPE('a) j\ + by (auto simp add: min_def elim: possible_bit_less_imp) lemma bit_eqI: \a = b\ if \\n. possible_bit TYPE('a) n \ bit a n \ bit b n\ @@ -389,32 +388,36 @@ by simp qed +lemma bit_of_bool_iff [bit_simps]: + \bit (of_bool b) n \ b \ n = 0\ + by (simp add: bit_1_iff) + end lemma nat_bit_induct [case_names zero even odd]: - "P n" if zero: "P 0" - and even: "\n. P n \ n > 0 \ P (2 * n)" - and odd: "\n. P n \ P (Suc (2 * n))" + \P n\ if zero: \P 0\ + and even: \\n. P n \ n > 0 \ P (2 * n)\ + and odd: \\n. P n \ P (Suc (2 * n))\ proof (induction n rule: less_induct) case (less n) - show "P n" - proof (cases "n = 0") + show \P n\ + proof (cases \n = 0\) case True with zero show ?thesis by simp next case False - with less have hyp: "P (n div 2)" by simp + with less have hyp: \P (n div 2)\ by simp show ?thesis - proof (cases "even n") + proof (cases \even n\) case True - then have "n \ 1" + then have \n \ 1\ by auto - with \n \ 0\ have "n div 2 > 0" + with \n \ 0\ have \n div 2 > 0\ by simp - with \even n\ hyp even [of "n div 2"] show ?thesis + with \even n\ hyp even [of \n div 2\] show ?thesis by simp next case False - with hyp odd [of "n div 2"] show ?thesis + with hyp odd [of \n div 2\] show ?thesis by simp qed qed @@ -466,8 +469,8 @@ end -lemma possible_bit_nat[simp]: - "possible_bit TYPE(nat) n" +lemma possible_bit_nat [simp]: + \possible_bit TYPE(nat) n\ by (simp add: possible_bit_def) lemma not_bit_Suc_0_Suc [simp]: @@ -478,70 +481,9 @@ \\ bit (Suc 0) (numeral n)\ by (simp add: numeral_eq_Suc) -lemma int_bit_induct [case_names zero minus even odd]: - "P k" if zero_int: "P 0" - and minus_int: "P (- 1)" - and even_int: "\k. P k \ k \ 0 \ P (k * 2)" - and odd_int: "\k. P k \ k \ - 1 \ P (1 + (k * 2))" for k :: int -proof (cases "k \ 0") - case True - define n where "n = nat k" - with True have "k = int n" - by simp - then show "P k" - proof (induction n arbitrary: k rule: nat_bit_induct) - case zero - then show ?case - by (simp add: zero_int) - next - case (even n) - have "P (int n * 2)" - by (rule even_int) (use even in simp_all) - with even show ?case - by (simp add: ac_simps) - next - case (odd n) - have "P (1 + (int n * 2))" - by (rule odd_int) (use odd in simp_all) - with odd show ?case - by (simp add: ac_simps) - qed -next - case False - define n where "n = nat (- k - 1)" - with False have "k = - int n - 1" - by simp - then show "P k" - proof (induction n arbitrary: k rule: nat_bit_induct) - case zero - then show ?case - by (simp add: minus_int) - next - case (even n) - have "P (1 + (- int (Suc n) * 2))" - by (rule odd_int) (use even in \simp_all add: algebra_simps\) - also have "\ = - int (2 * n) - 1" - by (simp add: algebra_simps) - finally show ?case - using even.prems by simp - next - case (odd n) - have "P (- int (Suc n) * 2)" - by (rule even_int) (use odd in \simp_all add: algebra_simps\) - also have "\ = - int (Suc (2 * n)) - 1" - by (simp add: algebra_simps) - finally show ?case - using odd.prems by simp - qed -qed - context semiring_bits begin -lemma bit_of_bool_iff [bit_simps]: - \bit (of_bool b) n \ b \ n = 0\ - by (simp add: bit_1_iff) - lemma bit_of_nat_iff [bit_simps]: \bit (of_nat m) n \ possible_bit TYPE('a) n \ bit m n\ proof (cases \(2::'a) ^ n = 0\) @@ -573,6 +515,63 @@ end +lemma int_bit_induct [case_names zero minus even odd]: + \P k\ if zero_int: \P 0\ + and minus_int: \P (- 1)\ + and even_int: \\k. P k \ k \ 0 \ P (k * 2)\ + and odd_int: \\k. P k \ k \ - 1 \ P (1 + (k * 2))\ for k :: int +proof (cases \k \ 0\) + case True + define n where \n = nat k\ + with True have \k = int n\ + by simp + then show \P k\ + proof (induction n arbitrary: k rule: nat_bit_induct) + case zero + then show ?case + by (simp add: zero_int) + next + case (even n) + have \P (int n * 2)\ + by (rule even_int) (use even in simp_all) + with even show ?case + by (simp add: ac_simps) + next + case (odd n) + have \P (1 + (int n * 2))\ + by (rule odd_int) (use odd in simp_all) + with odd show ?case + by (simp add: ac_simps) + qed +next + case False + define n where \n = nat (- k - 1)\ + with False have \k = - int n - 1\ + by simp + then show \P k\ + proof (induction n arbitrary: k rule: nat_bit_induct) + case zero + then show ?case + by (simp add: minus_int) + next + case (even n) + have \P (1 + (- int (Suc n) * 2))\ + by (rule odd_int) (use even in \simp_all add: algebra_simps\) + also have \\ = - int (2 * n) - 1\ + by (simp add: algebra_simps) + finally show ?case + using even.prems by simp + next + case (odd n) + have \P (- int (Suc n) * 2)\ + by (rule even_int) (use odd in \simp_all add: algebra_simps\) + also have \\ = - int (Suc (2 * n)) - 1\ + by (simp add: algebra_simps) + finally show ?case + using odd.prems by simp + qed +qed + instantiation int :: semiring_bits begin @@ -644,13 +643,12 @@ end -lemma possible_bit_int[simp]: - "possible_bit TYPE(int) n" +lemma possible_bit_int [simp]: + \possible_bit TYPE(int) n\ by (simp add: possible_bit_def) lemma bit_not_int_iff': - \bit (- k - 1) n \ \ bit k n\ - for k :: int + \bit (- k - 1) n \ \ bit k n\ for k :: int proof (induction n arbitrary: k) case 0 show ?case @@ -857,35 +855,35 @@ by (simp add: push_bit_eq_mult drop_bit_eq_div) lemma bits_ident: - "push_bit n (drop_bit n a) + take_bit n a = a" + \push_bit n (drop_bit n a) + take_bit n a = a\ using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div) lemma push_bit_push_bit [simp]: - "push_bit m (push_bit n a) = push_bit (m + n) a" + \push_bit m (push_bit n a) = push_bit (m + n) a\ by (simp add: push_bit_eq_mult power_add ac_simps) lemma push_bit_0_id [simp]: - "push_bit 0 = id" + \push_bit 0 = id\ by (simp add: fun_eq_iff push_bit_eq_mult) lemma push_bit_of_0 [simp]: - "push_bit n 0 = 0" + \push_bit n 0 = 0\ by (simp add: push_bit_eq_mult) lemma push_bit_of_1 [simp]: - "push_bit n 1 = 2 ^ n" + \push_bit n 1 = 2 ^ n\ by (simp add: push_bit_eq_mult) lemma push_bit_Suc [simp]: - "push_bit (Suc n) a = push_bit n (a * 2)" + \push_bit (Suc n) a = push_bit n (a * 2)\ by (simp add: push_bit_eq_mult ac_simps) lemma push_bit_double: - "push_bit n (a * 2) = push_bit n a * 2" + \push_bit n (a * 2) = push_bit n a * 2\ by (simp add: push_bit_eq_mult ac_simps) lemma push_bit_add: - "push_bit n (a + b) = push_bit n a + push_bit n b" + \push_bit n (a + b) = push_bit n a + push_bit n b\ by (simp add: push_bit_eq_mult algebra_simps) lemma push_bit_numeral [simp]: @@ -916,39 +914,39 @@ by (simp add: take_bit_eq_mod) lemma take_bit_of_0 [simp]: - "take_bit n 0 = 0" + \take_bit n 0 = 0\ by (simp add: take_bit_eq_mod) lemma take_bit_of_1 [simp]: - "take_bit n 1 = of_bool (n > 0)" + \take_bit n 1 = of_bool (n > 0)\ by (cases n) (simp_all add: take_bit_Suc) lemma drop_bit_of_0 [simp]: - "drop_bit n 0 = 0" + \drop_bit n 0 = 0\ by (simp add: drop_bit_eq_div) lemma drop_bit_of_1 [simp]: - "drop_bit n 1 = of_bool (n = 0)" + \drop_bit n 1 = of_bool (n = 0)\ by (simp add: drop_bit_eq_div) lemma drop_bit_0 [simp]: - "drop_bit 0 = id" + \drop_bit 0 = id\ by (simp add: fun_eq_iff drop_bit_eq_div) lemma drop_bit_Suc: - "drop_bit (Suc n) a = drop_bit n (a div 2)" + \drop_bit (Suc n) a = drop_bit n (a div 2)\ using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div) lemma drop_bit_rec: - "drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))" + \drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))\ by (cases n) (simp_all add: drop_bit_Suc) lemma drop_bit_half: - "drop_bit n (a div 2) = drop_bit n a div 2" + \drop_bit n (a div 2) = drop_bit n a div 2\ by (induction n arbitrary: a) (simp_all add: drop_bit_Suc) lemma drop_bit_of_bool [simp]: - "drop_bit n (of_bool b) = of_bool (n = 0 \ b)" + \drop_bit n (of_bool b) = of_bool (n = 0 \ b)\ by (cases n) simp_all lemma even_take_bit_eq [simp]: @@ -956,22 +954,22 @@ by (simp add: take_bit_rec [of n a]) lemma take_bit_take_bit [simp]: - "take_bit m (take_bit n a) = take_bit (min m n) a" + \take_bit m (take_bit n a) = take_bit (min m n) a\ by (simp add: take_bit_eq_mod mod_exp_eq ac_simps) lemma drop_bit_drop_bit [simp]: - "drop_bit m (drop_bit n a) = drop_bit (m + n) a" + \drop_bit m (drop_bit n a) = drop_bit (m + n) a\ by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps) lemma push_bit_take_bit: - "push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)" + \push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)\ apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps) using mult_exp_mod_exp_eq [of m \m + n\ a] apply (simp add: ac_simps power_add) done lemma take_bit_push_bit: - "take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)" -proof (cases "m \ n") + \take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)\ +proof (cases \m \ n\) case True then show ?thesis apply (simp add:) @@ -983,16 +981,16 @@ next case False then show ?thesis - using push_bit_take_bit [of n "m - n" a] + using push_bit_take_bit [of n \m - n\ a] by simp qed lemma take_bit_drop_bit: - "take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)" + \take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)\ by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq) lemma drop_bit_take_bit: - "drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)" + \drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)\ proof (cases "m \ n") case True then show ?thesis @@ -1197,11 +1195,9 @@ apply (simp_all add: bit_exp_iff) done -lemmas set_bit_def = set_bit_eq_or - lemma bit_set_bit_iff [bit_simps]: \bit (set_bit m a) n \ bit a n \ (m = n \ possible_bit TYPE('a) n)\ - by (auto simp add: set_bit_def bit_or_iff bit_exp_iff) + by (auto simp add: set_bit_eq_or bit_or_iff bit_exp_iff) lemma even_set_bit_iff: \even (set_bit m a) \ even a \ m \ 0\ @@ -1216,8 +1212,6 @@ using bit_imp_possible_bit[of a n] by (auto simp add: bit_eq_iff bit_simps) -lemmas flip_bit_def = flip_bit_eq_xor - lemma bit_flip_bit_iff [bit_simps]: \bit (flip_bit m a) n \ (m = n \ \ bit a n) \ possible_bit TYPE('a) n\ by (auto simp add: bit_eq_iff bit_simps flip_bit_eq_xor bit_imp_possible_bit) @@ -1295,6 +1289,44 @@ \mask n = 0 \ n = 0\ by (cases n) (simp_all add: mask_Suc_double or_eq_0_iff) +lemma bit_horner_sum_bit_iff [bit_simps]: + \bit (horner_sum of_bool 2 bs) n \ possible_bit TYPE('a) n \ n < length bs \ bs ! n\ +proof (induction bs arbitrary: n) + case Nil + then show ?case + by simp +next + case (Cons b bs) + show ?case + proof (cases n) + case 0 + then show ?thesis + by (simp add: bit_0) + next + case (Suc m) + with bit_rec [of _ n] Cons.prems Cons.IH [of m] + show ?thesis + by (simp add: bit_simps) + (auto simp add: possible_bit_less_imp bit_simps simp flip: bit_Suc) + qed +qed + +lemma horner_sum_bit_eq_take_bit: + \horner_sum of_bool 2 (map (bit a) [0.. + by (rule bit_eqI) (auto simp add: bit_simps) + +lemma take_bit_horner_sum_bit_eq: + \take_bit n (horner_sum of_bool 2 bs) = horner_sum of_bool 2 (take n bs)\ + by (auto simp add: bit_eq_iff bit_take_bit_iff bit_horner_sum_bit_iff) + +lemma take_bit_sum: + \take_bit n a = (\k = 0.. + by (simp flip: horner_sum_bit_eq_take_bit add: horner_sum_eq_sum push_bit_eq_mult) + +lemmas set_bit_def = set_bit_eq_or + +lemmas flip_bit_def = flip_bit_eq_xor + end class ring_bit_operations = semiring_bit_operations + ring_parity + @@ -1303,8 +1335,6 @@ assumes minus_eq_not_minus_1: \- a = NOT (a - 1)\ begin -lemmas bit_not_iff[bit_simps] = bit_not_iff_eq[unfolded fold_possible_bit] - text \ For the sake of code generation \<^const>\not\ is specified as definitional class operation. Note that \<^const>\not\ has no @@ -1312,6 +1342,10 @@ (type \<^typ>\nat\). \ +lemma bit_not_iff [bit_simps]: + \bit (NOT a) n \ possible_bit TYPE('a) n \ \ bit a n\ + by (simp add: bit_not_iff_eq fold_possible_bit) + lemma bits_minus_1_mod_2_eq [simp]: \(- 1) mod 2 = 1\ by (simp add: mod_2_eq_odd) @@ -1448,8 +1482,6 @@ \unset_bit n a = a AND NOT (push_bit n 1)\ by (rule bit_eqI) (auto simp add: bit_simps) -lemmas unset_bit_def = unset_bit_eq_and_not - lemma push_bit_Suc_minus_numeral [simp]: \push_bit (Suc n) (- numeral k) = push_bit n (- numeral (Num.Bit0 k))\ apply (simp only: numeral_Bit0) @@ -1486,6 +1518,8 @@ \push_bit (numeral n) (- 1) = - (2 ^ numeral n)\ by (simp add: push_bit_eq_mult) +lemmas unset_bit_def = unset_bit_eq_and_not + end @@ -1521,27 +1555,15 @@ by auto termination proof (relation \measure (\(k, l). nat (\k\ + \l\))\) + have less_eq: \\k div 2\ \ \k\\ for k :: int + by (cases k) (simp_all add: divide_int_def nat_add_distrib) + then have less: \\k div 2\ < \k\\ if \k \ {0, - 1}\ for k :: int + using that by (auto simp add: less_le [of k]) show \wf (measure (\(k, l). nat (\k\ + \l\)))\ by simp show \((k div 2, l div 2), k, l) \ measure (\(k, l). nat (\k\ + \l\))\ if \\ (k \ {0, - 1} \ l \ {0, - 1})\ for k l proof - - have less_eq: \\k div 2\ \ \k\\ for k :: int - by (cases k) (simp_all add: divide_int_def nat_add_distrib) - have less: \\k div 2\ < \k\\ if \k \ {0, - 1}\ for k :: int - proof (cases k) - case (nonneg n) - with that show ?thesis - by (simp add: int_div_less_self) - next - case (neg n) - with that have \n \ 0\ - by simp - then have \n div 2 < n\ - by (simp add: div_less_iff_less_mult) - with neg that show ?thesis - by (simp add: divide_int_def nat_add_distrib) - qed from that have *: \k \ {0, - 1} \ l \ {0, - 1}\ by simp then have \0 < \k\ + \l\\ @@ -1813,10 +1835,8 @@ by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) lemma OR_upper: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ \x < 2 ^ n\ \y < 2 ^ n\ - shows \x OR y < 2 ^ n\ -using assms proof (induction x arbitrary: y n rule: int_bit_induct) + \x OR y < 2 ^ n\ if \0 \ x\ \x < 2 ^ n\ \y < 2 ^ n\ for x y :: int +using that proof (induction x arbitrary: y n rule: int_bit_induct) case zero then show ?case by simp @@ -1837,10 +1857,8 @@ qed lemma XOR_upper: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ \x < 2 ^ n\ \y < 2 ^ n\ - shows \x XOR y < 2 ^ n\ -using assms proof (induction x arbitrary: y n rule: int_bit_induct) + \x XOR y < 2 ^ n\ if \0 \ x\ \x < 2 ^ n\ \y < 2 ^ n\ for x y :: int +using that proof (induction x arbitrary: y n rule: int_bit_induct) case zero then show ?case by simp @@ -1861,28 +1879,20 @@ qed lemma AND_lower [simp]: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ - shows \0 \ x AND y\ - using assms by simp + \0 \ x AND y\ if \0 \ x\ for x y :: int + using that by simp lemma OR_lower [simp]: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ \0 \ y\ - shows \0 \ x OR y\ - using assms by simp + \0 \ x OR y\ if \0 \ x\ \0 \ y\ for x y :: int + using that by simp lemma XOR_lower [simp]: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ \0 \ y\ - shows \0 \ x XOR y\ - using assms by simp + \0 \ x XOR y\ if \0 \ x\ \0 \ y\ for x y :: int + using that by simp lemma AND_upper1 [simp]: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ x\ - shows \x AND y \ x\ -using assms proof (induction x arbitrary: y rule: int_bit_induct) + \x AND y \ x\ if \0 \ x\ for x y :: int +using that proof (induction x arbitrary: y rule: int_bit_induct) case (odd k) then have \k AND y div 2 \ k\ by simp @@ -1890,19 +1900,28 @@ by (simp add: and_int_rec [of \1 + _ * 2\]) qed (simp_all add: and_int_rec [of \_ * 2\]) -lemmas AND_upper1' [simp] = order_trans [OF AND_upper1] \<^marker>\contributor \Stefan Berghofer\\ -lemmas AND_upper1'' [simp] = order_le_less_trans [OF AND_upper1] \<^marker>\contributor \Stefan Berghofer\\ +lemma AND_upper1' [simp]: \<^marker>\contributor \Stefan Berghofer\\ + \y AND x \ z\ if \0 \ y\ \y \ z\ for x y z :: int + using _ \y \ z\ by (rule order_trans) (use \0 \ y\ in simp) + +lemma AND_upper1'' [simp]: \<^marker>\contributor \Stefan Berghofer\\ + \y AND x < z\ if \0 \ y\ \y < z\ for x y z :: int + using _ \y < z\ by (rule order_le_less_trans) (use \0 \ y\ in simp) lemma AND_upper2 [simp]: \<^marker>\contributor \Stefan Berghofer\\ - fixes x y :: int - assumes \0 \ y\ - shows \x AND y \ y\ - using assms AND_upper1 [of y x] by (simp add: ac_simps) - -lemmas AND_upper2' [simp] = order_trans [OF AND_upper2] \<^marker>\contributor \Stefan Berghofer\\ -lemmas AND_upper2'' [simp] = order_le_less_trans [OF AND_upper2] \<^marker>\contributor \Stefan Berghofer\\ - -lemma plus_and_or: \(x AND y) + (x OR y) = x + y\ for x y :: int + \x AND y \ y\ if \0 \ y\ for x y :: int + using that AND_upper1 [of y x] by (simp add: ac_simps) + +lemma AND_upper2' [simp]: \<^marker>\contributor \Stefan Berghofer\\ + \x AND y \ z\ if \0 \ y\ \y \ z\ for x y :: int + using that AND_upper1' [of y z x] by (simp add: ac_simps) + +lemma AND_upper2'' [simp]: \<^marker>\contributor \Stefan Berghofer\\ + \x AND y < z\ if \0 \ y\ \y < z\ for x y :: int + using that AND_upper1'' [of y z x] by (simp add: ac_simps) + +lemma plus_and_or: + \(x AND y) + (x OR y) = x + y\ for x y :: int proof (induction x arbitrary: y rule: int_bit_induct) case zero then show ?case @@ -1924,7 +1943,7 @@ qed lemma push_bit_minus_one: - "push_bit n (- 1 :: int) = - (2 ^ n)" + \push_bit n (- 1 :: int) = - (2 ^ n)\ by (simp add: push_bit_eq_mult) lemma minus_1_div_exp_eq_int: @@ -2103,13 +2122,11 @@ by (auto simp add: xor_int_rec [of k l] not_int_def elim!: oddE) lemma bit_minus_int_iff: - \bit (- k) n \ bit (NOT (k - 1)) n\ - for k :: int + \bit (- k) n \ bit (NOT (k - 1)) n\ for k :: int by (simp add: bit_simps) lemma take_bit_incr_eq: - \take_bit n (k + 1) = 1 + take_bit n k\ if \take_bit n k \ 2 ^ n - 1\ - for k :: int + \take_bit n (k + 1) = 1 + take_bit n k\ if \take_bit n k \ 2 ^ n - 1\ for k :: int proof - from that have \2 ^ n \ k mod 2 ^ n + 1\ by (simp add: take_bit_eq_mod) @@ -2127,8 +2144,7 @@ qed lemma take_bit_decr_eq: - \take_bit n (k - 1) = take_bit n k - 1\ if \take_bit n k \ 0\ - for k :: int + \take_bit n (k - 1) = take_bit n k - 1\ if \take_bit n k \ 0\ for k :: int proof - from that have \k mod 2 ^ n \ 0\ by (simp add: take_bit_eq_mod) @@ -2173,8 +2189,7 @@ by (simp add: take_bit_eq_mod) lemma take_bit_int_less_eq_self_iff: - \take_bit n k \ k \ 0 \ k\ (is \?P \ ?Q\) - for k :: int + \take_bit n k \ k \ 0 \ k\ (is \?P \ ?Q\) for k :: int proof assume ?P show ?Q @@ -2195,19 +2210,16 @@ qed lemma take_bit_int_less_self_iff: - \take_bit n k < k \ 2 ^ n \ k\ - for k :: int + \take_bit n k < k \ 2 ^ n \ k\ for k :: int by (auto simp add: less_le take_bit_int_less_eq_self_iff take_bit_int_eq_self_iff intro: order_trans [of 0 \2 ^ n\ k]) lemma take_bit_int_greater_self_iff: - \k < take_bit n k \ k < 0\ - for k :: int + \k < take_bit n k \ k < 0\ for k :: int using take_bit_int_less_eq_self_iff [of n k] by auto lemma take_bit_int_greater_eq_self_iff: - \k \ take_bit n k \ k < 2 ^ n\ - for k :: int + \k \ take_bit n k \ k < 2 ^ n\ for k :: int by (auto simp add: le_less take_bit_int_greater_self_iff take_bit_int_eq_self_iff dest: sym not_sym intro: less_trans [of k 0 \2 ^ n\]) @@ -2431,12 +2443,11 @@ end lemma take_bit_nat_less_exp [simp]: - \take_bit n m < 2 ^ n\ for n m ::nat + \take_bit n m < 2 ^ n\ for n m :: nat by (simp add: take_bit_eq_mod) lemma take_bit_nat_eq_self_iff: - \take_bit n m = m \ m < 2 ^ n\ (is \?P \ ?Q\) - for n m :: nat + \take_bit n m = m \ m < 2 ^ n\ (is \?P \ ?Q\) for n m :: nat proof assume ?P moreover note take_bit_nat_less_exp [of n m] @@ -2457,8 +2468,7 @@ by (simp add: take_bit_eq_mod) lemma take_bit_nat_less_self_iff: - \take_bit n m < m \ 2 ^ n \ m\ (is \?P \ ?Q\) - for m n :: nat + \take_bit n m < m \ 2 ^ n \ m\ (is \?P \ ?Q\) for m n :: nat proof assume ?P then have \take_bit n m \ m\ @@ -2637,11 +2647,6 @@ \of_nat (take_bit n m) = take_bit n (of_nat m)\ by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff) -end - -context semiring_bit_operations -begin - lemma of_nat_and_eq: \of_nat (m AND n) = of_nat m AND of_nat n\ by (rule bit_eqI) (simp add: bit_of_nat_iff bit_and_iff Bit_Operations.bit_and_iff) @@ -2684,15 +2689,15 @@ using take_bit_of_exp [of n 1] by simp lemma push_bit_eq_0_iff [simp]: - "push_bit n a = 0 \ a = 0" + \push_bit n a = 0 \ a = 0\ by (simp add: push_bit_eq_mult) lemma take_bit_add: - "take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)" + \take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)\ by (simp add: take_bit_eq_mod mod_simps) lemma take_bit_of_1_eq_0_iff [simp]: - "take_bit n 1 = 0 \ n = 0" + \take_bit n 1 = 0 \ n = 0\ by (simp add: take_bit_eq_mod) lemma drop_bit_Suc_bit0 [simp]: @@ -2758,63 +2763,12 @@ \of_nat (drop_bit m n) = drop_bit m (of_nat n)\ by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div) -lemma take_bit_sum: - "take_bit n a = (\k = 0..k = 0..k = Suc 0..k = Suc 0..k = 0..drop_bit (Suc n) (- numeral (Num.Bit0 k)) = drop_bit n (- numeral k :: int)\ - by (simp add: drop_bit_Suc numeral_Bit0_div_2) - -lemma drop_bit_Suc_minus_bit1 [simp]: - \drop_bit (Suc n) (- numeral (Num.Bit1 k)) = drop_bit n (- numeral (Num.inc k) :: int)\ - by (simp add: drop_bit_Suc numeral_Bit1_div_2 add_One) - -lemma drop_bit_numeral_minus_bit0 [simp]: - \drop_bit (numeral l) (- numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (- numeral k :: int)\ - by (simp add: numeral_eq_Suc numeral_Bit0_div_2) - -lemma drop_bit_numeral_minus_bit1 [simp]: - \drop_bit (numeral l) (- numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (- numeral (Num.inc k) :: int)\ - by (simp add: numeral_eq_Suc numeral_Bit1_div_2) - -lemma take_bit_Suc_minus_bit0: - \take_bit (Suc n) (- numeral (Num.Bit0 k)) = take_bit n (- numeral k) * (2 :: int)\ - by (simp add: take_bit_Suc numeral_Bit0_div_2) - -lemma take_bit_Suc_minus_bit1: - \take_bit (Suc n) (- numeral (Num.Bit1 k)) = take_bit n (- numeral (Num.inc k)) * 2 + (1 :: int)\ - by (simp add: take_bit_Suc numeral_Bit1_div_2 add_One) - -lemma take_bit_numeral_minus_bit0: - \take_bit (numeral l) (- numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (- numeral k) * (2 :: int)\ - by (simp add: numeral_eq_Suc numeral_Bit0_div_2 take_bit_Suc_minus_bit0) - -lemma take_bit_numeral_minus_bit1: - \take_bit (numeral l) (- numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (- numeral (Num.inc k)) * 2 + (1 :: int)\ - by (simp add: numeral_eq_Suc numeral_Bit1_div_2 take_bit_Suc_minus_bit1) - subsection \Symbolic computations on numeral expressions\ @@ -2973,6 +2927,38 @@ end +lemma drop_bit_Suc_minus_bit0 [simp]: + \drop_bit (Suc n) (- numeral (Num.Bit0 k)) = drop_bit n (- numeral k :: int)\ + by (simp add: drop_bit_Suc numeral_Bit0_div_2) + +lemma drop_bit_Suc_minus_bit1 [simp]: + \drop_bit (Suc n) (- numeral (Num.Bit1 k)) = drop_bit n (- numeral (Num.inc k) :: int)\ + by (simp add: drop_bit_Suc numeral_Bit1_div_2 add_One) + +lemma drop_bit_numeral_minus_bit0 [simp]: + \drop_bit (numeral l) (- numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (- numeral k :: int)\ + by (simp add: numeral_eq_Suc numeral_Bit0_div_2) + +lemma drop_bit_numeral_minus_bit1 [simp]: + \drop_bit (numeral l) (- numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (- numeral (Num.inc k) :: int)\ + by (simp add: numeral_eq_Suc numeral_Bit1_div_2) + +lemma take_bit_Suc_minus_bit0: + \take_bit (Suc n) (- numeral (Num.Bit0 k)) = take_bit n (- numeral k) * (2 :: int)\ + by (simp add: take_bit_Suc numeral_Bit0_div_2) + +lemma take_bit_Suc_minus_bit1: + \take_bit (Suc n) (- numeral (Num.Bit1 k)) = take_bit n (- numeral (Num.inc k)) * 2 + (1 :: int)\ + by (simp add: take_bit_Suc numeral_Bit1_div_2 add_One) + +lemma take_bit_numeral_minus_bit0: + \take_bit (numeral l) (- numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (- numeral k) * (2 :: int)\ + by (simp add: numeral_eq_Suc numeral_Bit0_div_2 take_bit_Suc_minus_bit0) + +lemma take_bit_numeral_minus_bit1: + \take_bit (numeral l) (- numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (- numeral (Num.inc k)) * 2 + (1 :: int)\ + by (simp add: numeral_eq_Suc numeral_Bit1_div_2 take_bit_Suc_minus_bit1) + lemma bit_Suc_0_iff [bit_simps]: \bit (Suc 0) n \ n = 0\ using bit_1_iff [of n, where ?'a = nat] by simp @@ -3661,83 +3647,6 @@ qed -subsection \Horner sums\ - -context semiring_bit_operations -begin - -lemma horner_sum_bit_eq_take_bit: - \horner_sum of_bool 2 (map (bit a) [0.. -proof (induction a arbitrary: n rule: bits_induct) - case (stable a) - moreover have \bit a = (\_. odd a)\ - using stable by (simp add: stable_imp_bit_iff_odd fun_eq_iff) - moreover have \{q. q < n} = {0.. - by auto - ultimately show ?case - by (simp add: stable_imp_take_bit_eq horner_sum_eq_sum mask_eq_sum_exp) -next - case (rec a b) - show ?case - proof (cases n) - case 0 - then show ?thesis - by simp - next - case (Suc m) - have \map (bit (of_bool b + 2 * a)) [0.. - by (simp only: upt_conv_Cons) (simp add: bit_0) - also have \\ = b # map (bit a) [0.. - by (simp only: flip: map_Suc_upt) (simp add: bit_Suc rec.hyps) - finally show ?thesis - using Suc rec.IH [of m] by (simp add: take_bit_Suc rec.hyps) - (simp_all add: ac_simps mod_2_eq_odd) - qed -qed - -end - -context linordered_euclidean_semiring_bit_operations -begin - -lemma bit_horner_sum_bit_iff [bit_simps]: - \bit (horner_sum of_bool 2 bs) n \ n < length bs \ bs ! n\ -proof (induction bs arbitrary: n) - case Nil - then show ?case - by simp -next - case (Cons b bs) - show ?case - proof (cases n) - case 0 - then show ?thesis - by (simp add: bit_0) - next - case (Suc m) - with bit_rec [of _ n] Cons.prems Cons.IH [of m] - show ?thesis by simp - qed -qed - -lemma take_bit_horner_sum_bit_eq: - \take_bit n (horner_sum of_bool 2 bs) = horner_sum of_bool 2 (take n bs)\ - by (auto simp add: bit_eq_iff bit_take_bit_iff bit_horner_sum_bit_iff) - -end - -lemma horner_sum_of_bool_2_less: - \(horner_sum of_bool 2 bs :: int) < 2 ^ length bs\ -proof - - have \(\n = 0.. (\n = 0.. - by (rule sum_mono) simp - also have \\ = 2 ^ length bs - 1\ - by (induction bs) simp_all - finally show ?thesis - by (simp add: horner_sum_eq_sum) -qed - - subsection \Symbolic computations for code generation\ lemma bit_int_code [code]: