equal
deleted
inserted
replaced
6 |
6 |
7 header{* The Division Operators div and mod *} |
7 header{* The Division Operators div and mod *} |
8 |
8 |
9 theory IntDiv |
9 theory IntDiv |
10 imports Int Divides FunDef |
10 imports Int Divides FunDef |
11 uses |
|
12 "~~/src/Provers/Arith/cancel_numeral_factor.ML" |
|
13 "~~/src/Provers/Arith/extract_common_term.ML" |
|
14 ("Tools/int_factor_simprocs.ML") |
|
15 begin |
11 begin |
16 |
12 |
17 definition divmod_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where |
13 definition divmod_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where |
18 --{*definition of quotient and remainder*} |
14 --{*definition of quotient and remainder*} |
19 [code]: "divmod_rel a b = (\<lambda>(q, r). a = b * q + r \<and> |
15 [code]: "divmod_rel a b = (\<lambda>(q, r). a = b * q + r \<and> |
722 with `a \<noteq> 0` |
718 with `a \<noteq> 0` |
723 have "\<And>q r. divmod_rel b c (q, r) \<Longrightarrow> divmod_rel (a * b) (a * c) (q, a * r)" |
719 have "\<And>q r. divmod_rel b c (q, r) \<Longrightarrow> divmod_rel (a * b) (a * c) (q, a * r)" |
724 apply (auto simp add: divmod_rel_def) |
720 apply (auto simp add: divmod_rel_def) |
725 apply (auto simp add: algebra_simps) |
721 apply (auto simp add: algebra_simps) |
726 apply (auto simp add: zero_less_mult_iff zero_le_mult_iff mult_le_0_iff) |
722 apply (auto simp add: zero_less_mult_iff zero_le_mult_iff mult_le_0_iff) |
727 apply (simp_all add: mult_less_cancel_left_disj mult_commute [of _ a]) |
|
728 done |
723 done |
729 moreover with `c \<noteq> 0` divmod_rel_div_mod have "divmod_rel b c (b div c, b mod c)" by auto |
724 moreover with `c \<noteq> 0` divmod_rel_div_mod have "divmod_rel b c (b div c, b mod c)" by auto |
730 ultimately have "divmod_rel (a * b) (a * c) (b div c, a * (b mod c))" . |
725 ultimately have "divmod_rel (a * b) (a * c) (b div c, a * (b mod c))" . |
731 moreover from `a \<noteq> 0` `c \<noteq> 0` have "a * c \<noteq> 0" by simp |
726 moreover from `a \<noteq> 0` `c \<noteq> 0` have "a * c \<noteq> 0" by simp |
732 ultimately show ?thesis by (rule divmod_rel_div) |
727 ultimately show ?thesis by (rule divmod_rel_div) |
1076 apply (auto elim!: dvdE) |
1071 apply (auto elim!: dvdE) |
1077 apply (subgoal_tac "0 < n") |
1072 apply (subgoal_tac "0 < n") |
1078 prefer 2 |
1073 prefer 2 |
1079 apply (blast intro: order_less_trans) |
1074 apply (blast intro: order_less_trans) |
1080 apply (simp add: zero_less_mult_iff) |
1075 apply (simp add: zero_less_mult_iff) |
1081 apply (subgoal_tac "n * k < n * 1") |
|
1082 apply (drule mult_less_cancel_left [THEN iffD1], auto) |
|
1083 done |
1076 done |
1084 |
1077 |
1085 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)" |
1078 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)" |
1086 using zmod_zdiv_equality[where a="m" and b="n"] |
1079 using zmod_zdiv_equality[where a="m" and b="n"] |
1087 by (simp add: algebra_simps) |
1080 by (simp add: algebra_simps) |
1332 hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp |
1325 hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp |
1333 thus ?lhs by simp |
1326 thus ?lhs by simp |
1334 qed |
1327 qed |
1335 |
1328 |
1336 |
1329 |
1337 subsection {* Simproc setup *} |
|
1338 |
|
1339 use "Tools/int_factor_simprocs.ML" |
|
1340 |
|
1341 |
|
1342 subsection {* Code generation *} |
1330 subsection {* Code generation *} |
1343 |
1331 |
1344 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where |
1332 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where |
1345 "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)" |
1333 "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)" |
1346 |
1334 |