src/HOL/Real/Rational.thy
author huffman
Thu Feb 09 03:01:11 2006 +0100 (2006-02-09)
changeset 18982 a2950f748445
parent 18913 57f19fad8c2a
child 18983 075550af9e11
permissions -rw-r--r--
no longer need All_equiv lemmas
paulson@14365
     1
(*  Title: HOL/Library/Rational.thy
paulson@14365
     2
    ID:    $Id$
paulson@14365
     3
    Author: Markus Wenzel, TU Muenchen
paulson@14365
     4
*)
paulson@14365
     5
wenzelm@14691
     6
header {* Rational numbers *}
paulson@14365
     7
nipkow@15131
     8
theory Rational
huffman@18913
     9
imports Main
haftmann@16417
    10
uses ("rat_arith.ML")
nipkow@15131
    11
begin
paulson@14365
    12
huffman@18913
    13
subsection {* Rational numbers *}
paulson@14365
    14
paulson@14365
    15
subsubsection {* Equivalence of fractions *}
paulson@14365
    16
huffman@18913
    17
constdefs
huffman@18913
    18
  fraction :: "(int \<times> int) set"
huffman@18913
    19
   "fraction \<equiv> {x. snd x \<noteq> 0}"
huffman@18913
    20
huffman@18913
    21
  ratrel :: "((int \<times> int) \<times> (int \<times> int)) set"
huffman@18913
    22
   "ratrel \<equiv> {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
paulson@14365
    23
huffman@18913
    24
lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
huffman@18913
    25
by (simp add: fraction_def)
paulson@14365
    26
huffman@18913
    27
lemma ratrel_iff [simp]:
huffman@18913
    28
  "((x,y) \<in> ratrel) =
huffman@18913
    29
   (snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
huffman@18913
    30
by (simp add: ratrel_def)
paulson@14365
    31
huffman@18913
    32
lemma refl_ratrel: "refl fraction ratrel"
huffman@18913
    33
by (auto simp add: refl_def fraction_def ratrel_def)
huffman@18913
    34
huffman@18913
    35
lemma sym_ratrel: "sym ratrel"
huffman@18913
    36
by (simp add: ratrel_def sym_def)
huffman@18913
    37
huffman@18913
    38
lemma trans_ratrel_lemma:
huffman@18913
    39
  assumes 1: "a * b' = a' * b"
huffman@18913
    40
  assumes 2: "a' * b'' = a'' * b'"
huffman@18913
    41
  assumes 3: "b' \<noteq> (0::int)"
huffman@18913
    42
  shows "a * b'' = a'' * b"
huffman@18913
    43
proof -
huffman@18913
    44
  have "b' * (a * b'') = b'' * (a * b')" by simp
huffman@18913
    45
  also note 1
huffman@18913
    46
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
huffman@18913
    47
  also note 2
huffman@18913
    48
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
huffman@18913
    49
  finally have "b' * (a * b'') = b' * (a'' * b)" .
huffman@18913
    50
  with 3 show "a * b'' = a'' * b" by simp
paulson@14365
    51
qed
paulson@14365
    52
huffman@18913
    53
lemma trans_ratrel: "trans ratrel"
huffman@18913
    54
by (auto simp add: trans_def elim: trans_ratrel_lemma)
huffman@18913
    55
huffman@18913
    56
lemma equiv_ratrel: "equiv fraction ratrel"
huffman@18913
    57
by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
huffman@18913
    58
huffman@18913
    59
lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
huffman@18913
    60
huffman@18913
    61
lemma equiv_ratrel_iff2:
huffman@18913
    62
  "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
huffman@18913
    63
    \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
huffman@18913
    64
by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all)
paulson@14365
    65
paulson@14365
    66
huffman@18913
    67
subsubsection {* The type of rational numbers *}
paulson@14365
    68
huffman@18913
    69
typedef (Rat) rat = "fraction//ratrel"
huffman@18913
    70
proof
huffman@18913
    71
  have "(0,1) \<in> fraction" by (simp add: fraction_def)
huffman@18913
    72
  thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI)
paulson@14365
    73
