src/HOL/Bit_Operations.thy
changeset 79068 cb72e2c0c539
parent 79031 4596a14d9a95
child 79069 48ca09068adf
equal deleted inserted replaced
79067:212c94edae2b 79068:cb72e2c0c539
   735   \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
   735   \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
   736   as separate operations makes proofs easier, otherwise proof automation
   736   as separate operations makes proofs easier, otherwise proof automation
   737   would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
   737   would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
   738   algebraic relationships between those operations.
   738   algebraic relationships between those operations.
   739 
   739 
   740   For the sake of code generation operations 
   740   For the sake of code generation operations
   741   are specified as definitional class operations,
   741   are specified as definitional class operations,
   742   taking into account that specific instances of these can be implemented
   742   taking into account that specific instances of these can be implemented
   743   differently wrt. code generation.
   743   differently wrt. code generation.
   744 \<close>
   744 \<close>
   745 
   745 
  1062   then show ?P
  1062   then show ?P
  1063     by (simp add: take_bit_push_bit)
  1063     by (simp add: take_bit_push_bit)
  1064 qed
  1064 qed
  1065 
  1065 
  1066 lemma take_bit_tightened:
  1066 lemma take_bit_tightened:
  1067   \<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close> 
  1067   \<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close>
  1068 proof -
  1068 proof -
  1069   from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close>
  1069   from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close>
  1070     by simp
  1070     by simp
  1071   then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close>
  1071   then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close>
  1072     by simp
  1072     by simp
  1082   proof (rule bit_eqI)
  1082   proof (rule bit_eqI)
  1083     fix m
  1083     fix m
  1084     from \<open>?P\<close> have \<open>a = take_bit n a\<close> ..
  1084     from \<open>?P\<close> have \<open>a = take_bit n a\<close> ..
  1085     also have \<open>\<not> bit (take_bit n a) (n + m)\<close>
  1085     also have \<open>\<not> bit (take_bit n a) (n + m)\<close>
  1086       unfolding bit_simps
  1086       unfolding bit_simps
  1087       by (simp add: bit_simps) 
  1087       by (simp add: bit_simps)
  1088     finally show \<open>bit (drop_bit n a) m \<longleftrightarrow> bit 0 m\<close>
  1088     finally show \<open>bit (drop_bit n a) m \<longleftrightarrow> bit 0 m\<close>
  1089       by (simp add: bit_simps)
  1089       by (simp add: bit_simps)
  1090   qed
  1090   qed
  1091 next
  1091 next
  1092   assume ?Q
  1092   assume ?Q
  1334 
  1334 
  1335 lemma take_bit_sum:
  1335 lemma take_bit_sum:
  1336   \<open>take_bit n a = (\<Sum>k = 0..<n. push_bit k (of_bool (bit a k)))\<close>
  1336   \<open>take_bit n a = (\<Sum>k = 0..<n. push_bit k (of_bool (bit a k)))\<close>
  1337   by (simp flip: horner_sum_bit_eq_take_bit add: horner_sum_eq_sum push_bit_eq_mult)
  1337   by (simp flip: horner_sum_bit_eq_take_bit add: horner_sum_eq_sum push_bit_eq_mult)
  1338 
  1338 
  1339 lemmas set_bit_def = set_bit_eq_or
       
  1340 
       
  1341 lemmas flip_bit_def = flip_bit_eq_xor
       
  1342 
       
  1343 end
  1339 end
  1344 
  1340 
  1345 class ring_bit_operations = semiring_bit_operations + ring_parity +
  1341 class ring_bit_operations = semiring_bit_operations + ring_parity +
  1346   fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
  1342   fixes not :: \<open>'a \<Rightarrow> 'a\<close>  (\<open>NOT\<close>)
  1347   assumes not_rec: \<open>NOT a = of_bool (even a) + 2 * NOT (a div 2)\<close>
  1343   assumes not_rec: \<open>NOT a = of_bool (even a) + 2 * NOT (a div 2)\<close>
  1374 lemma bit_not_iff [bit_simps]:
  1370 lemma bit_not_iff [bit_simps]:
  1375   \<open>bit (NOT a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit a n\<close>
  1371   \<open>bit (NOT a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit a n\<close>
  1376 proof (cases \<open>possible_bit TYPE('a) n\<close>)
  1372 proof (cases \<open>possible_bit TYPE('a) n\<close>)
  1377   case False
  1373   case False
  1378   then show ?thesis
  1374   then show ?thesis
  1379     by (auto dest: bit_imp_possible_bit) 
  1375     by (auto dest: bit_imp_possible_bit)
  1380 next
  1376 next
  1381   case True
  1377   case True
  1382   moreover have \<open>bit (NOT a) n \<longleftrightarrow> \<not> bit a n\<close>
  1378   moreover have \<open>bit (NOT a) n \<longleftrightarrow> \<not> bit a n\<close>
  1383   using \<open>possible_bit TYPE('a) n\<close> proof (induction n arbitrary: a)
  1379   using \<open>possible_bit TYPE('a) n\<close> proof (induction n arbitrary: a)
  1384     case 0
  1380     case 0
  1551   by (rule bit_eqI) (auto simp add: bit_simps)
  1547   by (rule bit_eqI) (auto simp add: bit_simps)
  1552 
  1548 
  1553 lemma push_bit_numeral_minus_1 [simp]:
  1549 lemma push_bit_numeral_minus_1 [simp]:
  1554   \<open>push_bit (numeral n) (- 1) = - (2 ^ numeral n)\<close>
  1550   \<open>push_bit (numeral n) (- 1) = - (2 ^ numeral n)\<close>
  1555   by (simp add: push_bit_eq_mult)
  1551   by (simp add: push_bit_eq_mult)
  1556 
       
  1557 lemmas unset_bit_def = unset_bit_eq_and_not
       
  1558 
  1552 
  1559 end
  1553 end
  1560 
  1554 
  1561 
  1555 
  1562 subsection \<open>Instance \<^typ>\<open>int\<close>\<close>
  1556 subsection \<open>Instance \<^typ>\<open>int\<close>\<close>
  1642 begin
  1636 begin
  1643 
  1637 
  1644 definition not_int :: \<open>int \<Rightarrow> int\<close>
  1638 definition not_int :: \<open>int \<Rightarrow> int\<close>
  1645   where \<open>not_int k = - k - 1\<close>
  1639   where \<open>not_int k = - k - 1\<close>
  1646 
  1640 
  1647 lemma not_int_rec:
  1641 global_interpretation and_int: fold2_bit_int \<open>(\<and>)\<close>
  1648   \<open>NOT k = of_bool (even k) + 2 * NOT (k div 2)\<close> for k :: int
  1642   defines and_int = and_int.F .
  1649   by (auto simp add: not_int_def elim: oddE)
  1643 
  1650 
  1644 global_interpretation or_int: fold2_bit_int \<open>(\<or>)\<close>
  1651 lemma even_not_iff_int:
  1645   defines or_int = or_int.F .
  1652   \<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int
  1646 
  1653   by (simp add: not_int_def)
  1647 global_interpretation xor_int: fold2_bit_int \<open>(\<noteq>)\<close>
  1654 
  1648   defines xor_int = xor_int.F .
  1655 lemma not_int_div_2:
  1649 
  1656   \<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int
  1650 definition mask_int :: \<open>nat \<Rightarrow> int\<close>
  1657   by (simp add: not_int_def)
  1651   where \<open>mask n = (2 :: int) ^ n - 1\<close>
       
  1652 
       
  1653 definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1654   where \<open>push_bit_int n k = k * 2 ^ n\<close>
       
  1655 
       
  1656 definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1657   where \<open>drop_bit_int n k = k div 2 ^ n\<close>
       
  1658 
       
  1659 definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1660   where \<open>take_bit_int n k = k mod 2 ^ n\<close>
       
  1661 
       
  1662 definition set_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1663   where \<open>set_bit n k = k OR push_bit n 1\<close> for k :: int
       
  1664 
       
  1665 definition unset_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1666   where \<open>unset_bit n k = k AND NOT (push_bit n 1)\<close> for k :: int
       
  1667 
       
  1668 definition flip_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1669   where \<open>flip_bit n k = k XOR push_bit n 1\<close> for k :: int
  1658 
  1670 
  1659 lemma bit_not_int_iff:
  1671 lemma bit_not_int_iff:
  1660   \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close>
  1672   \<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close>
  1661   for k :: int
  1673   for k :: int
  1662   by (simp add: bit_not_int_iff' not_int_def)
  1674   by (simp add: bit_not_int_iff' not_int_def)
  1663 
       
  1664 global_interpretation and_int: fold2_bit_int \<open>(\<and>)\<close>
       
  1665   defines and_int = and_int.F .
       
  1666 
       
  1667 global_interpretation or_int: fold2_bit_int \<open>(\<or>)\<close>
       
  1668   defines or_int = or_int.F .
       
  1669 
       
  1670 global_interpretation xor_int: fold2_bit_int \<open>(\<noteq>)\<close>
       
  1671   defines xor_int = xor_int.F .
       
  1672 
       
  1673 definition mask_int :: \<open>nat \<Rightarrow> int\<close>
       
  1674   where \<open>mask n = (2 :: int) ^ n - 1\<close>
       
  1675 
       
  1676 definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1677   where \<open>push_bit_int n k = k * 2 ^ n\<close>
       
  1678 
       
  1679 definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1680   where \<open>drop_bit_int n k = k div 2 ^ n\<close>
       
  1681 
       
  1682 definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1683   where \<open>take_bit_int n k = k mod 2 ^ n\<close>
       
  1684 
       
  1685 definition set_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1686   where \<open>set_bit n k = k OR push_bit n 1\<close> for k :: int
       
  1687 
       
  1688 definition unset_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1689   where \<open>unset_bit n k = k AND NOT (push_bit n 1)\<close> for k :: int
       
  1690 
       
  1691 definition flip_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
       
  1692   where \<open>flip_bit n k = k XOR push_bit n 1\<close> for k :: int
       
  1693 
  1675 
  1694 instance proof
  1676 instance proof
  1695   fix k l :: int and m n :: nat
  1677   fix k l :: int and m n :: nat
  1696   show \<open>- k = NOT (k - 1)\<close>
  1678   show \<open>- k = NOT (k - 1)\<close>
  1697     by (simp add: not_int_def)
  1679     by (simp add: not_int_def)
  1706 qed (fact and_int.rec or_int.rec xor_int.rec mask_int_def set_bit_int_def flip_bit_int_def
  1688 qed (fact and_int.rec or_int.rec xor_int.rec mask_int_def set_bit_int_def flip_bit_int_def
  1707   push_bit_int_def drop_bit_int_def take_bit_int_def)+
  1689   push_bit_int_def drop_bit_int_def take_bit_int_def)+
  1708 
  1690 
  1709 end
  1691 end
  1710 
  1692 
  1711 lemmas and_int_rec = and_int.rec
  1693 lemma not_int_div_2:
  1712 
  1694   \<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int
  1713 lemmas bit_and_int_iff = and_int.bit_iff
  1695   by (simp add: not_int_def)
  1714 
       
  1715 lemmas or_int_rec = or_int.rec
       
  1716 
       
  1717 lemmas bit_or_int_iff = or_int.bit_iff
       
  1718 
       
  1719 lemmas xor_int_rec = xor_int.rec
       
  1720 
       
  1721 lemmas bit_xor_int_iff = xor_int.bit_iff
       
  1722 
       
  1723 lemma even_and_iff_int:
       
  1724   \<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int
       
  1725   by (fact even_and_iff)
       
  1726 
  1696 
  1727 lemma bit_push_bit_iff_int:
  1697 lemma bit_push_bit_iff_int:
  1728   \<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int
  1698   \<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int
  1729   by (auto simp add: bit_push_bit_iff)
  1699   by (auto simp add: bit_push_bit_iff)
  1730 
  1700 
  1789   then show ?case
  1759   then show ?case
  1790     by simp
  1760     by simp
  1791 next
  1761 next
  1792   case (even k)
  1762   case (even k)
  1793   then show ?case
  1763   then show ?case
  1794     using and_int_rec [of \<open>k * 2\<close> l]
  1764     using and_int.rec [of \<open>k * 2\<close> l]
  1795     by (simp add: pos_imp_zdiv_nonneg_iff zero_le_mult_iff)
  1765     by (simp add: pos_imp_zdiv_nonneg_iff zero_le_mult_iff)
  1796 next
  1766 next
  1797   case (odd k)
  1767   case (odd k)
  1798   from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close>
  1768   from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close>
  1799     by simp
  1769     by simp
  1800   then have \<open>0 \<le> (1 + k * 2) div 2 AND l div 2 \<longleftrightarrow> 0 \<le> (1 + k * 2) div 2 \<or> 0 \<le> l div 2\<close>
  1770   then have \<open>0 \<le> (1 + k * 2) div 2 AND l div 2 \<longleftrightarrow> 0 \<le> (1 + k * 2) div 2 \<or> 0 \<le> l div 2\<close>
  1801     by simp
  1771     by simp
  1802   with and_int_rec [of \<open>1 + k * 2\<close> l]
  1772   with and_int.rec [of \<open>1 + k * 2\<close> l]
  1803   show ?case
  1773   show ?case
  1804     by (auto simp add: zero_le_mult_iff not_le)
  1774     by (auto simp add: zero_le_mult_iff not_le)
  1805 qed
  1775 qed
  1806 
  1776 
  1807 lemma and_negative_int_iff [simp]:
  1777 lemma and_negative_int_iff [simp]:
  1820     by simp
  1790     by simp
  1821 next
  1791 next
  1822   case (even k)
  1792   case (even k)
  1823   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
  1793   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
  1824   show ?case
  1794   show ?case
  1825     by (simp add: and_int_rec [of _ l])
  1795     by (simp add: and_int.rec [of _ l])
  1826 next
  1796 next
  1827   case (odd k)
  1797   case (odd k)
  1828   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
  1798   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
  1829   show ?case
  1799   show ?case
  1830     by (simp add: and_int_rec [of _ l])
  1800     by (simp add: and_int.rec [of _ l])
  1831 qed
  1801 qed
  1832 
  1802 
  1833 lemma or_nonnegative_int_iff [simp]:
  1803 lemma or_nonnegative_int_iff [simp]:
  1834   \<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int
  1804   \<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int
  1835   by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp
  1805   by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp
  1850     by simp
  1820     by simp
  1851 next
  1821 next
  1852   case (even k)
  1822   case (even k)
  1853   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
  1823   from even.IH [of \<open>l div 2\<close>] even.hyps even.prems
  1854   show ?case
  1824   show ?case
  1855     by (simp add: or_int_rec [of _ l])
  1825     by (simp add: or_int.rec [of _ l])
  1856 next
  1826 next
  1857   case (odd k)
  1827   case (odd k)
  1858   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
  1828   from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems
  1859   show ?case
  1829   show ?case
  1860     by (simp add: or_int_rec [of _ l])
  1830     by (simp add: or_int.rec [of _ l])
  1861 qed
  1831 qed
  1862 
  1832 
  1863 lemma xor_nonnegative_int_iff [simp]:
  1833 lemma xor_nonnegative_int_iff [simp]:
  1864   \<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int
  1834   \<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int
  1865   by (simp only: bit.xor_def or_nonnegative_int_iff) auto
  1835   by (simp only: bit.xor_def or_nonnegative_int_iff) auto
  1879   then show ?case
  1849   then show ?case
  1880     by simp
  1850     by simp
  1881 next
  1851 next
  1882   case (even x)
  1852   case (even x)
  1883   from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
  1853   from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
  1884   show ?case 
  1854   show ?case
  1885     by (cases n) (auto simp add: or_int_rec [of \<open>_ * 2\<close>] elim: oddE)
  1855     by (cases n) (auto simp add: or_int.rec [of \<open>_ * 2\<close>] elim: oddE)
  1886 next
  1856 next
  1887   case (odd x)
  1857   case (odd x)
  1888   from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
  1858   from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
  1889   show ?case
  1859   show ?case
  1890     by (cases n) (auto simp add: or_int_rec [of \<open>1 + _ * 2\<close>], linarith)
  1860     by (cases n) (auto simp add: or_int.rec [of \<open>1 + _ * 2\<close>], linarith)
  1891 qed
  1861 qed
  1892 
  1862 
  1893 lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1863 lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1894   \<open>x XOR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int
  1864   \<open>x XOR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int
  1895 using that proof (induction x arbitrary: y n rule: int_bit_induct)
  1865 using that proof (induction x arbitrary: y n rule: int_bit_induct)
  1901   then show ?case
  1871   then show ?case
  1902     by simp
  1872     by simp
  1903 next
  1873 next
  1904   case (even x)
  1874   case (even x)
  1905   from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
  1875   from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
  1906   show ?case 
  1876   show ?case
  1907     by (cases n) (auto simp add: xor_int_rec [of \<open>_ * 2\<close>] elim: oddE)
  1877     by (cases n) (auto simp add: xor_int.rec [of \<open>_ * 2\<close>] elim: oddE)
  1908 next
  1878 next
  1909   case (odd x)
  1879   case (odd x)
  1910   from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
  1880   from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
  1911   show ?case
  1881   show ?case
  1912     by (cases n) (auto simp add: xor_int_rec [of \<open>1 + _ * 2\<close>])
  1882     by (cases n) (auto simp add: xor_int.rec [of \<open>1 + _ * 2\<close>])
  1913 qed
  1883 qed
  1914 
  1884 
  1915 lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1885 lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1916   \<open>0 \<le> x AND y\<close> if \<open>0 \<le> x\<close> for x y :: int
  1886   \<open>0 \<le> x AND y\<close> if \<open>0 \<le> x\<close> for x y :: int
  1917   using that by simp
  1887   using that by simp
  1928   \<open>x AND y \<le> x\<close> if \<open>0 \<le> x\<close> for x y :: int
  1898   \<open>x AND y \<le> x\<close> if \<open>0 \<le> x\<close> for x y :: int
  1929 using that proof (induction x arbitrary: y rule: int_bit_induct)
  1899 using that proof (induction x arbitrary: y rule: int_bit_induct)
  1930   case (odd k)
  1900   case (odd k)
  1931   then have \<open>k AND y div 2 \<le> k\<close>
  1901   then have \<open>k AND y div 2 \<le> k\<close>
  1932     by simp
  1902     by simp
  1933   then show ?case 
  1903   then show ?case
  1934     by (simp add: and_int_rec [of \<open>1 + _ * 2\<close>])
  1904     by (simp add: and_int.rec [of \<open>1 + _ * 2\<close>])
  1935 qed (simp_all add: and_int_rec [of \<open>_ * 2\<close>])
  1905 qed (simp_all add: and_int.rec [of \<open>_ * 2\<close>])
  1936 
  1906 
  1937 lemma AND_upper1' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1907 lemma AND_upper1' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
  1938   \<open>y AND x \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y z :: int
  1908   \<open>y AND x \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y z :: int
  1939   using _ \<open>y \<le> z\<close> by (rule order_trans) (use \<open>0 \<le> y\<close> in simp)
  1909   using _ \<open>y \<le> z\<close> by (rule order_trans) (use \<open>0 \<le> y\<close> in simp)
  1940 
  1910 
  1966     by simp
  1936     by simp
  1967 next
  1937 next
  1968   case (even x)
  1938   case (even x)
  1969   from even.IH [of \<open>y div 2\<close>]
  1939   from even.IH [of \<open>y div 2\<close>]
  1970   show ?case
  1940   show ?case
  1971     by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
  1941     by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE)
  1972 next
  1942 next
  1973   case (odd x)
  1943   case (odd x)
  1974   from odd.IH [of \<open>y div 2\<close>]
  1944   from odd.IH [of \<open>y div 2\<close>]
  1975   show ?case
  1945   show ?case
  1976     by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
  1946     by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE)
  1977 qed
  1947 qed
  1978 
  1948 
  1979 lemma push_bit_minus_one:
  1949 lemma push_bit_minus_one:
  1980   \<open>push_bit n (- 1 :: int) = - (2 ^ n)\<close>
  1950   \<open>push_bit n (- 1 :: int) = - (2 ^ n)\<close>
  1981   by (simp add: push_bit_eq_mult)
  1951   by (simp add: push_bit_eq_mult)
  2062   \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2032   \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2063   by (subst Not_eq_iff [symmetric]) (simp add: not_less)
  2033   by (subst Not_eq_iff [symmetric]) (simp add: not_less)
  2064 
  2034 
  2065 lemma set_bit_nonnegative_int_iff [simp]:
  2035 lemma set_bit_nonnegative_int_iff [simp]:
  2066   \<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2036   \<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2067   by (simp add: set_bit_def)
  2037   by (simp add: set_bit_eq_or)
  2068 
  2038 
  2069 lemma set_bit_negative_int_iff [simp]:
  2039 lemma set_bit_negative_int_iff [simp]:
  2070   \<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2040   \<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2071   by (simp add: set_bit_def)
  2041   by (simp add: set_bit_eq_or)
  2072 
  2042 
  2073 lemma unset_bit_nonnegative_int_iff [simp]:
  2043 lemma unset_bit_nonnegative_int_iff [simp]:
  2074   \<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2044   \<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2075   by (simp add: unset_bit_def)
  2045   by (simp add: unset_bit_eq_and_not)
  2076 
  2046 
  2077 lemma unset_bit_negative_int_iff [simp]:
  2047 lemma unset_bit_negative_int_iff [simp]:
  2078   \<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2048   \<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2079   by (simp add: unset_bit_def)
  2049   by (simp add: unset_bit_eq_and_not)
  2080 
  2050 
  2081 lemma flip_bit_nonnegative_int_iff [simp]:
  2051 lemma flip_bit_nonnegative_int_iff [simp]:
  2082   \<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2052   \<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
  2083   by (simp add: flip_bit_def)
  2053   by (simp add: flip_bit_eq_xor)
  2084 
  2054 
  2085 lemma flip_bit_negative_int_iff [simp]:
  2055 lemma flip_bit_negative_int_iff [simp]:
  2086   \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2056   \<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
  2087   by (simp add: flip_bit_def)
  2057   by (simp add: flip_bit_eq_xor)
  2088 
  2058 
  2089 lemma set_bit_greater_eq:
  2059 lemma set_bit_greater_eq:
  2090   \<open>set_bit n k \<ge> k\<close> for k :: int
  2060   \<open>set_bit n k \<ge> k\<close> for k :: int
  2091   by (simp add: set_bit_def or_greater_eq)
  2061   by (simp add: set_bit_eq_or or_greater_eq)
  2092 
  2062 
  2093 lemma unset_bit_less_eq:
  2063 lemma unset_bit_less_eq:
  2094   \<open>unset_bit n k \<le> k\<close> for k :: int
  2064   \<open>unset_bit n k \<le> k\<close> for k :: int
  2095   by (simp add: unset_bit_def and_less_eq)
  2065   by (simp add: unset_bit_eq_and_not and_less_eq)
  2096 
  2066 
  2097 lemma set_bit_eq:
  2067 lemma set_bit_eq:
  2098   \<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int
  2068   \<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int
  2099 proof (rule bit_eqI)
  2069 proof -
  2100   fix m
  2070   have \<open>set_bit n k = k OR of_bool (\<not> bit k n) * 2 ^ n\<close>
  2101   show \<open>bit (set_bit n k) m \<longleftrightarrow> bit (k + of_bool (\<not> bit k n) * 2 ^ n) m\<close>
  2071     by (rule bit_eqI) (auto simp add: bit_simps)
  2102   proof (cases \<open>m = n\<close>)
  2072   then show ?thesis
  2103     case True
  2073     by (subst disjunctive_add) (auto simp add: bit_simps)
  2104     then show ?thesis
       
  2105       apply (simp add: bit_set_bit_iff)
       
  2106       apply (simp add: bit_iff_odd div_plus_div_distrib_dvd_right)
       
  2107       done
       
  2108   next
       
  2109     case False
       
  2110     then show ?thesis
       
  2111       apply (clarsimp simp add: bit_set_bit_iff)
       
  2112       apply (subst disjunctive_add)
       
  2113       apply (clarsimp simp add: bit_exp_iff)
       
  2114       apply (clarsimp simp add: bit_or_iff bit_exp_iff)
       
  2115       done
       
  2116   qed
       
  2117 qed
  2074 qed
  2118 
  2075 
  2119 lemma unset_bit_eq:
  2076 lemma unset_bit_eq:
  2120   \<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int
  2077   \<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int
  2121 proof (rule bit_eqI)
  2078 proof -
  2122   fix m
  2079   have \<open>unset_bit n k = k AND NOT (of_bool (bit k n) * 2 ^ n)\<close>
  2123   show \<open>bit (unset_bit n k) m \<longleftrightarrow> bit (k - of_bool (bit k n) * 2 ^ n) m\<close>
  2080     by (rule bit_eqI) (auto simp add: bit_simps)
  2124   proof (cases \<open>m = n\<close>)
  2081   then show ?thesis
  2125     case True
  2082     by (subst disjunctive_diff) (auto simp add: bit_simps simp flip: push_bit_eq_mult)
  2126     then show ?thesis
       
  2127       apply (simp add: bit_unset_bit_iff)
       
  2128       apply (simp add: bit_iff_odd)
       
  2129       using div_plus_div_distrib_dvd_right [of \<open>2 ^ n\<close> \<open>- (2 ^ n)\<close> k]
       
  2130       apply (simp add: dvd_neg_div)
       
  2131       done
       
  2132   next
       
  2133     case False
       
  2134     then show ?thesis
       
  2135       apply (clarsimp simp add: bit_unset_bit_iff)
       
  2136       apply (subst disjunctive_diff)
       
  2137       apply (clarsimp simp add: bit_exp_iff)
       
  2138       apply (clarsimp simp add: bit_and_iff bit_not_iff bit_exp_iff)
       
  2139       done
       
  2140   qed
       
  2141 qed
  2083 qed
  2142 
  2084 
  2143 lemma and_int_unfold:
  2085 lemma and_int_unfold:
  2144   \<open>k AND l = (if k = 0 \<or> l = 0 then 0 else if k = - 1 then l else if l = - 1 then k
  2086   \<open>k AND l = (if k = 0 \<or> l = 0 then 0 else if k = - 1 then l else if l = - 1 then k
  2145     else (k mod 2) * (l mod 2) + 2 * ((k div 2) AND (l div 2)))\<close> for k l :: int
  2087     else (k mod 2) * (l mod 2) + 2 * ((k div 2) AND (l div 2)))\<close> for k l :: int
  2146   by (auto simp add: and_int_rec [of k l] zmult_eq_1_iff elim: oddE)
  2088   by (auto simp add: and_int.rec [of k l] zmult_eq_1_iff elim: oddE)
  2147 
  2089 
  2148 lemma or_int_unfold:
  2090 lemma or_int_unfold:
  2149   \<open>k OR l = (if k = - 1 \<or> l = - 1 then - 1 else if k = 0 then l else if l = 0 then k
  2091   \<open>k OR l = (if k = - 1 \<or> l = - 1 then - 1 else if k = 0 then l else if l = 0 then k
  2150     else max (k mod 2) (l mod 2) + 2 * ((k div 2) OR (l div 2)))\<close> for k l :: int
  2092     else max (k mod 2) (l mod 2) + 2 * ((k div 2) OR (l div 2)))\<close> for k l :: int
  2151   by (auto simp add: or_int_rec [of k l] elim: oddE)
  2093   by (auto simp add: or_int.rec [of k l] elim: oddE)
  2152 
  2094 
  2153 lemma xor_int_unfold:
  2095 lemma xor_int_unfold:
  2154   \<open>k XOR l = (if k = - 1 then NOT l else if l = - 1 then NOT k else if k = 0 then l else if l = 0 then k
  2096   \<open>k XOR l = (if k = - 1 then NOT l else if l = - 1 then NOT k else if k = 0 then l else if l = 0 then k
  2155     else \<bar>k mod 2 - l mod 2\<bar> + 2 * ((k div 2) XOR (l div 2)))\<close> for k l :: int
  2097     else \<bar>k mod 2 - l mod 2\<bar> + 2 * ((k div 2) XOR (l div 2)))\<close> for k l :: int
  2156   by (auto simp add: xor_int_rec [of k l] not_int_def elim!: oddE)
  2098   by (auto simp add: xor_int.rec [of k l] not_int_def elim!: oddE)
  2157 
  2099 
  2158 lemma bit_minus_int_iff:
  2100 lemma bit_minus_int_iff:
  2159   \<open>bit (- k) n \<longleftrightarrow> bit (NOT (k - 1)) n\<close> for k :: int
  2101   \<open>bit (- k) n \<longleftrightarrow> bit (NOT (k - 1)) n\<close> for k :: int
  2160   by (simp add: bit_simps)
  2102   by (simp add: bit_simps)
  2161 
  2103 
  2477 qed (simp_all add: mask_nat_def set_bit_nat_def flip_bit_nat_def push_bit_nat_def drop_bit_nat_def take_bit_nat_def)
  2419 qed (simp_all add: mask_nat_def set_bit_nat_def flip_bit_nat_def push_bit_nat_def drop_bit_nat_def take_bit_nat_def)
  2478 
  2420 
  2479 end
  2421 end
  2480 
  2422 
  2481 lemma take_bit_nat_less_exp [simp]:
  2423 lemma take_bit_nat_less_exp [simp]:
  2482   \<open>take_bit n m < 2 ^ n\<close> for n m :: nat 
  2424   \<open>take_bit n m < 2 ^ n\<close> for n m :: nat
  2483   by (simp add: take_bit_eq_mod)
  2425   by (simp add: take_bit_eq_mod)
  2484 
  2426 
  2485 lemma take_bit_nat_eq_self_iff:
  2427 lemma take_bit_nat_eq_self_iff:
  2486   \<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for n m :: nat
  2428   \<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for n m :: nat
  2487 proof
  2429 proof
  2522   \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
  2464   \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
  2523   by (auto simp add: bit_push_bit_iff)
  2465   by (auto simp add: bit_push_bit_iff)
  2524 
  2466 
  2525 lemma and_nat_rec:
  2467 lemma and_nat_rec:
  2526   \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat
  2468   \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat
  2527   by (simp add: and_nat_def and_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2469   by (simp add: and_nat_def and_int.rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2528 
  2470 
  2529 lemma or_nat_rec:
  2471 lemma or_nat_rec:
  2530   \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat
  2472   \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat
  2531   by (simp add: or_nat_def or_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2473   by (simp add: or_nat_def or_int.rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2532 
  2474 
  2533 lemma xor_nat_rec:
  2475 lemma xor_nat_rec:
  2534   \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat
  2476   \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat
  2535   by (simp add: xor_nat_def xor_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2477   by (simp add: xor_nat_def xor_int.rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib)
  2536 
  2478 
  2537 lemma Suc_0_and_eq [simp]:
  2479 lemma Suc_0_and_eq [simp]:
  2538   \<open>Suc 0 AND n = n mod 2\<close>
  2480   \<open>Suc 0 AND n = n mod 2\<close>
  2539   using one_and_eq [of n] by simp
  2481   using one_and_eq [of n] by simp
  2540 
  2482 
  3047   \<open>- (NOT (numeral n)) = numeral (Num.inc n)\<close>
  2989   \<open>- (NOT (numeral n)) = numeral (Num.inc n)\<close>
  3048   by simp
  2990   by simp
  3049 
  2991 
  3050 lemma not_numeral_BitM_eq:
  2992 lemma not_numeral_BitM_eq:
  3051   \<open>NOT (numeral (Num.BitM n)) =  - numeral (num.Bit0 n)\<close>
  2993   \<open>NOT (numeral (Num.BitM n)) =  - numeral (num.Bit0 n)\<close>
  3052   by (simp add: inc_BitM_eq) 
  2994   by (simp add: inc_BitM_eq)
  3053 
  2995 
  3054 lemma not_numeral_Bit0_eq:
  2996 lemma not_numeral_Bit0_eq:
  3055   \<open>NOT (numeral (Num.Bit0 n)) =  - numeral (num.Bit1 n)\<close>
  2997   \<open>NOT (numeral (Num.Bit0 n)) =  - numeral (num.Bit1 n)\<close>
  3056   by simp
  2998   by simp
  3057 
  2999 
  3226   then have \<open>of_nat (take_bit m (numeral n)) = of_nat 0\<close>
  3168   then have \<open>of_nat (take_bit m (numeral n)) = of_nat 0\<close>
  3227     by simp
  3169     by simp
  3228   then show ?thesis
  3170   then show ?thesis
  3229     by (simp add: of_nat_take_bit)
  3171     by (simp add: of_nat_take_bit)
  3230 qed
  3172 qed
  3231     
  3173 
  3232 lemma take_bit_num_eq_Some_imp:
  3174 lemma take_bit_num_eq_Some_imp:
  3233   \<open>take_bit m (numeral n) = numeral q\<close> if \<open>take_bit_num m n = Some q\<close>
  3175   \<open>take_bit m (numeral n) = numeral q\<close> if \<open>take_bit_num m n = Some q\<close>
  3234 proof -
  3176 proof -
  3235   from that have \<open>take_bit m (numeral n :: nat) = numeral q\<close>
  3177   from that have \<open>take_bit m (numeral n :: nat) = numeral q\<close>
  3236     by (auto simp add: take_bit_num_def Num.numeral_num_of_nat_unfold split: if_splits)
  3178     by (auto simp add: take_bit_num_def Num.numeral_num_of_nat_unfold split: if_splits)
  3384 qed
  3326 qed
  3385 
  3327 
  3386 lemma take_bit_concat_bit_eq:
  3328 lemma take_bit_concat_bit_eq:
  3387   \<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close>
  3329   \<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close>
  3388   by (rule bit_eqI)
  3330   by (rule bit_eqI)
  3389     (auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def)  
  3331     (auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def)
  3390 
  3332 
  3391 lemma concat_bit_take_bit_eq:
  3333 lemma concat_bit_take_bit_eq:
  3392   \<open>concat_bit n (take_bit n b) = concat_bit n b\<close>
  3334   \<open>concat_bit n (take_bit n b) = concat_bit n b\<close>
  3393   by (simp add: concat_bit_def [abs_def])
  3335   by (simp add: concat_bit_def [abs_def])
  3394 
  3336 
  3826 
  3768 
  3827       \<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]}
  3769       \<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]}
  3828 
  3770 
  3829       \<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]}
  3771       \<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]}
  3830 
  3772 
  3831       \<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]}
  3773       \<^item> Set a single bit: @{thm set_bit_eq_or [where ?'a = int, no_vars]}
  3832 
  3774 
  3833       \<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]}
  3775       \<^item> Unset a single bit: @{thm unset_bit_eq_and_not [where ?'a = int, no_vars]}
  3834 
  3776 
  3835       \<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]}
  3777       \<^item> Flip a single bit: @{thm flip_bit_eq_xor [where ?'a = int, no_vars]}
  3836 
  3778 
  3837       \<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]}
  3779       \<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]}
  3838 
  3780 
  3839       \<^item> Bit concatenation: @{thm concat_bit_def [no_vars]}
  3781       \<^item> Bit concatenation: @{thm concat_bit_def [no_vars]}
  3840 
  3782 
  3841       \<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]}
  3783       \<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]}
  3842 \<close>
  3784 \<close>
       
  3785 
       
  3786 
       
  3787 subsection \<open>Lemma duplicates and other\<close>
       
  3788 
       
  3789 lemmas set_bit_def = set_bit_eq_or
       
  3790 
       
  3791 lemmas unset_bit_def = unset_bit_eq_and_not
       
  3792 
       
  3793 lemmas flip_bit_def = flip_bit_eq_xor
       
  3794 
       
  3795 lemmas and_int_rec = and_int.rec
       
  3796 
       
  3797 lemmas bit_and_int_iff = and_int.bit_iff
       
  3798 
       
  3799 lemmas or_int_rec = or_int.rec
       
  3800 
       
  3801 lemmas bit_or_int_iff = or_int.bit_iff
       
  3802 
       
  3803 lemmas xor_int_rec = xor_int.rec
       
  3804 
       
  3805 lemmas bit_xor_int_iff = xor_int.bit_iff
       
  3806 
       
  3807 lemma not_int_rec:
       
  3808   \<open>NOT k = of_bool (even k) + 2 * NOT (k div 2)\<close> for k :: int
       
  3809   by (fact not_rec)
       
  3810 
       
  3811 lemma even_not_iff_int:
       
  3812   \<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int
       
  3813   by (fact even_not_iff)
       
  3814 
       
  3815 lemma even_and_iff_int:
       
  3816   \<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int
       
  3817   by (fact even_and_iff)
  3843 
  3818 
  3844 no_notation
  3819 no_notation
  3845   not  (\<open>NOT\<close>)
  3820   not  (\<open>NOT\<close>)
  3846     and "and"  (infixr \<open>AND\<close> 64)
  3821     and "and"  (infixr \<open>AND\<close> 64)
  3847     and or  (infixr \<open>OR\<close>  59)
  3822     and or  (infixr \<open>OR\<close>  59)