src/HOL/Library/Fraction_Field.thy
 author wenzelm Wed Jun 17 11:03:05 2015 +0200 (2015-06-17) changeset 60500 903bb1495239 parent 60429 d3d1e185cd63 child 61076 bdc1e2f0a86a child 61106 5bafa612ede4 permissions -rw-r--r--
isabelle update_cartouches;
```     1 (*  Title:      HOL/Library/Fraction_Field.thy
```
```     2     Author:     Amine Chaieb, University of Cambridge
```
```     3 *)
```
```     4
```
```     5 section\<open>A formalization of the fraction field of any integral domain;
```
```     6          generalization of theory Rat from int to any integral domain\<close>
```
```     7
```
```     8 theory Fraction_Field
```
```     9 imports Main
```
```    10 begin
```
```    11
```
```    12 subsection \<open>General fractions construction\<close>
```
```    13
```
```    14 subsubsection \<open>Construction of the type of fractions\<close>
```
```    15
```
```    16 definition fractrel :: "(('a::idom * 'a ) * ('a * 'a)) set" where
```
```    17   "fractrel = {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
```
```    18
```
```    19 lemma fractrel_iff [simp]:
```
```    20   "(x, y) \<in> fractrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
```
```    21   by (simp add: fractrel_def)
```
```    22
```
```    23 lemma refl_fractrel: "refl_on {x. snd x \<noteq> 0} fractrel"
```
```    24   by (auto simp add: refl_on_def fractrel_def)
```
```    25
```
```    26 lemma sym_fractrel: "sym fractrel"
```
```    27   by (simp add: fractrel_def sym_def)
```
```    28
```
```    29 lemma trans_fractrel: "trans fractrel"
```
```    30 proof (rule transI, unfold split_paired_all)
```
```    31   fix a b a' b' a'' b'' :: 'a
```
```    32   assume A: "((a, b), (a', b')) \<in> fractrel"
```
```    33   assume B: "((a', b'), (a'', b'')) \<in> fractrel"
```
```    34   have "b' * (a * b'') = b'' * (a * b')" by (simp add: ac_simps)
```
```    35   also from A have "a * b' = a' * b" by auto
```
```    36   also have "b'' * (a' * b) = b * (a' * b'')" by (simp add: ac_simps)
```
```    37   also from B have "a' * b'' = a'' * b'" by auto
```
```    38   also have "b * (a'' * b') = b' * (a'' * b)" by (simp add: ac_simps)
```
```    39   finally have "b' * (a * b'') = b' * (a'' * b)" .
```
```    40   moreover from B have "b' \<noteq> 0" by auto
```
```    41   ultimately have "a * b'' = a'' * b" by simp
```
```    42   with A B show "((a, b), (a'', b'')) \<in> fractrel" by auto
```
```    43 qed
```
```    44
```
```    45 lemma equiv_fractrel: "equiv {x. snd x \<noteq> 0} fractrel"
```
```    46   by (rule equivI [OF refl_fractrel sym_fractrel trans_fractrel])
```
```    47
```
```    48 lemmas UN_fractrel = UN_equiv_class [OF equiv_fractrel]
```
```    49 lemmas UN_fractrel2 = UN_equiv_class2 [OF equiv_fractrel equiv_fractrel]
```
```    50
```
```    51 lemma equiv_fractrel_iff [iff]:
```
```    52   assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
```
```    53   shows "fractrel `` {x} = fractrel `` {y} \<longleftrightarrow> (x, y) \<in> fractrel"
```
```    54   by (rule eq_equiv_class_iff, rule equiv_fractrel) (auto simp add: assms)
```
```    55
```
```    56 definition "fract = {(x::'a\<times>'a). snd x \<noteq> (0::'a::idom)} // fractrel"
```
```    57
```
```    58 typedef 'a fract = "fract :: ('a * 'a::idom) set set"
```
```    59   unfolding fract_def
```
```    60 proof
```
```    61   have "(0::'a, 1::'a) \<in> {x. snd x \<noteq> 0}" by simp
```
```    62   then show "fractrel `` {(0::'a, 1)} \<in> {x. snd x \<noteq> 0} // fractrel"
```
```    63     by (rule quotientI)
```
```    64 qed
```
```    65
```
```    66 lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
```
```    67   by (simp add: fract_def quotientI)
```
```    68
```
```    69 declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
```
```    70
```
```    71
```
```    72 subsubsection \<open>Representation and basic operations\<close>
```
```    73
```
```    74 definition Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract"
```
```    75   where "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
```
```    76
```
```    77 code_datatype Fract
```
```    78
```
```    79 lemma Fract_cases [cases type: fract]:
```
```    80   obtains (Fract) a b where "q = Fract a b" "b \<noteq> 0"
```
```    81   by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
```
```    82
```
```    83 lemma Fract_induct [case_names Fract, induct type: fract]:
```
```    84   "(\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)) \<Longrightarrow> P q"
```
```    85   by (cases q) simp
```
```    86
```
```    87 lemma eq_fract:
```
```    88   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"
```
```    89     and "\<And>a. Fract a 0 = Fract 0 1"
```
```    90     and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    91   by (simp_all add: Fract_def)
```
```    92
```
```    93 instantiation fract :: (idom) "{comm_ring_1,power}"
```
```    94 begin
```
```    95
```
```    96 definition Zero_fract_def [code_unfold]: "0 = Fract 0 1"
```
```    97
```
```    98 definition One_fract_def [code_unfold]: "1 = Fract 1 1"
```
```    99
```
```   100 definition add_fract_def:
```
```   101   "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   102     fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   103
```
```   104 lemma add_fract [simp]:
```
```   105   assumes "b \<noteq> (0::'a::idom)"
```
```   106     and "d \<noteq> 0"
```
```   107   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   108 proof -
```
```   109   have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)}) respects2 fractrel"
```
```   110     by (rule equiv_fractrel [THEN congruent2_commuteI]) (simp_all add: algebra_simps)
```
```   111   with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
```
```   112 qed
```
```   113
```
```   114 definition minus_fract_def:
```
```   115   "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
```
```   116
```
```   117 lemma minus_fract [simp, code]:
```
```   118   fixes a b :: "'a::idom"
```
```   119   shows "- Fract a b = Fract (- a) b"
```
```   120 proof -
```
```   121   have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
```
```   122     by (simp add: congruent_def split_paired_all)
```
```   123   then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
```
```   124 qed
```
```   125
```
```   126 lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
```
```   127   by (cases "b = 0") (simp_all add: eq_fract)
```
```   128
```
```   129 definition diff_fract_def: "q - r = q + - (r::'a fract)"
```
```   130
```
```   131 lemma diff_fract [simp]:
```
```   132   assumes "b \<noteq> 0"
```
```   133     and "d \<noteq> 0"
```
```   134   shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
```
```   135   using assms by (simp add: diff_fract_def)
```
```   136
```
```   137 definition mult_fract_def:
```
```   138   "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   139     fractrel``{(fst x * fst y, snd x * snd y)})"
```
```   140
```
```   141 lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
```
```   142 proof -
```
```   143   have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
```
```   144     by (rule equiv_fractrel [THEN congruent2_commuteI]) (simp_all add: algebra_simps)
```
```   145   then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
```
```   146 qed
```
```   147
```
```   148 lemma mult_fract_cancel:
```
```   149   assumes "c \<noteq> (0::'a)"
```
```   150   shows "Fract (c * a) (c * b) = Fract a b"
```
```   151 proof -
```
```   152   from assms have "Fract c c = Fract 1 1"
```
```   153     by (simp add: Fract_def)
```
```   154   then show ?thesis
```
```   155     by (simp add: mult_fract [symmetric])
```
```   156 qed
```
```   157
```
```   158 instance
```
```   159 proof
```
```   160   fix q r s :: "'a fract"
```
```   161   show "(q * r) * s = q * (r * s)"
```
```   162     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   163   show "q * r = r * q"
```
```   164     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   165   show "1 * q = q"
```
```   166     by (cases q) (simp add: One_fract_def eq_fract)
```
```   167   show "(q + r) + s = q + (r + s)"
```
```   168     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   169   show "q + r = r + q"
```
```   170     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   171   show "0 + q = q"
```
```   172     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   173   show "- q + q = 0"
```
```   174     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   175   show "q - r = q + - r"
```
```   176     by (cases q, cases r) (simp add: eq_fract)
```
```   177   show "(q + r) * s = q * s + r * s"
```
```   178     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   179   show "(0::'a fract) \<noteq> 1"
```
```   180     by (simp add: Zero_fract_def One_fract_def eq_fract)
```
```   181 qed
```
```   182
```
```   183 end
```
```   184
```
```   185 lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
```
```   186   by (induct k) (simp_all add: Zero_fract_def One_fract_def)
```
```   187
```
```   188 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   189   by (rule of_nat_fract [symmetric])
```
```   190
```
```   191 lemma fract_collapse [code_post]:
```
```   192   "Fract 0 k = 0"
```
```   193   "Fract 1 1 = 1"
```
```   194   "Fract k 0 = 0"
```
```   195   by (cases "k = 0")
```
```   196     (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
```
```   197
```
```   198 lemma fract_expand [code_unfold]:
```
```   199   "0 = Fract 0 1"
```
```   200   "1 = Fract 1 1"
```
```   201   by (simp_all add: fract_collapse)
```
```   202
```
```   203 lemma Fract_cases_nonzero:
```
```   204   obtains (Fract) a b where "q = Fract a b" and "b \<noteq> 0" and "a \<noteq> 0"
```
```   205     | (0) "q = 0"
```
```   206 proof (cases "q = 0")
```
```   207   case True
```
```   208   then show thesis using 0 by auto
```
```   209 next
```
```   210   case False
```
```   211   then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
```
```   212   with False have "0 \<noteq> Fract a b" by simp
```
```   213   with \<open>b \<noteq> 0\<close> have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
```
```   214   with Fract \<open>q = Fract a b\<close> \<open>b \<noteq> 0\<close> show thesis by auto
```
```   215 qed
```
```   216
```
```   217
```
```   218 subsubsection \<open>The field of rational numbers\<close>
```
```   219
```
```   220 context idom
```
```   221 begin
```
```   222
```
```   223 subclass ring_no_zero_divisors ..
```
```   224
```
```   225 end
```
```   226
```
```   227 instantiation fract :: (idom) field
```
```   228 begin
```
```   229
```
```   230 definition inverse_fract_def:
```
```   231   "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
```
```   232      fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
```
```   233
```
```   234 lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
```
```   235 proof -
```
```   236   have *: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0"
```
```   237     by auto
```
```   238   have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
```
```   239     by (auto simp add: congruent_def * algebra_simps)
```
```   240   then show ?thesis
```
```   241     by (simp add: Fract_def inverse_fract_def UN_fractrel)
```
```   242 qed
```
```   243
```
```   244 definition divide_fract_def: "q div r = q * inverse (r:: 'a fract)"
```
```   245
```
```   246 lemma divide_fract [simp]: "Fract a b div Fract c d = Fract (a * d) (b * c)"
```
```   247   by (simp add: divide_fract_def)
```
```   248
```
```   249 instance
```
```   250 proof
```
```   251   fix q :: "'a fract"
```
```   252   assume "q \<noteq> 0"
```
```   253   then show "inverse q * q = 1"
```
```   254     by (cases q rule: Fract_cases_nonzero)
```
```   255       (simp_all add: fract_expand eq_fract mult.commute)
```
```   256 next
```
```   257   fix q r :: "'a fract"
```
```   258   show "q div r = q * inverse r" by (simp add: divide_fract_def)
```
```   259 next
```
```   260   show "inverse 0 = (0:: 'a fract)"
```
```   261     by (simp add: fract_expand) (simp add: fract_collapse)
```
```   262 qed
```
```   263
```
```   264 end
```
```   265
```
```   266
```
```   267 subsubsection \<open>The ordered field of fractions over an ordered idom\<close>
```
```   268
```
```   269 lemma le_congruent2:
```
```   270   "(\<lambda>x y::'a \<times> 'a::linordered_idom.
```
```   271     {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
```
```   272     respects2 fractrel"
```
```   273 proof (clarsimp simp add: congruent2_def)
```
```   274   fix a b a' b' c d c' d' :: 'a
```
```   275   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   276   assume eq1: "a * b' = a' * b"
```
```   277   assume eq2: "c * d' = c' * d"
```
```   278
```
```   279   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   280   {
```
```   281     fix a b c d x :: 'a
```
```   282     assume x: "x \<noteq> 0"
```
```   283     have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   284     proof -
```
```   285       from x have "0 < x * x"
```
```   286         by (auto simp add: zero_less_mult_iff)
```
```   287       then have "?le a b c d =
```
```   288           ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
```
```   289         by (simp add: mult_le_cancel_right)
```
```   290       also have "... = ?le (a * x) (b * x) c d"
```
```   291         by (simp add: ac_simps)
```
```   292       finally show ?thesis .
```
```   293     qed
```
```   294   } note le_factor = this
```
```   295
```
```   296   let ?D = "b * d" and ?D' = "b' * d'"
```
```   297   from neq have D: "?D \<noteq> 0" by simp
```
```   298   from neq have "?D' \<noteq> 0" by simp
```
```   299   then have "?le a b c d = ?le (a * ?D') (b * ?D') c d"
```
```   300     by (rule le_factor)
```
```   301   also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
```
```   302     by (simp add: ac_simps)
```
```   303   also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
```
```   304     by (simp only: eq1 eq2)
```
```   305   also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
```
```   306     by (simp add: ac_simps)
```
```   307   also from D have "... = ?le a' b' c' d'"
```
```   308     by (rule le_factor [symmetric])
```
```   309   finally show "?le a b c d = ?le a' b' c' d'" .
```
```   310 qed
```
```   311
```
```   312 instantiation fract :: (linordered_idom) linorder
```
```   313 begin
```
```   314
```
```   315 definition le_fract_def:
```
```   316   "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   317     {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
```
```   318
```
```   319 definition less_fract_def: "z < (w::'a fract) \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
```
```   320
```
```   321 lemma le_fract [simp]:
```
```   322   assumes "b \<noteq> 0"
```
```   323     and "d \<noteq> 0"
```
```   324   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   325   by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
```
```   326
```
```   327 lemma less_fract [simp]:
```
```   328   assumes "b \<noteq> 0"
```
```   329     and "d \<noteq> 0"
```
```   330   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
```
```   331   by (simp add: less_fract_def less_le_not_le ac_simps assms)
```
```   332
```
```   333 instance
```
```   334 proof
```
```   335   fix q r s :: "'a fract"
```
```   336   assume "q \<le> r" and "r \<le> s"
```
```   337   then show "q \<le> s"
```
```   338   proof (induct q, induct r, induct s)
```
```   339     fix a b c d e f :: 'a
```
```   340     assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0"
```
```   341     assume 1: "Fract a b \<le> Fract c d"
```
```   342     assume 2: "Fract c d \<le> Fract e f"
```
```   343     show "Fract a b \<le> Fract e f"
```
```   344     proof -
```
```   345       from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   346         by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   347       have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   348       proof -
```
```   349         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   350           by simp
```
```   351         with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   352       qed
```
```   353       also have "... = (c * f) * (d * f) * (b * b)"
```
```   354         by (simp only: ac_simps)
```
```   355       also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   356       proof -
```
```   357         from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   358           by simp
```
```   359         with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   360       qed
```
```   361       finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   362         by (simp only: ac_simps)
```
```   363       with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   364         by (simp add: mult_le_cancel_right)
```
```   365       with neq show ?thesis by simp
```
```   366     qed
```
```   367   qed
```
```   368 next
```
```   369   fix q r :: "'a fract"
```
```   370   assume "q \<le> r" and "r \<le> q"
```
```   371   then show "q = r"
```
```   372   proof (induct q, induct r)
```
```   373     fix a b c d :: 'a
```
```   374     assume neq: "b \<noteq> 0" "d \<noteq> 0"
```
```   375     assume 1: "Fract a b \<le> Fract c d"
```
```   376     assume 2: "Fract c d \<le> Fract a b"
```
```   377     show "Fract a b = Fract c d"
```
```   378     proof -
```
```   379       from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   380         by simp
```
```   381       also have "... \<le> (a * d) * (b * d)"
```
```   382       proof -
```
```   383         from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   384           by simp
```
```   385         then show ?thesis by (simp only: ac_simps)
```
```   386       qed
```
```   387       finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   388       moreover from neq have "b * d \<noteq> 0" by simp
```
```   389       ultimately have "a * d = c * b" by simp
```
```   390       with neq show ?thesis by (simp add: eq_fract)
```
```   391     qed
```
```   392   qed
```
```   393 next
```
```   394   fix q r :: "'a fract"
```
```   395   show "q \<le> q"
```
```   396     by (induct q) simp
```
```   397   show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
```
```   398     by (simp only: less_fract_def)
```
```   399   show "q \<le> r \<or> r \<le> q"
```
```   400     by (induct q, induct r)
```
```   401        (simp add: mult.commute, rule linorder_linear)
```
```   402 qed
```
```   403
```
```   404 end
```
```   405
```
```   406 instantiation fract :: (linordered_idom) "{distrib_lattice,abs_if,sgn_if}"
```
```   407 begin
```
```   408
```
```   409 definition abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
```
```   410
```
```   411 definition sgn_fract_def:
```
```   412   "sgn (q::'a fract) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
```
```   413
```
```   414 theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   415   by (auto simp add: abs_fract_def Zero_fract_def le_less
```
```   416       eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
```
```   417
```
```   418 definition inf_fract_def:
```
```   419   "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
```
```   420
```
```   421 definition sup_fract_def:
```
```   422   "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
```
```   423
```
```   424 instance
```
```   425   by intro_classes
```
```   426     (auto simp add: abs_fract_def sgn_fract_def
```
```   427       max_min_distrib2 inf_fract_def sup_fract_def)
```
```   428
```
```   429 end
```
```   430
```
```   431 instance fract :: (linordered_idom) linordered_field
```
```   432 proof
```
```   433   fix q r s :: "'a fract"
```
```   434   assume "q \<le> r"
```
```   435   then show "s + q \<le> s + r"
```
```   436   proof (induct q, induct r, induct s)
```
```   437     fix a b c d e f :: 'a
```
```   438     assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0"
```
```   439     assume le: "Fract a b \<le> Fract c d"
```
```   440     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   441     proof -
```
```   442       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   443         by (auto simp add: zero_less_mult_iff)
```
```   444       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   445         by simp
```
```   446       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   447         by (simp add: mult_le_cancel_right)
```
```   448       with neq show ?thesis by (simp add: field_simps)
```
```   449     qed
```
```   450   qed
```
```   451 next
```
```   452   fix q r s :: "'a fract"
```
```   453   assume "q < r" and "0 < s"
```
```   454   then show "s * q < s * r"
```
```   455   proof (induct q, induct r, induct s)
```
```   456     fix a b c d e f :: 'a
```
```   457     assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0"
```
```   458     assume le: "Fract a b < Fract c d"
```
```   459     assume gt: "0 < Fract e f"
```
```   460     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   461     proof -
```
```   462       let ?E = "e * f" and ?F = "f * f"
```
```   463       from neq gt have "0 < ?E"
```
```   464         by (auto simp add: Zero_fract_def order_less_le eq_fract)
```
```   465       moreover from neq have "0 < ?F"
```
```   466         by (auto simp add: zero_less_mult_iff)
```
```   467       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   468         by simp
```
```   469       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   470         by (simp add: mult_less_cancel_right)
```
```   471       with neq show ?thesis
```
```   472         by (simp add: ac_simps)
```
```   473     qed
```
```   474   qed
```
```   475 qed
```
```   476
```
```   477 lemma fract_induct_pos [case_names Fract]:
```
```   478   fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
```
```   479   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
```
```   480   shows "P q"
```
```   481 proof (cases q)
```
```   482   case (Fract a b)
```
```   483   {
```
```   484     fix a b :: 'a
```
```   485     assume b: "b < 0"
```
```   486     have "P (Fract a b)"
```
```   487     proof -
```
```   488       from b have "0 < - b" by simp
```
```   489       then have "P (Fract (- a) (- b))"
```
```   490         by (rule step)
```
```   491       then show "P (Fract a b)"
```
```   492         by (simp add: order_less_imp_not_eq [OF b])
```
```   493     qed
```
```   494   }
```
```   495   with Fract show "P q"
```
```   496     by (auto simp add: linorder_neq_iff step)
```
```   497 qed
```
```   498
```
```   499 lemma zero_less_Fract_iff: "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
```
```   500   by (auto simp add: Zero_fract_def zero_less_mult_iff)
```
```   501
```
```   502 lemma Fract_less_zero_iff: "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
```
```   503   by (auto simp add: Zero_fract_def mult_less_0_iff)
```
```   504
```
```   505 lemma zero_le_Fract_iff: "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
```
```   506   by (auto simp add: Zero_fract_def zero_le_mult_iff)
```
```   507
```
```   508 lemma Fract_le_zero_iff: "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
```
```   509   by (auto simp add: Zero_fract_def mult_le_0_iff)
```
```   510
```
```   511 lemma one_less_Fract_iff: "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
```
```   512   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   513
```
```   514 lemma Fract_less_one_iff: "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
```
```   515   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   516
```
```   517 lemma one_le_Fract_iff: "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
```
```   518   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   519
```
```   520 lemma Fract_le_one_iff: "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
```
```   521   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   522
```
```   523 end
```