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