
haftmann@35372: (*  Title:      HOL/Library/Fraction_Field.thy
chaieb@31761:     Author:     Amine Chaieb, University of Cambridge
chaieb@31761: *)
chaieb@31761: 
chaieb@31761: header{* A formalization of the fraction field of any integral domain 
haftmann@35372:          A generalization of Rat.thy from int to any integral domain *}
chaieb@31761: 
chaieb@31761: theory Fraction_Field
haftmann@35372: imports Main
chaieb@31761: begin
chaieb@31761: 
chaieb@31761: subsection {* General fractions construction *}
chaieb@31761: 
chaieb@31761: subsubsection {* Construction of the type of fractions *}
chaieb@31761: 
chaieb@31761: definition fractrel :: "(('a::idom * 'a ) * ('a * 'a)) set" where
chaieb@31761:   "fractrel == {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
chaieb@31761: 
chaieb@31761: lemma fractrel_iff [simp]:
chaieb@31761:   "(x, y) \<in> fractrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
chaieb@31761:   by (simp add: fractrel_def)
chaieb@31761: 
chaieb@31761: lemma refl_fractrel: "refl_on {x. snd x \<noteq> 0} fractrel"
chaieb@31761:   by (auto simp add: refl_on_def fractrel_def)
chaieb@31761: 
chaieb@31761: lemma sym_fractrel: "sym fractrel"
chaieb@31761:   by (simp add: fractrel_def sym_def)
chaieb@31761: 
chaieb@31761: lemma trans_fractrel: "trans fractrel"
chaieb@31761: proof (rule transI, unfold split_paired_all)
chaieb@31761:   fix a b a' b' a'' b'' :: 'a
chaieb@31761:   assume A: "((a, b), (a', b')) \<in> fractrel"
chaieb@31761:   assume B: "((a', b'), (a'', b'')) \<in> fractrel"
chaieb@31761:   have "b' * (a * b'') = b'' * (a * b')" by (simp add: mult_ac)
chaieb@31761:   also from A have "a * b' = a' * b" by auto
chaieb@31761:   also have "b'' * (a' * b) = b * (a' * b'')" by (simp add: mult_ac)
chaieb@31761:   also from B have "a' * b'' = a'' * b'" by auto
chaieb@31761:   also have "b * (a'' * b') = b' * (a'' * b)" by (simp add: mult_ac)
chaieb@31761:   finally have "b' * (a * b'') = b' * (a'' * b)" .
chaieb@31761:   moreover from B have "b' \<noteq> 0" by auto
chaieb@31761:   ultimately have "a * b'' = a'' * b" by simp
chaieb@31761:   with A B show "((a, b), (a'', b'')) \<in> fractrel" by auto
chaieb@31761: qed
chaieb@31761:   
chaieb@31761: lemma equiv_fractrel: "equiv {x. snd x \<noteq> 0} fractrel"
haftmann@40815:   by (rule equivI [OF refl_fractrel sym_fractrel trans_fractrel])
chaieb@31761: 
chaieb@31761: lemmas UN_fractrel = UN_equiv_class [OF equiv_fractrel]
chaieb@31761: lemmas UN_fractrel2 = UN_equiv_class2 [OF equiv_fractrel equiv_fractrel]
chaieb@31761: 
chaieb@31761: lemma equiv_fractrel_iff [iff]: 
chaieb@31761:   assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
chaieb@31761:   shows "fractrel `` {x} = fractrel `` {y} \<longleftrightarrow> (x, y) \<in> fractrel"
chaieb@31761:   by (rule eq_equiv_class_iff, rule equiv_fractrel) (auto simp add: assms)
chaieb@31761: 
chaieb@31761: typedef 'a fract = "{(x::'a\<times>'a). snd x \<noteq> (0::'a::idom)} // fractrel"
chaieb@31761: proof
chaieb@31761:   have "(0::'a, 1::'a) \<in> {x. snd x \<noteq> 0}" by simp
chaieb@31761:   then show "fractrel `` {(0::'a, 1)} \<in> {x. snd x \<noteq> 0} // fractrel" by (rule quotientI)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
chaieb@31761:   by (simp add: fract_def quotientI)
chaieb@31761: 
chaieb@31761: declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
chaieb@31761: 
chaieb@31761: 
chaieb@31761: subsubsection {* Representation and basic operations *}
chaieb@31761: 
chaieb@31761: definition
chaieb@31761:   Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
haftmann@37765:   "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
chaieb@31761: 
chaieb@31761: code_datatype Fract
chaieb@31761: 
chaieb@31761: lemma Fract_cases [case_names Fract, cases type: fract]:
chaieb@31761:   assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
chaieb@31761:   shows C
chaieb@31761:   using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
chaieb@31761: 
chaieb@31761: lemma Fract_induct [case_names Fract, induct type: fract]:
chaieb@31761:   assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
chaieb@31761:   shows "P q"
chaieb@31761:   using assms by (cases q) simp
chaieb@31761: 
chaieb@31761: lemma eq_fract:
chaieb@31761:   shows "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
chaieb@31761:   and "\<And>a. Fract a 0 = Fract 0 1"
chaieb@31761:   and "\<And>a c. Fract 0 a = Fract 0 c"
chaieb@31761:   by (simp_all add: Fract_def)
chaieb@31761: 
chaieb@31761: instantiation fract :: (idom) "{comm_ring_1, power}"
chaieb@31761: begin
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   Zero_fract_def [code_unfold]: "0 = Fract 0 1"
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   One_fract_def [code_unfold]: "1 = Fract 1 1"
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   add_fract_def:
chaieb@31761:   "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761:     fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
chaieb@31761: 
chaieb@31761: lemma add_fract [simp]:
chaieb@31761:   assumes "b \<noteq> (0::'a::idom)" and "d \<noteq> 0"
chaieb@31761:   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
chaieb@31761: proof -
chaieb@31761:   have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
chaieb@31761:     respects2 fractrel"
chaieb@31761:   apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761:   unfolding mult_assoc[symmetric] .
chaieb@31761:   with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   minus_fract_def:
chaieb@31761:   "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
chaieb@31761: 
chaieb@31761: lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
chaieb@31761: proof -
chaieb@31761:   have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
haftmann@40822:     by (simp add: congruent_def split_paired_all)
chaieb@31761:   then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
chaieb@31761:   by (cases "b = 0") (simp_all add: eq_fract)
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   diff_fract_def: "q - r = q + - (r::'a fract)"
chaieb@31761: 
chaieb@31761: lemma diff_fract [simp]:
chaieb@31761:   assumes "b \<noteq> 0" and "d \<noteq> 0"
chaieb@31761:   shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
chaieb@31761:   using assms by (simp add: diff_fract_def diff_minus)
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   mult_fract_def:
chaieb@31761:   "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761:     fractrel``{(fst x * fst y, snd x * snd y)})"
chaieb@31761: 
chaieb@31761: lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
chaieb@31761: proof -
chaieb@31761:   have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
chaieb@31761:     apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761:     unfolding mult_assoc[symmetric] .
chaieb@31761:   then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: lemma mult_fract_cancel:
chaieb@31761:   assumes "c \<noteq> 0"
chaieb@31761:   shows "Fract (c * a) (c * b) = Fract a b"
chaieb@31761: proof -
chaieb@31761:   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
chaieb@31761:   then show ?thesis by (simp add: mult_fract [symmetric])
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: instance proof
chaieb@31761:   fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)" 
chaieb@31761:     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761: next
chaieb@31761:   fix q r :: "'a fract" show "q * r = r * q"
chaieb@31761:     by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761: next
chaieb@31761:   fix q :: "'a fract" show "1 * q = q"
chaieb@31761:     by (cases q) (simp add: One_fract_def eq_fract)
chaieb@31761: next
chaieb@31761:   fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
chaieb@31761:     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761: next
chaieb@31761:   fix q r :: "'a fract" show "q + r = r + q"
chaieb@31761:     by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761: next
chaieb@31761:   fix q :: "'a fract" show "0 + q = q"
chaieb@31761:     by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761: next
chaieb@31761:   fix q :: "'a fract" show "- q + q = 0"
chaieb@31761:     by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761: next
chaieb@31761:   fix q r :: "'a fract" show "q - r = q + - r"
chaieb@31761:     by (cases q, cases r) (simp add: eq_fract)
chaieb@31761: next
chaieb@31761:   fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
chaieb@31761:     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761: next
chaieb@31761:   show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: end
chaieb@31761: 
chaieb@31761: lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
chaieb@31761:   by (induct k) (simp_all add: Zero_fract_def One_fract_def)
chaieb@31761: 
chaieb@31761: lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
chaieb@31761:   by (rule of_nat_fract [symmetric])
chaieb@31761: 
haftmann@31998: lemma fract_collapse [code_post]:
chaieb@31761:   "Fract 0 k = 0"
chaieb@31761:   "Fract 1 1 = 1"
chaieb@31761:   "Fract k 0 = 0"
chaieb@31761:   by (cases "k = 0")
chaieb@31761:     (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
chaieb@31761: 
haftmann@31998: lemma fract_expand [code_unfold]:
chaieb@31761:   "0 = Fract 0 1"
chaieb@31761:   "1 = Fract 1 1"
chaieb@31761:   by (simp_all add: fract_collapse)
chaieb@31761: 
chaieb@31761: lemma Fract_cases_nonzero [case_names Fract 0]:
chaieb@31761:   assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
chaieb@31761:   assumes 0: "q = 0 \<Longrightarrow> C"
chaieb@31761:   shows C
chaieb@31761: proof (cases "q = 0")
chaieb@31761:   case True then show C using 0 by auto
chaieb@31761: next
chaieb@31761:   case False
chaieb@31761:   then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
chaieb@31761:   moreover with False have "0 \<noteq> Fract a b" by simp
chaieb@31761:   with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
chaieb@31761:   with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
chaieb@31761: qed
chaieb@31761:   
chaieb@31761: 
chaieb@31761: 
chaieb@31761: subsubsection {* The field of rational numbers *}
chaieb@31761: 
chaieb@31761: context idom
chaieb@31761: begin
chaieb@31761: subclass ring_no_zero_divisors ..
chaieb@31761: thm mult_eq_0_iff
chaieb@31761: end
chaieb@31761: 
haftmann@36409: instantiation fract :: (idom) field_inverse_zero
chaieb@31761: begin
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   inverse_fract_def:
chaieb@31761:   "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
chaieb@31761:      fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
chaieb@31761: 
chaieb@31761: 
chaieb@31761: lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
chaieb@31761: proof -
chaieb@31761:   have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
chaieb@31761:   have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
chaieb@31761:     by (auto simp add: congruent_def stupid algebra_simps)
chaieb@31761:   then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: definition
haftmann@37765:   divide_fract_def: "q / r = q * inverse (r:: 'a fract)"
chaieb@31761: 
chaieb@31761: lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
chaieb@31761:   by (simp add: divide_fract_def)
chaieb@31761: 
chaieb@31761: instance proof
chaieb@31761:   fix q :: "'a fract"
chaieb@31761:   assume "q \<noteq> 0"
chaieb@31761:   then show "inverse q * q = 1" apply (cases q rule: Fract_cases_nonzero)
chaieb@31761:     by (simp_all add: mult_fract  inverse_fract fract_expand eq_fract mult_commute)
chaieb@31761: next
chaieb@31761:   fix q r :: "'a fract"
chaieb@31761:   show "q / r = q * inverse r" by (simp add: divide_fract_def)
haftmann@36409: next
haftmann@36409:   show "inverse 0 = (0:: 'a fract)" by (simp add: fract_expand)
haftmann@36409:     (simp add: fract_collapse)
chaieb@31761: qed
chaieb@31761: 
chaieb@31761: end
chaieb@31761: 
chaieb@31761: 
huffman@36331: subsubsection {* The ordered field of fractions over an ordered idom *}
huffman@36331: 
huffman@36331: lemma le_congruent2:
huffman@36331:   "(\<lambda>x y::'a \<times> 'a::linordered_idom.
huffman@36331:     {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
huffman@36331:     respects2 fractrel"
huffman@36331: proof (clarsimp simp add: congruent2_def)
huffman@36331:   fix a b a' b' c d c' d' :: 'a
huffman@36331:   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
huffman@36331:   assume eq1: "a * b' = a' * b"
huffman@36331:   assume eq2: "c * d' = c' * d"
huffman@36331: 
huffman@36331:   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
huffman@36331:   {
huffman@36331:     fix a b c d x :: 'a assume x: "x \<noteq> 0"
huffman@36331:     have "?le a b c d = ?le (a * x) (b * x) c d"
huffman@36331:     proof -
huffman@36331:       from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
huffman@36331:       hence "?le a b c d =
huffman@36331:           ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
huffman@36331:         by (simp add: mult_le_cancel_right)
huffman@36331:       also have "... = ?le (a * x) (b * x) c d"
huffman@36331:         by (simp add: mult_ac)
huffman@36331:       finally show ?thesis .
huffman@36331:     qed
huffman@36331:   } note le_factor = this
huffman@36331: 
huffman@36331:   let ?D = "b * d" and ?D' = "b' * d'"
huffman@36331:   from neq have D: "?D \<noteq> 0" by simp
huffman@36331:   from neq have "?D' \<noteq> 0" by simp
huffman@36331:   hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
huffman@36331:     by (rule le_factor)
huffman@36331:   also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
huffman@36331:     by (simp add: mult_ac)
huffman@36331:   also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
huffman@36331:     by (simp only: eq1 eq2)
huffman@36331:   also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
huffman@36331:     by (simp add: mult_ac)
huffman@36331:   also from D have "... = ?le a' b' c' d'"
huffman@36331:     by (rule le_factor [symmetric])
huffman@36331:   finally show "?le a b c d = ?le a' b' c' d'" .
huffman@36331: qed
huffman@36331: 
huffman@36331: instantiation fract :: (linordered_idom) linorder
huffman@36331: begin
huffman@36331: 
huffman@36331: definition
haftmann@37765:   le_fract_def:
haftmann@39910:    "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
huffman@36331:       {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
huffman@36331: 
huffman@36331: definition
haftmann@37765:   less_fract_def: "z < (w::'a fract) \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
huffman@36331: 
huffman@36331: lemma le_fract [simp]:
huffman@36331:   assumes "b \<noteq> 0" and "d \<noteq> 0"
huffman@36331:   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331: by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
huffman@36331: 
huffman@36331: lemma less_fract [simp]:
huffman@36331:   assumes "b \<noteq> 0" and "d \<noteq> 0"
huffman@36331:   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
huffman@36331: by (simp add: less_fract_def less_le_not_le mult_ac assms)
huffman@36331: 
huffman@36331: instance proof
huffman@36331:   fix q r s :: "'a fract"
huffman@36331:   assume "q \<le> r" and "r \<le> s" thus "q \<le> s"
huffman@36331:   proof (induct q, induct r, induct s)
huffman@36331:     fix a b c d e f :: 'a
huffman@36331:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331:     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
huffman@36331:     show "Fract a b \<le> Fract e f"
huffman@36331:     proof -
huffman@36331:       from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
huffman@36331:         by (auto simp add: zero_less_mult_iff linorder_neq_iff)
huffman@36331:       have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
huffman@36331:       proof -
huffman@36331:         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331:           by simp
huffman@36331:         with ff show ?thesis by (simp add: mult_le_cancel_right)
huffman@36331:       qed
huffman@36331:       also have "... = (c * f) * (d * f) * (b * b)"
huffman@36331:         by (simp only: mult_ac)
huffman@36331:       also have "... \<le> (e * d) * (d * f) * (b * b)"
huffman@36331:       proof -
huffman@36331:         from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
huffman@36331:           by simp
huffman@36331:         with bb show ?thesis by (simp add: mult_le_cancel_right)
huffman@36331:       qed
huffman@36331:       finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
huffman@36331:         by (simp only: mult_ac)
huffman@36331:       with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
huffman@36331:         by (simp add: mult_le_cancel_right)
huffman@36331:       with neq show ?thesis by simp
huffman@36331:     qed
huffman@36331:   qed
huffman@36331: next
huffman@36331:   fix q r :: "'a fract"
huffman@36331:   assume "q \<le> r" and "r \<le> q" thus "q = r"
huffman@36331:   proof (induct q, induct r)
huffman@36331:     fix a b c d :: 'a
huffman@36331:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"
huffman@36331:     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
huffman@36331:     show "Fract a b = Fract c d"
huffman@36331:     proof -
huffman@36331:       from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331:         by simp
huffman@36331:       also have "... \<le> (a * d) * (b * d)"
huffman@36331:       proof -
huffman@36331:         from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
huffman@36331:           by simp
huffman@36331:         thus ?thesis by (simp only: mult_ac)
huffman@36331:       qed
huffman@36331:       finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
huffman@36331:       moreover from neq have "b * d \<noteq> 0" by simp
huffman@36331:       ultimately have "a * d = c * b" by simp
huffman@36331:       with neq show ?thesis by (simp add: eq_fract)
huffman@36331:     qed
huffman@36331:   qed
huffman@36331: next
huffman@36331:   fix q r :: "'a fract"
huffman@36331:   show "q \<le> q"
huffman@36331:     by (induct q) simp
huffman@36331:   show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
huffman@36331:     by (simp only: less_fract_def)
huffman@36331:   show "q \<le> r \<or> r \<le> q"
huffman@36331:     by (induct q, induct r)
huffman@36331:        (simp add: mult_commute, rule linorder_linear)
huffman@36331: qed
huffman@36331: 
huffman@36331: end
huffman@36331: 
huffman@36331: instantiation fract :: (linordered_idom) "{distrib_lattice, abs_if, sgn_if}"
huffman@36331: begin
huffman@36331: 
huffman@36331: definition
huffman@36331:   abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
huffman@36331: 
huffman@36331: definition
huffman@36331:   sgn_fract_def:
huffman@36331:     "sgn (q::'a fract) = (if q=0 then 0 else if 0<q then 1 else - 1)"
huffman@36331: 
huffman@36331: theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
huffman@36331:   by (auto simp add: abs_fract_def Zero_fract_def le_less
huffman@36331:       eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
huffman@36331: 
huffman@36331: definition
huffman@36331:   inf_fract_def:
huffman@36331:     "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
huffman@36331: 
huffman@36331: definition
huffman@36331:   sup_fract_def:
huffman@36331:     "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
huffman@36331: 
huffman@36331: instance by intro_classes
huffman@36331:   (auto simp add: abs_fract_def sgn_fract_def
huffman@36331:     min_max.sup_inf_distrib1 inf_fract_def sup_fract_def)
huffman@36331: 
huffman@36331: end
huffman@36331: 
haftmann@36414: instance fract :: (linordered_idom) linordered_field_inverse_zero
huffman@36331: proof
huffman@36331:   fix q r s :: "'a fract"
huffman@36331:   show "q \<le> r ==> s + q \<le> s + r"
huffman@36331:   proof (induct q, induct r, induct s)
huffman@36331:     fix a b c d e f :: 'a
huffman@36331:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331:     assume le: "Fract a b \<le> Fract c d"
huffman@36331:     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
huffman@36331:     proof -
huffman@36331:       let ?F = "f * f" from neq have F: "0 < ?F"
huffman@36331:         by (auto simp add: zero_less_mult_iff)
huffman@36331:       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331:         by simp
huffman@36331:       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
huffman@36331:         by (simp add: mult_le_cancel_right)
haftmann@36348:       with neq show ?thesis by (simp add: field_simps)
huffman@36331:     qed
huffman@36331:   qed
huffman@36331:   show "q < r ==> 0 < s ==> s * q < s * r"
huffman@36331:   proof (induct q, induct r, induct s)
huffman@36331:     fix a b c d e f :: 'a
huffman@36331:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331:     assume le: "Fract a b < Fract c d"
huffman@36331:     assume gt: "0 < Fract e f"
huffman@36331:     show "Fract e f * Fract a b < Fract e f * Fract c d"
huffman@36331:     proof -
huffman@36331:       let ?E = "e * f" and ?F = "f * f"
huffman@36331:       from neq gt have "0 < ?E"
huffman@36331:         by (auto simp add: Zero_fract_def order_less_le eq_fract)
huffman@36331:       moreover from neq have "0 < ?F"
huffman@36331:         by (auto simp add: zero_less_mult_iff)
huffman@36331:       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
huffman@36331:         by simp
huffman@36331:       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
huffman@36331:         by (simp add: mult_less_cancel_right)
huffman@36331:       with neq show ?thesis
huffman@36331:         by (simp add: mult_ac)
huffman@36331:     qed
huffman@36331:   qed
huffman@36331: qed
huffman@36331: 
huffman@36331: lemma fract_induct_pos [case_names Fract]:
huffman@36331:   fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
huffman@36331:   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
huffman@36331:   shows "P q"
huffman@36331: proof (cases q)
huffman@36331:   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
huffman@36331:   proof -
huffman@36331:     fix a::'a and b::'a
huffman@36331:     assume b: "b < 0"
huffman@36331:     hence "0 < -b" by simp
huffman@36331:     hence "P (Fract (-a) (-b))" by (rule step)
huffman@36331:     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
huffman@36331:   qed
huffman@36331:   case (Fract a b)
huffman@36331:   thus "P q" by (force simp add: linorder_neq_iff step step')
huffman@36331: qed
huffman@36331: 
huffman@36331: lemma zero_less_Fract_iff:
huffman@36331:   "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
huffman@36331:   by (auto simp add: Zero_fract_def zero_less_mult_iff)
huffman@36331: 
huffman@36331: lemma Fract_less_zero_iff:
huffman@36331:   "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
huffman@36331:   by (auto simp add: Zero_fract_def mult_less_0_iff)
huffman@36331: 
huffman@36331: lemma zero_le_Fract_iff:
huffman@36331:   "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
huffman@36331:   by (auto simp add: Zero_fract_def zero_le_mult_iff)
huffman@36331: 
huffman@36331: lemma Fract_le_zero_iff:
huffman@36331:   "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
huffman@36331:   by (auto simp add: Zero_fract_def mult_le_0_iff)
huffman@36331: 
huffman@36331: lemma one_less_Fract_iff:
huffman@36331:   "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
huffman@36331:   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
huffman@36331: 
huffman@36331: lemma Fract_less_one_iff:
huffman@36331:   "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
huffman@36331:   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
huffman@36331: 
huffman@36331: lemma one_le_Fract_iff:
huffman@36331:   "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
huffman@36331:   by (auto simp add: One_fract_def mult_le_cancel_right)
huffman@36331: 
huffman@36331: lemma Fract_le_one_iff:
huffman@36331:   "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
huffman@36331:   by (auto simp add: One_fract_def mult_le_cancel_right)
huffman@36331: 
huffman@36331: end




