src/HOL/Real/Rational.thy
author haftmann
Fri Jun 17 16:12:49 2005 +0200 (2005-06-17)
changeset 16417 9bc16273c2d4
parent 15140 322485b816ac
child 18372 2bffdf62fe7f
permissions -rw-r--r--
migrated theory headers to new format
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
nipkow@15140
     9
imports Quotient
haftmann@16417
    10
uses ("rat_arith.ML")
nipkow@15131
    11
begin
paulson@14365
    12
paulson@14365
    13
subsection {* Fractions *}
paulson@14365
    14
paulson@14365
    15
subsubsection {* The type of fractions *}
paulson@14365
    16
paulson@14365
    17
typedef fraction = "{(a, b) :: int \<times> int | a b. b \<noteq> 0}"
paulson@14365
    18
proof
paulson@14365
    19
  show "(0, 1) \<in> ?fraction" by simp
paulson@14365
    20
qed
paulson@14365
    21
paulson@14365
    22
constdefs
paulson@14365
    23
  fract :: "int => int => fraction"
paulson@14365
    24
  "fract a b == Abs_fraction (a, b)"
paulson@14365
    25
  num :: "fraction => int"
paulson@14365
    26
  "num Q == fst (Rep_fraction Q)"
paulson@14365
    27
  den :: "fraction => int"
paulson@14365
    28
  "den Q == snd (Rep_fraction Q)"
paulson@14365
    29
paulson@14365
    30
lemma fract_num [simp]: "b \<noteq> 0 ==> num (fract a b) = a"
paulson@14365
    31
  by (simp add: fract_def num_def fraction_def Abs_fraction_inverse)
paulson@14365
    32
paulson@14365
    33
lemma fract_den [simp]: "b \<noteq> 0 ==> den (fract a b) = b"
paulson@14365
    34
  by (simp add: fract_def den_def fraction_def Abs_fraction_inverse)
paulson@14365
    35
paulson@14365
    36
lemma fraction_cases [case_names fract, cases type: fraction]:
paulson@14365
    37
  "(!!a b. Q = fract a b ==> b \<noteq> 0 ==> C) ==> C"
paulson@14365
    38
proof -
paulson@14365
    39
  assume r: "!!a b. Q = fract a b ==> b \<noteq> 0 ==> C"
paulson@14365
    40
  obtain a b where "Q = fract a b" and "b \<noteq> 0"
paulson@14365
    41
    by (cases Q) (auto simp add: fract_def fraction_def)
paulson@14365
    42
  thus C by (rule r)
paulson@14365
    43
qed
paulson@14365
    44
paulson@14365
    45
lemma fraction_induct [case_names fract, induct type: fraction]:
paulson@14365
    46
    "(!!a b. b \<noteq> 0 ==> P (fract a b)) ==> P Q"
paulson@14365
    47
  by (cases Q) simp
paulson@14365
    48
paulson@14365
    49
paulson@14365
    50
subsubsection {* Equivalence of fractions *}
paulson@14365
    51
paulson@14365
    52
instance fraction :: eqv ..
paulson@14365
    53
paulson@14365
    54
defs (overloaded)
paulson@14365
    55
  equiv_fraction_def: "Q \<sim> R == num Q * den R = num R * den Q"
paulson@14365
    56
paulson@14365
    57
lemma equiv_fraction_iff [iff]:
paulson@14365
    58
    "b \<noteq> 0 ==> b' \<noteq> 0 ==> (fract a b \<sim> fract a' b') = (a * b' = a' * b)"
paulson@14365
    59
  by (simp add: equiv_fraction_def)
paulson@14365
    60
paulson@14365
    61
instance fraction :: equiv
paulson@14365
    62
proof
paulson@14365
    63
  fix Q R S :: fraction
paulson@14365
    64
  {
paulson@14365
    65
    show "Q \<sim> Q"
paulson@14365
    66
    proof (induct Q)
paulson@14365
    67
      fix a b :: int
paulson@14365
    68
      assume "b \<noteq> 0" and "b \<noteq> 0"
paulson@14365
    69
      with refl show "fract a b \<sim> fract a b" ..
paulson@14365
    70
    qed
paulson@14365
    71
  next
paulson@14365
    72
    assume "Q \<sim> R" and "R \<sim> S"
paulson@14365
    73
    show "Q \<sim> S"
paulson@14365
    74
    proof (insert prems, induct Q, induct R, induct S)
paulson@14365
    75
      fix a b a' b' a'' b'' :: int
paulson@14365
    76
      assume b: "b \<noteq> 0" and b': "b' \<noteq> 0" and b'': "b'' \<noteq> 0"
paulson@14365
    77
      assume "fract a b \<sim> fract a' b'" hence eq1: "a * b' = a' * b" ..
paulson@14365
    78
      assume "fract a' b' \<sim> fract a'' b''" hence eq2: "a' * b'' = a'' * b'" ..
paulson@14365
    79
      have "a * b'' = a'' * b"
paulson@14365
    80
      proof cases
paulson@14365
    81
        assume "a' = 0"
paulson@14365
    82
        with b' eq1 eq2 have "a = 0 \<and> a'' = 0" by auto
paulson@14365
    83
        thus ?thesis by simp
paulson@14365
    84
      next
paulson@14365
    85
        assume a': "a' \<noteq> 0"
paulson@14365
    86
        from eq1 eq2 have "(a * b') * (a' * b'') = (a' * b) * (a'' * b')" by simp
paulson@14365
    87
        hence "(a * b'') * (a' * b') = (a'' * b) * (a' * b')" by (simp only: mult_ac)
paulson@14365
    88
        with a' b' show ?thesis by simp
paulson@14365
    89
      qed
paulson@14365
    90
      thus "fract a b \<sim> fract a'' b''" ..
paulson@14365
    91
    qed
paulson@14365
    92
  next
paulson@14365
    93
    show "Q \<sim> R ==> R \<sim> Q"
paulson@14365
    94
    proof (induct Q, induct R)
paulson@14365
    95
      fix a b a' b' :: int
paulson@14365
    96
      assume b: "b \<noteq> 0" and b': "b' \<noteq> 0"
paulson@14365
    97
      assume "fract a b \<sim> fract a' b'"
paulson@14365
    98
      hence "a * b' = a' * b" ..
paulson@14365
    99
      hence "a' * b = a * b'" ..
paulson@14365
   100
      thus "fract a' b' \<sim> fract a b" ..
paulson@14365
   101
    qed
paulson@14365
   102
  }
