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