973 by (simp add: bit_iff_odd even_mask_div_iff not_le) |
973 by (simp add: bit_iff_odd even_mask_div_iff not_le) |
974 |
974 |
975 lemma bit_Numeral1_iff [simp]: |
975 lemma bit_Numeral1_iff [simp]: |
976 \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close> |
976 \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close> |
977 by (simp add: bit_rec) |
977 by (simp add: bit_rec) |
|
978 |
|
979 lemma exp_add_not_zero_imp: |
|
980 \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> if \<open>2 ^ (m + n) \<noteq> 0\<close> |
|
981 proof - |
|
982 have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close> |
|
983 proof (rule notI) |
|
984 assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close> |
|
985 then have \<open>2 ^ (m + n) = 0\<close> |
|
986 by (rule disjE) (simp_all add: power_add) |
|
987 with that show False .. |
|
988 qed |
|
989 then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> |
|
990 by simp_all |
|
991 qed |
978 |
992 |
979 end |
993 end |
980 |
994 |
981 lemma nat_bit_induct [case_names zero even odd]: |
995 lemma nat_bit_induct [case_names zero even odd]: |
982 "P n" if zero: "P 0" |
996 "P n" if zero: "P 0" |
1108 by (simp add: algebra_simps) |
1122 by (simp add: algebra_simps) |
1109 finally show ?case |
1123 finally show ?case |
1110 using odd by simp |
1124 using odd by simp |
1111 qed |
1125 qed |
1112 qed |
1126 qed |
|
1127 |
|
1128 context semiring_bits |
|
1129 begin |
|
1130 |
|
1131 lemma even_of_nat_iff: |
|
1132 \<open>even (of_nat n) \<longleftrightarrow> even n\<close> |
|
1133 by (induction n rule: nat_bit_induct) simp_all |
|
1134 |
|
1135 lemma bit_of_nat_iff: |
|
1136 \<open>bit (of_nat m) n \<longleftrightarrow> (2::'a) ^ n \<noteq> 0 \<and> bit m n\<close> |
|
1137 proof (cases \<open>(2::'a) ^ n = 0\<close>) |
|
1138 case True |
|
1139 then show ?thesis |
|
1140 by (simp add: exp_eq_0_imp_not_bit) |
|
1141 next |
|
1142 case False |
|
1143 then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
|
1144 proof (induction m arbitrary: n rule: nat_bit_induct) |
|
1145 case zero |
|
1146 then show ?case |
|
1147 by simp |
|
1148 next |
|
1149 case (even m) |
|
1150 then show ?case |
|
1151 by (cases n) |
|
1152 (auto simp add: bit_double_iff Parity.bit_double_iff dest: mult_not_zero) |
|
1153 next |
|
1154 case (odd m) |
|
1155 then show ?case |
|
1156 by (cases n) |
|
1157 (auto simp add: bit_double_iff even_bit_succ_iff Parity.bit_Suc dest: mult_not_zero) |
|
1158 qed |
|
1159 with False show ?thesis |
|
1160 by simp |
|
1161 qed |
|
1162 |
|
1163 end |
1113 |
1164 |
1114 instantiation int :: semiring_bits |
1165 instantiation int :: semiring_bits |
1115 begin |
1166 begin |
1116 |
1167 |
1117 definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
1168 definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
1451 instance |
1502 instance |
1452 by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def) |
1503 by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def) |
1453 |
1504 |
1454 end |
1505 end |
1455 |
1506 |
|
1507 context semiring_bit_shifts |
|
1508 begin |
|
1509 |
|
1510 lemma push_bit_of_nat: |
|
1511 \<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close> |
|
1512 by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
1513 |
|
1514 lemma of_nat_push_bit: |
|
1515 \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close> |
|
1516 by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
1517 |
|
1518 lemma take_bit_of_nat: |
|
1519 \<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close> |
|
1520 by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_nat_iff) |
|
1521 |
|
1522 lemma of_nat_take_bit: |
|
1523 \<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close> |
|
1524 by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_nat_iff) |
|
1525 |
|
1526 end |
|
1527 |
1456 instantiation int :: semiring_bit_shifts |
1528 instantiation int :: semiring_bit_shifts |
1457 begin |
1529 begin |
1458 |
1530 |
1459 definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
1531 definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
1460 where \<open>push_bit_int n k = k * 2 ^ n\<close> |
1532 where \<open>push_bit_int n k = k * 2 ^ n\<close> |
1496 |
1568 |
1497 lemma push_bit_eq_0_iff [simp]: |
1569 lemma push_bit_eq_0_iff [simp]: |
1498 "push_bit n a = 0 \<longleftrightarrow> a = 0" |
1570 "push_bit n a = 0 \<longleftrightarrow> a = 0" |
1499 by (simp add: push_bit_eq_mult) |
1571 by (simp add: push_bit_eq_mult) |
1500 |
1572 |
1501 lemma push_bit_of_nat: |
|
1502 "push_bit n (of_nat m) = of_nat (push_bit n m)" |
|
1503 by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult) |
|
1504 |
|
1505 lemma take_bit_add: |
1573 lemma take_bit_add: |
1506 "take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)" |
1574 "take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)" |
1507 by (simp add: take_bit_eq_mod mod_simps) |
1575 by (simp add: take_bit_eq_mod mod_simps) |
1508 |
1576 |
1509 lemma take_bit_of_1_eq_0_iff [simp]: |
1577 lemma take_bit_of_1_eq_0_iff [simp]: |
1531 by (simp add: take_bit_rec numeral_Bit0_div_2) |
1599 by (simp add: take_bit_rec numeral_Bit0_div_2) |
1532 |
1600 |
1533 lemma take_bit_numeral_bit1 [simp]: |
1601 lemma take_bit_numeral_bit1 [simp]: |
1534 \<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close> |
1602 \<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close> |
1535 by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd) |
1603 by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd) |
1536 |
|
1537 lemma take_bit_of_nat: |
|
1538 "take_bit n (of_nat m) = of_nat (take_bit n m)" |
|
1539 by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"]) |
|
1540 |
1604 |
1541 lemma drop_bit_Suc_bit0 [simp]: |
1605 lemma drop_bit_Suc_bit0 [simp]: |
1542 \<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close> |
1606 \<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close> |
1543 by (simp add: drop_bit_Suc numeral_Bit0_div_2) |
1607 by (simp add: drop_bit_Suc numeral_Bit0_div_2) |
1544 |
1608 |
1567 by (simp add: of_nat_div) |
1631 by (simp add: of_nat_div) |
1568 finally show ?thesis |
1632 finally show ?thesis |
1569 by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd) |
1633 by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd) |
1570 qed |
1634 qed |
1571 |
1635 |
1572 lemma of_nat_push_bit: |
|
1573 \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close> |
|
1574 by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
1575 |
|
1576 lemma of_nat_drop_bit: |
1636 lemma of_nat_drop_bit: |
1577 \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close> |
1637 \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close> |
1578 by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div) |
1638 by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div) |
1579 |
1639 |
1580 lemma of_nat_take_bit: |
|
1581 \<open>of_nat (take_bit m n) = take_bit m (of_nat n)\<close> |
|
1582 by (simp add: take_bit_eq_mod semiring_bit_shifts_class.take_bit_eq_mod of_nat_mod) |
|
1583 |
|
1584 lemma bit_push_bit_iff_of_nat_iff: |
1640 lemma bit_push_bit_iff_of_nat_iff: |
1585 \<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close> |
1641 \<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close> |
1586 by (auto simp add: bit_push_bit_iff) |
1642 by (auto simp add: bit_push_bit_iff) |
1587 |
1643 |
1588 end |
1644 end |
1589 |
1645 |
1590 instance nat :: unique_euclidean_semiring_with_bit_shifts .. |
1646 instance nat :: unique_euclidean_semiring_with_bit_shifts .. |
1591 |
1647 |
1592 instance int :: unique_euclidean_semiring_with_bit_shifts .. |
1648 instance int :: unique_euclidean_semiring_with_bit_shifts .. |
1593 |
1649 |
1594 lemma bit_nat_iff [simp]: |
1650 lemma bit_nat_iff: |
1595 \<open>bit (nat k) n \<longleftrightarrow> k > 0 \<and> bit k n\<close> |
1651 \<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close> |
1596 proof (cases \<open>k > 0\<close>) |
1652 proof (cases \<open>k \<ge> 0\<close>) |
1597 case True |
1653 case True |
1598 moreover define m where \<open>m = nat k\<close> |
1654 moreover define m where \<open>m = nat k\<close> |
1599 ultimately have \<open>k = int m\<close> |
1655 ultimately have \<open>k = int m\<close> |
1600 by simp |
1656 by simp |
1601 then show ?thesis |
1657 then show ?thesis |
1603 next |
1659 next |
1604 case False |
1660 case False |
1605 then show ?thesis |
1661 then show ?thesis |
1606 by simp |
1662 by simp |
1607 qed |
1663 qed |
|
1664 |
|
1665 lemma push_bit_nat_eq: |
|
1666 \<open>push_bit n (nat k) = nat (push_bit n k)\<close> |
|
1667 by (cases \<open>k \<ge> 0\<close>) (simp_all add: push_bit_eq_mult nat_mult_distrib not_le mult_nonneg_nonpos2) |
|
1668 |
|
1669 lemma drop_bit_nat_eq: |
|
1670 \<open>drop_bit n (nat k) = nat (drop_bit n k)\<close> |
|
1671 apply (cases \<open>k \<ge> 0\<close>) |
|
1672 apply (simp_all add: drop_bit_eq_div nat_div_distrib nat_power_eq not_le) |
|
1673 apply (simp add: divide_int_def) |
|
1674 done |
|
1675 |
|
1676 lemma take_bit_nat_eq: |
|
1677 \<open>take_bit n (nat k) = nat (take_bit n k)\<close> if \<open>k \<ge> 0\<close> |
|
1678 using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
1608 |
1679 |
1609 lemma nat_take_bit_eq: |
1680 lemma nat_take_bit_eq: |
1610 \<open>nat (take_bit n k) = take_bit n (nat k)\<close> |
1681 \<open>nat (take_bit n k) = take_bit n (nat k)\<close> |
1611 if \<open>k \<ge> 0\<close> |
1682 if \<open>k \<ge> 0\<close> |
1612 using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
1683 using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |