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