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