src/HOL/Parity.thy
changeset 71965 d45f5d4c41bd
parent 71958 4320875eb8a1
child 71966 e18e9ac8c205
equal deleted inserted replaced
71964:235173749448 71965:d45f5d4c41bd
   725     and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close>
   725     and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close>
   726     and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close>
   726     and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close>
   727     and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close>
   727     and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close>
   728     and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
   728     and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
   729     and even_mult_exp_div_exp_iff: \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close>
   729     and even_mult_exp_div_exp_iff: \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close>
       
   730   fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>
       
   731   assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>
   730 begin
   732 begin
       
   733 
       
   734 text \<open>
       
   735   Having \<^const>\<open>bit\<close> as definitional class operation
       
   736   takes into account that specific instances can be implemented
       
   737   differently wrt. code generation.
       
   738 \<close>
   731 
   739 
   732 lemma bits_div_by_0 [simp]:
   740 lemma bits_div_by_0 [simp]:
   733   \<open>a div 0 = 0\<close>
   741   \<open>a div 0 = 0\<close>
   734   by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero)
   742   by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero)
   735 
   743 
   780 
   788 
   781 lemma bits_one_mod_two_eq_one [simp]:
   789 lemma bits_one_mod_two_eq_one [simp]:
   782   \<open>1 mod 2 = 1\<close>
   790   \<open>1 mod 2 = 1\<close>
   783   by (simp add: mod2_eq_if)
   791   by (simp add: mod2_eq_if)
   784 
   792 
   785 definition bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>
       
   786   where \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>
       
   787 
       
   788 lemma bit_0 [simp]:
   793 lemma bit_0 [simp]:
   789   \<open>bit a 0 \<longleftrightarrow> odd a\<close>
   794   \<open>bit a 0 \<longleftrightarrow> odd a\<close>
   790   by (simp add: bit_def)
   795   by (simp add: bit_iff_odd)
   791 
   796 
   792 lemma bit_Suc:
   797 lemma bit_Suc:
   793   \<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>
   798   \<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>
   794   using div_exp_eq [of a 1 n] by (simp add: bit_def)
   799   using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd)
   795 
   800 
   796 lemma bit_rec:
   801 lemma bit_rec:
   797   \<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close>
   802   \<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close>
   798   by (cases n) (simp_all add: bit_Suc)
   803   by (cases n) (simp_all add: bit_Suc)
   799 
   804 
   800 lemma bit_0_eq [simp]:
   805 lemma bit_0_eq [simp]:
   801   \<open>bit 0 = bot\<close>
   806   \<open>bit 0 = bot\<close>
   802   by (simp add: fun_eq_iff bit_def)
   807   by (simp add: fun_eq_iff bit_iff_odd)
   803 
   808 
   804 context
   809 context
   805   fixes a
   810   fixes a
   806   assumes stable: \<open>a div 2 = a\<close>
   811   assumes stable: \<open>a div 2 = a\<close>
   807 begin
   812 begin
   848     using \<open>a div 2 = a\<close> by (simp add: hyp)
   853     using \<open>a div 2 = a\<close> by (simp add: hyp)
   849 qed
   854 qed
   850 
   855 
   851 lemma exp_eq_0_imp_not_bit:
   856 lemma exp_eq_0_imp_not_bit:
   852   \<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>
   857   \<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>
   853   using that by (simp add: bit_def)
   858   using that by (simp add: bit_iff_odd)
   854 
   859 
   855 lemma bit_eqI:
   860 lemma bit_eqI:
   856   \<open>a = b\<close> if \<open>\<And>n. 2 ^ n \<noteq> 0 \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
   861   \<open>a = b\<close> if \<open>\<And>n. 2 ^ n \<noteq> 0 \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
   857 proof -
   862 proof -
   858   have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n
   863   have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n
   910   \<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close>
   915   \<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close>
   911   by (auto intro: bit_eqI)
   916   by (auto intro: bit_eqI)
   912 
   917 
   913 lemma bit_exp_iff:
   918 lemma bit_exp_iff:
   914   \<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close>
   919   \<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close>
   915   by (auto simp add: bit_def exp_div_exp_eq)
   920   by (auto simp add: bit_iff_odd exp_div_exp_eq)
   916 
   921 
   917 lemma bit_1_iff:
   922 lemma bit_1_iff:
   918   \<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>
   923   \<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>
   919   using bit_exp_iff [of 0 n] by simp
   924   using bit_exp_iff [of 0 n] by simp
   920 
   925 
   922   \<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>
   927   \<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>
   923   using bit_exp_iff [of 1 n] by auto
   928   using bit_exp_iff [of 1 n] by auto
   924 
   929 
   925 lemma even_bit_succ_iff:
   930 lemma even_bit_succ_iff:
   926   \<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>
   931   \<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>
   927   using that by (cases \<open>n = 0\<close>) (simp_all add: bit_def)
   932   using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd)
   928 
   933 
   929 lemma odd_bit_iff_bit_pred:
   934 lemma odd_bit_iff_bit_pred:
   930   \<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close>
   935   \<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close>
   931 proof -
   936 proof -
   932   from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
   937   from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
   936 qed
   941 qed
   937 
   942 
   938 lemma bit_double_iff:
   943 lemma bit_double_iff:
   939   \<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> 2 ^ n \<noteq> 0\<close>
   944   \<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> 2 ^ n \<noteq> 0\<close>
   940   using even_mult_exp_div_exp_iff [of a 1 n]
   945   using even_mult_exp_div_exp_iff [of a 1 n]
   941   by (cases n, auto simp add: bit_def ac_simps)
   946   by (cases n, auto simp add: bit_iff_odd ac_simps)
   942 
   947 
   943 lemma bit_eq_rec:
   948 lemma bit_eq_rec:
   944   \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>)
   949   \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>)
   945 proof
   950 proof
   946   assume ?P
   951   assume ?P
   968   qed
   973   qed
   969 qed
   974 qed
   970 
   975 
   971 lemma bit_mod_2_iff [simp]:
   976 lemma bit_mod_2_iff [simp]:
   972   \<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close>
   977   \<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close>
   973   by (cases a rule: parity_cases) (simp_all add: bit_def)
   978   by (cases a rule: parity_cases) (simp_all add: bit_iff_odd)
   974 
   979 
   975 lemma bit_mask_iff:
   980 lemma bit_mask_iff:
   976   \<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>
   981   \<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>
   977   by (simp add: bit_def even_mask_div_iff not_le)
   982   by (simp add: bit_iff_odd even_mask_div_iff not_le)
   978 
   983 
   979 lemma bit_Numeral1_iff [simp]:
   984 lemma bit_Numeral1_iff [simp]:
   980   \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>
   985   \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>
   981   by (simp add: bit_rec)
   986   by (simp add: bit_rec)
   982 
   987 
  1009         by simp
  1014         by simp
  1010     qed
  1015     qed
  1011   qed
  1016   qed
  1012 qed
  1017 qed
  1013 
  1018 
  1014 instance nat :: semiring_bits
  1019 instantiation nat :: semiring_bits
       
  1020 begin
       
  1021 
       
  1022 definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close>
       
  1023   where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close>
       
  1024 
       
  1025 instance
  1015 proof
  1026 proof
  1016   show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close>
  1027   show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close>
  1017     and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close>
  1028     and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close>
  1018     for P and n :: nat
  1029     for P and n :: nat
  1019   proof (induction n rule: nat_bit_induct)
  1030   proof (induction n rule: nat_bit_induct)
  1046   show \<open>even (q * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::nat) ^ n = 0 \<or> m \<le> n \<and> even (q div 2 ^ (n - m))\<close>
  1057   show \<open>even (q * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::nat) ^ n = 0 \<or> m \<le> n \<and> even (q div 2 ^ (n - m))\<close>
  1047     for m n q r :: nat
  1058     for m n q r :: nat
  1048     apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
  1059     apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
  1049     apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc)
  1060     apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc)
  1050     done
  1061     done
  1051 qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff)
  1062 qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff bit_nat_def)
       
  1063 
       
  1064 end
  1052 
  1065 
  1053 lemma int_bit_induct [case_names zero minus even odd]:
  1066 lemma int_bit_induct [case_names zero minus even odd]:
  1054   "P k" if zero_int: "P 0"
  1067   "P k" if zero_int: "P 0"
  1055     and minus_int: "P (- 1)"
  1068     and minus_int: "P (- 1)"
  1056     and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)"
  1069     and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)"
  1105     finally show ?case
  1118     finally show ?case
  1106       using odd by simp
  1119       using odd by simp
  1107   qed
  1120   qed
  1108 qed
  1121 qed
  1109 
  1122 
  1110 instance int :: semiring_bits
  1123 instantiation int :: semiring_bits
       
  1124 begin
       
  1125 
       
  1126 definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>
       
  1127   where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close>
       
  1128 
       
  1129 instance
  1111 proof
  1130 proof
  1112   show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close>
  1131   show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close>
  1113     and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close>
  1132     and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close>
  1114     for P and k :: int
  1133     for P and k :: int
  1115   proof (induction k rule: int_bit_induct)
  1134   proof (induction k rule: int_bit_induct)
  1167   show \<open>even (k * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::int) ^ n = 0 \<or> m \<le> n \<and> even (k div 2 ^ (n - m))\<close>
  1186   show \<open>even (k * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::int) ^ n = 0 \<or> m \<le> n \<and> even (k div 2 ^ (n - m))\<close>
  1168     for m n :: nat and k l :: int
  1187     for m n :: nat and k l :: int
  1169     apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
  1188     apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
  1170     apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2))
  1189     apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2))
  1171     done
  1190     done
  1172 qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le)
  1191 qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le bit_int_def)
       
  1192 
       
  1193 end
  1173 
  1194 
  1174 class semiring_bit_shifts = semiring_bits +
  1195 class semiring_bit_shifts = semiring_bits +
  1175   fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
  1196   fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
  1176   assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close>
  1197   assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close>
  1177   fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
  1198   fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
  1178   assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>
  1199   assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>
       
  1200   fixes take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
       
  1201   assumes take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close>
  1179 begin
  1202 begin
  1180 
       
  1181 definition take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
       
  1182   where take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close>
       
  1183 
  1203 
  1184 text \<open>
  1204 text \<open>
  1185   Logically, \<^const>\<open>push_bit\<close>,
  1205   Logically, \<^const>\<open>push_bit\<close>,
  1186   \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
  1206   \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
  1187   as separate operations makes proofs easier, otherwise proof automation
  1207   as separate operations makes proofs easier, otherwise proof automation
  1188   would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
  1208   would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
  1189   algebraic relationships between those operations.
  1209   algebraic relationships between those operations.
  1190   Having
  1210   Having
  1191   \<^const>\<open>push_bit\<close> and \<^const>\<open>drop_bit\<close> as definitional class operations
  1211   them as definitional class operations
  1192   takes into account that specific instances of these can be implemented
  1212   takes into account that specific instances of these can be implemented
  1193   differently wrt. code generation.
  1213   differently wrt. code generation.
  1194 \<close>
  1214 \<close>
  1195 
  1215 
  1196 lemma bit_iff_odd_drop_bit:
  1216 lemma bit_iff_odd_drop_bit:
  1197   \<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>
  1217   \<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>
  1198   by (simp add: bit_def drop_bit_eq_div)
  1218   by (simp add: bit_iff_odd drop_bit_eq_div)
  1199 
  1219 
  1200 lemma even_drop_bit_iff_not_bit:
  1220 lemma even_drop_bit_iff_not_bit:
  1201   \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
  1221   \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
  1202   by (simp add: bit_iff_odd_drop_bit)
  1222   by (simp add: bit_iff_odd_drop_bit)
  1203 
  1223 
  1357   \<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close>
  1377   \<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close>
  1358   by (simp add: push_bit_eq_mult) auto
  1378   by (simp add: push_bit_eq_mult) auto
  1359 
  1379 
  1360 lemma bit_push_bit_iff:
  1380 lemma bit_push_bit_iff:
  1361   \<open>bit (push_bit m a) n \<longleftrightarrow> n \<ge> m \<and> 2 ^ n \<noteq> 0 \<and> (n < m \<or> bit a (n - m))\<close>
  1381   \<open>bit (push_bit m a) n \<longleftrightarrow> n \<ge> m \<and> 2 ^ n \<noteq> 0 \<and> (n < m \<or> bit a (n - m))\<close>
  1362   by (auto simp add: bit_def push_bit_eq_mult even_mult_exp_div_exp_iff)
  1382   by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff)
  1363 
  1383 
  1364 lemma bit_drop_bit_eq:
  1384 lemma bit_drop_bit_eq:
  1365   \<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>
  1385   \<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>
  1366   by (simp add: bit_def fun_eq_iff ac_simps flip: drop_bit_eq_div)
  1386   by (simp add: bit_iff_odd fun_eq_iff ac_simps flip: drop_bit_eq_div)
  1367 
  1387 
  1368 lemma bit_take_bit_iff:
  1388 lemma bit_take_bit_iff:
  1369   \<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>
  1389   \<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>
  1370   by (simp add: bit_def drop_bit_take_bit not_le flip: drop_bit_eq_div)
  1390   by (simp add: bit_iff_odd drop_bit_take_bit not_le flip: drop_bit_eq_div)
  1371 
  1391 
  1372 lemma stable_imp_drop_bit_eq:
  1392 lemma stable_imp_drop_bit_eq:
  1373   \<open>drop_bit n a = a\<close>
  1393   \<open>drop_bit n a = a\<close>
  1374   if \<open>a div 2 = a\<close>
  1394   if \<open>a div 2 = a\<close>
  1375   by (induction n) (simp_all add: that drop_bit_Suc)
  1395   by (induction n) (simp_all add: that drop_bit_Suc)
  1417   where \<open>push_bit_nat n m = m * 2 ^ n\<close>
  1437   where \<open>push_bit_nat n m = m * 2 ^ n\<close>
  1418 
  1438 
  1419 definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
  1439 definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
  1420   where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
  1440   where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
  1421 
  1441 
  1422 instance proof
  1442 definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
  1423   show \<open>push_bit n m = m * 2 ^ n\<close> for n m :: nat
  1443   where \<open>take_bit_nat n m = m mod 2 ^ n\<close>
  1424     by (simp add: push_bit_nat_def)
  1444 
  1425   show \<open>drop_bit n m = m div 2 ^ n\<close> for n m :: nat
  1445 instance
  1426     by (simp add: drop_bit_nat_def)
  1446   by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def)
  1427 qed
       
  1428 
  1447 
  1429 end
  1448 end
  1430 
  1449 
  1431 instantiation int :: semiring_bit_shifts
  1450 instantiation int :: semiring_bit_shifts
  1432 begin
  1451 begin
  1435   where \<open>push_bit_int n k = k * 2 ^ n\<close>
  1454   where \<open>push_bit_int n k = k * 2 ^ n\<close>
  1436 
  1455 
  1437 definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
  1456 definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
  1438   where \<open>drop_bit_int n k = k div 2 ^ n\<close>
  1457   where \<open>drop_bit_int n k = k div 2 ^ n\<close>
  1439 
  1458 
  1440 instance proof
  1459 definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
  1441   show \<open>push_bit n k = k * 2 ^ n\<close> for n :: nat and k :: int
  1460   where \<open>take_bit_int n k = k mod 2 ^ n\<close>
  1442     by (simp add: push_bit_int_def)
  1461 
  1443   show \<open>drop_bit n k = k div 2 ^ n\<close> for n :: nat and k :: int
  1462 instance
  1444     by (simp add: drop_bit_int_def)
  1463   by standard (simp_all add: push_bit_int_def drop_bit_int_def take_bit_int_def)
  1445 qed
       
  1446 
  1464 
  1447 end
  1465 end
  1448 
  1466 
  1449 lemma bit_push_bit_iff_nat:
  1467 lemma bit_push_bit_iff_nat:
  1450   \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
  1468   \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
  1536   have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
  1554   have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
  1537     by simp
  1555     by simp
  1538   also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close>
  1556   also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close>
  1539     by (simp add: of_nat_div)
  1557     by (simp add: of_nat_div)
  1540   finally show ?thesis
  1558   finally show ?thesis
  1541     by (simp add: bit_def semiring_bits_class.bit_def)
  1559     by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd)
  1542 qed
  1560 qed
  1543 
  1561 
  1544 lemma of_nat_push_bit:
  1562 lemma of_nat_push_bit:
  1545   \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
  1563   \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
  1546   by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
  1564   by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)