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