# HG changeset patch # User haftmann # Date 1596533585 0 # Node ID 41393ecb57ace7ccd69e36f6a377678f50d4512a # Parent e4d42f5766dc30fdb7ed0d870bc7eb3d690c0369 uniform mask operation diff -r e4d42f5766dc -r 41393ecb57ac NEWS --- a/NEWS Tue Aug 04 09:24:00 2020 +0000 +++ b/NEWS Tue Aug 04 09:33:05 2020 +0000 @@ -83,6 +83,13 @@ * Session HOL-Word: Compound operation "bin_split" simplifies by default into its components "drop_bit" and "take_bit". INCOMPATIBILITY. +* Session HOL-Word: Uniform polymorphic "mask" operation for both +types int and word. INCOMPATIBILITY + +* Session HOL-Word: Operations lsb, msb and set_bit are separated +into theories Misc_lsb, Misc_msb and Misc_set_bit respectively. +INCOMPATIBILITY. + * Session HOL-Word: Operations "bin_last", "bin_rest", "bin_nth", "bintrunc", "sbintrunc", "norm_sint", "bin_cat" and "max_word" are now mere input abbreviations. Minor INCOMPATIBILITY. @@ -90,10 +97,6 @@ * Session HOL-Word: Theory HOL-Library.Z2 is not imported any longer. Minor INCOMPATIBILITY. -* Session HOL-Word: Operations lsb, msb and set_bit are separated -into theories Misc_lsb, Misc_msb and Misc_set_bit respectively. -INCOMPATIBILITY. - * Session HOL-TPTP: The "tptp_isabelle" and "tptp_sledgehammer" commands are in working order again, as opposed to outputting "GaveUp" on nearly all problems. diff -r e4d42f5766dc -r 41393ecb57ac src/HOL/Library/Bit_Operations.thy --- a/src/HOL/Library/Bit_Operations.thy Tue Aug 04 09:24:00 2020 +0000 +++ b/src/HOL/Library/Bit_Operations.thy Tue Aug 04 09:33:05 2020 +0000 @@ -15,9 +15,11 @@ fixes "and" :: \'a \ 'a \ 'a\ (infixr \AND\ 64) and or :: \'a \ 'a \ 'a\ (infixr \OR\ 59) and xor :: \'a \ 'a \ 'a\ (infixr \XOR\ 59) + and mask :: \nat \ 'a\ assumes bit_and_iff: \\n. bit (a AND b) n \ bit a n \ bit b n\ and bit_or_iff: \\n. bit (a OR b) n \ bit a n \ bit b n\ and bit_xor_iff: \\n. bit (a XOR b) n \ bit a n \ bit b n\ + and mask_eq_exp_minus_1: \mask n = 2 ^ n - 1\ begin text \ @@ -93,9 +95,6 @@ \take_bit n (a XOR b) = take_bit n a XOR take_bit n b\ by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff) -definition mask :: \nat \ 'a\ - where mask_eq_exp_minus_1: \mask n = 2 ^ n - 1\ - lemma bit_mask_iff: \bit (mask m) n \ 2 ^ n \ 0 \ n < m\ by (simp add: mask_eq_exp_minus_1 bit_mask_iff) @@ -104,25 +103,33 @@ \even (mask n) \ n = 0\ using bit_mask_iff [of n 0] by auto -lemma mask_0 [simp, code]: +lemma mask_0 [simp]: \mask 0 = 0\ by (simp add: mask_eq_exp_minus_1) -lemma mask_Suc_exp [code]: +lemma mask_Suc_0 [simp]: + \mask (Suc 0) = 1\ + by (simp add: mask_eq_exp_minus_1 add_implies_diff sym) + +lemma mask_Suc_exp: \mask (Suc n) = 2 ^ n OR mask n\ by (rule bit_eqI) (auto simp add: bit_or_iff bit_mask_iff bit_exp_iff not_less le_less_Suc_eq) lemma mask_Suc_double: - \mask (Suc n) = 2 * mask n OR 1\ + \mask (Suc n) = 1 OR 2 * mask n\ proof (rule bit_eqI) fix q assume \2 ^ q \ 0\ - show \bit (mask (Suc n)) q \ bit (2 * mask n OR 1) q\ + show \bit (mask (Suc n)) q \ bit (1 OR 2 * mask n) q\ by (cases q) (simp_all add: even_mask_iff even_or_iff bit_or_iff bit_mask_iff bit_exp_iff bit_double_iff not_less le_less_Suc_eq bit_1_iff, auto simp add: mult_2) qed +lemma mask_numeral: + \mask (numeral n) = 1 + 2 * mask (pred_numeral n)\ + by (simp add: numeral_eq_Suc mask_Suc_double one_or_eq ac_simps) + lemma take_bit_eq_mask: \take_bit n a = a AND mask n\ by (rule bit_eqI) @@ -495,6 +502,9 @@ \bit (k XOR l) n \ bit k n \ bit l n\ for k l :: int by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff) +definition mask_int :: \nat \ int\ + where \mask n = (2 :: int) ^ n - 1\ + instance proof fix k l :: int and n :: nat show \- k = NOT (k - 1)\ @@ -505,7 +515,7 @@ by (fact bit_or_int_iff) show \bit (k XOR l) n \ bit k n \ bit l n\ by (fact bit_xor_int_iff) -qed (simp_all add: bit_not_int_iff) +qed (simp_all add: bit_not_int_iff mask_int_def) end @@ -976,6 +986,9 @@ definition xor_nat :: \nat \ nat \ nat\ where \m XOR n = nat (int m XOR int n)\ for m n :: nat +definition mask_nat :: \nat \ nat\ + where \mask n = (2 :: nat) ^ n - 1\ + instance proof fix m n q :: nat show \bit (m AND n) q \ bit m q \ bit n q\ @@ -984,7 +997,7 @@ by (auto simp add: or_nat_def bit_or_iff less_le bit_eq_iff) show \bit (m XOR n) q \ bit m q \ bit n q\ by (auto simp add: xor_nat_def bit_xor_iff less_le bit_eq_iff) -qed +qed (simp add: mask_nat_def) end @@ -1044,19 +1057,12 @@ lift_definition xor_integer :: \integer \ integer \ integer\ is xor . -instance proof - fix k l :: \integer\ and n :: nat - show \- k = NOT (k - 1)\ - by transfer (simp add: minus_eq_not_minus_1) - show \bit (NOT k) n \ (2 :: integer) ^ n \ 0 \ \ bit k n\ - by transfer (fact bit_not_iff) - show \bit (k AND l) n \ bit k n \ bit l n\ - by transfer (fact bit_and_iff) - show \bit (k OR l) n \ bit k n \ bit l n\ - by transfer (fact bit_or_iff) - show \bit (k XOR l) n \ bit k n \ bit l n\ - by transfer (fact bit_xor_iff) -qed +lift_definition mask_integer :: \nat \ integer\ + is mask . + +instance by (standard; transfer) + (simp_all add: minus_eq_not_minus_1 mask_eq_exp_minus_1 + bit_not_iff bit_and_iff bit_or_iff bit_xor_iff) end @@ -1072,15 +1078,11 @@ lift_definition xor_natural :: \natural \ natural \ natural\ is xor . -instance proof - fix m n :: \natural\ and q :: nat - show \bit (m AND n) q \ bit m q \ bit n q\ - by transfer (fact bit_and_iff) - show \bit (m OR n) q \ bit m q \ bit n q\ - by transfer (fact bit_or_iff) - show \bit (m XOR n) q \ bit m q \ bit n q\ - by transfer (fact bit_xor_iff) -qed +lift_definition mask_natural :: \nat \ natural\ + is mask . + +instance by (standard; transfer) + (simp_all add: mask_eq_exp_minus_1 bit_and_iff bit_or_iff bit_xor_iff) end diff -r e4d42f5766dc -r 41393ecb57ac src/HOL/Library/Z2.thy --- a/src/HOL/Library/Z2.thy Tue Aug 04 09:24:00 2020 +0000 +++ b/src/HOL/Library/Z2.thy Tue Aug 04 09:33:05 2020 +0000 @@ -187,6 +187,9 @@ definition xor_bit :: \bit \ bit \ bit\ where [simp]: \b XOR c = of_bool (odd b \ odd c)\ for b c :: bit +definition mask_bit :: \nat \ bit\ + where [simp]: \mask_bit n = of_bool (n > 0)\ + instance by standard auto diff -r e4d42f5766dc -r 41393ecb57ac src/HOL/Word/Word.thy --- a/src/HOL/Word/Word.thy Tue Aug 04 09:24:00 2020 +0000 +++ b/src/HOL/Word/Word.thy Tue Aug 04 09:33:05 2020 +0000 @@ -1000,19 +1000,13 @@ is xor by simp -instance proof - fix a b :: \'a word\ and n :: nat - show \- a = NOT (a - 1)\ - by transfer (simp add: minus_eq_not_minus_1) - show \bit (NOT a) n \ (2 :: 'a word) ^ n \ 0 \ \ bit a n\ - by transfer (simp add: bit_not_iff) - show \bit (a AND b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_and_iff) - show \bit (a OR b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_or_iff) - show \bit (a XOR b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_xor_iff) -qed +lift_definition mask_word :: \nat \ 'a word\ + is mask + . + +instance by (standard; transfer) + (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 + bit_not_iff bit_and_iff bit_or_iff bit_xor_iff) end @@ -1173,6 +1167,11 @@ \take_bit n a = a AND mask n\ for a :: \'a::len word\ by (fact take_bit_eq_mask) +lemma [code]: + \mask (Suc n) = push_bit n (1 :: 'a word) OR mask n\ + \mask 0 = (0 :: 'a::len word)\ + by (simp_all add: mask_Suc_exp push_bit_of_1) + lemma [code_abbrev]: \push_bit n 1 = (2 :: 'a::len word) ^ n\ by (fact push_bit_of_1) @@ -1289,9 +1288,6 @@ is \\b k. take_bit LENGTH('a) k div 2 + of_bool b * 2 ^ (LENGTH('a) - Suc 0)\ by (fact arg_cong) -lift_definition mask :: \nat \ 'a::len word\ - is \take_bit LENGTH('a) \ Bit_Operations.mask\ . - lemma sshiftr1_eq: \sshiftr1 w = word_of_int (bin_rest (sint w))\ by transfer simp @@ -1328,7 +1324,7 @@ qed lemma mask_eq: - \mask n = (1 << n) - 1\ + \mask n = (1 << n) - (1 :: 'a::len word)\ by transfer (simp add: mask_eq_exp_minus_1 push_bit_of_1) lemma uint_sshiftr_eq [code]: @@ -1977,18 +1973,8 @@ lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \ n < LENGTH('a))" by transfer (simp add: bit_take_bit_iff ac_simps) -context - includes lifting_syntax -begin - -lemma transfer_rule_mask_word [transfer_rule]: - \((=) ===> pcr_word) Bit_Operations.mask Bit_Operations.mask\ - by (simp only: mask_eq_exp_minus_1 [abs_def]) transfer_prover - -end - lemma ucast_mask_eq: - \ucast (Bit_Operations.mask n :: 'b word) = Bit_Operations.mask (min LENGTH('b::len) n)\ + \ucast (mask n :: 'b word) = mask (min LENGTH('b::len) n)\ by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff exp_eq_zero_iff) \ \literal u(s)cast\ @@ -4075,27 +4061,23 @@ subsubsection \Mask\ lemma minus_1_eq_mask: - \- 1 = (Bit_Operations.mask LENGTH('a) :: 'a::len word)\ + \- 1 = (mask LENGTH('a) :: 'a::len word)\ by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff) - -lemma mask_eq_mask [code]: - \mask = Bit_Operations.mask\ - by (rule ext, transfer) simp lemma mask_eq_decr_exp: - \mask n = 2 ^ n - 1\ - by (simp add: mask_eq_mask mask_eq_exp_minus_1) + \mask n = 2 ^ n - (1 :: 'a::len word)\ + by (fact mask_eq_exp_minus_1) lemma mask_Suc_rec: - \mask (Suc n) = 2 * mask n + 1\ - by (simp add: mask_eq_mask mask_eq_exp_minus_1) + \mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\ + by (simp add: mask_eq_exp_minus_1) context begin qualified lemma bit_mask_iff: \bit (mask m :: 'a::len word) n \ n < min LENGTH('a) m\ - by (simp add: mask_eq_mask bit_mask_iff exp_eq_zero_iff not_le) + by (simp add: bit_mask_iff exp_eq_zero_iff not_le) end @@ -4188,7 +4170,8 @@ lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size] -lemma and_mask_less_size: "n < size x \ x AND mask n < 2^n" +lemma and_mask_less_size: "n < size x \ x AND mask n < 2 ^ n" + for x :: \'a::len word\ unfolding word_size by (erule and_mask_less') lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \ c > 0 \ x mod c = x AND mask n" @@ -4212,6 +4195,7 @@ by (auto simp: and_mask_wi' word_of_int_homs word.abs_eq_iff bintrunc_mod2p mod_simps) lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n" + for x :: \'a::len word\ using word_of_int_Ex [where x=x] by (auto simp: and_mask_wi' word_of_int_power_hom word.abs_eq_iff bintrunc_mod2p mod_simps) @@ -5365,10 +5349,6 @@ "(1 :: 'a :: len word) !! n \ 0 < LENGTH('a) \ n = 0" by simp -lemma mask_0 [simp]: - "mask 0 = 0" - by (simp add: Word.mask_def) - lemma shiftl0: "x << 0 = (x :: 'a :: len word)" by (fact shiftl_x_0) @@ -5379,7 +5359,7 @@ lemma mask_Suc_0: "mask (Suc 0) = 1" using mask_1 by simp -lemma mask_numeral: "mask (numeral n) = 2 * mask (pred_numeral n) + 1" +lemma mask_numeral: "mask (numeral n) = 2 * mask (pred_numeral n) + (1 :: 'a::len word)" by (simp add: mask_Suc_rec numeral_eq_Suc) lemma bin_last_bintrunc: "bin_last (bintrunc l n) = (l > 0 \ bin_last n)" diff -r e4d42f5766dc -r 41393ecb57ac src/HOL/ex/Word.thy --- a/src/HOL/ex/Word.thy Tue Aug 04 09:24:00 2020 +0000 +++ b/src/HOL/ex/Word.thy Tue Aug 04 09:33:05 2020 +0000 @@ -644,19 +644,12 @@ is xor by simp -instance proof - fix a b :: \'a word\ and n :: nat - show \- a = NOT (a - 1)\ - by transfer (simp add: minus_eq_not_minus_1) - show \bit (NOT a) n \ (2 :: 'a word) ^ n \ 0 \ \ bit a n\ - by transfer (simp add: bit_not_iff) - show \bit (a AND b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_and_iff) - show \bit (a OR b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_or_iff) - show \bit (a XOR b) n \ bit a n \ bit b n\ - by transfer (auto simp add: bit_xor_iff) -qed +lift_definition mask_word :: \nat \ 'a word\ + is mask . + +instance by (standard; transfer) + (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 + bit_not_iff bit_and_iff bit_or_iff bit_xor_iff) end