src/HOL/Rational.thy
 author wenzelm Mon Mar 16 18:24:30 2009 +0100 (2009-03-16) changeset 30549 d2d7874648bd parent 30273 ecd6f0ca62ea child 30649 57753e0ec1d4 permissions -rw-r--r--
simplified method setup;
```     1 (*  Title:  HOL/Rational.thy
```
```     2     Author: Markus Wenzel, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 header {* Rational numbers *}
```
```     6
```
```     7 theory Rational
```
```     8 imports GCD Archimedean_Field
```
```     9 uses ("Tools/rat_arith.ML")
```
```    10 begin
```
```    11
```
```    12 subsection {* Rational numbers as quotient *}
```
```    13
```
```    14 subsubsection {* Construction of the type of rational numbers *}
```
```    15
```
```    16 definition
```
```    17   ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
```
```    18   "ratrel = {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
```
```    19
```
```    20 lemma ratrel_iff [simp]:
```
```    21   "(x, y) \<in> ratrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
```
```    22   by (simp add: ratrel_def)
```
```    23
```
```    24 lemma refl_on_ratrel: "refl_on {x. snd x \<noteq> 0} ratrel"
```
```    25   by (auto simp add: refl_on_def ratrel_def)
```
```    26
```
```    27 lemma sym_ratrel: "sym ratrel"
```
```    28   by (simp add: ratrel_def sym_def)
```
```    29
```
```    30 lemma trans_ratrel: "trans ratrel"
```
```    31 proof (rule transI, unfold split_paired_all)
```
```    32   fix a b a' b' a'' b'' :: int
```
```    33   assume A: "((a, b), (a', b')) \<in> ratrel"
```
```    34   assume B: "((a', b'), (a'', b'')) \<in> ratrel"
```
```    35   have "b' * (a * b'') = b'' * (a * b')" by simp
```
```    36   also from A have "a * b' = a' * b" by auto
```
```    37   also have "b'' * (a' * b) = b * (a' * b'')" by simp
```
```    38   also from B have "a' * b'' = a'' * b'" by auto
```
```    39   also have "b * (a'' * b') = b' * (a'' * b)" by simp
```
```    40   finally have "b' * (a * b'') = b' * (a'' * b)" .
```
```    41   moreover from B have "b' \<noteq> 0" by auto
```
```    42   ultimately have "a * b'' = a'' * b" by simp
```
```    43   with A B show "((a, b), (a'', b'')) \<in> ratrel" by auto
```
```    44 qed
```
```    45
```
```    46 lemma equiv_ratrel: "equiv {x. snd x \<noteq> 0} ratrel"
```
```    47   by (rule equiv.intro [OF refl_on_ratrel sym_ratrel trans_ratrel])
```
```    48
```
```    49 lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
```
```    50 lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
```
```    51
```
```    52 lemma equiv_ratrel_iff [iff]:
```
```    53   assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
```
```    54   shows "ratrel `` {x} = ratrel `` {y} \<longleftrightarrow> (x, y) \<in> ratrel"
```
```    55   by (rule eq_equiv_class_iff, rule equiv_ratrel) (auto simp add: assms)
```
```    56
```
```    57 typedef (Rat) rat = "{x. snd x \<noteq> 0} // ratrel"
```
```    58 proof
```
```    59   have "(0::int, 1::int) \<in> {x. snd x \<noteq> 0}" by simp
```
```    60   then show "ratrel `` {(0, 1)} \<in> {x. snd x \<noteq> 0} // ratrel" by (rule quotientI)
```
```    61 qed
```
```    62
```
```    63 lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel `` {x} \<in> Rat"
```
```    64   by (simp add: Rat_def quotientI)
```
```    65
```
```    66 declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp]
```
```    67
```
```    68
```
```    69 subsubsection {* Representation and basic operations *}
```
```    70
```
```    71 definition
```
```    72   Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
```
```    73   [code del]: "Fract a b = Abs_Rat (ratrel `` {if b = 0 then (0, 1) else (a, b)})"
```
```    74
```
```    75 code_datatype Fract
```
```    76
```
```    77 lemma Rat_cases [case_names Fract, cases type: rat]:
```
```    78   assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
```
```    79   shows C
```
```    80   using assms by (cases q) (clarsimp simp add: Fract_def Rat_def quotient_def)
```
```    81
```
```    82 lemma Rat_induct [case_names Fract, induct type: rat]:
```
```    83   assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
```
```    84   shows "P q"
```
```    85   using assms by (cases q) simp
```
```    86
```
```    87 lemma eq_rat:
```
```    88   shows "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
```
```    89   and "\<And>a. Fract a 0 = Fract 0 1"
```
```    90   and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    91   by (simp_all add: Fract_def)
```
```    92
```
```    93 instantiation rat :: "{comm_ring_1, recpower}"
```
```    94 begin
```
```    95
```
```    96 definition
```
```    97   Zero_rat_def [code, code unfold]: "0 = Fract 0 1"
```
```    98
```
```    99 definition
```
```   100   One_rat_def [code, code unfold]: "1 = Fract 1 1"
```
```   101
```
```   102 definition
```
```   103   add_rat_def [code del]:
```
```   104   "q + r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   105     ratrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   106
```
```   107 lemma add_rat [simp]:
```
```   108   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   109   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   110 proof -
```
```   111   have "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
```
```   112     respects2 ratrel"
```
```   113   by (rule equiv_ratrel [THEN congruent2_commuteI]) (simp_all add: left_distrib)
```
```   114   with assms show ?thesis by (simp add: Fract_def add_rat_def UN_ratrel2)
```
```   115 qed
```
```   116
```
```   117 definition
```
```   118   minus_rat_def [code del]:
```
```   119   "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel `` {(- fst x, snd x)})"
```
```   120
```
```   121 lemma minus_rat [simp, code]: "- Fract a b = Fract (- a) b"
```
```   122 proof -
```
```   123   have "(\<lambda>x. ratrel `` {(- fst x, snd x)}) respects ratrel"
```
```   124     by (simp add: congruent_def)
```
```   125   then show ?thesis by (simp add: Fract_def minus_rat_def UN_ratrel)
```
```   126 qed
```
```   127
```
```   128 lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
```
```   129   by (cases "b = 0") (simp_all add: eq_rat)
```
```   130
```
```   131 definition
```
```   132   diff_rat_def [code del]: "q - r = q + - (r::rat)"
```
```   133
```
```   134 lemma diff_rat [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_rat_def)
```
```   138
```
```   139 definition
```
```   140   mult_rat_def [code del]:
```
```   141   "q * r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   142     ratrel``{(fst x * fst y, snd x * snd y)})"
```
```   143
```
```   144 lemma mult_rat [simp]: "Fract a b * Fract c d = Fract (a * c) (b * d)"
```
```   145 proof -
```
```   146   have "(\<lambda>x y. ratrel `` {(fst x * fst y, snd x * snd y)}) respects2 ratrel"
```
```   147     by (rule equiv_ratrel [THEN congruent2_commuteI]) simp_all
```
```   148   then show ?thesis by (simp add: Fract_def mult_rat_def UN_ratrel2)
```
```   149 qed
```
```   150
```
```   151 lemma mult_rat_cancel:
```
```   152   assumes "c \<noteq> 0"
```
```   153   shows "Fract (c * a) (c * b) = Fract a b"
```
```   154 proof -
```
```   155   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
```
```   156   then show ?thesis by (simp add: mult_rat [symmetric])
```
```   157 qed
```
```   158
```
```   159 primrec power_rat
```
```   160 where
```
```   161   "q ^ 0 = (1\<Colon>rat)"
```
```   162 | "q ^ Suc n = (q\<Colon>rat) * (q ^ n)"
```
```   163
```
```   164 instance proof
```
```   165   fix q r s :: rat show "(q * r) * s = q * (r * s)"
```
```   166     by (cases q, cases r, cases s) (simp add: eq_rat)
```
```   167 next
```
```   168   fix q r :: rat show "q * r = r * q"
```
```   169     by (cases q, cases r) (simp add: eq_rat)
```
```   170 next
```
```   171   fix q :: rat show "1 * q = q"
```
```   172     by (cases q) (simp add: One_rat_def eq_rat)
```
```   173 next
```
```   174   fix q r s :: rat show "(q + r) + s = q + (r + s)"
```
```   175     by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
```
```   176 next
```
```   177   fix q r :: rat show "q + r = r + q"
```
```   178     by (cases q, cases r) (simp add: eq_rat)
```
```   179 next
```
```   180   fix q :: rat show "0 + q = q"
```
```   181     by (cases q) (simp add: Zero_rat_def eq_rat)
```
```   182 next
```
```   183   fix q :: rat show "- q + q = 0"
```
```   184     by (cases q) (simp add: Zero_rat_def eq_rat)
```
```   185 next
```
```   186   fix q r :: rat show "q - r = q + - r"
```
```   187     by (cases q, cases r) (simp add: eq_rat)
```
```   188 next
```
```   189   fix q r s :: rat show "(q + r) * s = q * s + r * s"
```
```   190     by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
```
```   191 next
```
```   192   show "(0::rat) \<noteq> 1" by (simp add: Zero_rat_def One_rat_def eq_rat)
```
```   193 next
```
```   194   fix q :: rat show "q * 1 = q"
```
```   195     by (cases q) (simp add: One_rat_def eq_rat)
```
```   196 next
```
```   197   fix q :: rat
```
```   198   fix n :: nat
```
```   199   show "q ^ 0 = 1" by simp
```
```   200   show "q ^ (Suc n) = q * (q ^ n)" by simp
```
```   201 qed
```
```   202
```
```   203 declare power_rat.simps [simp del]
```
```   204
```
```   205 end
```
```   206
```
```   207 lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
```
```   208   by (induct k) (simp_all add: Zero_rat_def One_rat_def)
```
```   209
```
```   210 lemma of_int_rat: "of_int k = Fract k 1"
```
```   211   by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
```
```   212
```
```   213 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   214   by (rule of_nat_rat [symmetric])
```
```   215
```
```   216 lemma Fract_of_int_eq: "Fract k 1 = of_int k"
```
```   217   by (rule of_int_rat [symmetric])
```
```   218
```
```   219 instantiation rat :: number_ring
```
```   220 begin
```
```   221
```
```   222 definition
```
```   223   rat_number_of_def [code del]: "number_of w = Fract w 1"
```
```   224
```
```   225 instance by intro_classes (simp add: rat_number_of_def of_int_rat)
```
```   226
```
```   227 end
```
```   228
```
```   229 lemma rat_number_collapse [code post]:
```
```   230   "Fract 0 k = 0"
```
```   231   "Fract 1 1 = 1"
```
```   232   "Fract (number_of k) 1 = number_of k"
```
```   233   "Fract k 0 = 0"
```
```   234   by (cases "k = 0")
```
```   235     (simp_all add: Zero_rat_def One_rat_def number_of_is_id number_of_eq of_int_rat eq_rat Fract_def)
```
```   236
```
```   237 lemma rat_number_expand [code unfold]:
```
```   238   "0 = Fract 0 1"
```
```   239   "1 = Fract 1 1"
```
```   240   "number_of k = Fract (number_of k) 1"
```
```   241   by (simp_all add: rat_number_collapse)
```
```   242
```
```   243 lemma iszero_rat [simp]:
```
```   244   "iszero (number_of k :: rat) \<longleftrightarrow> iszero (number_of k :: int)"
```
```   245   by (simp add: iszero_def rat_number_expand number_of_is_id eq_rat)
```
```   246
```
```   247 lemma Rat_cases_nonzero [case_names Fract 0]:
```
```   248   assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
```
```   249   assumes 0: "q = 0 \<Longrightarrow> C"
```
```   250   shows C
```
```   251 proof (cases "q = 0")
```
```   252   case True then show C using 0 by auto
```
```   253 next
```
```   254   case False
```
```   255   then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
```
```   256   moreover with False have "0 \<noteq> Fract a b" by simp
```
```   257   with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_rat_def eq_rat)
```
```   258   with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
```
```   259 qed
```
```   260
```
```   261
```
```   262 subsubsection {* The field of rational numbers *}
```
```   263
```
```   264 instantiation rat :: "{field, division_by_zero}"
```
```   265 begin
```
```   266
```
```   267 definition
```
```   268   inverse_rat_def [code del]:
```
```   269   "inverse q = Abs_Rat (\<Union>x \<in> Rep_Rat q.
```
```   270      ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
```
```   271
```
```   272 lemma inverse_rat [simp]: "inverse (Fract a b) = Fract b a"
```
```   273 proof -
```
```   274   have "(\<lambda>x. ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)}) respects ratrel"
```
```   275     by (auto simp add: congruent_def mult_commute)
```
```   276   then show ?thesis by (simp add: Fract_def inverse_rat_def UN_ratrel)
```
```   277 qed
```
```   278
```
```   279 definition
```
```   280   divide_rat_def [code del]: "q / r = q * inverse (r::rat)"
```
```   281
```
```   282 lemma divide_rat [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   283   by (simp add: divide_rat_def)
```
```   284
```
```   285 instance proof
```
```   286   show "inverse 0 = (0::rat)" by (simp add: rat_number_expand)
```
```   287     (simp add: rat_number_collapse)
```
```   288 next
```
```   289   fix q :: rat
```
```   290   assume "q \<noteq> 0"
```
```   291   then show "inverse q * q = 1" by (cases q rule: Rat_cases_nonzero)
```
```   292    (simp_all add: mult_rat  inverse_rat rat_number_expand eq_rat)
```
```   293 next
```
```   294   fix q r :: rat
```
```   295   show "q / r = q * inverse r" by (simp add: divide_rat_def)
```
```   296 qed
```
```   297
```
```   298 end
```
```   299
```
```   300
```
```   301 subsubsection {* Various *}
```
```   302
```
```   303 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
```
```   304   by (simp add: rat_number_expand)
```
```   305
```
```   306 lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
```
```   307   by (simp add: Fract_of_int_eq [symmetric])
```
```   308
```
```   309 lemma Fract_number_of_quotient [code post]:
```
```   310   "Fract (number_of k) (number_of l) = number_of k / number_of l"
```
```   311   unfolding Fract_of_int_quotient number_of_is_id number_of_eq ..
```
```   312
```
```   313 lemma Fract_1_number_of [code post]:
```
```   314   "Fract 1 (number_of k) = 1 / number_of k"
```
```   315   unfolding Fract_of_int_quotient number_of_eq by simp
```
```   316
```
```   317 subsubsection {* The ordered field of rational numbers *}
```
```   318
```
```   319 instantiation rat :: linorder
```
```   320 begin
```
```   321
```
```   322 definition
```
```   323   le_rat_def [code del]:
```
```   324    "q \<le> r \<longleftrightarrow> contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   325       {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
```
```   326
```
```   327 lemma le_rat [simp]:
```
```   328   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   329   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   330 proof -
```
```   331   have "(\<lambda>x y. {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})
```
```   332     respects2 ratrel"
```
```   333   proof (clarsimp simp add: congruent2_def)
```
```   334     fix a b a' b' c d c' d'::int
```
```   335     assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   336     assume eq1: "a * b' = a' * b"
```
```   337     assume eq2: "c * d' = c' * d"
```
```   338
```
```   339     let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   340     {
```
```   341       fix a b c d x :: int assume x: "x \<noteq> 0"
```
```   342       have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   343       proof -
```
```   344         from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
```
```   345         hence "?le a b c d =
```
```   346             ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
```
```   347           by (simp add: mult_le_cancel_right)
```
```   348         also have "... = ?le (a * x) (b * x) c d"
```
```   349           by (simp add: mult_ac)
```
```   350         finally show ?thesis .
```
```   351       qed
```
```   352     } note le_factor = this
```
```   353
```
```   354     let ?D = "b * d" and ?D' = "b' * d'"
```
```   355     from neq have D: "?D \<noteq> 0" by simp
```
```   356     from neq have "?D' \<noteq> 0" by simp
```
```   357     hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
```
```   358       by (rule le_factor)
```
```   359     also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
```
```   360       by (simp add: mult_ac)
```
```   361     also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
```
```   362       by (simp only: eq1 eq2)
```
```   363     also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
```
```   364       by (simp add: mult_ac)
```
```   365     also from D have "... = ?le a' b' c' d'"
```
```   366       by (rule le_factor [symmetric])
```
```   367     finally show "?le a b c d = ?le a' b' c' d'" .
```
```   368   qed
```
```   369   with assms show ?thesis by (simp add: Fract_def le_rat_def UN_ratrel2)
```
```   370 qed
```
```   371
```
```   372 definition
```
```   373   less_rat_def [code del]: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
```
```   374
```
```   375 lemma less_rat [simp]:
```
```   376   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   377   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
```
```   378   using assms by (simp add: less_rat_def eq_rat order_less_le)
```
```   379
```
```   380 instance proof
```
```   381   fix q r s :: rat
```
```   382   {
```
```   383     assume "q \<le> r" and "r \<le> s"
```
```   384     show "q \<le> s"
```
```   385     proof (insert prems, induct q, induct r, induct s)
```
```   386       fix a b c d e f :: int
```
```   387       assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   388       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
```
```   389       show "Fract a b \<le> Fract e f"
```
```   390       proof -
```
```   391         from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   392           by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   393         have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   394         proof -
```
```   395           from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   396             by simp
```
```   397           with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   398         qed
```
```   399         also have "... = (c * f) * (d * f) * (b * b)" by algebra
```
```   400         also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   401         proof -
```
```   402           from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   403             by simp
```
```   404           with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   405         qed
```
```   406         finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   407           by (simp only: mult_ac)
```
```   408         with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   409           by (simp add: mult_le_cancel_right)
```
```   410         with neq show ?thesis by simp
```
```   411       qed
```
```   412     qed
```
```   413   next
```
```   414     assume "q \<le> r" and "r \<le> q"
```
```   415     show "q = r"
```
```   416     proof (insert prems, induct q, induct r)
```
```   417       fix a b c d :: int
```
```   418       assume neq: "b \<noteq> 0"  "d \<noteq> 0"
```
```   419       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
```
```   420       show "Fract a b = Fract c d"
```
```   421       proof -
```
```   422         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   423           by simp
```
```   424         also have "... \<le> (a * d) * (b * d)"
```
```   425         proof -
```
```   426           from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   427             by simp
```
```   428           thus ?thesis by (simp only: mult_ac)
```
```   429         qed
```
```   430         finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   431         moreover from neq have "b * d \<noteq> 0" by simp
```
```   432         ultimately have "a * d = c * b" by simp
```
```   433         with neq show ?thesis by (simp add: eq_rat)
```
```   434       qed
```
```   435     qed
```
```   436   next
```
```   437     show "q \<le> q"
```
```   438       by (induct q) simp
```
```   439     show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
```
```   440       by (induct q, induct r) (auto simp add: le_less mult_commute)
```
```   441     show "q \<le> r \<or> r \<le> q"
```
```   442       by (induct q, induct r)
```
```   443          (simp add: mult_commute, rule linorder_linear)
```
```   444   }
```
```   445 qed
```
```   446
```
```   447 end
```
```   448
```
```   449 instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
```
```   450 begin
```
```   451
```
```   452 definition
```
```   453   abs_rat_def [code del]: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
```
```   454
```
```   455 lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   456   by (auto simp add: abs_rat_def zabs_def Zero_rat_def less_rat not_less le_less minus_rat eq_rat zero_compare_simps)
```
```   457
```
```   458 definition
```
```   459   sgn_rat_def [code del]: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
```
```   460
```
```   461 lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
```
```   462   unfolding Fract_of_int_eq
```
```   463   by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
```
```   464     (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
```
```   465
```
```   466 definition
```
```   467   "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
```
```   468
```
```   469 definition
```
```   470   "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
```
```   471
```
```   472 instance by intro_classes
```
```   473   (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
```
```   474
```
```   475 end
```
```   476
```
```   477 instance rat :: ordered_field
```
```   478 proof
```
```   479   fix q r s :: rat
```
```   480   show "q \<le> r ==> s + q \<le> s + r"
```
```   481   proof (induct q, induct r, induct s)
```
```   482     fix a b c d e f :: int
```
```   483     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   484     assume le: "Fract a b \<le> Fract c d"
```
```   485     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   486     proof -
```
```   487       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   488         by (auto simp add: zero_less_mult_iff)
```
```   489       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   490         by simp
```
```   491       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   492         by (simp add: mult_le_cancel_right)
```
```   493       with neq show ?thesis by (simp add: mult_ac int_distrib)
```
```   494     qed
```
```   495   qed
```
```   496   show "q < r ==> 0 < s ==> s * q < s * r"
```
```   497   proof (induct q, induct r, induct s)
```
```   498     fix a b c d e f :: int
```
```   499     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   500     assume le: "Fract a b < Fract c d"
```
```   501     assume gt: "0 < Fract e f"
```
```   502     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   503     proof -
```
```   504       let ?E = "e * f" and ?F = "f * f"
```
```   505       from neq gt have "0 < ?E"
```
```   506         by (auto simp add: Zero_rat_def order_less_le eq_rat)
```
```   507       moreover from neq have "0 < ?F"
```
```   508         by (auto simp add: zero_less_mult_iff)
```
```   509       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   510         by simp
```
```   511       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   512         by (simp add: mult_less_cancel_right)
```
```   513       with neq show ?thesis
```
```   514         by (simp add: mult_ac)
```
```   515     qed
```
```   516   qed
```
```   517 qed auto
```
```   518
```
```   519 lemma Rat_induct_pos [case_names Fract, induct type: rat]:
```
```   520   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
```
```   521   shows "P q"
```
```   522 proof (cases q)
```
```   523   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
```
```   524   proof -
```
```   525     fix a::int and b::int
```
```   526     assume b: "b < 0"
```
```   527     hence "0 < -b" by simp
```
```   528     hence "P (Fract (-a) (-b))" by (rule step)
```
```   529     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
```
```   530   qed
```
```   531   case (Fract a b)
```
```   532   thus "P q" by (force simp add: linorder_neq_iff step step')
```
```   533 qed
```
```   534
```
```   535 lemma zero_less_Fract_iff:
```
```   536   "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
```
```   537   by (simp add: Zero_rat_def zero_less_mult_iff)
```
```   538
```
```   539 lemma Fract_less_zero_iff:
```
```   540   "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
```
```   541   by (simp add: Zero_rat_def mult_less_0_iff)
```
```   542
```
```   543 lemma zero_le_Fract_iff:
```
```   544   "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
```
```   545   by (simp add: Zero_rat_def zero_le_mult_iff)
```
```   546
```
```   547 lemma Fract_le_zero_iff:
```
```   548   "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
```
```   549   by (simp add: Zero_rat_def mult_le_0_iff)
```
```   550
```
```   551 lemma one_less_Fract_iff:
```
```   552   "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
```
```   553   by (simp add: One_rat_def mult_less_cancel_right_disj)
```
```   554
```
```   555 lemma Fract_less_one_iff:
```
```   556   "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
```
```   557   by (simp add: One_rat_def mult_less_cancel_right_disj)
```
```   558
```
```   559 lemma one_le_Fract_iff:
```
```   560   "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
```
```   561   by (simp add: One_rat_def mult_le_cancel_right)
```
```   562
```
```   563 lemma Fract_le_one_iff:
```
```   564   "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
```
```   565   by (simp add: One_rat_def mult_le_cancel_right)
```
```   566
```
```   567
```
```   568 subsubsection {* Rationals are an Archimedean field *}
```
```   569
```
```   570 lemma rat_floor_lemma:
```
```   571   assumes "0 < b"
```
```   572   shows "of_int (a div b) \<le> Fract a b \<and> Fract a b < of_int (a div b + 1)"
```
```   573 proof -
```
```   574   have "Fract a b = of_int (a div b) + Fract (a mod b) b"
```
```   575     using `0 < b` by (simp add: of_int_rat)
```
```   576   moreover have "0 \<le> Fract (a mod b) b \<and> Fract (a mod b) b < 1"
```
```   577     using `0 < b` by (simp add: zero_le_Fract_iff Fract_less_one_iff)
```
```   578   ultimately show ?thesis by simp
```
```   579 qed
```
```   580
```
```   581 instance rat :: archimedean_field
```
```   582 proof
```
```   583   fix r :: rat
```
```   584   show "\<exists>z. r \<le> of_int z"
```
```   585   proof (induct r)
```
```   586     case (Fract a b)
```
```   587     then have "Fract a b \<le> of_int (a div b + 1)"
```
```   588       using rat_floor_lemma [of b a] by simp
```
```   589     then show "\<exists>z. Fract a b \<le> of_int z" ..
```
```   590   qed
```
```   591 qed
```
```   592
```
```   593 lemma floor_Fract:
```
```   594   assumes "0 < b" shows "floor (Fract a b) = a div b"
```
```   595   using rat_floor_lemma [OF `0 < b`, of a]
```
```   596   by (simp add: floor_unique)
```
```   597
```
```   598
```
```   599 subsection {* Arithmetic setup *}
```
```   600
```
```   601 use "Tools/rat_arith.ML"
```
```   602 declaration {* K rat_arith_setup *}
```
```   603
```
```   604
```
```   605 subsection {* Embedding from Rationals to other Fields *}
```
```   606
```
```   607 class field_char_0 = field + ring_char_0
```
```   608
```
```   609 subclass (in ordered_field) field_char_0 ..
```
```   610
```
```   611 context field_char_0
```
```   612 begin
```
```   613
```
```   614 definition of_rat :: "rat \<Rightarrow> 'a" where
```
```   615   [code del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
```
```   616
```
```   617 end
```
```   618
```
```   619 lemma of_rat_congruent:
```
```   620   "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
```
```   621 apply (rule congruent.intro)
```
```   622 apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   623 apply (simp only: of_int_mult [symmetric])
```
```   624 done
```
```   625
```
```   626 lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
```
```   627   unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
```
```   628
```
```   629 lemma of_rat_0 [simp]: "of_rat 0 = 0"
```
```   630 by (simp add: Zero_rat_def of_rat_rat)
```
```   631
```
```   632 lemma of_rat_1 [simp]: "of_rat 1 = 1"
```
```   633 by (simp add: One_rat_def of_rat_rat)
```
```   634
```
```   635 lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
```
```   636 by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
```
```   637
```
```   638 lemma of_rat_minus: "of_rat (- a) = - of_rat a"
```
```   639 by (induct a, simp add: of_rat_rat)
```
```   640
```
```   641 lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
```
```   642 by (simp only: diff_minus of_rat_add of_rat_minus)
```
```   643
```
```   644 lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
```
```   645 apply (induct a, induct b, simp add: of_rat_rat)
```
```   646 apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
```
```   647 done
```
```   648
```
```   649 lemma nonzero_of_rat_inverse:
```
```   650   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
```
```   651 apply (rule inverse_unique [symmetric])
```
```   652 apply (simp add: of_rat_mult [symmetric])
```
```   653 done
```
```   654
```
```   655 lemma of_rat_inverse:
```
```   656   "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
```
```   657    inverse (of_rat a)"
```
```   658 by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
```
```   659
```
```   660 lemma nonzero_of_rat_divide:
```
```   661   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
```
```   662 by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
```
```   663
```
```   664 lemma of_rat_divide:
```
```   665   "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
```
```   666    = of_rat a / of_rat b"
```
```   667 by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
```
```   668
```
```   669 lemma of_rat_power:
```
```   670   "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
```
```   671 by (induct n) (simp_all add: of_rat_mult)
```
```   672
```
```   673 lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
```
```   674 apply (induct a, induct b)
```
```   675 apply (simp add: of_rat_rat eq_rat)
```
```   676 apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   677 apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
```
```   678 done
```
```   679
```
```   680 lemma of_rat_less:
```
```   681   "(of_rat r :: 'a::ordered_field) < of_rat s \<longleftrightarrow> r < s"
```
```   682 proof (induct r, induct s)
```
```   683   fix a b c d :: int
```
```   684   assume not_zero: "b > 0" "d > 0"
```
```   685   then have "b * d > 0" by (rule mult_pos_pos)
```
```   686   have of_int_divide_less_eq:
```
```   687     "(of_int a :: 'a) / of_int b < of_int c / of_int d
```
```   688       \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
```
```   689     using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
```
```   690   show "(of_rat (Fract a b) :: 'a::ordered_field) < of_rat (Fract c d)
```
```   691     \<longleftrightarrow> Fract a b < Fract c d"
```
```   692     using not_zero `b * d > 0`
```
```   693     by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
```
```   694       (auto intro: mult_strict_right_mono mult_right_less_imp_less)
```
```   695 qed
```
```   696
```
```   697 lemma of_rat_less_eq:
```
```   698   "(of_rat r :: 'a::ordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
```
```   699   unfolding le_less by (auto simp add: of_rat_less)
```
```   700
```
```   701 lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
```
```   702
```
```   703 lemma of_rat_eq_id [simp]: "of_rat = id"
```
```   704 proof
```
```   705   fix a
```
```   706   show "of_rat a = id a"
```
```   707   by (induct a)
```
```   708      (simp add: of_rat_rat Fract_of_int_eq [symmetric])
```
```   709 qed
```
```   710
```
```   711 text{*Collapse nested embeddings*}
```
```   712 lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
```
```   713 by (induct n) (simp_all add: of_rat_add)
```
```   714
```
```   715 lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
```
```   716 by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
```
```   717
```
```   718 lemma of_rat_number_of_eq [simp]:
```
```   719   "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
```
```   720 by (simp add: number_of_eq)
```
```   721
```
```   722 lemmas zero_rat = Zero_rat_def
```
```   723 lemmas one_rat = One_rat_def
```
```   724
```
```   725 abbreviation
```
```   726   rat_of_nat :: "nat \<Rightarrow> rat"
```
```   727 where
```
```   728   "rat_of_nat \<equiv> of_nat"
```
```   729
```
```   730 abbreviation
```
```   731   rat_of_int :: "int \<Rightarrow> rat"
```
```   732 where
```
```   733   "rat_of_int \<equiv> of_int"
```
```   734
```
```   735 subsection {* The Set of Rational Numbers *}
```
```   736
```
```   737 context field_char_0
```
```   738 begin
```
```   739
```
```   740 definition
```
```   741   Rats  :: "'a set" where
```
```   742   [code del]: "Rats = range of_rat"
```
```   743
```
```   744 notation (xsymbols)
```
```   745   Rats  ("\<rat>")
```
```   746
```
```   747 end
```
```   748
```
```   749 lemma Rats_of_rat [simp]: "of_rat r \<in> Rats"
```
```   750 by (simp add: Rats_def)
```
```   751
```
```   752 lemma Rats_of_int [simp]: "of_int z \<in> Rats"
```
```   753 by (subst of_rat_of_int_eq [symmetric], rule Rats_of_rat)
```
```   754
```
```   755 lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
```
```   756 by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
```
```   757
```
```   758 lemma Rats_number_of [simp]:
```
```   759   "(number_of w::'a::{number_ring,field_char_0}) \<in> Rats"
```
```   760 by (subst of_rat_number_of_eq [symmetric], rule Rats_of_rat)
```
```   761
```
```   762 lemma Rats_0 [simp]: "0 \<in> Rats"
```
```   763 apply (unfold Rats_def)
```
```   764 apply (rule range_eqI)
```
```   765 apply (rule of_rat_0 [symmetric])
```
```   766 done
```
```   767
```
```   768 lemma Rats_1 [simp]: "1 \<in> Rats"
```
```   769 apply (unfold Rats_def)
```
```   770 apply (rule range_eqI)
```
```   771 apply (rule of_rat_1 [symmetric])
```
```   772 done
```
```   773
```
```   774 lemma Rats_add [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a + b \<in> Rats"
```
```   775 apply (auto simp add: Rats_def)
```
```   776 apply (rule range_eqI)
```
```   777 apply (rule of_rat_add [symmetric])
```
```   778 done
```
```   779
```
```   780 lemma Rats_minus [simp]: "a \<in> Rats \<Longrightarrow> - a \<in> Rats"
```
```   781 apply (auto simp add: Rats_def)
```
```   782 apply (rule range_eqI)
```
```   783 apply (rule of_rat_minus [symmetric])
```
```   784 done
```
```   785
```
```   786 lemma Rats_diff [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a - b \<in> Rats"
```
```   787 apply (auto simp add: Rats_def)
```
```   788 apply (rule range_eqI)
```
```   789 apply (rule of_rat_diff [symmetric])
```
```   790 done
```
```   791
```
```   792 lemma Rats_mult [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a * b \<in> Rats"
```
```   793 apply (auto simp add: Rats_def)
```
```   794 apply (rule range_eqI)
```
```   795 apply (rule of_rat_mult [symmetric])
```
```   796 done
```
```   797
```
```   798 lemma nonzero_Rats_inverse:
```
```   799   fixes a :: "'a::field_char_0"
```
```   800   shows "\<lbrakk>a \<in> Rats; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Rats"
```
```   801 apply (auto simp add: Rats_def)
```
```   802 apply (rule range_eqI)
```
```   803 apply (erule nonzero_of_rat_inverse [symmetric])
```
```   804 done
```
```   805
```
```   806 lemma Rats_inverse [simp]:
```
```   807   fixes a :: "'a::{field_char_0,division_by_zero}"
```
```   808   shows "a \<in> Rats \<Longrightarrow> inverse a \<in> Rats"
```
```   809 apply (auto simp add: Rats_def)
```
```   810 apply (rule range_eqI)
```
```   811 apply (rule of_rat_inverse [symmetric])
```
```   812 done
```
```   813
```
```   814 lemma nonzero_Rats_divide:
```
```   815   fixes a b :: "'a::field_char_0"
```
```   816   shows "\<lbrakk>a \<in> Rats; b \<in> Rats; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
```
```   817 apply (auto simp add: Rats_def)
```
```   818 apply (rule range_eqI)
```
```   819 apply (erule nonzero_of_rat_divide [symmetric])
```
```   820 done
```
```   821
```
```   822 lemma Rats_divide [simp]:
```
```   823   fixes a b :: "'a::{field_char_0,division_by_zero}"
```
```   824   shows "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
```
```   825 apply (auto simp add: Rats_def)
```
```   826 apply (rule range_eqI)
```
```   827 apply (rule of_rat_divide [symmetric])
```
```   828 done
```
```   829
```
```   830 lemma Rats_power [simp]:
```
```   831   fixes a :: "'a::{field_char_0,recpower}"
```
```   832   shows "a \<in> Rats \<Longrightarrow> a ^ n \<in> Rats"
```
```   833 apply (auto simp add: Rats_def)
```
```   834 apply (rule range_eqI)
```
```   835 apply (rule of_rat_power [symmetric])
```
```   836 done
```
```   837
```
```   838 lemma Rats_cases [cases set: Rats]:
```
```   839   assumes "q \<in> \<rat>"
```
```   840   obtains (of_rat) r where "q = of_rat r"
```
```   841   unfolding Rats_def
```
```   842 proof -
```
```   843   from `q \<in> \<rat>` have "q \<in> range of_rat" unfolding Rats_def .
```
```   844   then obtain r where "q = of_rat r" ..
```
```   845   then show thesis ..
```
```   846 qed
```
```   847
```
```   848 lemma Rats_induct [case_names of_rat, induct set: Rats]:
```
```   849   "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
```
```   850   by (rule Rats_cases) auto
```
```   851
```
```   852
```
```   853 subsection {* Implementation of rational numbers as pairs of integers *}
```
```   854
```
```   855 lemma Fract_norm: "Fract (a div zgcd a b) (b div zgcd a b) = Fract a b"
```
```   856 proof (cases "a = 0 \<or> b = 0")
```
```   857   case True then show ?thesis by (auto simp add: eq_rat)
```
```   858 next
```
```   859   let ?c = "zgcd a b"
```
```   860   case False then have "a \<noteq> 0" and "b \<noteq> 0" by auto
```
```   861   then have "?c \<noteq> 0" by simp
```
```   862   then have "Fract ?c ?c = Fract 1 1" by (simp add: eq_rat)
```
```   863   moreover have "Fract (a div ?c * ?c + a mod ?c) (b div ?c * ?c + b mod ?c) = Fract a b"
```
```   864     by (simp add: semiring_div_class.mod_div_equality)
```
```   865   moreover have "a mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
```
```   866   moreover have "b mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
```
```   867   ultimately show ?thesis
```
```   868     by (simp add: mult_rat [symmetric])
```
```   869 qed
```
```   870
```
```   871 definition Fract_norm :: "int \<Rightarrow> int \<Rightarrow> rat" where
```
```   872   [simp, code del]: "Fract_norm a b = Fract a b"
```
```   873
```
```   874 lemma Fract_norm_code [code]: "Fract_norm a b = (if a = 0 \<or> b = 0 then 0 else let c = zgcd a b in
```
```   875   if b > 0 then Fract (a div c) (b div c) else Fract (- (a div c)) (- (b div c)))"
```
```   876   by (simp add: eq_rat Zero_rat_def Let_def Fract_norm)
```
```   877
```
```   878 lemma [code]:
```
```   879   "of_rat (Fract a b) = (if b \<noteq> 0 then of_int a / of_int b else 0)"
```
```   880   by (cases "b = 0") (simp_all add: rat_number_collapse of_rat_rat)
```
```   881
```
```   882 instantiation rat :: eq
```
```   883 begin
```
```   884
```
```   885 definition [code del]: "eq_class.eq (a\<Colon>rat) b \<longleftrightarrow> a - b = 0"
```
```   886
```
```   887 instance by default (simp add: eq_rat_def)
```
```   888
```
```   889 lemma rat_eq_code [code]:
```
```   890   "eq_class.eq (Fract a b) (Fract c d) \<longleftrightarrow> (if b = 0
```
```   891        then c = 0 \<or> d = 0
```
```   892      else if d = 0
```
```   893        then a = 0 \<or> b = 0
```
```   894      else a * d = b * c)"
```
```   895   by (auto simp add: eq eq_rat)
```
```   896
```
```   897 lemma rat_eq_refl [code nbe]:
```
```   898   "eq_class.eq (r::rat) r \<longleftrightarrow> True"
```
```   899   by (rule HOL.eq_refl)
```
```   900
```
```   901 end
```
```   902
```
```   903 lemma le_rat':
```
```   904   assumes "b \<noteq> 0"
```
```   905     and "d \<noteq> 0"
```
```   906   shows "Fract a b \<le> Fract c d \<longleftrightarrow> a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d"
```
```   907 proof -
```
```   908   have abs_sgn: "\<And>k::int. \<bar>k\<bar> = k * sgn k" unfolding abs_if sgn_if by simp
```
```   909   have "a * d * (b * d) \<le> c * b * (b * d) \<longleftrightarrow> a * d * (sgn b * sgn d) \<le> c * b * (sgn b * sgn d)"
```
```   910   proof (cases "b * d > 0")
```
```   911     case True
```
```   912     moreover from True have "sgn b * sgn d = 1"
```
```   913       by (simp add: sgn_times [symmetric] sgn_1_pos)
```
```   914     ultimately show ?thesis by (simp add: mult_le_cancel_right)
```
```   915   next
```
```   916     case False with assms have "b * d < 0" by (simp add: less_le)
```
```   917     moreover from this have "sgn b * sgn d = - 1"
```
```   918       by (simp only: sgn_times [symmetric] sgn_1_neg)
```
```   919     ultimately show ?thesis by (simp add: mult_le_cancel_right)
```
```   920   qed
```
```   921   also have "\<dots> \<longleftrightarrow> a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d"
```
```   922     by (simp add: abs_sgn mult_ac)
```
```   923   finally show ?thesis using assms by simp
```
```   924 qed
```
```   925
```
```   926 lemma less_rat':
```
```   927   assumes "b \<noteq> 0"
```
```   928     and "d \<noteq> 0"
```
```   929   shows "Fract a b < Fract c d \<longleftrightarrow> a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d"
```
```   930 proof -
```
```   931   have abs_sgn: "\<And>k::int. \<bar>k\<bar> = k * sgn k" unfolding abs_if sgn_if by simp
```
```   932   have "a * d * (b * d) < c * b * (b * d) \<longleftrightarrow> a * d * (sgn b * sgn d) < c * b * (sgn b * sgn d)"
```
```   933   proof (cases "b * d > 0")
```
```   934     case True
```
```   935     moreover from True have "sgn b * sgn d = 1"
```
```   936       by (simp add: sgn_times [symmetric] sgn_1_pos)
```
```   937     ultimately show ?thesis by (simp add: mult_less_cancel_right)
```
```   938   next
```
```   939     case False with assms have "b * d < 0" by (simp add: less_le)
```
```   940     moreover from this have "sgn b * sgn d = - 1"
```
```   941       by (simp only: sgn_times [symmetric] sgn_1_neg)
```
```   942     ultimately show ?thesis by (simp add: mult_less_cancel_right)
```
```   943   qed
```
```   944   also have "\<dots> \<longleftrightarrow> a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d"
```
```   945     by (simp add: abs_sgn mult_ac)
```
```   946   finally show ?thesis using assms by simp
```
```   947 qed
```
```   948
```
```   949 lemma (in ordered_idom) sgn_greater [simp]:
```
```   950   "0 < sgn a \<longleftrightarrow> 0 < a"
```
```   951   unfolding sgn_if by auto
```
```   952
```
```   953 lemma (in ordered_idom) sgn_less [simp]:
```
```   954   "sgn a < 0 \<longleftrightarrow> a < 0"
```
```   955   unfolding sgn_if by auto
```
```   956
```
```   957 lemma rat_le_eq_code [code]:
```
```   958   "Fract a b < Fract c d \<longleftrightarrow> (if b = 0
```
```   959        then sgn c * sgn d > 0
```
```   960      else if d = 0
```
```   961        then sgn a * sgn b < 0
```
```   962      else a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d)"
```
```   963   by (auto simp add: sgn_times mult_less_0_iff zero_less_mult_iff less_rat' eq_rat simp del: less_rat)
```
```   964
```
```   965 lemma rat_less_eq_code [code]:
```
```   966   "Fract a b \<le> Fract c d \<longleftrightarrow> (if b = 0
```
```   967        then sgn c * sgn d \<ge> 0
```
```   968      else if d = 0
```
```   969        then sgn a * sgn b \<le> 0
```
```   970      else a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d)"
```
```   971   by (auto simp add: sgn_times mult_le_0_iff zero_le_mult_iff le_rat' eq_rat simp del: le_rat)
```
```   972     (auto simp add: le_less not_less sgn_0_0)
```
```   973
```
```   974
```
```   975 lemma rat_plus_code [code]:
```
```   976   "Fract a b + Fract c d = (if b = 0
```
```   977      then Fract c d
```
```   978    else if d = 0
```
```   979      then Fract a b
```
```   980    else Fract_norm (a * d + c * b) (b * d))"
```
```   981   by (simp add: eq_rat, simp add: Zero_rat_def)
```
```   982
```
```   983 lemma rat_times_code [code]:
```
```   984   "Fract a b * Fract c d = Fract_norm (a * c) (b * d)"
```
```   985   by simp
```
```   986
```
```   987 lemma rat_minus_code [code]:
```
```   988   "Fract a b - Fract c d = (if b = 0
```
```   989      then Fract (- c) d
```
```   990    else if d = 0
```
```   991      then Fract a b
```
```   992    else Fract_norm (a * d - c * b) (b * d))"
```
```   993   by (simp add: eq_rat, simp add: Zero_rat_def)
```
```   994
```
```   995 lemma rat_inverse_code [code]:
```
```   996   "inverse (Fract a b) = (if b = 0 then Fract 1 0
```
```   997     else if a < 0 then Fract (- b) (- a)
```
```   998     else Fract b a)"
```
```   999   by (simp add: eq_rat)
```
```  1000
```
```  1001 lemma rat_divide_code [code]:
```
```  1002   "Fract a b / Fract c d = Fract_norm (a * d) (b * c)"
```
```  1003   by simp
```
```  1004
```
```  1005 hide (open) const Fract_norm
```
```  1006
```
```  1007 text {* Setup for SML code generator *}
```
```  1008
```
```  1009 types_code
```
```  1010   rat ("(int */ int)")
```
```  1011 attach (term_of) {*
```
```  1012 fun term_of_rat (p, q) =
```
```  1013   let
```
```  1014     val rT = Type ("Rational.rat", [])
```
```  1015   in
```
```  1016     if q = 1 orelse p = 0 then HOLogic.mk_number rT p
```
```  1017     else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} \$
```
```  1018       HOLogic.mk_number rT p \$ HOLogic.mk_number rT q
```
```  1019   end;
```
```  1020 *}
```
```  1021 attach (test) {*
```
```  1022 fun gen_rat i =
```
```  1023   let
```
```  1024     val p = random_range 0 i;
```
```  1025     val q = random_range 1 (i + 1);
```
```  1026     val g = Integer.gcd p q;
```
```  1027     val p' = p div g;
```
```  1028     val q' = q div g;
```
```  1029     val r = (if one_of [true, false] then p' else ~ p',
```
```  1030       if p' = 0 then 0 else q')
```
```  1031   in
```
```  1032     (r, fn () => term_of_rat r)
```
```  1033   end;
```
```  1034 *}
```
```  1035
```
```  1036 consts_code
```
```  1037   Fract ("(_,/ _)")
```
```  1038
```
```  1039 consts_code
```
```  1040   "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
```
```  1041 attach {*
```
```  1042 fun rat_of_int 0 = (0, 0)
```
```  1043   | rat_of_int i = (i, 1);
```
```  1044 *}
```
```  1045
```
```  1046 end
```