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