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