paulson@14365
   103
qed
paulson@14365
   104
paulson@14365
   105
lemma eq_fraction_iff [iff]:
paulson@14365
   106
    "b \<noteq> 0 ==> b' \<noteq> 0 ==> (\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>) = (a * b' = a' * b)"
paulson@14365
   107
  by (simp add: equiv_fraction_iff quot_equality)
paulson@14365
   108
paulson@14365
   109
paulson@14365
   110
subsubsection {* Operations on fractions *}
paulson@14365
   111
paulson@14365
   112
text {*
paulson@14365
   113
 We define the basic arithmetic operations on fractions and
paulson@14365
   114
 demonstrate their ``well-definedness'', i.e.\ congruence with respect
paulson@14365
   115
 to equivalence of fractions.
paulson@14365
   116
*}
paulson@14365
   117
wenzelm@14691
   118
instance fraction :: "{zero, one, plus, minus, times, inverse, ord}" ..
paulson@14365
   119
paulson@14365
   120
defs (overloaded)
paulson@14365
   121
  zero_fraction_def: "0 == fract 0 1"
paulson@14365
   122
  one_fraction_def: "1 == fract 1 1"
paulson@14365
   123
  add_fraction_def: "Q + R ==
paulson@14365
   124
    fract (num Q * den R + num R * den Q) (den Q * den R)"
paulson@14365
   125
  minus_fraction_def: "-Q == fract (-(num Q)) (den Q)"
paulson@14365
   126
  mult_fraction_def: "Q * R == fract (num Q * num R) (den Q * den R)"
paulson@14365
   127
  inverse_fraction_def: "inverse Q == fract (den Q) (num Q)"
paulson@14365
   128
  le_fraction_def: "Q \<le> R ==
paulson@14365
   129
    (num Q * den R) * (den Q * den R) \<le> (num R * den Q) * (den Q * den R)"
paulson@14365
   130
paulson@14365
   131
lemma is_zero_fraction_iff: "b \<noteq> 0 ==> (\<lfloor>fract a b\<rfloor> = \<lfloor>0\<rfloor>) = (a = 0)"
paulson@14365
   132
  by (simp add: zero_fraction_def eq_fraction_iff)
paulson@14365
   133
paulson@14365
   134
theorem add_fraction_cong:
paulson@14365
   135
  "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>
paulson@14365
   136
    ==> b \<noteq> 0 ==> b' \<noteq> 0 ==> d \<noteq> 0 ==> d' \<noteq> 0
paulson@14365
   137
    ==> \<lfloor>fract a b + fract c d\<rfloor> = \<lfloor>fract a' b' + fract c' d'\<rfloor>"
paulson@14365
   138
proof -
paulson@14365
   139
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
paulson@14365
   140
  assume "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>" hence eq1: "a * b' = a' * b" ..
paulson@14365
   141
  assume "\<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>" hence eq2: "c * d' = c' * d" ..
paulson@14365
   142
  have "\<lfloor>fract (a * d + c * b) (b * d)\<rfloor> = \<lfloor>fract (a' * d' + c' * b') (b' * d')\<rfloor>"
paulson@14365
   143
  proof
paulson@14365
   144
    show "(a * d + c * b) * (b' * d') = (a' * d' + c' * b') * (b * d)"
paulson@14365
   145
      (is "?lhs = ?rhs")
paulson@14365
   146
    proof -
paulson@14365
   147
      have "?lhs = (a * b') * (d * d') + (c * d') * (b * b')"
paulson@14365
   148
        by (simp add: int_distrib mult_ac)
paulson@14365
   149
      also have "... = (a' * b) * (d * d') + (c' * d) * (b * b')"
paulson@14365
   150
        by (simp only: eq1 eq2)
paulson@14365
   151
      also have "... = ?rhs"
paulson@14365
   152
        by (simp add: int_distrib mult_ac)
paulson@14365
   153
      finally show "?lhs = ?rhs" .
paulson@14365
   154
    qed
paulson@14365
   155
    from neq show "b * d \<noteq> 0" by simp
paulson@14365
   156
    from neq show "b' * d' \<noteq> 0" by simp
paulson@14365
   157
  qed
paulson@14365
   158
  with neq show ?thesis by (simp add: add_fraction_def)
paulson@14365
   159
qed
paulson@14365
   160
paulson@14365
   161
theorem minus_fraction_cong:
paulson@14365
   162
  "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> b \<noteq> 0 ==> b' \<noteq> 0
paulson@14365
   163
    ==> \<lfloor>-(fract a b)\<rfloor> = \<lfloor>-(fract a' b')\<rfloor>"
paulson@14365
   164
proof -
paulson@14365
   165
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"
paulson@14365
   166
  assume "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>"
