src/HOL/Rat.thy
 author huffman Sun Mar 25 20:15:39 2012 +0200 (2012-03-25) changeset 47108 2a1953f0d20d parent 46758 4106258260b3 child 47906 09a896d295bd permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
```     1 (*  Title:  HOL/Rat.thy
```
```     2     Author: Markus Wenzel, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 header {* Rational numbers *}
```
```     6
```
```     7 theory Rat
```
```     8 imports GCD Archimedean_Field
```
```     9 uses ("Tools/float_syntax.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 equivI [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 definition "Rat = {x. snd x \<noteq> 0} // ratrel"
```
```    58
```
```    59 typedef (open) rat = Rat
```
```    60   morphisms Rep_Rat Abs_Rat
```
```    61   unfolding Rat_def
```
```    62 proof
```
```    63   have "(0::int, 1::int) \<in> {x. snd x \<noteq> 0}" by simp
```
```    64   then show "ratrel `` {(0, 1)} \<in> {x. snd x \<noteq> 0} // ratrel" by (rule quotientI)
```
```    65 qed
```
```    66
```
```    67 lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel `` {x} \<in> Rat"
```
```    68   by (simp add: Rat_def quotientI)
```
```    69
```
```    70 declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp]
```
```    71
```
```    72
```
```    73 subsubsection {* Representation and basic operations *}
```
```    74
```
```    75 definition
```
```    76   Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
```
```    77   "Fract a b = Abs_Rat (ratrel `` {if b = 0 then (0, 1) else (a, b)})"
```
```    78
```
```    79 lemma eq_rat:
```
```    80   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"
```
```    81   and "\<And>a. Fract a 0 = Fract 0 1"
```
```    82   and "\<And>a c. Fract 0 a = Fract 0 c"
```
```    83   by (simp_all add: Fract_def)
```
```    84
```
```    85 lemma Rat_cases [case_names Fract, cases type: rat]:
```
```    86   assumes "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
```
```    87   shows C
```
```    88 proof -
```
```    89   obtain a b :: int where "q = Fract a b" and "b \<noteq> 0"
```
```    90     by (cases q) (clarsimp simp add: Fract_def Rat_def quotient_def)
```
```    91   let ?a = "a div gcd a b"
```
```    92   let ?b = "b div gcd a b"
```
```    93   from `b \<noteq> 0` have "?b * gcd a b = b"
```
```    94     by (simp add: dvd_div_mult_self)
```
```    95   with `b \<noteq> 0` have "?b \<noteq> 0" by auto
```
```    96   from `q = Fract a b` `b \<noteq> 0` `?b \<noteq> 0` have q: "q = Fract ?a ?b"
```
```    97     by (simp add: eq_rat dvd_div_mult mult_commute [of a])
```
```    98   from `b \<noteq> 0` have coprime: "coprime ?a ?b"
```
```    99     by (auto intro: div_gcd_coprime_int)
```
```   100   show C proof (cases "b > 0")
```
```   101     case True
```
```   102     note assms
```
```   103     moreover note q
```
```   104     moreover from True have "?b > 0" by (simp add: nonneg1_imp_zdiv_pos_iff)
```
```   105     moreover note coprime
```
```   106     ultimately show C .
```
```   107   next
```
```   108     case False
```
```   109     note assms
```
```   110     moreover from q have "q = Fract (- ?a) (- ?b)" by (simp add: Fract_def)
```
```   111     moreover from False `b \<noteq> 0` have "- ?b > 0" by (simp add: pos_imp_zdiv_neg_iff)
```
```   112     moreover from coprime have "coprime (- ?a) (- ?b)" by simp
```
```   113     ultimately show C .
```
```   114   qed
```
```   115 qed
```
```   116
```
```   117 lemma Rat_induct [case_names Fract, induct type: rat]:
```
```   118   assumes "\<And>a b. b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> P (Fract a b)"
```
```   119   shows "P q"
```
```   120   using assms by (cases q) simp
```
```   121
```
```   122 instantiation rat :: comm_ring_1
```
```   123 begin
```
```   124
```
```   125 definition
```
```   126   Zero_rat_def: "0 = Fract 0 1"
```
```   127
```
```   128 definition
```
```   129   One_rat_def: "1 = Fract 1 1"
```
```   130
```
```   131 definition
```
```   132   add_rat_def:
```
```   133   "q + r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   134     ratrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
```
```   135
```
```   136 lemma add_rat [simp]:
```
```   137   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   138   shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   139 proof -
```
```   140   have "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
```
```   141     respects2 ratrel"
```
```   142   by (rule equiv_ratrel [THEN congruent2_commuteI]) (simp_all add: left_distrib)
```
```   143   with assms show ?thesis by (simp add: Fract_def add_rat_def UN_ratrel2)
```
```   144 qed
```
```   145
```
```   146 definition
```
```   147   minus_rat_def:
```
```   148   "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel `` {(- fst x, snd x)})"
```
```   149
```
```   150 lemma minus_rat [simp]: "- Fract a b = Fract (- a) b"
```
```   151 proof -
```
```   152   have "(\<lambda>x. ratrel `` {(- fst x, snd x)}) respects ratrel"
```
```   153     by (simp add: congruent_def split_paired_all)
```
```   154   then show ?thesis by (simp add: Fract_def minus_rat_def UN_ratrel)
```
```   155 qed
```
```   156
```
```   157 lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
```
```   158   by (cases "b = 0") (simp_all add: eq_rat)
```
```   159
```
```   160 definition
```
```   161   diff_rat_def: "q - r = q + - (r::rat)"
```
```   162
```
```   163 lemma diff_rat [simp]:
```
```   164   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   165   shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
```
```   166   using assms by (simp add: diff_rat_def)
```
```   167
```
```   168 definition
```
```   169   mult_rat_def:
```
```   170   "q * r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   171     ratrel``{(fst x * fst y, snd x * snd y)})"
```
```   172
```
```   173 lemma mult_rat [simp]: "Fract a b * Fract c d = Fract (a * c) (b * d)"
```
```   174 proof -
```
```   175   have "(\<lambda>x y. ratrel `` {(fst x * fst y, snd x * snd y)}) respects2 ratrel"
```
```   176     by (rule equiv_ratrel [THEN congruent2_commuteI]) simp_all
```
```   177   then show ?thesis by (simp add: Fract_def mult_rat_def UN_ratrel2)
```
```   178 qed
```
```   179
```
```   180 lemma mult_rat_cancel:
```
```   181   assumes "c \<noteq> 0"
```
```   182   shows "Fract (c * a) (c * b) = Fract a b"
```
```   183 proof -
```
```   184   from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
```
```   185   then show ?thesis by (simp add: mult_rat [symmetric])
```
```   186 qed
```
```   187
```
```   188 instance proof
```
```   189   fix q r s :: rat show "(q * r) * s = q * (r * s)"
```
```   190     by (cases q, cases r, cases s) (simp add: eq_rat)
```
```   191 next
```
```   192   fix q r :: rat show "q * r = r * q"
```
```   193     by (cases q, cases r) (simp add: eq_rat)
```
```   194 next
```
```   195   fix q :: rat show "1 * q = q"
```
```   196     by (cases q) (simp add: One_rat_def eq_rat)
```
```   197 next
```
```   198   fix q r s :: rat show "(q + r) + s = q + (r + s)"
```
```   199     by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
```
```   200 next
```
```   201   fix q r :: rat show "q + r = r + q"
```
```   202     by (cases q, cases r) (simp add: eq_rat)
```
```   203 next
```
```   204   fix q :: rat show "0 + q = q"
```
```   205     by (cases q) (simp add: Zero_rat_def eq_rat)
```
```   206 next
```
```   207   fix q :: rat show "- q + q = 0"
```
```   208     by (cases q) (simp add: Zero_rat_def eq_rat)
```
```   209 next
```
```   210   fix q r :: rat show "q - r = q + - r"
```
```   211     by (cases q, cases r) (simp add: eq_rat)
```
```   212 next
```
```   213   fix q r s :: rat show "(q + r) * s = q * s + r * s"
```
```   214     by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
```
```   215 next
```
```   216   show "(0::rat) \<noteq> 1" by (simp add: Zero_rat_def One_rat_def eq_rat)
```
```   217 qed
```
```   218
```
```   219 end
```
```   220
```
```   221 lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
```
```   222   by (induct k) (simp_all add: Zero_rat_def One_rat_def)
```
```   223
```
```   224 lemma of_int_rat: "of_int k = Fract k 1"
```
```   225   by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
```
```   226
```
```   227 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   228   by (rule of_nat_rat [symmetric])
```
```   229
```
```   230 lemma Fract_of_int_eq: "Fract k 1 = of_int k"
```
```   231   by (rule of_int_rat [symmetric])
```
```   232
```
```   233 lemma rat_number_collapse:
```
```   234   "Fract 0 k = 0"
```
```   235   "Fract 1 1 = 1"
```
```   236   "Fract (numeral w) 1 = numeral w"
```
```   237   "Fract (neg_numeral w) 1 = neg_numeral w"
```
```   238   "Fract k 0 = 0"
```
```   239   using Fract_of_int_eq [of "numeral w"]
```
```   240   using Fract_of_int_eq [of "neg_numeral w"]
```
```   241   by (simp_all add: Zero_rat_def One_rat_def eq_rat)
```
```   242
```
```   243 lemma rat_number_expand:
```
```   244   "0 = Fract 0 1"
```
```   245   "1 = Fract 1 1"
```
```   246   "numeral k = Fract (numeral k) 1"
```
```   247   "neg_numeral k = Fract (neg_numeral k) 1"
```
```   248   by (simp_all add: rat_number_collapse)
```
```   249
```
```   250 lemma Rat_cases_nonzero [case_names Fract 0]:
```
```   251   assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
```
```   252   assumes 0: "q = 0 \<Longrightarrow> C"
```
```   253   shows C
```
```   254 proof (cases "q = 0")
```
```   255   case True then show C using 0 by auto
```
```   256 next
```
```   257   case False
```
```   258   then obtain a b where "q = Fract a b" and "b > 0" and "coprime a b" by (cases q) auto
```
```   259   moreover with False have "0 \<noteq> Fract a b" by simp
```
```   260   with `b > 0` have "a \<noteq> 0" by (simp add: Zero_rat_def eq_rat)
```
```   261   with Fract `q = Fract a b` `b > 0` `coprime a b` show C by blast
```
```   262 qed
```
```   263
```
```   264 subsubsection {* Function @{text normalize} *}
```
```   265
```
```   266 lemma Fract_coprime: "Fract (a div gcd a b) (b div gcd a b) = Fract a b"
```
```   267 proof (cases "b = 0")
```
```   268   case True then show ?thesis by (simp add: eq_rat)
```
```   269 next
```
```   270   case False
```
```   271   moreover have "b div gcd a b * gcd a b = b"
```
```   272     by (rule dvd_div_mult_self) simp
```
```   273   ultimately have "b div gcd a b \<noteq> 0" by auto
```
```   274   with False show ?thesis by (simp add: eq_rat dvd_div_mult mult_commute [of a])
```
```   275 qed
```
```   276
```
```   277 definition normalize :: "int \<times> int \<Rightarrow> int \<times> int" where
```
```   278   "normalize p = (if snd p > 0 then (let a = gcd (fst p) (snd p) in (fst p div a, snd p div a))
```
```   279     else if snd p = 0 then (0, 1)
```
```   280     else (let a = - gcd (fst p) (snd p) in (fst p div a, snd p div a)))"
```
```   281
```
```   282 lemma normalize_crossproduct:
```
```   283   assumes "q \<noteq> 0" "s \<noteq> 0"
```
```   284   assumes "normalize (p, q) = normalize (r, s)"
```
```   285   shows "p * s = r * q"
```
```   286 proof -
```
```   287   have aux: "p * gcd r s = sgn (q * s) * r * gcd p q \<Longrightarrow> q * gcd r s = sgn (q * s) * s * gcd p q \<Longrightarrow> p * s = q * r"
```
```   288   proof -
```
```   289     assume "p * gcd r s = sgn (q * s) * r * gcd p q" and "q * gcd r s = sgn (q * s) * s * gcd p q"
```
```   290     then have "(p * gcd r s) * (sgn (q * s) * s * gcd p q) = (q * gcd r s) * (sgn (q * s) * r * gcd p q)" by simp
```
```   291     with assms show "p * s = q * r" by (auto simp add: mult_ac sgn_times sgn_0_0)
```
```   292   qed
```
```   293   from assms show ?thesis
```
```   294     by (auto simp add: normalize_def Let_def dvd_div_div_eq_mult mult_commute sgn_times split: if_splits intro: aux)
```
```   295 qed
```
```   296
```
```   297 lemma normalize_eq: "normalize (a, b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
```
```   298   by (auto simp add: normalize_def Let_def Fract_coprime dvd_div_neg rat_number_collapse
```
```   299     split:split_if_asm)
```
```   300
```
```   301 lemma normalize_denom_pos: "normalize r = (p, q) \<Longrightarrow> q > 0"
```
```   302   by (auto simp add: normalize_def Let_def dvd_div_neg pos_imp_zdiv_neg_iff nonneg1_imp_zdiv_pos_iff
```
```   303     split:split_if_asm)
```
```   304
```
```   305 lemma normalize_coprime: "normalize r = (p, q) \<Longrightarrow> coprime p q"
```
```   306   by (auto simp add: normalize_def Let_def dvd_div_neg div_gcd_coprime_int
```
```   307     split:split_if_asm)
```
```   308
```
```   309 lemma normalize_stable [simp]:
```
```   310   "q > 0 \<Longrightarrow> coprime p q \<Longrightarrow> normalize (p, q) = (p, q)"
```
```   311   by (simp add: normalize_def)
```
```   312
```
```   313 lemma normalize_denom_zero [simp]:
```
```   314   "normalize (p, 0) = (0, 1)"
```
```   315   by (simp add: normalize_def)
```
```   316
```
```   317 lemma normalize_negative [simp]:
```
```   318   "q < 0 \<Longrightarrow> normalize (p, q) = normalize (- p, - q)"
```
```   319   by (simp add: normalize_def Let_def dvd_div_neg dvd_neg_div)
```
```   320
```
```   321 text{*
```
```   322   Decompose a fraction into normalized, i.e. coprime numerator and denominator:
```
```   323 *}
```
```   324
```
```   325 definition quotient_of :: "rat \<Rightarrow> int \<times> int" where
```
```   326   "quotient_of x = (THE pair. x = Fract (fst pair) (snd pair) &
```
```   327                    snd pair > 0 & coprime (fst pair) (snd pair))"
```
```   328
```
```   329 lemma quotient_of_unique:
```
```   330   "\<exists>!p. r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
```
```   331 proof (cases r)
```
```   332   case (Fract a b)
```
```   333   then have "r = Fract (fst (a, b)) (snd (a, b)) \<and> snd (a, b) > 0 \<and> coprime (fst (a, b)) (snd (a, b))" by auto
```
```   334   then show ?thesis proof (rule ex1I)
```
```   335     fix p
```
```   336     obtain c d :: int where p: "p = (c, d)" by (cases p)
```
```   337     assume "r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
```
```   338     with p have Fract': "r = Fract c d" "d > 0" "coprime c d" by simp_all
```
```   339     have "c = a \<and> d = b"
```
```   340     proof (cases "a = 0")
```
```   341       case True with Fract Fract' show ?thesis by (simp add: eq_rat)
```
```   342     next
```
```   343       case False
```
```   344       with Fract Fract' have *: "c * b = a * d" and "c \<noteq> 0" by (auto simp add: eq_rat)
```
```   345       then have "c * b > 0 \<longleftrightarrow> a * d > 0" by auto
```
```   346       with `b > 0` `d > 0` have "a > 0 \<longleftrightarrow> c > 0" by (simp add: zero_less_mult_iff)
```
```   347       with `a \<noteq> 0` `c \<noteq> 0` have sgn: "sgn a = sgn c" by (auto simp add: not_less)
```
```   348       from `coprime a b` `coprime c d` have "\<bar>a\<bar> * \<bar>d\<bar> = \<bar>c\<bar> * \<bar>b\<bar> \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> \<bar>d\<bar> = \<bar>b\<bar>"
```
```   349         by (simp add: coprime_crossproduct_int)
```
```   350       with `b > 0` `d > 0` have "\<bar>a\<bar> * d = \<bar>c\<bar> * b \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> d = b" by simp
```
```   351       then have "a * sgn a * d = c * sgn c * b \<longleftrightarrow> a * sgn a = c * sgn c \<and> d = b" by (simp add: abs_sgn)
```
```   352       with sgn * show ?thesis by (auto simp add: sgn_0_0)
```
```   353     qed
```
```   354     with p show "p = (a, b)" by simp
```
```   355   qed
```
```   356 qed
```
```   357
```
```   358 lemma quotient_of_Fract [code]:
```
```   359   "quotient_of (Fract a b) = normalize (a, b)"
```
```   360 proof -
```
```   361   have "Fract a b = Fract (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?Fract)
```
```   362     by (rule sym) (auto intro: normalize_eq)
```
```   363   moreover have "0 < snd (normalize (a, b))" (is ?denom_pos)
```
```   364     by (cases "normalize (a, b)") (rule normalize_denom_pos, simp)
```
```   365   moreover have "coprime (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?coprime)
```
```   366     by (rule normalize_coprime) simp
```
```   367   ultimately have "?Fract \<and> ?denom_pos \<and> ?coprime" by blast
```
```   368   with quotient_of_unique have
```
```   369     "(THE p. Fract a b = Fract (fst p) (snd p) \<and> 0 < snd p \<and> coprime (fst p) (snd p)) = normalize (a, b)"
```
```   370     by (rule the1_equality)
```
```   371   then show ?thesis by (simp add: quotient_of_def)
```
```   372 qed
```
```   373
```
```   374 lemma quotient_of_number [simp]:
```
```   375   "quotient_of 0 = (0, 1)"
```
```   376   "quotient_of 1 = (1, 1)"
```
```   377   "quotient_of (numeral k) = (numeral k, 1)"
```
```   378   "quotient_of (neg_numeral k) = (neg_numeral k, 1)"
```
```   379   by (simp_all add: rat_number_expand quotient_of_Fract)
```
```   380
```
```   381 lemma quotient_of_eq: "quotient_of (Fract a b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
```
```   382   by (simp add: quotient_of_Fract normalize_eq)
```
```   383
```
```   384 lemma quotient_of_denom_pos: "quotient_of r = (p, q) \<Longrightarrow> q > 0"
```
```   385   by (cases r) (simp add: quotient_of_Fract normalize_denom_pos)
```
```   386
```
```   387 lemma quotient_of_coprime: "quotient_of r = (p, q) \<Longrightarrow> coprime p q"
```
```   388   by (cases r) (simp add: quotient_of_Fract normalize_coprime)
```
```   389
```
```   390 lemma quotient_of_inject:
```
```   391   assumes "quotient_of a = quotient_of b"
```
```   392   shows "a = b"
```
```   393 proof -
```
```   394   obtain p q r s where a: "a = Fract p q"
```
```   395     and b: "b = Fract r s"
```
```   396     and "q > 0" and "s > 0" by (cases a, cases b)
```
```   397   with assms show ?thesis by (simp add: eq_rat quotient_of_Fract normalize_crossproduct)
```
```   398 qed
```
```   399
```
```   400 lemma quotient_of_inject_eq:
```
```   401   "quotient_of a = quotient_of b \<longleftrightarrow> a = b"
```
```   402   by (auto simp add: quotient_of_inject)
```
```   403
```
```   404
```
```   405 subsubsection {* The field of rational numbers *}
```
```   406
```
```   407 instantiation rat :: field_inverse_zero
```
```   408 begin
```
```   409
```
```   410 definition
```
```   411   inverse_rat_def:
```
```   412   "inverse q = Abs_Rat (\<Union>x \<in> Rep_Rat q.
```
```   413      ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
```
```   414
```
```   415 lemma inverse_rat [simp]: "inverse (Fract a b) = Fract b a"
```
```   416 proof -
```
```   417   have "(\<lambda>x. ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)}) respects ratrel"
```
```   418     by (auto simp add: congruent_def mult_commute)
```
```   419   then show ?thesis by (simp add: Fract_def inverse_rat_def UN_ratrel)
```
```   420 qed
```
```   421
```
```   422 definition
```
```   423   divide_rat_def: "q / r = q * inverse (r::rat)"
```
```   424
```
```   425 lemma divide_rat [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   426   by (simp add: divide_rat_def)
```
```   427
```
```   428 instance proof
```
```   429   fix q :: rat
```
```   430   assume "q \<noteq> 0"
```
```   431   then show "inverse q * q = 1" by (cases q rule: Rat_cases_nonzero)
```
```   432    (simp_all add: rat_number_expand eq_rat)
```
```   433 next
```
```   434   fix q r :: rat
```
```   435   show "q / r = q * inverse r" by (simp add: divide_rat_def)
```
```   436 next
```
```   437   show "inverse 0 = (0::rat)" by (simp add: rat_number_expand, simp add: rat_number_collapse)
```
```   438 qed
```
```   439
```
```   440 end
```
```   441
```
```   442
```
```   443 subsubsection {* Various *}
```
```   444
```
```   445 lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
```
```   446   by (simp add: Fract_of_int_eq [symmetric])
```
```   447
```
```   448 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
```
```   449   by (simp add: rat_number_expand)
```
```   450
```
```   451
```
```   452 subsubsection {* The ordered field of rational numbers *}
```
```   453
```
```   454 instantiation rat :: linorder
```
```   455 begin
```
```   456
```
```   457 definition
```
```   458   le_rat_def:
```
```   459    "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   460       {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
```
```   461
```
```   462 lemma le_rat [simp]:
```
```   463   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   464   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   465 proof -
```
```   466   have "(\<lambda>x y. {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})
```
```   467     respects2 ratrel"
```
```   468   proof (clarsimp simp add: congruent2_def)
```
```   469     fix a b a' b' c d c' d'::int
```
```   470     assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   471     assume eq1: "a * b' = a' * b"
```
```   472     assume eq2: "c * d' = c' * d"
```
```   473
```
```   474     let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   475     {
```
```   476       fix a b c d x :: int assume x: "x \<noteq> 0"
```
```   477       have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   478       proof -
```
```   479         from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
```
```   480         hence "?le a b c d =
```
```   481             ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
```
```   482           by (simp add: mult_le_cancel_right)
```
```   483         also have "... = ?le (a * x) (b * x) c d"
```
```   484           by (simp add: mult_ac)
```
```   485         finally show ?thesis .
```
```   486       qed
```
```   487     } note le_factor = this
```
```   488
```
```   489     let ?D = "b * d" and ?D' = "b' * d'"
```
```   490     from neq have D: "?D \<noteq> 0" by simp
```
```   491     from neq have "?D' \<noteq> 0" by simp
```
```   492     hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
```
```   493       by (rule le_factor)
```
```   494     also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
```
```   495       by (simp add: mult_ac)
```
```   496     also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
```
```   497       by (simp only: eq1 eq2)
```
```   498     also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
```
```   499       by (simp add: mult_ac)
```
```   500     also from D have "... = ?le a' b' c' d'"
```
```   501       by (rule le_factor [symmetric])
```
```   502     finally show "?le a b c d = ?le a' b' c' d'" .
```
```   503   qed
```
```   504   with assms show ?thesis by (simp add: Fract_def le_rat_def UN_ratrel2)
```
```   505 qed
```
```   506
```
```   507 definition
```
```   508   less_rat_def: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
```
```   509
```
```   510 lemma less_rat [simp]:
```
```   511   assumes "b \<noteq> 0" and "d \<noteq> 0"
```
```   512   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
```
```   513   using assms by (simp add: less_rat_def eq_rat order_less_le)
```
```   514
```
```   515 instance proof
```
```   516   fix q r s :: rat
```
```   517   {
```
```   518     assume "q \<le> r" and "r \<le> s"
```
```   519     then show "q \<le> s"
```
```   520     proof (induct q, induct r, induct s)
```
```   521       fix a b c d e f :: int
```
```   522       assume neq: "b > 0"  "d > 0"  "f > 0"
```
```   523       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
```
```   524       show "Fract a b \<le> Fract e f"
```
```   525       proof -
```
```   526         from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   527           by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   528         have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   529         proof -
```
```   530           from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   531             by simp
```
```   532           with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   533         qed
```
```   534         also have "... = (c * f) * (d * f) * (b * b)" by algebra
```
```   535         also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   536         proof -
```
```   537           from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   538             by simp
```
```   539           with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   540         qed
```
```   541         finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   542           by (simp only: mult_ac)
```
```   543         with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   544           by (simp add: mult_le_cancel_right)
```
```   545         with neq show ?thesis by simp
```
```   546       qed
```
```   547     qed
```
```   548   next
```
```   549     assume "q \<le> r" and "r \<le> q"
```
```   550     then show "q = r"
```
```   551     proof (induct q, induct r)
```
```   552       fix a b c d :: int
```
```   553       assume neq: "b > 0"  "d > 0"
```
```   554       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
```
```   555       show "Fract a b = Fract c d"
```
```   556       proof -
```
```   557         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   558           by simp
```
```   559         also have "... \<le> (a * d) * (b * d)"
```
```   560         proof -
```
```   561           from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   562             by simp
```
```   563           thus ?thesis by (simp only: mult_ac)
```
```   564         qed
```
```   565         finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   566         moreover from neq have "b * d \<noteq> 0" by simp
```
```   567         ultimately have "a * d = c * b" by simp
```
```   568         with neq show ?thesis by (simp add: eq_rat)
```
```   569       qed
```
```   570     qed
```
```   571   next
```
```   572     show "q \<le> q"
```
```   573       by (induct q) simp
```
```   574     show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
```
```   575       by (induct q, induct r) (auto simp add: le_less mult_commute)
```
```   576     show "q \<le> r \<or> r \<le> q"
```
```   577       by (induct q, induct r)
```
```   578          (simp add: mult_commute, rule linorder_linear)
```
```   579   }
```
```   580 qed
```
```   581
```
```   582 end
```
```   583
```
```   584 instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
```
```   585 begin
```
```   586
```
```   587 definition
```
```   588   abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
```
```   589
```
```   590 lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   591   by (auto simp add: abs_rat_def zabs_def Zero_rat_def not_less le_less eq_rat zero_less_mult_iff)
```
```   592
```
```   593 definition
```
```   594   sgn_rat_def: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
```
```   595
```
```   596 lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
```
```   597   unfolding Fract_of_int_eq
```
```   598   by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
```
```   599     (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
```
```   600
```
```   601 definition
```
```   602   "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
```
```   603
```
```   604 definition
```
```   605   "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
```
```   606
```
```   607 instance by intro_classes
```
```   608   (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
```
```   609
```
```   610 end
```
```   611
```
```   612 instance rat :: linordered_field_inverse_zero
```
```   613 proof
```
```   614   fix q r s :: rat
```
```   615   show "q \<le> r ==> s + q \<le> s + r"
```
```   616   proof (induct q, induct r, induct s)
```
```   617     fix a b c d e f :: int
```
```   618     assume neq: "b > 0"  "d > 0"  "f > 0"
```
```   619     assume le: "Fract a b \<le> Fract c d"
```
```   620     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   621     proof -
```
```   622       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   623         by (auto simp add: zero_less_mult_iff)
```
```   624       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   625         by simp
```
```   626       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   627         by (simp add: mult_le_cancel_right)
```
```   628       with neq show ?thesis by (simp add: mult_ac int_distrib)
```
```   629     qed
```
```   630   qed
```
```   631   show "q < r ==> 0 < s ==> s * q < s * r"
```
```   632   proof (induct q, induct r, induct s)
```
```   633     fix a b c d e f :: int
```
```   634     assume neq: "b > 0"  "d > 0"  "f > 0"
```
```   635     assume le: "Fract a b < Fract c d"
```
```   636     assume gt: "0 < Fract e f"
```
```   637     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   638     proof -
```
```   639       let ?E = "e * f" and ?F = "f * f"
```
```   640       from neq gt have "0 < ?E"
```
```   641         by (auto simp add: Zero_rat_def order_less_le eq_rat)
```
```   642       moreover from neq have "0 < ?F"
```
```   643         by (auto simp add: zero_less_mult_iff)
```
```   644       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   645         by simp
```
```   646       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   647         by (simp add: mult_less_cancel_right)
```
```   648       with neq show ?thesis
```
```   649         by (simp add: mult_ac)
```
```   650     qed
```
```   651   qed
```
```   652 qed auto
```
```   653
```
```   654 lemma Rat_induct_pos [case_names Fract, induct type: rat]:
```
```   655   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
```
```   656   shows "P q"
```
```   657 proof (cases q)
```
```   658   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
```
```   659   proof -
```
```   660     fix a::int and b::int
```
```   661     assume b: "b < 0"
```
```   662     hence "0 < -b" by simp
```
```   663     hence "P (Fract (-a) (-b))" by (rule step)
```
```   664     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
```
```   665   qed
```
```   666   case (Fract a b)
```
```   667   thus "P q" by (force simp add: linorder_neq_iff step step')
```
```   668 qed
```
```   669
```
```   670 lemma zero_less_Fract_iff:
```
```   671   "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
```
```   672   by (simp add: Zero_rat_def zero_less_mult_iff)
```
```   673
```
```   674 lemma Fract_less_zero_iff:
```
```   675   "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
```
```   676   by (simp add: Zero_rat_def mult_less_0_iff)
```
```   677
```
```   678 lemma zero_le_Fract_iff:
```
```   679   "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
```
```   680   by (simp add: Zero_rat_def zero_le_mult_iff)
```
```   681
```
```   682 lemma Fract_le_zero_iff:
```
```   683   "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
```
```   684   by (simp add: Zero_rat_def mult_le_0_iff)
```
```   685
```
```   686 lemma one_less_Fract_iff:
```
```   687   "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
```
```   688   by (simp add: One_rat_def mult_less_cancel_right_disj)
```
```   689
```
```   690 lemma Fract_less_one_iff:
```
```   691   "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
```
```   692   by (simp add: One_rat_def mult_less_cancel_right_disj)
```
```   693
```
```   694 lemma one_le_Fract_iff:
```
```   695   "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
```
```   696   by (simp add: One_rat_def mult_le_cancel_right)
```
```   697
```
```   698 lemma Fract_le_one_iff:
```
```   699   "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
```
```   700   by (simp add: One_rat_def mult_le_cancel_right)
```
```   701
```
```   702
```
```   703 subsubsection {* Rationals are an Archimedean field *}
```
```   704
```
```   705 lemma rat_floor_lemma:
```
```   706   shows "of_int (a div b) \<le> Fract a b \<and> Fract a b < of_int (a div b + 1)"
```
```   707 proof -
```
```   708   have "Fract a b = of_int (a div b) + Fract (a mod b) b"
```
```   709     by (cases "b = 0", simp, simp add: of_int_rat)
```
```   710   moreover have "0 \<le> Fract (a mod b) b \<and> Fract (a mod b) b < 1"
```
```   711     unfolding Fract_of_int_quotient
```
```   712     by (rule linorder_cases [of b 0]) (simp add: divide_nonpos_neg, simp, simp add: divide_nonneg_pos)
```
```   713   ultimately show ?thesis by simp
```
```   714 qed
```
```   715
```
```   716 instance rat :: archimedean_field
```
```   717 proof
```
```   718   fix r :: rat
```
```   719   show "\<exists>z. r \<le> of_int z"
```
```   720   proof (induct r)
```
```   721     case (Fract a b)
```
```   722     have "Fract a b \<le> of_int (a div b + 1)"
```
```   723       using rat_floor_lemma [of a b] by simp
```
```   724     then show "\<exists>z. Fract a b \<le> of_int z" ..
```
```   725   qed
```
```   726 qed
```
```   727
```
```   728 instantiation rat :: floor_ceiling
```
```   729 begin
```
```   730
```
```   731 definition [code del]:
```
```   732   "floor (x::rat) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
```
```   733
```
```   734 instance proof
```
```   735   fix x :: rat
```
```   736   show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
```
```   737     unfolding floor_rat_def using floor_exists1 by (rule theI')
```
```   738 qed
```
```   739
```
```   740 end
```
```   741
```
```   742 lemma floor_Fract: "floor (Fract a b) = a div b"
```
```   743   using rat_floor_lemma [of a b]
```
```   744   by (simp add: floor_unique)
```
```   745
```
```   746
```
```   747 subsection {* Linear arithmetic setup *}
```
```   748
```
```   749 declaration {*
```
```   750   K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
```
```   751     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
```
```   752   #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_int_eq_iff} RS iffD2]
```
```   753     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
```
```   754   #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
```
```   755       @{thm True_implies_equals},
```
```   756       read_instantiate @{context} [(("a", 0), "(numeral ?v)")] @{thm right_distrib},
```
```   757       read_instantiate @{context} [(("a", 0), "(neg_numeral ?v)")] @{thm right_distrib},
```
```   758       @{thm divide_1}, @{thm divide_zero_left},
```
```   759       @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
```
```   760       @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
```
```   761       @{thm of_int_minus}, @{thm of_int_diff},
```
```   762       @{thm of_int_of_nat_eq}]
```
```   763   #> Lin_Arith.add_simprocs Numeral_Simprocs.field_cancel_numeral_factors
```
```   764   #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat => rat"})
```
```   765   #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int => rat"}))
```
```   766 *}
```
```   767
```
```   768
```
```   769 subsection {* Embedding from Rationals to other Fields *}
```
```   770
```
```   771 class field_char_0 = field + ring_char_0
```
```   772
```
```   773 subclass (in linordered_field) field_char_0 ..
```
```   774
```
```   775 context field_char_0
```
```   776 begin
```
```   777
```
```   778 definition of_rat :: "rat \<Rightarrow> 'a" where
```
```   779   "of_rat q = the_elem (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
```
```   780
```
```   781 end
```
```   782
```
```   783 lemma of_rat_congruent:
```
```   784   "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
```
```   785 apply (rule congruentI)
```
```   786 apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   787 apply (simp only: of_int_mult [symmetric])
```
```   788 done
```
```   789
```
```   790 lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
```
```   791   unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
```
```   792
```
```   793 lemma of_rat_0 [simp]: "of_rat 0 = 0"
```
```   794 by (simp add: Zero_rat_def of_rat_rat)
```
```   795
```
```   796 lemma of_rat_1 [simp]: "of_rat 1 = 1"
```
```   797 by (simp add: One_rat_def of_rat_rat)
```
```   798
```
```   799 lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
```
```   800 by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
```
```   801
```
```   802 lemma of_rat_minus: "of_rat (- a) = - of_rat a"
```
```   803 by (induct a, simp add: of_rat_rat)
```
```   804
```
```   805 lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
```
```   806 by (simp only: diff_minus of_rat_add of_rat_minus)
```
```   807
```
```   808 lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
```
```   809 apply (induct a, induct b, simp add: of_rat_rat)
```
```   810 apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
```
```   811 done
```
```   812
```
```   813 lemma nonzero_of_rat_inverse:
```
```   814   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
```
```   815 apply (rule inverse_unique [symmetric])
```
```   816 apply (simp add: of_rat_mult [symmetric])
```
```   817 done
```
```   818
```
```   819 lemma of_rat_inverse:
```
```   820   "(of_rat (inverse a)::'a::{field_char_0, field_inverse_zero}) =
```
```   821    inverse (of_rat a)"
```
```   822 by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
```
```   823
```
```   824 lemma nonzero_of_rat_divide:
```
```   825   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
```
```   826 by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
```
```   827
```
```   828 lemma of_rat_divide:
```
```   829   "(of_rat (a / b)::'a::{field_char_0, field_inverse_zero})
```
```   830    = of_rat a / of_rat b"
```
```   831 by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
```
```   832
```
```   833 lemma of_rat_power:
```
```   834   "(of_rat (a ^ n)::'a::field_char_0) = of_rat a ^ n"
```
```   835 by (induct n) (simp_all add: of_rat_mult)
```
```   836
```
```   837 lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
```
```   838 apply (induct a, induct b)
```
```   839 apply (simp add: of_rat_rat eq_rat)
```
```   840 apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   841 apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
```
```   842 done
```
```   843
```
```   844 lemma of_rat_less:
```
```   845   "(of_rat r :: 'a::linordered_field) < of_rat s \<longleftrightarrow> r < s"
```
```   846 proof (induct r, induct s)
```
```   847   fix a b c d :: int
```
```   848   assume not_zero: "b > 0" "d > 0"
```
```   849   then have "b * d > 0" by (rule mult_pos_pos)
```
```   850   have of_int_divide_less_eq:
```
```   851     "(of_int a :: 'a) / of_int b < of_int c / of_int d
```
```   852       \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
```
```   853     using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
```
```   854   show "(of_rat (Fract a b) :: 'a::linordered_field) < of_rat (Fract c d)
```
```   855     \<longleftrightarrow> Fract a b < Fract c d"
```
```   856     using not_zero `b * d > 0`
```
```   857     by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
```
```   858 qed
```
```   859
```
```   860 lemma of_rat_less_eq:
```
```   861   "(of_rat r :: 'a::linordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
```
```   862   unfolding le_less by (auto simp add: of_rat_less)
```
```   863
```
```   864 lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
```
```   865
```
```   866 lemma of_rat_eq_id [simp]: "of_rat = id"
```
```   867 proof
```
```   868   fix a
```
```   869   show "of_rat a = id a"
```
```   870   by (induct a)
```
```   871      (simp add: of_rat_rat Fract_of_int_eq [symmetric])
```
```   872 qed
```
```   873
```
```   874 text{*Collapse nested embeddings*}
```
```   875 lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
```
```   876 by (induct n) (simp_all add: of_rat_add)
```
```   877
```
```   878 lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
```
```   879 by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
```
```   880
```
```   881 lemma of_rat_numeral_eq [simp]:
```
```   882   "of_rat (numeral w) = numeral w"
```
```   883 using of_rat_of_int_eq [of "numeral w"] by simp
```
```   884
```
```   885 lemma of_rat_neg_numeral_eq [simp]:
```
```   886   "of_rat (neg_numeral w) = neg_numeral w"
```
```   887 using of_rat_of_int_eq [of "neg_numeral w"] by simp
```
```   888
```
```   889 lemmas zero_rat = Zero_rat_def
```
```   890 lemmas one_rat = One_rat_def
```
```   891
```
```   892 abbreviation
```
```   893   rat_of_nat :: "nat \<Rightarrow> rat"
```
```   894 where
```
```   895   "rat_of_nat \<equiv> of_nat"
```
```   896
```
```   897 abbreviation
```
```   898   rat_of_int :: "int \<Rightarrow> rat"
```
```   899 where
```
```   900   "rat_of_int \<equiv> of_int"
```
```   901
```
```   902 subsection {* The Set of Rational Numbers *}
```
```   903
```
```   904 context field_char_0
```
```   905 begin
```
```   906
```
```   907 definition
```
```   908   Rats  :: "'a set" where
```
```   909   "Rats = range of_rat"
```
```   910
```
```   911 notation (xsymbols)
```
```   912   Rats  ("\<rat>")
```
```   913
```
```   914 end
```
```   915
```
```   916 lemma Rats_of_rat [simp]: "of_rat r \<in> Rats"
```
```   917 by (simp add: Rats_def)
```
```   918
```
```   919 lemma Rats_of_int [simp]: "of_int z \<in> Rats"
```
```   920 by (subst of_rat_of_int_eq [symmetric], rule Rats_of_rat)
```
```   921
```
```   922 lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
```
```   923 by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
```
```   924
```
```   925 lemma Rats_number_of [simp]: "numeral w \<in> Rats"
```
```   926 by (subst of_rat_numeral_eq [symmetric], rule Rats_of_rat)
```
```   927
```
```   928 lemma Rats_neg_number_of [simp]: "neg_numeral w \<in> Rats"
```
```   929 by (subst of_rat_neg_numeral_eq [symmetric], rule Rats_of_rat)
```
```   930
```
```   931 lemma Rats_0 [simp]: "0 \<in> Rats"
```
```   932 apply (unfold Rats_def)
```
```   933 apply (rule range_eqI)
```
```   934 apply (rule of_rat_0 [symmetric])
```
```   935 done
```
```   936
```
```   937 lemma Rats_1 [simp]: "1 \<in> Rats"
```
```   938 apply (unfold Rats_def)
```
```   939 apply (rule range_eqI)
```
```   940 apply (rule of_rat_1 [symmetric])
```
```   941 done
```
```   942
```
```   943 lemma Rats_add [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a + b \<in> Rats"
```
```   944 apply (auto simp add: Rats_def)
```
```   945 apply (rule range_eqI)
```
```   946 apply (rule of_rat_add [symmetric])
```
```   947 done
```
```   948
```
```   949 lemma Rats_minus [simp]: "a \<in> Rats \<Longrightarrow> - a \<in> Rats"
```
```   950 apply (auto simp add: Rats_def)
```
```   951 apply (rule range_eqI)
```
```   952 apply (rule of_rat_minus [symmetric])
```
```   953 done
```
```   954
```
```   955 lemma Rats_diff [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a - b \<in> Rats"
```
```   956 apply (auto simp add: Rats_def)
```
```   957 apply (rule range_eqI)
```
```   958 apply (rule of_rat_diff [symmetric])
```
```   959 done
```
```   960
```
```   961 lemma Rats_mult [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a * b \<in> Rats"
```
```   962 apply (auto simp add: Rats_def)
```
```   963 apply (rule range_eqI)
```
```   964 apply (rule of_rat_mult [symmetric])
```
```   965 done
```
```   966
```
```   967 lemma nonzero_Rats_inverse:
```
```   968   fixes a :: "'a::field_char_0"
```
```   969   shows "\<lbrakk>a \<in> Rats; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Rats"
```
```   970 apply (auto simp add: Rats_def)
```
```   971 apply (rule range_eqI)
```
```   972 apply (erule nonzero_of_rat_inverse [symmetric])
```
```   973 done
```
```   974
```
```   975 lemma Rats_inverse [simp]:
```
```   976   fixes a :: "'a::{field_char_0, field_inverse_zero}"
```
```   977   shows "a \<in> Rats \<Longrightarrow> inverse a \<in> Rats"
```
```   978 apply (auto simp add: Rats_def)
```
```   979 apply (rule range_eqI)
```
```   980 apply (rule of_rat_inverse [symmetric])
```
```   981 done
```
```   982
```
```   983 lemma nonzero_Rats_divide:
```
```   984   fixes a b :: "'a::field_char_0"
```
```   985   shows "\<lbrakk>a \<in> Rats; b \<in> Rats; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
```
```   986 apply (auto simp add: Rats_def)
```
```   987 apply (rule range_eqI)
```
```   988 apply (erule nonzero_of_rat_divide [symmetric])
```
```   989 done
```
```   990
```
```   991 lemma Rats_divide [simp]:
```
```   992   fixes a b :: "'a::{field_char_0, field_inverse_zero}"
```
```   993   shows "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
```
```   994 apply (auto simp add: Rats_def)
```
```   995 apply (rule range_eqI)
```
```   996 apply (rule of_rat_divide [symmetric])
```
```   997 done
```
```   998
```
```   999 lemma Rats_power [simp]:
```
```  1000   fixes a :: "'a::field_char_0"
```
```  1001   shows "a \<in> Rats \<Longrightarrow> a ^ n \<in> Rats"
```
```  1002 apply (auto simp add: Rats_def)
```
```  1003 apply (rule range_eqI)
```
```  1004 apply (rule of_rat_power [symmetric])
```
```  1005 done
```
```  1006
```
```  1007 lemma Rats_cases [cases set: Rats]:
```
```  1008   assumes "q \<in> \<rat>"
```
```  1009   obtains (of_rat) r where "q = of_rat r"
```
```  1010   unfolding Rats_def
```
```  1011 proof -
```
```  1012   from `q \<in> \<rat>` have "q \<in> range of_rat" unfolding Rats_def .
```
```  1013   then obtain r where "q = of_rat r" ..
```
```  1014   then show thesis ..
```
```  1015 qed
```
```  1016
```
```  1017 lemma Rats_induct [case_names of_rat, induct set: Rats]:
```
```  1018   "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
```
```  1019   by (rule Rats_cases) auto
```
```  1020
```
```  1021
```
```  1022 subsection {* Implementation of rational numbers as pairs of integers *}
```
```  1023
```
```  1024 text {* Formal constructor *}
```
```  1025
```
```  1026 definition Frct :: "int \<times> int \<Rightarrow> rat" where
```
```  1027   [simp]: "Frct p = Fract (fst p) (snd p)"
```
```  1028
```
```  1029 lemma [code abstype]:
```
```  1030   "Frct (quotient_of q) = q"
```
```  1031   by (cases q) (auto intro: quotient_of_eq)
```
```  1032
```
```  1033
```
```  1034 text {* Numerals *}
```
```  1035
```
```  1036 declare quotient_of_Fract [code abstract]
```
```  1037
```
```  1038 definition of_int :: "int \<Rightarrow> rat"
```
```  1039 where
```
```  1040   [code_abbrev]: "of_int = Int.of_int"
```
```  1041 hide_const (open) of_int
```
```  1042
```
```  1043 lemma quotient_of_int [code abstract]:
```
```  1044   "quotient_of (Rat.of_int a) = (a, 1)"
```
```  1045   by (simp add: of_int_def of_int_rat quotient_of_Fract)
```
```  1046
```
```  1047 lemma [code_unfold]:
```
```  1048   "numeral k = Rat.of_int (numeral k)"
```
```  1049   by (simp add: Rat.of_int_def)
```
```  1050
```
```  1051 lemma [code_unfold]:
```
```  1052   "neg_numeral k = Rat.of_int (neg_numeral k)"
```
```  1053   by (simp add: Rat.of_int_def)
```
```  1054
```
```  1055 lemma Frct_code_post [code_post]:
```
```  1056   "Frct (0, a) = 0"
```
```  1057   "Frct (a, 0) = 0"
```
```  1058   "Frct (1, 1) = 1"
```
```  1059   "Frct (numeral k, 1) = numeral k"
```
```  1060   "Frct (neg_numeral k, 1) = neg_numeral k"
```
```  1061   "Frct (1, numeral k) = 1 / numeral k"
```
```  1062   "Frct (1, neg_numeral k) = 1 / neg_numeral k"
```
```  1063   "Frct (numeral k, numeral l) = numeral k / numeral l"
```
```  1064   "Frct (numeral k, neg_numeral l) = numeral k / neg_numeral l"
```
```  1065   "Frct (neg_numeral k, numeral l) = neg_numeral k / numeral l"
```
```  1066   "Frct (neg_numeral k, neg_numeral l) = neg_numeral k / neg_numeral l"
```
```  1067   by (simp_all add: Fract_of_int_quotient)
```
```  1068
```
```  1069
```
```  1070 text {* Operations *}
```
```  1071
```
```  1072 lemma rat_zero_code [code abstract]:
```
```  1073   "quotient_of 0 = (0, 1)"
```
```  1074   by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
```
```  1075
```
```  1076 lemma rat_one_code [code abstract]:
```
```  1077   "quotient_of 1 = (1, 1)"
```
```  1078   by (simp add: One_rat_def quotient_of_Fract normalize_def)
```
```  1079
```
```  1080 lemma rat_plus_code [code abstract]:
```
```  1081   "quotient_of (p + q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
```
```  1082      in normalize (a * d + b * c, c * d))"
```
```  1083   by (cases p, cases q) (simp add: quotient_of_Fract)
```
```  1084
```
```  1085 lemma rat_uminus_code [code abstract]:
```
```  1086   "quotient_of (- p) = (let (a, b) = quotient_of p in (- a, b))"
```
```  1087   by (cases p) (simp add: quotient_of_Fract)
```
```  1088
```
```  1089 lemma rat_minus_code [code abstract]:
```
```  1090   "quotient_of (p - q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
```
```  1091      in normalize (a * d - b * c, c * d))"
```
```  1092   by (cases p, cases q) (simp add: quotient_of_Fract)
```
```  1093
```
```  1094 lemma rat_times_code [code abstract]:
```
```  1095   "quotient_of (p * q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
```
```  1096      in normalize (a * b, c * d))"
```
```  1097   by (cases p, cases q) (simp add: quotient_of_Fract)
```
```  1098
```
```  1099 lemma rat_inverse_code [code abstract]:
```
```  1100   "quotient_of (inverse p) = (let (a, b) = quotient_of p
```
```  1101     in if a = 0 then (0, 1) else (sgn a * b, \<bar>a\<bar>))"
```
```  1102 proof (cases p)
```
```  1103   case (Fract a b) then show ?thesis
```
```  1104     by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract gcd_int.commute)
```
```  1105 qed
```
```  1106
```
```  1107 lemma rat_divide_code [code abstract]:
```
```  1108   "quotient_of (p / q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
```
```  1109      in normalize (a * d, c * b))"
```
```  1110   by (cases p, cases q) (simp add: quotient_of_Fract)
```
```  1111
```
```  1112 lemma rat_abs_code [code abstract]:
```
```  1113   "quotient_of \<bar>p\<bar> = (let (a, b) = quotient_of p in (\<bar>a\<bar>, b))"
```
```  1114   by (cases p) (simp add: quotient_of_Fract)
```
```  1115
```
```  1116 lemma rat_sgn_code [code abstract]:
```
```  1117   "quotient_of (sgn p) = (sgn (fst (quotient_of p)), 1)"
```
```  1118 proof (cases p)
```
```  1119   case (Fract a b) then show ?thesis
```
```  1120   by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract)
```
```  1121 qed
```
```  1122
```
```  1123 lemma rat_floor_code [code]:
```
```  1124   "floor p = (let (a, b) = quotient_of p in a div b)"
```
```  1125 by (cases p) (simp add: quotient_of_Fract floor_Fract)
```
```  1126
```
```  1127 instantiation rat :: equal
```
```  1128 begin
```
```  1129
```
```  1130 definition [code]:
```
```  1131   "HOL.equal a b \<longleftrightarrow> quotient_of a = quotient_of b"
```
```  1132
```
```  1133 instance proof
```
```  1134 qed (simp add: equal_rat_def quotient_of_inject_eq)
```
```  1135
```
```  1136 lemma rat_eq_refl [code nbe]:
```
```  1137   "HOL.equal (r::rat) r \<longleftrightarrow> True"
```
```  1138   by (rule equal_refl)
```
```  1139
```
```  1140 end
```
```  1141
```
```  1142 lemma rat_less_eq_code [code]:
```
```  1143   "p \<le> q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d \<le> c * b)"
```
```  1144   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
```
```  1145
```
```  1146 lemma rat_less_code [code]:
```
```  1147   "p < q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d < c * b)"
```
```  1148   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
```
```  1149
```
```  1150 lemma [code]:
```
```  1151   "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
```
```  1152   by (cases p) (simp add: quotient_of_Fract of_rat_rat)
```
```  1153
```
```  1154
```
```  1155 text {* Quickcheck *}
```
```  1156
```
```  1157 definition (in term_syntax)
```
```  1158   valterm_fract :: "int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> rat \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
```
```  1159   [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
```
```  1160
```
```  1161 notation fcomp (infixl "\<circ>>" 60)
```
```  1162 notation scomp (infixl "\<circ>\<rightarrow>" 60)
```
```  1163
```
```  1164 instantiation rat :: random
```
```  1165 begin
```
```  1166
```
```  1167 definition
```
```  1168   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>num. Random.range i \<circ>\<rightarrow> (\<lambda>denom. Pair (
```
```  1169      let j = Code_Numeral.int_of (denom + 1)
```
```  1170      in valterm_fract num (j, \<lambda>u. Code_Evaluation.term_of j))))"
```
```  1171
```
```  1172 instance ..
```
```  1173
```
```  1174 end
```
```  1175
```
```  1176 no_notation fcomp (infixl "\<circ>>" 60)
```
```  1177 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
```
```  1178
```
```  1179 instantiation rat :: exhaustive
```
```  1180 begin
```
```  1181
```
```  1182 definition
```
```  1183   "exhaustive_rat f d = Quickcheck_Exhaustive.exhaustive (%l. Quickcheck_Exhaustive.exhaustive (%k. f (Fract k (Code_Numeral.int_of l + 1))) d) d"
```
```  1184
```
```  1185 instance ..
```
```  1186
```
```  1187 end
```
```  1188
```
```  1189 instantiation rat :: full_exhaustive
```
```  1190 begin
```
```  1191
```
```  1192 definition
```
```  1193   "full_exhaustive_rat f d = Quickcheck_Exhaustive.full_exhaustive (%(l, _). Quickcheck_Exhaustive.full_exhaustive (%k.
```
```  1194      f (let j = Code_Numeral.int_of l + 1
```
```  1195         in valterm_fract k (j, %_. Code_Evaluation.term_of j))) d) d"
```
```  1196
```
```  1197 instance ..
```
```  1198
```
```  1199 end
```
```  1200
```
```  1201 instantiation rat :: partial_term_of
```
```  1202 begin
```
```  1203
```
```  1204 instance ..
```
```  1205
```
```  1206 end
```
```  1207
```
```  1208 lemma [code]:
```
```  1209   "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_variable p tt) == Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Rat.rat'') [])"
```
```  1210   "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_constructor 0 [l, k]) ==
```
```  1211      Code_Evaluation.App (Code_Evaluation.Const (STR ''Rat.Frct'')
```
```  1212      (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Product_Type.prod'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []],
```
```  1213         Typerep.Typerep (STR ''Rat.rat'') []])) (Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.Const (STR ''Product_Type.Pair'') (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Product_Type.prod'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []]]])) (partial_term_of (TYPE(int)) l)) (partial_term_of (TYPE(int)) k))"
```
```  1214 by (rule partial_term_of_anything)+
```
```  1215
```
```  1216 instantiation rat :: narrowing
```
```  1217 begin
```
```  1218
```
```  1219 definition
```
```  1220   "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.apply
```
```  1221     (Quickcheck_Narrowing.cons (%nom denom. Fract nom denom)) narrowing) narrowing"
```
```  1222
```
```  1223 instance ..
```
```  1224
```
```  1225 end
```
```  1226
```
```  1227
```
```  1228 subsection {* Setup for Nitpick *}
```
```  1229
```
```  1230 declaration {*
```
```  1231   Nitpick_HOL.register_frac_type @{type_name rat}
```
```  1232    [(@{const_name zero_rat_inst.zero_rat}, @{const_name Nitpick.zero_frac}),
```
```  1233     (@{const_name one_rat_inst.one_rat}, @{const_name Nitpick.one_frac}),
```
```  1234     (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
```
```  1235     (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
```
```  1236     (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
```
```  1237     (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
```
```  1238     (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
```
```  1239     (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
```
```  1240     (@{const_name field_char_0_class.of_rat}, @{const_name Nitpick.of_frac})]
```
```  1241 *}
```
```  1242
```
```  1243 lemmas [nitpick_unfold] = inverse_rat_inst.inverse_rat
```
```  1244   one_rat_inst.one_rat ord_rat_inst.less_rat
```
```  1245   ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
```
```  1246   uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
```
```  1247
```
```  1248 subsection{* Float syntax *}
```
```  1249
```
```  1250 syntax "_Float" :: "float_const \<Rightarrow> 'a"    ("_")
```
```  1251
```
```  1252 use "Tools/float_syntax.ML"
```
```  1253 setup Float_Syntax.setup
```
```  1254
```
```  1255 text{* Test: *}
```
```  1256 lemma "123.456 = -111.111 + 200 + 30 + 4 + 5/10 + 6/100 + (7/1000::rat)"
```
```  1257 by simp
```
```  1258
```
```  1259
```
```  1260 hide_const (open) normalize
```
```  1261
```
```  1262 end
```