qed
paulson@14365
    74
huffman@18913
    75
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat"
huffman@18913
    76
by (simp add: Rat_def quotientI)
huffman@18913
    77
huffman@18913
    78
declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
huffman@18913
    79
huffman@18913
    80
huffman@18913
    81
constdefs
huffman@18913
    82
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat"
huffman@18913
    83
  "Fract a b \<equiv> Abs_Rat (ratrel``{(a,b)})"
huffman@18913
    84
huffman@18913
    85
theorem Rat_cases [case_names Fract, cases type: rat]:
huffman@18913
    86
  "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
huffman@18913
    87
by (cases q, clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
huffman@18913
    88
huffman@18913
    89
theorem Rat_induct [case_names Fract, induct type: rat]:
huffman@18913
    90
    "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
huffman@18913
    91
  by (cases q) simp
huffman@18913
    92
huffman@18913
    93
huffman@18913
    94
subsubsection {* Congruence lemmas *}
paulson@14365
    95
huffman@18913
    96
lemma add_congruent2:
huffman@18913
    97
     "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
huffman@18913
    98
      respects2 ratrel"
huffman@18913
    99
apply (rule equiv_ratrel [THEN congruent2_commuteI])
huffman@18913
   100
apply (simp_all add: left_distrib)
huffman@18913
   101
done
huffman@18913
   102
huffman@18913
   103
lemma minus_congruent:
huffman@18913
   104
  "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
huffman@18913
   105
by (simp add: congruent_def)
huffman@18913
   106
huffman@18913
   107
lemma mult_congruent2:
huffman@18913
   108
  "(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel"
huffman@18913
   109
by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
huffman@18913
   110
huffman@18913
   111
lemma inverse_congruent:
huffman@18913
   112
  "(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel"
huffman@18913
   113
by (auto simp add: congruent_def mult_commute)
huffman@18913
   114
huffman@18913
   115
lemma le_congruent2:
huffman@18982
   116
  "(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
huffman@18913
   117
   respects2 ratrel"
huffman@18913
   118
proof (clarsimp simp add: congruent2_def)
huffman@18913
   119
  fix a b a' b' c d c' d'::int
paulson@14365
   120
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
huffman@18913
   121
  assume eq1: "a * b' = a' * b"
huffman@18913
   122
  assume eq2: "c * d' = c' * d"
paulson@14365
   123
paulson@14365
   124
  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365
   125
  {
paulson@14365
   126
    fix a b c d x :: int assume x: "x \<noteq> 0"
paulson@14365
   127
    have "?le a b c d = ?le (a * x) (b * x) c d"
paulson@14365
   128
    proof -
paulson@14365
   129
      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
paulson@14365
   130
      hence "?le a b c d =
paulson@14365
   131
          ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
paulson@14365
   132
        by (simp add: mult_le_cancel_right)
paulson@14365
   133
      also have "... = ?le (a * x) (b * x) c d"
paulson@14365
   134
        by (simp add: mult_ac)
paulson@14365
   135
      finally show ?thesis .
paulson@14365
   136
    qed
paulson@14365
   137
  } note le_factor = this
paulson@14365
   138
paulson@14365
   139
  let ?D = "b * d" and ?D' = "b' * d'"
paulson@14365
   140
  from neq have D: "?D \<noteq> 0" by simp
paulson@14365
   141
  from neq have "?D' \<noteq> 0" by simp
paulson@14365
   142
  hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
paulson@14365
   143
    by (rule le_factor)
paulson@14365
   144
  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
paulson@14365
   145
    by (simp add: mult_ac)
paulson@14365
   146
  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
paulson@14365
   147
    by (simp only: eq1 eq2)
paulson@14365
   148
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
paulson@14365
   149
    by (simp add: mult_ac)
paulson@14365
   150
  also from D have "... = ?le a' b' c' d'"
paulson@14365
   151
    by (rule le_factor [symmetric])
huffman@18913
   152
  finally show "?le a b c d = ?le a' b' c' d'" .
paulson@14365
   153
qed
paulson@14365
   154
huffman@18913
   155
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
huffman@18913
   156
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
paulson@14365
   157
paulson@14365
   158
paulson@14365
   159
subsubsection {* Standard operations on rational numbers *}
paulson@14365
   160
huffman@18913
   161
instance rat :: "{ord, zero, one, plus, times, minus, inverse}" ..
paulson@14365
   162
paulson@14365
   163
defs (overloaded)
huffman@18913
   164
  Zero_rat_def:  "0 == Fract 0 1"
huffman@18913
   165
  One_rat_def:   "1 == Fract 1 1"
huffman@18913
   166
huffman@18913
   167
  add_rat_def:
huffman@18913
   168
   "q + r ==
huffman@18913
   169
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18913
   170
           ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})"