paulson@14365
   167
  hence "a * b' = a' * b" ..
paulson@14365
   168
  hence "-a * b' = -a' * b" by simp
paulson@14365
   169
  hence "\<lfloor>fract (-a) b\<rfloor> = \<lfloor>fract (-a') b'\<rfloor>" ..
paulson@14365
   170
  with neq show ?thesis by (simp add: minus_fraction_def)
paulson@14365
   171
qed
paulson@14365
   172
paulson@14365
   173
theorem mult_fraction_cong:
paulson@14365
   174
  "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>
paulson@14365
   175
    ==> b \<noteq> 0 ==> b' \<noteq> 0 ==> d \<noteq> 0 ==> d' \<noteq> 0
paulson@14365
   176
    ==> \<lfloor>fract a b * fract c d\<rfloor> = \<lfloor>fract a' b' * fract c' d'\<rfloor>"
paulson@14365
   177
proof -
paulson@14365
   178
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
paulson@14365
   179
  assume "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>" hence eq1: "a * b' = a' * b" ..
paulson@14365
   180
  assume "\<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>" hence eq2: "c * d' = c' * d" ..
paulson@14365
   181
  have "\<lfloor>fract (a * c) (b * d)\<rfloor> = \<lfloor>fract (a' * c') (b' * d')\<rfloor>"
paulson@14365
   182
  proof
paulson@14365
   183
    from eq1 eq2 have "(a * b') * (c * d') = (a' * b) * (c' * d)" by simp
paulson@14365
   184
    thus "(a * c) * (b' * d') = (a' * c') * (b * d)" by (simp add: mult_ac)
paulson@14365
   185
    from neq show "b * d \<noteq> 0" by simp
paulson@14365
   186
    from neq show "b' * d' \<noteq> 0" by simp
paulson@14365
   187
  qed
paulson@14365
   188
  with neq show "\<lfloor>fract a b * fract c d\<rfloor> = \<lfloor>fract a' b' * fract c' d'\<rfloor>"
paulson@14365
   189
    by (simp add: mult_fraction_def)
paulson@14365
   190
qed
paulson@14365
   191
paulson@14365
   192
theorem inverse_fraction_cong:
paulson@14365
   193
  "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract a b\<rfloor> \<noteq> \<lfloor>0\<rfloor> ==> \<lfloor>fract a' b'\<rfloor> \<noteq> \<lfloor>0\<rfloor>
paulson@14365
   194
    ==> b \<noteq> 0 ==> b' \<noteq> 0
paulson@14365
   195
    ==> \<lfloor>inverse (fract a b)\<rfloor> = \<lfloor>inverse (fract a' b')\<rfloor>"
paulson@14365
   196
proof -
paulson@14365
   197
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"
paulson@14365
   198
  assume "\<lfloor>fract a b\<rfloor> \<noteq> \<lfloor>0\<rfloor>" and "\<lfloor>fract a' b'\<rfloor> \<noteq> \<lfloor>0\<rfloor>"
paulson@14365
   199
  with neq obtain "a \<noteq> 0" and "a' \<noteq> 0" by (simp add: is_zero_fraction_iff)
paulson@14365
   200
  assume "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>"
paulson@14365
   201
  hence "a * b' = a' * b" ..
paulson@14365
   202
  hence "b * a' = b' * a" by (simp only: mult_ac)
paulson@14365
   203
  hence "\<lfloor>fract b a\<rfloor> = \<lfloor>fract b' a'\<rfloor>" ..
paulson@14365
   204
  with neq show ?thesis by (simp add: inverse_fraction_def)
paulson@14365
   205
qed
paulson@14365
   206
paulson@14365
   207
theorem le_fraction_cong:
paulson@14365
   208
  "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>
paulson@14365
   209
    ==> b \<noteq> 0 ==> b' \<noteq> 0 ==> d \<noteq> 0 ==> d' \<noteq> 0
paulson@14365
   210
    ==> (fract a b \<le> fract c d) = (fract a' b' \<le> fract c' d')"
paulson@14365
   211
proof -
paulson@14365
   212
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
paulson@14365
   213
  assume "\<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor>" hence eq1: "a * b' = a' * b" ..
paulson@14365
   214
  assume "\<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor>" hence eq2: "c * d' = c' * d" ..
paulson@14365
   215
paulson@14365
   216
  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365
   217
  {
paulson@14365
   218
    fix a b c d x :: int assume x: "x \<noteq> 0"
paulson@14365
   219
    have "?le a b c d = ?le (a * x) (b * x) c d"
paulson@14365
   220
    proof -
paulson@14365
   221
      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
paulson@14365
   222
      hence "?le a b c d =
paulson@14365
   223
          ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
paulson@14365
   224
        by (simp add: mult_le_cancel_right)
paulson@14365
   225
      also have "... = ?le (a * x) (b * x) c d"
paulson@14365
   226
        by (simp add: mult_ac)
paulson@14365
   227
      finally show ?thesis .
paulson@14365
   228
    qed
paulson@14365
   229
  } note le_factor = this
paulson@14365
   230
paulson@14365
   231
  let ?D = "b * d" and ?D' = "b' * d'"
paulson@14365
   232
  from neq have D: "?D \<noteq> 0" by simp
paulson@14365
   233
  from neq have "?D' \<noteq> 0" by simp
paulson@14365
   234
  hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
paulson@14365
   235
    by (rule le_factor)
paulson@14365
   236
  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
paulson@14365
   237
    by (simp add: mult_ac)
paulson@14365
   238
  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
paulson@14365
   239
    by (simp only: eq1 eq2)
