src/HOL/Real/Rational.thy
author huffman
Thu, 02 Feb 2006 19:57:13 +0100
changeset 18913 57f19fad8c2a
parent 18372 2bffdf62fe7f
child 18982 a2950f748445
permissions -rw-r--r--
reimplemented using Equiv_Relations.thy
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
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
     9
imports Main
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
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    13
subsection {* Rational numbers *}
14365
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 {* Equivalence of fractions *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    16
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    17
constdefs
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    18
  fraction :: "(int \<times> int) set"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    19
   "fraction \<equiv> {x. snd x \<noteq> 0}"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    20
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    21
  ratrel :: "((int \<times> int) \<times> (int \<times> int)) set"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    22
   "ratrel \<equiv> {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    23
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    24
lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    25
by (simp add: fraction_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    26
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    27
lemma ratrel_iff [simp]:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    28
  "((x,y) \<in> ratrel) =
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    29
   (snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    30
by (simp add: ratrel_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    31
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    32
lemma refl_ratrel: "refl fraction ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    33
by (auto simp add: refl_def fraction_def ratrel_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    34
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    35
lemma sym_ratrel: "sym ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    36
by (simp add: ratrel_def sym_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    37
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    38
lemma trans_ratrel_lemma:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    39
  assumes 1: "a * b' = a' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    40
  assumes 2: "a' * b'' = a'' * b'"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    41
  assumes 3: "b' \<noteq> (0::int)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    42
  shows "a * b'' = a'' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    43
proof -
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    44
  have "b' * (a * b'') = b'' * (a * b')" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    45
  also note 1
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    46
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    47
  also note 2
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    48
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    49
  finally have "b' * (a * b'') = b' * (a'' * b)" .
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    50
  with 3 show "a * b'' = a'' * b" by simp
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    51
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    52
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    53
lemma trans_ratrel: "trans ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    54
by (auto simp add: trans_def elim: trans_ratrel_lemma)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    55
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    56
lemma equiv_ratrel: "equiv fraction ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    57
by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    58
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    59
lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    60
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    61
lemma equiv_ratrel_iff2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    62
  "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    63
    \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    64
by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    65
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    66
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    67
subsubsection {* The type of rational numbers *}
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    68
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    69
typedef (Rat) rat = "fraction//ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    70
proof
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    71
  have "(0,1) \<in> fraction" by (simp add: fraction_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    72
  thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    73
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    74
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    75
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    76
by (simp add: Rat_def quotientI)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    77
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    78
declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    79
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    80
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    81
constdefs
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    82
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    83
  "Fract a b \<equiv> Abs_Rat (ratrel``{(a,b)})"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    84
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    85
theorem Rat_cases [case_names Fract, cases type: rat]:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    86
  "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    87
by (cases q, clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    88
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    89
theorem Rat_induct [case_names Fract, induct type: rat]:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    90
    "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    91
  by (cases q) simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    92
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    93
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    94
subsubsection {* Congruence lemmas *}
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    95
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    96
lemma add_congruent2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    97
     "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    98
      respects2 ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    99
apply (rule equiv_ratrel [THEN congruent2_commuteI])
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   100
apply (simp_all add: left_distrib)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   101
done
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   102
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   103
lemma minus_congruent:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   104
  "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   105
by (simp add: congruent_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   106
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   107
lemma mult_congruent2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   108
  "(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   109
by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   110
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   111
lemma inverse_congruent:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   112
  "(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   113
by (auto simp add: congruent_def mult_commute)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   114
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   115
lemma le_congruent2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   116
  "(\<lambda>x y. (fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y))
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   117
   respects2 ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   118
proof (clarsimp simp add: congruent2_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   119
  fix a b a' b' c d c' d'::int
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   120
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   121
  assume eq1: "a * b' = a' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   122
  assume eq2: "c * d' = c' * d"
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   123
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   124
  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
   125
  {
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   126
    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
   127
    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
   128
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   129
      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
   130
      hence "?le a b c d =
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   131
          ((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
   132
        by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   133
      also have "... = ?le (a * x) (b * x) c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   134
        by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   135
      finally show ?thesis .
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   136
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   137
  } note le_factor = this
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   138
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   139
  let ?D = "b * d" and ?D' = "b' * d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   140
  from neq have D: "?D \<noteq> 0" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   141
  from neq have "?D' \<noteq> 0" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   142
  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
   143
    by (rule le_factor)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   144
  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
   145
    by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   146
  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
   147
    by (simp only: eq1 eq2)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   148
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   149
    by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   150
  also from D have "... = ?le a' b' c' d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   151
    by (rule le_factor [symmetric])
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   152
  finally show "?le a b c d = ?le a' b' c' d'" .
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   153
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   154
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   155
lemma All_equiv_class:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   156
  "equiv A r ==> f respects r ==> a \<in> A
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   157
    ==> (\<forall>x \<in> r``{a}. f x) = f a"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   158
apply safe
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   159
apply (drule (1) equiv_class_self)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   160
apply simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   161
apply (drule (1) congruent.congruent)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   162
apply simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   163
done
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   164
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   165
lemma congruent2_implies_congruent_All:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   166
  "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a \<in> A2 ==>
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   167
    congruent r1 (\<lambda>x1. \<forall>x2 \<in> r2``{a}. f x1 x2)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   168
  apply (unfold congruent_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   169
  apply clarify
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   170
  apply (rule equiv_type [THEN subsetD, THEN SigmaE2], assumption+)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   171
  apply (simp add: UN_equiv_class congruent2_implies_congruent)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   172
  apply (unfold congruent2_def equiv_def refl_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   173
  apply (blast del: equalityI)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   174
  done
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   175
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   176
lemma All_equiv_class2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   177
  "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a1 \<in> A1 ==> a2 \<in> A2
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   178
    ==> (\<forall>x1 \<in> r1``{a1}. \<forall>x2 \<in> r2``{a2}. f x1 x2) = f a1 a2"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   179
  by (simp add: All_equiv_class congruent2_implies_congruent
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   180
    congruent2_implies_congruent_All)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   181
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   182
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   183
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   184
lemmas All_ratrel2 = All_equiv_class2 [OF equiv_ratrel equiv_ratrel]
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   185
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   186
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   187
subsubsection {* Standard operations on rational numbers *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   188
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   189
instance rat :: "{ord, zero, one, plus, times, minus, inverse}" ..
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   190
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   191
defs (overloaded)
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   192
  Zero_rat_def:  "0 == Fract 0 1"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   193
  One_rat_def:   "1 == Fract 1 1"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   194
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   195
  add_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   196
   "q + r ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   197
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   198
           ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   199
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   200
  minus_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   201
    "- q == Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   202
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   203
  diff_rat_def:  "q - r == q + - (r::rat)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   204
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   205
  mult_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   206
   "q * r ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   207
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   208
           ratrel``{(fst x * fst y, snd x * snd y)})"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   209
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   210
  inverse_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   211
    "inverse q ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   212
        Abs_Rat (\<Union>x \<in> Rep_Rat q.
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   213
            ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   214
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   215
  divide_rat_def:  "q / r == q * inverse (r::rat)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   216
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   217
  le_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   218
   "q \<le> (r::rat) ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   219
    \<forall>x \<in> Rep_Rat q. \<forall>y \<in> Rep_Rat r.
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   220
        (fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   221
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   222
  less_rat_def: "(z < (w::rat)) == (z \<le> w & z \<noteq> w)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   223
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   224
  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
   225
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   226
lemma zero_rat: "0 = Fract 0 1"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   227
by (simp add: Zero_rat_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   228
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   229
lemma one_rat: "1 = Fract 1 1"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   230
by (simp add: One_rat_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   231
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   232
theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   233
  (Fract a b = Fract c d) = (a * d = c * b)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   234
by (simp add: Fract_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   235
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   236
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   237
  Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   238
by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   239
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   240
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   241
by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   242
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   243
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   244
    Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   245
by (simp add: diff_rat_def add_rat minus_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   246
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   247
theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   248
  Fract a b * Fract c d = Fract (a * c) (b * d)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   249
by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   250
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   251
theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==>
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   252
  inverse (Fract a b) = Fract b a"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   253
by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   254
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   255
theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   256
  Fract a b / Fract c d = Fract (a * d) (b * c)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   257
by (simp add: divide_rat_def inverse_rat mult_rat)
14365
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
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   260
  (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   261
by (simp add: Fract_def le_rat_def le_congruent2 All_ratrel2)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   262
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   263
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   264
    (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   265
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
   266
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   267
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
   268
  by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat)
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   269
     (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
   270
                split: abs_split)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   271
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   272
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   273
subsubsection {* The ordered field of rational numbers *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   274
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   275
lemma rat_add_assoc: "(q + r) + s = q + (r + (s::rat))"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   276
  by (induct q, induct r, induct s)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   277
     (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
   278
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   279
lemma rat_add_0: "0 + q = (q::rat)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   280
  by (induct q) (simp add: zero_rat add_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   281
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   282
lemma rat_left_minus: "(-q) + q = (0::rat)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   283
  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
   284
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   285
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   286
instance rat :: field
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
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   289
  show "(q + r) + s = q + (r + s)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   290
    by (induct q, induct r, induct s)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   291
       (simp add: add_rat add_ac mult_ac int_distrib)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   292
  show "q + r = r + q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   293
    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
   294
  show "0 + q = q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   295
    by (induct q) (simp add: zero_rat add_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   296
  show "(-q) + q = 0"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   297
    by (induct q) (simp add: zero_rat minus_rat add_rat eq_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   298
  show "q - r = q + (-r)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   299
    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
   300
  show "(q * r) * s = q * (r * s)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   301
    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
   302
  show "q * r = r * q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   303
    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
   304
  show "1 * q = q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   305
    by (induct q) (simp add: one_rat mult_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   306
  show "(q + r) * s = q * s + r * s"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   307
    by (induct q, induct r, induct s)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   308
       (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
   309
  show "q \<noteq> 0 ==> inverse q * q = 1"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   310
    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
   311
  show "q / r = q * inverse r"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   312
    by (simp add: divide_rat_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   313
  show "0 \<noteq> (1::rat)"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   314
    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
   315
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   316
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   317
instance rat :: linorder
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
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   320
  {
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   321
    assume "q \<le> r" and "r \<le> s"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   322
    show "q \<le> s"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   323
    proof (insert prems, induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   324
      fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   325
      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
   326
      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
   327
      show "Fract a b \<le> Fract e f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   328
      proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   329
        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
   330
          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
   331
        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
   332
        proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   333
          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
   334
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   335
          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
   336
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   337
        also have "... = (c * f) * (d * f) * (b * b)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   338
          by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   339
        also have "... \<le> (e * d) * (d * f) * (b * b)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   340
        proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   341
          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
   342
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   343
          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
   344
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   345
        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
   346
          by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   347
        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
   348
          by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   349
        with neq show ?thesis by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   350
      qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   351
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   352
  next
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   353
    assume "q \<le> r" and "r \<le> q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   354
    show "q = r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   355
    proof (insert prems, induct q, induct r)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   356
      fix a b c d :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   357
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   358
      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
   359
      show "Fract a b = Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   360
      proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   361
        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
   362
          by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   363
        also have "... \<le> (a * d) * (b * d)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   364
        proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   365
          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
   366
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   367
          thus ?thesis by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   368
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   369
        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
   370
        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
   371
        ultimately have "a * d = c * b" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   372
        with neq show ?thesis by (simp add: eq_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   373
      qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   374
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   375
  next
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   376
    show "q \<le> q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   377
      by (induct q) (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   378
    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
   379
      by (simp only: less_rat_def)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   380
    show "q \<le> r \<or> r \<le> q"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   381
      by (induct q, induct r)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   382
         (simp add: le_rat mult_commute, rule linorder_linear)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   383
  }
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   384
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   385
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   386
instance rat :: ordered_field
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   387
proof
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   388
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   389
  show "q \<le> r ==> s + q \<le> s + r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   390
  proof (induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   391
    fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   392
    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
   393
    assume le: "Fract a b \<le> Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   394
    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
   395
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   396
      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
   397
        by (auto simp add: zero_less_mult_iff)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   398
      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
   399
        by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   400
      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
   401
        by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   402
      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
   403
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   404
  qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   405
  show "q < r ==> 0 < s ==> s * q < s * r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   406
  proof (induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   407
    fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   408
    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
   409
    assume le: "Fract a b < Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   410
    assume gt: "0 < Fract e f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   411
    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
   412
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   413
      let ?E = "e * f" and ?F = "f * f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   414
      from neq gt have "0 < ?E"
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   415
        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
   416
      moreover from neq have "0 < ?F"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   417
        by (auto simp add: zero_less_mult_iff)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   418
      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
   419
        by (simp add: less_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   420
      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
   421
        by (simp add: mult_less_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   422
      with neq show ?thesis
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   423
        by (simp add: less_rat mult_rat mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   424
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   425
  qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   426
  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
   427
    by (simp only: abs_rat_def)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   428
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   429
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   430
instance rat :: division_by_zero
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   431
proof
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   432
  show "inverse 0 = (0::rat)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   433
    by (simp add: zero_rat Fract_def inverse_rat_def
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   434
                  inverse_congruent UN_ratrel)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   435
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   436
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
subsection {* Various Other Results *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   439
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   440
lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   441
by (simp add: eq_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   442
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   443
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
   444
  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
   445
    shows "P q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   446
proof (cases q)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   447
  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
   448
  proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   449
    fix a::int and b::int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   450
    assume b: "b < 0"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   451
    hence "0 < -b" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   452
    hence "P (Fract (-a) (-b))" by (rule step)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   453
    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
   454
  qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   455
  case (Fract a b)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   456
  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
   457
qed
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
lemma zero_less_Fract_iff:
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   460
     "0 < b ==> (0 < Fract a b) = (0 < a)"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   461
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
   462
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   463
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
   464
apply (insert add_rat [of concl: m n 1 1])
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   465
apply (simp add: one_rat  [symmetric])
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   466
done
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   467
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   468
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   469
apply (induct k)
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   470
apply (simp add: zero_rat)
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   471
apply (simp add: Fract_add_one)
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   472
done
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   473
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   474
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   475
proof (cases k rule: int_cases)
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   476
  case (nonneg n)
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   477
    thus ?thesis by (simp add: int_eq_of_nat Fract_of_nat_eq)
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   478
next
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   479
  case (neg n)
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   480
    hence "Fract k 1 = - (Fract (of_nat (Suc n)) 1)"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   481
      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
   482
    also have "... =  - (of_nat (Suc n))"
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   483
      by (simp only: Fract_of_nat_eq)
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   484
    finally show ?thesis
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   485
      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
   486
qed
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   487
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   488
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   489
subsection {* Numerals and Arithmetic *}
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   490
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   491
instance rat :: number ..
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   492
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14981
diff changeset
   493
defs (overloaded)
34264f5e4691 new treatment of binary numerals
paulson
parents: 14981
diff changeset
   494
  rat_number_of_def: "(number_of w :: rat) == of_int (Rep_Bin w)"
34264f5e4691 new treatment of binary numerals
paulson
parents: 14981
diff changeset
   495
    --{*the type constraint is essential!*}
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   496
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   497
instance rat :: number_ring
15013
34264f5e4691 new treatment of binary numerals
paulson
parents: 14981
diff changeset
   498
by (intro_classes, simp add: rat_number_of_def) 
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   499
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   500
declare diff_rat_def [symmetric]
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   501
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   502
use "rat_arith.ML"
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   503
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   504
setup rat_arith_setup
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   505
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   506
end