src/HOL/Word/Bits_Int.thy
changeset 71957 3e162c63371a
parent 71949 5b8b1183c641
child 71984 10a8d943a8d8
equal deleted inserted replaced
71956:a4bffc0de967 71957:3e162c63371a
  1191   by (simp add: numeral_eq_Suc)
  1191   by (simp add: numeral_eq_Suc)
  1192 
  1192 
  1193 instantiation int :: bit_operations
  1193 instantiation int :: bit_operations
  1194 begin
  1194 begin
  1195 
  1195 
  1196 definition int_not_def: "NOT = (\<lambda>x::int. - x - 1)"
       
  1197 
       
  1198 function bitAND_int
       
  1199   where "bitAND_int x y =
       
  1200     (if x = 0 then 0 else if x = -1 then y
       
  1201      else (bin_rest x AND bin_rest y) BIT (bin_last x \<and> bin_last y))"
       
  1202   by pat_completeness simp
       
  1203 
       
  1204 termination
       
  1205   by (relation \<open>measure (nat \<circ> abs \<circ> fst)\<close>) simp_all
       
  1206 
       
  1207 declare bitAND_int.simps [simp del]
       
  1208 
       
  1209 definition int_or_def: "(OR) = (\<lambda>x y::int. NOT (NOT x AND NOT y))"
       
  1210 
       
  1211 definition int_xor_def: "(XOR) = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))"
       
  1212 
       
  1213 definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
  1196 definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
  1214 
  1197 
  1215 definition "lsb i = i !! 0" for i :: int
  1198 definition "lsb i = i !! 0" for i :: int
  1216 
  1199 
  1217 definition "set_bit i n b = bin_sc n b i"
  1200 definition "set_bit i n b = bin_sc n b i"
  1235   by (simp add: shiftr_int_def drop_bit_eq_div)
  1218   by (simp add: shiftr_int_def drop_bit_eq_div)
  1236 
  1219 
  1237 
  1220 
  1238 subsubsection \<open>Basic simplification rules\<close>
  1221 subsubsection \<open>Basic simplification rules\<close>
  1239 
  1222 
       
  1223 lemmas int_not_def = not_int_def
       
  1224 
  1240 lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
  1225 lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
  1241   by (cases b) (simp_all add: int_not_def Bit_def)
  1226   by (simp add: not_int_def Bit_def)
  1242 
  1227 
  1243 lemma int_not_simps [simp]:
  1228 lemma int_not_simps [simp]:
  1244   "NOT (0::int) = -1"
  1229   "NOT (0::int) = -1"
  1245   "NOT (1::int) = -2"
  1230   "NOT (1::int) = -2"
  1246   "NOT (- 1::int) = 0"
  1231   "NOT (- 1::int) = 0"
  1247   "NOT (numeral w::int) = - numeral (w + Num.One)"
  1232   "NOT (numeral w::int) = - numeral (w + Num.One)"
  1248   "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
  1233   "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
  1249   "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
  1234   "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
  1250   unfolding int_not_def by simp_all
  1235   by (simp_all add: not_int_def)
  1251 
  1236 
  1252 lemma int_not_not [simp]: "NOT (NOT x) = x"
  1237 lemma int_not_not: "NOT (NOT x) = x"
  1253   for x :: int
  1238   for x :: int
  1254   unfolding int_not_def by simp
  1239   by (fact bit.double_compl)
  1255 
  1240 
  1256 lemma int_and_0 [simp]: "0 AND x = 0"
  1241 lemma int_and_0 [simp]: "0 AND x = 0"
  1257   for x :: int
  1242   for x :: int
  1258   by (simp add: bitAND_int.simps)
  1243   by (fact bit.conj_zero_left)
  1259 
  1244 
  1260 lemma int_and_m1 [simp]: "-1 AND x = x"
  1245 lemma int_and_m1 [simp]: "-1 AND x = x"
  1261   for x :: int
  1246   for x :: int
  1262   by (simp add: bitAND_int.simps)
  1247   by (fact bit.conj_one_left)
  1263 
  1248 
  1264 lemma int_and_Bits [simp]: "(x BIT b) AND (y BIT c) = (x AND y) BIT (b \<and> c)"
  1249 lemma int_and_Bits [simp]: "(x BIT b) AND (y BIT c) = (x AND y) BIT (b \<and> c)"
  1265   by (subst bitAND_int.simps) (simp add: Bit_eq_0_iff Bit_eq_m1_iff)
  1250   using and_int_rec [of \<open>x BIT b\<close> \<open>y BIT c\<close>] by (auto simp add: Bit_B0_2t Bit_B1_2t)
  1266 
  1251 
  1267 lemma int_or_zero [simp]: "0 OR x = x"
  1252 lemma int_or_zero [simp]: "0 OR x = x"
  1268   for x :: int
  1253   for x :: int
  1269   by (simp add: int_or_def)
  1254   by (fact bit.disj_zero_left)
  1270 
  1255 
  1271 lemma int_or_minus1 [simp]: "-1 OR x = -1"
  1256 lemma int_or_minus1 [simp]: "-1 OR x = -1"
  1272   for x :: int
  1257   for x :: int
  1273   by (simp add: int_or_def)
  1258   by (fact bit.disj_one_left)
  1274 
  1259 
  1275 lemma int_or_Bits [simp]: "(x BIT b) OR (y BIT c) = (x OR y) BIT (b \<or> c)"
  1260 lemma int_or_Bits [simp]: "(x BIT b) OR (y BIT c) = (x OR y) BIT (b \<or> c)"
  1276   by (simp add: int_or_def)
  1261   using or_int_rec [of \<open>x BIT b\<close> \<open>y BIT c\<close>] by (auto simp add: Bit_B0_2t Bit_B1_2t)
  1277 
  1262 
  1278 lemma int_xor_zero [simp]: "0 XOR x = x"
  1263 lemma int_xor_zero [simp]: "0 XOR x = x"
  1279   for x :: int
  1264   for x :: int
  1280   by (simp add: int_xor_def)
  1265   by (fact bit.xor_zero_left)
  1281 
  1266 
  1282 lemma int_xor_Bits [simp]: "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
  1267 lemma int_xor_Bits [simp]: "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
  1283   unfolding int_xor_def by auto
  1268   using xor_int_rec [of \<open>x BIT b\<close> \<open>y BIT c\<close>] by (auto simp add: Bit_B0_2t Bit_B1_2t)
  1284 
  1269 
  1285 
  1270 
  1286 subsubsection \<open>Binary destructors\<close>
  1271 subsubsection \<open>Binary destructors\<close>
  1287 
  1272 
  1288 lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
  1273 lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
  1289   by (cases x rule: bin_exhaust) simp
  1274   by (fact not_int_div_2)
  1290 
  1275 
  1291 lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
  1276 lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
  1292   by (cases x rule: bin_exhaust) simp
  1277   by simp
  1293 
  1278 
  1294 lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
  1279 lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
  1295   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1280   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1296 
  1281 
  1297 lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
  1282 lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
  1304   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1289   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1305 
  1290 
  1306 lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
  1291 lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
  1307   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1292   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1308 
  1293 
  1309 lemma bin_last_XOR [simp]:
  1294 lemma bin_last_XOR [simp]: "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
  1310   "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
       
  1311   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1295   by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
  1312 
  1296 
  1313 lemma bin_nth_ops:
  1297 lemma bin_nth_ops:
  1314   "\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n"
  1298   "\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n"
  1315   "\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n"
  1299   "\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n"
  1316   "\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n"
  1300   "\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n"
  1317   "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
  1301   "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
  1318   by (induct n) (auto simp add: bit_Suc)
  1302   by (simp_all add: bit_and_iff bit_or_iff bit_xor_iff bit_not_iff)
  1319 
  1303 
  1320 
  1304 
  1321 subsubsection \<open>Derived properties\<close>
  1305 subsubsection \<open>Derived properties\<close>
  1322 
  1306 
  1323 lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
  1307 lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
  1324   for x :: int
  1308   for x :: int
  1325   by (auto simp add: bin_eq_iff bin_nth_ops)
  1309   by (fact bit.xor_one_left)
  1326 
  1310 
  1327 lemma int_xor_extra_simps [simp]:
  1311 lemma int_xor_extra_simps [simp]:
  1328   "w XOR 0 = w"
  1312   "w XOR 0 = w"
  1329   "w XOR -1 = NOT w"
  1313   "w XOR -1 = NOT w"
  1330   for w :: int
  1314   for w :: int
  1331   by (auto simp add: bin_eq_iff bin_nth_ops)
  1315   by simp_all
  1332 
  1316 
  1333 lemma int_or_extra_simps [simp]:
  1317 lemma int_or_extra_simps [simp]:
  1334   "w OR 0 = w"
  1318   "w OR 0 = w"
  1335   "w OR -1 = -1"
  1319   "w OR -1 = -1"
  1336   for w :: int
  1320   for w :: int
  1337   by (auto simp add: bin_eq_iff bin_nth_ops)
  1321   by simp_all
  1338 
  1322 
  1339 lemma int_and_extra_simps [simp]:
  1323 lemma int_and_extra_simps [simp]:
  1340   "w AND 0 = 0"
  1324   "w AND 0 = 0"
  1341   "w AND -1 = w"
  1325   "w AND -1 = w"
  1342   for w :: int
  1326   for w :: int
  1343   by (auto simp add: bin_eq_iff bin_nth_ops)
  1327   by simp_all
  1344 
  1328 
  1345 text \<open>Commutativity of the above.\<close>
  1329 text \<open>Commutativity of the above.\<close>
  1346 lemma bin_ops_comm:
  1330 lemma bin_ops_comm:
  1347   fixes x y :: int
  1331   fixes x y :: int
  1348   shows int_and_comm: "x AND y = y AND x"
  1332   shows int_and_comm: "x AND y = y AND x"
  1349     and int_or_comm:  "x OR y = y OR x"
  1333     and int_or_comm:  "x OR y = y OR x"
  1350     and int_xor_comm: "x XOR y = y XOR x"
  1334     and int_xor_comm: "x XOR y = y XOR x"
  1351   by (auto simp add: bin_eq_iff bin_nth_ops)
  1335   by (simp_all add: ac_simps)
  1352 
  1336 
  1353 lemma bin_ops_same [simp]:
  1337 lemma bin_ops_same [simp]:
  1354   "x AND x = x"
  1338   "x AND x = x"
  1355   "x OR x = x"
  1339   "x OR x = x"
  1356   "x XOR x = 0"
  1340   "x XOR x = 0"
  1357   for x :: int
  1341   for x :: int
  1358   by (auto simp add: bin_eq_iff bin_nth_ops)
  1342   by simp_all
  1359 
  1343 
  1360 lemmas bin_log_esimps =
  1344 lemmas bin_log_esimps =
  1361   int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
  1345   int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
  1362   int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
  1346   int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
  1363 
  1347 
  1820   "NOT i < 0 \<longleftrightarrow> i \<ge> 0"
  1804   "NOT i < 0 \<longleftrightarrow> i \<ge> 0"
  1821   "NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1"
  1805   "NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1"
  1822 by(simp_all add: int_not_def) arith+
  1806 by(simp_all add: int_not_def) arith+
  1823 
  1807 
  1824 lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z"
  1808 lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z"
  1825 by(metis int_and_comm bbw_ao_dist)
  1809   by (fact bit.conj_disj_distrib)
  1826 
  1810 
  1827 lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc
  1811 lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc
  1828 
  1812 
  1829 lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0"
  1813 lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0"
  1830 by(induct x y\<equiv>"NOT x" rule: bitAND_int.induct)(subst bitAND_int.simps, clarsimp)
  1814   by simp
  1831 
  1815 
  1832 lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0"
  1816 lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0"
  1833 by (metis bbw_lcs(1) int_and_0 int_nand_same)
  1817   by (simp add: bit_eq_iff bit_and_iff bit_not_iff)
  1834 
  1818 
  1835 lemma and_xor_dist: fixes x :: int shows
  1819 lemma and_xor_dist: fixes x :: int shows
  1836   "x AND (y XOR z) = (x AND y) XOR (x AND z)"
  1820   "x AND (y XOR z) = (x AND y) XOR (x AND z)"
  1837   by (simp add: int_xor_def bbw_ao_dist2 bbw_not_dist int_and_comm int_nand_same_middle)
  1821   by (fact bit.conj_xor_distrib)
  1838 
  1822 
  1839 lemma int_and_lt0 [simp]: fixes x y :: int shows
  1823 lemma int_and_lt0 [simp]:
  1840   "x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0"
  1824   \<open>x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0\<close> for x y :: int
  1841 by(induct x y rule: bitAND_int.induct)(subst bitAND_int.simps, simp)
  1825   by (fact and_negative_int_iff)
  1842 
  1826 
  1843 lemma int_and_ge0 [simp]: fixes x y :: int shows 
  1827 lemma int_and_ge0 [simp]: 
  1844   "x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0"
  1828   \<open>x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0\<close> for x y :: int
  1845 by (metis int_and_lt0 linorder_not_less)
  1829   by (fact and_nonnegative_int_iff)
  1846 
  1830   
  1847 lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2"
  1831 lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2"
  1848 by(subst bitAND_int.simps)(simp add: Bit_def bin_last_def zmod_minus1)
  1832   by (fact and_one_eq)
  1849 
  1833 
  1850 lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2"
  1834 lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2"
  1851 by(subst int_and_comm)(simp add: int_and_1)
  1835   by (fact one_and_eq)
  1852 
  1836 
  1853 lemma int_or_lt0 [simp]: fixes x y :: int shows 
  1837 lemma int_or_lt0 [simp]: 
  1854   "x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0"
  1838   \<open>x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0\<close> for x y :: int
  1855 by(simp add: int_or_def)
  1839   by (fact or_negative_int_iff)
  1856 
  1840 
  1857 lemma int_xor_lt0 [simp]: fixes x y :: int shows
  1841 lemma int_or_ge0 [simp]:
  1858   "x XOR y < 0 \<longleftrightarrow> ((x < 0) \<noteq> (y < 0))"
  1842   \<open>x OR y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<and> y \<ge> 0\<close> for x y :: int
  1859 by(auto simp add: int_xor_def)
  1843   by (fact or_nonnegative_int_iff)
  1860 
  1844   
  1861 lemma int_xor_ge0 [simp]: fixes x y :: int shows
  1845 lemma int_xor_lt0 [simp]:
  1862   "x XOR y \<ge> 0 \<longleftrightarrow> ((x \<ge> 0) \<longleftrightarrow> (y \<ge> 0))"
  1846   \<open>x XOR y < 0 \<longleftrightarrow> (x < 0) \<noteq> (y < 0)\<close> for x y :: int
  1863 by (metis int_xor_lt0 linorder_not_le)
  1847   by (fact xor_negative_int_iff)
  1864 
  1848 
       
  1849 lemma int_xor_ge0 [simp]:
       
  1850   \<open>x XOR y \<ge> 0 \<longleftrightarrow> (x \<ge> 0 \<longleftrightarrow> y \<ge> 0)\<close> for x y :: int
       
  1851   by (fact xor_nonnegative_int_iff)
       
  1852   
  1865 lemma even_conv_AND:
  1853 lemma even_conv_AND:
  1866   \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int
  1854   \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int
  1867 proof -
  1855   by (simp add: and_one_eq mod2_eq_if)
  1868   obtain x b where \<open>i = x BIT b\<close>
       
  1869     by (cases i rule: bin_exhaust)
       
  1870   then have "i AND 1 = 0 BIT b"
       
  1871     by (simp add: BIT_special_simps(2) [symmetric] del: BIT_special_simps(2))
       
  1872   then show ?thesis
       
  1873     using \<open>i = x BIT b\<close> by (cases b) simp_all
       
  1874 qed
       
  1875 
  1856 
  1876 lemma bin_last_conv_AND:
  1857 lemma bin_last_conv_AND:
  1877   "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0"
  1858   "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0"
  1878   by (simp add: even_conv_AND)
  1859   by (simp add: and_one_eq mod2_eq_if)
  1879 
  1860 
  1880 lemma bitval_bin_last:
  1861 lemma bitval_bin_last:
  1881   "of_bool (bin_last i) = i AND 1"
  1862   "of_bool (bin_last i) = i AND 1"
  1882 proof -
  1863   by (simp add: and_one_eq mod2_eq_if)
  1883   obtain x b where "i = x BIT b" by(cases i rule: bin_exhaust)
       
  1884   hence "i AND 1 = 0 BIT b"
       
  1885     by(simp add: BIT_special_simps(2)[symmetric] del: BIT_special_simps(2))
       
  1886   thus ?thesis by(cases b)(simp_all add: bin_last_conv_AND)
       
  1887 qed
       
  1888 
  1864 
  1889 lemma bin_sign_and:
  1865 lemma bin_sign_and:
  1890   "bin_sign (i AND j) = - (bin_sign i * bin_sign j)"
  1866   "bin_sign (i AND j) = - (bin_sign i * bin_sign j)"
  1891 by(simp add: bin_sign_def)
  1867 by(simp add: bin_sign_def)
  1892 
  1868 
  1899 lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)"
  1875 lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)"
  1900 by(simp add: int_not_def)
  1876 by(simp add: int_not_def)
  1901 
  1877 
  1902 
  1878 
  1903 subsection \<open>Setting and clearing bits\<close>
  1879 subsection \<open>Setting and clearing bits\<close>
  1904 
       
  1905 
       
  1906 
  1880 
  1907 lemma int_lsb_BIT [simp]: fixes x :: int shows
  1881 lemma int_lsb_BIT [simp]: fixes x :: int shows
  1908   "lsb (x BIT b) \<longleftrightarrow> b"
  1882   "lsb (x BIT b) \<longleftrightarrow> b"
  1909 by(simp add: lsb_int_def)
  1883 by(simp add: lsb_int_def)
  1910 
  1884