src/HOL/Library/Fraction_Field.thy
 author Andreas Lochbihler Fri Sep 20 10:09:16 2013 +0200 (2013-09-20) changeset 53745 788730ab7da4 parent 53374 a14d2a854c02 child 54230 b1d955791529 permissions -rw-r--r--
prefer Code.abort over code_abort
```     1 (*  Title:      HOL/Library/Fraction_Field.thy
```
```     2     Author:     Amine Chaieb, University of Cambridge
```
```     3 *)
```
```     4
```
```     5 header{* A formalization of the fraction field of any integral domain;
```
```     6          generalization of theory Rat from int to any integral domain *}
```
```     7
```
```     8 theory Fraction_Field
```
```     9 imports Main
```
```    10 begin
```
```    11
```
```    12 subsection {* General fractions construction *}
```
```    13
```
```    14 subsubsection {* Construction of the type of fractions *}
```
```    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: mult_ac)
```
```    35   also from A have "a * b' = a' * b" by auto
```
```    36   also have "b'' * (a' * b) = b * (a' * b'')" by (simp add: mult_ac)
```
```    37   also from B have "a' * b'' = a'' * b'" by auto
```
```    38   also have "b * (a'' * b') = b' * (a'' * b)" by (simp add: mult_ac)
```
```    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" by (rule quotientI)
```
```    63 qed
```
```    64
```
```    65 lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
```
```    66   by (simp add: fract_def quotientI)
```
```    67
```
```    68 declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
```
```    69
```
```    70
```
```    71 subsubsection {* Representation and basic operations *}
```
```    72
```
```    73 definition Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
```
```    74   "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
```
```    75
```
```    76 code_datatype Fract
```
```    77
```
```    78 lemma Fract_cases [cases type: fract]:
```
```    79   obtains (Fract) a b where "q = Fract a b" "b \<noteq> 0"
```
```    80   by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
```
```    81
```
```    82 lemma Fract_induct [case_names Fract, induct type: fract]:
```
```    83   shows "(\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)) \<Longrightarrow> P q"
```
```    84   by (cases q) simp
```
```    85
```
```    86 lemma eq_fract:
```
```    87   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"
```
```    88     and "\<And>a. Fract a 0 = Fract 0 1"
```
```    89     and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    90   by (simp_all add: Fract_def)
```
```    91
```
```    92 instantiation fract :: (idom) "{comm_ring_1,power}"
```
```    93 begin
```
```    94
```
```    95 definition Zero_fract_def [code_unfold]: "0 = Fract 0 1"
```
```    96
```
```    97 definition One_fract_def [code_unfold]: "1 = Fract 1 1"
```
```    98
```
```    99 definition add_fract_def:
```
```   100   "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   101     fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   102
```
```   103 lemma add_fract [simp]:
```
```   104   assumes "b \<noteq> (0::'a::idom)"
```
```   105     and "d \<noteq> 0"
```
```   106   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   107 proof -
```
```   108   have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
```
```   109     respects2 fractrel"
```
```   110     apply (rule equiv_fractrel [THEN congruent2_commuteI])
```
```   111     apply (auto simp add: algebra_simps)
```
```   112     unfolding mult_assoc[symmetric]
```
```   113     done
```
```   114   with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
```
```   115 qed
```
```   116
```
```   117 definition minus_fract_def:
```
```   118   "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
```
```   119
```
```   120 lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
```
```   121 proof -
```
```   122   have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
```
```   123     by (simp add: congruent_def split_paired_all)
```
```   124   then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
```
```   125 qed
```
```   126
```
```   127 lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
```
```   128   by (cases "b = 0") (simp_all add: eq_fract)
```
```   129
```
```   130 definition diff_fract_def: "q - r = q + - (r::'a fract)"
```
```   131
```
```   132 lemma diff_fract [simp]:
```
```   133   assumes "b \<noteq> 0" 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 diff_minus)
```
```   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     apply (rule equiv_fractrel [THEN congruent2_commuteI])
```
```   145     apply (auto simp add: algebra_simps)
```
```   146     done
```
```   147   then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
```
```   148 qed
```
```   149
```
```   150 lemma mult_fract_cancel:
```
```   151   assumes "c \<noteq> (0::'a)"
```
```   152   shows "Fract (c * a) (c * b) = Fract a b"
```
```   153 proof -
```
```   154   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
```
```   155   then show ?thesis 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" "b \<noteq> 0" "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 `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
```
```   214   with Fract `q = Fract a b` `b \<noteq> 0` show thesis by auto
```
```   215 qed
```
```   216
```
```   217
```
```   218 subsubsection {* The field of rational numbers *}
```
```   219
```
```   220 context idom
```
```   221 begin
```
```   222
```
```   223 subclass ring_no_zero_divisors ..
```
```   224
```
```   225 end
```
```   226
```
```   227 instantiation fract :: (idom) field_inverse_zero
```
```   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" by auto
```
```   237   have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
```
```   238     by (auto simp add: congruent_def * algebra_simps)
```
```   239   then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
```
```   240 qed
```
```   241
```
```   242 definition divide_fract_def: "q / r = q * inverse (r:: 'a fract)"
```
```   243
```
```   244 lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   245   by (simp add: divide_fract_def)
```
```   246
```
```   247 instance
```
```   248 proof
```
```   249   fix q :: "'a fract"
```
```   250   assume "q \<noteq> 0"
```
```   251   then show "inverse q * q = 1"
```
```   252     by (cases q rule: Fract_cases_nonzero)
```
```   253       (simp_all add: fract_expand eq_fract mult_commute)
```
```   254 next
```
```   255   fix q r :: "'a fract"
```
```   256   show "q / r = q * inverse r" by (simp add: divide_fract_def)
```
```   257 next
```
```   258   show "inverse 0 = (0:: 'a fract)"
```
```   259     by (simp add: fract_expand) (simp add: fract_collapse)
```
```   260 qed
```
```   261
```
```   262 end
```
```   263
```
```   264
```
```   265 subsubsection {* The ordered field of fractions over an ordered idom *}
```
```   266
```
```   267 lemma le_congruent2:
```
```   268   "(\<lambda>x y::'a \<times> 'a::linordered_idom.
```
```   269     {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
```
```   270     respects2 fractrel"
```
```   271 proof (clarsimp simp add: congruent2_def)
```
```   272   fix a b a' b' c d c' d' :: 'a
```
```   273   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   274   assume eq1: "a * b' = a' * b"
```
```   275   assume eq2: "c * d' = c' * d"
```
```   276
```
```   277   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   278   {
```
```   279     fix a b c d x :: 'a assume x: "x \<noteq> 0"
```
```   280     have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   281     proof -
```
```   282       from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
```
```   283       then have "?le a b c d =
```
```   284           ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
```
```   285         by (simp add: mult_le_cancel_right)
```
```   286       also have "... = ?le (a * x) (b * x) c d"
```
```   287         by (simp add: mult_ac)
```
```   288       finally show ?thesis .
```
```   289     qed
```
```   290   } note le_factor = this
```
```   291
```
```   292   let ?D = "b * d" and ?D' = "b' * d'"
```
```   293   from neq have D: "?D \<noteq> 0" by simp
```
```   294   from neq have "?D' \<noteq> 0" by simp
```
```   295   then have "?le a b c d = ?le (a * ?D') (b * ?D') c d"
```
```   296     by (rule le_factor)
```
```   297   also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
```
```   298     by (simp add: mult_ac)
```
```   299   also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
```
```   300     by (simp only: eq1 eq2)
```
```   301   also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
```
```   302     by (simp add: mult_ac)
```
```   303   also from D have "... = ?le a' b' c' d'"
```
```   304     by (rule le_factor [symmetric])
```
```   305   finally show "?le a b c d = ?le a' b' c' d'" .
```
```   306 qed
```
```   307
```
```   308 instantiation fract :: (linordered_idom) linorder
```
```   309 begin
```
```   310
```
```   311 definition le_fract_def:
```
```   312   "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   313     {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
```
```   314
```
```   315 definition less_fract_def: "z < (w::'a fract) \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
```
```   316
```
```   317 lemma le_fract [simp]:
```
```   318   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   319   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   320   by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
```
```   321
```
```   322 lemma less_fract [simp]:
```
```   323   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   324   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
```
```   325   by (simp add: less_fract_def less_le_not_le mult_ac assms)
```
```   326
```
```   327 instance
```
```   328 proof
```
```   329   fix q r s :: "'a fract"
```
```   330   assume "q \<le> r" and "r \<le> s" thus "q \<le> s"
```
```   331   proof (induct q, induct r, induct s)
```
```   332     fix a b c d e f :: 'a
```
```   333     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   334     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
```
```   335     show "Fract a b \<le> Fract e f"
```
```   336     proof -
```
```   337       from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   338         by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   339       have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   340       proof -
```
```   341         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   342           by simp
```
```   343         with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   344       qed
```
```   345       also have "... = (c * f) * (d * f) * (b * b)"
```
```   346         by (simp only: mult_ac)
```
```   347       also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   348       proof -
```
```   349         from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   350           by simp
```
```   351         with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   352       qed
```
```   353       finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   354         by (simp only: mult_ac)
```
```   355       with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   356         by (simp add: mult_le_cancel_right)
```
```   357       with neq show ?thesis by simp
```
```   358     qed
```
```   359   qed
```
```   360 next
```
```   361   fix q r :: "'a fract"
```
```   362   assume "q \<le> r" and "r \<le> q" thus "q = r"
```
```   363   proof (induct q, induct r)
```
```   364     fix a b c d :: 'a
```
```   365     assume neq: "b \<noteq> 0"  "d \<noteq> 0"
```
```   366     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
```
```   367     show "Fract a b = Fract c d"
```
```   368     proof -
```
```   369       from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   370         by simp
```
```   371       also have "... \<le> (a * d) * (b * d)"
```
```   372       proof -
```
```   373         from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   374           by simp
```
```   375         thus ?thesis by (simp only: mult_ac)
```
```   376       qed
```
```   377       finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   378       moreover from neq have "b * d \<noteq> 0" by simp
```
```   379       ultimately have "a * d = c * b" by simp
```
```   380       with neq show ?thesis by (simp add: eq_fract)
```
```   381     qed
```
```   382   qed
```
```   383 next
```
```   384   fix q r :: "'a fract"
```
```   385   show "q \<le> q"
```
```   386     by (induct q) simp
```
```   387   show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
```
```   388     by (simp only: less_fract_def)
```
```   389   show "q \<le> r \<or> r \<le> q"
```
```   390     by (induct q, induct r)
```
```   391        (simp add: mult_commute, rule linorder_linear)
```
```   392 qed
```
```   393
```
```   394 end
```
```   395
```
```   396 instantiation fract :: (linordered_idom) "{distrib_lattice, abs_if, sgn_if}"
```
```   397 begin
```
```   398
```
```   399 definition abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
```
```   400
```
```   401 definition sgn_fract_def:
```
```   402   "sgn (q::'a fract) = (if q=0 then 0 else if 0<q then 1 else - 1)"
```
```   403
```
```   404 theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   405   by (auto simp add: abs_fract_def Zero_fract_def le_less
```
```   406       eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
```
```   407
```
```   408 definition inf_fract_def:
```
```   409   "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
```
```   410
```
```   411 definition sup_fract_def:
```
```   412   "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
```
```   413
```
```   414 instance
```
```   415   by intro_classes
```
```   416     (auto simp add: abs_fract_def sgn_fract_def
```
```   417       min_max.sup_inf_distrib1 inf_fract_def sup_fract_def)
```
```   418
```
```   419 end
```
```   420
```
```   421 instance fract :: (linordered_idom) linordered_field_inverse_zero
```
```   422 proof
```
```   423   fix q r s :: "'a fract"
```
```   424   assume "q \<le> r"
```
```   425   then show "s + q \<le> s + r"
```
```   426   proof (induct q, induct r, induct s)
```
```   427     fix a b c d e f :: 'a
```
```   428     assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0"
```
```   429     assume le: "Fract a b \<le> Fract c d"
```
```   430     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   431     proof -
```
```   432       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   433         by (auto simp add: zero_less_mult_iff)
```
```   434       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   435         by simp
```
```   436       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   437         by (simp add: mult_le_cancel_right)
```
```   438       with neq show ?thesis by (simp add: field_simps)
```
```   439     qed
```
```   440   qed
```
```   441 next
```
```   442   fix q r s :: "'a fract"
```
```   443   assume "q < r" and "0 < s"
```
```   444   then show "s * q < s * r"
```
```   445   proof (induct q, induct r, induct s)
```
```   446     fix a b c d e f :: 'a
```
```   447     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   448     assume le: "Fract a b < Fract c d"
```
```   449     assume gt: "0 < Fract e f"
```
```   450     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   451     proof -
```
```   452       let ?E = "e * f" and ?F = "f * f"
```
```   453       from neq gt have "0 < ?E"
```
```   454         by (auto simp add: Zero_fract_def order_less_le eq_fract)
```
```   455       moreover from neq have "0 < ?F"
```
```   456         by (auto simp add: zero_less_mult_iff)
```
```   457       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   458         by simp
```
```   459       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   460         by (simp add: mult_less_cancel_right)
```
```   461       with neq show ?thesis
```
```   462         by (simp add: mult_ac)
```
```   463     qed
```
```   464   qed
```
```   465 qed
```
```   466
```
```   467 lemma fract_induct_pos [case_names Fract]:
```
```   468   fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
```
```   469   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
```
```   470   shows "P q"
```
```   471 proof (cases q)
```
```   472   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
```
```   473   proof -
```
```   474     fix a::'a and b::'a
```
```   475     assume b: "b < 0"
```
```   476     then have "0 < -b" by simp
```
```   477     then have "P (Fract (-a) (-b))" by (rule step)
```
```   478     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
```
```   479   qed
```
```   480   case (Fract a b)
```
```   481   thus "P q" by (force simp add: linorder_neq_iff step step')
```
```   482 qed
```
```   483
```
```   484 lemma zero_less_Fract_iff: "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
```
```   485   by (auto simp add: Zero_fract_def zero_less_mult_iff)
```
```   486
```
```   487 lemma Fract_less_zero_iff: "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
```
```   488   by (auto simp add: Zero_fract_def mult_less_0_iff)
```
```   489
```
```   490 lemma zero_le_Fract_iff: "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
```
```   491   by (auto simp add: Zero_fract_def zero_le_mult_iff)
```
```   492
```
```   493 lemma Fract_le_zero_iff: "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
```
```   494   by (auto simp add: Zero_fract_def mult_le_0_iff)
```
```   495
```
```   496 lemma one_less_Fract_iff: "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
```
```   497   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   498
```
```   499 lemma Fract_less_one_iff: "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
```
```   500   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   501
```
```   502 lemma one_le_Fract_iff: "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
```
```   503   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   504
```
```   505 lemma Fract_le_one_iff: "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
```
```   506   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   507
```
```   508 end
```