449 by (simp add: div_def divmod_def) |
449 by (simp add: div_def divmod_def) |
450 |
450 |
451 lemma zmod_zero [simp]: "(0::int) mod b = 0" |
451 lemma zmod_zero [simp]: "(0::int) mod b = 0" |
452 by (simp add: mod_def divmod_def) |
452 by (simp add: mod_def divmod_def) |
453 |
453 |
454 lemma zdiv_minus1: "(0::int) < b ==> -1 div b = -1" |
|
455 by (simp add: div_def divmod_def) |
|
456 |
|
457 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1" |
454 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1" |
458 by (simp add: mod_def divmod_def) |
455 by (simp add: mod_def divmod_def) |
459 |
456 |
460 text{*a positive, b positive *} |
457 text{*a positive, b positive *} |
461 |
458 |
727 lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)" |
724 lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)" |
728 apply (case_tac "c = 0", simp) |
725 apply (case_tac "c = 0", simp) |
729 apply (blast intro: divmod_rel_div_mod [THEN zmult1_lemma, THEN divmod_rel_mod]) |
726 apply (blast intro: divmod_rel_div_mod [THEN zmult1_lemma, THEN divmod_rel_mod]) |
730 done |
727 done |
731 |
728 |
732 lemma zmod_zmult1_eq': "(a*b) mod (c::int) = ((a mod c) * b) mod c" |
|
733 apply (rule trans) |
|
734 apply (rule_tac s = "b*a mod c" in trans) |
|
735 apply (rule_tac [2] zmod_zmult1_eq) |
|
736 apply (simp_all add: mult_commute) |
|
737 done |
|
738 |
|
739 lemma zmod_zmult_distrib: "(a*b) mod (c::int) = ((a mod c) * (b mod c)) mod c" |
|
740 apply (rule zmod_zmult1_eq' [THEN trans]) |
|
741 apply (rule zmod_zmult1_eq) |
|
742 done |
|
743 |
|
744 lemma zdiv_zmult_self1 [simp]: "b \<noteq> (0::int) ==> (a*b) div b = a" |
729 lemma zdiv_zmult_self1 [simp]: "b \<noteq> (0::int) ==> (a*b) div b = a" |
745 by (simp add: zdiv_zmult1_eq) |
730 by (simp add: zdiv_zmult1_eq) |
746 |
731 |
747 lemma zmod_zdiv_trivial: "(a mod b) div b = (0::int)" |
732 lemma zmod_zdiv_trivial: "(a mod b) div b = (0::int)" |
748 apply (case_tac "b = 0", simp) |
733 apply (case_tac "b = 0", simp) |
749 apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial) |
734 apply (auto simp add: linorder_neq_iff div_pos_pos_trivial div_neg_neg_trivial) |
750 done |
|
751 |
|
752 lemma zmod_zmod_trivial: "(a mod b) mod b = a mod (b::int)" |
|
753 apply (case_tac "b = 0", simp) |
|
754 apply (force simp add: linorder_neq_iff mod_pos_pos_trivial mod_neg_neg_trivial) |
|
755 done |
735 done |
756 |
736 |
757 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *} |
737 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *} |
758 |
738 |
759 lemma zadd1_lemma: |
739 lemma zadd1_lemma: |
764 (*NOT suitable for rewriting: the RHS has an instance of the LHS*) |
744 (*NOT suitable for rewriting: the RHS has an instance of the LHS*) |
765 lemma zdiv_zadd1_eq: |
745 lemma zdiv_zadd1_eq: |
766 "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)" |
746 "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)" |
767 apply (case_tac "c = 0", simp) |
747 apply (case_tac "c = 0", simp) |
768 apply (blast intro: zadd1_lemma [OF divmod_rel_div_mod divmod_rel_div_mod] divmod_rel_div) |
748 apply (blast intro: zadd1_lemma [OF divmod_rel_div_mod divmod_rel_div_mod] divmod_rel_div) |
769 done |
|
770 |
|
771 lemma zmod_zadd1_eq: "(a+b) mod (c::int) = (a mod c + b mod c) mod c" |
|
772 apply (case_tac "c = 0", simp) |
|
773 apply (blast intro: zadd1_lemma [OF divmod_rel_div_mod divmod_rel_div_mod] divmod_rel_mod) |
|
774 done |
749 done |
775 |
750 |
776 instance int :: ring_div |
751 instance int :: ring_div |
777 proof |
752 proof |
778 fix a b c :: int |
753 fix a b c :: int |
969 lemma split_pos_lemma: |
944 lemma split_pos_lemma: |
970 "0<k ==> |
945 "0<k ==> |
971 P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)" |
946 P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)" |
972 apply (rule iffI, clarify) |
947 apply (rule iffI, clarify) |
973 apply (erule_tac P="P ?x ?y" in rev_mp) |
948 apply (erule_tac P="P ?x ?y" in rev_mp) |
974 apply (subst zmod_zadd1_eq) |
949 apply (subst mod_add_eq) |
975 apply (subst zdiv_zadd1_eq) |
950 apply (subst zdiv_zadd1_eq) |
976 apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial) |
951 apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial) |
977 txt{*converse direction*} |
952 txt{*converse direction*} |
978 apply (drule_tac x = "n div k" in spec) |
953 apply (drule_tac x = "n div k" in spec) |
979 apply (drule_tac x = "n mod k" in spec, simp) |
954 apply (drule_tac x = "n mod k" in spec, simp) |
982 lemma split_neg_lemma: |
957 lemma split_neg_lemma: |
983 "k<0 ==> |
958 "k<0 ==> |
984 P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)" |
959 P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)" |
985 apply (rule iffI, clarify) |
960 apply (rule iffI, clarify) |
986 apply (erule_tac P="P ?x ?y" in rev_mp) |
961 apply (erule_tac P="P ?x ?y" in rev_mp) |
987 apply (subst zmod_zadd1_eq) |
962 apply (subst mod_add_eq) |
988 apply (subst zdiv_zadd1_eq) |
963 apply (subst zdiv_zadd1_eq) |
989 apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial) |
964 apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial) |
990 txt{*converse direction*} |
965 txt{*converse direction*} |
991 apply (drule_tac x = "n div k" in spec) |
966 apply (drule_tac x = "n div k" in spec) |
992 apply (drule_tac x = "n mod k" in spec, simp) |
967 apply (drule_tac x = "n mod k" in spec, simp) |
1055 apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))") |
1030 apply (subgoal_tac " (-1 - (2 * b)) = - (1 + (2 * b))") |
1056 apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric], |
1031 apply (simp only: zdiv_zminus_zminus diff_minus minus_add_distrib [symmetric], |
1057 simp) |
1032 simp) |
1058 done |
1033 done |
1059 |
1034 |
1060 (*Not clear why this must be proved separately; probably number_of causes |
|
1061 simplification problems*) |
|
1062 lemma not_0_le_lemma: "~ 0 \<le> x ==> x \<le> (0::int)" |
|
1063 by auto |
|
1064 |
|
1065 lemma zdiv_number_of_Bit0 [simp]: |
1035 lemma zdiv_number_of_Bit0 [simp]: |
1066 "number_of (Int.Bit0 v) div number_of (Int.Bit0 w) = |
1036 "number_of (Int.Bit0 v) div number_of (Int.Bit0 w) = |
1067 number_of v div (number_of w :: int)" |
1037 number_of v div (number_of w :: int)" |
1068 by (simp only: number_of_eq numeral_simps) simp |
1038 by (simp only: number_of_eq numeral_simps) simp |
1069 |
1039 |
1086 prefer 2 apply arith |
1056 prefer 2 apply arith |
1087 apply (subgoal_tac "2* (1 + b mod a) \<le> 2*a") |
1057 apply (subgoal_tac "2* (1 + b mod a) \<le> 2*a") |
1088 apply (rule_tac [2] mult_left_mono) |
1058 apply (rule_tac [2] mult_left_mono) |
1089 apply (auto simp add: add_commute [of 1] mult_commute add1_zle_eq |
1059 apply (auto simp add: add_commute [of 1] mult_commute add1_zle_eq |
1090 pos_mod_bound) |
1060 pos_mod_bound) |
1091 apply (subst zmod_zadd1_eq) |
1061 apply (subst mod_add_eq) |
1092 apply (simp add: zmod_zmult_zmult2 mod_pos_pos_trivial) |
1062 apply (simp add: zmod_zmult_zmult2 mod_pos_pos_trivial) |
1093 apply (rule mod_pos_pos_trivial) |
1063 apply (rule mod_pos_pos_trivial) |
1094 apply (auto simp add: mod_pos_pos_trivial ring_distribs) |
1064 apply (auto simp add: mod_pos_pos_trivial ring_distribs) |
1095 apply (subgoal_tac "0 \<le> b mod a", arith, simp) |
1065 apply (subgoal_tac "0 \<le> b mod a", arith, simp) |
1096 done |
1066 done |
1109 lemma zmod_number_of_Bit0 [simp]: |
1079 lemma zmod_number_of_Bit0 [simp]: |
1110 "number_of (Int.Bit0 v) mod number_of (Int.Bit0 w) = |
1080 "number_of (Int.Bit0 v) mod number_of (Int.Bit0 w) = |
1111 (2::int) * (number_of v mod number_of w)" |
1081 (2::int) * (number_of v mod number_of w)" |
1112 apply (simp only: number_of_eq numeral_simps) |
1082 apply (simp only: number_of_eq numeral_simps) |
1113 apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 |
1083 apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 |
1114 not_0_le_lemma neg_zmod_mult_2 add_ac) |
1084 neg_zmod_mult_2 add_ac) |
1115 done |
1085 done |
1116 |
1086 |
1117 lemma zmod_number_of_Bit1 [simp]: |
1087 lemma zmod_number_of_Bit1 [simp]: |
1118 "number_of (Int.Bit1 v) mod number_of (Int.Bit0 w) = |
1088 "number_of (Int.Bit1 v) mod number_of (Int.Bit0 w) = |
1119 (if (0::int) \<le> number_of w |
1089 (if (0::int) \<le> number_of w |
1120 then 2 * (number_of v mod number_of w) + 1 |
1090 then 2 * (number_of v mod number_of w) + 1 |
1121 else 2 * ((number_of v + (1::int)) mod number_of w) - 1)" |
1091 else 2 * ((number_of v + (1::int)) mod number_of w) - 1)" |
1122 apply (simp only: number_of_eq numeral_simps) |
1092 apply (simp only: number_of_eq numeral_simps) |
1123 apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 |
1093 apply (simp add: zmod_zmult_zmult1 pos_zmod_mult_2 |
1124 not_0_le_lemma neg_zmod_mult_2 add_ac) |
1094 neg_zmod_mult_2 add_ac) |
1125 done |
1095 done |
1126 |
1096 |
1127 |
1097 |
1128 subsection{*Quotients of Signs*} |
1098 subsection{*Quotients of Signs*} |
1129 |
1099 |
1130 lemma div_neg_pos_less0: "[| a < (0::int); 0 < b |] ==> a div b < 0" |
1100 lemma div_neg_pos_less0: "[| a < (0::int); 0 < b |] ==> a div b < 0" |
1131 apply (subgoal_tac "a div b \<le> -1", force) |
1101 apply (subgoal_tac "a div b \<le> -1", force) |
1132 apply (rule order_trans) |
1102 apply (rule order_trans) |
1133 apply (rule_tac a' = "-1" in zdiv_mono1) |
1103 apply (rule_tac a' = "-1" in zdiv_mono1) |
1134 apply (auto simp add: zdiv_minus1) |
1104 apply (auto simp add: div_eq_minus1) |
1135 done |
1105 done |
1136 |
1106 |
1137 lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0" |
1107 lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0" |
1138 by (drule zdiv_mono1_neg, auto) |
1108 by (drule zdiv_mono1_neg, auto) |
1139 |
1109 |
1377 |
1347 |
1378 lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m" |
1348 lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m" |
1379 apply (induct "y", auto) |
1349 apply (induct "y", auto) |
1380 apply (rule zmod_zmult1_eq [THEN trans]) |
1350 apply (rule zmod_zmult1_eq [THEN trans]) |
1381 apply (simp (no_asm_simp)) |
1351 apply (simp (no_asm_simp)) |
1382 apply (rule zmod_zmult_distrib [symmetric]) |
1352 apply (rule mod_mult_eq [symmetric]) |
1383 done |
1353 done |
1384 |
1354 |
1385 lemma zdiv_int: "int (a div b) = (int a) div (int b)" |
1355 lemma zdiv_int: "int (a div b) = (int a) div (int b)" |
1386 apply (subst split_div, auto) |
1356 apply (subst split_div, auto) |
1387 apply (subst split_zdiv, auto) |
1357 apply (subst split_zdiv, auto) |
1418 |
1388 |
1419 lemmas zmod_simps = |
1389 lemmas zmod_simps = |
1420 IntDiv.zmod_zadd_left_eq [symmetric] |
1390 IntDiv.zmod_zadd_left_eq [symmetric] |
1421 IntDiv.zmod_zadd_right_eq [symmetric] |
1391 IntDiv.zmod_zadd_right_eq [symmetric] |
1422 IntDiv.zmod_zmult1_eq [symmetric] |
1392 IntDiv.zmod_zmult1_eq [symmetric] |
1423 IntDiv.zmod_zmult1_eq' [symmetric] |
1393 mod_mult_left_eq [symmetric] |
1424 IntDiv.zpower_zmod |
1394 IntDiv.zpower_zmod |
1425 zminus_zmod zdiff_zmod_left zdiff_zmod_right |
1395 zminus_zmod zdiff_zmod_left zdiff_zmod_right |
1426 |
1396 |
1427 text {* Distributive laws for function @{text nat}. *} |
1397 text {* Distributive laws for function @{text nat}. *} |
1428 |
1398 |