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