
paulson@3366: (*  Title:      HOL/Divides.thy
paulson@3366:     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6865:     Copyright   1999  University of Cambridge
huffman@18154: *)
paulson@3366: 
wenzelm@60758: section \<open>The division operators div and mod\<close>
paulson@3366: 
nipkow@15131: theory Divides
haftmann@58778: imports Parity
nipkow@15131: begin
paulson@3366: 
wenzelm@60758: subsection \<open>Abstract division in commutative semirings.\<close>
haftmann@25942: 
haftmann@60352: class div = dvd + divide +
haftmann@60352:   fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "mod" 70)
haftmann@25942: 
haftmann@59833: class semiring_div = semidom + div +
haftmann@25942:   assumes mod_div_equality: "a div b * b + a mod b = a"
haftmann@27651:     and div_by_0 [simp]: "a div 0 = 0"
haftmann@27651:     and div_0 [simp]: "0 div a = 0"
haftmann@27651:     and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@30930:     and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b"
haftmann@25942: begin
haftmann@25942: 
haftmann@60517: subclass algebraic_semidom
haftmann@60353: proof
haftmann@60353:   fix b a
haftmann@60353:   assume "b \<noteq> 0"
haftmann@60353:   then show "a * b div b = a"
haftmann@60353:     using div_mult_self1 [of b 0 a] by (simp add: ac_simps)
haftmann@60353: qed simp
haftmann@58953: 
haftmann@60867: lemma div_by_1:
haftmann@60867:   "a div 1 = a"
haftmann@60867:   by (fact divide_1)
haftmann@60867: 
haftmann@60867: lemma div_mult_self1_is_id:
haftmann@60867:   "b \<noteq> 0 \<Longrightarrow> b * a div b = a"
haftmann@60867:   by (fact nonzero_mult_divide_cancel_left)
haftmann@60867: 
haftmann@60867: lemma div_mult_self2_is_id:
haftmann@60867:   "b \<noteq> 0 \<Longrightarrow> a * b div b = a"
haftmann@60867:   by (fact nonzero_mult_divide_cancel_right)
haftmann@59009: 
wenzelm@60758: text \<open>@{const divide} and @{const mod}\<close>
haftmann@26100: 
haftmann@26062: lemma mod_div_equality2: "b * (a div b) + a mod b = a"
haftmann@57512:   unfolding mult.commute [of b]
haftmann@26062:   by (rule mod_div_equality)
haftmann@26062: 
huffman@29403: lemma mod_div_equality': "a mod b + a div b * b = a"
huffman@29403:   using mod_div_equality [of a b]
haftmann@57514:   by (simp only: ac_simps)
huffman@29403: 
haftmann@26062: lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"
haftmann@30934:   by (simp add: mod_div_equality)
haftmann@26062: 
haftmann@26062: lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"
haftmann@30934:   by (simp add: mod_div_equality2)
haftmann@26062: 
haftmann@27651: lemma mod_by_0 [simp]: "a mod 0 = a"
haftmann@30934:   using mod_div_equality [of a zero] by simp
haftmann@27651: 
haftmann@27651: lemma mod_0 [simp]: "0 mod a = 0"
haftmann@30934:   using mod_div_equality [of zero a] div_0 by simp
haftmann@27651: 
haftmann@27651: lemma div_mult_self2 [simp]:
haftmann@27651:   assumes "b \<noteq> 0"
haftmann@27651:   shows "(a + b * c) div b = c + a div b"
haftmann@57512:   using assms div_mult_self1 [of b a c] by (simp add: mult.commute)
haftmann@26100: 
haftmann@54221: lemma div_mult_self3 [simp]:
haftmann@54221:   assumes "b \<noteq> 0"
haftmann@54221:   shows "(c * b + a) div b = c + a div b"
haftmann@54221:   using assms by (simp add: add.commute)
haftmann@54221: 
haftmann@54221: lemma div_mult_self4 [simp]:
haftmann@54221:   assumes "b \<noteq> 0"
haftmann@54221:   shows "(b * c + a) div b = c + a div b"
haftmann@54221:   using assms by (simp add: add.commute)
haftmann@54221: 
haftmann@27651: lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
haftmann@27651: proof (cases "b = 0")
haftmann@27651:   case True then show ?thesis by simp
haftmann@27651: next
haftmann@27651:   case False
haftmann@27651:   have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
haftmann@27651:     by (simp add: mod_div_equality)
haftmann@27651:   also from False div_mult_self1 [of b a c] have
haftmann@27651:     "\<dots> = (c + a div b) * b + (a + c * b) mod b"
nipkow@29667:       by (simp add: algebra_simps)
haftmann@27651:   finally have "a = a div b * b + (a + c * b) mod b"
haftmann@57512:     by (simp add: add.commute [of a] add.assoc distrib_right)
haftmann@27651:   then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
haftmann@27651:     by (simp add: mod_div_equality)
haftmann@27651:   then show ?thesis by simp
haftmann@27651: qed
haftmann@27651: 
lp15@60562: lemma mod_mult_self2 [simp]:
haftmann@54221:   "(a + b * c) mod b = a mod b"
haftmann@57512:   by (simp add: mult.commute [of b])
haftmann@27651: 
haftmann@54221: lemma mod_mult_self3 [simp]:
haftmann@54221:   "(c * b + a) mod b = a mod b"
haftmann@54221:   by (simp add: add.commute)
haftmann@54221: 
haftmann@54221: lemma mod_mult_self4 [simp]:
haftmann@54221:   "(b * c + a) mod b = a mod b"
haftmann@54221:   by (simp add: add.commute)
haftmann@54221: 
haftmann@60867: lemma mod_mult_self1_is_0 [simp]:
haftmann@60867:   "b * a mod b = 0"
haftmann@27651:   using mod_mult_self2 [of 0 b a] by simp
haftmann@27651: 
haftmann@60867: lemma mod_mult_self2_is_0 [simp]:
haftmann@60867:   "a * b mod b = 0"
haftmann@27651:   using mod_mult_self1 [of 0 a b] by simp
haftmann@26062: 
haftmann@60867: lemma mod_by_1 [simp]:
haftmann@60867:   "a mod 1 = 0"
haftmann@27651: proof -
haftmann@27651:   from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp
haftmann@27651:   then have "a + a mod 1 = a + 0" by simp
haftmann@27651:   then show ?thesis by (rule add_left_imp_eq)
haftmann@27651: qed
haftmann@27651: 
haftmann@60867: lemma mod_self [simp]:
haftmann@60867:   "a mod a = 0"
haftmann@27651:   using mod_mult_self2_is_0 [of 1] by simp
haftmann@27651: 
haftmann@27676: lemma div_add_self1 [simp]:
haftmann@27651:   assumes "b \<noteq> 0"
haftmann@27651:   shows "(b + a) div b = a div b + 1"
haftmann@57512:   using assms div_mult_self1 [of b a 1] by (simp add: add.commute)
haftmann@26062: 
haftmann@27676: lemma div_add_self2 [simp]:
haftmann@27651:   assumes "b \<noteq> 0"
haftmann@27651:   shows "(a + b) div b = a div b + 1"
haftmann@57512:   using assms div_add_self1 [of b a] by (simp add: add.commute)
haftmann@27651: 
haftmann@27676: lemma mod_add_self1 [simp]:
haftmann@27651:   "(b + a) mod b = a mod b"
haftmann@57512:   using mod_mult_self1 [of a 1 b] by (simp add: add.commute)
haftmann@27651: 
haftmann@27676: lemma mod_add_self2 [simp]:
haftmann@27651:   "(a + b) mod b = a mod b"
haftmann@27651:   using mod_mult_self1 [of a 1 b] by simp
haftmann@27651: 
haftmann@27651: lemma mod_div_decomp:
haftmann@27651:   fixes a b
haftmann@27651:   obtains q r where "q = a div b" and "r = a mod b"
haftmann@27651:     and "a = q * b + r"
haftmann@27651: proof -
haftmann@27651:   from mod_div_equality have "a = a div b * b + a mod b" by simp
haftmann@27651:   moreover have "a div b = a div b" ..
haftmann@27651:   moreover have "a mod b = a mod b" ..
haftmann@27651:   note that ultimately show thesis by blast
haftmann@27651: qed
haftmann@27651: 
haftmann@58834: lemma dvd_imp_mod_0 [simp]:
haftmann@58834:   assumes "a dvd b"
haftmann@58834:   shows "b mod a = 0"
haftmann@58834: proof -
haftmann@58834:   from assms obtain c where "b = a * c" ..
haftmann@58834:   then have "b mod a = a * c mod a" by simp
haftmann@58834:   then show "b mod a = 0" by simp
haftmann@58834: qed
haftmann@58911: 
haftmann@58911: lemma mod_eq_0_iff_dvd:
haftmann@58911:   "a mod b = 0 \<longleftrightarrow> b dvd a"
haftmann@58911: proof
haftmann@58911:   assume "b dvd a"
haftmann@58911:   then show "a mod b = 0" by simp
haftmann@58911: next
haftmann@58911:   assume "a mod b = 0"
haftmann@58911:   with mod_div_equality [of a b] have "a div b * b = a" by simp
haftmann@58911:   then have "a = b * (a div b)" by (simp add: ac_simps)
haftmann@58911:   then show "b dvd a" ..
haftmann@58911: qed
haftmann@58911: 
haftmann@60867: lemma dvd_eq_mod_eq_0 [nitpick_unfold, code]:
haftmann@58834:   "a dvd b \<longleftrightarrow> b mod a = 0"
haftmann@58911:   by (simp add: mod_eq_0_iff_dvd)
haftmann@58911: 
haftmann@58911: lemma mod_div_trivial [simp]:
haftmann@58911:   "a mod b div b = 0"
huffman@29403: proof (cases "b = 0")
huffman@29403:   assume "b = 0"
huffman@29403:   thus ?thesis by simp
huffman@29403: next
huffman@29403:   assume "b \<noteq> 0"
huffman@29403:   hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
huffman@29403:     by (rule div_mult_self1 [symmetric])
huffman@29403:   also have "\<dots> = a div b"
huffman@29403:     by (simp only: mod_div_equality')
huffman@29403:   also have "\<dots> = a div b + 0"
huffman@29403:     by simp
huffman@29403:   finally show ?thesis
huffman@29403:     by (rule add_left_imp_eq)
huffman@29403: qed
huffman@29403: 
haftmann@58911: lemma mod_mod_trivial [simp]:
haftmann@58911:   "a mod b mod b = a mod b"
huffman@29403: proof -
huffman@29403:   have "a mod b mod b = (a mod b + a div b * b) mod b"
huffman@29403:     by (simp only: mod_mult_self1)
huffman@29403:   also have "\<dots> = a mod b"
huffman@29403:     by (simp only: mod_div_equality')
huffman@29403:   finally show ?thesis .
huffman@29403: qed
huffman@29403: 
haftmann@58834: lemma dvd_mod_imp_dvd:
haftmann@58834:   assumes "k dvd m mod n" and "k dvd n"
haftmann@58834:   shows "k dvd m"
haftmann@58834: proof -
haftmann@58834:   from assms have "k dvd (m div n) * n + m mod n"
haftmann@58834:     by (simp only: dvd_add dvd_mult)
haftmann@58834:   then show ?thesis by (simp add: mod_div_equality)
haftmann@58834: qed
huffman@30078: 
wenzelm@60758: text \<open>Addition respects modular equivalence.\<close>
huffman@29403: 
wenzelm@61799: lemma mod_add_left_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a + b) mod c = (a mod c + b) mod c"
huffman@29403: proof -
huffman@29403:   have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
huffman@29403:     by (simp only: mod_div_equality)
huffman@29403:   also have "\<dots> = (a mod c + b + a div c * c) mod c"
haftmann@57514:     by (simp only: ac_simps)
huffman@29403:   also have "\<dots> = (a mod c + b) mod c"
huffman@29403:     by (rule mod_mult_self1)
huffman@29403:   finally show ?thesis .
huffman@29403: qed
huffman@29403: 
wenzelm@61799: lemma mod_add_right_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a + b) mod c = (a + b mod c) mod c"
huffman@29403: proof -
huffman@29403:   have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
huffman@29403:     by (simp only: mod_div_equality)
huffman@29403:   also have "\<dots> = (a + b mod c + b div c * c) mod c"
haftmann@57514:     by (simp only: ac_simps)
huffman@29403:   also have "\<dots> = (a + b mod c) mod c"
huffman@29403:     by (rule mod_mult_self1)
huffman@29403:   finally show ?thesis .
huffman@29403: qed
huffman@29403: 
wenzelm@61799: lemma mod_add_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a + b) mod c = (a mod c + b mod c) mod c"
huffman@29403: by (rule trans [OF mod_add_left_eq mod_add_right_eq])
huffman@29403: 
huffman@29403: lemma mod_add_cong:
huffman@29403:   assumes "a mod c = a' mod c"
huffman@29403:   assumes "b mod c = b' mod c"
huffman@29403:   shows "(a + b) mod c = (a' + b') mod c"
huffman@29403: proof -
huffman@29403:   have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
huffman@29403:     unfolding assms ..
huffman@29403:   thus ?thesis
huffman@29403:     by (simp only: mod_add_eq [symmetric])
huffman@29403: qed
huffman@29403: 
wenzelm@60758: text \<open>Multiplication respects modular equivalence.\<close>
huffman@29403: 
wenzelm@61799: lemma mod_mult_left_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a * b) mod c = ((a mod c) * b) mod c"
huffman@29403: proof -
huffman@29403:   have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
huffman@29403:     by (simp only: mod_div_equality)
huffman@29403:   also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
nipkow@29667:     by (simp only: algebra_simps)
huffman@29403:   also have "\<dots> = (a mod c * b) mod c"
huffman@29403:     by (rule mod_mult_self1)
huffman@29403:   finally show ?thesis .
huffman@29403: qed
huffman@29403: 
wenzelm@61799: lemma mod_mult_right_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a * b) mod c = (a * (b mod c)) mod c"
huffman@29403: proof -
huffman@29403:   have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
huffman@29403:     by (simp only: mod_div_equality)
huffman@29403:   also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
nipkow@29667:     by (simp only: algebra_simps)
huffman@29403:   also have "\<dots> = (a * (b mod c)) mod c"
huffman@29403:     by (rule mod_mult_self1)
huffman@29403:   finally show ?thesis .
huffman@29403: qed
huffman@29403: 
wenzelm@61799: lemma mod_mult_eq: \<comment> \<open>FIXME reorient\<close>
haftmann@60867:   "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
huffman@29403: by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
huffman@29403: 
huffman@29403: lemma mod_mult_cong:
huffman@29403:   assumes "a mod c = a' mod c"
huffman@29403:   assumes "b mod c = b' mod c"
huffman@29403:   shows "(a * b) mod c = (a' * b') mod c"
huffman@29403: proof -
huffman@29403:   have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
huffman@29403:     unfolding assms ..
huffman@29403:   thus ?thesis
huffman@29403:     by (simp only: mod_mult_eq [symmetric])
huffman@29403: qed
huffman@29403: 
wenzelm@60758: text \<open>Exponentiation respects modular equivalence.\<close>
huffman@47164: 
haftmann@60867: lemma power_mod: "(a mod b) ^ n mod b = a ^ n mod b"
huffman@47164: apply (induct n, simp_all)
huffman@47164: apply (rule mod_mult_right_eq [THEN trans])
huffman@47164: apply (simp (no_asm_simp))
huffman@47164: apply (rule mod_mult_eq [symmetric])
huffman@47164: done
huffman@47164: 
huffman@29404: lemma mod_mod_cancel:
huffman@29404:   assumes "c dvd b"
huffman@29404:   shows "a mod b mod c = a mod c"
huffman@29404: proof -
wenzelm@60758:   from \<open>c dvd b\<close> obtain k where "b = c * k"
huffman@29404:     by (rule dvdE)
huffman@29404:   have "a mod b mod c = a mod (c * k) mod c"
wenzelm@60758:     by (simp only: \<open>b = c * k\<close>)
huffman@29404:   also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
huffman@29404:     by (simp only: mod_mult_self1)
huffman@29404:   also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
haftmann@58786:     by (simp only: ac_simps)
huffman@29404:   also have "\<dots> = a mod c"
huffman@29404:     by (simp only: mod_div_equality)
huffman@29404:   finally show ?thesis .
huffman@29404: qed
huffman@29404: 
haftmann@30930: lemma div_mult_mult2 [simp]:
haftmann@30930:   "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
haftmann@57512:   by (drule div_mult_mult1) (simp add: mult.commute)
haftmann@30930: 
haftmann@30930: lemma div_mult_mult1_if [simp]:
haftmann@30930:   "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
haftmann@30930:   by simp_all
nipkow@30476: 
haftmann@30930: lemma mod_mult_mult1:
haftmann@30930:   "(c * a) mod (c * b) = c * (a mod b)"
haftmann@30930: proof (cases "c = 0")
haftmann@30930:   case True then show ?thesis by simp
haftmann@30930: next
haftmann@30930:   case False
haftmann@30930:   from mod_div_equality
haftmann@30930:   have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
haftmann@30930:   with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
haftmann@30930:     = c * a + c * (a mod b)" by (simp add: algebra_simps)
lp15@60562:   with mod_div_equality show ?thesis by simp
haftmann@30930: qed
lp15@60562: 
haftmann@30930: lemma mod_mult_mult2:
haftmann@30930:   "(a * c) mod (b * c) = (a mod b) * c"
haftmann@57512:   using mod_mult_mult1 [of c a b] by (simp add: mult.commute)
haftmann@30930: 
huffman@47159: lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"
huffman@47159:   by (fact mod_mult_mult2 [symmetric])
huffman@47159: 
huffman@47159: lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"
huffman@47159:   by (fact mod_mult_mult1 [symmetric])
huffman@47159: 
huffman@31662: lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
huffman@31662:   unfolding dvd_def by (auto simp add: mod_mult_mult1)
huffman@31662: 
huffman@31662: lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
huffman@31662: by (blast intro: dvd_mod_imp_dvd dvd_mod)
huffman@31662: 
huffman@31661: end
huffman@31661: 
haftmann@59833: class ring_div = comm_ring_1 + semiring_div
huffman@29405: begin
huffman@29405: 
haftmann@60353: subclass idom_divide ..
haftmann@36634: 
wenzelm@60758: text \<open>Negation respects modular equivalence.\<close>
huffman@29405: 
huffman@29405: lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
huffman@29405: proof -
huffman@29405:   have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
huffman@29405:     by (simp only: mod_div_equality)
huffman@29405:   also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
haftmann@57514:     by (simp add: ac_simps)
huffman@29405:   also have "\<dots> = (- (a mod b)) mod b"
huffman@29405:     by (rule mod_mult_self1)
huffman@29405:   finally show ?thesis .
huffman@29405: qed
huffman@29405: 
huffman@29405: lemma mod_minus_cong:
huffman@29405:   assumes "a mod b = a' mod b"
huffman@29405:   shows "(- a) mod b = (- a') mod b"
huffman@29405: proof -
huffman@29405:   have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
huffman@29405:     unfolding assms ..
huffman@29405:   thus ?thesis
huffman@29405:     by (simp only: mod_minus_eq [symmetric])
huffman@29405: qed
huffman@29405: 
wenzelm@60758: text \<open>Subtraction respects modular equivalence.\<close>
huffman@29405: 
haftmann@54230: lemma mod_diff_left_eq:
haftmann@54230:   "(a - b) mod c = (a mod c - b) mod c"
haftmann@54230:   using mod_add_cong [of a c "a mod c" "- b" "- b"] by simp
haftmann@54230: 
haftmann@54230: lemma mod_diff_right_eq:
haftmann@54230:   "(a - b) mod c = (a - b mod c) mod c"
haftmann@54230:   using mod_add_cong [of a c a "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp
haftmann@54230: 
haftmann@54230: lemma mod_diff_eq:
haftmann@54230:   "(a - b) mod c = (a mod c - b mod c) mod c"
haftmann@54230:   using mod_add_cong [of a c "a mod c" "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp
huffman@29405: 
huffman@29405: lemma mod_diff_cong:
huffman@29405:   assumes "a mod c = a' mod c"
huffman@29405:   assumes "b mod c = b' mod c"
huffman@29405:   shows "(a - b) mod c = (a' - b') mod c"
haftmann@54230:   using assms mod_add_cong [of a c a' "- b" "- b'"] mod_minus_cong [of b c "b'"] by simp
huffman@29405: 
nipkow@30180: lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
nipkow@30180: apply (case_tac "y = 0") apply simp
nipkow@30180: apply (auto simp add: dvd_def)
nipkow@30180: apply (subgoal_tac "-(y * k) = y * - k")
thomas@57492:  apply (simp only:)
nipkow@30180:  apply (erule div_mult_self1_is_id)
nipkow@30180: apply simp
nipkow@30180: done
nipkow@30180: 
nipkow@30180: lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
nipkow@30180: apply (case_tac "y = 0") apply simp
nipkow@30180: apply (auto simp add: dvd_def)
nipkow@30180: apply (subgoal_tac "y * k = -y * -k")
thomas@57492:  apply (erule ssubst, rule div_mult_self1_is_id)
nipkow@30180:  apply simp
nipkow@30180: apply simp
nipkow@30180: done
nipkow@30180: 
haftmann@60867: lemma div_diff [simp]:
haftmann@60867:   "z dvd x \<Longrightarrow> z dvd y \<Longrightarrow> (x - y) div z = x div z - y div z"
haftmann@60867:   using div_add [of _ _ "- y"] by (simp add: dvd_neg_div)
nipkow@59380: 
huffman@47159: lemma div_minus_minus [simp]: "(-a) div (-b) = a div b"
huffman@47159:   using div_mult_mult1 [of "- 1" a b]
huffman@47159:   unfolding neg_equal_0_iff_equal by simp
huffman@47159: 
huffman@47159: lemma mod_minus_minus [simp]: "(-a) mod (-b) = - (a mod b)"
huffman@47159:   using mod_mult_mult1 [of "- 1" a b] by simp
huffman@47159: 
huffman@47159: lemma div_minus_right: "a div (-b) = (-a) div b"
huffman@47159:   using div_minus_minus [of "-a" b] by simp
huffman@47159: 
huffman@47159: lemma mod_minus_right: "a mod (-b) = - ((-a) mod b)"
huffman@47159:   using mod_minus_minus [of "-a" b] by simp
huffman@47159: 
huffman@47160: lemma div_minus1_right [simp]: "a div (-1) = -a"
huffman@47160:   using div_minus_right [of a 1] by simp
huffman@47160: 
huffman@47160: lemma mod_minus1_right [simp]: "a mod (-1) = 0"
huffman@47160:   using mod_minus_right [of a 1] by simp
huffman@47160: 
lp15@60562: lemma minus_mod_self2 [simp]:
haftmann@54221:   "(a - b) mod b = a mod b"
haftmann@54221:   by (simp add: mod_diff_right_eq)
haftmann@54221: 
lp15@60562: lemma minus_mod_self1 [simp]:
haftmann@54221:   "(b - a) mod b = - a mod b"
haftmann@54230:   using mod_add_self2 [of "- a" b] by simp
haftmann@54221: 
huffman@29405: end
huffman@29405: 
haftmann@58778: 
wenzelm@60758: subsubsection \<open>Parity and division\<close>
haftmann@58778: 
lp15@60562: class semiring_div_parity = semiring_div + comm_semiring_1_cancel + numeral +
haftmann@54226:   assumes parity: "a mod 2 = 0 \<or> a mod 2 = 1"
haftmann@58786:   assumes one_mod_two_eq_one [simp]: "1 mod 2 = 1"
haftmann@58710:   assumes zero_not_eq_two: "0 \<noteq> 2"
haftmann@54226: begin
haftmann@54226: 
haftmann@54226: lemma parity_cases [case_names even odd]:
haftmann@54226:   assumes "a mod 2 = 0 \<Longrightarrow> P"
haftmann@54226:   assumes "a mod 2 = 1 \<Longrightarrow> P"
haftmann@54226:   shows P
haftmann@54226:   using assms parity by blast
haftmann@54226: 
haftmann@58786: lemma one_div_two_eq_zero [simp]:
haftmann@58778:   "1 div 2 = 0"
haftmann@58778: proof (cases "2 = 0")
haftmann@58778:   case True then show ?thesis by simp
haftmann@58778: next
haftmann@58778:   case False
haftmann@58778:   from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" .
haftmann@58778:   with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp
haftmann@58953:   then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq del: mult_eq_0_iff)
haftmann@58953:   then have "1 div 2 = 0 \<or> 2 = 0" by simp
haftmann@58778:   with False show ?thesis by auto
haftmann@58778: qed
haftmann@58778: 
haftmann@58786: lemma not_mod_2_eq_0_eq_1 [simp]:
haftmann@58786:   "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
haftmann@58786:   by (cases a rule: parity_cases) simp_all
haftmann@58786: 
haftmann@58786: lemma not_mod_2_eq_1_eq_0 [simp]:
haftmann@58786:   "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
haftmann@58786:   by (cases a rule: parity_cases) simp_all
haftmann@58786: 
haftmann@58778: subclass semiring_parity
haftmann@58778: proof (unfold_locales, unfold dvd_eq_mod_eq_0 not_mod_2_eq_0_eq_1)
haftmann@58778:   show "1 mod 2 = 1"
haftmann@58778:     by (fact one_mod_two_eq_one)
haftmann@58778: next
haftmann@58778:   fix a b
haftmann@58778:   assume "a mod 2 = 1"
haftmann@58778:   moreover assume "b mod 2 = 1"
haftmann@58778:   ultimately show "(a + b) mod 2 = 0"
haftmann@58778:     using mod_add_eq [of a b 2] by simp
haftmann@58778: next
haftmann@58778:   fix a b
haftmann@58778:   assume "(a * b) mod 2 = 0"
haftmann@58778:   then have "(a mod 2) * (b mod 2) = 0"
haftmann@58778:     by (cases "a mod 2 = 0") (simp_all add: mod_mult_eq [of a b 2])
haftmann@58778:   then show "a mod 2 = 0 \<or> b mod 2 = 0"
haftmann@58778:     by (rule divisors_zero)
haftmann@58778: next
haftmann@58778:   fix a
haftmann@58778:   assume "a mod 2 = 1"
haftmann@58778:   then have "a = a div 2 * 2 + 1" using mod_div_equality [of a 2] by simp
haftmann@58778:   then show "\<exists>b. a = b + 1" ..
haftmann@58778: qed
haftmann@58778: 
haftmann@58778: lemma even_iff_mod_2_eq_zero:
haftmann@58778:   "even a \<longleftrightarrow> a mod 2 = 0"
haftmann@58778:   by (fact dvd_eq_mod_eq_0)
haftmann@58778: 
haftmann@58778: lemma even_succ_div_two [simp]:
haftmann@58778:   "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
haftmann@58778:   by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
haftmann@58778: 
haftmann@58778: lemma odd_succ_div_two [simp]:
haftmann@58778:   "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
haftmann@58778:   by (auto elim!: oddE simp add: zero_not_eq_two [symmetric] add.assoc)
haftmann@58778: 
haftmann@58778: lemma even_two_times_div_two:
haftmann@58778:   "even a \<Longrightarrow> 2 * (a div 2) = a"
haftmann@58778:   by (fact dvd_mult_div_cancel)
haftmann@58778: 
haftmann@58834: lemma odd_two_times_div_two_succ [simp]:
haftmann@58778:   "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
haftmann@58778:   using mod_div_equality2 [of 2 a] by (simp add: even_iff_mod_2_eq_zero)
haftmann@60868:  
haftmann@54226: end
haftmann@54226: 
haftmann@25942: 
wenzelm@60758: subsection \<open>Generic numeral division with a pragmatic type class\<close>
wenzelm@60758: 
wenzelm@60758: text \<open>
haftmann@53067:   The following type class contains everything necessary to formulate
haftmann@53067:   a division algorithm in ring structures with numerals, restricted
haftmann@53067:   to its positive segments.  This is its primary motiviation, and it
haftmann@53067:   could surely be formulated using a more fine-grained, more algebraic
haftmann@53067:   and less technical class hierarchy.
wenzelm@60758: \<close>
haftmann@53067: 
lp15@60562: class semiring_numeral_div = semiring_div + comm_semiring_1_cancel + linordered_semidom +
haftmann@59816:   assumes div_less: "0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a div b = 0"
haftmann@53067:     and mod_less: " 0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a mod b = a"
haftmann@53067:     and div_positive: "0 < b \<Longrightarrow> b \<le> a \<Longrightarrow> a div b > 0"
haftmann@53067:     and mod_less_eq_dividend: "0 \<le> a \<Longrightarrow> a mod b \<le> a"
haftmann@53067:     and pos_mod_bound: "0 < b \<Longrightarrow> a mod b < b"
haftmann@53067:     and pos_mod_sign: "0 < b \<Longrightarrow> 0 \<le> a mod b"
haftmann@53067:     and mod_mult2_eq: "0 \<le> c \<Longrightarrow> a mod (b * c) = b * (a div b mod c) + a mod b"
haftmann@53067:     and div_mult2_eq: "0 \<le> c \<Longrightarrow> a div (b * c) = a div b div c"
haftmann@53067:   assumes discrete: "a < b \<longleftrightarrow> a + 1 \<le> b"
haftmann@61275:   fixes divmod :: "num \<Rightarrow> num \<Rightarrow> 'a \<times> 'a"
haftmann@61275:     and divmod_step :: "num \<Rightarrow> 'a \<times> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@61275:   assumes divmod_def: "divmod m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@61275:     and divmod_step_def: "divmod_step l qr = (let (q, r) = qr
haftmann@61275:     in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275:     else (2 * q, r))"
wenzelm@61799:     \<comment> \<open>These are conceptually definitions but force generated code
haftmann@61275:     to be monomorphic wrt. particular instances of this class which
haftmann@61275:     yields a significant speedup.\<close>
haftmann@61275: 
haftmann@53067: begin
haftmann@53067: 
haftmann@59816: lemma mult_div_cancel:
haftmann@59816:   "b * (a div b) = a - a mod b"
haftmann@59816: proof -
haftmann@59816:   have "b * (a div b) + a mod b = a"
haftmann@59816:     using mod_div_equality [of a b] by (simp add: ac_simps)
haftmann@59816:   then have "b * (a div b) + a mod b - a mod b = a - a mod b"
haftmann@59816:     by simp
haftmann@59816:   then show ?thesis
haftmann@59816:     by simp
haftmann@59816: qed
haftmann@53067: 
haftmann@54226: subclass semiring_div_parity
haftmann@54226: proof
haftmann@54226:   fix a
haftmann@54226:   show "a mod 2 = 0 \<or> a mod 2 = 1"
haftmann@54226:   proof (rule ccontr)
haftmann@54226:     assume "\<not> (a mod 2 = 0 \<or> a mod 2 = 1)"
haftmann@54226:     then have "a mod 2 \<noteq> 0" and "a mod 2 \<noteq> 1" by simp_all
haftmann@54226:     have "0 < 2" by simp
haftmann@54226:     with pos_mod_bound pos_mod_sign have "0 \<le> a mod 2" "a mod 2 < 2" by simp_all
wenzelm@60758:     with \<open>a mod 2 \<noteq> 0\<close> have "0 < a mod 2" by simp
haftmann@54226:     with discrete have "1 \<le> a mod 2" by simp
wenzelm@60758:     with \<open>a mod 2 \<noteq> 1\<close> have "1 < a mod 2" by simp
haftmann@54226:     with discrete have "2 \<le> a mod 2" by simp
wenzelm@60758:     with \<open>a mod 2 < 2\<close> show False by simp
haftmann@54226:   qed
haftmann@58646: next
haftmann@58646:   show "1 mod 2 = 1"
haftmann@58646:     by (rule mod_less) simp_all
haftmann@58710: next
haftmann@58710:   show "0 \<noteq> 2"
haftmann@58710:     by simp
haftmann@53067: qed
haftmann@53067: 
haftmann@53067: lemma divmod_digit_1:
haftmann@53067:   assumes "0 \<le> a" "0 < b" and "b \<le> a mod (2 * b)"
haftmann@53067:   shows "2 * (a div (2 * b)) + 1 = a div b" (is "?P")
haftmann@53067:     and "a mod (2 * b) - b = a mod b" (is "?Q")
haftmann@53067: proof -
haftmann@53067:   from assms mod_less_eq_dividend [of a "2 * b"] have "b \<le> a"
haftmann@53067:     by (auto intro: trans)
wenzelm@60758:   with \<open>0 < b\<close> have "0 < a div b" by (auto intro: div_positive)
haftmann@53067:   then have [simp]: "1 \<le> a div b" by (simp add: discrete)
wenzelm@60758:   with \<open>0 < b\<close> have mod_less: "a mod b < b" by (simp add: pos_mod_bound)
haftmann@53067:   def w \<equiv> "a div b mod 2" with parity have w_exhaust: "w = 0 \<or> w = 1" by auto
haftmann@53067:   have mod_w: "a mod (2 * b) = a mod b + b * w"
haftmann@53067:     by (simp add: w_def mod_mult2_eq ac_simps)
haftmann@53067:   from assms w_exhaust have "w = 1"
haftmann@53067:     by (auto simp add: mod_w) (insert mod_less, auto)
haftmann@53067:   with mod_w have mod: "a mod (2 * b) = a mod b + b" by simp
haftmann@53067:   have "2 * (a div (2 * b)) = a div b - w"
haftmann@53067:     by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)
wenzelm@60758:   with \<open>w = 1\<close> have div: "2 * (a div (2 * b)) = a div b - 1" by simp
haftmann@53067:   then show ?P and ?Q
haftmann@60867:     by (simp_all add: div mod add_implies_diff [symmetric])
haftmann@53067: qed
haftmann@53067: 
haftmann@53067: lemma divmod_digit_0:
haftmann@53067:   assumes "0 < b" and "a mod (2 * b) < b"
haftmann@53067:   shows "2 * (a div (2 * b)) = a div b" (is "?P")
haftmann@53067:     and "a mod (2 * b) = a mod b" (is "?Q")
haftmann@53067: proof -
haftmann@53067:   def w \<equiv> "a div b mod 2" with parity have w_exhaust: "w = 0 \<or> w = 1" by auto
haftmann@53067:   have mod_w: "a mod (2 * b) = a mod b + b * w"
haftmann@53067:     by (simp add: w_def mod_mult2_eq ac_simps)
haftmann@53067:   moreover have "b \<le> a mod b + b"
haftmann@53067:   proof -
wenzelm@60758:     from \<open>0 < b\<close> pos_mod_sign have "0 \<le> a mod b" by blast
haftmann@53067:     then have "0 + b \<le> a mod b + b" by (rule add_right_mono)
haftmann@53067:     then show ?thesis by simp
haftmann@53067:   qed
haftmann@53067:   moreover note assms w_exhaust
haftmann@53067:   ultimately have "w = 0" by auto
haftmann@53067:   with mod_w have mod: "a mod (2 * b) = a mod b" by simp
haftmann@53067:   have "2 * (a div (2 * b)) = a div b - w"
haftmann@53067:     by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)
wenzelm@60758:   with \<open>w = 0\<close> have div: "2 * (a div (2 * b)) = a div b" by simp
haftmann@53067:   then show ?P and ?Q
haftmann@53067:     by (simp_all add: div mod)
haftmann@53067: qed
haftmann@53067: 
haftmann@60867: lemma fst_divmod:
haftmann@53067:   "fst (divmod m n) = numeral m div numeral n"
haftmann@53067:   by (simp add: divmod_def)
haftmann@53067: 
haftmann@60867: lemma snd_divmod:
haftmann@53067:   "snd (divmod m n) = numeral m mod numeral n"
haftmann@53067:   by (simp add: divmod_def)
haftmann@53067: 
wenzelm@60758: text \<open>
haftmann@53067:   This is a formulation of one step (referring to one digit position)
haftmann@53067:   in school-method division: compare the dividend at the current
haftmann@53070:   digit position with the remainder from previous division steps
haftmann@53067:   and evaluate accordingly.
wenzelm@60758: \<close>
haftmann@53067: 
haftmann@61275: lemma divmod_step_eq [simp]:
haftmann@53067:   "divmod_step l (q, r) = (if numeral l \<le> r
haftmann@53067:     then (2 * q + 1, r - numeral l) else (2 * q, r))"
haftmann@53067:   by (simp add: divmod_step_def)
haftmann@53067: 
wenzelm@60758: text \<open>
haftmann@53067:   This is a formulation of school-method division.
haftmann@53067:   If the divisor is smaller than the dividend, terminate.
haftmann@53067:   If not, shift the dividend to the right until termination
haftmann@53067:   occurs and then reiterate single division steps in the
haftmann@53067:   opposite direction.
wenzelm@60758: \<close>
haftmann@53067: 
haftmann@60867: lemma divmod_divmod_step:
haftmann@53067:   "divmod m n = (if m < n then (0, numeral m)
haftmann@53067:     else divmod_step n (divmod m (Num.Bit0 n)))"
haftmann@53067: proof (cases "m < n")
haftmann@53067:   case True then have "numeral m < numeral n" by simp
haftmann@53067:   then show ?thesis
haftmann@60867:     by (simp add: prod_eq_iff div_less mod_less fst_divmod snd_divmod)
haftmann@53067: next
haftmann@53067:   case False
haftmann@53067:   have "divmod m n =
haftmann@53067:     divmod_step n (numeral m div (2 * numeral n),
haftmann@53067:       numeral m mod (2 * numeral n))"
haftmann@53067:   proof (cases "numeral n \<le> numeral m mod (2 * numeral n)")
haftmann@53067:     case True
haftmann@60867:     with divmod_step_eq
haftmann@53067:       have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =
haftmann@53067:         (2 * (numeral m div (2 * numeral n)) + 1, numeral m mod (2 * numeral n) - numeral n)"
haftmann@60867:         by simp
haftmann@53067:     moreover from True divmod_digit_1 [of "numeral m" "numeral n"]
haftmann@53067:       have "2 * (numeral m div (2 * numeral n)) + 1 = numeral m div numeral n"
haftmann@53067:       and "numeral m mod (2 * numeral n) - numeral n = numeral m mod numeral n"
haftmann@53067:       by simp_all
haftmann@53067:     ultimately show ?thesis by (simp only: divmod_def)
haftmann@53067:   next
haftmann@53067:     case False then have *: "numeral m mod (2 * numeral n) < numeral n"
haftmann@53067:       by (simp add: not_le)
haftmann@60867:     with divmod_step_eq
haftmann@53067:       have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =
haftmann@53067:         (2 * (numeral m div (2 * numeral n)), numeral m mod (2 * numeral n))"
haftmann@60867:         by auto
haftmann@53067:     moreover from * divmod_digit_0 [of "numeral n" "numeral m"]
haftmann@53067:       have "2 * (numeral m div (2 * numeral n)) = numeral m div numeral n"
haftmann@53067:       and "numeral m mod (2 * numeral n) = numeral m mod numeral n"
haftmann@53067:       by (simp_all only: zero_less_numeral)
haftmann@53067:     ultimately show ?thesis by (simp only: divmod_def)
haftmann@53067:   qed
haftmann@53067:   then have "divmod m n =
haftmann@53067:     divmod_step n (numeral m div numeral (Num.Bit0 n),
haftmann@53067:       numeral m mod numeral (Num.Bit0 n))"
lp15@60562:     by (simp only: numeral.simps distrib mult_1)
haftmann@53067:   then have "divmod m n = divmod_step n (divmod m (Num.Bit0 n))"
haftmann@53067:     by (simp add: divmod_def)
haftmann@53067:   with False show ?thesis by simp
haftmann@53067: qed
haftmann@53067: 
wenzelm@61799: text \<open>The division rewrite proper -- first, trivial results involving \<open>1\<close>\<close>
haftmann@60867: 
haftmann@61275: lemma divmod_trivial [simp]:
haftmann@60867:   "divmod Num.One Num.One = (numeral Num.One, 0)"
haftmann@60867:   "divmod (Num.Bit0 m) Num.One = (numeral (Num.Bit0 m), 0)"
haftmann@60867:   "divmod (Num.Bit1 m) Num.One = (numeral (Num.Bit1 m), 0)"
haftmann@60867:   "divmod num.One (num.Bit0 n) = (0, Numeral1)"
haftmann@60867:   "divmod num.One (num.Bit1 n) = (0, Numeral1)"
haftmann@60867:   using divmod_divmod_step [of "Num.One"] by (simp_all add: divmod_def)
haftmann@60867: 
haftmann@60867: text \<open>Division by an even number is a right-shift\<close>
haftmann@58953: 
haftmann@61275: lemma divmod_cancel [simp]:
haftmann@53069:   "divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))" (is ?P)
haftmann@53069:   "divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))" (is ?Q)
haftmann@53069: proof -
haftmann@53069:   have *: "\<And>q. numeral (Num.Bit0 q) = 2 * numeral q"
haftmann@53069:     "\<And>q. numeral (Num.Bit1 q) = 2 * numeral q + 1"
haftmann@53069:     by (simp_all only: numeral_mult numeral.simps distrib) simp_all
haftmann@53069:   have "1 div 2 = 0" "1 mod 2 = 1" by (auto intro: div_less mod_less)
haftmann@53069:   then show ?P and ?Q
haftmann@60867:     by (simp_all add: fst_divmod snd_divmod prod_eq_iff split_def * [of m] * [of n] mod_mult_mult1
haftmann@60867:       div_mult2_eq [of _ _ 2] mod_mult2_eq [of _ _ 2]
haftmann@60867:       add.commute del: numeral_times_numeral)
haftmann@58953: qed
haftmann@58953: 
haftmann@60867: text \<open>The really hard work\<close>
haftmann@60867: 
haftmann@61275: lemma divmod_steps [simp]:
haftmann@60867:   "divmod (num.Bit0 m) (num.Bit1 n) =
haftmann@60867:       (if m \<le> n then (0, numeral (num.Bit0 m))
haftmann@60867:        else divmod_step (num.Bit1 n)
haftmann@60867:              (divmod (num.Bit0 m)
haftmann@60867:                (num.Bit0 (num.Bit1 n))))"
haftmann@60867:   "divmod (num.Bit1 m) (num.Bit1 n) =
haftmann@60867:       (if m < n then (0, numeral (num.Bit1 m))
haftmann@60867:        else divmod_step (num.Bit1 n)
haftmann@60867:              (divmod (num.Bit1 m)
haftmann@60867:                (num.Bit0 (num.Bit1 n))))"
haftmann@60867:   by (simp_all add: divmod_divmod_step)
haftmann@60867: 
haftmann@61275: lemmas divmod_algorithm_code = divmod_step_eq divmod_trivial divmod_cancel divmod_steps  
haftmann@61275: 
wenzelm@60758: text \<open>Special case: divisibility\<close>
haftmann@58953: 
haftmann@58953: definition divides_aux :: "'a \<times> 'a \<Rightarrow> bool"
haftmann@58953: where
haftmann@58953:   "divides_aux qr \<longleftrightarrow> snd qr = 0"
haftmann@58953: 
haftmann@58953: lemma divides_aux_eq [simp]:
haftmann@58953:   "divides_aux (q, r) \<longleftrightarrow> r = 0"
haftmann@58953:   by (simp add: divides_aux_def)
haftmann@58953: 
haftmann@58953: lemma dvd_numeral_simp [simp]:
haftmann@58953:   "numeral m dvd numeral n \<longleftrightarrow> divides_aux (divmod n m)"
haftmann@58953:   by (simp add: divmod_def mod_eq_0_iff_dvd)
haftmann@53069: 
haftmann@60867: text \<open>Generic computation of quotient and remainder\<close>  
haftmann@60867: 
haftmann@60867: lemma numeral_div_numeral [simp]: 
haftmann@60867:   "numeral k div numeral l = fst (divmod k l)"
haftmann@60867:   by (simp add: fst_divmod)
haftmann@60867: 
haftmann@60867: lemma numeral_mod_numeral [simp]: 
haftmann@60867:   "numeral k mod numeral l = snd (divmod k l)"
haftmann@60867:   by (simp add: snd_divmod)
haftmann@60867: 
haftmann@60867: lemma one_div_numeral [simp]:
haftmann@60867:   "1 div numeral n = fst (divmod num.One n)"
haftmann@60867:   by (simp add: fst_divmod)
haftmann@60867: 
haftmann@60867: lemma one_mod_numeral [simp]:
haftmann@60867:   "1 mod numeral n = snd (divmod num.One n)"
haftmann@60867:   by (simp add: snd_divmod)
haftmann@60867:   
haftmann@53067: end
haftmann@53067: 
lp15@60562: 
wenzelm@60758: subsection \<open>Division on @{typ nat}\<close>
wenzelm@60758: 
haftmann@61433: context
haftmann@61433: begin
haftmann@61433: 
wenzelm@60758: text \<open>
haftmann@60429:   We define @{const divide} and @{const mod} on @{typ nat} by means
haftmann@26100:   of a characteristic relation with two input arguments
wenzelm@61076:   @{term "m::nat"}, @{term "n::nat"} and two output arguments
wenzelm@61076:   @{term "q::nat"}(uotient) and @{term "r::nat"}(emainder).
wenzelm@60758: \<close>
haftmann@26100: 
haftmann@33340: definition divmod_nat_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@33340:   "divmod_nat_rel m n qr \<longleftrightarrow>
haftmann@30923:     m = fst qr * n + snd qr \<and>
haftmann@30923:       (if n = 0 then fst qr = 0 else if n > 0 then 0 \<le> snd qr \<and> snd qr < n else n < snd qr \<and> snd qr \<le> 0)"
haftmann@26100: 
wenzelm@60758: text \<open>@{const divmod_nat_rel} is total:\<close>
haftmann@26100: 
haftmann@61433: qualified lemma divmod_nat_rel_ex:
haftmann@33340:   obtains q r where "divmod_nat_rel m n (q, r)"
haftmann@26100: proof (cases "n = 0")
haftmann@30923:   case True  with that show thesis
haftmann@33340:     by (auto simp add: divmod_nat_rel_def)
haftmann@26100: next
haftmann@26100:   case False
haftmann@26100:   have "\<exists>q r. m = q * n + r \<and> r < n"
haftmann@26100:   proof (induct m)
wenzelm@60758:     case 0 with \<open>n \<noteq> 0\<close>
wenzelm@61076:     have "(0::nat) = 0 * n + 0 \<and> 0 < n" by simp
haftmann@26100:     then show ?case by blast
haftmann@26100:   next
haftmann@26100:     case (Suc m) then obtain q' r'
haftmann@26100:       where m: "m = q' * n + r'" and n: "r' < n" by auto
haftmann@26100:     then show ?case proof (cases "Suc r' < n")
haftmann@26100:       case True
haftmann@26100:       from m n have "Suc m = q' * n + Suc r'" by simp
haftmann@26100:       with True show ?thesis by blast
haftmann@26100:     next
haftmann@26100:       case False then have "n \<le> Suc r'" by auto
haftmann@26100:       moreover from n have "Suc r' \<le> n" by auto
haftmann@26100:       ultimately have "n = Suc r'" by auto
haftmann@26100:       with m have "Suc m = Suc q' * n + 0" by simp
wenzelm@60758:       with \<open>n \<noteq> 0\<close> show ?thesis by blast
haftmann@26100:     qed
haftmann@26100:   qed
haftmann@26100:   with that show thesis
wenzelm@60758:     using \<open>n \<noteq> 0\<close> by (auto simp add: divmod_nat_rel_def)
haftmann@26100: qed
haftmann@26100: 
wenzelm@60758: text \<open>@{const divmod_nat_rel} is injective:\<close>
haftmann@26100: 
haftmann@61433: qualified lemma divmod_nat_rel_unique:
haftmann@33340:   assumes "divmod_nat_rel m n qr"
haftmann@33340:     and "divmod_nat_rel m n qr'"
haftmann@30923:   shows "qr = qr'"
haftmann@26100: proof (cases "n = 0")
haftmann@26100:   case True with assms show ?thesis
haftmann@30923:     by (cases qr, cases qr')
haftmann@33340:       (simp add: divmod_nat_rel_def)
haftmann@26100: next
haftmann@26100:   case False
wenzelm@61076:   have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q::nat)"
haftmann@26100:   apply (rule leI)
haftmann@26100:   apply (subst less_iff_Suc_add)
haftmann@26100:   apply (auto simp add: add_mult_distrib)
haftmann@26100:   done
wenzelm@60758:   from \<open>n \<noteq> 0\<close> assms have *: "fst qr = fst qr'"
haftmann@33340:     by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)
wenzelm@53374:   with assms have "snd qr = snd qr'"
haftmann@33340:     by (simp add: divmod_nat_rel_def)
wenzelm@53374:   with * show ?thesis by (cases qr, cases qr') simp
haftmann@26100: qed
haftmann@26100: 
wenzelm@60758: text \<open>
haftmann@26100:   We instantiate divisibility on the natural numbers by
haftmann@33340:   means of @{const divmod_nat_rel}:
wenzelm@60758: \<close>
haftmann@25942: 
haftmann@61433: qualified definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@37767:   "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"
haftmann@30923: 
haftmann@61433: qualified lemma divmod_nat_rel_divmod_nat:
haftmann@33340:   "divmod_nat_rel m n (divmod_nat m n)"
haftmann@30923: proof -
haftmann@33340:   from divmod_nat_rel_ex
haftmann@33340:     obtain qr where rel: "divmod_nat_rel m n qr" .
haftmann@30923:   then show ?thesis
haftmann@33340:   by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)
haftmann@30923: qed
haftmann@30923: 
haftmann@61433: qualified lemma divmod_nat_unique:
lp15@60562:   assumes "divmod_nat_rel m n qr"
haftmann@33340:   shows "divmod_nat m n = qr"
haftmann@33340:   using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)
haftmann@26100: 
haftmann@61433: qualified lemma divmod_nat_zero: "divmod_nat m 0 = (0, m)"
haftmann@61433:   by (simp add: Divides.divmod_nat_unique divmod_nat_rel_def)
haftmann@61433: 
haftmann@61433: qualified lemma divmod_nat_zero_left: "divmod_nat 0 n = (0, 0)"
haftmann@61433:   by (simp add: Divides.divmod_nat_unique divmod_nat_rel_def)
haftmann@61433: 
haftmann@61433: qualified lemma divmod_nat_base: "m < n \<Longrightarrow> divmod_nat m n = (0, m)"
haftmann@61433:   by (simp add: divmod_nat_unique divmod_nat_rel_def)
haftmann@61433: 
haftmann@61433: qualified lemma divmod_nat_step:
haftmann@61433:   assumes "0 < n" and "n \<le> m"
haftmann@61433:   shows "divmod_nat m n = apfst Suc (divmod_nat (m - n) n)"
haftmann@61433: proof (rule divmod_nat_unique)
haftmann@61433:   have "divmod_nat_rel (m - n) n (divmod_nat (m - n) n)"
haftmann@61433:     by (fact divmod_nat_rel_divmod_nat)
haftmann@61433:   then show "divmod_nat_rel m n (apfst Suc (divmod_nat (m - n) n))"
haftmann@61433:     unfolding divmod_nat_rel_def using assms by auto
haftmann@61433: qed
haftmann@61433: 
haftmann@61433: end
haftmann@61433:   
haftmann@60429: instantiation nat :: semiring_div
haftmann@60352: begin
haftmann@60352: 
haftmann@60352: definition divide_nat where
haftmann@61433:   div_nat_def: "m div n = fst (Divides.divmod_nat m n)"
haftmann@60352: 
haftmann@60352: definition mod_nat where
haftmann@61433:   "m mod n = snd (Divides.divmod_nat m n)"
huffman@46551: 
huffman@46551: lemma fst_divmod_nat [simp]:
haftmann@61433:   "fst (Divides.divmod_nat m n) = m div n"
huffman@46551:   by (simp add: div_nat_def)
huffman@46551: 
huffman@46551: lemma snd_divmod_nat [simp]:
haftmann@61433:   "snd (Divides.divmod_nat m n) = m mod n"
huffman@46551:   by (simp add: mod_nat_def)
huffman@46551: 
haftmann@33340: lemma divmod_nat_div_mod:
haftmann@61433:   "Divides.divmod_nat m n = (m div n, m mod n)"
huffman@46551:   by (simp add: prod_eq_iff)
haftmann@26100: 
huffman@47135: lemma div_nat_unique:
lp15@60562:   assumes "divmod_nat_rel m n (q, r)"
haftmann@26100:   shows "m div n = q"
haftmann@61433:   using assms by (auto dest!: Divides.divmod_nat_unique simp add: prod_eq_iff)
huffman@47135: 
huffman@47135: lemma mod_nat_unique:
lp15@60562:   assumes "divmod_nat_rel m n (q, r)"
haftmann@26100:   shows "m mod n = r"
haftmann@61433:   using assms by (auto dest!: Divides.divmod_nat_unique simp add: prod_eq_iff)
haftmann@25571: 
haftmann@33340: lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"
haftmann@61433:   using Divides.divmod_nat_rel_divmod_nat by (simp add: divmod_nat_div_mod)
haftmann@25942: 
wenzelm@60758: text \<open>The ''recursion'' equations for @{const divide} and @{const mod}\<close>
haftmann@26100: 
haftmann@26100: lemma div_less [simp]:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   assumes "m < n"
haftmann@26100:   shows "m div n = 0"
haftmann@61433:   using assms Divides.divmod_nat_base by (simp add: prod_eq_iff)
haftmann@25942: 
haftmann@26100: lemma le_div_geq:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   assumes "0 < n" and "n \<le> m"
haftmann@26100:   shows "m div n = Suc ((m - n) div n)"
haftmann@61433:   using assms Divides.divmod_nat_step by (simp add: prod_eq_iff)
paulson@14267: 
haftmann@26100: lemma mod_less [simp]:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   assumes "m < n"
haftmann@26100:   shows "m mod n = m"
haftmann@61433:   using assms Divides.divmod_nat_base by (simp add: prod_eq_iff)
haftmann@26100: 
haftmann@26100: lemma le_mod_geq:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   assumes "n \<le> m"
haftmann@26100:   shows "m mod n = (m - n) mod n"
haftmann@61433:   using assms Divides.divmod_nat_step by (cases "n = 0") (simp_all add: prod_eq_iff)
paulson@14267: 
huffman@47136: instance proof
huffman@47136:   fix m n :: nat
huffman@47136:   show "m div n * n + m mod n = m"
huffman@47136:     using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)
huffman@47136: next
huffman@47136:   fix m n q :: nat
huffman@47136:   assume "n \<noteq> 0"
huffman@47136:   then show "(q + m * n) div n = m + q div n"
huffman@47136:     by (induct m) (simp_all add: le_div_geq)
huffman@47136: next
huffman@47136:   fix m n q :: nat
huffman@47136:   assume "m \<noteq> 0"
huffman@47136:   hence "\<And>a b. divmod_nat_rel n q (a, b) \<Longrightarrow> divmod_nat_rel (m * n) (m * q) (a, m * b)"
huffman@47136:     unfolding divmod_nat_rel_def
huffman@47136:     by (auto split: split_if_asm, simp_all add: algebra_simps)
huffman@47136:   moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .
huffman@47136:   ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .
huffman@47136:   thus "(m * n) div (m * q) = n div q" by (rule div_nat_unique)
huffman@47136: next
huffman@47136:   fix n :: nat show "n div 0 = 0"
haftmann@61433:     by (simp add: div_nat_def Divides.divmod_nat_zero)
huffman@47136: next
huffman@47136:   fix n :: nat show "0 div n = 0"
haftmann@61433:     by (simp add: div_nat_def Divides.divmod_nat_zero_left)
haftmann@25942: qed
haftmann@26100: 
haftmann@25942: end
paulson@14267: 
haftmann@60685: instantiation nat :: normalization_semidom
haftmann@60685: begin
haftmann@60685: 
haftmann@60685: definition normalize_nat
haftmann@60685:   where [simp]: "normalize = (id :: nat \<Rightarrow> nat)"
haftmann@60685: 
haftmann@60685: definition unit_factor_nat
haftmann@60685:   where "unit_factor n = (if n = 0 then 0 else 1 :: nat)"
haftmann@60685: 
haftmann@60685: lemma unit_factor_simps [simp]:
haftmann@60685:   "unit_factor 0 = (0::nat)"
haftmann@60685:   "unit_factor (Suc n) = 1"
haftmann@60685:   by (simp_all add: unit_factor_nat_def)
haftmann@60685: 
haftmann@60685: instance
haftmann@60685:   by standard (simp_all add: unit_factor_nat_def)
haftmann@60685:   
haftmann@60685: end
haftmann@60685: 
haftmann@61433: lemma divmod_nat_if [code]:
haftmann@61433:   "Divides.divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
haftmann@61433:     let (q, r) = Divides.divmod_nat (m - n) n in (Suc q, r))"
blanchet@55414:   by (simp add: prod_eq_iff case_prod_beta not_less le_div_geq le_mod_geq)
haftmann@33361: 
wenzelm@60758: text \<open>Simproc for cancelling @{const divide} and @{const mod}\<close>
haftmann@25942: 
wenzelm@51299: ML_file "~~/src/Provers/Arith/cancel_div_mod.ML"
wenzelm@51299: 
wenzelm@60758: ML \<open>
wenzelm@43594: structure Cancel_Div_Mod_Nat = Cancel_Div_Mod
wenzelm@41550: (
haftmann@60352:   val div_name = @{const_name divide};
haftmann@30934:   val mod_name = @{const_name mod};
haftmann@30934:   val mk_binop = HOLogic.mk_binop;
huffman@48561:   val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
huffman@48561:   val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
huffman@48561:   fun mk_sum [] = HOLogic.zero
huffman@48561:     | mk_sum [t] = t
huffman@48561:     | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
huffman@48561:   fun dest_sum tm =
huffman@48561:     if HOLogic.is_zero tm then []
huffman@48561:     else
huffman@48561:       (case try HOLogic.dest_Suc tm of
huffman@48561:         SOME t => HOLogic.Suc_zero :: dest_sum t
huffman@48561:       | NONE =>
huffman@48561:           (case try dest_plus tm of
huffman@48561:             SOME (t, u) => dest_sum t @ dest_sum u
huffman@48561:           | NONE => [tm]));
haftmann@25942: 
haftmann@30934:   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
paulson@14267: 
haftmann@30934:   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
haftmann@57514:     (@{thm add_0_left} :: @{thm add_0_right} :: @{thms ac_simps}))
wenzelm@41550: )
wenzelm@60758: \<close>
wenzelm@60758: 
wenzelm@60758: simproc_setup cancel_div_mod_nat ("(m::nat) + n") = \<open>K Cancel_Div_Mod_Nat.proc\<close>
wenzelm@60758: 
wenzelm@60758: 
wenzelm@60758: subsubsection \<open>Quotient\<close>
haftmann@26100: 
haftmann@26100: lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
nipkow@29667: by (simp add: le_div_geq linorder_not_less)
haftmann@26100: 
haftmann@26100: lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
nipkow@29667: by (simp add: div_geq)
haftmann@26100: 
haftmann@26100: lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
nipkow@29667: by simp
haftmann@26100: 
haftmann@26100: lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
nipkow@29667: by simp
haftmann@26100: 
haftmann@53066: lemma div_positive:
haftmann@53066:   fixes m n :: nat
haftmann@53066:   assumes "n > 0"
haftmann@53066:   assumes "m \<ge> n"
haftmann@53066:   shows "m div n > 0"
haftmann@53066: proof -
wenzelm@60758:   from \<open>m \<ge> n\<close> obtain q where "m = n + q"
haftmann@53066:     by (auto simp add: le_iff_add)
wenzelm@60758:   with \<open>n > 0\<close> show ?thesis by simp
haftmann@53066: qed
haftmann@53066: 
hoelzl@59000: lemma div_eq_0_iff: "(a div b::nat) = 0 \<longleftrightarrow> a < b \<or> b = 0"
hoelzl@59000:   by (metis div_less div_positive div_by_0 gr0I less_numeral_extra(3) not_less)
haftmann@25942: 
wenzelm@60758: subsubsection \<open>Remainder\<close>
haftmann@25942: 
haftmann@26100: lemma mod_less_divisor [simp]:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   assumes "n > 0"
haftmann@26100:   shows "m mod n < (n::nat)"
haftmann@33340:   using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
paulson@14267: 
haftmann@51173: lemma mod_Suc_le_divisor [simp]:
haftmann@51173:   "m mod Suc n \<le> n"
haftmann@51173:   using mod_less_divisor [of "Suc n" m] by arith
haftmann@51173: 
haftmann@26100: lemma mod_less_eq_dividend [simp]:
haftmann@26100:   fixes m n :: nat
haftmann@26100:   shows "m mod n \<le> m"
haftmann@26100: proof (rule add_leD2)
haftmann@26100:   from mod_div_equality have "m div n * n + m mod n = m" .
haftmann@26100:   then show "m div n * n + m mod n \<le> m" by auto
haftmann@26100: qed
haftmann@26100: 
wenzelm@61076: lemma mod_geq: "\<not> m < (n::nat) \<Longrightarrow> m mod n = (m - n) mod n"
nipkow@29667: by (simp add: le_mod_geq linorder_not_less)
paulson@14267: 
wenzelm@61076: lemma mod_if: "m mod (n::nat) = (if m < n then m else (m - n) mod n)"
nipkow@29667: by (simp add: le_mod_geq)
haftmann@26100: 
paulson@14267: lemma mod_1 [simp]: "m mod Suc 0 = 0"
nipkow@29667: by (induct m) (simp_all add: mod_geq)
paulson@14267: 
paulson@14267: (* a simple rearrangement of mod_div_equality: *)
paulson@14267: lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
huffman@47138:   using mod_div_equality2 [of n m] by arith
paulson@14267: 
nipkow@15439: lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718:   apply (drule mod_less_divisor [where m = m])
wenzelm@22718:   apply simp
wenzelm@22718:   done
paulson@14267: 
wenzelm@60758: subsubsection \<open>Quotient and Remainder\<close>
paulson@14267: 
haftmann@33340: lemma divmod_nat_rel_mult1_eq:
bulwahn@46552:   "divmod_nat_rel b c (q, r)
haftmann@33340:    \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
haftmann@33340: by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267: 
haftmann@30923: lemma div_mult1_eq:
haftmann@30923:   "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
huffman@47135: by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267: 
haftmann@33340: lemma divmod_nat_rel_add1_eq:
bulwahn@46552:   "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br)
haftmann@33340:    \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
haftmann@33340: by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267: 
paulson@14267: (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267: lemma div_add1_eq:
nipkow@25134:   "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
huffman@47135: by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267: 
haftmann@33340: lemma divmod_nat_rel_mult2_eq:
haftmann@60352:   assumes "divmod_nat_rel a b (q, r)"
haftmann@60352:   shows "divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
haftmann@60352: proof -
lp15@60562:   { assume "r < b" and "0 < c"
haftmann@60352:     then have "b * (q mod c) + r < b * c"
haftmann@60352:       apply (cut_tac m = q and n = c in mod_less_divisor)
haftmann@60352:       apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
haftmann@60352:       apply (erule_tac P = "%x. lhs < rhs x" for lhs rhs in ssubst)
haftmann@60352:       apply (simp add: add_mult_distrib2)
haftmann@60352:       done
haftmann@60352:     then have "r + b * (q mod c) < b * c"
haftmann@60352:       by (simp add: ac_simps)
haftmann@60352:   } with assms show ?thesis
haftmann@60352:     by (auto simp add: divmod_nat_rel_def algebra_simps add_mult_distrib2 [symmetric])
haftmann@60352: qed
lp15@60562: 
blanchet@55085: lemma div_mult2_eq: "a div (b * c) = (a div b) div (c::nat)"
huffman@47135: by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])
paulson@14267: 
blanchet@55085: lemma mod_mult2_eq: "a mod (b * c) = b * (a div b mod c) + a mod (b::nat)"
haftmann@57512: by (auto simp add: mult.commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])
paulson@14267: 
haftmann@61275: instantiation nat :: semiring_numeral_div
haftmann@61275: begin
haftmann@61275: 
haftmann@61275: definition divmod_nat :: "num \<Rightarrow> num \<Rightarrow> nat \<times> nat"
haftmann@61275: where
haftmann@61275:   divmod'_nat_def: "divmod_nat m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@61275: 
haftmann@61275: definition divmod_step_nat :: "num \<Rightarrow> nat \<times> nat \<Rightarrow> nat \<times> nat"
haftmann@61275: where
haftmann@61275:   "divmod_step_nat l qr = (let (q, r) = qr
haftmann@61275:     in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275:     else (2 * q, r))"
haftmann@61275: 
haftmann@61275: instance
haftmann@61275:   by standard (auto intro: div_positive simp add: divmod'_nat_def divmod_step_nat_def mod_mult2_eq div_mult2_eq)
haftmann@61275: 
haftmann@61275: end
haftmann@61275: 
haftmann@61275: declare divmod_algorithm_code [where ?'a = nat, code]
haftmann@61275:   
paulson@14267: 
wenzelm@60758: subsubsection \<open>Further Facts about Quotient and Remainder\<close>
paulson@14267: 
haftmann@58786: lemma div_1 [simp]:
haftmann@58786:   "m div Suc 0 = m"
haftmann@58786:   using div_by_1 [of m] by simp
paulson@14267: 
paulson@14267: (* Monotonicity of div in first argument *)
haftmann@30923: lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718:     "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267: apply (case_tac "k=0", simp)
paulson@15251: apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267: apply (case_tac "n<k")
paulson@14267: (* 1  case n<k *)
paulson@14267: apply simp
paulson@14267: (* 2  case n >= k *)
paulson@14267: apply (case_tac "m<k")
paulson@14267: (* 2.1  case m<k *)
paulson@14267: apply simp
paulson@14267: (* 2.2  case m>=k *)
nipkow@15439: apply (simp add: div_geq diff_le_mono)
paulson@14267: done
paulson@14267: 
paulson@14267: (* Antimonotonicity of div in second argument *)
paulson@14267: lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267: apply (subgoal_tac "0<n")
wenzelm@22718:  prefer 2 apply simp
paulson@15251: apply (induct_tac k rule: nat_less_induct)
paulson@14267: apply (rename_tac "k")
paulson@14267: apply (case_tac "k<n", simp)
paulson@14267: apply (subgoal_tac "~ (k<m) ")
wenzelm@22718:  prefer 2 apply simp
paulson@14267: apply (simp add: div_geq)
paulson@15251: apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267:  prefer 2
paulson@14267:  apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267: apply (rule le_trans, simp)
nipkow@15439: apply (simp)
paulson@14267: done
paulson@14267: 
paulson@14267: lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267: apply (case_tac "n=0", simp)
paulson@14267: apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267: apply (rule div_le_mono2)
paulson@14267: apply (simp_all (no_asm_simp))
paulson@14267: done
paulson@14267: 
wenzelm@22718: (* Similar for "less than" *)
huffman@47138: lemma div_less_dividend [simp]:
huffman@47138:   "\<lbrakk>(1::nat) < n; 0 < m\<rbrakk> \<Longrightarrow> m div n < m"
huffman@47138: apply (induct m rule: nat_less_induct)
paulson@14267: apply (rename_tac "m")
paulson@14267: apply (case_tac "m<n", simp)
paulson@14267: apply (subgoal_tac "0<n")
wenzelm@22718:  prefer 2 apply simp
paulson@14267: apply (simp add: div_geq)
paulson@14267: apply (case_tac "n<m")
paulson@15251:  apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267:   apply (rule impI less_trans_Suc)+
paulson@14267: apply assumption
nipkow@15439:   apply (simp_all)
paulson@14267: done
paulson@14267: 
wenzelm@60758: text\<open>A fact for the mutilated chess board\<close>
paulson@14267: lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267: apply (case_tac "n=0", simp)
paulson@15251: apply (induct "m" rule: nat_less_induct)
paulson@14267: apply (case_tac "Suc (na) <n")
paulson@14267: (* case Suc(na) < n *)
paulson@14267: apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267: (* case n \<le> Suc(na) *)
paulson@16796: apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439: apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267: done
paulson@14267: 
paulson@14267: lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
nipkow@29667: by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084: 
wenzelm@22718: lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267: 
paulson@14267: (*Loses information, namely we also have r<d provided d is nonzero*)
haftmann@57514: lemma mod_eqD:
haftmann@57514:   fixes m d r q :: nat
haftmann@57514:   assumes "m mod d = r"
haftmann@57514:   shows "\<exists>q. m = r + q * d"
haftmann@57514: proof -
haftmann@57514:   from mod_div_equality obtain q where "q * d + m mod d = m" by blast
haftmann@57514:   with assms have "m = r + q * d" by simp
haftmann@57514:   then show ?thesis ..
haftmann@57514: qed
paulson@14267: 
nipkow@13152: lemma split_div:
nipkow@13189:  "P(n div k :: nat) =
nipkow@13189:  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189:  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189: proof
nipkow@13189:   assume P: ?P
nipkow@13189:   show ?Q
nipkow@13189:   proof (cases)
nipkow@13189:     assume "k = 0"
haftmann@27651:     with P show ?Q by simp
nipkow@13189:   next
nipkow@13189:     assume not0: "k \<noteq> 0"
nipkow@13189:     thus ?Q
nipkow@13189:     proof (simp, intro allI impI)
nipkow@13189:       fix i j
nipkow@13189:       assume n: "n = k*i + j" and j: "j < k"
nipkow@13189:       show "P i"
nipkow@13189:       proof (cases)
wenzelm@22718:         assume "i = 0"
wenzelm@22718:         with n j P show "P i" by simp
nipkow@13189:       next
wenzelm@22718:         assume "i \<noteq> 0"
haftmann@57514:         with not0 n j P show "P i" by(simp add:ac_simps)
nipkow@13189:       qed
nipkow@13189:     qed
nipkow@13189:   qed
nipkow@13189: next
nipkow@13189:   assume Q: ?Q
nipkow@13189:   show ?P
nipkow@13189:   proof (cases)
nipkow@13189:     assume "k = 0"
haftmann@27651:     with Q show ?P by simp
nipkow@13189:   next
nipkow@13189:     assume not0: "k \<noteq> 0"
nipkow@13189:     with Q have R: ?R by simp
nipkow@13189:     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517:     show ?P by simp
nipkow@13189:   qed
nipkow@13189: qed
nipkow@13189: 
berghofe@13882: lemma split_div_lemma:
haftmann@26100:   assumes "0 < n"
wenzelm@61076:   shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m::nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@26100: proof
haftmann@26100:   assume ?rhs
haftmann@26100:   with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
haftmann@26100:   then have A: "n * q \<le> m" by simp
haftmann@26100:   have "n - (m mod n) > 0" using mod_less_divisor assms by auto
haftmann@26100:   then have "m < m + (n - (m mod n))" by simp
haftmann@26100:   then have "m < n + (m - (m mod n))" by simp
haftmann@26100:   with nq have "m < n + n * q" by simp
haftmann@26100:   then have B: "m < n * Suc q" by simp
haftmann@26100:   from A B show ?lhs ..
haftmann@26100: next
haftmann@26100:   assume P: ?lhs
haftmann@33340:   then have "divmod_nat_rel m n (q, m - n * q)"
haftmann@57514:     unfolding divmod_nat_rel_def by (auto simp add: ac_simps)
haftmann@61433:   then have "m div n = q"
haftmann@61433:     by (rule div_nat_unique)
haftmann@30923:   then show ?rhs by simp
haftmann@26100: qed
berghofe@13882: 
berghofe@13882: theorem split_div':
berghofe@13882:   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267:    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
haftmann@61433:   apply (cases "0 < n")
berghofe@13882:   apply (simp only: add: split_div_lemma)
haftmann@27651:   apply simp_all
berghofe@13882:   done
berghofe@13882: 
nipkow@13189: lemma split_mod:
nipkow@13189:  "P(n mod k :: nat) =
nipkow@13189:  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189:  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189: proof
nipkow@13189:   assume P: ?P
nipkow@13189:   show ?Q
nipkow@13189:   proof (cases)
nipkow@13189:     assume "k = 0"
haftmann@27651:     with P show ?Q by simp
nipkow@13189:   next
nipkow@13189:     assume not0: "k \<noteq> 0"
nipkow@13189:     thus ?Q
nipkow@13189:     proof (simp, intro allI impI)
nipkow@13189:       fix i j
nipkow@13189:       assume "n = k*i + j" "j < k"
haftmann@58786:       thus "P j" using not0 P by (simp add: ac_simps)
nipkow@13189:     qed
nipkow@13189:   qed
nipkow@13189: next
nipkow@13189:   assume Q: ?Q
nipkow@13189:   show ?P
nipkow@13189:   proof (cases)
nipkow@13189:     assume "k = 0"
haftmann@27651:     with Q show ?P by simp
nipkow@13189:   next
nipkow@13189:     assume not0: "k \<noteq> 0"
nipkow@13189:     with Q have R: ?R by simp
nipkow@13189:     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517:     show ?P by simp
nipkow@13189:   qed
nipkow@13189: qed
nipkow@13189: 
haftmann@60868: theorem mod_div_equality' [nitpick_unfold]: "(m::nat) mod n = m - (m div n) * n"
huffman@47138:   using mod_div_equality [of m n] by arith
huffman@47138: 
huffman@47138: lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"
huffman@47138:   using mod_div_equality [of m n] by arith
huffman@47138: (* FIXME: very similar to mult_div_cancel *)
haftmann@22800: 
noschinl@52398: lemma div_eq_dividend_iff: "a \<noteq> 0 \<Longrightarrow> (a :: nat) div b = a \<longleftrightarrow> b = 1"
noschinl@52398:   apply rule
noschinl@52398:   apply (cases "b = 0")
noschinl@52398:   apply simp_all
noschinl@52398:   apply (metis (full_types) One_nat_def Suc_lessI div_less_dividend less_not_refl3)
noschinl@52398:   done
noschinl@52398: 
haftmann@22800: 
wenzelm@60758: subsubsection \<open>An ``induction'' law for modulus arithmetic.\<close>
paulson@14640: 
paulson@14640: lemma mod_induct_0:
paulson@14640:   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640:   and base: "P i" and i: "i<p"
paulson@14640:   shows "P 0"
paulson@14640: proof (rule ccontr)
paulson@14640:   assume contra: "\<not>(P 0)"
paulson@14640:   from i have p: "0<p" by simp
paulson@14640:   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640:   proof
paulson@14640:     fix k
paulson@14640:     show "?A k"
paulson@14640:     proof (induct k)
wenzelm@61799:       show "?A 0" by simp  \<comment> "by contradiction"
paulson@14640:     next
paulson@14640:       fix n
paulson@14640:       assume ih: "?A n"
paulson@14640:       show "?A (Suc n)"
paulson@14640:       proof (clarsimp)
wenzelm@22718:         assume y: "P (p - Suc n)"
wenzelm@22718:         have n: "Suc n < p"
wenzelm@22718:         proof (rule ccontr)
wenzelm@22718:           assume "\<not>(Suc n < p)"
wenzelm@22718:           hence "p - Suc n = 0"
wenzelm@22718:             by simp
wenzelm@22718:           with y contra show "False"
wenzelm@22718:             by simp
wenzelm@22718:         qed
wenzelm@22718:         hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718:         from p have "p - Suc n < p" by arith
wenzelm@22718:         with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718:           by blast
wenzelm@22718:         show "False"
wenzelm@22718:         proof (cases "n=0")
wenzelm@22718:           case True
wenzelm@22718:           with z n2 contra show ?thesis by simp
wenzelm@22718:         next
wenzelm@22718:           case False
wenzelm@22718:           with p have "p-n < p" by arith
wenzelm@22718:           with z n2 False ih show ?thesis by simp
wenzelm@22718:         qed
paulson@14640:       qed
paulson@14640:     qed
paulson@14640:   qed
paulson@14640:   moreover
paulson@14640:   from i obtain k where "0<k \<and> i+k=p"
paulson@14640:     by (blast dest: less_imp_add_positive)
paulson@14640:   hence "0<k \<and> i=p-k" by auto
paulson@14640:   moreover
paulson@14640:   note base
paulson@14640:   ultimately
paulson@14640:   show "False" by blast
paulson@14640: qed
paulson@14640: 
paulson@14640: lemma mod_induct:
paulson@14640:   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640:   and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640:   shows "P j"
paulson@14640: proof -
paulson@14640:   have "\<forall>j<p. P j"
paulson@14640:   proof
paulson@14640:     fix j
paulson@14640:     show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640:     proof (induct j)
paulson@14640:       from step base i show "?A 0"
wenzelm@22718:         by (auto elim: mod_induct_0)
paulson@14640:     next
paulson@14640:       fix k
paulson@14640:       assume ih: "?A k"
paulson@14640:       show "?A (Suc k)"
paulson@14640:       proof
wenzelm@22718:         assume suc: "Suc k < p"
wenzelm@22718:         hence k: "k<p" by simp
wenzelm@22718:         with ih have "P k" ..
wenzelm@22718:         with step k have "P (Suc k mod p)"
wenzelm@22718:           by blast
wenzelm@22718:         moreover
wenzelm@22718:         from suc have "Suc k mod p = Suc k"
wenzelm@22718:           by simp
wenzelm@22718:         ultimately
wenzelm@22718:         show "P (Suc k)" by simp
paulson@14640:       qed
paulson@14640:     qed
paulson@14640:   qed
paulson@14640:   with j show ?thesis by blast
paulson@14640: qed
paulson@14640: 
haftmann@33296: lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
huffman@47138:   by (simp add: numeral_2_eq_2 le_div_geq)
huffman@47138: 
huffman@47138: lemma mod2_Suc_Suc [simp]: "Suc (Suc m) mod 2 = m mod 2"
huffman@47138:   by (simp add: numeral_2_eq_2 le_mod_geq)
haftmann@33296: 
haftmann@33296: lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
huffman@47217: by (simp add: mult_2 [symmetric])
haftmann@33296: 
wenzelm@61076: lemma mod2_gr_0 [simp]: "0 < (m::nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
haftmann@33296: proof -
boehmes@35815:   { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (cases n) simp_all }
haftmann@33296:   moreover have "m mod 2 < 2" by simp
haftmann@33296:   ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
haftmann@33296:   then show ?thesis by auto
haftmann@33296: qed
haftmann@33296: 
wenzelm@60758: text\<open>These lemmas collapse some needless occurrences of Suc:
haftmann@33296:     at least three Sucs, since two and fewer are rewritten back to Suc again!
wenzelm@60758:     We already have some rules to simplify operands smaller than 3.\<close>
haftmann@33296: 
haftmann@33296: lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
haftmann@33296: by (simp add: Suc3_eq_add_3)
haftmann@33296: 
haftmann@33296: lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
haftmann@33296: by (simp add: Suc3_eq_add_3)
haftmann@33296: 
haftmann@33296: lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
haftmann@33296: by (simp add: Suc3_eq_add_3)
haftmann@33296: 
haftmann@33296: lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
haftmann@33296: by (simp add: Suc3_eq_add_3)
haftmann@33296: 
huffman@47108: lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
huffman@47108: lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
haftmann@33296: 
lp15@60562: lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1"
haftmann@33361: apply (induct "m")
haftmann@33361: apply (simp_all add: mod_Suc)
haftmann@33361: done
haftmann@33361: 
huffman@47108: declare Suc_times_mod_eq [of "numeral w", simp] for w
haftmann@33361: 
haftmann@60868: lemma mod_greater_zero_iff_not_dvd:
haftmann@60868:   fixes m n :: nat
haftmann@60868:   shows "m mod n > 0 \<longleftrightarrow> \<not> n dvd m"
haftmann@60868:   by (simp add: dvd_eq_mod_eq_0)
haftmann@60868: 
huffman@47138: lemma Suc_div_le_mono [simp]: "n div k \<le> (Suc n) div k"
huffman@47138: by (simp add: div_le_mono)
haftmann@33361: 
haftmann@33361: lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
haftmann@33361: by (cases n) simp_all
haftmann@33361: 
boehmes@35815: lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"
boehmes@35815: proof -
boehmes@35815:   from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all
lp15@60562:   from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp
boehmes@35815: qed
haftmann@33361: 
haftmann@33361: lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
haftmann@33361: proof -
haftmann@33361:   have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
lp15@60562:   also have "... = Suc m mod n" by (rule mod_mult_self3)
haftmann@33361:   finally show ?thesis .
haftmann@33361: qed
haftmann@33361: 
haftmann@33361: lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
lp15@60562: apply (subst mod_Suc [of m])
lp15@60562: apply (subst mod_Suc [of "m mod n"], simp)
haftmann@33361: done
haftmann@33361: 
huffman@47108: lemma mod_2_not_eq_zero_eq_one_nat:
huffman@47108:   fixes n :: nat
huffman@47108:   shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
haftmann@58786:   by (fact not_mod_2_eq_0_eq_1)
lp15@60562: 
haftmann@58778: lemma even_Suc_div_two [simp]:
haftmann@58778:   "even n \<Longrightarrow> Suc n div 2 = n div 2"
haftmann@58778:   using even_succ_div_two [of n] by simp
lp15@60562: 
haftmann@58778: lemma odd_Suc_div_two [simp]:
haftmann@58778:   "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
haftmann@58778:   using odd_succ_div_two [of n] by simp
haftmann@58778: 
haftmann@58834: lemma odd_two_times_div_two_nat [simp]:
haftmann@60352:   assumes "odd n"
haftmann@60352:   shows "2 * (n div 2) = n - (1 :: nat)"
haftmann@60352: proof -
haftmann@60352:   from assms have "2 * (n div 2) + 1 = n"
haftmann@60352:     by (rule odd_two_times_div_two_succ)
haftmann@60352:   then have "Suc (2 * (n div 2)) - 1 = n - 1"
haftmann@60352:     by simp
haftmann@60352:   then show ?thesis
haftmann@60352:     by simp
haftmann@60352: qed
haftmann@58778: 
haftmann@58778: lemma parity_induct [case_names zero even odd]:
haftmann@58778:   assumes zero: "P 0"
haftmann@58778:   assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
haftmann@58778:   assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
haftmann@58778:   shows "P n"
haftmann@58778: proof (induct n rule: less_induct)
haftmann@58778:   case (less n)
haftmann@58778:   show "P n"
haftmann@58778:   proof (cases "n = 0")
haftmann@58778:     case True with zero show ?thesis by simp
haftmann@58778:   next
haftmann@58778:     case False
haftmann@58778:     with less have hyp: "P (n div 2)" by simp
haftmann@58778:     show ?thesis
haftmann@58778:     proof (cases "even n")
haftmann@58778:       case True
haftmann@58778:       with hyp even [of "n div 2"] show ?thesis
haftmann@58834:         by simp
haftmann@58778:     next
haftmann@58778:       case False
lp15@60562:       with hyp odd [of "n div 2"] show ?thesis
haftmann@58834:         by simp
haftmann@58778:     qed
haftmann@58778:   qed
haftmann@58778: qed
haftmann@58778: 
haftmann@60868: lemma Suc_0_div_numeral [simp]:
haftmann@60868:   fixes k l :: num
haftmann@60868:   shows "Suc 0 div numeral k = fst (divmod Num.One k)"
haftmann@60868:   by (simp_all add: fst_divmod)
haftmann@60868: 
haftmann@60868: lemma Suc_0_mod_numeral [simp]:
haftmann@60868:   fixes k l :: num
haftmann@60868:   shows "Suc 0 mod numeral k = snd (divmod Num.One k)"
haftmann@60868:   by (simp_all add: snd_divmod)
haftmann@60868: 
haftmann@33361: 
wenzelm@60758: subsection \<open>Division on @{typ int}\<close>
haftmann@33361: 
wenzelm@61799: definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" \<comment> \<open>definition of quotient and remainder\<close>
haftmann@60868:   where "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
huffman@47139:     (if 0 < b then 0 \<le> r \<and> r < b else if b < 0 then b < r \<and> r \<le> 0 else q = 0))"
haftmann@33361: 
haftmann@33361: lemma unique_quotient_lemma:
haftmann@60868:   "b * q' + r' \<le> b * q + r \<Longrightarrow> 0 \<le> r' \<Longrightarrow> r' < b \<Longrightarrow> r < b \<Longrightarrow> q' \<le> (q::int)"
haftmann@33361: apply (subgoal_tac "r' + b * (q'-q) \<le> r")
haftmann@33361:  prefer 2 apply (simp add: right_diff_distrib)
haftmann@33361: apply (subgoal_tac "0 < b * (1 + q - q') ")
haftmann@33361: apply (erule_tac [2] order_le_less_trans)
webertj@49962:  prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361: apply (subgoal_tac "b * q' < b * (1 + q) ")
webertj@49962:  prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361: apply (simp add: mult_less_cancel_left)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma unique_quotient_lemma_neg:
haftmann@60868:   "b * q' + r' \<le> b*q + r \<Longrightarrow> r \<le> 0 \<Longrightarrow> b < r \<Longrightarrow> b < r' \<Longrightarrow> q \<le> (q'::int)"
haftmann@60868:   by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma) auto
haftmann@33361: 
haftmann@33361: lemma unique_quotient:
haftmann@60868:   "divmod_int_rel a b (q, r) \<Longrightarrow> divmod_int_rel a b (q', r') \<Longrightarrow> q = q'"
haftmann@33361: apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
haftmann@33361: apply (blast intro: order_antisym
lp15@60562:              dest: order_eq_refl [THEN unique_quotient_lemma]
haftmann@33361:              order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma unique_remainder:
haftmann@60868:   "divmod_int_rel a b (q, r) \<Longrightarrow> divmod_int_rel a b (q', r') \<Longrightarrow> r = r'"
haftmann@33361: apply (subgoal_tac "q = q'")
haftmann@33361:  apply (simp add: divmod_int_rel_def)
haftmann@33361: apply (blast intro: unique_quotient)
haftmann@33361: done
haftmann@33361: 
haftmann@60868: instantiation int :: Divides.div
haftmann@60868: begin
haftmann@60868: 
haftmann@60868: definition divide_int
haftmann@60868:   where "k div l = (if l = 0 \<or> k = 0 then 0
haftmann@60868:     else if k > 0 \<and> l > 0 \<or> k < 0 \<and> l < 0
haftmann@60868:       then int (nat \<bar>k\<bar> div nat \<bar>l\<bar>)
haftmann@60868:       else
haftmann@60868:         if l dvd k then - int (nat \<bar>k\<bar> div nat \<bar>l\<bar>)
haftmann@60868:         else - int (Suc (nat \<bar>k\<bar> div nat \<bar>l\<bar>)))"
haftmann@60868: 
haftmann@60868: definition mod_int
haftmann@60868:   where "k mod l = (if l = 0 then k else if l dvd k then 0
haftmann@60868:     else if k > 0 \<and> l > 0 \<or> k < 0 \<and> l < 0
haftmann@60868:       then sgn l * int (nat \<bar>k\<bar> mod nat \<bar>l\<bar>)
haftmann@60868:       else sgn l * (\<bar>l\<bar> - int (nat \<bar>k\<bar> mod nat \<bar>l\<bar>)))"
haftmann@60868: 
haftmann@60868: instance ..      
haftmann@60868: 
haftmann@60868: end
haftmann@60868: 
haftmann@60868: lemma divmod_int_rel:
haftmann@60868:   "divmod_int_rel k l (k div l, k mod l)"
lp15@61649:   unfolding divmod_int_rel_def divide_int_def mod_int_def
haftmann@60868:   apply (cases k rule: int_cases3)
lp15@61649:   apply (simp add: mod_greater_zero_iff_not_dvd not_le algebra_simps)
haftmann@60868:   apply (cases l rule: int_cases3)
lp15@61649:   apply (simp add: mod_greater_zero_iff_not_dvd not_le algebra_simps)
lp15@61649:   apply (simp_all del: of_nat_add of_nat_mult add: mod_greater_zero_iff_not_dvd not_le algebra_simps int_dvd_iff of_nat_add [symmetric] of_nat_mult [symmetric])
haftmann@60868:   apply (cases l rule: int_cases3)
lp15@61649:   apply (simp_all del: of_nat_add of_nat_mult add: not_le algebra_simps int_dvd_iff of_nat_add [symmetric] of_nat_mult [symmetric])
wenzelm@41550:   done
haftmann@33361: 
haftmann@60868: instantiation int :: ring_div
haftmann@60868: begin
haftmann@60868: 
haftmann@60868: subsubsection \<open>Uniqueness and Monotonicity of Quotients and Remainders\<close>
haftmann@33361: 
huffman@47141: lemma divmod_int_unique:
haftmann@60868:   assumes "divmod_int_rel k l (q, r)"
haftmann@60868:   shows div_int_unique: "k div l = q" and mod_int_unique: "k mod l = r"
haftmann@60868:   using assms divmod_int_rel [of k l]
haftmann@60868:   using unique_quotient [of k l] unique_remainder [of k l]
haftmann@60868:   by auto
haftmann@60868:   
haftmann@60429: instance
huffman@47141: proof
huffman@47141:   fix a b :: int
huffman@47141:   show "a div b * b + a mod b = a"
haftmann@60868:     using divmod_int_rel [of a b]
haftmann@57512:     unfolding divmod_int_rel_def by (simp add: mult.commute)
huffman@47141: next
huffman@47141:   fix a b c :: int
huffman@47141:   assume "b \<noteq> 0"
huffman@47141:   hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"
haftmann@60868:     using divmod_int_rel [of a b]
huffman@47141:     unfolding divmod_int_rel_def by (auto simp: algebra_simps)
huffman@47141:   thus "(a + c * b) div b = c + a div b"
huffman@47141:     by (rule div_int_unique)
huffman@47141: next
huffman@47141:   fix a b c :: int
huffman@47141:   assume "c \<noteq> 0"
huffman@47141:   hence "\<And>q r. divmod_int_rel a b (q, r)
huffman@47141:     \<Longrightarrow> divmod_int_rel (c * a) (c * b) (q, c * r)"
huffman@47141:     unfolding divmod_int_rel_def
huffman@47141:     by - (rule linorder_cases [of 0 b], auto simp: algebra_simps
huffman@47141:       mult_less_0_iff zero_less_mult_iff mult_strict_right_mono
huffman@47141:       mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)
huffman@47141:   hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"
haftmann@60868:     using divmod_int_rel [of a b] .
huffman@47141:   thus "(c * a) div (c * b) = a div b"
huffman@47141:     by (rule div_int_unique)
huffman@47141: next
huffman@47141:   fix a :: int show "a div 0 = 0"
huffman@47141:     by (rule div_int_unique, simp add: divmod_int_rel_def)
huffman@47141: next
huffman@47141:   fix a :: int show "0 div a = 0"
huffman@47141:     by (rule div_int_unique, auto simp add: divmod_int_rel_def)
huffman@47141: qed
huffman@47141: 
haftmann@60429: end
haftmann@60429: 
haftmann@60517: lemma is_unit_int:
haftmann@60517:   "is_unit (k::int) \<longleftrightarrow> k = 1 \<or> k = - 1"
haftmann@60517:   by auto
haftmann@60517: 
haftmann@60685: instantiation int :: normalization_semidom
haftmann@60685: begin
haftmann@60685: 
haftmann@60685: definition normalize_int
haftmann@60685:   where [simp]: "normalize = (abs :: int \<Rightarrow> int)"
haftmann@60685: 
haftmann@60685: definition unit_factor_int
haftmann@60685:   where [simp]: "unit_factor = (sgn :: int \<Rightarrow> int)"
haftmann@60685: 
haftmann@60685: instance
haftmann@60685: proof
haftmann@60685:   fix k :: int
haftmann@60685:   assume "k \<noteq> 0"
haftmann@60685:   then have "\<bar>sgn k\<bar> = 1"
haftmann@60685:     by (cases "0::int" k rule: linorder_cases) simp_all
haftmann@60685:   then show "is_unit (unit_factor k)"
haftmann@60685:     by simp
haftmann@60685: qed (simp_all add: sgn_times mult_sgn_abs)
haftmann@60685:   
haftmann@60685: end
haftmann@60685:   
wenzelm@60758: text\<open>Basic laws about division and remainder\<close>
haftmann@33361: 
haftmann@33361: lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
huffman@47141:   by (fact mod_div_equality2 [symmetric])
haftmann@33361: 
haftmann@60868: lemma zdiv_int: "int (a div b) = int a div int b"
haftmann@60868:   by (simp add: divide_int_def)
haftmann@60868: 
haftmann@60868: lemma zmod_int: "int (a mod b) = int a mod int b"
haftmann@60868:   by (simp add: mod_int_def int_dvd_iff)
haftmann@60868:   
wenzelm@60758: text \<open>Tool setup\<close>
wenzelm@60758: 
wenzelm@60758: ML \<open>
wenzelm@43594: structure Cancel_Div_Mod_Int = Cancel_Div_Mod
wenzelm@41550: (
haftmann@60352:   val div_name = @{const_name Rings.divide};
haftmann@33361:   val mod_name = @{const_name mod};
haftmann@33361:   val mk_binop = HOLogic.mk_binop;
haftmann@33361:   val mk_sum = Arith_Data.mk_sum HOLogic.intT;
haftmann@33361:   val dest_sum = Arith_Data.dest_sum;
haftmann@33361: 
huffman@47165:   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
haftmann@33361: 
lp15@60562:   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
haftmann@59556:     (@{thm diff_conv_add_uminus} :: @{thms add_0_left add_0_right} @ @{thms ac_simps}))
wenzelm@41550: )
wenzelm@60758: \<close>
wenzelm@60758: 
wenzelm@60758: simproc_setup cancel_div_mod_int ("(k::int) + l") = \<open>K Cancel_Div_Mod_Int.proc\<close>
wenzelm@43594: 
huffman@47141: lemma pos_mod_conj: "(0::int) < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b"
haftmann@60868:   using divmod_int_rel [of a b]
huffman@47141:   by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361: 
wenzelm@45607: lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]
wenzelm@45607:    and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]
haftmann@33361: 
huffman@47141: lemma neg_mod_conj: "b < (0::int) \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b"
haftmann@60868:   using divmod_int_rel [of a b]
huffman@47141:   by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361: 
wenzelm@45607: lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]
wenzelm@45607:    and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]
haftmann@33361: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>General Properties of div and mod\<close>
haftmann@33361: 
haftmann@33361: lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
huffman@47140: apply (rule div_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
huffman@47140: apply (rule div_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
huffman@47140: apply (rule div_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: (*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
haftmann@33361: 
haftmann@33361: lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
huffman@47140: apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
huffman@47140: apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
huffman@47140: apply (rule_tac q = "-1" in mod_int_unique)
haftmann@33361: apply (auto simp add: divmod_int_rel_def)
haftmann@33361: done
haftmann@33361: 
wenzelm@61799: text\<open>There is no \<open>mod_neg_pos_trivial\<close>.\<close>
wenzelm@60758: 
wenzelm@60758: 
wenzelm@60758: subsubsection \<open>Laws for div and mod with Unary Minus\<close>
haftmann@33361: 
haftmann@33361: lemma zminus1_lemma:
huffman@47139:      "divmod_int_rel a b (q, r) ==> b \<noteq> 0
lp15@60562:       ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,
haftmann@33361:                           if r=0 then 0 else b-r)"
haftmann@33361: by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
haftmann@33361: 
haftmann@33361: 
haftmann@33361: lemma zdiv_zminus1_eq_if:
lp15@60562:      "b \<noteq> (0::int)
lp15@60562:       ==> (-a) div b =
haftmann@33361:           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
haftmann@60868: by (blast intro: divmod_int_rel [THEN zminus1_lemma, THEN div_int_unique])
haftmann@33361: 
haftmann@33361: lemma zmod_zminus1_eq_if:
haftmann@33361:      "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
haftmann@33361: apply (case_tac "b = 0", simp)
haftmann@60868: apply (blast intro: divmod_int_rel [THEN zminus1_lemma, THEN mod_int_unique])
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmod_zminus1_not_zero:
haftmann@33361:   fixes k l :: int
haftmann@33361:   shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
haftmann@33361:   unfolding zmod_zminus1_eq_if by auto
haftmann@33361: 
haftmann@33361: lemma zdiv_zminus2_eq_if:
lp15@60562:      "b \<noteq> (0::int)
lp15@60562:       ==> a div (-b) =
haftmann@33361:           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
huffman@47159: by (simp add: zdiv_zminus1_eq_if div_minus_right)
haftmann@33361: 
haftmann@33361: lemma zmod_zminus2_eq_if:
haftmann@33361:      "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
huffman@47159: by (simp add: zmod_zminus1_eq_if mod_minus_right)
haftmann@33361: 
haftmann@33361: lemma zmod_zminus2_not_zero:
haftmann@33361:   fixes k l :: int
haftmann@33361:   shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
lp15@60562:   unfolding zmod_zminus2_eq_if by auto
haftmann@33361: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>Monotonicity in the First Argument (Dividend)\<close>
haftmann@33361: 
haftmann@33361: lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
haftmann@33361: apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361: apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361: apply (rule unique_quotient_lemma)
haftmann@33361: apply (erule subst)
haftmann@33361: apply (erule subst, simp_all)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
haftmann@33361: apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361: apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361: apply (rule unique_quotient_lemma_neg)
haftmann@33361: apply (erule subst)
haftmann@33361: apply (erule subst, simp_all)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>Monotonicity in the Second Argument (Divisor)\<close>
haftmann@33361: 
haftmann@33361: lemma q_pos_lemma:
haftmann@33361:      "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
haftmann@33361: apply (subgoal_tac "0 < b'* (q' + 1) ")
haftmann@33361:  apply (simp add: zero_less_mult_iff)
webertj@49962: apply (simp add: distrib_left)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zdiv_mono2_lemma:
lp15@60562:      "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';
lp15@60562:          r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]
haftmann@33361:       ==> q \<le> (q'::int)"
lp15@60562: apply (frule q_pos_lemma, assumption+)
haftmann@33361: apply (subgoal_tac "b*q < b* (q' + 1) ")
haftmann@33361:  apply (simp add: mult_less_cancel_left)
haftmann@33361: apply (subgoal_tac "b*q = r' - r + b'*q'")
haftmann@33361:  prefer 2 apply simp
webertj@49962: apply (simp (no_asm_simp) add: distrib_left)
haftmann@57512: apply (subst add.commute, rule add_less_le_mono, arith)
haftmann@33361: apply (rule mult_right_mono, auto)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zdiv_mono2:
haftmann@33361:      "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
haftmann@33361: apply (subgoal_tac "b \<noteq> 0")
haftmann@33361:  prefer 2 apply arith
haftmann@33361: apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361: apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361: apply (rule zdiv_mono2_lemma)
haftmann@33361: apply (erule subst)
haftmann@33361: apply (erule subst, simp_all)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma q_neg_lemma:
haftmann@33361:      "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
haftmann@33361: apply (subgoal_tac "b'*q' < 0")
haftmann@33361:  apply (simp add: mult_less_0_iff, arith)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zdiv_mono2_neg_lemma:
lp15@60562:      "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;
lp15@60562:          r < b;  0 \<le> r';  0 < b';  b' \<le> b |]
haftmann@33361:       ==> q' \<le> (q::int)"
lp15@60562: apply (frule q_neg_lemma, assumption+)
haftmann@33361: apply (subgoal_tac "b*q' < b* (q + 1) ")
haftmann@33361:  apply (simp add: mult_less_cancel_left)
webertj@49962: apply (simp add: distrib_left)
haftmann@33361: apply (subgoal_tac "b*q' \<le> b'*q'")
haftmann@33361:  prefer 2 apply (simp add: mult_right_mono_neg, arith)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zdiv_mono2_neg:
haftmann@33361:      "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
haftmann@33361: apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361: apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361: apply (rule zdiv_mono2_neg_lemma)
haftmann@33361: apply (erule subst)
haftmann@33361: apply (erule subst, simp_all)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>More Algebraic Laws for div and mod\<close>
wenzelm@60758: 
wenzelm@60758: text\<open>proving (a*b) div c = a * (b div c) + a * (b mod c)\<close>
haftmann@33361: 
haftmann@33361: lemma zmult1_lemma:
lp15@60562:      "[| divmod_int_rel b c (q, r) |]
haftmann@33361:       ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
haftmann@57514: by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left ac_simps)
haftmann@33361: 
haftmann@33361: lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
haftmann@33361: apply (case_tac "c = 0", simp)
haftmann@60868: apply (blast intro: divmod_int_rel [THEN zmult1_lemma, THEN div_int_unique])
haftmann@33361: done
haftmann@33361: 
wenzelm@60758: text\<open>proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c)\<close>
haftmann@33361: 
haftmann@33361: lemma zadd1_lemma:
lp15@60562:      "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br) |]
haftmann@33361:       ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
webertj@49962: by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left)
haftmann@33361: 
haftmann@33361: (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
haftmann@33361: lemma zdiv_zadd1_eq:
haftmann@33361:      "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
haftmann@33361: apply (case_tac "c = 0", simp)
haftmann@60868: apply (blast intro: zadd1_lemma [OF divmod_int_rel divmod_int_rel] div_int_unique)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
haftmann@33361: by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
haftmann@33361: 
haftmann@33361: (* REVISIT: should this be generalized to all semiring_div types? *)
haftmann@33361: lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
haftmann@33361: 
haftmann@60868: lemma zmod_zdiv_equality' [nitpick_unfold]:
wenzelm@61076:   "(m::int) mod n = m - (m div n) * n"
huffman@47141:   using mod_div_equality [of m n] by arith
huffman@47108: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>Proving  @{term "a div (b * c) = (a div b) div c"}\<close>
haftmann@33361: 
haftmann@33361: (*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
haftmann@33361:   7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
haftmann@33361:   to cause particular problems.*)
haftmann@33361: 
wenzelm@60758: text\<open>first, four lemmas to bound the remainder for the cases b<0 and b>0\<close>
haftmann@33361: 
blanchet@55085: lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b * c < b * (q mod c) + r"
haftmann@33361: apply (subgoal_tac "b * (c - q mod c) < r * 1")
haftmann@33361:  apply (simp add: algebra_simps)
haftmann@33361: apply (rule order_le_less_trans)
haftmann@33361:  apply (erule_tac [2] mult_strict_right_mono)
haftmann@33361:  apply (rule mult_left_mono_neg)
huffman@35216:   using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)
haftmann@33361:  apply (simp)
haftmann@33361: apply (simp)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmult2_lemma_aux2:
haftmann@33361:      "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
haftmann@33361: apply (subgoal_tac "b * (q mod c) \<le> 0")
haftmann@33361:  apply arith
haftmann@33361: apply (simp add: mult_le_0_iff)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
haftmann@33361: apply (subgoal_tac "0 \<le> b * (q mod c) ")
haftmann@33361: apply arith
haftmann@33361: apply (simp add: zero_le_mult_iff)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
haftmann@33361: apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
haftmann@33361:  apply (simp add: right_diff_distrib)
haftmann@33361: apply (rule order_less_le_trans)
haftmann@33361:  apply (erule mult_strict_right_mono)
haftmann@33361:  apply (rule_tac [2] mult_left_mono)
haftmann@33361:   apply simp
huffman@35216:  using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)
haftmann@33361: apply simp
haftmann@33361: done
haftmann@33361: 
lp15@60562: lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]
haftmann@33361:       ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
haftmann@57514: by (auto simp add: mult.assoc divmod_int_rel_def linorder_neq_iff
lp15@60562:                    zero_less_mult_iff distrib_left [symmetric]
huffman@47139:                    zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)
haftmann@33361: 
haftmann@53068: lemma zdiv_zmult2_eq:
haftmann@53068:   fixes a b c :: int
haftmann@53068:   shows "0 \<le> c \<Longrightarrow> a div (b * c) = (a div b) div c"
haftmann@33361: apply (case_tac "b = 0", simp)
haftmann@60868: apply (force simp add: le_less divmod_int_rel [THEN zmult2_lemma, THEN div_int_unique])
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmod_zmult2_eq:
haftmann@53068:   fixes a b c :: int
haftmann@53068:   shows "0 \<le> c \<Longrightarrow> a mod (b * c) = b * (a div b mod c) + a mod b"
haftmann@33361: apply (case_tac "b = 0", simp)
haftmann@60868: apply (force simp add: le_less divmod_int_rel [THEN zmult2_lemma, THEN mod_int_unique])
haftmann@33361: done
haftmann@33361: 
huffman@47108: lemma div_pos_geq:
huffman@47108:   fixes k l :: int
huffman@47108:   assumes "0 < l" and "l \<le> k"
huffman@47108:   shows "k div l = (k - l) div l + 1"
huffman@47108: proof -
huffman@47108:   have "k = (k - l) + l" by simp
huffman@47108:   then obtain j where k: "k = j + l" ..
huffman@47108:   with assms show ?thesis by simp
huffman@47108: qed
huffman@47108: 
huffman@47108: lemma mod_pos_geq:
huffman@47108:   fixes k l :: int
huffman@47108:   assumes "0 < l" and "l \<le> k"
huffman@47108:   shows "k mod l = (k - l) mod l"
huffman@47108: proof -
huffman@47108:   have "k = (k - l) + l" by simp
huffman@47108:   then obtain j where k: "k = j + l" ..
huffman@47108:   with assms show ?thesis by simp
huffman@47108: qed
huffman@47108: 
haftmann@33361: 
wenzelm@60758: subsubsection \<open>Splitting Rules for div and mod\<close>
wenzelm@60758: 
wenzelm@60758: text\<open>The proofs of the two lemmas below are essentially identical\<close>
haftmann@33361: 
haftmann@33361: lemma split_pos_lemma:
lp15@60562:  "0<k ==>
haftmann@33361:     P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
haftmann@33361: apply (rule iffI, clarify)
lp15@60562:  apply (erule_tac P="P x y" for x y in rev_mp)
lp15@60562:  apply (subst mod_add_eq)
lp15@60562:  apply (subst zdiv_zadd1_eq)
lp15@60562:  apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)
wenzelm@60758: txt\<open>converse direction\<close>
lp15@60562: apply (drule_tac x = "n div k" in spec)
haftmann@33361: apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma split_neg_lemma:
haftmann@33361:  "k<0 ==>
haftmann@33361:     P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
haftmann@33361: apply (rule iffI, clarify)
lp15@60562:  apply (erule_tac P="P x y" for x y in rev_mp)
lp15@60562:  apply (subst mod_add_eq)
lp15@60562:  apply (subst zdiv_zadd1_eq)
lp15@60562:  apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)
wenzelm@60758: txt\<open>converse direction\<close>
lp15@60562: apply (drule_tac x = "n div k" in spec)
haftmann@33361: apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma split_zdiv:
haftmann@33361:  "P(n div k :: int) =
lp15@60562:   ((k = 0 --> P 0) &
lp15@60562:    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) &
haftmann@33361:    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
haftmann@33361: apply (case_tac "k=0", simp)
haftmann@33361: apply (simp only: linorder_neq_iff)
lp15@60562: apply (erule disjE)
lp15@60562:  apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"]
haftmann@33361:                       split_neg_lemma [of concl: "%x y. P x"])
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma split_zmod:
haftmann@33361:  "P(n mod k :: int) =
lp15@60562:   ((k = 0 --> P n) &
lp15@60562:    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) &
haftmann@33361:    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
haftmann@33361: apply (case_tac "k=0", simp)
haftmann@33361: apply (simp only: linorder_neq_iff)
lp15@60562: apply (erule disjE)
lp15@60562:  apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"]
haftmann@33361:                       split_neg_lemma [of concl: "%x y. P y"])
haftmann@33361: done
haftmann@33361: 
wenzelm@60758: text \<open>Enable (lin)arith to deal with @{const divide} and @{const mod}
webertj@33730:   when these are applied to some constant that is of the form
wenzelm@60758:   @{term "numeral k"}:\<close>
huffman@47108: declare split_zdiv [of _ _ "numeral k", arith_split] for k
huffman@47108: declare split_zmod [of _ _ "numeral k", arith_split] for k
haftmann@33361: 
haftmann@33361: 
wenzelm@61799: subsubsection \<open>Computing \<open>div\<close> and \<open>mod\<close> with shifting\<close>
huffman@47166: 
huffman@47166: lemma pos_divmod_int_rel_mult_2:
huffman@47166:   assumes "0 \<le> b"
huffman@47166:   assumes "divmod_int_rel a b (q, r)"
huffman@47166:   shows "divmod_int_rel (1 + 2*a) (2*b) (q, 1 + 2*r)"
huffman@47166:   using assms unfolding divmod_int_rel_def by auto
huffman@47166: 
wenzelm@60758: declaration \<open>K (Lin_Arith.add_simps @{thms uminus_numeral_One})\<close>
haftmann@54489: 
huffman@47166: lemma neg_divmod_int_rel_mult_2:
huffman@47166:   assumes "b \<le> 0"
huffman@47166:   assumes "divmod_int_rel (a + 1) b (q, r)"
huffman@47166:   shows "divmod_int_rel (1 + 2*a) (2*b) (q, 2*r - 1)"
huffman@47166:   using assms unfolding divmod_int_rel_def by auto
haftmann@33361: 
wenzelm@60758: text\<open>computing div by shifting\<close>
haftmann@33361: 
haftmann@33361: lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
haftmann@60868:   using pos_divmod_int_rel_mult_2 [OF _ divmod_int_rel]
huffman@47166:   by (rule div_int_unique)
haftmann@33361: 
lp15@60562: lemma neg_zdiv_mult_2:
boehmes@35815:   assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"
haftmann@60868:   using neg_divmod_int_rel_mult_2 [OF A divmod_int_rel]
huffman@47166:   by (rule div_int_unique)
haftmann@33361: 
huffman@47108: (* FIXME: add rules for negative numerals *)
huffman@47108: lemma zdiv_numeral_Bit0 [simp]:
huffman@47108:   "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
huffman@47108:     numeral v div (numeral w :: int)"
huffman@47108:   unfolding numeral.simps unfolding mult_2 [symmetric]
huffman@47108:   by (rule div_mult_mult1, simp)
huffman@47108: 
huffman@47108: lemma zdiv_numeral_Bit1 [simp]:
lp15@60562:   "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =
huffman@47108:     (numeral v div (numeral w :: int))"
huffman@47108:   unfolding numeral.simps
haftmann@57512:   unfolding mult_2 [symmetric] add.commute [of _ 1]
huffman@47108:   by (rule pos_zdiv_mult_2, simp)
haftmann@33361: 
haftmann@33361: lemma pos_zmod_mult_2:
haftmann@33361:   fixes a b :: int
haftmann@33361:   assumes "0 \<le> a"
haftmann@33361:   shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
haftmann@60868:   using pos_divmod_int_rel_mult_2 [OF assms divmod_int_rel]
huffman@47166:   by (rule mod_int_unique)
haftmann@33361: 
haftmann@33361: lemma neg_zmod_mult_2:
haftmann@33361:   fixes a b :: int
haftmann@33361:   assumes "a \<le> 0"
haftmann@33361:   shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
haftmann@60868:   using neg_divmod_int_rel_mult_2 [OF assms divmod_int_rel]
huffman@47166:   by (rule mod_int_unique)
haftmann@33361: 
huffman@47108: (* FIXME: add rules for negative numerals *)
huffman@47108: lemma zmod_numeral_Bit0 [simp]:
lp15@60562:   "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) =
huffman@47108:     (2::int) * (numeral v mod numeral w)"
huffman@47108:   unfolding numeral_Bit0 [of v] numeral_Bit0 [of w]
huffman@47108:   unfolding mult_2 [symmetric] by (rule mod_mult_mult1)
huffman@47108: 
huffman@47108: lemma zmod_numeral_Bit1 [simp]:
huffman@47108:   "numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) =
huffman@47108:     2 * (numeral v mod numeral w) + (1::int)"
huffman@47108:   unfolding numeral_Bit1 [of v] numeral_Bit0 [of w]
haftmann@57512:   unfolding mult_2 [symmetric] add.commute [of _ 1]
huffman@47108:   by (rule pos_zmod_mult_2, simp)
haftmann@33361: 
nipkow@39489: lemma zdiv_eq_0_iff:
nipkow@39489:  "(i::int) div k = 0 \<longleftrightarrow> k=0 \<or> 0\<le>i \<and> i<k \<or> i\<le>0 \<and> k<i" (is "?L = ?R")
nipkow@39489: proof
nipkow@39489:   assume ?L
nipkow@39489:   have "?L \<longrightarrow> ?R" by (rule split_zdiv[THEN iffD2]) simp
wenzelm@60758:   with \<open>?L\<close> show ?R by blast
nipkow@39489: next
nipkow@39489:   assume ?R thus ?L
nipkow@39489:     by(auto simp: div_pos_pos_trivial div_neg_neg_trivial)
nipkow@39489: qed
nipkow@39489: 
nipkow@39489: 
wenzelm@60758: subsubsection \<open>Quotients of Signs\<close>
haftmann@33361: 
haftmann@60868: lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
haftmann@60868: by (simp add: divide_int_def)
haftmann@60868: 
haftmann@60868: lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
haftmann@60868: by (simp add: mod_int_def)
haftmann@60868: 
haftmann@33361: lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
haftmann@33361: apply (subgoal_tac "a div b \<le> -1", force)
haftmann@33361: apply (rule order_trans)
haftmann@33361: apply (rule_tac a' = "-1" in zdiv_mono1)
haftmann@33361: apply (auto simp add: div_eq_minus1)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
haftmann@33361: by (drule zdiv_mono1_neg, auto)
haftmann@33361: 
haftmann@33361: lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
haftmann@33361: by (drule zdiv_mono1, auto)
haftmann@33361: 
wenzelm@61799: text\<open>Now for some equivalences of the form \<open>a div b >=< 0 \<longleftrightarrow> \<dots>\<close>
wenzelm@61799: conditional upon the sign of \<open>a\<close> or \<open>b\<close>. There are many more.
wenzelm@60758: They should all be simp rules unless that causes too much search.\<close>
nipkow@33804: 
haftmann@33361: lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
haftmann@33361: apply auto
haftmann@33361: apply (drule_tac [2] zdiv_mono1)
haftmann@33361: apply (auto simp add: linorder_neq_iff)
haftmann@33361: apply (simp (no_asm_use) add: linorder_not_less [symmetric])
haftmann@33361: apply (blast intro: div_neg_pos_less0)
haftmann@33361: done
haftmann@33361: 
haftmann@60868: lemma pos_imp_zdiv_pos_iff:
haftmann@60868:   "0<k \<Longrightarrow> 0 < (i::int) div k \<longleftrightarrow> k \<le> i"
haftmann@60868: using pos_imp_zdiv_nonneg_iff[of k i] zdiv_eq_0_iff[of i k]
haftmann@60868: by arith
haftmann@60868: 
haftmann@33361: lemma neg_imp_zdiv_nonneg_iff:
nipkow@33804:   "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
huffman@47159: apply (subst div_minus_minus [symmetric])
haftmann@33361: apply (subst pos_imp_zdiv_nonneg_iff, auto)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: (*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
haftmann@33361: lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
haftmann@33361: by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
haftmann@33361: 
haftmann@33361: (*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
haftmann@33361: lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
haftmann@33361: by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
haftmann@33361: 
nipkow@33804: lemma nonneg1_imp_zdiv_pos_iff:
nipkow@33804:   "(0::int) <= a \<Longrightarrow> (a div b > 0) = (a >= b & b>0)"
nipkow@33804: apply rule
nipkow@33804:  apply rule
nipkow@33804:   using div_pos_pos_trivial[of a b]apply arith
nipkow@33804:  apply(cases "b=0")apply simp
nipkow@33804:  using div_nonneg_neg_le0[of a b]apply arith
nipkow@33804: using int_one_le_iff_zero_less[of "a div b"] zdiv_mono1[of b a b]apply simp
nipkow@33804: done
nipkow@33804: 
nipkow@39489: lemma zmod_le_nonneg_dividend: "(m::int) \<ge> 0 ==> m mod k \<le> m"
nipkow@39489: apply (rule split_zmod[THEN iffD2])
nipkow@44890: apply(fastforce dest: q_pos_lemma intro: split_mult_pos_le)
nipkow@39489: done
nipkow@39489: 
haftmann@60868: lemma zmult_div_cancel:
haftmann@60868:   "(n::int) * (m div n) = m - (m mod n)"
haftmann@60868:   using zmod_zdiv_equality [where a="m" and b="n"]
huffman@47142:   by (simp add: algebra_simps) (* FIXME: generalize *)
haftmann@33361: 
haftmann@60868: 
haftmann@60868: subsubsection \<open>Computation of Division and Remainder\<close>
haftmann@60868: 
haftmann@61275: instantiation int :: semiring_numeral_div
haftmann@61275: begin
haftmann@61275: 
haftmann@61275: definition divmod_int :: "num \<Rightarrow> num \<Rightarrow> int \<times> int"
haftmann@61275: where
haftmann@61275:   "divmod_int m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@61275: 
haftmann@61275: definition divmod_step_int :: "num \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int"
haftmann@61275: where
haftmann@61275:   "divmod_step_int l qr = (let (q, r) = qr
haftmann@61275:     in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275:     else (2 * q, r))"
haftmann@61275: 
haftmann@61275: instance
haftmann@61275:   by standard (auto intro: zmod_le_nonneg_dividend simp add: divmod_int_def divmod_step_int_def
haftmann@61275:     pos_imp_zdiv_pos_iff div_pos_pos_trivial mod_pos_pos_trivial zmod_zmult2_eq zdiv_zmult2_eq)
haftmann@61275: 
haftmann@61275: end
haftmann@61275: 
haftmann@61275: declare divmod_algorithm_code [where ?'a = int, code]
lp15@60562: 
haftmann@60930: context
haftmann@60930: begin
haftmann@60930:   
haftmann@60930: qualified definition adjust_div :: "int \<times> int \<Rightarrow> int"
haftmann@60868: where
haftmann@60868:   "adjust_div qr = (let (q, r) = qr in q + of_bool (r \<noteq> 0))"
haftmann@60868: 
haftmann@60930: qualified lemma adjust_div_eq [simp, code]:
haftmann@60868:   "adjust_div (q, r) = q + of_bool (r \<noteq> 0)"
haftmann@60868:   by (simp add: adjust_div_def)
haftmann@60868: 
haftmann@60930: qualified definition adjust_mod :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@60868: where
haftmann@60868:   [simp]: "adjust_mod l r = (if r = 0 then 0 else l - r)"
haftmann@60868: 
haftmann@60868: lemma minus_numeral_div_numeral [simp]:
haftmann@60868:   "- numeral m div numeral n = - (adjust_div (divmod m n) :: int)"
haftmann@60868: proof -
haftmann@60868:   have "int (fst (divmod m n)) = fst (divmod m n)"
haftmann@60868:     by (simp only: fst_divmod divide_int_def) auto
haftmann@60868:   then show ?thesis
haftmann@60868:     by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def)
haftmann@60868: qed
haftmann@60868: 
haftmann@60868: lemma minus_numeral_mod_numeral [simp]:
haftmann@60868:   "- numeral m mod numeral n = adjust_mod (numeral n) (snd (divmod m n) :: int)"
haftmann@60868: proof -
haftmann@60868:   have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)"
haftmann@60868:     using that by (simp only: snd_divmod mod_int_def) auto
haftmann@60868:   then show ?thesis
haftmann@60868:     by (auto simp add: split_def Let_def adjust_div_def divides_aux_def mod_int_def)
haftmann@60868: qed
haftmann@60868: 
haftmann@60868: lemma numeral_div_minus_numeral [simp]:
haftmann@60868:   "numeral m div - numeral n = - (adjust_div (divmod m n) :: int)"
haftmann@60868: proof -
haftmann@60868:   have "int (fst (divmod m n)) = fst (divmod m n)"
haftmann@60868:     by (simp only: fst_divmod divide_int_def) auto
haftmann@60868:   then show ?thesis
haftmann@60868:     by (auto simp add: split_def Let_def adjust_div_def divides_aux_def divide_int_def)
haftmann@60868: qed
haftmann@60868:   
haftmann@60868: lemma numeral_mod_minus_numeral [simp]:
haftmann@60868:   "numeral m mod - numeral n = - adjust_mod (numeral n) (snd (divmod m n) :: int)"
haftmann@60868: proof -
haftmann@60868:   have "int (snd (divmod m n)) = snd (divmod m n)" if "snd (divmod m n) \<noteq> (0::int)"
haftmann@60868:     using that by (simp only: snd_divmod mod_int_def) auto
haftmann@60868:   then show ?thesis
haftmann@60868:     by (auto simp add: split_def Let_def adjust_div_def divides_aux_def mod_int_def)
haftmann@60868: qed
haftmann@60868: 
haftmann@60868: lemma minus_one_div_numeral [simp]:
haftmann@60868:   "- 1 div numeral n = - (adjust_div (divmod Num.One n) :: int)"
haftmann@60868:   using minus_numeral_div_numeral [of Num.One n] by simp  
haftmann@60868: 
haftmann@60868: lemma minus_one_mod_numeral [simp]:
haftmann@60868:   "- 1 mod numeral n = adjust_mod (numeral n) (snd (divmod Num.One n) :: int)"
haftmann@60868:   using minus_numeral_mod_numeral [of Num.One n] by simp
haftmann@60868: 
haftmann@60868: lemma one_div_minus_numeral [simp]:
haftmann@60868:   "1 div - numeral n = - (adjust_div (divmod Num.One n) :: int)"
haftmann@60868:   using numeral_div_minus_numeral [of Num.One n] by simp
haftmann@60868:   
haftmann@60868: lemma one_mod_minus_numeral [simp]:
haftmann@60868:   "1 mod - numeral n = - adjust_mod (numeral n) (snd (divmod Num.One n) :: int)"
haftmann@60868:   using numeral_mod_minus_numeral [of Num.One n] by simp
haftmann@60868: 
haftmann@60930: end
haftmann@60930: 
haftmann@60868: 
haftmann@60868: subsubsection \<open>Further properties\<close>
haftmann@60868: 
haftmann@60868: text \<open>Simplify expresions in which div and mod combine numerical constants\<close>
haftmann@60868: 
haftmann@60868: lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q"
haftmann@60868:   by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)
haftmann@60868: 
haftmann@60868: lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q"
haftmann@60868:   by (rule div_int_unique [of a b q r],
haftmann@60868:     simp add: divmod_int_rel_def)
haftmann@60868: 
haftmann@60868: lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r"
haftmann@60868:   by (rule mod_int_unique [of a b q r],
haftmann@60868:     simp add: divmod_int_rel_def)
haftmann@60868: 
haftmann@60868: lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r"
haftmann@60868:   by (rule mod_int_unique [of a b q r],
haftmann@60868:     simp add: divmod_int_rel_def)
haftmann@33361: 
wenzelm@61944: lemma abs_div: "(y::int) dvd x \<Longrightarrow> \<bar>x div y\<bar> = \<bar>x\<bar> div \<bar>y\<bar>"
haftmann@33361: by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
haftmann@33361: 
wenzelm@60758: text\<open>Suggested by Matthias Daum\<close>
haftmann@33361: lemma int_power_div_base:
haftmann@33361:      "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
haftmann@33361: apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
haftmann@33361:  apply (erule ssubst)
haftmann@33361:  apply (simp only: power_add)
haftmann@33361:  apply simp_all
haftmann@33361: done
haftmann@33361: 
wenzelm@60758: text \<open>by Brian Huffman\<close>
haftmann@33361: lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
haftmann@33361: by (rule mod_minus_eq [symmetric])
haftmann@33361: 
haftmann@33361: lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
haftmann@33361: by (rule mod_diff_left_eq [symmetric])
haftmann@33361: 
haftmann@33361: lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
haftmann@33361: by (rule mod_diff_right_eq [symmetric])
haftmann@33361: 
haftmann@33361: lemmas zmod_simps =
haftmann@33361:   mod_add_left_eq  [symmetric]
haftmann@33361:   mod_add_right_eq [symmetric]
huffman@47142:   mod_mult_right_eq[symmetric]
haftmann@33361:   mod_mult_left_eq [symmetric]
huffman@47164:   power_mod
haftmann@33361:   zminus_zmod zdiff_zmod_left zdiff_zmod_right
haftmann@33361: 
wenzelm@61799: text \<open>Distributive laws for function \<open>nat\<close>.\<close>
haftmann@33361: 
haftmann@33361: lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
haftmann@33361: apply (rule linorder_cases [of y 0])
haftmann@33361: apply (simp add: div_nonneg_neg_le0)
haftmann@33361: apply simp
haftmann@33361: apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: (*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
haftmann@33361: lemma nat_mod_distrib:
haftmann@33361:   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
haftmann@33361: apply (case_tac "y = 0", simp)
haftmann@33361: apply (simp add: nat_eq_iff zmod_int)
haftmann@33361: done
haftmann@33361: 
wenzelm@60758: text  \<open>transfer setup\<close>
haftmann@33361: 
haftmann@33361: lemma transfer_nat_int_functions:
haftmann@33361:     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
haftmann@33361:     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
haftmann@33361:   by (auto simp add: nat_div_distrib nat_mod_distrib)
haftmann@33361: 
haftmann@33361: lemma transfer_nat_int_function_closures:
haftmann@33361:     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
haftmann@33361:     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
haftmann@33361:   apply (cases "y = 0")
haftmann@33361:   apply (auto simp add: pos_imp_zdiv_nonneg_iff)
haftmann@33361:   apply (cases "y = 0")
haftmann@33361:   apply auto
haftmann@33361: done
haftmann@33361: 
haftmann@35644: declare transfer_morphism_nat_int [transfer add return:
haftmann@33361:   transfer_nat_int_functions
haftmann@33361:   transfer_nat_int_function_closures
haftmann@33361: ]
haftmann@33361: 
haftmann@33361: lemma transfer_int_nat_functions:
haftmann@33361:     "(int x) div (int y) = int (x div y)"
haftmann@33361:     "(int x) mod (int y) = int (x mod y)"
haftmann@33361:   by (auto simp add: zdiv_int zmod_int)
haftmann@33361: 
haftmann@33361: lemma transfer_int_nat_function_closures:
haftmann@33361:     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
haftmann@33361:     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
haftmann@33361:   by (simp_all only: is_nat_def transfer_nat_int_function_closures)
haftmann@33361: 
haftmann@35644: declare transfer_morphism_int_nat [transfer add return:
haftmann@33361:   transfer_int_nat_functions
haftmann@33361:   transfer_int_nat_function_closures
haftmann@33361: ]
haftmann@33361: 
wenzelm@60758: text\<open>Suggested by Matthias Daum\<close>
haftmann@33361: lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
haftmann@33361: apply (subgoal_tac "nat x div nat k < nat x")
nipkow@34225:  apply (simp add: nat_div_distrib [symmetric])
haftmann@33361: apply (rule Divides.div_less_dividend, simp_all)
haftmann@33361: done
haftmann@33361: 
haftmann@33361: lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
haftmann@33361: proof
haftmann@33361:   assume H: "x mod n = y mod n"
haftmann@33361:   hence "x mod n - y mod n = 0" by simp
lp15@60562:   hence "(x mod n - y mod n) mod n = 0" by simp
haftmann@33361:   hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
haftmann@33361:   thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
haftmann@33361: next
haftmann@33361:   assume H: "n dvd x - y"
haftmann@33361:   then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
haftmann@33361:   hence "x = n*k + y" by simp
haftmann@33361:   hence "x mod n = (n*k + y) mod n" by simp
haftmann@33361:   thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
haftmann@33361: qed
haftmann@33361: 
haftmann@33361: lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
haftmann@33361:   shows "\<exists>q. x = y + n * q"
haftmann@33361: proof-
lp15@60562:   from xy have th: "int x - int y = int (x - y)" by simp
lp15@60562:   from xyn have "int x mod int n = int y mod int n"
huffman@46551:     by (simp add: zmod_int [symmetric])
lp15@60562:   hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric])
haftmann@33361:   hence "n dvd x - y" by (simp add: th zdvd_int)
haftmann@33361:   then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
haftmann@33361: qed
haftmann@33361: 
lp15@60562: lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)"
haftmann@33361:   (is "?lhs = ?rhs")
haftmann@33361: proof
haftmann@33361:   assume H: "x mod n = y mod n"
haftmann@33361:   {assume xy: "x \<le> y"
haftmann@33361:     from H have th: "y mod n = x mod n" by simp
lp15@60562:     from nat_mod_eq_lemma[OF th xy] have ?rhs
haftmann@33361:       apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
haftmann@33361:   moreover
haftmann@33361:   {assume xy: "y \<le> x"
lp15@60562:     from nat_mod_eq_lemma[OF H xy] have ?rhs
haftmann@33361:       apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
lp15@60562:   ultimately  show ?rhs using linear[of x y] by blast
haftmann@33361: next
haftmann@33361:   assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
haftmann@33361:   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
haftmann@33361:   thus  ?lhs by simp
haftmann@33361: qed
haftmann@33361: 
haftmann@60868: subsubsection \<open>Dedicated simproc for calculation\<close>
haftmann@60868: 
wenzelm@60758: text \<open>
haftmann@60868:   There is space for improvement here: the calculation itself
haftmann@60868:   could be carried outside the logic, and a generic simproc
haftmann@60868:   (simplifier setup) for generic calculation would be helpful. 
wenzelm@60758: \<close>
haftmann@53067: 
haftmann@60868: simproc_setup numeral_divmod
haftmann@60868:   ("0 div 0 :: 'a :: semiring_numeral_div" | "0 mod 0 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "0 div 1 :: 'a :: semiring_numeral_div" | "0 mod 1 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "0 div - 1 :: int" | "0 mod - 1 :: int" |
haftmann@60868:    "0 div numeral b :: 'a :: semiring_numeral_div" | "0 mod numeral b :: 'a :: semiring_numeral_div" |
haftmann@60868:    "0 div - numeral b :: int" | "0 mod - numeral b :: int" |
haftmann@60868:    "1 div 0 :: 'a :: semiring_numeral_div" | "1 mod 0 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "1 div 1 :: 'a :: semiring_numeral_div" | "1 mod 1 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "1 div - 1 :: int" | "1 mod - 1 :: int" |
haftmann@60868:    "1 div numeral b :: 'a :: semiring_numeral_div" | "1 mod numeral b :: 'a :: semiring_numeral_div" |
haftmann@60868:    "1 div - numeral b :: int" |"1 mod - numeral b :: int" |
haftmann@60868:    "- 1 div 0 :: int" | "- 1 mod 0 :: int" | "- 1 div 1 :: int" | "- 1 mod 1 :: int" |
haftmann@60868:    "- 1 div - 1 :: int" | "- 1 mod - 1 :: int" | "- 1 div numeral b :: int" | "- 1 mod numeral b :: int" |
haftmann@60868:    "- 1 div - numeral b :: int" | "- 1 mod - numeral b :: int" |
haftmann@60868:    "numeral a div 0 :: 'a :: semiring_numeral_div" | "numeral a mod 0 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "numeral a div 1 :: 'a :: semiring_numeral_div" | "numeral a mod 1 :: 'a :: semiring_numeral_div" |
haftmann@60868:    "numeral a div - 1 :: int" | "numeral a mod - 1 :: int" |
haftmann@60868:    "numeral a div numeral b :: 'a :: semiring_numeral_div" | "numeral a mod numeral b :: 'a :: semiring_numeral_div" |
haftmann@60868:    "numeral a div - numeral b :: int" | "numeral a mod - numeral b :: int" |
haftmann@60868:    "- numeral a div 0 :: int" | "- numeral a mod 0 :: int" |
haftmann@60868:    "- numeral a div 1 :: int" | "- numeral a mod 1 :: int" |
haftmann@60868:    "- numeral a div - 1 :: int" | "- numeral a mod - 1 :: int" |
haftmann@60868:    "- numeral a div numeral b :: int" | "- numeral a mod numeral b :: int" |
haftmann@60868:    "- numeral a div - numeral b :: int" | "- numeral a mod - numeral b :: int") =
haftmann@60868: \<open> let
haftmann@60868:     val if_cong = the (Code.get_case_cong @{theory} @{const_name If});
haftmann@60868:     fun successful_rewrite ctxt ct =
haftmann@60868:       let
haftmann@60868:         val thm = Simplifier.rewrite ctxt ct
haftmann@60868:       in if Thm.is_reflexive thm then NONE else SOME thm end;
haftmann@60868:   in fn phi =>
haftmann@60868:     let
haftmann@60868:       val simps = Morphism.fact phi (@{thms div_0 mod_0 div_by_0 mod_by_0 div_by_1 mod_by_1
haftmann@60868:         one_div_numeral one_mod_numeral minus_one_div_numeral minus_one_mod_numeral
haftmann@60868:         one_div_minus_numeral one_mod_minus_numeral
haftmann@60868:         numeral_div_numeral numeral_mod_numeral minus_numeral_div_numeral minus_numeral_mod_numeral
haftmann@60868:         numeral_div_minus_numeral numeral_mod_minus_numeral
haftmann@60930:         div_minus_minus mod_minus_minus Divides.adjust_div_eq of_bool_eq one_neq_zero
haftmann@60868:         numeral_neq_zero neg_equal_0_iff_equal arith_simps arith_special divmod_trivial
haftmann@60868:         divmod_cancel divmod_steps divmod_step_eq fst_conv snd_conv numeral_One
haftmann@60930:         case_prod_beta rel_simps Divides.adjust_mod_def div_minus1_right mod_minus1_right
haftmann@60868:         minus_minus numeral_times_numeral mult_zero_right mult_1_right}
haftmann@60868:         @ [@{lemma "0 = 0 \<longleftrightarrow> True" by simp}]);
haftmann@60868:       fun prepare_simpset ctxt = HOL_ss |> Simplifier.simpset_map ctxt
haftmann@60868:         (Simplifier.add_cong if_cong #> fold Simplifier.add_simp simps)
haftmann@60868:     in fn ctxt => successful_rewrite (Simplifier.put_simpset (prepare_simpset ctxt) ctxt) end
haftmann@60868:   end;
haftmann@60868: \<close>
blanchet@34126: 
haftmann@35673: 
wenzelm@60758: subsubsection \<open>Code generation\<close>
haftmann@33361: 
haftmann@60868: lemma [code]:
haftmann@60868:   fixes k :: int
haftmann@60868:   shows 
haftmann@60868:     "k div 0 = 0"
haftmann@60868:     "k mod 0 = k"
haftmann@60868:     "0 div k = 0"
haftmann@60868:     "0 mod k = 0"
haftmann@60868:     "k div Int.Pos Num.One = k"
haftmann@60868:     "k mod Int.Pos Num.One = 0"
haftmann@60868:     "k div Int.Neg Num.One = - k"
haftmann@60868:     "k mod Int.Neg Num.One = 0"
haftmann@60868:     "Int.Pos m div Int.Pos n = (fst (divmod m n) :: int)"
haftmann@60868:     "Int.Pos m mod Int.Pos n = (snd (divmod m n) :: int)"
haftmann@60930:     "Int.Neg m div Int.Pos n = - (Divides.adjust_div (divmod m n) :: int)"
haftmann@60930:     "Int.Neg m mod Int.Pos n = Divides.adjust_mod (Int.Pos n) (snd (divmod m n) :: int)"
haftmann@60930:     "Int.Pos m div Int.Neg n = - (Divides.adjust_div (divmod m n) :: int)"
haftmann@60930:     "Int.Pos m mod Int.Neg n = - Divides.adjust_mod (Int.Pos n) (snd (divmod m n) :: int)"
haftmann@60868:     "Int.Neg m div Int.Neg n = (fst (divmod m n) :: int)"
haftmann@60868:     "Int.Neg m mod Int.Neg n = - (snd (divmod m n) :: int)"
haftmann@60868:   by simp_all
haftmann@53069: 
haftmann@52435: code_identifier
haftmann@52435:   code_module Divides \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364: 
haftmann@60868: lemma dvd_eq_mod_eq_0_numeral:
haftmann@60868:   "numeral x dvd (numeral y :: 'a) \<longleftrightarrow> numeral y mod numeral x = (0 :: 'a::semiring_div)"
haftmann@60868:   by (fact dvd_eq_mod_eq_0)
haftmann@60868: 
haftmann@33361: end