huffman@18913
   171
huffman@18913
   172
  minus_rat_def:
huffman@18913
   173
    "- q == Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
huffman@18913
   174
huffman@18913
   175
  diff_rat_def:  "q - r == q + - (r::rat)"
huffman@18913
   176
huffman@18913
   177
  mult_rat_def:
huffman@18913
   178
   "q * r ==
huffman@18913
   179
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18913
   180
           ratrel``{(fst x * fst y, snd x * snd y)})"
huffman@18913
   181
huffman@18913
   182
  inverse_rat_def:
huffman@18913
   183
    "inverse q ==
huffman@18913
   184
        Abs_Rat (\<Union>x \<in> Rep_Rat q.
huffman@18913
   185
            ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
huffman@18913
   186
huffman@18913
   187
  divide_rat_def:  "q / r == q * inverse (r::rat)"
huffman@18913
   188
huffman@18913
   189
  le_rat_def:
huffman@18982
   190
   "q \<le> r == contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@18982
   191
      {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
huffman@18913
   192
huffman@18913
   193
  less_rat_def: "(z < (w::rat)) == (z \<le> w & z \<noteq> w)"
huffman@18913
   194
paulson@14365
   195
  abs_rat_def: "\<bar>q\<bar> == if q < 0 then -q else (q::rat)"
paulson@14365
   196
huffman@18913
   197
lemma zero_rat: "0 = Fract 0 1"
huffman@18913
   198
by (simp add: Zero_rat_def)
paulson@14365
   199
huffman@18913
   200
lemma one_rat: "1 = Fract 1 1"
huffman@18913
   201
by (simp add: One_rat_def)
huffman@18913
   202
huffman@18913
   203
theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
huffman@18913
   204
  (Fract a b = Fract c d) = (a * d = c * b)"
huffman@18913
   205
by (simp add: Fract_def)
paulson@14365
   206
paulson@14365
   207
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   208
  Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
huffman@18913
   209
by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2)
paulson@14365
   210
paulson@14365
   211
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
huffman@18913
   212
by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel)
paulson@14365
   213
paulson@14365
   214
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   215
    Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
huffman@18913
   216
by (simp add: diff_rat_def add_rat minus_rat)
paulson@14365
   217
paulson@14365
   218
theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   219
  Fract a b * Fract c d = Fract (a * c) (b * d)"
huffman@18913
   220
by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2)
paulson@14365
   221
huffman@18913
   222
theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==>
paulson@14365
   223
  inverse (Fract a b) = Fract b a"
huffman@18913
   224
by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel)
paulson@14365
   225
huffman@18913
   226
theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   227
  Fract a b / Fract c d = Fract (a * d) (b * c)"
huffman@18913
   228
by (simp add: divide_rat_def inverse_rat mult_rat)
paulson@14365
   229
paulson@14365
   230
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   231
  (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
huffman@18982
   232
by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2)
paulson@14365
   233
paulson@14365
   234
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   235
    (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
huffman@18913
   236
by (simp add: less_rat_def le_rat eq_rat order_less_le)
paulson@14365
   237
paulson@14365
   238
theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
paulson@14365
   239
  by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat)
wenzelm@14691
   240
     (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less
paulson@14365
   241
                split: abs_split)
paulson@14365
   242
paulson@14365
   243
paulson@14365
   244
subsubsection {* The ordered field of rational numbers *}
paulson@14365
   245
paulson@14365
   246
