src/HOL/Library/Fraction_Field.thy
changeset 36331 6b9e487450ba
parent 36312 26eea417ccc4
child 36348 89c54f51f55a
     1.1 --- a/src/HOL/Library/Fraction_Field.thy	Fri Apr 23 23:42:46 2010 +0200
     1.2 +++ b/src/HOL/Library/Fraction_Field.thy	Sat Apr 24 09:34:36 2010 -0700
     1.3 @@ -274,4 +274,255 @@
     1.4  qed
     1.5  
     1.6  
     1.7 -end
     1.8 \ No newline at end of file
     1.9 +subsubsection {* The ordered field of fractions over an ordered idom *}
    1.10 +
    1.11 +lemma le_congruent2:
    1.12 +  "(\<lambda>x y::'a \<times> 'a::linordered_idom.
    1.13 +    {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
    1.14 +    respects2 fractrel"
    1.15 +proof (clarsimp simp add: congruent2_def)
    1.16 +  fix a b a' b' c d c' d' :: 'a
    1.17 +  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
    1.18 +  assume eq1: "a * b' = a' * b"
    1.19 +  assume eq2: "c * d' = c' * d"
    1.20 +
    1.21 +  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
    1.22 +  {
    1.23 +    fix a b c d x :: 'a assume x: "x \<noteq> 0"
    1.24 +    have "?le a b c d = ?le (a * x) (b * x) c d"
    1.25 +    proof -
    1.26 +      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
    1.27 +      hence "?le a b c d =
    1.28 +          ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
    1.29 +        by (simp add: mult_le_cancel_right)
    1.30 +      also have "... = ?le (a * x) (b * x) c d"
    1.31 +        by (simp add: mult_ac)
    1.32 +      finally show ?thesis .
    1.33 +    qed
    1.34 +  } note le_factor = this
    1.35 +
    1.36 +  let ?D = "b * d" and ?D' = "b' * d'"
    1.37 +  from neq have D: "?D \<noteq> 0" by simp
    1.38 +  from neq have "?D' \<noteq> 0" by simp
    1.39 +  hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
    1.40 +    by (rule le_factor)
    1.41 +  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
    1.42 +    by (simp add: mult_ac)
    1.43 +  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
    1.44 +    by (simp only: eq1 eq2)
    1.45 +  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
    1.46 +    by (simp add: mult_ac)
    1.47 +  also from D have "... = ?le a' b' c' d'"
    1.48 +    by (rule le_factor [symmetric])
    1.49 +  finally show "?le a b c d = ?le a' b' c' d'" .
    1.50 +qed
    1.51 +
    1.52 +instantiation fract :: (linordered_idom) linorder
    1.53 +begin
    1.54 +
    1.55 +definition
    1.56 +  le_fract_def [code del]:
    1.57 +   "q \<le> r \<longleftrightarrow> contents (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
    1.58 +      {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
    1.59 +
    1.60 +definition
    1.61 +  less_fract_def [code del]: "z < (w::'a fract) \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
    1.62 +
    1.63 +lemma le_fract [simp]:
    1.64 +  assumes "b \<noteq> 0" and "d \<noteq> 0"
    1.65 +  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
    1.66 +by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
    1.67 +
    1.68 +lemma less_fract [simp]:
    1.69 +  assumes "b \<noteq> 0" and "d \<noteq> 0"
    1.70 +  shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
    1.71 +by (simp add: less_fract_def less_le_not_le mult_ac assms)
    1.72 +
    1.73 +instance proof
    1.74 +  fix q r s :: "'a fract"
    1.75 +  assume "q \<le> r" and "r \<le> s" thus "q \<le> s"
    1.76 +  proof (induct q, induct r, induct s)
    1.77 +    fix a b c d e f :: 'a
    1.78 +    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
    1.79 +    assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
    1.80 +    show "Fract a b \<le> Fract e f"
    1.81 +    proof -
    1.82 +      from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
    1.83 +        by (auto simp add: zero_less_mult_iff linorder_neq_iff)
    1.84 +      have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
    1.85 +      proof -
    1.86 +        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
    1.87 +          by simp
    1.88 +        with ff show ?thesis by (simp add: mult_le_cancel_right)
    1.89 +      qed
    1.90 +      also have "... = (c * f) * (d * f) * (b * b)"
    1.91 +        by (simp only: mult_ac)
    1.92 +      also have "... \<le> (e * d) * (d * f) * (b * b)"
    1.93 +      proof -
    1.94 +        from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
    1.95 +          by simp
    1.96 +        with bb show ?thesis by (simp add: mult_le_cancel_right)
    1.97 +      qed
    1.98 +      finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
    1.99 +        by (simp only: mult_ac)
   1.100 +      with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
   1.101 +        by (simp add: mult_le_cancel_right)
   1.102 +      with neq show ?thesis by simp
   1.103 +    qed
   1.104 +  qed
   1.105 +next
   1.106 +  fix q r :: "'a fract"
   1.107 +  assume "q \<le> r" and "r \<le> q" thus "q = r"
   1.108 +  proof (induct q, induct r)
   1.109 +    fix a b c d :: 'a
   1.110 +    assume neq: "b \<noteq> 0"  "d \<noteq> 0"
   1.111 +    assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
   1.112 +    show "Fract a b = Fract c d"
   1.113 +    proof -
   1.114 +      from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.115 +        by simp
   1.116 +      also have "... \<le> (a * d) * (b * d)"
   1.117 +      proof -
   1.118 +        from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
   1.119 +          by simp
   1.120 +        thus ?thesis by (simp only: mult_ac)
   1.121 +      qed
   1.122 +      finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
   1.123 +      moreover from neq have "b * d \<noteq> 0" by simp
   1.124 +      ultimately have "a * d = c * b" by simp
   1.125 +      with neq show ?thesis by (simp add: eq_fract)
   1.126 +    qed
   1.127 +  qed
   1.128 +next
   1.129 +  fix q r :: "'a fract"
   1.130 +  show "q \<le> q"
   1.131 +    by (induct q) simp
   1.132 +  show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
   1.133 +    by (simp only: less_fract_def)
   1.134 +  show "q \<le> r \<or> r \<le> q"
   1.135 +    by (induct q, induct r)
   1.136 +       (simp add: mult_commute, rule linorder_linear)
   1.137 +qed
   1.138 +
   1.139 +end
   1.140 +
   1.141 +instantiation fract :: (linordered_idom) "{distrib_lattice, abs_if, sgn_if}"
   1.142 +begin
   1.143 +
   1.144 +definition
   1.145 +  abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
   1.146 +
   1.147 +definition
   1.148 +  sgn_fract_def:
   1.149 +    "sgn (q::'a fract) = (if q=0 then 0 else if 0<q then 1 else - 1)"
   1.150 +
   1.151 +theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
   1.152 +  by (auto simp add: abs_fract_def Zero_fract_def le_less
   1.153 +      eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
   1.154 +
   1.155 +definition
   1.156 +  inf_fract_def:
   1.157 +    "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
   1.158 +
   1.159 +definition
   1.160 +  sup_fract_def:
   1.161 +    "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
   1.162 +
   1.163 +instance by intro_classes
   1.164 +  (auto simp add: abs_fract_def sgn_fract_def
   1.165 +    min_max.sup_inf_distrib1 inf_fract_def sup_fract_def)
   1.166 +
   1.167 +end
   1.168 +
   1.169 +instance fract :: (linordered_idom) linordered_field
   1.170 +proof
   1.171 +  fix q r s :: "'a fract"
   1.172 +  show "q \<le> r ==> s + q \<le> s + r"
   1.173 +  proof (induct q, induct r, induct s)
   1.174 +    fix a b c d e f :: 'a
   1.175 +    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
   1.176 +    assume le: "Fract a b \<le> Fract c d"
   1.177 +    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
   1.178 +    proof -
   1.179 +      let ?F = "f * f" from neq have F: "0 < ?F"
   1.180 +        by (auto simp add: zero_less_mult_iff)
   1.181 +      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.182 +        by simp
   1.183 +      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
   1.184 +        by (simp add: mult_le_cancel_right)
   1.185 +      with neq show ?thesis by (simp add: ring_simps)
   1.186 +    qed
   1.187 +  qed
   1.188 +  show "q < r ==> 0 < s ==> s * q < s * r"
   1.189 +  proof (induct q, induct r, induct s)
   1.190 +    fix a b c d e f :: 'a
   1.191 +    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
   1.192 +    assume le: "Fract a b < Fract c d"
   1.193 +    assume gt: "0 < Fract e f"
   1.194 +    show "Fract e f * Fract a b < Fract e f * Fract c d"
   1.195 +    proof -
   1.196 +      let ?E = "e * f" and ?F = "f * f"
   1.197 +      from neq gt have "0 < ?E"
   1.198 +        by (auto simp add: Zero_fract_def order_less_le eq_fract)
   1.199 +      moreover from neq have "0 < ?F"
   1.200 +        by (auto simp add: zero_less_mult_iff)
   1.201 +      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
   1.202 +        by simp
   1.203 +      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
   1.204 +        by (simp add: mult_less_cancel_right)
   1.205 +      with neq show ?thesis
   1.206 +        by (simp add: mult_ac)
   1.207 +    qed
   1.208 +  qed
   1.209 +qed
   1.210 +
   1.211 +lemma fract_induct_pos [case_names Fract]:
   1.212 +  fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
   1.213 +  assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
   1.214 +  shows "P q"
   1.215 +proof (cases q)
   1.216 +  have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
   1.217 +  proof -
   1.218 +    fix a::'a and b::'a
   1.219 +    assume b: "b < 0"
   1.220 +    hence "0 < -b" by simp
   1.221 +    hence "P (Fract (-a) (-b))" by (rule step)
   1.222 +    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
   1.223 +  qed
   1.224 +  case (Fract a b)
   1.225 +  thus "P q" by (force simp add: linorder_neq_iff step step')
   1.226 +qed
   1.227 +
   1.228 +lemma zero_less_Fract_iff:
   1.229 +  "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
   1.230 +  by (auto simp add: Zero_fract_def zero_less_mult_iff)
   1.231 +
   1.232 +lemma Fract_less_zero_iff:
   1.233 +  "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
   1.234 +  by (auto simp add: Zero_fract_def mult_less_0_iff)
   1.235 +
   1.236 +lemma zero_le_Fract_iff:
   1.237 +  "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
   1.238 +  by (auto simp add: Zero_fract_def zero_le_mult_iff)
   1.239 +
   1.240 +lemma Fract_le_zero_iff:
   1.241 +  "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
   1.242 +  by (auto simp add: Zero_fract_def mult_le_0_iff)
   1.243 +
   1.244 +lemma one_less_Fract_iff:
   1.245 +  "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
   1.246 +  by (auto simp add: One_fract_def mult_less_cancel_right_disj)
   1.247 +
   1.248 +lemma Fract_less_one_iff:
   1.249 +  "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
   1.250 +  by (auto simp add: One_fract_def mult_less_cancel_right_disj)
   1.251 +
   1.252 +lemma one_le_Fract_iff:
   1.253 +  "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
   1.254 +  by (auto simp add: One_fract_def mult_le_cancel_right)
   1.255 +
   1.256 +lemma Fract_le_one_iff:
   1.257 +  "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
   1.258 +  by (auto simp add: One_fract_def mult_le_cancel_right)
   1.259 +
   1.260 +end