926 lemma bit_neg_numeral_word_iff [simp]: |
926 lemma bit_neg_numeral_word_iff [simp]: |
927 \<open>bit (- numeral w :: 'a::len word) n |
927 \<open>bit (- numeral w :: 'a::len word) n |
928 \<longleftrightarrow> n < LENGTH('a) \<and> bit (- numeral w :: int) n\<close> |
928 \<longleftrightarrow> n < LENGTH('a) \<and> bit (- numeral w :: int) n\<close> |
929 by transfer simp |
929 by transfer simp |
930 |
930 |
931 instantiation word :: (len) semiring_bit_shifts |
931 instantiation word :: (len) ring_bit_operations |
932 begin |
932 begin |
|
933 |
|
934 lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close> |
|
935 is not |
|
936 by (simp add: take_bit_not_iff) |
|
937 |
|
938 lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
939 is \<open>and\<close> |
|
940 by simp |
|
941 |
|
942 lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
943 is or |
|
944 by simp |
|
945 |
|
946 lift_definition xor_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
947 is xor |
|
948 by simp |
|
949 |
|
950 lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close> |
|
951 is mask |
|
952 . |
|
953 |
|
954 lift_definition set_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
955 is set_bit |
|
956 by (simp add: set_bit_def) |
|
957 |
|
958 lift_definition unset_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
959 is unset_bit |
|
960 by (simp add: unset_bit_def) |
|
961 |
|
962 lift_definition flip_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
963 is flip_bit |
|
964 by (simp add: flip_bit_def) |
933 |
965 |
934 lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
966 lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
935 is push_bit |
967 is push_bit |
936 proof - |
968 proof - |
937 show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close> |
969 show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close> |
954 |
986 |
955 lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
987 lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
956 is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close> |
988 is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close> |
957 by (simp add: ac_simps) (simp only: flip: take_bit_take_bit) |
989 by (simp add: ac_simps) (simp only: flip: take_bit_take_bit) |
958 |
990 |
959 instance proof |
991 instance apply (standard; transfer) |
960 show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
992 apply (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 |
961 by transfer (simp add: push_bit_eq_mult) |
993 bit_simps set_bit_def flip_bit_def take_bit_drop_bit |
962 show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
994 simp flip: drop_bit_eq_div take_bit_eq_mod) |
963 by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit) |
995 apply (simp_all add: drop_bit_take_bit flip: push_bit_eq_mult) |
964 show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
996 done |
965 by transfer (auto simp flip: take_bit_eq_mod) |
|
966 qed |
|
967 |
997 |
968 end |
998 end |
969 |
999 |
970 lemma [code]: |
1000 lemma [code]: |
971 \<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close> |
1001 \<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close> |
977 |
1007 |
978 lemma [code]: |
1008 lemma [code]: |
979 \<open>Word.the_int (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (Word.the_int w) else Word.the_int w)\<close> |
1009 \<open>Word.the_int (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (Word.the_int w) else Word.the_int w)\<close> |
980 for w :: \<open>'a::len word\<close> |
1010 for w :: \<open>'a::len word\<close> |
981 by transfer (simp add: not_le not_less ac_simps min_absorb2) |
1011 by transfer (simp add: not_le not_less ac_simps min_absorb2) |
982 |
|
983 |
|
984 instantiation word :: (len) ring_bit_operations |
|
985 begin |
|
986 |
|
987 lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close> |
|
988 is not |
|
989 by (simp add: take_bit_not_iff) |
|
990 |
|
991 lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
992 is \<open>and\<close> |
|
993 by simp |
|
994 |
|
995 lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
996 is or |
|
997 by simp |
|
998 |
|
999 lift_definition xor_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
1000 is xor |
|
1001 by simp |
|
1002 |
|
1003 lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close> |
|
1004 is mask |
|
1005 . |
|
1006 |
|
1007 lift_definition set_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
1008 is set_bit |
|
1009 by (simp add: set_bit_def) |
|
1010 |
|
1011 lift_definition unset_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
1012 is unset_bit |
|
1013 by (simp add: unset_bit_def) |
|
1014 |
|
1015 lift_definition flip_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
1016 is flip_bit |
|
1017 by (simp add: flip_bit_def) |
|
1018 |
|
1019 instance by (standard; transfer) |
|
1020 (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 |
|
1021 bit_simps set_bit_def flip_bit_def) |
|
1022 |
|
1023 end |
|
1024 |
1012 |
1025 lemma [code_abbrev]: |
1013 lemma [code_abbrev]: |
1026 \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> |
1014 \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> |
1027 by (fact push_bit_of_1) |
1015 by (fact push_bit_of_1) |
1028 |
1016 |
1112 for w :: \<open>'b::len word\<close> |
1100 for w :: \<open>'b::len word\<close> |
1113 by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff) |
1101 by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff) |
1114 |
1102 |
1115 end |
1103 end |
1116 |
1104 |
1117 context semiring_bit_shifts |
1105 context semiring_bit_operations |
1118 begin |
1106 begin |
1119 |
1107 |
1120 lemma unsigned_push_bit_eq: |
1108 lemma unsigned_push_bit_eq: |
1121 \<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close> |
1109 \<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close> |
1122 for w :: \<open>'b::len word\<close> |
1110 for w :: \<open>'b::len word\<close> |
1142 for w :: \<open>'b::len word\<close> |
1130 for w :: \<open>'b::len word\<close> |
1143 by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Bit_Operations.bit_take_bit_iff) |
1131 by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Bit_Operations.bit_take_bit_iff) |
1144 |
1132 |
1145 end |
1133 end |
1146 |
1134 |
1147 context unique_euclidean_semiring_with_bit_shifts |
1135 context unique_euclidean_semiring_with_bit_operations |
1148 begin |
1136 begin |
1149 |
1137 |
1150 lemma unsigned_drop_bit_eq: |
1138 lemma unsigned_drop_bit_eq: |
1151 \<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close> |
1139 \<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close> |
1152 for w :: \<open>'b::len word\<close> |
1140 for w :: \<open>'b::len word\<close> |
1398 by (simp add: unat_mod_distrib) |
1386 by (simp add: unat_mod_distrib) |
1399 then show ?thesis |
1387 then show ?thesis |
1400 by (simp add: of_nat_mod) |
1388 by (simp add: of_nat_mod) |
1401 qed |
1389 qed |
1402 |
1390 |
1403 context semiring_bit_shifts |
1391 context semiring_bit_operations |
1404 begin |
1392 begin |
1405 |
1393 |
1406 lemma unsigned_ucast_eq: |
1394 lemma unsigned_ucast_eq: |
1407 \<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close> |
1395 \<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close> |
1408 for w :: \<open>'b::len word\<close> |
1396 for w :: \<open>'b::len word\<close> |