src/HOL/Library/Fraction_Field.thy
 author wenzelm Tue Feb 21 16:28:18 2012 +0100 (2012-02-21) changeset 46573 8c4c5c8dcf7a parent 45694 4a8743618257 child 47252 3a096e7a1871 permissions -rw-r--r--
misc tuning;
more indentation;
```     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 (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 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 [case_names Fract, cases type: fract]:
```
```    79   assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
```
```    80   shows C
```
```    81   using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
```
```    82
```
```    83 lemma Fract_induct [case_names Fract, induct type: fract]:
```
```    84   assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
```
```    85   shows "P q"
```
```    86   using assms by (cases q) simp
```
```    87
```
```    88 lemma eq_fract:
```
```    89   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"
```
```    90   and "\<And>a. Fract a 0 = Fract 0 1"
```
```    91   and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    92   by (simp_all add: Fract_def)
```
```    93
```
```    94 instantiation fract :: (idom) "{comm_ring_1, power}"
```
```    95 begin
```
```    96
```
```    97 definition Zero_fract_def [code_unfold]: "0 = Fract 0 1"
```
```    98
```
```    99 definition One_fract_def [code_unfold]: "1 = Fract 1 1"
```
```   100
```
```   101 definition add_fract_def:
```
```   102   "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   103     fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   104
```
```   105 lemma add_fract [simp]:
```
```   106   assumes "b \<noteq> (0::'a::idom)" 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)})
```
```   110     respects2 fractrel"
```
```   111   apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
```
```   112   unfolding mult_assoc[symmetric] .
```
```   113   with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
```
```   114 qed
```
```   115
```
```   116 definition minus_fract_def:
```
```   117   "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
```
```   118
```
```   119 lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
```
```   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" and "d \<noteq> 0"
```
```   133   shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
```
```   134   using assms by (simp add: diff_fract_def diff_minus)
```
```   135
```
```   136 definition mult_fract_def:
```
```   137   "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
```
```   138     fractrel``{(fst x * fst y, snd x * snd y)})"
```
```   139
```
```   140 lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
```
```   141 proof -
```
```   142   have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
```
```   143     apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
```
```   144     unfolding mult_assoc[symmetric] .
```
```   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"
```
```   150   shows "Fract (c * a) (c * b) = Fract a b"
```
```   151 proof -
```
```   152   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
```
```   153   then show ?thesis by (simp add: mult_fract [symmetric])
```
```   154 qed
```
```   155
```
```   156 instance proof
```
```   157   fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)"
```
```   158     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   159 next
```
```   160   fix q r :: "'a fract" show "q * r = r * q"
```
```   161     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   162 next
```
```   163   fix q :: "'a fract" show "1 * q = q"
```
```   164     by (cases q) (simp add: One_fract_def eq_fract)
```
```   165 next
```
```   166   fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
```
```   167     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   168 next
```
```   169   fix q r :: "'a fract" show "q + r = r + q"
```
```   170     by (cases q, cases r) (simp add: eq_fract algebra_simps)
```
```   171 next
```
```   172   fix q :: "'a fract" show "0 + q = q"
```
```   173     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   174 next
```
```   175   fix q :: "'a fract" show "- q + q = 0"
```
```   176     by (cases q) (simp add: Zero_fract_def eq_fract)
```
```   177 next
```
```   178   fix q r :: "'a fract" show "q - r = q + - r"
```
```   179     by (cases q, cases r) (simp add: eq_fract)
```
```   180 next
```
```   181   fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
```
```   182     by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
```
```   183 next
```
```   184   show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
```
```   185 qed
```
```   186
```
```   187 end
```
```   188
```
```   189 lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
```
```   190   by (induct k) (simp_all add: Zero_fract_def One_fract_def)
```
```   191
```
```   192 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   193   by (rule of_nat_fract [symmetric])
```
```   194
```
```   195 lemma fract_collapse [code_post]:
```
```   196   "Fract 0 k = 0"
```
```   197   "Fract 1 1 = 1"
```
```   198   "Fract k 0 = 0"
```
```   199   by (cases "k = 0")
```
```   200     (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
```
```   201
```
```   202 lemma fract_expand [code_unfold]:
```
```   203   "0 = Fract 0 1"
```
```   204   "1 = Fract 1 1"
```
```   205   by (simp_all add: fract_collapse)
```
```   206
```
```   207 lemma Fract_cases_nonzero [case_names Fract 0]:
```
```   208   assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
```
```   209   assumes 0: "q = 0 \<Longrightarrow> C"
```
```   210   shows C
```
```   211 proof (cases "q = 0")
```
```   212   case True then show C using 0 by auto
```
```   213 next
```
```   214   case False
```
```   215   then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
```
```   216   moreover with False have "0 \<noteq> Fract a b" by simp
```
```   217   with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
```
```   218   with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
```
```   219 qed
```
```   220
```
```   221
```
```   222
```
```   223 subsubsection {* The field of rational numbers *}
```
```   224
```
```   225 context idom
```
```   226 begin
```
```   227 subclass ring_no_zero_divisors ..
```
```   228 thm mult_eq_0_iff
```
```   229 end
```
```   230
```
```   231 instantiation fract :: (idom) field_inverse_zero
```
```   232 begin
```
```   233
```
```   234 definition inverse_fract_def:
```
```   235   "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
```
```   236      fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
```
```   237
```
```   238
```
```   239 lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
```
```   240 proof -
```
```   241   have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
```
```   242   have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
```
```   243     by (auto simp add: congruent_def stupid algebra_simps)
```
```   244   then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
```
```   245 qed
```
```   246
```
```   247 definition divide_fract_def: "q / r = q * inverse (r:: 'a fract)"
```
```   248
```
```   249 lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   250   by (simp add: divide_fract_def)
```
```   251
```
```   252 instance proof
```
```   253   fix q :: "'a fract"
```
```   254   assume "q \<noteq> 0"
```
```   255   then show "inverse q * q = 1"
```
```   256     by (cases q rule: Fract_cases_nonzero)
```
```   257       (simp_all add: fract_expand eq_fract mult_commute)
```
```   258 next
```
```   259   fix q r :: "'a fract"
```
```   260   show "q / r = q * inverse r" by (simp add: divide_fract_def)
```
```   261 next
```
```   262   show "inverse 0 = (0:: 'a fract)"
```
```   263     by (simp add: fract_expand) (simp add: fract_collapse)
```
```   264 qed
```
```   265
```
```   266 end
```
```   267
```
```   268
```
```   269 subsubsection {* The ordered field of fractions over an ordered idom *}
```
```   270
```
```   271 lemma le_congruent2:
```
```   272   "(\<lambda>x y::'a \<times> 'a::linordered_idom.
```
```   273     {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
```
```   274     respects2 fractrel"
```
```   275 proof (clarsimp simp add: congruent2_def)
```
```   276   fix a b a' b' c d c' d' :: 'a
```
```   277   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   278   assume eq1: "a * b' = a' * b"
```
```   279   assume eq2: "c * d' = c' * d"
```
```   280
```
```   281   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   282   {
```
```   283     fix a b c d x :: 'a assume x: "x \<noteq> 0"
```
```   284     have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   285     proof -
```
```   286       from x have "0 < x * x" 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: mult_ac)
```
```   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: mult_ac)
```
```   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: mult_ac)
```
```   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" and "d \<noteq> 0"
```
```   323   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   324 by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
```
```   325
```
```   326 lemma less_fract [simp]:
```
```   327   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   328   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
```
```   329 by (simp add: less_fract_def less_le_not_le mult_ac assms)
```
```   330
```
```   331 instance proof
```
```   332   fix q r s :: "'a fract"
```
```   333   assume "q \<le> r" and "r \<le> s" thus "q \<le> s"
```
```   334   proof (induct q, induct r, induct s)
```
```   335     fix a b c d e f :: 'a
```
```   336     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   337     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
```
```   338     show "Fract a b \<le> Fract e f"
```
```   339     proof -
```
```   340       from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   341         by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   342       have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   343       proof -
```
```   344         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   345           by simp
```
```   346         with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   347       qed
```
```   348       also have "... = (c * f) * (d * f) * (b * b)"
```
```   349         by (simp only: mult_ac)
```
```   350       also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   351       proof -
```
```   352         from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   353           by simp
```
```   354         with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   355       qed
```
```   356       finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   357         by (simp only: mult_ac)
```
```   358       with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   359         by (simp add: mult_le_cancel_right)
```
```   360       with neq show ?thesis by simp
```
```   361     qed
```
```   362   qed
```
```   363 next
```
```   364   fix q r :: "'a fract"
```
```   365   assume "q \<le> r" and "r \<le> q" thus "q = r"
```
```   366   proof (induct q, induct r)
```
```   367     fix a b c d :: 'a
```
```   368     assume neq: "b \<noteq> 0"  "d \<noteq> 0"
```
```   369     assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
```
```   370     show "Fract a b = Fract c d"
```
```   371     proof -
```
```   372       from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   373         by simp
```
```   374       also have "... \<le> (a * d) * (b * d)"
```
```   375       proof -
```
```   376         from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   377           by simp
```
```   378         thus ?thesis by (simp only: mult_ac)
```
```   379       qed
```
```   380       finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   381       moreover from neq have "b * d \<noteq> 0" by simp
```
```   382       ultimately have "a * d = c * b" by simp
```
```   383       with neq show ?thesis by (simp add: eq_fract)
```
```   384     qed
```
```   385   qed
```
```   386 next
```
```   387   fix q r :: "'a fract"
```
```   388   show "q \<le> q"
```
```   389     by (induct q) simp
```
```   390   show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
```
```   391     by (simp only: less_fract_def)
```
```   392   show "q \<le> r \<or> r \<le> q"
```
```   393     by (induct q, induct r)
```
```   394        (simp add: mult_commute, rule linorder_linear)
```
```   395 qed
```
```   396
```
```   397 end
```
```   398
```
```   399 instantiation fract :: (linordered_idom) "{distrib_lattice, abs_if, sgn_if}"
```
```   400 begin
```
```   401
```
```   402 definition abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
```
```   403
```
```   404 definition sgn_fract_def:
```
```   405   "sgn (q::'a fract) = (if q=0 then 0 else if 0<q then 1 else - 1)"
```
```   406
```
```   407 theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   408   by (auto simp add: abs_fract_def Zero_fract_def le_less
```
```   409       eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
```
```   410
```
```   411 definition inf_fract_def:
```
```   412   "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
```
```   413
```
```   414 definition sup_fract_def:
```
```   415   "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
```
```   416
```
```   417 instance
```
```   418   by intro_classes
```
```   419     (auto simp add: abs_fract_def sgn_fract_def
```
```   420       min_max.sup_inf_distrib1 inf_fract_def sup_fract_def)
```
```   421
```
```   422 end
```
```   423
```
```   424 instance fract :: (linordered_idom) linordered_field_inverse_zero
```
```   425 proof
```
```   426   fix q r s :: "'a fract"
```
```   427   show "q \<le> r ==> s + q \<le> s + r"
```
```   428   proof (induct q, induct r, induct s)
```
```   429     fix a b c d e f :: 'a
```
```   430     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   431     assume le: "Fract a b \<le> Fract c d"
```
```   432     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   433     proof -
```
```   434       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   435         by (auto simp add: zero_less_mult_iff)
```
```   436       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   437         by simp
```
```   438       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   439         by (simp add: mult_le_cancel_right)
```
```   440       with neq show ?thesis by (simp add: field_simps)
```
```   441     qed
```
```   442   qed
```
```   443   show "q < r ==> 0 < s ==> s * q < s * r"
```
```   444   proof (induct q, induct r, induct s)
```
```   445     fix a b c d e f :: 'a
```
```   446     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   447     assume le: "Fract a b < Fract c d"
```
```   448     assume gt: "0 < Fract e f"
```
```   449     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   450     proof -
```
```   451       let ?E = "e * f" and ?F = "f * f"
```
```   452       from neq gt have "0 < ?E"
```
```   453         by (auto simp add: Zero_fract_def order_less_le eq_fract)
```
```   454       moreover from neq have "0 < ?F"
```
```   455         by (auto simp add: zero_less_mult_iff)
```
```   456       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   457         by simp
```
```   458       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   459         by (simp add: mult_less_cancel_right)
```
```   460       with neq show ?thesis
```
```   461         by (simp add: mult_ac)
```
```   462     qed
```
```   463   qed
```
```   464 qed
```
```   465
```
```   466 lemma fract_induct_pos [case_names Fract]:
```
```   467   fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
```
```   468   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
```
```   469   shows "P q"
```
```   470 proof (cases q)
```
```   471   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
```
```   472   proof -
```
```   473     fix a::'a and b::'a
```
```   474     assume b: "b < 0"
```
```   475     then have "0 < -b" by simp
```
```   476     then have "P (Fract (-a) (-b))" by (rule step)
```
```   477     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
```
```   478   qed
```
```   479   case (Fract a b)
```
```   480   thus "P q" by (force simp add: linorder_neq_iff step step')
```
```   481 qed
```
```   482
```
```   483 lemma zero_less_Fract_iff:
```
```   484   "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:
```
```   488   "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
```
```   489   by (auto simp add: Zero_fract_def mult_less_0_iff)
```
```   490
```
```   491 lemma zero_le_Fract_iff:
```
```   492   "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
```
```   493   by (auto simp add: Zero_fract_def zero_le_mult_iff)
```
```   494
```
```   495 lemma Fract_le_zero_iff:
```
```   496   "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
```
```   497   by (auto simp add: Zero_fract_def mult_le_0_iff)
```
```   498
```
```   499 lemma one_less_Fract_iff:
```
```   500   "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
```
```   501   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   502
```
```   503 lemma Fract_less_one_iff:
```
```   504   "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
```
```   505   by (auto simp add: One_fract_def mult_less_cancel_right_disj)
```
```   506
```
```   507 lemma one_le_Fract_iff:
```
```   508   "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
```
```   509   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   510
```
```   511 lemma Fract_le_one_iff:
```
```   512   "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
```
```   513   by (auto simp add: One_fract_def mult_le_cancel_right)
```
```   514
```
```   515 end
```