paulson@14365
   240
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
paulson@14365
   241
    by (simp add: mult_ac)
paulson@14365
   242
  also from D have "... = ?le a' b' c' d'"
paulson@14365
   243
    by (rule le_factor [symmetric])
paulson@14365
   244
  finally have "?le a b c d = ?le a' b' c' d'" .
paulson@14365
   245
  with neq show ?thesis by (simp add: le_fraction_def)
paulson@14365
   246
qed
paulson@14365
   247
paulson@14365
   248
paulson@14365
   249
subsection {* Rational numbers *}
paulson@14365
   250
paulson@14365
   251
subsubsection {* The type of rational numbers *}
paulson@14365
   252
paulson@14365
   253
typedef (Rat)
paulson@14365
   254
  rat = "UNIV :: fraction quot set" ..
paulson@14365
   255
paulson@14365
   256
lemma RatI [intro, simp]: "Q \<in> Rat"
paulson@14365
   257
  by (simp add: Rat_def)
paulson@14365
   258
paulson@14365
   259
constdefs
paulson@14365
   260
  fraction_of :: "rat => fraction"
paulson@14365
   261
  "fraction_of q == pick (Rep_Rat q)"
paulson@14365
   262
  rat_of :: "fraction => rat"
paulson@14365
   263
  "rat_of Q == Abs_Rat \<lfloor>Q\<rfloor>"
paulson@14365
   264
paulson@14365
   265
theorem rat_of_equality [iff?]: "(rat_of Q = rat_of Q') = (\<lfloor>Q\<rfloor> = \<lfloor>Q'\<rfloor>)"
paulson@14365
   266
  by (simp add: rat_of_def Abs_Rat_inject)
paulson@14365
   267
paulson@14365
   268
lemma rat_of: "\<lfloor>Q\<rfloor> = \<lfloor>Q'\<rfloor> ==> rat_of Q = rat_of Q'" ..
paulson@14365
   269
paulson@14365
   270
constdefs
paulson@14365
   271
  Fract :: "int => int => rat"
paulson@14365
   272
  "Fract a b == rat_of (fract a b)"
paulson@14365
   273
paulson@14365
   274
theorem Fract_inverse: "\<lfloor>fraction_of (Fract a b)\<rfloor> = \<lfloor>fract a b\<rfloor>"
paulson@14365
   275
  by (simp add: fraction_of_def rat_of_def Fract_def Abs_Rat_inverse pick_inverse)
paulson@14365
   276
paulson@14365
   277
theorem Fract_equality [iff?]:
paulson@14365
   278
    "(Fract a b = Fract c d) = (\<lfloor>fract a b\<rfloor> = \<lfloor>fract c d\<rfloor>)"
paulson@14365
   279
  by (simp add: Fract_def rat_of_equality)
paulson@14365
   280
paulson@14365
   281
theorem eq_rat:
paulson@14365
   282
    "b \<noteq> 0 ==> d \<noteq> 0 ==> (Fract a b = Fract c d) = (a * d = c * b)"
paulson@14365
   283
  by (simp add: Fract_equality eq_fraction_iff)
paulson@14365
   284
paulson@14365
   285
theorem Rat_cases [case_names Fract, cases type: rat]:
paulson@14365
   286
  "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
paulson@14365
   287
proof -
paulson@14365
   288
  assume r: "!!a b. q = Fract a b ==> b \<noteq> 0 ==> C"
paulson@14365
   289
  obtain x where "q = Abs_Rat x" by (cases q)
paulson@14365
   290
  moreover obtain Q where "x = \<lfloor>Q\<rfloor>" by (cases x)
paulson@14365
   291
  moreover obtain a b where "Q = fract a b" and "b \<noteq> 0" by (cases Q)
paulson@14365
   292
  ultimately have "q = Fract a b" by (simp only: Fract_def rat_of_def)
paulson@14365
   293
  thus ?thesis by (rule r)
paulson@14365
   294
qed
paulson@14365
   295
paulson@14365
   296
theorem Rat_induct [case_names Fract, induct type: rat]:
paulson@14365
   297
    "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
paulson@14365
   298
  by (cases q) simp
paulson@14365
   299
paulson@14365
   300
paulson@14365
   301
subsubsection {* Canonical function definitions *}
paulson@14365
   302
paulson@14365
   303
text {*
paulson@14365
   304
  Note that the unconditional version below is much easier to read.
paulson@14365
   305
*}
paulson@14365
   306
paulson@14365
   307
theorem rat_cond_function:
paulson@14365
   308
  "(!!q r. P \<lfloor>fraction_of q\<rfloor> \<lfloor>fraction_of r\<rfloor> ==>
paulson@14365
   309
      f q r == g (fraction_of q) (fraction_of r)) ==>
paulson@14365
   310
    (!!a b a' b' c d c' d'.
paulson@14365
   311
      \<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor> ==>
paulson@14365
   312
      P \<lfloor>fract a b\<rfloor> \<lfloor>fract c d\<rfloor> ==> P \<lfloor>fract a' b'\<rfloor> \<lfloor>fract c' d'\<rfloor> ==>
paulson@14365
   313
      b \<noteq> 0 ==> b' \<noteq> 0 ==> d \<noteq> 0 ==> d' \<noteq> 0 ==>
paulson@14365
   314
      g (fract a b) (fract c d) = g (fract a' b') (fract c' d')) ==>
paulson@14365
   315
    P \<lfloor>fract a b\<rfloor> \<lfloor>fract c d\<rfloor> ==>
paulson@14365
   316
      f (Fract a b) (Fract c d) = g (fract a b) (fract c d)"
