# HG changeset patch # User wenzelm # Date 1623008366 -7200 # Node ID c10fe904ac103eb2c638b50488b6072861321f94 # Parent 0510c7a4256a019f8d00866b924b0b64138bfe9e# Parent 1192c68ebe1c0f8d2603ae4651c947a4972cc9c8 merged diff -r 1192c68ebe1c -r c10fe904ac10 NEWS --- a/NEWS Sun Jun 06 21:17:23 2021 +0200 +++ b/NEWS Sun Jun 06 21:39:26 2021 +0200 @@ -157,8 +157,10 @@ * Bit operations set_bit, unset_bit and flip_bit are now class operations. INCOMPATIBILITY. -* Abbreviation "max_word" has been moved to session Word_Lib in the AFP. -See there further the changelog in theory Guide. INCOMPATIBILITY. +* Abbreviation "max_word" has been moved to session Word_Lib in the AFP, +as also have constants "shiftl1", "shiftr1", "sshiftr1", "bshiftr1", +"setBit", "clearBit". See there further the changelog in theory Guide. +INCOMPATIBILITY. *** ML *** diff -r 1192c68ebe1c -r c10fe904ac10 src/HOL/Library/Bit_Operations.thy --- a/src/HOL/Library/Bit_Operations.thy Sun Jun 06 21:17:23 2021 +0200 +++ b/src/HOL/Library/Bit_Operations.thy Sun Jun 06 21:39:26 2021 +0200 @@ -9,6 +9,11 @@ "HOL-Library.Boolean_Algebra" begin +lemma bit_numeral_int_iff [bit_simps]: \ \TODO: move\ + \bit (numeral m :: int) n \ bit (numeral m :: nat) n\ + using bit_of_nat_iff_bit [of \numeral m\ n] by simp + + subsection \Bit operations\ class semiring_bit_operations = semiring_bit_shifts + diff -r 1192c68ebe1c -r c10fe904ac10 src/HOL/Library/Word.thy --- a/src/HOL/Library/Word.thy Sun Jun 06 21:17:23 2021 +0200 +++ b/src/HOL/Library/Word.thy Sun Jun 06 21:39:26 2021 +0200 @@ -1802,40 +1802,6 @@ for w :: \'a::len word\ by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def) -lift_definition shiftl1 :: \'a::len word \ 'a word\ - is \(*) 2\ - by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) - -lemma shiftl1_eq: - \shiftl1 w = word_of_int (2 * uint w)\ - by transfer (simp add: take_bit_eq_mod mod_simps) - -lemma shiftl1_eq_mult_2: - \shiftl1 = (*) 2\ - by (rule ext, transfer) simp - -lemma bit_shiftl1_iff [bit_simps]: - \bit (shiftl1 w) n \ 0 < n \ n < LENGTH('a) \ bit w (n - 1)\ - for w :: \'a::len word\ - by (simp add: shiftl1_eq_mult_2 bit_double_iff not_le) (simp add: ac_simps) - -lift_definition shiftr1 :: \'a::len word \ 'a word\ - \ \shift right as unsigned or as signed, ie logical or arithmetic\ - is \\k. take_bit LENGTH('a) k div 2\ - by simp - -lemma shiftr1_eq_div_2: - \shiftr1 w = w div 2\ - by transfer simp - -lemma bit_shiftr1_iff [bit_simps]: - \bit (shiftr1 w) n \ bit w (Suc n)\ - by transfer (auto simp flip: bit_Suc simp add: bit_take_bit_iff) - -lemma shiftr1_eq: - \shiftr1 w = word_of_int (uint w div 2)\ - by transfer simp - lemma bit_word_iff_drop_bit_and [code]: \bit a n \ drop_bit n a AND 1 = 1\ for a :: \'a::len word\ by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq) @@ -1847,32 +1813,6 @@ and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" by (transfer, simp add: take_bit_not_take_bit)+ -lift_definition setBit :: \'a::len word \ nat \ 'a word\ - is \\k n. set_bit n k\ - by (simp add: take_bit_set_bit_eq) - -lemma set_Bit_eq: - \setBit w n = set_bit n w\ - by transfer simp - -lemma bit_setBit_iff [bit_simps]: - \bit (setBit w m) n \ (m = n \ n < LENGTH('a) \ bit w n)\ - for w :: \'a::len word\ - by transfer (auto simp add: bit_set_bit_iff) - -lift_definition clearBit :: \'a::len word \ nat \ 'a word\ - is \\k n. unset_bit n k\ - by (simp add: take_bit_unset_bit_eq) - -lemma clear_Bit_eq: - \clearBit w n = unset_bit n w\ - by transfer simp - -lemma bit_clearBit_iff [bit_simps]: - \bit (clearBit w m) n \ m \ n \ bit w n\ - for w :: \'a::len word\ - by transfer (auto simp add: bit_unset_bit_iff) - definition even_word :: \'a::len word \ bool\ where [code_abbrev]: \even_word = even\ @@ -1962,6 +1902,14 @@ for w :: \'a::len word\ by transfer simp +lemma signed_drop_bit_of_0 [simp]: + \signed_drop_bit n 0 = 0\ + by transfer simp + +lemma signed_drop_bit_of_minus_1 [simp]: + \signed_drop_bit n (- 1) = - 1\ + by transfer simp + lemma signed_drop_bit_signed_drop_bit [simp]: \signed_drop_bit m (signed_drop_bit n w) = signed_drop_bit (m + n) w\ for w :: \'a::len word\ @@ -1989,22 +1937,6 @@ by (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length) qed auto -lift_definition sshiftr1 :: \'a::len word \ 'a word\ - is \\k. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - Suc 0) k div 2)\ - by (simp flip: signed_take_bit_decr_length_iff) - -lift_definition bshiftr1 :: \bool \ 'a::len word \ 'a word\ - is \\b k. take_bit LENGTH('a) k div 2 + of_bool b * 2 ^ (LENGTH('a) - Suc 0)\ - by (fact arg_cong) - -lemma sshiftr1_eq_signed_drop_bit_Suc_0: - \sshiftr1 = signed_drop_bit (Suc 0)\ - by (rule ext) (transfer, simp add: drop_bit_Suc) - -lemma sshiftr1_eq: - \sshiftr1 w = word_of_int (sint w div 2)\ - by transfer simp - subsection \Rotation\ @@ -3605,92 +3537,8 @@ by (induct n) (simp_all add: wi_hom_syms) -subsection \Shifting, Rotating, and Splitting Words\ - -lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (2 * w)" - by transfer simp - -lemma shiftl1_numeral [simp]: "shiftl1 (numeral w) = numeral (Num.Bit0 w)" - unfolding word_numeral_alt shiftl1_wi by simp - -lemma shiftl1_neg_numeral [simp]: "shiftl1 (- numeral w) = - numeral (Num.Bit0 w)" - unfolding word_neg_numeral_alt shiftl1_wi by simp - -lemma shiftl1_0 [simp] : "shiftl1 0 = 0" - by transfer simp - -lemma shiftl1_def_u: "shiftl1 w = word_of_int (2 * uint w)" - by (fact shiftl1_eq) - -lemma shiftl1_def_s: "shiftl1 w = word_of_int (2 * sint w)" - by (simp add: shiftl1_def_u wi_hom_syms) - -lemma shiftr1_0 [simp]: "shiftr1 0 = 0" - by transfer simp - -lemma sshiftr1_0 [simp]: "sshiftr1 0 = 0" - by transfer simp - -lemma sshiftr1_n1 [simp]: "sshiftr1 (- 1) = - 1" - by transfer simp - -text \ - see paper page 10, (1), (2), \shiftr1_def\ is of the form of (1), - where \f\ (ie \_ div 2\) takes normal arguments to normal results, - thus we get (2) from (1) -\ - -lemma uint_shiftr1: "uint (shiftr1 w) = uint w div 2" - using drop_bit_eq_div [of 1 \uint w\, symmetric] - by transfer (simp add: drop_bit_take_bit min_def) - -lemma bit_sshiftr1_iff [bit_simps]: - \bit (sshiftr1 w) n \ bit w (if n = LENGTH('a) - 1 then LENGTH('a) - 1 else Suc n)\ - for w :: \'a::len word\ - apply transfer - by (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def le_Suc_eq simp flip: bit_Suc) - -lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2" - by (fact uint_shiftr1) - -lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2" - using sint_signed_drop_bit_eq [of 1 w] - by (simp add: drop_bit_Suc sshiftr1_eq_signed_drop_bit_Suc_0) - -lemma bit_bshiftr1_iff [bit_simps]: - \bit (bshiftr1 b w) n \ b \ n = LENGTH('a) - 1 \ bit w (Suc n)\ - for w :: \'a::len word\ - apply transfer - apply (subst disjunctive_add) - apply (auto simp add: bit_take_bit_iff bit_or_iff bit_exp_iff simp flip: bit_Suc) - done - - subsubsection \shift functions in terms of lists of bools\ -lemma shiftl1_rev: "shiftl1 w = word_reverse (shiftr1 (word_reverse w))" - by (intro bit_word_eqI) (auto simp add: bit_shiftl1_iff bit_word_reverse_iff bit_shiftr1_iff Suc_diff_Suc) - -\ \note -- the following results use \'a::len word < number_ring\\ - -lemma shiftl1_2t: "shiftl1 w = 2 * w" - for w :: "'a::len word" - by (simp add: shiftl1_eq wi_hom_mult [symmetric]) - -lemma shiftl1_p: "shiftl1 w = w + w" - for w :: "'a::len word" - by (simp add: shiftl1_2t) - -lemma shiftr1_bintr [simp]: - "(shiftr1 (numeral w) :: 'a::len word) = - word_of_int (take_bit LENGTH('a) (numeral w) div 2)" - by transfer simp - -lemma sshiftr1_sbintr [simp]: - "(sshiftr1 (numeral w) :: 'a::len word) = - word_of_int (signed_take_bit (LENGTH('a) - 1) (numeral w) div 2)" - by transfer simp - text \TODO: rules for \<^term>\- (numeral n)\\ lemma drop_bit_word_numeral [simp]: @@ -4173,9 +4021,6 @@ lemma uint_lt_0 [simp]: "uint x < 0 = False" by (simp add: linorder_not_less) -lemma shiftr1_1 [simp]: "shiftr1 (1::'a::len word) = 0" - by transfer simp - lemma word_less_1 [simp]: "x < 1 \ x = 0" for x :: "'a::len word" by (simp add: word_less_nat_alt unat_0_iff)