src/HOL/Real/Rational.thy
 author nipkow Tue Oct 23 23:27:23 2007 +0200 (2007-10-23) changeset 25162 ad4d5365d9d8 parent 24661 a705b9834590 child 25502 9200b36280c0 permissions -rw-r--r--
went back to >0
```     1 (*  Title: HOL/Library/Rational.thy
```
```     2     ID:    \$Id\$
```
```     3     Author: Markus Wenzel, TU Muenchen
```
```     4 *)
```
```     5
```
```     6 header {* Rational numbers *}
```
```     7
```
```     8 theory Rational
```
```     9 imports Abstract_Rat
```
```    10 uses ("rat_arith.ML")
```
```    11 begin
```
```    12
```
```    13 subsection {* Rational numbers *}
```
```    14
```
```    15 subsubsection {* Equivalence of fractions *}
```
```    16
```
```    17 definition
```
```    18   fraction :: "(int \<times> int) set" where
```
```    19   "fraction = {x. snd x \<noteq> 0}"
```
```    20
```
```    21 definition
```
```    22   ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
```
```    23   "ratrel = {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
```
```    24
```
```    25 lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
```
```    26 by (simp add: fraction_def)
```
```    27
```
```    28 lemma ratrel_iff [simp]:
```
```    29   "((x,y) \<in> ratrel) =
```
```    30    (snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
```
```    31 by (simp add: ratrel_def)
```
```    32
```
```    33 lemma refl_ratrel: "refl fraction ratrel"
```
```    34 by (auto simp add: refl_def fraction_def ratrel_def)
```
```    35
```
```    36 lemma sym_ratrel: "sym ratrel"
```
```    37 by (simp add: ratrel_def sym_def)
```
```    38
```
```    39 lemma trans_ratrel_lemma:
```
```    40   assumes 1: "a * b' = a' * b"
```
```    41   assumes 2: "a' * b'' = a'' * b'"
```
```    42   assumes 3: "b' \<noteq> (0::int)"
```
```    43   shows "a * b'' = a'' * b"
```
```    44 proof -
```
```    45   have "b' * (a * b'') = b'' * (a * b')" by simp
```
```    46   also note 1
```
```    47   also have "b'' * (a' * b) = b * (a' * b'')" by simp
```
```    48   also note 2
```
```    49   also have "b * (a'' * b') = b' * (a'' * b)" by simp
```
```    50   finally have "b' * (a * b'') = b' * (a'' * b)" .
```
```    51   with 3 show "a * b'' = a'' * b" by simp
```
```    52 qed
```
```    53
```
```    54 lemma trans_ratrel: "trans ratrel"
```
```    55 by (auto simp add: trans_def elim: trans_ratrel_lemma)
```
```    56
```
```    57 lemma equiv_ratrel: "equiv fraction ratrel"
```
```    58 by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
```
```    59
```
```    60 lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
```
```    61
```
```    62 lemma equiv_ratrel_iff2:
```
```    63   "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
```
```    64     \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
```
```    65 by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all)
```
```    66
```
```    67
```
```    68 subsubsection {* The type of rational numbers *}
```
```    69
```
```    70 typedef (Rat) rat = "fraction//ratrel"
```
```    71 proof
```
```    72   have "(0,1) \<in> fraction" by (simp add: fraction_def)
```
```    73   thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI)
```
```    74 qed
```
```    75
```
```    76 lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat"
```
```    77 by (simp add: Rat_def quotientI)
```
```    78
```
```    79 declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
```
```    80
```
```    81
```
```    82 definition
```
```    83   Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
```
```    84   [code func del]: "Fract a b = Abs_Rat (ratrel``{(a,b)})"
```
```    85
```
```    86 lemma Fract_zero:
```
```    87   "Fract k 0 = Fract l 0"
```
```    88   by (simp add: Fract_def ratrel_def)
```
```    89
```
```    90 theorem Rat_cases [case_names Fract, cases type: rat]:
```
```    91     "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
```
```    92   by (cases q) (clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
```
```    93
```
```    94 theorem Rat_induct [case_names Fract, induct type: rat]:
```
```    95     "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
```
```    96   by (cases q) simp
```
```    97
```
```    98
```
```    99 subsubsection {* Congruence lemmas *}
```
```   100
```
```   101 lemma add_congruent2:
```
```   102      "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
```
```   103       respects2 ratrel"
```
```   104 apply (rule equiv_ratrel [THEN congruent2_commuteI])
```
```   105 apply (simp_all add: left_distrib)
```
```   106 done
```
```   107
```
```   108 lemma minus_congruent:
```
```   109   "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
```
```   110 by (simp add: congruent_def)
```
```   111
```
```   112 lemma mult_congruent2:
```
```   113   "(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel"
```
```   114 by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
```
```   115
```
```   116 lemma inverse_congruent:
```
```   117   "(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel"
```
```   118 by (auto simp add: congruent_def mult_commute)
```
```   119
```
```   120 lemma le_congruent2:
```
```   121   "(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
```
```   122    respects2 ratrel"
```
```   123 proof (clarsimp simp add: congruent2_def)
```
```   124   fix a b a' b' c d c' d'::int
```
```   125   assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
```
```   126   assume eq1: "a * b' = a' * b"
```
```   127   assume eq2: "c * d' = c' * d"
```
```   128
```
```   129   let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   130   {
```
```   131     fix a b c d x :: int assume x: "x \<noteq> 0"
```
```   132     have "?le a b c d = ?le (a * x) (b * x) c d"
```
```   133     proof -
```
```   134       from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
```
```   135       hence "?le a b c d =
```
```   136           ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
```
```   137         by (simp add: mult_le_cancel_right)
```
```   138       also have "... = ?le (a * x) (b * x) c d"
```
```   139         by (simp add: mult_ac)
```
```   140       finally show ?thesis .
```
```   141     qed
```
```   142   } note le_factor = this
```
```   143
```
```   144   let ?D = "b * d" and ?D' = "b' * d'"
```
```   145   from neq have D: "?D \<noteq> 0" by simp
```
```   146   from neq have "?D' \<noteq> 0" by simp
```
```   147   hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
```
```   148     by (rule le_factor)
```
```   149   also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
```
```   150     by (simp add: mult_ac)
```
```   151   also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
```
```   152     by (simp only: eq1 eq2)
```
```   153   also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
```
```   154     by (simp add: mult_ac)
```
```   155   also from D have "... = ?le a' b' c' d'"
```
```   156     by (rule le_factor [symmetric])
```
```   157   finally show "?le a b c d = ?le a' b' c' d'" .
```
```   158 qed
```
```   159
```
```   160 lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
```
```   161 lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
```
```   162
```
```   163
```
```   164 subsubsection {* Standard operations on rational numbers *}
```
```   165
```
```   166 instance rat :: zero
```
```   167   Zero_rat_def: "0 == Fract 0 1" ..
```
```   168 lemmas [code func del] = Zero_rat_def
```
```   169
```
```   170 instance rat :: one
```
```   171   One_rat_def: "1 == Fract 1 1" ..
```
```   172 lemmas [code func del] = One_rat_def
```
```   173
```
```   174 instance rat :: plus
```
```   175   add_rat_def:
```
```   176    "q + r ==
```
```   177        Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   178            ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})" ..
```
```   179 lemmas [code func del] = add_rat_def
```
```   180
```
```   181 instance rat :: minus
```
```   182   minus_rat_def:
```
```   183     "- q == Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
```
```   184   diff_rat_def:  "q - r == q + - (r::rat)" ..
```
```   185 lemmas [code func del] = minus_rat_def diff_rat_def
```
```   186
```
```   187 instance rat :: times
```
```   188   mult_rat_def:
```
```   189    "q * r ==
```
```   190        Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   191            ratrel``{(fst x * fst y, snd x * snd y)})" ..
```
```   192 lemmas [code func del] = mult_rat_def
```
```   193
```
```   194 instance rat :: inverse
```
```   195   inverse_rat_def:
```
```   196     "inverse q ==
```
```   197         Abs_Rat (\<Union>x \<in> Rep_Rat q.
```
```   198             ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
```
```   199   divide_rat_def:  "q / r == q * inverse (r::rat)" ..
```
```   200 lemmas [code func del] = inverse_rat_def divide_rat_def
```
```   201
```
```   202 instance rat :: ord
```
```   203   le_rat_def:
```
```   204    "q \<le> r == contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
```
```   205       {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
```
```   206   less_rat_def: "(z < (w::rat)) == (z \<le> w & z \<noteq> w)" ..
```
```   207 lemmas [code func del] = le_rat_def less_rat_def
```
```   208
```
```   209 instance rat :: abs
```
```   210   abs_rat_def: "\<bar>q\<bar> == if q < 0 then -q else (q::rat)" ..
```
```   211
```
```   212 instance rat :: sgn
```
```   213   sgn_rat_def: "sgn(q::rat) == (if q=0 then 0 else if 0<q then 1 else - 1)" ..
```
```   214
```
```   215 instance rat :: power ..
```
```   216
```
```   217 primrec (rat)
```
```   218   rat_power_0:   "q ^ 0       = 1"
```
```   219   rat_power_Suc: "q ^ (Suc n) = (q::rat) * (q ^ n)"
```
```   220
```
```   221 theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   222   (Fract a b = Fract c d) = (a * d = c * b)"
```
```   223 by (simp add: Fract_def)
```
```   224
```
```   225 theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   226   Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
```
```   227 by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2)
```
```   228
```
```   229 theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
```
```   230 by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel)
```
```   231
```
```   232 theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   233     Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
```
```   234 by (simp add: diff_rat_def add_rat minus_rat)
```
```   235
```
```   236 theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   237   Fract a b * Fract c d = Fract (a * c) (b * d)"
```
```   238 by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2)
```
```   239
```
```   240 theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==>
```
```   241   inverse (Fract a b) = Fract b a"
```
```   242 by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel)
```
```   243
```
```   244 theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   245   Fract a b / Fract c d = Fract (a * d) (b * c)"
```
```   246 by (simp add: divide_rat_def inverse_rat mult_rat)
```
```   247
```
```   248 theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   249   (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
```
```   250 by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2)
```
```   251
```
```   252 theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
```
```   253     (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
```
```   254 by (simp add: less_rat_def le_rat eq_rat order_less_le)
```
```   255
```
```   256 theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
```
```   257   by (simp add: abs_rat_def minus_rat Zero_rat_def less_rat eq_rat)
```
```   258      (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less
```
```   259                 split: abs_split)
```
```   260
```
```   261
```
```   262 subsubsection {* The ordered field of rational numbers *}
```
```   263
```
```   264 instance rat :: field
```
```   265 proof
```
```   266   fix q r s :: rat
```
```   267   show "(q + r) + s = q + (r + s)"
```
```   268     by (induct q, induct r, induct s)
```
```   269        (simp add: add_rat add_ac mult_ac int_distrib)
```
```   270   show "q + r = r + q"
```
```   271     by (induct q, induct r) (simp add: add_rat add_ac mult_ac)
```
```   272   show "0 + q = q"
```
```   273     by (induct q) (simp add: Zero_rat_def add_rat)
```
```   274   show "(-q) + q = 0"
```
```   275     by (induct q) (simp add: Zero_rat_def minus_rat add_rat eq_rat)
```
```   276   show "q - r = q + (-r)"
```
```   277     by (induct q, induct r) (simp add: add_rat minus_rat diff_rat)
```
```   278   show "(q * r) * s = q * (r * s)"
```
```   279     by (induct q, induct r, induct s) (simp add: mult_rat mult_ac)
```
```   280   show "q * r = r * q"
```
```   281     by (induct q, induct r) (simp add: mult_rat mult_ac)
```
```   282   show "1 * q = q"
```
```   283     by (induct q) (simp add: One_rat_def mult_rat)
```
```   284   show "(q + r) * s = q * s + r * s"
```
```   285     by (induct q, induct r, induct s)
```
```   286        (simp add: add_rat mult_rat eq_rat int_distrib)
```
```   287   show "q \<noteq> 0 ==> inverse q * q = 1"
```
```   288     by (induct q) (simp add: inverse_rat mult_rat One_rat_def Zero_rat_def eq_rat)
```
```   289   show "q / r = q * inverse r"
```
```   290     by (simp add: divide_rat_def)
```
```   291   show "0 \<noteq> (1::rat)"
```
```   292     by (simp add: Zero_rat_def One_rat_def eq_rat)
```
```   293 qed
```
```   294
```
```   295 instance rat :: linorder
```
```   296 proof
```
```   297   fix q r s :: rat
```
```   298   {
```
```   299     assume "q \<le> r" and "r \<le> s"
```
```   300     show "q \<le> s"
```
```   301     proof (insert prems, induct q, induct r, induct s)
```
```   302       fix a b c d e f :: int
```
```   303       assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   304       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
```
```   305       show "Fract a b \<le> Fract e f"
```
```   306       proof -
```
```   307         from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
```
```   308           by (auto simp add: zero_less_mult_iff linorder_neq_iff)
```
```   309         have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
```
```   310         proof -
```
```   311           from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   312             by (simp add: le_rat)
```
```   313           with ff show ?thesis by (simp add: mult_le_cancel_right)
```
```   314         qed
```
```   315         also have "... = (c * f) * (d * f) * (b * b)"
```
```   316           by (simp only: mult_ac)
```
```   317         also have "... \<le> (e * d) * (d * f) * (b * b)"
```
```   318         proof -
```
```   319           from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
```
```   320             by (simp add: le_rat)
```
```   321           with bb show ?thesis by (simp add: mult_le_cancel_right)
```
```   322         qed
```
```   323         finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
```
```   324           by (simp only: mult_ac)
```
```   325         with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
```
```   326           by (simp add: mult_le_cancel_right)
```
```   327         with neq show ?thesis by (simp add: le_rat)
```
```   328       qed
```
```   329     qed
```
```   330   next
```
```   331     assume "q \<le> r" and "r \<le> q"
```
```   332     show "q = r"
```
```   333     proof (insert prems, induct q, induct r)
```
```   334       fix a b c d :: int
```
```   335       assume neq: "b \<noteq> 0"  "d \<noteq> 0"
```
```   336       assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
```
```   337       show "Fract a b = Fract c d"
```
```   338       proof -
```
```   339         from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   340           by (simp add: le_rat)
```
```   341         also have "... \<le> (a * d) * (b * d)"
```
```   342         proof -
```
```   343           from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
```
```   344             by (simp add: le_rat)
```
```   345           thus ?thesis by (simp only: mult_ac)
```
```   346         qed
```
```   347         finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
```
```   348         moreover from neq have "b * d \<noteq> 0" by simp
```
```   349         ultimately have "a * d = c * b" by simp
```
```   350         with neq show ?thesis by (simp add: eq_rat)
```
```   351       qed
```
```   352     qed
```
```   353   next
```
```   354     show "q \<le> q"
```
```   355       by (induct q) (simp add: le_rat)
```
```   356     show "(q < r) = (q \<le> r \<and> q \<noteq> r)"
```
```   357       by (simp only: less_rat_def)
```
```   358     show "q \<le> r \<or> r \<le> q"
```
```   359       by (induct q, induct r)
```
```   360          (simp add: le_rat mult_commute, rule linorder_linear)
```
```   361   }
```
```   362 qed
```
```   363
```
```   364 instance rat :: distrib_lattice
```
```   365   "inf r s \<equiv> min r s"
```
```   366   "sup r s \<equiv> max r s"
```
```   367   by default (auto simp add: min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
```
```   368
```
```   369 instance rat :: ordered_field
```
```   370 proof
```
```   371   fix q r s :: rat
```
```   372   show "q \<le> r ==> s + q \<le> s + r"
```
```   373   proof (induct q, induct r, induct s)
```
```   374     fix a b c d e f :: int
```
```   375     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   376     assume le: "Fract a b \<le> Fract c d"
```
```   377     show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
```
```   378     proof -
```
```   379       let ?F = "f * f" from neq have F: "0 < ?F"
```
```   380         by (auto simp add: zero_less_mult_iff)
```
```   381       from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
```
```   382         by (simp add: le_rat)
```
```   383       with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
```
```   384         by (simp add: mult_le_cancel_right)
```
```   385       with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib)
```
```   386     qed
```
```   387   qed
```
```   388   show "q < r ==> 0 < s ==> s * q < s * r"
```
```   389   proof (induct q, induct r, induct s)
```
```   390     fix a b c d e f :: int
```
```   391     assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
```
```   392     assume le: "Fract a b < Fract c d"
```
```   393     assume gt: "0 < Fract e f"
```
```   394     show "Fract e f * Fract a b < Fract e f * Fract c d"
```
```   395     proof -
```
```   396       let ?E = "e * f" and ?F = "f * f"
```
```   397       from neq gt have "0 < ?E"
```
```   398         by (auto simp add: Zero_rat_def less_rat le_rat order_less_le eq_rat)
```
```   399       moreover from neq have "0 < ?F"
```
```   400         by (auto simp add: zero_less_mult_iff)
```
```   401       moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
```
```   402         by (simp add: less_rat)
```
```   403       ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
```
```   404         by (simp add: mult_less_cancel_right)
```
```   405       with neq show ?thesis
```
```   406         by (simp add: less_rat mult_rat mult_ac)
```
```   407     qed
```
```   408   qed
```
```   409   show "\<bar>q\<bar> = (if q < 0 then -q else q)"
```
```   410     by (simp only: abs_rat_def)
```
```   411 qed (auto simp: sgn_rat_def)
```
```   412
```
```   413 instance rat :: division_by_zero
```
```   414 proof
```
```   415   show "inverse 0 = (0::rat)"
```
```   416     by (simp add: Zero_rat_def Fract_def inverse_rat_def
```
```   417                   inverse_congruent UN_ratrel)
```
```   418 qed
```
```   419
```
```   420 instance rat :: recpower
```
```   421 proof
```
```   422   fix q :: rat
```
```   423   fix n :: nat
```
```   424   show "q ^ 0 = 1" by simp
```
```   425   show "q ^ (Suc n) = q * (q ^ n)" by simp
```
```   426 qed
```
```   427
```
```   428
```
```   429 subsection {* Various Other Results *}
```
```   430
```
```   431 lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
```
```   432 by (simp add: eq_rat)
```
```   433
```
```   434 theorem Rat_induct_pos [case_names Fract, induct type: rat]:
```
```   435   assumes step: "!!a b. 0 < b ==> P (Fract a b)"
```
```   436     shows "P q"
```
```   437 proof (cases q)
```
```   438   have step': "!!a b. b < 0 ==> P (Fract a b)"
```
```   439   proof -
```
```   440     fix a::int and b::int
```
```   441     assume b: "b < 0"
```
```   442     hence "0 < -b" by simp
```
```   443     hence "P (Fract (-a) (-b))" by (rule step)
```
```   444     thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
```
```   445   qed
```
```   446   case (Fract a b)
```
```   447   thus "P q" by (force simp add: linorder_neq_iff step step')
```
```   448 qed
```
```   449
```
```   450 lemma zero_less_Fract_iff:
```
```   451      "0 < b ==> (0 < Fract a b) = (0 < a)"
```
```   452 by (simp add: Zero_rat_def less_rat order_less_imp_not_eq2 zero_less_mult_iff)
```
```   453
```
```   454 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
```
```   455 apply (insert add_rat [of concl: m n 1 1])
```
```   456 apply (simp add: One_rat_def [symmetric])
```
```   457 done
```
```   458
```
```   459 lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
```
```   460 by (induct k) (simp_all add: Zero_rat_def One_rat_def add_rat)
```
```   461
```
```   462 lemma of_int_rat: "of_int k = Fract k 1"
```
```   463 by (cases k rule: int_diff_cases, simp add: of_nat_rat diff_rat)
```
```   464
```
```   465 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
```
```   466 by (rule of_nat_rat [symmetric])
```
```   467
```
```   468 lemma Fract_of_int_eq: "Fract k 1 = of_int k"
```
```   469 by (rule of_int_rat [symmetric])
```
```   470
```
```   471 lemma Fract_of_int_quotient: "Fract k l = (if l = 0 then Fract 1 0 else of_int k / of_int l)"
```
```   472 by (auto simp add: Fract_zero Fract_of_int_eq [symmetric] divide_rat)
```
```   473
```
```   474
```
```   475 subsection {* Numerals and Arithmetic *}
```
```   476
```
```   477 instance rat :: number
```
```   478   rat_number_of_def: "(number_of w :: rat) \<equiv> of_int w" ..
```
```   479
```
```   480 instance rat :: number_ring
```
```   481   by default (simp add: rat_number_of_def)
```
```   482
```
```   483 use "rat_arith.ML"
```
```   484 declaration {* K rat_arith_setup *}
```
```   485
```
```   486
```
```   487 subsection {* Embedding from Rationals to other Fields *}
```
```   488
```
```   489 class field_char_0 = field + ring_char_0
```
```   490
```
```   491 instance ordered_field < field_char_0 ..
```
```   492
```
```   493 definition
```
```   494   of_rat :: "rat \<Rightarrow> 'a::field_char_0"
```
```   495 where
```
```   496   [code func del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
```
```   497
```
```   498 lemma of_rat_congruent:
```
```   499   "(\<lambda>(a, b). {of_int a / of_int b::'a::field_char_0}) respects ratrel"
```
```   500 apply (rule congruent.intro)
```
```   501 apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   502 apply (simp only: of_int_mult [symmetric])
```
```   503 done
```
```   504
```
```   505 lemma of_rat_rat:
```
```   506   "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
```
```   507 unfolding Fract_def of_rat_def
```
```   508 by (simp add: UN_ratrel of_rat_congruent)
```
```   509
```
```   510 lemma of_rat_0 [simp]: "of_rat 0 = 0"
```
```   511 by (simp add: Zero_rat_def of_rat_rat)
```
```   512
```
```   513 lemma of_rat_1 [simp]: "of_rat 1 = 1"
```
```   514 by (simp add: One_rat_def of_rat_rat)
```
```   515
```
```   516 lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
```
```   517 by (induct a, induct b, simp add: add_rat of_rat_rat add_frac_eq)
```
```   518
```
```   519 lemma of_rat_minus: "of_rat (- a) = - of_rat a"
```
```   520 by (induct a, simp add: minus_rat of_rat_rat)
```
```   521
```
```   522 lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
```
```   523 by (simp only: diff_minus of_rat_add of_rat_minus)
```
```   524
```
```   525 lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
```
```   526 apply (induct a, induct b, simp add: mult_rat of_rat_rat)
```
```   527 apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
```
```   528 done
```
```   529
```
```   530 lemma nonzero_of_rat_inverse:
```
```   531   "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
```
```   532 apply (rule inverse_unique [symmetric])
```
```   533 apply (simp add: of_rat_mult [symmetric])
```
```   534 done
```
```   535
```
```   536 lemma of_rat_inverse:
```
```   537   "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
```
```   538    inverse (of_rat a)"
```
```   539 by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
```
```   540
```
```   541 lemma nonzero_of_rat_divide:
```
```   542   "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
```
```   543 by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
```
```   544
```
```   545 lemma of_rat_divide:
```
```   546   "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
```
```   547    = of_rat a / of_rat b"
```
```   548 by (cases "b = 0", simp_all add: nonzero_of_rat_divide)
```
```   549
```
```   550 lemma of_rat_power:
```
```   551   "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
```
```   552 by (induct n) (simp_all add: of_rat_mult power_Suc)
```
```   553
```
```   554 lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
```
```   555 apply (induct a, induct b)
```
```   556 apply (simp add: of_rat_rat eq_rat)
```
```   557 apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
```
```   558 apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
```
```   559 done
```
```   560
```
```   561 lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
```
```   562
```
```   563 lemma of_rat_eq_id [simp]: "of_rat = (id :: rat \<Rightarrow> rat)"
```
```   564 proof
```
```   565   fix a
```
```   566   show "of_rat a = id a"
```
```   567   by (induct a)
```
```   568      (simp add: of_rat_rat divide_rat Fract_of_int_eq [symmetric])
```
```   569 qed
```
```   570
```
```   571 text{*Collapse nested embeddings*}
```
```   572 lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
```
```   573 by (induct n) (simp_all add: of_rat_add)
```
```   574
```
```   575 lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
```
```   576 by (cases z rule: int_diff_cases, simp add: of_rat_diff)
```
```   577
```
```   578 lemma of_rat_number_of_eq [simp]:
```
```   579   "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
```
```   580 by (simp add: number_of_eq)
```
```   581
```
```   582 lemmas zero_rat = Zero_rat_def
```
```   583 lemmas one_rat = One_rat_def
```
```   584
```
```   585 abbreviation
```
```   586   rat_of_nat :: "nat \<Rightarrow> rat"
```
```   587 where
```
```   588   "rat_of_nat \<equiv> of_nat"
```
```   589
```
```   590 abbreviation
```
```   591   rat_of_int :: "int \<Rightarrow> rat"
```
```   592 where
```
```   593   "rat_of_int \<equiv> of_int"
```
```   594
```
```   595
```
```   596 subsection {* Implementation of rational numbers as pairs of integers *}
```
```   597
```
```   598 definition
```
```   599   Rational :: "int \<times> int \<Rightarrow> rat"
```
```   600 where
```
```   601   "Rational = INum"
```
```   602
```
```   603 code_datatype Rational
```
```   604
```
```   605 lemma Rational_simp:
```
```   606   "Rational (k, l) = rat_of_int k / rat_of_int l"
```
```   607   unfolding Rational_def INum_def by simp
```
```   608
```
```   609 lemma Rational_zero [simp]: "Rational 0\<^sub>N = 0"
```
```   610   by (simp add: Rational_simp)
```
```   611
```
```   612 lemma Rational_lit [simp]: "Rational i\<^sub>N = rat_of_int i"
```
```   613   by (simp add: Rational_simp)
```
```   614
```
```   615 lemma zero_rat_code [code, code unfold]:
```
```   616   "0 = Rational 0\<^sub>N" by simp
```
```   617
```
```   618 lemma zero_rat_code [code, code unfold]:
```
```   619   "1 = Rational 1\<^sub>N" by simp
```
```   620
```
```   621 lemma [code, code unfold]:
```
```   622   "number_of k = rat_of_int (number_of k)"
```
```   623   by (simp add: number_of_is_id rat_number_of_def)
```
```   624
```
```   625 definition
```
```   626   [code func del]: "Fract' (b\<Colon>bool) k l = Fract k l"
```
```   627
```
```   628 lemma [code]:
```
```   629   "Fract k l = Fract' (l \<noteq> 0) k l"
```
```   630   unfolding Fract'_def ..
```
```   631
```
```   632 lemma [code]:
```
```   633   "Fract' True k l = (if l \<noteq> 0 then Rational (k, l) else Fract 1 0)"
```
```   634   by (simp add: Fract'_def Rational_simp Fract_of_int_quotient [of k l])
```
```   635
```
```   636 lemma [code]:
```
```   637   "of_rat (Rational (k, l)) = (if l \<noteq> 0 then of_int k / of_int l else 0)"
```
```   638   by (cases "l = 0")
```
```   639     (auto simp add: Rational_simp of_rat_rat [simplified Fract_of_int_quotient [of k l], symmetric])
```
```   640
```
```   641 instance rat :: eq ..
```
```   642
```
```   643 lemma rat_eq_code [code]: "Rational x = Rational y \<longleftrightarrow> normNum x = normNum y"
```
```   644   unfolding Rational_def INum_normNum_iff ..
```
```   645
```
```   646 lemma rat_less_eq_code [code]: "Rational x \<le> Rational y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
```
```   647 proof -
```
```   648   have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) \<le> Rational (normNum y)"
```
```   649     by (simp add: Rational_def del: normNum)
```
```   650   also have "\<dots> = (Rational x \<le> Rational y)" by (simp add: Rational_def)
```
```   651   finally show ?thesis by simp
```
```   652 qed
```
```   653
```
```   654 lemma rat_less_code [code]: "Rational x < Rational y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
```
```   655 proof -
```
```   656   have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) < Rational (normNum y)"
```
```   657     by (simp add: Rational_def del: normNum)
```
```   658   also have "\<dots> = (Rational x < Rational y)" by (simp add: Rational_def)
```
```   659   finally show ?thesis by simp
```
```   660 qed
```
```   661
```
```   662 lemma rat_add_code [code]: "Rational x + Rational y = Rational (x +\<^sub>N y)"
```
```   663   unfolding Rational_def by simp
```
```   664
```
```   665 lemma rat_mul_code [code]: "Rational x * Rational y = Rational (x *\<^sub>N y)"
```
```   666   unfolding Rational_def by simp
```
```   667
```
```   668 lemma rat_neg_code [code]: "- Rational x = Rational (~\<^sub>N x)"
```
```   669   unfolding Rational_def by simp
```
```   670
```
```   671 lemma rat_sub_code [code]: "Rational x - Rational y = Rational (x -\<^sub>N y)"
```
```   672   unfolding Rational_def by simp
```
```   673
```
```   674 lemma rat_inv_code [code]: "inverse (Rational x) = Rational (Ninv x)"
```
```   675   unfolding Rational_def Ninv divide_rat_def by simp
```
```   676
```
```   677 lemma rat_div_code [code]: "Rational x / Rational y = Rational (x \<div>\<^sub>N y)"
```
```   678   unfolding Rational_def by simp
```
```   679
```
```   680 text {* Setup for SML code generator *}
```
```   681
```
```   682 types_code
```
```   683   rat ("(int */ int)")
```
```   684 attach (term_of) {*
```
```   685 fun term_of_rat (p, q) =
```
```   686   let
```
```   687     val rT = Type ("Rational.rat", [])
```
```   688   in
```
```   689     if q = 1 orelse p = 0 then HOLogic.mk_number rT p
```
```   690     else Const ("HOL.inverse_class.divide", rT --> rT --> rT) \$
```
```   691       HOLogic.mk_number rT p \$ HOLogic.mk_number rT q
```
```   692   end;
```
```   693 *}
```
```   694 attach (test) {*
```
```   695 fun gen_rat i =
```
```   696   let
```
```   697     val p = random_range 0 i;
```
```   698     val q = random_range 1 (i + 1);
```
```   699     val g = Integer.gcd p q;
```
```   700     val p' = p div g;
```
```   701     val q' = q div g;
```
```   702   in
```
```   703     (if one_of [true, false] then p' else ~ p',
```
```   704      if p' = 0 then 0 else q')
```
```   705   end;
```
```   706 *}
```
```   707
```
```   708 consts_code
```
```   709   Rational ("(_)")
```
```   710
```
```   711 consts_code
```
```   712   "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
```
```   713 attach {*
```
```   714 fun rat_of_int 0 = (0, 0)
```
```   715   | rat_of_int i = (i, 1);
```
```   716 *}
```
```   717
```
```   718 end
```