src/HOL/Rat.thy
author blanchet
Mon Aug 09 12:53:16 2010 +0200 (2010-08-09)
changeset 38287 796302ca3611
parent 38242 f26d590dce0f
child 38857 97775f3e8722
permissions -rw-r--r--
replace "setup" with "declaration"
     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_inverse_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   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
   446 
   447 end
   448 
   449 
   450 subsubsection {* Various *}
   451 
   452 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
   453   by (simp add: rat_number_expand)
   454 
   455 lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
   456   by (simp add: Fract_of_int_eq [symmetric])
   457 
   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 ..
   461 
   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
   465 
   466 subsubsection {* The ordered field of rational numbers *}
   467 
   468 instantiation rat :: linorder
   469 begin
   470 
   471 definition
   472   le_rat_def:
   473    "q \<le> r \<longleftrightarrow> contents (\<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)})"
   475 
   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"
   487 
   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
   502 
   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
   520 
   521 definition
   522   less_rat_def: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
   523 
   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)
   528 
   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
   595 
   596 end
   597 
   598 instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
   599 begin
   600 
   601 definition
   602   abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
   603 
   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)
   606 
   607 definition
   608   sgn_rat_def: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
   609 
   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)
   614 
   615 definition
   616   "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
   617 
   618 definition
   619   "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
   620 
   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)
   623 
   624 end
   625 
   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
   667 
   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
   683 
   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)
   687 
   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)
   691 
   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)
   695 
   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)
   699 
   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)
   703 
   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)
   707 
   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)
   711 
   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)
   715 
   716 
   717 subsubsection {* Rationals are an Archimedean field *}
   718 
   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
   729 
   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
   741 
   742 lemma floor_Fract: "floor (Fract a b) = a div b"
   743   using rat_floor_lemma [of a b]
   744   by (simp add: floor_unique)
   745 
   746 
   747 subsection {* Linear arithmetic setup *}
   748 
   749 declaration {*
   750   K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
   751     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
   752   #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_int_eq_iff} RS iffD2]
   753     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
   754   #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
   755       @{thm True_implies_equals},
   756       read_instantiate @{context} [(("a", 0), "(number_of ?v)")] @{thm right_distrib},
   757       @{thm divide_1}, @{thm divide_zero_left},
   758       @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
   759       @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
   760       @{thm of_int_minus}, @{thm of_int_diff},
   761       @{thm of_int_of_nat_eq}]
   762   #> Lin_Arith.add_simprocs Numeral_Simprocs.field_cancel_numeral_factors
   763   #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat => rat"})
   764   #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int => rat"}))
   765 *}
   766 
   767 
   768 subsection {* Embedding from Rationals to other Fields *}
   769 
   770 class field_char_0 = field + ring_char_0
   771 
   772 subclass (in linordered_field) field_char_0 ..
   773 
   774 context field_char_0
   775 begin
   776 
   777 definition of_rat :: "rat \<Rightarrow> 'a" where
   778   "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
   779 
   780 end
   781 
   782 lemma of_rat_congruent:
   783   "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
   784 apply (rule congruent.intro)
   785 apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
   786 apply (simp only: of_int_mult [symmetric])
   787 done
   788 
   789 lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
   790   unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
   791 
   792 lemma of_rat_0 [simp]: "of_rat 0 = 0"
   793 by (simp add: Zero_rat_def of_rat_rat)
   794 
   795 lemma of_rat_1 [simp]: "of_rat 1 = 1"
   796 by (simp add: One_rat_def of_rat_rat)
   797 
   798 lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
   799 by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
   800 
   801 lemma of_rat_minus: "of_rat (- a) = - of_rat a"
   802 by (induct a, simp add: of_rat_rat)
   803 
   804 lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
   805 by (simp only: diff_minus of_rat_add of_rat_minus)
   806 
   807 lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
   808 apply (induct a, induct b, simp add: of_rat_rat)
   809 apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
   810 done
   811 
   812 lemma nonzero_of_rat_inverse:
   813   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
   814 apply (rule inverse_unique [symmetric])
   815 apply (simp add: of_rat_mult [symmetric])
   816 done
   817 
   818 lemma of_rat_inverse:
   819   "(of_rat (inverse a)::'a::{field_char_0, field_inverse_zero}) =
   820    inverse (of_rat a)"
   821 by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
   822 
   823 lemma nonzero_of_rat_divide:
   824   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
   825 by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
   826 
   827 lemma of_rat_divide:
   828   "(of_rat (a / b)::'a::{field_char_0, field_inverse_zero})
   829    = of_rat a / of_rat b"
   830 by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
   831 
   832 lemma of_rat_power:
   833   "(of_rat (a ^ n)::'a::field_char_0) = of_rat a ^ n"
   834 by (induct n) (simp_all add: of_rat_mult)
   835 
   836 lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
   837 apply (induct a, induct b)
   838 apply (simp add: of_rat_rat eq_rat)
   839 apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
   840 apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
   841 done
   842 
   843 lemma of_rat_less:
   844   "(of_rat r :: 'a::linordered_field) < of_rat s \<longleftrightarrow> r < s"
   845 proof (induct r, induct s)
   846   fix a b c d :: int
   847   assume not_zero: "b > 0" "d > 0"
   848   then have "b * d > 0" by (rule mult_pos_pos)
   849   have of_int_divide_less_eq:
   850     "(of_int a :: 'a) / of_int b < of_int c / of_int d
   851       \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
   852     using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
   853   show "(of_rat (Fract a b) :: 'a::linordered_field) < of_rat (Fract c d)
   854     \<longleftrightarrow> Fract a b < Fract c d"
   855     using not_zero `b * d > 0`
   856     by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
   857 qed
   858 
   859 lemma of_rat_less_eq:
   860   "(of_rat r :: 'a::linordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
   861   unfolding le_less by (auto simp add: of_rat_less)
   862 
   863 lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
   864 
   865 lemma of_rat_eq_id [simp]: "of_rat = id"
   866 proof
   867   fix a
   868   show "of_rat a = id a"
   869   by (induct a)
   870      (simp add: of_rat_rat Fract_of_int_eq [symmetric])
   871 qed
   872 
   873 text{*Collapse nested embeddings*}
   874 lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
   875 by (induct n) (simp_all add: of_rat_add)
   876 
   877 lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
   878 by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
   879 
   880 lemma of_rat_number_of_eq [simp]:
   881   "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
   882 by (simp add: number_of_eq)
   883 
   884 lemmas zero_rat = Zero_rat_def
   885 lemmas one_rat = One_rat_def
   886 
   887 abbreviation
   888   rat_of_nat :: "nat \<Rightarrow> rat"
   889 where
   890   "rat_of_nat \<equiv> of_nat"
   891 
   892 abbreviation
   893   rat_of_int :: "int \<Rightarrow> rat"
   894 where
   895   "rat_of_int \<equiv> of_int"
   896 
   897 subsection {* The Set of Rational Numbers *}
   898 
   899 context field_char_0
   900 begin
   901 
   902 definition
   903   Rats  :: "'a set" where
   904   "Rats = range of_rat"
   905 
   906 notation (xsymbols)
   907   Rats  ("\<rat>")
   908 
   909 end
   910 
   911 lemma Rats_of_rat [simp]: "of_rat r \<in> Rats"
   912 by (simp add: Rats_def)
   913 
   914 lemma Rats_of_int [simp]: "of_int z \<in> Rats"
   915 by (subst of_rat_of_int_eq [symmetric], rule Rats_of_rat)
   916 
   917 lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
   918 by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
   919 
   920 lemma Rats_number_of [simp]:
   921   "(number_of w::'a::{number_ring,field_char_0}) \<in> Rats"
   922 by (subst of_rat_number_of_eq [symmetric], rule Rats_of_rat)
   923 
   924 lemma Rats_0 [simp]: "0 \<in> Rats"
   925 apply (unfold Rats_def)
   926 apply (rule range_eqI)
   927 apply (rule of_rat_0 [symmetric])
   928 done
   929 
   930 lemma Rats_1 [simp]: "1 \<in> Rats"
   931 apply (unfold Rats_def)
   932 apply (rule range_eqI)
   933 apply (rule of_rat_1 [symmetric])
   934 done
   935 
   936 lemma Rats_add [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a + b \<in> Rats"
   937 apply (auto simp add: Rats_def)
   938 apply (rule range_eqI)
   939 apply (rule of_rat_add [symmetric])
   940 done
   941 
   942 lemma Rats_minus [simp]: "a \<in> Rats \<Longrightarrow> - a \<in> Rats"
   943 apply (auto simp add: Rats_def)
   944 apply (rule range_eqI)
   945 apply (rule of_rat_minus [symmetric])
   946 done
   947 
   948 lemma Rats_diff [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a - b \<in> Rats"
   949 apply (auto simp add: Rats_def)
   950 apply (rule range_eqI)
   951 apply (rule of_rat_diff [symmetric])
   952 done
   953 
   954 lemma Rats_mult [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a * b \<in> Rats"
   955 apply (auto simp add: Rats_def)
   956 apply (rule range_eqI)
   957 apply (rule of_rat_mult [symmetric])
   958 done
   959 
   960 lemma nonzero_Rats_inverse:
   961   fixes a :: "'a::field_char_0"
   962   shows "\<lbrakk>a \<in> Rats; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Rats"
   963 apply (auto simp add: Rats_def)
   964 apply (rule range_eqI)
   965 apply (erule nonzero_of_rat_inverse [symmetric])
   966 done
   967 
   968 lemma Rats_inverse [simp]:
   969   fixes a :: "'a::{field_char_0, field_inverse_zero}"
   970   shows "a \<in> Rats \<Longrightarrow> inverse a \<in> Rats"
   971 apply (auto simp add: Rats_def)
   972 apply (rule range_eqI)
   973 apply (rule of_rat_inverse [symmetric])
   974 done
   975 
   976 lemma nonzero_Rats_divide:
   977   fixes a b :: "'a::field_char_0"
   978   shows "\<lbrakk>a \<in> Rats; b \<in> Rats; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
   979 apply (auto simp add: Rats_def)
   980 apply (rule range_eqI)
   981 apply (erule nonzero_of_rat_divide [symmetric])
   982 done
   983 
   984 lemma Rats_divide [simp]:
   985   fixes a b :: "'a::{field_char_0, field_inverse_zero}"
   986   shows "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
   987 apply (auto simp add: Rats_def)
   988 apply (rule range_eqI)
   989 apply (rule of_rat_divide [symmetric])
   990 done
   991 
   992 lemma Rats_power [simp]:
   993   fixes a :: "'a::field_char_0"
   994   shows "a \<in> Rats \<Longrightarrow> a ^ n \<in> Rats"
   995 apply (auto simp add: Rats_def)
   996 apply (rule range_eqI)
   997 apply (rule of_rat_power [symmetric])
   998 done
   999 
  1000 lemma Rats_cases [cases set: Rats]:
  1001   assumes "q \<in> \<rat>"
  1002   obtains (of_rat) r where "q = of_rat r"
  1003   unfolding Rats_def
  1004 proof -
  1005   from `q \<in> \<rat>` have "q \<in> range of_rat" unfolding Rats_def .
  1006   then obtain r where "q = of_rat r" ..
  1007   then show thesis ..
  1008 qed
  1009 
  1010 lemma Rats_induct [case_names of_rat, induct set: Rats]:
  1011   "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
  1012   by (rule Rats_cases) auto
  1013 
  1014 
  1015 subsection {* Implementation of rational numbers as pairs of integers *}
  1016 
  1017 definition Frct :: "int \<times> int \<Rightarrow> rat" where
  1018   [simp]: "Frct p = Fract (fst p) (snd p)"
  1019 
  1020 lemma [code abstype]:
  1021   "Frct (quotient_of q) = q"
  1022   by (cases q) (auto intro: quotient_of_eq)
  1023 
  1024 lemma Frct_code_post [code_post]:
  1025   "Frct (0, k) = 0"
  1026   "Frct (k, 0) = 0"
  1027   "Frct (1, 1) = 1"
  1028   "Frct (number_of k, 1) = number_of k"
  1029   "Frct (1, number_of k) = 1 / number_of k"
  1030   "Frct (number_of k, number_of l) = number_of k / number_of l"
  1031   by (simp_all add: rat_number_collapse Fract_number_of_quotient Fract_1_number_of)
  1032 
  1033 declare quotient_of_Fract [code abstract]
  1034 
  1035 lemma rat_zero_code [code abstract]:
  1036   "quotient_of 0 = (0, 1)"
  1037   by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
  1038 
  1039 lemma rat_one_code [code abstract]:
  1040   "quotient_of 1 = (1, 1)"
  1041   by (simp add: One_rat_def quotient_of_Fract normalize_def)
  1042 
  1043 lemma rat_plus_code [code abstract]:
  1044   "quotient_of (p + q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
  1045      in normalize (a * d + b * c, c * d))"
  1046   by (cases p, cases q) (simp add: quotient_of_Fract)
  1047 
  1048 lemma rat_uminus_code [code abstract]:
  1049   "quotient_of (- p) = (let (a, b) = quotient_of p in (- a, b))"
  1050   by (cases p) (simp add: quotient_of_Fract)
  1051 
  1052 lemma rat_minus_code [code abstract]:
  1053   "quotient_of (p - q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
  1054      in normalize (a * d - b * c, c * d))"
  1055   by (cases p, cases q) (simp add: quotient_of_Fract)
  1056 
  1057 lemma rat_times_code [code abstract]:
  1058   "quotient_of (p * q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
  1059      in normalize (a * b, c * d))"
  1060   by (cases p, cases q) (simp add: quotient_of_Fract)
  1061 
  1062 lemma rat_inverse_code [code abstract]:
  1063   "quotient_of (inverse p) = (let (a, b) = quotient_of p
  1064     in if a = 0 then (0, 1) else (sgn a * b, \<bar>a\<bar>))"
  1065 proof (cases p)
  1066   case (Fract a b) then show ?thesis
  1067     by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract gcd_int.commute)
  1068 qed
  1069 
  1070 lemma rat_divide_code [code abstract]:
  1071   "quotient_of (p / q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
  1072      in normalize (a * d, c * b))"
  1073   by (cases p, cases q) (simp add: quotient_of_Fract)
  1074 
  1075 lemma rat_abs_code [code abstract]:
  1076   "quotient_of \<bar>p\<bar> = (let (a, b) = quotient_of p in (\<bar>a\<bar>, b))"
  1077   by (cases p) (simp add: quotient_of_Fract)
  1078 
  1079 lemma rat_sgn_code [code abstract]:
  1080   "quotient_of (sgn p) = (sgn (fst (quotient_of p)), 1)"
  1081 proof (cases p)
  1082   case (Fract a b) then show ?thesis
  1083   by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract)
  1084 qed
  1085 
  1086 instantiation rat :: eq
  1087 begin
  1088 
  1089 definition [code]:
  1090   "eq_class.eq a b \<longleftrightarrow> quotient_of a = quotient_of b"
  1091 
  1092 instance proof
  1093 qed (simp add: eq_rat_def quotient_of_inject_eq)
  1094 
  1095 lemma rat_eq_refl [code nbe]:
  1096   "eq_class.eq (r::rat) r \<longleftrightarrow> True"
  1097   by (rule HOL.eq_refl)
  1098 
  1099 end
  1100 
  1101 lemma rat_less_eq_code [code]:
  1102   "p \<le> q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d \<le> c * b)"
  1103   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
  1104 
  1105 lemma rat_less_code [code]:
  1106   "p < q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d < c * b)"
  1107   by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
  1108 
  1109 lemma [code]:
  1110   "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
  1111   by (cases p) (simp add: quotient_of_Fract of_rat_rat)
  1112 
  1113 definition (in term_syntax)
  1114   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
  1115   [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
  1116 
  1117 notation fcomp (infixl "\<circ>>" 60)
  1118 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1119 
  1120 instantiation rat :: random
  1121 begin
  1122 
  1123 definition
  1124   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>num. Random.range i \<circ>\<rightarrow> (\<lambda>denom. Pair (
  1125      let j = Code_Numeral.int_of (denom + 1)
  1126      in valterm_fract num (j, \<lambda>u. Code_Evaluation.term_of j))))"
  1127 
  1128 instance ..
  1129 
  1130 end
  1131 
  1132 no_notation fcomp (infixl "\<circ>>" 60)
  1133 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1134 
  1135 text {* Setup for SML code generator *}
  1136 
  1137 types_code
  1138   rat ("(int */ int)")
  1139 attach (term_of) {*
  1140 fun term_of_rat (p, q) =
  1141   let
  1142     val rT = Type ("Rat.rat", [])
  1143   in
  1144     if q = 1 orelse p = 0 then HOLogic.mk_number rT p
  1145     else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} $
  1146       HOLogic.mk_number rT p $ HOLogic.mk_number rT q
  1147   end;
  1148 *}
  1149 attach (test) {*
  1150 fun gen_rat i =
  1151   let
  1152     val p = random_range 0 i;
  1153     val q = random_range 1 (i + 1);
  1154     val g = Integer.gcd p q;
  1155     val p' = p div g;
  1156     val q' = q div g;
  1157     val r = (if one_of [true, false] then p' else ~ p',
  1158       if p' = 0 then 1 else q')
  1159   in
  1160     (r, fn () => term_of_rat r)
  1161   end;
  1162 *}
  1163 
  1164 consts_code
  1165   Fract ("(_,/ _)")
  1166 
  1167 consts_code
  1168   quotient_of ("{*normalize*}")
  1169 
  1170 consts_code
  1171   "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
  1172 attach {*
  1173 fun rat_of_int i = (i, 1);
  1174 *}
  1175 
  1176 declaration {*
  1177   Nitpick_HOL.register_frac_type @{type_name rat}
  1178    [(@{const_name zero_rat_inst.zero_rat}, @{const_name Nitpick.zero_frac}),
  1179     (@{const_name one_rat_inst.one_rat}, @{const_name Nitpick.one_frac}),
  1180     (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
  1181     (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
  1182     (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
  1183     (@{const_name number_rat_inst.number_of_rat}, @{const_name Nitpick.number_of_frac}),
  1184     (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
  1185     (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
  1186     (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
  1187     (@{const_name field_char_0_class.of_rat}, @{const_name Nitpick.of_frac}),
  1188     (@{const_name field_char_0_class.Rats}, @{const_abbrev UNIV})]
  1189 *}
  1190 
  1191 lemmas [nitpick_def] = inverse_rat_inst.inverse_rat
  1192   number_rat_inst.number_of_rat one_rat_inst.one_rat ord_rat_inst.less_rat
  1193   ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
  1194   uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
  1195 
  1196 subsection{* Float syntax *}
  1197 
  1198 syntax "_Float" :: "float_const \<Rightarrow> 'a"    ("_")
  1199 
  1200 use "Tools/float_syntax.ML"
  1201 setup Float_Syntax.setup
  1202 
  1203 text{* Test: *}
  1204 lemma "123.456 = -111.111 + 200 + 30 + 4 + 5/10 + 6/100 + (7/1000::rat)"
  1205 by simp
  1206 
  1207 
  1208 hide_const (open) normalize
  1209 
  1210 end