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