lemma rat_add_assoc: "(q + r) + s = q + (r + (s::rat))"
wenzelm@14691
   247
  by (induct q, induct r, induct s)
paulson@14365
   248
     (simp add: add_rat add_ac mult_ac int_distrib)
paulson@14365
   249
paulson@14365
   250
lemma rat_add_0: "0 + q = (q::rat)"
paulson@14365
   251
  by (induct q) (simp add: zero_rat add_rat)
paulson@14365
   252
paulson@14365
   253
lemma rat_left_minus: "(-q) + q = (0::rat)"
paulson@14365
   254
  by (induct q) (simp add: zero_rat minus_rat add_rat eq_rat)
paulson@14365
   255
paulson@14365
   256
paulson@14365
   257
instance rat :: field
paulson@14365
   258
proof
paulson@14365
   259
  fix q r s :: rat
paulson@14365
   260
  show "(q + r) + s = q + (r + s)"
huffman@18913
   261
    by (induct q, induct r, induct s)
huffman@18913
   262
       (simp add: add_rat add_ac mult_ac int_distrib)
paulson@14365
   263
  show "q + r = r + q"
paulson@14365
   264
    by (induct q, induct r) (simp add: add_rat add_ac mult_ac)
paulson@14365
   265
  show "0 + q = q"
paulson@14365
   266
    by (induct q) (simp add: zero_rat add_rat)
paulson@14365
   267
  show "(-q) + q = 0"
huffman@18913
   268
    by (induct q) (simp add: zero_rat minus_rat add_rat eq_rat)
paulson@14365
   269
  show "q - r = q + (-r)"
paulson@14365
   270
    by (induct q, induct r) (simp add: add_rat minus_rat diff_rat)
paulson@14365
   271
  show "(q * r) * s = q * (r * s)"
paulson@14365
   272
    by (induct q, induct r, induct s) (simp add: mult_rat mult_ac)
paulson@14365
   273
  show "q * r = r * q"
paulson@14365
   274
    by (induct q, induct r) (simp add: mult_rat mult_ac)
paulson@14365
   275
  show "1 * q = q"
paulson@14365
   276
    by (induct q) (simp add: one_rat mult_rat)
paulson@14365
   277
  show "(q + r) * s = q * s + r * s"
wenzelm@14691
   278
    by (induct q, induct r, induct s)
paulson@14365
   279
       (simp add: add_rat mult_rat eq_rat int_distrib)
paulson@14365
   280
  show "q \<noteq> 0 ==> inverse q * q = 1"
paulson@14365
   281
    by (induct q) (simp add: inverse_rat mult_rat one_rat zero_rat eq_rat)
paulson@14430
   282
  show "q / r = q * inverse r"
wenzelm@14691
   283
    by (simp add: divide_rat_def)
paulson@14365
   284
  show "0 \<noteq> (1::rat)"
wenzelm@14691
   285
    by (simp add: zero_rat one_rat eq_rat)
paulson@14365
   286
qed
paulson@14365
   287
paulson@14365
   288
instance rat :: linorder
paulson@14365
   289
proof
paulson@14365
   290
  fix q r s :: rat