paulson@14365
   317
  (is "PROP ?eq ==> PROP ?cong ==> ?P ==> _")
paulson@14365
   318
proof -
paulson@14365
   319
  assume eq: "PROP ?eq" and cong: "PROP ?cong" and P: ?P
paulson@14365
   320
  have "f (Abs_Rat \<lfloor>fract a b\<rfloor>) (Abs_Rat \<lfloor>fract c d\<rfloor>) = g (fract a b) (fract c d)"
paulson@14365
   321
  proof (rule quot_cond_function)
paulson@14365
   322
    fix X Y assume "P X Y"
paulson@14365
   323
    with eq show "f (Abs_Rat X) (Abs_Rat Y) == g (pick X) (pick Y)"
paulson@14365
   324
      by (simp add: fraction_of_def pick_inverse Abs_Rat_inverse)
paulson@14365
   325
  next
paulson@14365
   326
    fix Q Q' R R' :: fraction
paulson@14365
   327
    show "\<lfloor>Q\<rfloor> = \<lfloor>Q'\<rfloor> ==> \<lfloor>R\<rfloor> = \<lfloor>R'\<rfloor> ==>
paulson@14365
   328
        P \<lfloor>Q\<rfloor> \<lfloor>R\<rfloor> ==> P \<lfloor>Q'\<rfloor> \<lfloor>R'\<rfloor> ==> g Q R = g Q' R'"
paulson@14365
   329
      by (induct Q, induct Q', induct R, induct R') (rule cong)
paulson@14365
   330
  qed
paulson@14365
   331
  thus ?thesis by (unfold Fract_def rat_of_def)
paulson@14365
   332
qed
paulson@14365
   333
paulson@14365
   334
theorem rat_function:
paulson@14365
   335
  "(!!q r. f q r == g (fraction_of q) (fraction_of r)) ==>
paulson@14365
   336
    (!!a b a' b' c d c' d'.
paulson@14365
   337
      \<lfloor>fract a b\<rfloor> = \<lfloor>fract a' b'\<rfloor> ==> \<lfloor>fract c d\<rfloor> = \<lfloor>fract c' d'\<rfloor> ==>
paulson@14365
   338
      b \<noteq> 0 ==> b' \<noteq> 0 ==> d \<noteq> 0 ==> d' \<noteq> 0 ==>
paulson@14365
   339
      g (fract a b) (fract c d) = g (fract a' b') (fract c' d')) ==>
paulson@14365
   340
    f (Fract a b) (Fract c d) = g (fract a b) (fract c d)"
paulson@14365
   341
proof -
paulson@14365
   342
  case rule_context from this TrueI
paulson@14365
   343
  show ?thesis by (rule rat_cond_function)
paulson@14365
   344
qed
paulson@14365
   345
paulson@14365
   346
paulson@14365
   347
subsubsection {* Standard operations on rational numbers *}
paulson@14365
   348
wenzelm@14691
   349
instance rat :: "{zero, one, plus, minus, times, inverse, ord}" ..
paulson@14365
   350
paulson@14365
   351
defs (overloaded)
paulson@14365
   352
  zero_rat_def: "0 == rat_of 0"
paulson@14365
   353
  one_rat_def: "1 == rat_of 1"
paulson@14365
   354
  add_rat_def: "q + r == rat_of (fraction_of q + fraction_of r)"
paulson@14365
   355
  minus_rat_def: "-q == rat_of (-(fraction_of q))"
paulson@14365
   356
  diff_rat_def: "q - r == q + (-(r::rat))"
paulson@14365
   357
  mult_rat_def: "q * r == rat_of (fraction_of q * fraction_of r)"
wenzelm@14691
   358
  inverse_rat_def: "inverse q ==
paulson@14365
   359
                    if q=0 then 0 else rat_of (inverse (fraction_of q))"
paulson@14365
   360
  divide_rat_def: "q / r == q * inverse (r::rat)"
paulson@14365
   361
  le_rat_def: "q \<le> r == fraction_of q \<le> fraction_of r"
paulson@14365
   362
  less_rat_def: "q < r == q \<le> r \<and> q \<noteq> (r::rat)"
paulson@14365
   363
  abs_rat_def: "\<bar>q\<bar> == if q < 0 then -q else (q::rat)"
paulson@14365
   364
paulson@14365
   365
theorem zero_rat: "0 = Fract 0 1"
wenzelm@14691
   366
  by (simp add: zero_rat_def zero_fraction_def rat_of_def Fract_def)
paulson@14365
   367
paulson@14365
   368
theorem one_rat: "1 = Fract 1 1"
paulson@14365
   369
  by (simp add: one_rat_def one_fraction_def rat_of_def Fract_def)
paulson@14365
   370
paulson@14365
   371
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   372
  Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
paulson@14365
   373
proof -
paulson@14365
   374
  have "Fract a b + Fract c d = rat_of (fract a b + fract c d)"
paulson@14365
   375
    by (rule rat_function, rule add_rat_def, rule rat_of, rule add_fraction_cong)
paulson@14365
   376
  also
paulson@14365
   377
  assume "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   378
  hence "fract a b + fract c d = fract (a * d + c * b) (b * d)"
paulson@14365
   379
    by (simp add: add_fraction_def)
paulson@14365
   380
  finally show ?thesis by (unfold Fract_def)
paulson@14365
   381
qed
paulson@14365
   382
paulson@14365
   383
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
paulson@14365
   384
proof -
paulson@14365
   385
  have "-(Fract a b) = rat_of (-(fract a b))"
paulson@14365
   386
    by (rule rat_function, rule minus_rat_def, rule rat_of, rule minus_fraction_cong)
