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