src/HOL/Rat.thy
 author bulwahn Mon Jul 18 10:34:21 2011 +0200 (2011-07-18) changeset 43887 442aceb54969 parent 43733 a6ca7b83612f child 43889 90d24cafb05d permissions -rw-r--r--
adding narrowing instances for real and rational
1 (*  Title:  HOL/Rat.thy
2     Author: Markus Wenzel, TU Muenchen
3 *)
5 header {* Rational numbers *}
7 theory Rat
8 imports GCD Archimedean_Field
9 uses ("Tools/float_syntax.ML")
10 begin
12 subsection {* Rational numbers as quotient *}
14 subsubsection {* Construction of the type of rational numbers *}
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}"
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)
24 lemma refl_on_ratrel: "refl_on {x. snd x \<noteq> 0} ratrel"
25   by (auto simp add: refl_on_def ratrel_def)
27 lemma sym_ratrel: "sym ratrel"
28   by (simp add: ratrel_def sym_def)
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
46 lemma equiv_ratrel: "equiv {x. snd x \<noteq> 0} ratrel"
47   by (rule equivI [OF refl_on_ratrel sym_ratrel trans_ratrel])
49 lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
50 lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
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)
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
63 lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel `` {x} \<in> Rat"
64   by (simp add: Rat_def quotientI)
66 declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp]
69 subsubsection {* Representation and basic operations *}
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)})"
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)
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
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
118 instantiation rat :: comm_ring_1
119 begin
121 definition
122   Zero_rat_def: "0 = Fract 0 1"
124 definition
125   One_rat_def: "1 = Fract 1 1"
127 definition
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)})"
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
142 definition
143   minus_rat_def:
144   "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel `` {(- fst x, snd x)})"
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 split_paired_all)
150   then show ?thesis by (simp add: Fract_def minus_rat_def UN_ratrel)
151 qed
153 lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
154   by (cases "b = 0") (simp_all add: eq_rat)
156 definition
157   diff_rat_def: "q - r = q + - (r::rat)"
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)
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)})"
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
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
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
215 end
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)
220 lemma of_int_rat: "of_int k = Fract k 1"
221   by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
223 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
224   by (rule of_nat_rat [symmetric])
226 lemma Fract_of_int_eq: "Fract k 1 = of_int k"
227   by (rule of_int_rat [symmetric])
229 instantiation rat :: number_ring
230 begin
232 definition
233   rat_number_of_def: "number_of w = Fract w 1"
235 instance proof
236 qed (simp add: rat_number_of_def of_int_rat)
238 end
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)
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)
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)
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
272 subsubsection {* Function @{text normalize} *}
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
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)))"
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
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)
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)
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)
317 lemma normalize_stable [simp]:
318   "q > 0 \<Longrightarrow> coprime p q \<Longrightarrow> normalize (p, q) = (p, q)"
319   by (simp add: normalize_def)
321 lemma normalize_denom_zero [simp]:
322   "normalize (p, 0) = (0, 1)"
323   by (simp add: normalize_def)
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)
329 text{*
330   Decompose a fraction into normalized, i.e. coprime numerator and denominator:
331 *}
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))"
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
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
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)
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)
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)
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)
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
407 lemma quotient_of_inject_eq:
408   "quotient_of a = quotient_of b \<longleftrightarrow> a = b"
409   by (auto simp add: quotient_of_inject)
412 subsubsection {* The field of rational numbers *}
414 instantiation rat :: field_inverse_zero
415 begin
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)})"
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
429 definition
430   divide_rat_def: "q / r = q * inverse (r::rat)"
432 lemma divide_rat [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
433   by (simp add: divide_rat_def)
435 instance proof
436   fix q :: rat
437   assume "q \<noteq> 0"
438   then show "inverse q * q = 1" by (cases q rule: Rat_cases_nonzero)
439    (simp_all add: rat_number_expand eq_rat)
440 next
441   fix q r :: rat
442   show "q / r = q * inverse r" by (simp add: divide_rat_def)
443 next
444   show "inverse 0 = (0::rat)" by (simp add: rat_number_expand, simp add: rat_number_collapse)
445 qed
447 end
450 subsubsection {* Various *}
452 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
453   by (simp add: rat_number_expand)
455 lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
456   by (simp add: Fract_of_int_eq [symmetric])
458 lemma Fract_number_of_quotient:
459   "Fract (number_of k) (number_of l) = number_of k / number_of l"
460   unfolding Fract_of_int_quotient number_of_is_id number_of_eq ..
462 lemma Fract_1_number_of:
463   "Fract 1 (number_of k) = 1 / number_of k"
464   unfolding Fract_of_int_quotient number_of_eq by simp
466 subsubsection {* The ordered field of rational numbers *}
468 instantiation rat :: linorder
469 begin
471 definition
472   le_rat_def:
473    "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
474       {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
476 lemma le_rat [simp]:
477   assumes "b \<noteq> 0" and "d \<noteq> 0"
478   shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
479 proof -
480   have "(\<lambda>x y. {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})
481     respects2 ratrel"
482   proof (clarsimp simp add: congruent2_def)
483     fix a b a' b' c d c' d'::int
484     assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
485     assume eq1: "a * b' = a' * b"
486     assume eq2: "c * d' = c' * d"
488     let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
489     {
490       fix a b c d x :: int assume x: "x \<noteq> 0"
491       have "?le a b c d = ?le (a * x) (b * x) c d"
492       proof -
493         from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
494         hence "?le a b c d =
495             ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
496           by (simp add: mult_le_cancel_right)
497         also have "... = ?le (a * x) (b * x) c d"
498           by (simp add: mult_ac)
499         finally show ?thesis .
500       qed
501     } note le_factor = this
503     let ?D = "b * d" and ?D' = "b' * d'"
504     from neq have D: "?D \<noteq> 0" by simp
505     from neq have "?D' \<noteq> 0" by simp
506     hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
507       by (rule le_factor)
508     also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
509       by (simp add: mult_ac)
510     also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
511       by (simp only: eq1 eq2)
512     also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
513       by (simp add: mult_ac)
514     also from D have "... = ?le a' b' c' d'"
515       by (rule le_factor [symmetric])
516     finally show "?le a b c d = ?le a' b' c' d'" .
517   qed
518   with assms show ?thesis by (simp add: Fract_def le_rat_def UN_ratrel2)
519 qed
521 definition
522   less_rat_def: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
524 lemma less_rat [simp]:
525   assumes "b \<noteq> 0" and "d \<noteq> 0"
526   shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
527   using assms by (simp add: less_rat_def eq_rat order_less_le)
529 instance proof
530   fix q r s :: rat
531   {
532     assume "q \<le> r" and "r \<le> s"
533     then show "q \<le> s"
534     proof (induct q, induct r, induct s)
535       fix a b c d e f :: int
536       assume neq: "b > 0"  "d > 0"  "f > 0"
537       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
538       show "Fract a b \<le> Fract e f"
539       proof -
540         from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
541           by (auto simp add: zero_less_mult_iff linorder_neq_iff)
542         have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
543         proof -
544           from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
545             by simp
546           with ff show ?thesis by (simp add: mult_le_cancel_right)
547         qed
548         also have "... = (c * f) * (d * f) * (b * b)" by algebra
549         also have "... \<le> (e * d) * (d * f) * (b * b)"
550         proof -
551           from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
552             by simp
553           with bb show ?thesis by (simp add: mult_le_cancel_right)
554         qed
555         finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
556           by (simp only: mult_ac)
557         with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
558           by (simp add: mult_le_cancel_right)
559         with neq show ?thesis by simp
560       qed
561     qed
562   next
563     assume "q \<le> r" and "r \<le> q"
564     then show "q = r"
565     proof (induct q, induct r)
566       fix a b c d :: int
567       assume neq: "b > 0"  "d > 0"
568       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
569       show "Fract a b = Fract c d"
570       proof -
571         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
572           by simp
573         also have "... \<le> (a * d) * (b * d)"
574         proof -
575           from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
576             by simp
577           thus ?thesis by (simp only: mult_ac)
578         qed
579         finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
580         moreover from neq have "b * d \<noteq> 0" by simp
581         ultimately have "a * d = c * b" by simp
582         with neq show ?thesis by (simp add: eq_rat)
583       qed
584     qed
585   next
586     show "q \<le> q"
587       by (induct q) simp
588     show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
589       by (induct q, induct r) (auto simp add: le_less mult_commute)
590     show "q \<le> r \<or> r \<le> q"
591       by (induct q, induct r)
592          (simp add: mult_commute, rule linorder_linear)
593   }
594 qed
596 end
598 instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
599 begin
601 definition
602   abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
604 lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
605   by (auto simp add: abs_rat_def zabs_def Zero_rat_def not_less le_less eq_rat zero_less_mult_iff)
607 definition
608   sgn_rat_def: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
610 lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
611   unfolding Fract_of_int_eq
612   by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
613     (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
615 definition
616   "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
618 definition
619   "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
621 instance by intro_classes
622   (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
624 end
626 instance rat :: linordered_field_inverse_zero
627 proof
628   fix q r s :: rat
629   show "q \<le> r ==> s + q \<le> s + r"
630   proof (induct q, induct r, induct s)
631     fix a b c d e f :: int
632     assume neq: "b > 0"  "d > 0"  "f > 0"
633     assume le: "Fract a b \<le> Fract c d"
634     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
635     proof -
636       let ?F = "f * f" from neq have F: "0 < ?F"
637         by (auto simp add: zero_less_mult_iff)
638       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
639         by simp
640       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
641         by (simp add: mult_le_cancel_right)
642       with neq show ?thesis by (simp add: mult_ac int_distrib)
643     qed
644   qed
645   show "q < r ==> 0 < s ==> s * q < s * r"
646   proof (induct q, induct r, induct s)
647     fix a b c d e f :: int
648     assume neq: "b > 0"  "d > 0"  "f > 0"
649     assume le: "Fract a b < Fract c d"
650     assume gt: "0 < Fract e f"
651     show "Fract e f * Fract a b < Fract e f * Fract c d"
652     proof -
653       let ?E = "e * f" and ?F = "f * f"
654       from neq gt have "0 < ?E"
655         by (auto simp add: Zero_rat_def order_less_le eq_rat)
656       moreover from neq have "0 < ?F"
657         by (auto simp add: zero_less_mult_iff)
658       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
659         by simp
660       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
661         by (simp add: mult_less_cancel_right)
662       with neq show ?thesis
663         by (simp add: mult_ac)
664     qed
665   qed
666 qed auto
668 lemma Rat_induct_pos [case_names Fract, induct type: rat]:
669   assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
670   shows "P q"
671 proof (cases q)
672   have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
673   proof -
674     fix a::int and b::int
675     assume b: "b < 0"
676     hence "0 < -b" by simp
677     hence "P (Fract (-a) (-b))" by (rule step)
678     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
679   qed
680   case (Fract a b)
681   thus "P q" by (force simp add: linorder_neq_iff step step')
682 qed
684 lemma zero_less_Fract_iff:
685   "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
686   by (simp add: Zero_rat_def zero_less_mult_iff)
688 lemma Fract_less_zero_iff:
689   "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
690   by (simp add: Zero_rat_def mult_less_0_iff)
692 lemma zero_le_Fract_iff:
693   "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
694   by (simp add: Zero_rat_def zero_le_mult_iff)
696 lemma Fract_le_zero_iff:
697   "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
698   by (simp add: Zero_rat_def mult_le_0_iff)
700 lemma one_less_Fract_iff:
701   "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
702   by (simp add: One_rat_def mult_less_cancel_right_disj)
704 lemma Fract_less_one_iff:
705   "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
706   by (simp add: One_rat_def mult_less_cancel_right_disj)
708 lemma one_le_Fract_iff:
709   "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
710   by (simp add: One_rat_def mult_le_cancel_right)
712 lemma Fract_le_one_iff:
713   "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
714   by (simp add: One_rat_def mult_le_cancel_right)
717 subsubsection {* Rationals are an Archimedean field *}
719 lemma rat_floor_lemma:
720   shows "of_int (a div b) \<le> Fract a b \<and> Fract a b < of_int (a div b + 1)"
721 proof -
722   have "Fract a b = of_int (a div b) + Fract (a mod b) b"
723     by (cases "b = 0", simp, simp add: of_int_rat)
724   moreover have "0 \<le> Fract (a mod b) b \<and> Fract (a mod b) b < 1"
725     unfolding Fract_of_int_quotient
726     by (rule linorder_cases [of b 0]) (simp add: divide_nonpos_neg, simp, simp add: divide_nonneg_pos)
727   ultimately show ?thesis by simp
728 qed
730 instance rat :: archimedean_field
731 proof
732   fix r :: rat
733   show "\<exists>z. r \<le> of_int z"
734   proof (induct r)
735     case (Fract a b)
736     have "Fract a b \<le> of_int (a div b + 1)"
737       using rat_floor_lemma [of a b] by simp
738     then show "\<exists>z. Fract a b \<le> of_int z" ..
739   qed
740 qed
742 instantiation rat :: floor_ceiling
743 begin
745 definition [code del]:
746   "floor (x::rat) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
748 instance proof
749   fix x :: rat
750   show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
751     unfolding floor_rat_def using floor_exists1 by (rule theI')
752 qed
754 end
756 lemma floor_Fract: "floor (Fract a b) = a div b"
757   using rat_floor_lemma [of a b]
758   by (simp add: floor_unique)
761 subsection {* Linear arithmetic setup *}
763 declaration {*
764   K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
765     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
766   #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_int_eq_iff} RS iffD2]
767     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
768   #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
769       @{thm True_implies_equals},
770       read_instantiate @{context} [(("a", 0), "(number_of ?v)")] @{thm right_distrib},
771       @{thm divide_1}, @{thm divide_zero_left},
772       @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
773       @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
774       @{thm of_int_minus}, @{thm of_int_diff},
775       @{thm of_int_of_nat_eq}]
776   #> Lin_Arith.add_simprocs Numeral_Simprocs.field_cancel_numeral_factors
777   #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat => rat"})
778   #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int => rat"}))
779 *}
782 subsection {* Embedding from Rationals to other Fields *}
784 class field_char_0 = field + ring_char_0
786 subclass (in linordered_field) field_char_0 ..
788 context field_char_0
789 begin
791 definition of_rat :: "rat \<Rightarrow> 'a" where
792   "of_rat q = the_elem (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
794 end
796 lemma of_rat_congruent:
797   "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
798 apply (rule congruentI)
799 apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
800 apply (simp only: of_int_mult [symmetric])
801 done
803 lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
804   unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
806 lemma of_rat_0 [simp]: "of_rat 0 = 0"
807 by (simp add: Zero_rat_def of_rat_rat)
809 lemma of_rat_1 [simp]: "of_rat 1 = 1"
810 by (simp add: One_rat_def of_rat_rat)
812 lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
813 by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
815 lemma of_rat_minus: "of_rat (- a) = - of_rat a"
816 by (induct a, simp add: of_rat_rat)
818 lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
819 by (simp only: diff_minus of_rat_add of_rat_minus)
821 lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
822 apply (induct a, induct b, simp add: of_rat_rat)
823 apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
824 done
826 lemma nonzero_of_rat_inverse:
827   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
828 apply (rule inverse_unique [symmetric])
829 apply (simp add: of_rat_mult [symmetric])
830 done
832 lemma of_rat_inverse:
833   "(of_rat (inverse a)::'a::{field_char_0, field_inverse_zero}) =
834    inverse (of_rat a)"
835 by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
837 lemma nonzero_of_rat_divide:
838   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
839 by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
841 lemma of_rat_divide:
842   "(of_rat (a / b)::'a::{field_char_0, field_inverse_zero})
843    = of_rat a / of_rat b"
844 by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
846 lemma of_rat_power:
847   "(of_rat (a ^ n)::'a::field_char_0) = of_rat a ^ n"
848 by (induct n) (simp_all add: of_rat_mult)
850 lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
851 apply (induct a, induct b)
852 apply (simp add: of_rat_rat eq_rat)
853 apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
854 apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
855 done
857 lemma of_rat_less:
858   "(of_rat r :: 'a::linordered_field) < of_rat s \<longleftrightarrow> r < s"
859 proof (induct r, induct s)
860   fix a b c d :: int
861   assume not_zero: "b > 0" "d > 0"
862   then have "b * d > 0" by (rule mult_pos_pos)
863   have of_int_divide_less_eq:
864     "(of_int a :: 'a) / of_int b < of_int c / of_int d
865       \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
866     using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
867   show "(of_rat (Fract a b) :: 'a::linordered_field) < of_rat (Fract c d)
868     \<longleftrightarrow> Fract a b < Fract c d"
869     using not_zero `b * d > 0`
870     by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
871 qed
873 lemma of_rat_less_eq:
874   "(of_rat r :: 'a::linordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
875   unfolding le_less by (auto simp add: of_rat_less)
877 lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
879 lemma of_rat_eq_id [simp]: "of_rat = id"
880 proof
881   fix a
882   show "of_rat a = id a"
883   by (induct a)
884      (simp add: of_rat_rat Fract_of_int_eq [symmetric])
885 qed
887 text{*Collapse nested embeddings*}
888 lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
889 by (induct n) (simp_all add: of_rat_add)
891 lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
892 by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
894 lemma of_rat_number_of_eq [simp]:
895   "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
896 by (simp add: number_of_eq)
898 lemmas zero_rat = Zero_rat_def
899 lemmas one_rat = One_rat_def
901 abbreviation
902   rat_of_nat :: "nat \<Rightarrow> rat"
903 where
904   "rat_of_nat \<equiv> of_nat"
906 abbreviation
907   rat_of_int :: "int \<Rightarrow> rat"
908 where
909   "rat_of_int \<equiv> of_int"
911 subsection {* The Set of Rational Numbers *}
913 context field_char_0
914 begin
916 definition
917   Rats  :: "'a set" where
918   "Rats = range of_rat"
920 notation (xsymbols)
921   Rats  ("\<rat>")
923 end
925 lemma Rats_of_rat [simp]: "of_rat r \<in> Rats"
926 by (simp add: Rats_def)
928 lemma Rats_of_int [simp]: "of_int z \<in> Rats"
929 by (subst of_rat_of_int_eq [symmetric], rule Rats_of_rat)
931 lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
932 by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
934 lemma Rats_number_of [simp]:
935   "(number_of w::'a::{number_ring,field_char_0}) \<in> Rats"
936 by (subst of_rat_number_of_eq [symmetric], rule Rats_of_rat)
938 lemma Rats_0 [simp]: "0 \<in> Rats"
939 apply (unfold Rats_def)
940 apply (rule range_eqI)
941 apply (rule of_rat_0 [symmetric])
942 done
944 lemma Rats_1 [simp]: "1 \<in> Rats"
945 apply (unfold Rats_def)
946 apply (rule range_eqI)
947 apply (rule of_rat_1 [symmetric])
948 done
950 lemma Rats_add [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_add [symmetric])
954 done
956 lemma Rats_minus [simp]: "a \<in> Rats \<Longrightarrow> - a \<in> Rats"
957 apply (auto simp add: Rats_def)
958 apply (rule range_eqI)
959 apply (rule of_rat_minus [symmetric])
960 done
962 lemma Rats_diff [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a - b \<in> Rats"
963 apply (auto simp add: Rats_def)
964 apply (rule range_eqI)
965 apply (rule of_rat_diff [symmetric])
966 done
968 lemma Rats_mult [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a * b \<in> Rats"
969 apply (auto simp add: Rats_def)
970 apply (rule range_eqI)
971 apply (rule of_rat_mult [symmetric])
972 done
974 lemma nonzero_Rats_inverse:
975   fixes a :: "'a::field_char_0"
976   shows "\<lbrakk>a \<in> Rats; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Rats"
977 apply (auto simp add: Rats_def)
978 apply (rule range_eqI)
979 apply (erule nonzero_of_rat_inverse [symmetric])
980 done
982 lemma Rats_inverse [simp]:
983   fixes a :: "'a::{field_char_0, field_inverse_zero}"
984   shows "a \<in> Rats \<Longrightarrow> inverse a \<in> Rats"
985 apply (auto simp add: Rats_def)
986 apply (rule range_eqI)
987 apply (rule of_rat_inverse [symmetric])
988 done
990 lemma nonzero_Rats_divide:
991   fixes a b :: "'a::field_char_0"
992   shows "\<lbrakk>a \<in> Rats; b \<in> Rats; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
993 apply (auto simp add: Rats_def)
994 apply (rule range_eqI)
995 apply (erule nonzero_of_rat_divide [symmetric])
996 done
998 lemma Rats_divide [simp]:
999   fixes a b :: "'a::{field_char_0, field_inverse_zero}"
1000   shows "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
1001 apply (auto simp add: Rats_def)
1002 apply (rule range_eqI)
1003 apply (rule of_rat_divide [symmetric])
1004 done
1006 lemma Rats_power [simp]:
1007   fixes a :: "'a::field_char_0"
1008   shows "a \<in> Rats \<Longrightarrow> a ^ n \<in> Rats"
1009 apply (auto simp add: Rats_def)
1010 apply (rule range_eqI)
1011 apply (rule of_rat_power [symmetric])
1012 done
1014 lemma Rats_cases [cases set: Rats]:
1015   assumes "q \<in> \<rat>"
1016   obtains (of_rat) r where "q = of_rat r"
1017   unfolding Rats_def
1018 proof -
1019   from `q \<in> \<rat>` have "q \<in> range of_rat" unfolding Rats_def .
1020   then obtain r where "q = of_rat r" ..
1021   then show thesis ..
1022 qed
1024 lemma Rats_induct [case_names of_rat, induct set: Rats]:
1025   "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
1026   by (rule Rats_cases) auto
1029 subsection {* Implementation of rational numbers as pairs of integers *}
1031 definition Frct :: "int \<times> int \<Rightarrow> rat" where
1032   [simp]: "Frct p = Fract (fst p) (snd p)"
1034 lemma [code abstype]:
1035   "Frct (quotient_of q) = q"
1036   by (cases q) (auto intro: quotient_of_eq)
1038 lemma Frct_code_post [code_post]:
1039   "Frct (0, k) = 0"
1040   "Frct (k, 0) = 0"
1041   "Frct (1, 1) = 1"
1042   "Frct (number_of k, 1) = number_of k"
1043   "Frct (1, number_of k) = 1 / number_of k"
1044   "Frct (number_of k, number_of l) = number_of k / number_of l"
1045   by (simp_all add: rat_number_collapse Fract_number_of_quotient Fract_1_number_of)
1047 declare quotient_of_Fract [code abstract]
1049 lemma rat_zero_code [code abstract]:
1050   "quotient_of 0 = (0, 1)"
1051   by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
1053 lemma rat_one_code [code abstract]:
1054   "quotient_of 1 = (1, 1)"
1055   by (simp add: One_rat_def quotient_of_Fract normalize_def)
1057 lemma rat_plus_code [code abstract]:
1058   "quotient_of (p + q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
1059      in normalize (a * d + b * c, c * d))"
1060   by (cases p, cases q) (simp add: quotient_of_Fract)
1062 lemma rat_uminus_code [code abstract]:
1063   "quotient_of (- p) = (let (a, b) = quotient_of p in (- a, b))"
1064   by (cases p) (simp add: quotient_of_Fract)
1066 lemma rat_minus_code [code abstract]:
1067   "quotient_of (p - q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
1068      in normalize (a * d - b * c, c * d))"
1069   by (cases p, cases q) (simp add: quotient_of_Fract)
1071 lemma rat_times_code [code abstract]:
1072   "quotient_of (p * q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
1073      in normalize (a * b, c * d))"
1074   by (cases p, cases q) (simp add: quotient_of_Fract)
1076 lemma rat_inverse_code [code abstract]:
1077   "quotient_of (inverse p) = (let (a, b) = quotient_of p
1078     in if a = 0 then (0, 1) else (sgn a * b, \<bar>a\<bar>))"
1079 proof (cases p)
1080   case (Fract a b) then show ?thesis
1081     by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract gcd_int.commute)
1082 qed
1084 lemma rat_divide_code [code abstract]:
1085   "quotient_of (p / q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
1086      in normalize (a * d, c * b))"
1087   by (cases p, cases q) (simp add: quotient_of_Fract)
1089 lemma rat_abs_code [code abstract]:
1090   "quotient_of \<bar>p\<bar> = (let (a, b) = quotient_of p in (\<bar>a\<bar>, b))"
1091   by (cases p) (simp add: quotient_of_Fract)
1093 lemma rat_sgn_code [code abstract]:
1094   "quotient_of (sgn p) = (sgn (fst (quotient_of p)), 1)"
1095 proof (cases p)
1096   case (Fract a b) then show ?thesis
1097   by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract)
1098 qed
1100 lemma rat_floor_code [code]:
1101   "floor p = (let (a, b) = quotient_of p in a div b)"
1102 by (cases p) (simp add: quotient_of_Fract floor_Fract)
1104 instantiation rat :: equal
1105 begin
1107 definition [code]:
1108   "HOL.equal a b \<longleftrightarrow> quotient_of a = quotient_of b"
1110 instance proof
1111 qed (simp add: equal_rat_def quotient_of_inject_eq)
1113 lemma rat_eq_refl [code nbe]:
1114   "HOL.equal (r::rat) r \<longleftrightarrow> True"
1115   by (rule equal_refl)
1117 end
1119 lemma rat_less_eq_code [code]:
1120   "p \<le> q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d \<le> c * b)"
1121   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
1123 lemma rat_less_code [code]:
1124   "p < q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d < c * b)"
1125   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
1127 lemma [code]:
1128   "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
1129   by (cases p) (simp add: quotient_of_Fract of_rat_rat)
1131 definition (in term_syntax)
1132   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
1133   [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
1135 notation fcomp (infixl "\<circ>>" 60)
1136 notation scomp (infixl "\<circ>\<rightarrow>" 60)
1138 instantiation rat :: random
1139 begin
1141 definition
1142   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>num. Random.range i \<circ>\<rightarrow> (\<lambda>denom. Pair (
1143      let j = Code_Numeral.int_of (denom + 1)
1144      in valterm_fract num (j, \<lambda>u. Code_Evaluation.term_of j))))"
1146 instance ..
1148 end
1150 no_notation fcomp (infixl "\<circ>>" 60)
1151 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
1153 instantiation rat :: exhaustive
1154 begin
1156 definition
1157   "exhaustive f d = exhaustive (%k. exhaustive (%l. f (Fract (Code_Numeral.int_of k) (Code_Numeral.int_of l))) d) d"
1159 instance ..
1161 end
1163 instantiation rat :: full_exhaustive
1164 begin
1166 definition
1167   "full_exhaustive f d = full_exhaustive (%(k, kt). full_exhaustive (%(l, lt).
1168      f (valterm_fract (Code_Numeral.int_of k, %_. Code_Evaluation.term_of (Code_Numeral.int_of k)) (Code_Numeral.int_of l, %_. Code_Evaluation.term_of (Code_Numeral.int_of l)))) d) d"
1170 instance ..
1172 end
1174 instantiation rat :: narrowing
1175 begin
1177 definition
1178   "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Fract) narrowing) narrowing"
1180 instance ..
1182 end
1185 text {* Setup for SML code generator *}
1187 types_code
1188   rat ("(int */ int)")
1189 attach (term_of) {*
1190 fun term_of_rat (p, q) =
1191   let
1192     val rT = Type ("Rat.rat", [])
1193   in
1194     if q = 1 orelse p = 0 then HOLogic.mk_number rT p
1195     else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} \$
1196       HOLogic.mk_number rT p \$ HOLogic.mk_number rT q
1197   end;
1198 *}
1199 attach (test) {*
1200 fun gen_rat i =
1201   let
1202     val p = random_range 0 i;
1203     val q = random_range 1 (i + 1);
1204     val g = Integer.gcd p q;
1205     val p' = p div g;
1206     val q' = q div g;
1207     val r = (if one_of [true, false] then p' else ~ p',
1208       if p' = 0 then 1 else q')
1209   in
1210     (r, fn () => term_of_rat r)
1211   end;
1212 *}
1214 consts_code
1215   Fract ("(_,/ _)")
1217 consts_code
1218   quotient_of ("{*normalize*}")
1220 consts_code
1221   "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
1222 attach {*
1223 fun rat_of_int i = (i, 1);
1224 *}
1226 declaration {*
1227   Nitpick_HOL.register_frac_type @{type_name rat}
1228    [(@{const_name zero_rat_inst.zero_rat}, @{const_name Nitpick.zero_frac}),
1229     (@{const_name one_rat_inst.one_rat}, @{const_name Nitpick.one_frac}),
1230     (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
1231     (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
1232     (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
1233     (@{const_name number_rat_inst.number_of_rat}, @{const_name Nitpick.number_of_frac}),
1234     (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
1235     (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
1236     (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
1237     (@{const_name field_char_0_class.of_rat}, @{const_name Nitpick.of_frac}),
1238     (@{const_name field_char_0_class.Rats}, @{const_abbrev UNIV})]
1239 *}
1241 lemmas [nitpick_unfold] = inverse_rat_inst.inverse_rat
1242   number_rat_inst.number_of_rat one_rat_inst.one_rat ord_rat_inst.less_rat
1243   ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
1244   uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
1246 subsection{* Float syntax *}
1248 syntax "_Float" :: "float_const \<Rightarrow> 'a"    ("_")
1250 use "Tools/float_syntax.ML"
1251 setup Float_Syntax.setup
1253 text{* Test: *}
1254 lemma "123.456 = -111.111 + 200 + 30 + 4 + 5/10 + 6/100 + (7/1000::rat)"
1255 by simp
1258 hide_const (open) normalize
1260 end