paulson@14365
   387
  also assume "b \<noteq> 0" hence "-(fract a b) = fract (-a) b"
paulson@14365
   388
    by (simp add: minus_fraction_def)
paulson@14365
   389
  finally show ?thesis by (unfold Fract_def)
paulson@14365
   390
qed
paulson@14365
   391
paulson@14365
   392
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   393
    Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
paulson@14365
   394
  by (simp add: diff_rat_def add_rat minus_rat)
paulson@14365
   395
paulson@14365
   396
theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   397
  Fract a b * Fract c d = Fract (a * c) (b * d)"
paulson@14365
   398
proof -
paulson@14365
   399
  have "Fract a b * Fract c d = rat_of (fract a b * fract c d)"
paulson@14365
   400
    by (rule rat_function, rule mult_rat_def, rule rat_of, rule mult_fraction_cong)
paulson@14365
   401
  also
paulson@14365
   402
  assume "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   403
  hence "fract a b * fract c d = fract (a * c) (b * d)"
paulson@14365
   404
    by (simp add: mult_fraction_def)
paulson@14365
   405
  finally show ?thesis by (unfold Fract_def)
paulson@14365
   406
qed
paulson@14365
   407
paulson@14365
   408
theorem inverse_rat: "Fract a b \<noteq> 0 ==> b \<noteq> 0 ==>
paulson@14365
   409
  inverse (Fract a b) = Fract b a"
paulson@14365
   410
proof -
paulson@14365
   411
  assume neq: "b \<noteq> 0" and nonzero: "Fract a b \<noteq> 0"
paulson@14365
   412
  hence "\<lfloor>fract a b\<rfloor> \<noteq> \<lfloor>0\<rfloor>"
paulson@14365
   413
    by (simp add: zero_rat eq_rat is_zero_fraction_iff)
paulson@14365
   414
  with _ inverse_fraction_cong [THEN rat_of]
paulson@14365
   415
  have "inverse (Fract a b) = rat_of (inverse (fract a b))"
paulson@14365
   416
  proof (rule rat_cond_function)
paulson@14365
   417
    fix q assume cond: "\<lfloor>fraction_of q\<rfloor> \<noteq> \<lfloor>0\<rfloor>"
paulson@14365
   418
    have "q \<noteq> 0"
paulson@14365
   419
    proof (cases q)
paulson@14365
   420
      fix a b assume "b \<noteq> 0" and "q = Fract a b"
paulson@14365
   421
      from this cond show ?thesis
paulson@14365
   422
        by (simp add: Fract_inverse is_zero_fraction_iff zero_rat eq_rat)
paulson@14365
   423
    qed
paulson@14365
   424
    thus "inverse q == rat_of (inverse (fraction_of q))"
paulson@14365
   425
      by (simp add: inverse_rat_def)
paulson@14365
   426
  qed
paulson@14365
   427
  also from neq nonzero have "inverse (fract a b) = fract b a"
paulson@14365
   428
    by (simp add: inverse_fraction_def)
paulson@14365
   429
  finally show ?thesis by (unfold Fract_def)
paulson@14365
   430
qed
paulson@14365
   431
paulson@14365
   432
theorem divide_rat: "Fract c d \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   433
  Fract a b / Fract c d = Fract (a * d) (b * c)"
paulson@14365
   434
proof -
paulson@14365
   435
  assume neq: "b \<noteq> 0"  "d \<noteq> 0" and nonzero: "Fract c d \<noteq> 0"
paulson@14365
   436
  hence "c \<noteq> 0" by (simp add: zero_rat eq_rat)
paulson@14365
   437
  with neq nonzero show ?thesis
paulson@14365
   438
    by (simp add: divide_rat_def inverse_rat mult_rat)
paulson@14365
   439
qed
paulson@14365
   440