paulson@14365
   291
  {
paulson@14365
   292
    assume "q \<le> r" and "r \<le> s"
paulson@14365
   293
    show "q \<le> s"
paulson@14365
   294
    proof (insert prems, induct q, induct r, induct s)
paulson@14365
   295
      fix a b c d e f :: int
paulson@14365
   296
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   297
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365
   298
      show "Fract a b \<le> Fract e f"
paulson@14365
   299
      proof -
paulson@14365
   300
        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365
   301
          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365
   302
        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365
   303
        proof -
paulson@14365
   304
          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   305
            by (simp add: le_rat)
paulson@14365
   306
          with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   307
        qed
paulson@14365
   308
        also have "... = (c * f) * (d * f) * (b * b)"
paulson@14365
   309
          by (simp only: mult_ac)
paulson@14365
   310
        also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365
   311
        proof -
paulson@14365
   312
          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
paulson@14365
   313
            by (simp add: le_rat)
paulson@14365
   314
          with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   315
        qed
paulson@14365
   316
        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365
   317
          by (simp only: mult_ac)
paulson@14365
   318
        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365
   319
          by (simp add: mult_le_cancel_right)
paulson@14365
   320
        with neq show ?thesis by (simp add: le_rat)
paulson@14365
   321
      qed
paulson@14365
   322
    qed
paulson@14365
   323
  next
paulson@14365
   324
    assume "q \<le> r" and "r \<le> q"
paulson@14365
   325
    show "q = r"
paulson@14365
   326
    proof (insert prems, induct q, induct r)
paulson@14365
   327
      fix a b c d :: int
paulson@14365
   328
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   329
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365
   330
      show "Fract a b = Fract c d"
paulson@14365
   331
      proof -
paulson@14365
   332
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   333
          by (simp add: le_rat)
paulson@14365
   334
        also have "... \<le> (a * d) * (b * d)"
paulson@14365
   335
        proof -
paulson@14365
   336
          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
paulson@14365
   337
            by (simp add: le_rat)
paulson@14365
   338
          thus ?thesis by (simp only: mult_ac)
paulson@14365
   339
        qed
paulson@14365
   340
        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365
   341
        moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365
   342
        ultimately have "a * d = c * b" by simp
paulson@14365
   343
        with neq show ?thesis by (simp add: eq_rat)
paulson@14365
   344
      qed
paulson@14365
   345
    qed
paulson@14365
   346
  next
paulson@14365
   347
    show "q \<le> q"
paulson@14365
   348
      by (induct q) (simp add: le_rat)
paulson@14365
   349
    show "(q < r) = (q \<le> r \<and> q \<noteq> r)"
paulson@14365
   350
      by (simp only: less_rat_def)
paulson@14365
   351
    show "q \<le> r \<or> r \<le> q"
huffman@18913
   352
      by (induct q, induct r)
huffman@18913
   353
         (simp add: le_rat mult_commute, rule linorder_linear)
paulson@14365
   354
  }
paulson@14365
   355
qed
paulson@14365
   356
paulson@14365
   357
instance rat :: ordered_field
paulson@14365
   358
proof
paulson@14365
   359
  fix q r s :: rat
paulson@14365
   360
  show "q \<le> r ==> s + q \<le> s + r"
paulson@14365
   361
  proof (induct q, induct r, induct s)
paulson@14365
   362
    fix a b c d e f :: int
paulson@14365
   363
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   364
    assume le: "Fract a b \<le> Fract c d"
paulson@14365
   365
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365
   366
    proof -
paulson@14365
   367
      let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365
   368
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   369
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   370
        by (simp add: le_rat)
paulson@14365
   371
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365
   372
        by (simp add: mult_le_cancel_right)
paulson@14365
   373
      with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib)
paulson@14365
   374
    qed
paulson@14365
   375
  qed
paulson@14365
   376
  show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365
   377
  proof (induct q, induct r, induct s)
paulson@14365
   378
    fix a b c d e f :: int
paulson@14365
   379
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   380
    assume le: "Fract a b < Fract c d"
paulson@14365
   381
    assume gt: "0 < Fract e f"
paulson@14365
   382
    show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365
   383
    proof -
paulson@14365
   384
      let ?E = "e * f" and ?F = "f * f"
paulson@14365
   385
      from neq gt have "0 < ?E"
paulson@14378
   386
        by (auto simp add: zero_rat less_rat le_rat order_less_le eq_rat)
paulson@14365
   387
      moreover from neq have "0 < ?F"
paulson@14365
   388
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   389
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
paulson@14365
   390
        by (simp add: less_rat)
paulson@14365
   391
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365
   392
        by (simp add: mult_less_cancel_right)
paulson@14365
   393
      with neq show ?thesis
paulson@14365
   394
        by (simp add: less_rat mult_rat mult_ac)
paulson@14365
   395
    qed
paulson@14365
   396
  qed
paulson@14365
   397
  show "\<bar>q\<bar> = (if q < 0 then -q else q)"
paulson@14365
   398
    by (simp only: abs_rat_def)
paulson@14365
   399
