23 and div_by_0 [simp]: "a div 0 = 0" |
23 and div_by_0 [simp]: "a div 0 = 0" |
24 and div_0 [simp]: "0 div a = 0" |
24 and div_0 [simp]: "0 div a = 0" |
25 and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b" |
25 and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b" |
26 and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b" |
26 and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b" |
27 begin |
27 begin |
|
28 |
|
29 subclass semiring_no_zero_divisors .. |
28 |
30 |
29 text {* @{const div} and @{const mod} *} |
31 text {* @{const div} and @{const mod} *} |
30 |
32 |
31 lemma mod_div_equality2: "b * (a div b) + a mod b = a" |
33 lemma mod_div_equality2: "b * (a div b) + a mod b = a" |
32 unfolding mult.commute [of b] |
34 unfolding mult.commute [of b] |
537 case True then show ?thesis by simp |
539 case True then show ?thesis by simp |
538 next |
540 next |
539 case False |
541 case False |
540 from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" . |
542 from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" . |
541 with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp |
543 with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp |
542 then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq) |
544 then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq del: mult_eq_0_iff) |
543 then have "1 div 2 = 0 \<or> 2 = 0" by (rule divisors_zero) |
545 then have "1 div 2 = 0 \<or> 2 = 0" by simp |
544 with False show ?thesis by auto |
546 with False show ?thesis by auto |
545 qed |
547 qed |
546 |
548 |
547 lemma not_mod_2_eq_0_eq_1 [simp]: |
549 lemma not_mod_2_eq_0_eq_1 [simp]: |
548 "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1" |
550 "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1" |
638 begin |
640 begin |
639 |
641 |
640 lemma diff_zero [simp]: |
642 lemma diff_zero [simp]: |
641 "a - 0 = a" |
643 "a - 0 = a" |
642 by (rule diff_invert_add1 [symmetric]) simp |
644 by (rule diff_invert_add1 [symmetric]) simp |
|
645 |
|
646 lemma dvd_times_left_cancel_iff [simp]: -- \<open>FIXME generalize\<close> |
|
647 assumes "c \<noteq> 0" |
|
648 shows "c * a dvd c * b \<longleftrightarrow> a dvd b" |
|
649 proof - |
|
650 have "(c * b) mod (c * a) = 0 \<longleftrightarrow> b mod a = 0" (is "?P \<longleftrightarrow> ?Q") |
|
651 using assms by (simp add: mod_mult_mult1) |
|
652 then show ?thesis by (simp add: mod_eq_0_iff_dvd) |
|
653 qed |
|
654 |
|
655 lemma dvd_times_right_cancel_iff [simp]: -- \<open>FIXME generalize\<close> |
|
656 assumes "c \<noteq> 0" |
|
657 shows "a * c dvd b * c \<longleftrightarrow> a dvd b" |
|
658 using assms dvd_times_left_cancel_iff [of c a b] by (simp add: ac_simps) |
643 |
659 |
644 subclass semiring_div_parity |
660 subclass semiring_div_parity |
645 proof |
661 proof |
646 fix a |
662 fix a |
647 show "a mod 2 = 0 \<or> a mod 2 = 1" |
663 show "a mod 2 = 0 \<or> a mod 2 = 1" |
797 then have "divmod m n = divmod_step n (divmod m (Num.Bit0 n))" |
813 then have "divmod m n = divmod_step n (divmod m (Num.Bit0 n))" |
798 by (simp add: divmod_def) |
814 by (simp add: divmod_def) |
799 with False show ?thesis by simp |
815 with False show ?thesis by simp |
800 qed |
816 qed |
801 |
817 |
802 lemma divmod_cancel [code]: |
818 lemma divmod_eq [simp]: |
|
819 "m < n \<Longrightarrow> divmod m n = (0, numeral m)" |
|
820 "n \<le> m \<Longrightarrow> divmod m n = divmod_step n (divmod m (Num.Bit0 n))" |
|
821 by (auto simp add: divmod_divmod_step [of m n]) |
|
822 |
|
823 lemma divmod_cancel [simp, code]: |
803 "divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))" (is ?P) |
824 "divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))" (is ?P) |
804 "divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))" (is ?Q) |
825 "divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))" (is ?Q) |
805 proof - |
826 proof - |
806 have *: "\<And>q. numeral (Num.Bit0 q) = 2 * numeral q" |
827 have *: "\<And>q. numeral (Num.Bit0 q) = 2 * numeral q" |
807 "\<And>q. numeral (Num.Bit1 q) = 2 * numeral q + 1" |
828 "\<And>q. numeral (Num.Bit1 q) = 2 * numeral q + 1" |
808 by (simp_all only: numeral_mult numeral.simps distrib) simp_all |
829 by (simp_all only: numeral_mult numeral.simps distrib) simp_all |
809 have "1 div 2 = 0" "1 mod 2 = 1" by (auto intro: div_less mod_less) |
830 have "1 div 2 = 0" "1 mod 2 = 1" by (auto intro: div_less mod_less) |
810 then show ?P and ?Q |
831 then show ?P and ?Q |
811 by (simp_all add: prod_eq_iff split_def * [of m] * [of n] mod_mult_mult1 |
832 by (simp_all add: prod_eq_iff split_def * [of m] * [of n] mod_mult_mult1 |
812 div_mult2_eq [of _ _ 2] mod_mult2_eq [of _ _ 2] add.commute del: numeral_times_numeral) |
833 div_mult2_eq [of _ _ 2] mod_mult2_eq [of _ _ 2] add.commute del: numeral_times_numeral) |
813 qed |
834 qed |
|
835 |
|
836 text {* Special case: divisibility *} |
|
837 |
|
838 definition divides_aux :: "'a \<times> 'a \<Rightarrow> bool" |
|
839 where |
|
840 "divides_aux qr \<longleftrightarrow> snd qr = 0" |
|
841 |
|
842 lemma divides_aux_eq [simp]: |
|
843 "divides_aux (q, r) \<longleftrightarrow> r = 0" |
|
844 by (simp add: divides_aux_def) |
|
845 |
|
846 lemma dvd_numeral_simp [simp]: |
|
847 "numeral m dvd numeral n \<longleftrightarrow> divides_aux (divmod n m)" |
|
848 by (simp add: divmod_def mod_eq_0_iff_dvd) |
814 |
849 |
815 end |
850 end |
816 |
851 |
817 hide_fact (open) diff_invert_add1 le_add_diff_inverse2 diff_zero |
852 hide_fact (open) diff_invert_add1 le_add_diff_inverse2 diff_zero |
818 -- {* restore simple accesses for more general variants of theorems *} |
853 -- {* restore simple accesses for more general variants of theorems *} |
2554 done |
2589 done |
2555 |
2590 |
2556 |
2591 |
2557 subsubsection {* The Divides Relation *} |
2592 subsubsection {* The Divides Relation *} |
2558 |
2593 |
2559 lemmas dvd_eq_mod_eq_0_numeral [simp] = |
2594 lemma dvd_eq_mod_eq_0_numeral: |
2560 dvd_eq_mod_eq_0 [of "numeral x" "numeral y"] for x y |
2595 "numeral x dvd (numeral y :: 'a) \<longleftrightarrow> numeral y mod numeral x = (0 :: 'a::semiring_div)" |
|
2596 by (fact dvd_eq_mod_eq_0) |
2561 |
2597 |
2562 |
2598 |
2563 subsubsection {* Further properties *} |
2599 subsubsection {* Further properties *} |
2564 |
2600 |
2565 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)" |
2601 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)" |