paulson@14365
   441
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   442
  (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365
   443
proof -
paulson@14365
   444
  have "(Fract a b \<le> Fract c d) = (fract a b \<le> fract c d)"
paulson@14365
   445
    by (rule rat_function, rule le_rat_def, rule le_fraction_cong)
paulson@14365
   446
  also
paulson@14365
   447
  assume "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   448
  hence "(fract a b \<le> fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365
   449
    by (simp add: le_fraction_def)
paulson@14365
   450
  finally show ?thesis .
paulson@14365
   451
qed
paulson@14365
   452
paulson@14365
   453
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   454
    (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
paulson@14378
   455
  by (simp add: less_rat_def le_rat eq_rat order_less_le)
paulson@14365
   456
paulson@14365
   457
theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
paulson@14365
   458
  by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat)
wenzelm@14691
   459
     (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less
paulson@14365
   460
                split: abs_split)
paulson@14365
   461
paulson@14365
   462
paulson@14365
   463
subsubsection {* The ordered field of rational numbers *}
paulson@14365
   464
paulson@14365
   465
lemma rat_add_assoc: "(q + r) + s = q + (r + (s::rat))"
wenzelm@14691
   466
  by (induct q, induct r, induct s)
paulson@14365
   467
     (simp add: add_rat add_ac mult_ac int_distrib)
paulson@14365
   468
paulson@14365
   469
lemma rat_add_0: "0 + q = (q::rat)"
paulson@14365
   470
  by (induct q) (simp add: zero_rat add_rat)
paulson@14365
   471
paulson@14365
   472
lemma rat_left_minus: "(-q) + q = (0::rat)"
paulson@14365
   473
  by (induct q) (simp add: zero_rat minus_rat add_rat eq_rat)
paulson@14365
   474
paulson@14365
   475
paulson@14365
   476
instance rat :: field
paulson@14365
   477
proof
paulson@14365
   478
  fix q r s :: rat
paulson@14365
   479
  show "(q + r) + s = q + (r + s)"
paulson@14365
   480
    by (rule rat_add_assoc)
paulson@14365
   481
  show "q + r = r + q"
paulson@14365
   482
    by (induct q, induct r) (simp add: add_rat add_ac mult_ac)
paulson@14365
   483
  show "0 + q = q"
paulson@14365
   484
    by (induct q) (simp add: zero_rat add_rat)
paulson@14365
   485
  show "(-q) + q = 0"
paulson@14365
   486
    by (rule rat_left_minus)
paulson@14365
   487
  show "q - r = q + (-r)"
paulson@14365
   488
    by (induct q, induct r) (simp add: add_rat minus_rat diff_rat)
paulson@14365
   489
  show "(q * r) * s = q * (r * s)"
paulson@14365
   490
    by (induct q, induct r, induct s) (simp add: mult_rat mult_ac)
paulson@14365
   491
  show "q * r = r * q"
paulson@14365
   492
    by (induct q, induct r) (simp add: mult_rat mult_ac)
paulson@14365
   493
  show "1 * q = q"
paulson@14365
   494
    by (induct q) (simp add: one_rat mult_rat)
paulson@14365
   495
  show "(q + r) * s = q * s + r * s"
wenzelm@14691
   496
    by (induct q, induct r, induct s)
paulson@14365
   497
       (simp add: add_rat mult_rat eq_rat int_distrib)
paulson@14365
   498
  show "q \<noteq> 0 ==> inverse q * q = 1"
paulson@14365
   499
    by (induct q) (simp add: inverse_rat mult_rat one_rat zero_rat eq_rat)
paulson@14430
   500
  show "q / r = q * inverse r"
wenzelm@14691
   501
    by (simp add: divide_rat_def)
paulson@14365
   502
  show "0 \<noteq> (1::rat)"
wenzelm@14691
   503
    by (simp add: zero_rat one_rat eq_rat)
paulson@14365
   504
qed
paulson@14365
   505
paulson@14365
   506
instance rat :: linorder
paulson@14365
   507
proof
paulson@14365
   508
  fix q r s :: rat
paulson@14365
   509
  {
paulson@14365
   510
    assume "q \<le> r" and "r \<le> s"
paulson@14365
   511
    show "q \<le> s"
paulson@14365
   512
    proof (insert prems, induct q, induct r, induct s)
paulson@14365
   513
      fix a b c d e f :: int
paulson@14365
   514
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   515
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365
   516
      show "Fract a b \<le> Fract e f"
paulson@14365
   517
      proof -
paulson@14365
   518
        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365
   519
          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365
   520
        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365
   521
        proof -
paulson@14365
   522
          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   523
            by (simp add: le_rat)
paulson@14365
   524
          with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   525
        qed
paulson@14365
   526
        also have "... = (c * f) * (d * f) * (b * b)"
paulson@14365
   527
          by (simp only: mult_ac)
paulson@14365
   528
        also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365
   529
        proof -
paulson@14365
   530
          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
paulson@14365
   531
            by (simp add: le_rat)
paulson@14365
   532
          with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   533
        qed
paulson@14365
   534
        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365
   535
          by (simp only: mult_ac)
paulson@14365
   536
        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365
   537
          by (simp add: mult_le_cancel_right)
paulson@14365
   538
        with neq show ?thesis by (simp add: le_rat)
paulson@14365
   539
      qed
paulson@14365
   540
    qed
paulson@14365
   541
  next
paulson@14365
   542
    assume "q \<le> r" and "r \<le> q"
paulson@14365
   543
    show "q = r"
paulson@14365
   544
    proof (insert prems, induct q, induct r)
paulson@14365
   545
      fix a b c d :: int
paulson@14365
   546
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   547
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365
   548
      show "Fract a b = Fract c d"
paulson@14365
   549
      proof -
paulson@14365
   550
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   551
          by (simp add: le_rat)
paulson@14365
   552
        also have "... \<le> (a * d) * (b * d)"
paulson@14365
   553
        proof -
paulson@14365
   554
          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
paulson@14365
   555
            by (simp add: le_rat)
paulson@14365
   556
          thus ?thesis by (simp only: mult_ac)
paulson@14365
   557
        qed
paulson@14365
   558
        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365
   559
        moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365
   560
        ultimately have "a * d = c * b" by simp
paulson@14365
   561
        with neq show ?thesis by (simp add: eq_rat)
paulson@14365
   562
      qed
paulson@14365
   563
    qed
paulson@14365
   564
  next
paulson@14365
   565
    show "q \<le> q"
paulson@14365
   566
      by (induct q) (simp add: le_rat)
paulson@14365
   567
    show "(q < r) = (q \<le> r \<and> q \<noteq> r)"
paulson@14365
   568
      by (simp only: less_rat_def)
paulson@14365
   569
    show "q \<le> r \<or> r \<le> q"
paulson@14365
   570
      by (induct q, induct r) (simp add: le_rat mult_ac, arith)
paulson@14365
   571
  }
paulson@14365
   572
qed
paulson@14365
   573
paulson@14365
   574
instance rat :: ordered_field
paulson@14365
   575
proof
paulson@14365
   576
  fix q r s :: rat
paulson@14365
   577
  show "q \<le> r ==> s + q \<le> s + r"
paulson@14365
   578
  proof (induct q, induct r, induct s)
paulson@14365
   579
    fix a b c d e f :: int
paulson@14365
   580
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   581
    assume le: "Fract a b \<le> Fract c d"
paulson@14365
   582
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365
   583
    proof -
paulson@14365
   584
      let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365
   585
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   586
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   587
        by (simp add: le_rat)
paulson@14365
   588
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365
   589
        by (simp add: mult_le_cancel_right)
paulson@14365
   590
      with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib)
