# HG changeset patch # User haftmann # Date 1602059954 -7200 # Node ID 633d14bd1e59eefaa76f502055dd8d773555b83b # Parent 04be6716cac617d72c557eb13b866eea8b1c0299 consolidated for the sake of documentation diff -r 04be6716cac6 -r 633d14bd1e59 NEWS --- a/NEWS Wed Oct 07 11:31:51 2020 +0200 +++ b/NEWS Wed Oct 07 10:39:14 2020 +0200 @@ -120,6 +120,10 @@ * Session HOL-Word: Uniform polymorphic "mask" operation for both types int and word. INCOMPATIBILITY. +* Session HOL-Word: Syntax for signed compare operators has been +consolidated with syntax of regular compare operators. +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. diff -r 04be6716cac6 -r 633d14bd1e59 src/HOL/Word/Misc_msb.thy --- a/src/HOL/Word/Misc_msb.thy Wed Oct 07 11:31:51 2020 +0200 +++ b/src/HOL/Word/Misc_msb.thy Wed Oct 07 10:39:14 2020 +0200 @@ -73,6 +73,11 @@ end +lemma msb_word_iff_bit: + \msb w \ bit w (LENGTH('a) - Suc 0)\ + for w :: \'a::len word\ + by (simp add: msb_word_def bin_sign_def bit_uint_iff) + lemma word_msb_def: "msb a \ bin_sign (sint a) = - 1" by (simp add: msb_word_def sint_uint) @@ -80,6 +85,10 @@ lemma word_msb_sint: "msb w \ sint w < 0" by (simp add: msb_word_eq bit_last_iff) +lemma msb_word_iff_sless_0: + \msb w \ w + by (simp add: word_msb_sint word_sless_alt) + lemma msb_word_of_int: "msb (word_of_int x::'a::len word) = bin_nth x (LENGTH('a) - 1)" by (simp add: word_msb_def word_sbin.eq_norm bin_sign_lem) diff -r 04be6716cac6 -r 633d14bd1e59 src/HOL/Word/More_Word.thy --- a/src/HOL/Word/More_Word.thy Wed Oct 07 11:31:51 2020 +0200 +++ b/src/HOL/Word/More_Word.thy Wed Oct 07 10:39:14 2020 +0200 @@ -26,7 +26,7 @@ lemmas shiftl1_def = shiftl1_eq lemmas shiftr1_def = shiftr1_eq lemmas sshiftr1_def = sshiftr1_eq -lemmas sshiftr_def = sshiftr_eq +lemmas sshiftr_def = sshiftr_eq_funpow_sshiftr1 lemmas to_bl_def = to_bl_eq lemmas ucast_def = ucast_eq lemmas unat_def = unat_eq_nat_uint diff -r 04be6716cac6 -r 633d14bd1e59 src/HOL/Word/Reversed_Bit_Lists.thy --- a/src/HOL/Word/Reversed_Bit_Lists.thy Wed Oct 07 11:31:51 2020 +0200 +++ b/src/HOL/Word/Reversed_Bit_Lists.thy Wed Oct 07 10:39:14 2020 +0200 @@ -1478,12 +1478,9 @@ lemma drop_sshiftr: "drop n (to_bl (w >>> n)) = take (size w - n) (to_bl w)" for w :: "'a::len word" - apply (unfold sshiftr_eq) - apply (induct n) - prefer 2 - apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric]) - apply (rule butlast_take [THEN trans]) - apply (auto simp: word_size) + apply (simp_all add: word_size sshiftr_eq) + apply (rule nth_equalityI) + apply (simp_all add: word_size nth_to_bl bit_signed_drop_bit_iff) done lemma take_shiftr: "n \ size w \ take n (to_bl (w >> n)) = replicate n False" @@ -1495,17 +1492,15 @@ apply (auto simp: word_size) done -lemma take_sshiftr' [rule_format] : - "n \ size w \ hd (to_bl (w >>> n)) = hd (to_bl w) \ +lemma take_sshiftr': + "n \ size w \ hd (to_bl (w >>> n)) = hd (to_bl w) \ take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))" for w :: "'a::len word" - apply (unfold sshiftr_eq) - apply (induct n) - prefer 2 - apply (simp add: bl_sshiftr1) - apply (rule impI) - apply (rule take_butlast [THEN trans]) - apply (auto simp: word_size) + apply (auto simp add: sshiftr_eq hd_bl_sign_sint bin_sign_def not_le word_size sint_signed_drop_bit_eq) + apply (rule nth_equalityI) + apply (auto simp add: nth_to_bl bit_signed_drop_bit_iff bit_last_iff) + apply (rule nth_equalityI) + apply (auto simp add: nth_to_bl bit_signed_drop_bit_iff bit_last_iff) done lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1] diff -r 04be6716cac6 -r 633d14bd1e59 src/HOL/Word/Word.thy --- a/src/HOL/Word/Word.thy Wed Oct 07 11:31:51 2020 +0200 +++ b/src/HOL/Word/Word.thy Wed Oct 07 10:39:14 2020 +0200 @@ -1306,6 +1306,10 @@ by (simp add: of_nat_div) qed +lemma unat_drop_bit_eq: + \unat (drop_bit n w) = drop_bit n (unat w)\ + by (rule bit_eqI) (simp add: bit_unsigned_iff bit_drop_bit_eq) + lemma uint_mod_distrib: \uint (v mod w) = uint v mod uint w\ proof - @@ -1605,38 +1609,54 @@ subsection \Ordering\ -lift_definition word_sle :: \'a::len word \ 'a word \ bool\ (\(_/ <=s _)\ [50, 51] 50) - is \\k l. signed_take_bit (LENGTH('a) - 1) k \ signed_take_bit (LENGTH('a) - 1) l\ +lift_definition word_sle :: \'a::len word \ 'a word \ bool\ + is \\k l. signed_take_bit (LENGTH('a) - Suc 0) k \ signed_take_bit (LENGTH('a) - Suc 0) l\ + by (simp flip: signed_take_bit_decr_length_iff) + +lift_definition word_sless :: \'a::len word \ 'a word \ bool\ + is \\k l. signed_take_bit (LENGTH('a) - Suc 0) k < signed_take_bit (LENGTH('a) - Suc 0) l\ by (simp flip: signed_take_bit_decr_length_iff) +notation + word_sle ("'(\s')") and + word_sle ("(_/ \s _)" [51, 51] 50) and + word_sless ("'(a <=s b \ sint a \ sint b\ by transfer simp - -lift_definition word_sless :: \'a::len word \ 'a word \ bool\ (\(_/ [50, 51] 50) - is \\k l. signed_take_bit (LENGTH('a) - 1) k < signed_take_bit (LENGTH('a) - 1) l\ - by (simp flip: signed_take_bit_decr_length_iff) - -lemma word_sless_eq: - \x x <=s y \ x \ y\ - by transfer (simp add: signed_take_bit_decr_length_iff less_le) lemma [code]: \a sint a < sint b\ by transfer simp +lemma signed_ordering: \ordering word_sle word_sless\ + apply (standard; transfer) + apply simp_all + using signed_take_bit_decr_length_iff apply force + using signed_take_bit_decr_length_iff apply force + done + +lemma signed_linorder: \class.linorder word_sle word_sless\ + by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff) + +interpretation signed: linorder word_sle word_sless + by (fact signed_linorder) + +lemma word_sless_eq: + \x x <=s y \ x \ y\ + by (fact signed.less_le) + lemma word_less_alt: "a < b \ uint a < uint b" by (fact word_less_def) -lemma signed_linorder: "class.linorder word_sle word_sless" - by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff) - -interpretation signed: linorder "word_sle" "word_sless" - by (rule signed_linorder) - lemma word_zero_le [simp]: "0 \ y" for y :: "'a::len word" - by transfer simp + by (fact word_coorder.extremum) lemma word_m1_ge [simp] : "word_pred 0 \ y" (* FIXME: delete *) by transfer (simp add: take_bit_minus_one_eq_mask mask_eq_exp_minus_1 bintr_lt2p) @@ -1996,6 +2016,32 @@ subsection \More shift operations\ +lift_definition signed_drop_bit :: \nat \ 'a word \ 'a::len word\ + is \\n. drop_bit n \ signed_take_bit (LENGTH('a) - Suc 0)\ + using signed_take_bit_decr_length_iff + by (simp add: take_bit_drop_bit) force + +lemma bit_signed_drop_bit_iff: + \bit (signed_drop_bit m w) n \ bit w (if LENGTH('a) - m \ n \ n < LENGTH('a) then LENGTH('a) - 1 else m + n)\ + for w :: \'a::len word\ + apply transfer + apply (auto simp add: bit_drop_bit_eq bit_signed_take_bit_iff not_le min_def) + apply (metis add.commute le_antisym less_diff_conv less_eq_decr_length_iff) + apply (metis le_antisym less_eq_decr_length_iff) + done + +lemma signed_drop_bit_0 [simp]: + \signed_drop_bit 0 w = w\ + by transfer simp + +lemma sint_signed_drop_bit_eq: + \sint (signed_drop_bit n w) = drop_bit n (sint w)\ + apply (cases \LENGTH('a)\; cases n) + apply simp_all + apply (rule bit_eqI) + apply (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length) + done + lift_definition sshiftr1 :: \'a::len word \ 'a word\ is \\k. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) k div 2)\ by (simp flip: signed_take_bit_decr_length_iff) @@ -2012,7 +2058,7 @@ \sshiftr1 w = word_of_int (sint w div 2)\ by transfer simp -lemma sshiftr_eq: +lemma sshiftr_eq_funpow_sshiftr1: \w >>> n = (sshiftr1 ^^ n) w\ proof - have *: \(\k::int. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - Suc 0) k div 2)) ^^ Suc n = @@ -3981,6 +4027,10 @@ using le_less_Suc_eq apply fastforce done +lemma sshiftr_eq: + \w >>> m = signed_drop_bit m w\ + by (rule bit_eqI) (simp add: bit_signed_drop_bit_iff bit_sshiftr_word_iff) + lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)" apply transfer apply (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def simp flip: bit_Suc)