qed
paulson@14365
   400
paulson@14365
   401
instance rat :: division_by_zero
paulson@14365
   402
proof
huffman@18913
   403
  show "inverse 0 = (0::rat)"
huffman@18913
   404
    by (simp add: zero_rat Fract_def inverse_rat_def
huffman@18913
   405
                  inverse_congruent UN_ratrel)
paulson@14365
   406
qed
paulson@14365
   407
paulson@14365
   408
paulson@14365
   409
subsection {* Various Other Results *}
paulson@14365
   410
paulson@14365
   411
lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
huffman@18913
   412
by (simp add: eq_rat)
paulson@14365
   413
paulson@14365
   414
theorem Rat_induct_pos [case_names Fract, induct type: rat]:
paulson@14365
   415
  assumes step: "!!a b. 0 < b ==> P (Fract a b)"
paulson@14365
   416
    shows "P q"
paulson@14365
   417
proof (cases q)
paulson@14365
   418
  have step': "!!a b. b < 0 ==> P (Fract a b)"
paulson@14365
   419
  proof -
paulson@14365
   420
    fix a::int and b::int
paulson@14365
   421
    assume b: "b < 0"
paulson@14365
   422
    hence "0 < -b" by simp
paulson@14365
   423
    hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365
   424
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   425
  qed
paulson@14365
   426
  case (Fract a b)
paulson@14365
   427
  thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365
   428
qed
paulson@14365
   429
paulson@14365
   430
lemma zero_less_Fract_iff:
paulson@14365
   431
     "0 < b ==> (0 < Fract a b) = (0 < a)"
wenzelm@14691
   432
by (simp add: zero_rat less_rat order_less_imp_not_eq2 zero_less_mult_iff)
paulson@14365
   433
paulson@14378
   434
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
paulson@14378
   435
apply (insert add_rat [of concl: m n 1 1])
wenzelm@14691
   436
apply (simp add: one_rat  [symmetric])
paulson@14378
   437
done
paulson@14378
   438
paulson@14378
   439
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
wenzelm@14691
   440
apply (induct k)
wenzelm@14691
   441
apply (simp add: zero_rat)
wenzelm@14691
   442
apply (simp add: Fract_add_one)
paulson@14378
   443
done
paulson@14378
   444
paulson@14378
   445
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
wenzelm@14691
   446
proof (cases k rule: int_cases)
paulson@14378
   447
  case (nonneg n)
paulson@14378
   448
    thus ?thesis by (simp add: int_eq_of_nat Fract_of_nat_eq)
wenzelm@14691
   449
next
paulson@14378
   450
  case (neg n)
paulson@14378
   451
    hence "Fract k 1 = - (Fract (of_nat (Suc n)) 1)"
wenzelm@14691
   452
      by (simp only: minus_rat int_eq_of_nat)
paulson@14378
   453
    also have "... =  - (of_nat (Suc n))"
paulson@14378
   454
      by (simp only: Fract_of_nat_eq)
wenzelm@14691
   455
    finally show ?thesis
wenzelm@14691
   456
      by (simp add: only: prems int_eq_of_nat of_int_minus of_int_of_nat_eq)
wenzelm@14691
   457
qed
paulson@14378
   458
paulson@14378
   459
wenzelm@14691
   460
subsection {* Numerals and Arithmetic *}
paulson@14387
   461
paulson@14387
   462
instance rat :: number ..
paulson@14387
   463
paulson@15013
   464
defs (overloaded)
paulson@15013
   465
  rat_number_of_def: "(number_of w :: rat) == of_int (Rep_Bin w)"
paulson@15013
   466
    --{*the type constraint is essential!*}
paulson@14387
   467
paulson@14387
   468
instance rat :: number_ring
paulson@15013
   469
by (intro_classes, simp add: rat_number_of_def) 
paulson@14387
   470
paulson@14387
   471
declare diff_rat_def [symmetric]
paulson@14387
   472
paulson@14387
   473
use "rat_arith.ML"
paulson@14387
   474
paulson@14387
   475
setup rat_arith_setup
paulson@14387
   476
paulson@14365
   477
end