
paulson@14365: (*  Title: HOL/Library/Rational.thy
paulson@14365:     ID:    $Id$
paulson@14365:     Author: Markus Wenzel, TU Muenchen
paulson@14365: *)
paulson@14365: 
wenzelm@14691: header {* Rational numbers *}
paulson@14365: 
nipkow@15131: theory Rational
huffman@18913: imports Main
haftmann@16417: uses ("rat_arith.ML")
nipkow@15131: begin
paulson@14365: 
huffman@18913: subsection {* Rational numbers *}
paulson@14365: 
paulson@14365: subsubsection {* Equivalence of fractions *}
paulson@14365: 
wenzelm@19765: definition
wenzelm@21404:   fraction :: "(int \<times> int) set" where
wenzelm@19765:   "fraction = {x. snd x \<noteq> 0}"
huffman@18913: 
wenzelm@21404: definition
wenzelm@21404:   ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
wenzelm@19765:   "ratrel = {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
paulson@14365: 
huffman@18913: lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
huffman@18913: by (simp add: fraction_def)
paulson@14365: 
huffman@18913: lemma ratrel_iff [simp]:
huffman@18913:   "((x,y) \<in> ratrel) =
huffman@18913:    (snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
huffman@18913: by (simp add: ratrel_def)
paulson@14365: 
huffman@18913: lemma refl_ratrel: "refl fraction ratrel"
huffman@18913: by (auto simp add: refl_def fraction_def ratrel_def)
huffman@18913: 
huffman@18913: lemma sym_ratrel: "sym ratrel"
huffman@18913: by (simp add: ratrel_def sym_def)
huffman@18913: 
huffman@18913: lemma trans_ratrel_lemma:
huffman@18913:   assumes 1: "a * b' = a' * b"
huffman@18913:   assumes 2: "a' * b'' = a'' * b'"
huffman@18913:   assumes 3: "b' \<noteq> (0::int)"
huffman@18913:   shows "a * b'' = a'' * b"
huffman@18913: proof -
huffman@18913:   have "b' * (a * b'') = b'' * (a * b')" by simp
huffman@18913:   also note 1
huffman@18913:   also have "b'' * (a' * b) = b * (a' * b'')" by simp
huffman@18913:   also note 2
huffman@18913:   also have "b * (a'' * b') = b' * (a'' * b)" by simp
huffman@18913:   finally have "b' * (a * b'') = b' * (a'' * b)" .
huffman@18913:   with 3 show "a * b'' = a'' * b" by simp
paulson@14365: qed
paulson@14365: 
huffman@18913: lemma trans_ratrel: "trans ratrel"
huffman@18913: by (auto simp add: trans_def elim: trans_ratrel_lemma)
huffman@18913: 
huffman@18913: lemma equiv_ratrel: "equiv fraction ratrel"
huffman@18913: by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
huffman@18913: 
huffman@18913: lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
huffman@18913: 
huffman@18913: lemma equiv_ratrel_iff2:
huffman@18913:   "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
huffman@18913:     \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
huffman@18913: by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all)
paulson@14365: 
paulson@14365: 
huffman@18913: subsubsection {* The type of rational numbers *}
paulson@14365: 
huffman@18913: typedef (Rat) rat = "fraction//ratrel"
huffman@18913: proof
huffman@18913:   have "(0,1) \<in> fraction" by (simp add: fraction_def)
huffman@18913:   thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI)
paulson@14365: qed
paulson@14365: 
huffman@18913: lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat"
huffman@18913: by (simp add: Rat_def quotientI)
huffman@18913: 
huffman@18913: declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
huffman@18913: 
huffman@18913: 
wenzelm@19765: definition
wenzelm@21404:   Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
wenzelm@19765:   "Fract a b = Abs_Rat (ratrel``{(a,b)})"
huffman@18913: 
huffman@18913: theorem Rat_cases [case_names Fract, cases type: rat]:
wenzelm@21404:     "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
wenzelm@21404:   by (cases q) (clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
huffman@18913: 
huffman@18913: theorem Rat_induct [case_names Fract, induct type: rat]:
huffman@18913:     "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
huffman@18913:   by (cases q) simp
huffman@18913: 
huffman@18913: 
huffman@18913: subsubsection {* Congruence lemmas *}
paulson@14365: 
huffman@18913: lemma add_congruent2:
huffman@18913:      "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
huffman@18913:       respects2 ratrel"
huffman@18913: apply (rule equiv_ratrel [THEN congruent2_commuteI])
huffman@18913: apply (simp_all add: left_distrib)
huffman@18913: done
huffman@18913: 
huffman@18913: lemma minus_congruent:
huffman@18913:   "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
huffman@18913: by (simp add: congruent_def)
huffman@18913: 
huffman@18913: lemma mult_congruent2:
huffman@18913:   "(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel"
huffman@18913: by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
huffman@18913: 
huffman@18913: lemma inverse_congruent:
huffman@18913:   "(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel"
huffman@18913: by (auto simp add: congruent_def mult_commute)
huffman@18913: 
huffman@18913: lemma le_congruent2:
huffman@18982:   "(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
huffman@18913:    respects2 ratrel"
huffman@18913: proof (clarsimp simp add: congruent2_def)
huffman@18913:   fix a b a' b' c d c' d'::int
paulson@14365:   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
huffman@18913:   assume eq1: "a * b' = a' * b"
huffman@18913:   assume eq2: "c * d' = c' * d"
paulson@14365: 
paulson@14365:   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365:   {
paulson@14365:     fix a b c d x :: int assume x: "x \<noteq> 0"
paulson@14365:     have "?le a b c d = ?le (a * x) (b * x) c d"
paulson@14365:     proof -
paulson@14365:       from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
paulson@14365:       hence "?le a b c d =
paulson@14365:           ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
paulson@14365:         by (simp add: mult_le_cancel_right)
paulson@14365:       also have "... = ?le (a * x) (b * x) c d"
paulson@14365:         by (simp add: mult_ac)
paulson@14365:       finally show ?thesis .
paulson@14365:     qed
paulson@14365:   } note le_factor = this
paulson@14365: 
paulson@14365:   let ?D = "b * d" and ?D' = "b' * d'"
paulson@14365:   from neq have D: "?D \<noteq> 0" by simp
paulson@14365:   from neq have "?D' \<noteq> 0" by simp
paulson@14365:   hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
paulson@14365:     by (rule le_factor)
paulson@14365:   also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
paulson@14365:     by (simp add: mult_ac)
paulson@14365:   also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
paulson@14365:     by (simp only: eq1 eq2)
paulson@14365:   also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
paulson@14365:     by (simp add: mult_ac)
paulson@14365:   also from D have "... = ?le a' b' c' d'"
paulson@14365:     by (rule le_factor [symmetric])
huffman@18913:   finally show "?le a b c d = ?le a' b' c' d'" .
paulson@14365: qed
paulson@14365: 
huffman@18913: lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
huffman@18913: lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
paulson@14365: 
paulson@14365: 
paulson@14365: subsubsection {* Standard operations on rational numbers *}
paulson@14365: 
huffman@20522: instance rat :: "{ord, zero, one, plus, times, minus, inverse, power}" ..
paulson@14365: 
paulson@14365: defs (overloaded)
huffman@18913:   Zero_rat_def:  "0 == Fract 0 1"
huffman@18913:   One_rat_def:   "1 == Fract 1 1"
huffman@18913: 
huffman@18913:   add_rat_def:
huffman@18913:    "q + r ==
huffman@18913:        Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18913:            ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})"
huffman@18913: 
huffman@18913:   minus_rat_def:
huffman@18913:     "- q == Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
huffman@18913: 
huffman@18913:   diff_rat_def:  "q - r == q + - (r::rat)"
huffman@18913: 
huffman@18913:   mult_rat_def:
huffman@18913:    "q * r ==
huffman@18913:        Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18913:            ratrel``{(fst x * fst y, snd x * snd y)})"
huffman@18913: 
huffman@18913:   inverse_rat_def:
huffman@18913:     "inverse q ==
huffman@18913:         Abs_Rat (\<Union>x \<in> Rep_Rat q.
huffman@18913:             ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
huffman@18913: 
huffman@18913:   divide_rat_def:  "q / r == q * inverse (r::rat)"
huffman@18913: 
huffman@18913:   le_rat_def:
huffman@18982:    "q \<le> r == contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18982:       {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
huffman@18913: 
huffman@18913:   less_rat_def: "(z < (w::rat)) == (z \<le> w & z \<noteq> w)"
huffman@18913: 
paulson@14365:   abs_rat_def: "\<bar>q\<bar> == if q < 0 then -q else (q::rat)"
paulson@14365: 
huffman@20522: primrec (rat)
huffman@20522:   rat_power_0:   "q ^ 0       = 1"
huffman@20522:   rat_power_Suc: "q ^ (Suc n) = (q::rat) * (q ^ n)"
huffman@20522: 
huffman@18913: lemma zero_rat: "0 = Fract 0 1"
huffman@18913: by (simp add: Zero_rat_def)
paulson@14365: 
huffman@18913: lemma one_rat: "1 = Fract 1 1"
huffman@18913: by (simp add: One_rat_def)
huffman@18913: 
huffman@18913: theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
huffman@18913:   (Fract a b = Fract c d) = (a * d = c * b)"
huffman@18913: by (simp add: Fract_def)
paulson@14365: 
paulson@14365: theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:   Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
huffman@18913: by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2)
paulson@14365: 
paulson@14365: theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
huffman@18913: by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel)
paulson@14365: 
paulson@14365: theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:     Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
huffman@18913: by (simp add: diff_rat_def add_rat minus_rat)
paulson@14365: 
paulson@14365: theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:   Fract a b * Fract c d = Fract (a * c) (b * d)"
huffman@18913: by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2)
paulson@14365: 
huffman@18913: theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==>
paulson@14365:   inverse (Fract a b) = Fract b a"
huffman@18913: by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel)
paulson@14365: 
huffman@18913: theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:   Fract a b / Fract c d = Fract (a * d) (b * c)"
huffman@18913: by (simp add: divide_rat_def inverse_rat mult_rat)
paulson@14365: 
paulson@14365: theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:   (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
huffman@18982: by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2)
paulson@14365: 
paulson@14365: theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365:     (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
huffman@18913: by (simp add: less_rat_def le_rat eq_rat order_less_le)
paulson@14365: 
paulson@14365: theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
paulson@14365:   by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat)
wenzelm@14691:      (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less
paulson@14365:                 split: abs_split)
paulson@14365: 
paulson@14365: 
paulson@14365: subsubsection {* The ordered field of rational numbers *}
paulson@14365: 
paulson@14365: instance rat :: field
paulson@14365: proof
paulson@14365:   fix q r s :: rat
paulson@14365:   show "(q + r) + s = q + (r + s)"
huffman@18913:     by (induct q, induct r, induct s)
huffman@18913:        (simp add: add_rat add_ac mult_ac int_distrib)
paulson@14365:   show "q + r = r + q"
paulson@14365:     by (induct q, induct r) (simp add: add_rat add_ac mult_ac)
paulson@14365:   show "0 + q = q"
paulson@14365:     by (induct q) (simp add: zero_rat add_rat)
paulson@14365:   show "(-q) + q = 0"
huffman@18913:     by (induct q) (simp add: zero_rat minus_rat add_rat eq_rat)
paulson@14365:   show "q - r = q + (-r)"
paulson@14365:     by (induct q, induct r) (simp add: add_rat minus_rat diff_rat)
paulson@14365:   show "(q * r) * s = q * (r * s)"
paulson@14365:     by (induct q, induct r, induct s) (simp add: mult_rat mult_ac)
paulson@14365:   show "q * r = r * q"
paulson@14365:     by (induct q, induct r) (simp add: mult_rat mult_ac)
paulson@14365:   show "1 * q = q"
paulson@14365:     by (induct q) (simp add: one_rat mult_rat)
paulson@14365:   show "(q + r) * s = q * s + r * s"
wenzelm@14691:     by (induct q, induct r, induct s)
paulson@14365:        (simp add: add_rat mult_rat eq_rat int_distrib)
paulson@14365:   show "q \<noteq> 0 ==> inverse q * q = 1"
paulson@14365:     by (induct q) (simp add: inverse_rat mult_rat one_rat zero_rat eq_rat)
paulson@14430:   show "q / r = q * inverse r"
wenzelm@14691:     by (simp add: divide_rat_def)
paulson@14365:   show "0 \<noteq> (1::rat)"
wenzelm@14691:     by (simp add: zero_rat one_rat eq_rat)
paulson@14365: qed
paulson@14365: 
paulson@14365: instance rat :: linorder
paulson@14365: proof
paulson@14365:   fix q r s :: rat
paulson@14365:   {
paulson@14365:     assume "q \<le> r" and "r \<le> s"
paulson@14365:     show "q \<le> s"
paulson@14365:     proof (insert prems, induct q, induct r, induct s)
paulson@14365:       fix a b c d e f :: int
paulson@14365:       assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365:       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365:       show "Fract a b \<le> Fract e f"
paulson@14365:       proof -
paulson@14365:         from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365:           by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365:         have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365:         proof -
paulson@14365:           from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365:             by (simp add: le_rat)
paulson@14365:           with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365:         qed
paulson@14365:         also have "... = (c * f) * (d * f) * (b * b)"
paulson@14365:           by (simp only: mult_ac)
paulson@14365:         also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365:         proof -
paulson@14365:           from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
paulson@14365:             by (simp add: le_rat)
paulson@14365:           with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365:         qed
paulson@14365:         finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365:           by (simp only: mult_ac)
paulson@14365:         with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365:           by (simp add: mult_le_cancel_right)
paulson@14365:         with neq show ?thesis by (simp add: le_rat)
paulson@14365:       qed
paulson@14365:     qed
paulson@14365:   next
paulson@14365:     assume "q \<le> r" and "r \<le> q"
paulson@14365:     show "q = r"
paulson@14365:     proof (insert prems, induct q, induct r)
paulson@14365:       fix a b c d :: int
paulson@14365:       assume neq: "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365:       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365:       show "Fract a b = Fract c d"
paulson@14365:       proof -
paulson@14365:         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365:           by (simp add: le_rat)
paulson@14365:         also have "... \<le> (a * d) * (b * d)"
paulson@14365:         proof -
paulson@14365:           from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
paulson@14365:             by (simp add: le_rat)
paulson@14365:           thus ?thesis by (simp only: mult_ac)
paulson@14365:         qed
paulson@14365:         finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365:         moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365:         ultimately have "a * d = c * b" by simp
paulson@14365:         with neq show ?thesis by (simp add: eq_rat)
paulson@14365:       qed
paulson@14365:     qed
paulson@14365:   next
paulson@14365:     show "q \<le> q"
paulson@14365:       by (induct q) (simp add: le_rat)
paulson@14365:     show "(q < r) = (q \<le> r \<and> q \<noteq> r)"
paulson@14365:       by (simp only: less_rat_def)
paulson@14365:     show "q \<le> r \<or> r \<le> q"
huffman@18913:       by (induct q, induct r)
huffman@18913:          (simp add: le_rat mult_commute, rule linorder_linear)
paulson@14365:   }
paulson@14365: qed
paulson@14365: 
haftmann@22456: instance rat :: distrib_lattice
haftmann@22456:   "inf r s \<equiv> min r s"
haftmann@22456:   "sup r s \<equiv> max r s"
haftmann@22456:   by default (auto simp add: min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
haftmann@22456: 
paulson@14365: instance rat :: ordered_field
paulson@14365: proof
paulson@14365:   fix q r s :: rat
paulson@14365:   show "q \<le> r ==> s + q \<le> s + r"
paulson@14365:   proof (induct q, induct r, induct s)
paulson@14365:     fix a b c d e f :: int
paulson@14365:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365:     assume le: "Fract a b \<le> Fract c d"
paulson@14365:     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365:     proof -
paulson@14365:       let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365:         by (auto simp add: zero_less_mult_iff)
paulson@14365:       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365:         by (simp add: le_rat)
paulson@14365:       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365:         by (simp add: mult_le_cancel_right)
paulson@14365:       with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib)
paulson@14365:     qed
paulson@14365:   qed
paulson@14365:   show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365:   proof (induct q, induct r, induct s)
paulson@14365:     fix a b c d e f :: int
paulson@14365:     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365:     assume le: "Fract a b < Fract c d"
paulson@14365:     assume gt: "0 < Fract e f"
paulson@14365:     show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365:     proof -
paulson@14365:       let ?E = "e * f" and ?F = "f * f"
paulson@14365:       from neq gt have "0 < ?E"
paulson@14378:         by (auto simp add: zero_rat less_rat le_rat order_less_le eq_rat)
paulson@14365:       moreover from neq have "0 < ?F"
paulson@14365:         by (auto simp add: zero_less_mult_iff)
paulson@14365:       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
paulson@14365:         by (simp add: less_rat)
paulson@14365:       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365:         by (simp add: mult_less_cancel_right)
paulson@14365:       with neq show ?thesis
paulson@14365:         by (simp add: less_rat mult_rat mult_ac)
paulson@14365:     qed
paulson@14365:   qed
paulson@14365:   show "\<bar>q\<bar> = (if q < 0 then -q else q)"
paulson@14365:     by (simp only: abs_rat_def)
haftmann@22456: qed auto
paulson@14365: 
paulson@14365: instance rat :: division_by_zero
paulson@14365: proof
huffman@18913:   show "inverse 0 = (0::rat)"
huffman@18913:     by (simp add: zero_rat Fract_def inverse_rat_def
huffman@18913:                   inverse_congruent UN_ratrel)
paulson@14365: qed
paulson@14365: 
huffman@20522: instance rat :: recpower
huffman@20522: proof
huffman@20522:   fix q :: rat
huffman@20522:   fix n :: nat
huffman@20522:   show "q ^ 0 = 1" by simp
huffman@20522:   show "q ^ (Suc n) = q * (q ^ n)" by simp
huffman@20522: qed
huffman@20522: 
paulson@14365: 
paulson@14365: subsection {* Various Other Results *}
paulson@14365: 
paulson@14365: lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
huffman@18913: by (simp add: eq_rat)
paulson@14365: 
paulson@14365: theorem Rat_induct_pos [case_names Fract, induct type: rat]:
paulson@14365:   assumes step: "!!a b. 0 < b ==> P (Fract a b)"
paulson@14365:     shows "P q"
paulson@14365: proof (cases q)
paulson@14365:   have step': "!!a b. b < 0 ==> P (Fract a b)"
paulson@14365:   proof -
paulson@14365:     fix a::int and b::int
paulson@14365:     assume b: "b < 0"
paulson@14365:     hence "0 < -b" by simp
paulson@14365:     hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365:     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365:   qed
paulson@14365:   case (Fract a b)
paulson@14365:   thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365: qed
paulson@14365: 
paulson@14365: lemma zero_less_Fract_iff:
paulson@14365:      "0 < b ==> (0 < Fract a b) = (0 < a)"
wenzelm@14691: by (simp add: zero_rat less_rat order_less_imp_not_eq2 zero_less_mult_iff)
paulson@14365: 
paulson@14378: lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
paulson@14378: apply (insert add_rat [of concl: m n 1 1])
wenzelm@14691: apply (simp add: one_rat  [symmetric])
paulson@14378: done
paulson@14378: 
paulson@14378: lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
wenzelm@14691: apply (induct k)
wenzelm@14691: apply (simp add: zero_rat)
wenzelm@14691: apply (simp add: Fract_add_one)
paulson@14378: done
paulson@14378: 
paulson@14378: lemma Fract_of_int_eq: "Fract k 1 = of_int k"
wenzelm@14691: proof (cases k rule: int_cases)
paulson@14378:   case (nonneg n)
paulson@14378:     thus ?thesis by (simp add: int_eq_of_nat Fract_of_nat_eq)
wenzelm@14691: next
paulson@14378:   case (neg n)
paulson@14378:     hence "Fract k 1 = - (Fract (of_nat (Suc n)) 1)"
wenzelm@14691:       by (simp only: minus_rat int_eq_of_nat)
paulson@14378:     also have "... =  - (of_nat (Suc n))"
paulson@14378:       by (simp only: Fract_of_nat_eq)
wenzelm@14691:     finally show ?thesis
wenzelm@14691:       by (simp add: only: prems int_eq_of_nat of_int_minus of_int_of_nat_eq)
wenzelm@14691: qed
paulson@14378: 
paulson@14378: 
wenzelm@14691: subsection {* Numerals and Arithmetic *}
paulson@14387: 
haftmann@22456: instance rat :: number
haftmann@22456:   rat_number_of_def: "(number_of w :: rat) \<equiv> of_int w" ..
paulson@14387: 
paulson@14387: instance rat :: number_ring
wenzelm@19765:   by default (simp add: rat_number_of_def) 
paulson@14387: 
paulson@14387: use "rat_arith.ML"
paulson@14387: setup rat_arith_setup
paulson@14387: 
huffman@23342: 
huffman@23342: subsection {* Embedding from Rationals to other Fields *}
huffman@23342: 
huffman@23342: axclass field_char_0 < field, ring_char_0
huffman@23342: 
huffman@23342: instance ordered_field < field_char_0 ..
huffman@23342: 
huffman@23342: definition
huffman@23342:   of_rat :: "rat \<Rightarrow> 'a::field_char_0"
huffman@23342: where
huffman@23342:   "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
huffman@23342: 
huffman@23342: lemma of_rat_congruent:
huffman@23342:   "(\<lambda>(a, b). {of_int a / of_int b::'a::field_char_0}) respects ratrel"
huffman@23342: apply (rule congruent.intro)
huffman@23342: apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23342: apply (simp only: of_int_mult [symmetric])
huffman@23342: done
huffman@23342: 
huffman@23342: lemma of_rat_rat:
huffman@23342:   "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
huffman@23342: unfolding Fract_def of_rat_def
huffman@23342: by (simp add: UN_ratrel of_rat_congruent)
huffman@23342: 
huffman@23342: lemma of_rat_0 [simp]: "of_rat 0 = 0"
huffman@23342: by (simp add: Zero_rat_def of_rat_rat)
huffman@23342: 
huffman@23342: lemma of_rat_1 [simp]: "of_rat 1 = 1"
huffman@23342: by (simp add: One_rat_def of_rat_rat)
huffman@23342: 
huffman@23342: lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
huffman@23342: by (induct a, induct b, simp add: add_rat of_rat_rat add_frac_eq)
huffman@23342: 
huffman@23343: lemma of_rat_minus: "of_rat (- a) = - of_rat a"
huffman@23343: by (induct a, simp add: minus_rat of_rat_rat)
huffman@23343: 
huffman@23343: lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
huffman@23343: by (simp only: diff_minus of_rat_add of_rat_minus)
huffman@23343: 
huffman@23342: lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
huffman@23342: apply (induct a, induct b, simp add: mult_rat of_rat_rat)
huffman@23342: apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
huffman@23342: done
huffman@23342: 
huffman@23342: lemma nonzero_of_rat_inverse:
huffman@23342:   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
huffman@23343: apply (rule inverse_unique [symmetric])
huffman@23343: apply (simp add: of_rat_mult [symmetric])
huffman@23342: done
huffman@23342: 
huffman@23342: lemma of_rat_inverse:
huffman@23342:   "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
huffman@23342:    inverse (of_rat a)"
huffman@23342: by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
huffman@23342: 
huffman@23342: lemma nonzero_of_rat_divide:
huffman@23342:   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
huffman@23342: by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
huffman@23342: 
huffman@23342: lemma of_rat_divide:
huffman@23342:   "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
huffman@23342:    = of_rat a / of_rat b"
huffman@23342: by (cases "b = 0", simp_all add: nonzero_of_rat_divide)
huffman@23342: 
huffman@23343: lemma of_rat_power:
huffman@23343:   "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
huffman@23343: by (induct n) (simp_all add: of_rat_mult power_Suc)
huffman@23343: 
huffman@23343: lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
huffman@23343: apply (induct a, induct b)
huffman@23343: apply (simp add: of_rat_rat eq_rat)
huffman@23343: apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23343: apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
huffman@23343: done
huffman@23343: 
huffman@23343: lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
huffman@23343: 
huffman@23343: lemma of_rat_eq_id [simp]: "of_rat = (id :: rat \<Rightarrow> rat)"
huffman@23343: proof
huffman@23343:   fix a
huffman@23343:   show "of_rat a = id a"
huffman@23343:   by (induct a)
huffman@23343:      (simp add: of_rat_rat divide_rat Fract_of_int_eq [symmetric])
huffman@23343: qed
huffman@23343: 
huffman@23343: text{*Collapse nested embeddings*}
huffman@23343: lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
huffman@23343: by (induct n) (simp_all add: of_rat_add)
huffman@23343: 
huffman@23343: lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
huffman@23343: by (cases z rule: int_diff_cases', simp add: of_rat_diff)
huffman@23343: 
huffman@23343: lemma of_rat_number_of_eq [simp]:
huffman@23343:   "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
huffman@23343: by (simp add: number_of_eq)
huffman@23343: 
paulson@14365: end