paulson@14365
   591
    qed
paulson@14365
   592
  qed
paulson@14365
   593
  show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365
   594
  proof (induct q, induct r, induct s)
paulson@14365
   595
    fix a b c d e f :: int
paulson@14365
   596
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   597
    assume le: "Fract a b < Fract c d"
paulson@14365
   598
    assume gt: "0 < Fract e f"
paulson@14365
   599
    show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365
   600
    proof -
paulson@14365
   601
      let ?E = "e * f" and ?F = "f * f"
paulson@14365
   602
      from neq gt have "0 < ?E"
paulson@14378
   603
        by (auto simp add: zero_rat less_rat le_rat order_less_le eq_rat)
paulson@14365
   604
      moreover from neq have "0 < ?F"
paulson@14365
   605
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   606
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
paulson@14365
   607
        by (simp add: less_rat)
paulson@14365
   608
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365
   609
        by (simp add: mult_less_cancel_right)
paulson@14365
   610
      with neq show ?thesis
paulson@14365
   611
        by (simp add: less_rat mult_rat mult_ac)
paulson@14365
   612
    qed
paulson@14365
   613
  qed
paulson@14365
   614
  show "\<bar>q\<bar> = (if q < 0 then -q else q)"
paulson@14365
   615
    by (simp only: abs_rat_def)
paulson@14365
   616
qed
paulson@14365
   617
paulson@14365
   618
instance rat :: division_by_zero
paulson@14365
   619
proof
paulson@14365
   620
  show "inverse 0 = (0::rat)"  by (simp add: inverse_rat_def)
paulson@14365
   621
qed
paulson@14365
   622
paulson@14365
   623
paulson@14365
   624
subsection {* Various Other Results *}
paulson@14365
   625
paulson@14365
   626
lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
wenzelm@14691
   627
by (simp add: Fract_equality eq_fraction_iff)
paulson@14365
   628
paulson@14365
   629
theorem Rat_induct_pos [case_names Fract, induct type: rat]:
paulson@14365
   630
  assumes step: "!!a b. 0 < b ==> P (Fract a b)"
paulson@14365
   631
    shows "P q"
paulson@14365
   632
proof (cases q)
paulson@14365
   633
  have step': "!!a b. b < 0 ==> P (Fract a b)"
paulson@14365
   634
  proof -
paulson@14365
   635
    fix a::int and b::int
paulson@14365
   636
    assume b: "b < 0"
paulson@14365
   637
    hence "0 < -b" by simp
paulson@14365
   638
    hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365
   639
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   640
  qed
paulson@14365
   641
  case (Fract a b)
paulson@14365
   642
  thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365
   643
qed
paulson@14365
   644
paulson@14365
   645
lemma zero_less_Fract_iff:
paulson@14365
   646
     "0 < b ==> (0 < Fract a b) = (0 < a)"
wenzelm@14691
   647
by (simp add: zero_rat less_rat order_less_imp_not_eq2 zero_less_mult_iff)
paulson@14365
   648
paulson@14378
   649
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
paulson@14378
   650
apply (insert add_rat [of concl: m n 1 1])
wenzelm@14691
   651
apply (simp add: one_rat  [symmetric])
paulson@14378
   652
done
paulson@14378
   653
paulson@14378
   654
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
wenzelm@14691
   655
apply (induct k)
wenzelm@14691
   656
apply (simp add: zero_rat)
wenzelm@14691
   657
apply (simp add: Fract_add_one)
paulson@14378
   658
done
paulson@14378
   659
paulson@14378
   660
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
wenzelm@14691
   661
proof (cases k rule: int_cases)
paulson@14378
   662
  case (nonneg n)
paulson@14378
   663
    thus ?thesis by (simp add: int_eq_of_nat Fract_of_nat_eq)
wenzelm@14691
   664
next
paulson@14378
   665
  case (neg n)
paulson@14378
   666
    hence "Fract k 1 = - (Fract (of_nat (Suc n)) 1)"
wenzelm@14691
   667
      by (simp only: minus_rat int_eq_of_nat)
paulson@14378
   668
    also have "... =  - (of_nat (Suc n))"
paulson@14378
   669
      by (simp only: Fract_of_nat_eq)
wenzelm@14691
   670
    finally show ?thesis
wenzelm@14691
   671
      by (simp add: only: prems int_eq_of_nat of_int_minus of_int_of_nat_eq)
wenzelm@14691
   672
qed
paulson@14378
   673
paulson@14378
   674
wenzelm@14691
   675
subsection {* Numerals and Arithmetic *}
paulson@14387
   676
paulson@14387
   677
instance rat :: number ..
paulson@14387
   678
paulson@15013
   679
defs (overloaded)
paulson@15013
   680
  rat_number_of_def: "(number_of w :: rat) == of_int (Rep_Bin w)"
paulson@15013
   681
    --{*the type constraint is essential!*}
paulson@14387
   682
paulson@14387
   683
instance rat :: number_ring
paulson@15013
   684
by (intro_classes, simp add: rat_number_of_def) 
paulson@14387
   685
paulson@14387
   686
declare diff_rat_def [symmetric]
paulson@14387
   687
paulson@14387
   688
use "rat_arith.ML"
paulson@14387
   689
paulson@14387
   690
setup rat_arith_setup
paulson@14387
   691
paulson@14365
   692
end