src/HOL/Library/Fraction_Field.thy
 author wenzelm Thu Feb 16 22:53:24 2012 +0100 (2012-02-16) changeset 46507 1b24c24017dd parent 45694 4a8743618257 child 46573 8c4c5c8dcf7a permissions -rw-r--r--
tuned proofs;
```     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 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 (open) '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
```
```    74   Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
```
```    75   "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 [case_names Fract, cases type: fract]:
```
```    80   assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
```
```    81   shows C
```
```    82   using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
```
```    83
```
```    84 lemma Fract_induct [case_names Fract, induct type: fract]:
```
```    85   assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
```
```    86   shows "P q"
```
```    87   using assms by (cases q) simp
```
```    88
```
```    89 lemma eq_fract:
```
```    90   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"
```
```    91   and "\<And>a. Fract a 0 = Fract 0 1"
```
```    92   and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    93   by (simp_all add: Fract_def)
```
```    94
```
```    95 instantiation fract :: (idom) "{comm_ring_1, power}"
```
```    96 begin
```
```    97
```
```    98 definition
```
```    99   Zero_fract_def [code_unfold]: "0 = Fract 0 1"
```
```   100
```
```   101 definition
```
```   102   One_fract_def [code_unfold]: "1 = Fract 1 1"
```
```   103
```
```   104 definition
```
```   105   add_fract_def:
```
```   106   "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   107     fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   108
```
```   109 lemma add_fract [simp]:
```
```   110   assumes "b \<noteq> (0::'a::idom)" and "d \<noteq> 0"
```
```   111   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   112 proof -
```
```   113   have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
```
```   114     respects2 fractrel"
```
```   115   apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
```
```   116   unfolding mult_assoc[symmetric] .
```
```   117   with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
```
```   118 qed
```
```   119
```
```   120 definition
```
```   121   minus_fract_def:
```
```   122   "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
```
```   123
```
```   124 lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
```
```   125 proof -
```
```   126   have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
```
```   127     by (simp add: congruent_def split_paired_all)
```
```   128   then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
```
```   129 qed
```
```   130
```
```   131 lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
```
```   132   by (cases "b = 0") (simp_all add: eq_fract)
```
```   133
```
```   134 definition
```
```   135   diff_fract_def: "q - r = q + - (r::'a fract)"
```
```   136
```
```   137 lemma diff_fract [simp]:
```
```   138   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   139   shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
```
```   140   using assms by (simp add: diff_fract_def diff_minus)
```
```   141
```
```   142 definition
```
```   143   mult_fract_def:
```
```   144   "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   145     fractrel``{(fst x * fst y, snd x * snd y)})"
```
```   146
```
```   147 lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
```
```   148 proof -
```
```   149   have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
```
```   150     apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
```
```   151     unfolding mult_assoc[symmetric] .
```
```   152   then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
```
```   153 qed
```
```   154
```
```   155 lemma mult_fract_cancel:
```
```   156   assumes "c \<noteq> 0"
```
```   157   shows "Fract (c * a) (c * b) = Fract a b"
```
```   158 proof -
```
```   159   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
```
```   160   then show ?thesis by (simp add: mult_fract [symmetric])
```
```   161 qed
```
```   162
```
```   163 instance proof
```
```   164   fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)"
```
```   165     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   166 next
```
```   167   fix q r :: "'a fract" show "q * r = r * q"
```
```   168     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   169 next
```
```   170   fix q :: "'a fract" show "1 * q = q"
```
```   171     by (cases q) (simp add: One_fract_def eq_fract)
```
```   172 next
```
```   173   fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
```
```   174     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   175 next
```
```   176   fix q r :: "'a fract" show "q + r = r + q"
```
```   177     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   178 next
```
```   179   fix q :: "'a fract" show "0 + q = q"
```
```   180     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   181 next
```
```   182   fix q :: "'a fract" show "- q + q = 0"
```
```   183     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   184 next
```
```   185   fix q r :: "'a fract" show "q - r = q + - r"
```
```   186     by (cases q, cases r) (simp add: eq_fract)
```
```   187 next
```
```   188   fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
```
```   189     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   190 next
```
```   191   show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
```
```   192 qed
```
```   193
```
```   194 end
```
```   195
```
```   196 lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
```
```   197   by (induct k) (simp_all add: Zero_fract_def One_fract_def)
```
```   198
```
```   199 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   200   by (rule of_nat_fract [symmetric])
```
```   201
```
```   202 lemma fract_collapse [code_post]:
```
```   203   "Fract 0 k = 0"
```
```   204   "Fract 1 1 = 1"
```
```   205   "Fract k 0 = 0"
```
```   206   by (cases "k = 0")
```
```   207     (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
```
```   208
```
```   209 lemma fract_expand [code_unfold]:
```
```   210   "0 = Fract 0 1"
```
```   211   "1 = Fract 1 1"
```
```   212   by (simp_all add: fract_collapse)
```
```   213
```
```   214 lemma Fract_cases_nonzero [case_names Fract 0]:
```
```   215   assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
```
```   216   assumes 0: "q = 0 \<Longrightarrow> C"
```
```   217   shows C
```
```   218 proof (cases "q = 0")
```
```   219   case True then show C using 0 by auto
```
```   220 next
```
```   221   case False
```
```   222   then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
```
```   223   moreover with False have "0 \<noteq> Fract a b" by simp
```
```   224   with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
```
```   225   with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
```
```   226 qed
```
```   227
```
```   228
```
```   229
```
```   230 subsubsection {* The field of rational numbers *}
```
```   231
```
```   232 context idom
```
```   233 begin
```
```   234 subclass ring_no_zero_divisors ..
```
```   235 thm mult_eq_0_iff
```
```   236 end
```
```   237
```
```   238 instantiation fract :: (idom) field_inverse_zero
```
```   239 begin
```
```   240
```
```   241 definition
```
```   242   inverse_fract_def:
```
```   243   "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
```
```   244      fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
```
```   245
```
```   246
```
```   247 lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
```
```   248 proof -
```
```   249   have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
```
```   250   have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
```
```   251     by (auto simp add: congruent_def stupid algebra_simps)
```
```   252   then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
```
```   253 qed
```
```   254
```
```   255 definition
```
```   256   divide_fract_def: "q / r = q * inverse (r:: 'a fract)"
```
```   257
```
```   258 lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   259   by (simp add: divide_fract_def)
```
```   260
```
```   261 instance proof
```
```   262   fix q :: "'a fract"
```
```   263   assume "q \<noteq> 0"
```
```   264   then show "inverse q * q = 1" apply (cases q rule: Fract_cases_nonzero)
```
```   265     by (simp_all add: mult_fract  inverse_fract fract_expand eq_fract mult_commute)
```
```   266 next
```
```   267   fix q r :: "'a fract"
```
```   268   show "q / r = q * inverse r" by (simp add: divide_fract_def)
```
```   269 next
```
```   270   show "inverse 0 = (0:: 'a fract)" by (simp add: fract_expand)
```
```   271     (simp add: fract_collapse)
```
```   272 qed
```
```   273
```
```   274 end
```
```   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:
```
```   325    "q \<le> r \<longleftrightarrow> the_elem (\<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: "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_inverse_zero
```
```   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
```