src/HOL/Real/Rational.thy
author berghofe
Thu, 06 Sep 2007 11:38:10 +0200
changeset 24533 fe1f93f6a15a
parent 24506 020db6ec334a
child 24622 8116eb022282
permissions -rw-r--r--
Added code generator setup (taken from Library/Executable_Rat.thy, also works for old code generator).
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
24533
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
     9
imports Abstract_Rat
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
19765
dfe940911617 misc cleanup;
wenzelm
parents: 18983
diff changeset
    17
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    18
  fraction :: "(int \<times> int) set" where
19765
dfe940911617 misc cleanup;
wenzelm
parents: 18983
diff changeset
    19
  "fraction = {x. snd x \<noteq> 0}"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    20
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    21
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    22
  ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
19765
dfe940911617 misc cleanup;
wenzelm
parents: 18983
diff changeset
    23
  "ratrel = {(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
    24
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    25
lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    26
by (simp add: fraction_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    27
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    28
lemma ratrel_iff [simp]:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    29
  "((x,y) \<in> ratrel) =
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    30
   (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
    31
by (simp add: ratrel_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    32
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    33
lemma refl_ratrel: "refl fraction ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    34
by (auto simp add: refl_def fraction_def ratrel_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    35
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    36
lemma sym_ratrel: "sym ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    37
by (simp add: ratrel_def sym_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    38
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    39
lemma trans_ratrel_lemma:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    40
  assumes 1: "a * b' = a' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    41
  assumes 2: "a' * b'' = a'' * b'"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    42
  assumes 3: "b' \<noteq> (0::int)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    43
  shows "a * b'' = a'' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    44
proof -
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    45
  have "b' * (a * b'') = b'' * (a * b')" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    46
  also note 1
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    47
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    48
  also note 2
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    49
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    50
  finally have "b' * (a * b'') = b' * (a'' * b)" .
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    51
  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
    52
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    53
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    54
lemma trans_ratrel: "trans ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    55
by (auto simp add: trans_def elim: trans_ratrel_lemma)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    56
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    57
lemma equiv_ratrel: "equiv fraction ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    58
by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    59
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    60
lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    61
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    62
lemma equiv_ratrel_iff2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    63
  "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    64
    \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    65
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
    66
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    67
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    68
subsubsection {* The type of rational numbers *}
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    69
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    70
typedef (Rat) rat = "fraction//ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    71
proof
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    72
  have "(0,1) \<in> fraction" by (simp add: fraction_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    73
  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
    74
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
    75
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    76
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
    77
by (simp add: Rat_def quotientI)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    78
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    79
declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    80
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    81
19765
dfe940911617 misc cleanup;
wenzelm
parents: 18983
diff changeset
    82
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    83
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
    84
  [code func del]: "Fract a b = Abs_Rat (ratrel``{(a,b)})"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
    85
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
    86
lemma Fract_zero:
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
    87
  "Fract k 0 = Fract l 0"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
    88
  by (simp add: Fract_def ratrel_def)
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    89
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    90
theorem Rat_cases [case_names Fract, cases type: rat]:
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    91
    "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20522
diff changeset
    92
  by (cases q) (clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    93
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    94
theorem Rat_induct [case_names Fract, induct type: rat]:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    95
    "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    96
  by (cases q) simp
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    97
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    98
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
    99
subsubsection {* Congruence lemmas *}
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   100
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   101
lemma add_congruent2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   102
     "(\<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
   103
      respects2 ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   104
apply (rule equiv_ratrel [THEN congruent2_commuteI])
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   105
apply (simp_all add: left_distrib)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   106
done
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   107
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   108
lemma minus_congruent:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   109
  "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   110
by (simp add: congruent_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   111
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   112
lemma mult_congruent2:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   113
  "(\<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
   114
by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   115
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   116
lemma inverse_congruent:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   117
  "(\<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
   118
by (auto simp add: congruent_def mult_commute)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   119
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   120
lemma le_congruent2:
18982
a2950f748445 no longer need All_equiv lemmas
huffman
parents: 18913
diff changeset
   121
  "(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   122
   respects2 ratrel"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   123
proof (clarsimp simp add: congruent2_def)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   124
  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
   125
  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
   126
  assume eq1: "a * b' = a' * b"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   127
  assume eq2: "c * d' = c' * d"
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   128
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   129
  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
   130
  {
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   131
    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
   132
    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
   133
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   134
      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
   135
      hence "?le a b c d =
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   136
          ((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
   137
        by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   138
      also have "... = ?le (a * x) (b * x) c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   139
        by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   140
      finally show ?thesis .
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   141
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   142
  } note le_factor = this
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   143
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   144
  let ?D = "b * d" and ?D' = "b' * d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   145
  from neq have D: "?D \<noteq> 0" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   146
  from neq have "?D' \<noteq> 0" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   147
  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
   148
    by (rule le_factor)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   149
  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
   150
    by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   151
  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
   152
    by (simp only: eq1 eq2)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   153
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   154
    by (simp add: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   155
  also from D have "... = ?le a' b' c' d'"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   156
    by (rule le_factor [symmetric])
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   157
  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
   158
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   159
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   160
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   161
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   162
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   163
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   164
subsubsection {* Standard operations on rational numbers *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   165
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   166
instance rat :: zero
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   167
  Zero_rat_def: "0 == Fract 0 1" ..
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   168
lemmas [code func del] = Zero_rat_def
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   169
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   170
instance rat :: one
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   171
  One_rat_def: "1 == Fract 1 1" ..
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   172
lemmas [code func del] = One_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   173
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   174
instance rat :: plus
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   175
  add_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   176
   "q + r ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   177
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   178
           ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})" ..
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   179
lemmas [code func del] = add_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   180
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   181
instance rat :: minus
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   182
  minus_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   183
    "- q == Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   184
  diff_rat_def:  "q - r == q + - (r::rat)" ..
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   185
lemmas [code func del] = minus_rat_def diff_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   186
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   187
instance rat :: times
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   188
  mult_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   189
   "q * r ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   190
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   191
           ratrel``{(fst x * fst y, snd x * snd y)})" ..
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   192
lemmas [code func del] = mult_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   193
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   194
instance rat :: inverse
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   195
  inverse_rat_def:
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   196
    "inverse q ==
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   197
        Abs_Rat (\<Union>x \<in> Rep_Rat q.
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   198
            ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   199
  divide_rat_def:  "q / r == q * inverse (r::rat)" ..
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   200
lemmas [code func del] = inverse_rat_def divide_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   201
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   202
instance rat :: ord
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   203
  le_rat_def:
18982
a2950f748445 no longer need All_equiv lemmas
huffman
parents: 18913
diff changeset
   204
   "q \<le> r == contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
a2950f748445 no longer need All_equiv lemmas
huffman
parents: 18913
diff changeset
   205
      {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   206
  less_rat_def: "(z < (w::rat)) == (z \<le> w & z \<noteq> w)" ..
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   207
lemmas [code func del] = le_rat_def less_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   208
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   209
instance rat :: abs
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   210
  abs_rat_def: "\<bar>q\<bar> == if q < 0 then -q else (q::rat)" ..
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   211
24506
020db6ec334a final(?) iteration of sgn saga.
nipkow
parents: 24198
diff changeset
   212
instance rat :: sgn
020db6ec334a final(?) iteration of sgn saga.
nipkow
parents: 24198
diff changeset
   213
  sgn_rat_def: "sgn(q::rat) == (if q=0 then 0 else if 0<q then 1 else - 1)" ..
020db6ec334a final(?) iteration of sgn saga.
nipkow
parents: 24198
diff changeset
   214
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   215
instance rat :: power ..
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   216
20522
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   217
primrec (rat)
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   218
  rat_power_0:   "q ^ 0       = 1"
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   219
  rat_power_Suc: "q ^ (Suc n) = (q::rat) * (q ^ n)"
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   220
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   221
theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   222
  (Fract a b = Fract c d) = (a * d = c * b)"
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   223
by (simp add: Fract_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   224
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   225
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   226
  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
   227
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
   228
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   229
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   230
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
   231
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   232
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   233
    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
   234
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
   235
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   236
theorem mult_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 * c) (b * d)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   238
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
   239
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   240
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
   241
  inverse (Fract a b) = Fract b a"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   242
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
   243
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   244
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
   245
  Fract a b / Fract c d = Fract (a * d) (b * c)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   246
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
   247
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   248
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   249
  (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
18982
a2950f748445 no longer need All_equiv lemmas
huffman
parents: 18913
diff changeset
   250
by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   251
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   252
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   253
    (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
   254
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
   255
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   256
theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   257
  by (simp add: abs_rat_def minus_rat Zero_rat_def less_rat eq_rat)
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   258
     (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
   259
                split: abs_split)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   260
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   261
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   262
subsubsection {* The ordered field of rational numbers *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   263
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   264
instance rat :: field
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   265
proof
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   266
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   267
  show "(q + r) + s = q + (r + s)"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   268
    by (induct q, induct r, induct s)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   269
       (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
   270
  show "q + r = r + q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   271
    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
   272
  show "0 + q = q"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   273
    by (induct q) (simp add: Zero_rat_def add_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   274
  show "(-q) + q = 0"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   275
    by (induct q) (simp add: Zero_rat_def minus_rat add_rat eq_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   276
  show "q - r = q + (-r)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   277
    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
   278
  show "(q * r) * s = q * (r * s)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   279
    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
   280
  show "q * r = r * q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   281
    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
   282
  show "1 * q = q"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   283
    by (induct q) (simp add: One_rat_def mult_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   284
  show "(q + r) * s = q * s + r * s"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   285
    by (induct q, induct r, induct s)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   286
       (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
   287
  show "q \<noteq> 0 ==> inverse q * q = 1"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   288
    by (induct q) (simp add: inverse_rat mult_rat One_rat_def Zero_rat_def eq_rat)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14421
diff changeset
   289
  show "q / r = q * inverse r"
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   290
    by (simp add: divide_rat_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   291
  show "0 \<noteq> (1::rat)"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   292
    by (simp add: Zero_rat_def One_rat_def eq_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   293
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   294
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   295
instance rat :: linorder
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   296
proof
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   297
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   298
  {
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   299
    assume "q \<le> r" and "r \<le> s"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   300
    show "q \<le> s"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   301
    proof (insert prems, induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   302
      fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   303
      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
   304
      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
   305
      show "Fract a b \<le> Fract e f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   306
      proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   307
        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
   308
          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
   309
        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
   310
        proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   311
          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
   312
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   313
          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
   314
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   315
        also have "... = (c * f) * (d * f) * (b * b)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   316
          by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   317
        also have "... \<le> (e * d) * (d * f) * (b * b)"
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
          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
   320
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   321
          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
   322
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   323
        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
   324
          by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   325
        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
   326
          by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   327
        with neq show ?thesis by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   328
      qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   329
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   330
  next
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   331
    assume "q \<le> r" and "r \<le> q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   332
    show "q = r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   333
    proof (insert prems, induct q, induct r)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   334
      fix a b c d :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   335
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   336
      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
   337
      show "Fract a b = Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   338
      proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   339
        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
   340
          by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   341
        also have "... \<le> (a * d) * (b * d)"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   342
        proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   343
          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
   344
            by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   345
          thus ?thesis by (simp only: mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   346
        qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   347
        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
   348
        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
   349
        ultimately have "a * d = c * b" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   350
        with neq show ?thesis by (simp add: eq_rat)
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
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   353
  next
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   354
    show "q \<le> q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   355
      by (induct q) (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   356
    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
   357
      by (simp only: less_rat_def)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   358
    show "q \<le> r \<or> r \<le> q"
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   359
      by (induct q, induct r)
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   360
         (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
   361
  }
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   362
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   363
22456
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   364
instance rat :: distrib_lattice
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   365
  "inf r s \<equiv> min r s"
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   366
  "sup r s \<equiv> max r s"
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   367
  by default (auto simp add: min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   368
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   369
instance rat :: ordered_field
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
  fix q r s :: rat
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   372
  show "q \<le> r ==> s + q \<le> s + r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   373
  proof (induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   374
    fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   375
    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
   376
    assume le: "Fract a b \<le> Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   377
    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
   378
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   379
      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
   380
        by (auto simp add: zero_less_mult_iff)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   381
      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
   382
        by (simp add: le_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   383
      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
   384
        by (simp add: mult_le_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   385
      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
   386
    qed
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
  show "q < r ==> 0 < s ==> s * q < s * r"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   389
  proof (induct q, induct r, induct s)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   390
    fix a b c d e f :: int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   391
    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
   392
    assume le: "Fract a b < Fract c d"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   393
    assume gt: "0 < Fract e f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   394
    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
   395
    proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   396
      let ?E = "e * f" and ?F = "f * f"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   397
      from neq gt have "0 < ?E"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   398
        by (auto simp add: Zero_rat_def 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
   399
      moreover from neq have "0 < ?F"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   400
        by (auto simp add: zero_less_mult_iff)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   401
      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
   402
        by (simp add: less_rat)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   403
      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
   404
        by (simp add: mult_less_cancel_right)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   405
      with neq show ?thesis
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   406
        by (simp add: less_rat mult_rat mult_ac)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   407
    qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   408
  qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   409
  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
   410
    by (simp only: abs_rat_def)
24506
020db6ec334a final(?) iteration of sgn saga.
nipkow
parents: 24198
diff changeset
   411
qed (auto simp: sgn_rat_def)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   412
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   413
instance rat :: division_by_zero
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   414
proof
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   415
  show "inverse 0 = (0::rat)"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   416
    by (simp add: Zero_rat_def Fract_def inverse_rat_def
18913
57f19fad8c2a reimplemented using Equiv_Relations.thy
huffman
parents: 18372
diff changeset
   417
                  inverse_congruent UN_ratrel)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   418
qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   419
20522
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   420
instance rat :: recpower
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   421
proof
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   422
  fix q :: rat
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   423
  fix n :: nat
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   424
  show "q ^ 0 = 1" by simp
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   425
  show "q ^ (Suc n) = q * (q ^ n)" by simp
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   426
qed
05072ae0d435 added instance rat :: recpower
huffman
parents: 20485
diff changeset
   427
14365
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
subsection {* Various Other Results *}
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   430
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   431
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
   432
by (simp add: eq_rat)
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   433
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   434
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
   435
  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
   436
    shows "P q"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   437
proof (cases q)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   438
  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
   439
  proof -
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   440
    fix a::int and b::int
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   441
    assume b: "b < 0"
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   442
    hence "0 < -b" by simp
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   443
    hence "P (Fract (-a) (-b))" by (rule step)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   444
    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
   445
  qed
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   446
  case (Fract a b)
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   447
  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
   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
lemma zero_less_Fract_iff:
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   451
     "0 < b ==> (0 < Fract a b) = (0 < a)"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   452
by (simp add: Zero_rat_def 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
   453
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   454
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
   455
apply (insert add_rat [of concl: m n 1 1])
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   456
apply (simp add: One_rat_def [symmetric])
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   457
done
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   458
23429
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   459
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   460
by (induct k) (simp_all add: Zero_rat_def One_rat_def add_rat)
23429
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   461
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   462
lemma of_int_rat: "of_int k = Fract k 1"
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   463
by (cases k rule: int_diff_cases, simp add: of_nat_rat diff_rat)
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   464
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   465
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
23429
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   466
by (rule of_nat_rat [symmetric])
14378
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_int_eq: "Fract k 1 = of_int k"
23429
5a55a9409e57 simplify some proofs
huffman
parents: 23365
diff changeset
   469
by (rule of_int_rat [symmetric])
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   470
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   471
lemma Fract_of_int_quotient: "Fract k l = (if l = 0 then Fract 1 0 else of_int k / of_int l)"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   472
by (auto simp add: Fract_zero Fract_of_int_eq [symmetric] divide_rat)
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   473
14378
69c4d5997669 generic of_nat and of_int functions, and generalization of iszero
paulson
parents: 14365
diff changeset
   474
14691
e1eedc8cad37 tuned instance statements;
wenzelm
parents: 14430
diff changeset
   475
subsection {* Numerals and Arithmetic *}
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   476
22456
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   477
instance rat :: number
6070e48ecb78 added lattice definitions
haftmann
parents: 21404
diff changeset
   478
  rat_number_of_def: "(number_of w :: rat) \<equiv> of_int w" ..
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   479
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   480
instance rat :: number_ring
19765
dfe940911617 misc cleanup;
wenzelm
parents: 18983
diff changeset
   481
  by default (simp add: rat_number_of_def) 
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   482
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   483
use "rat_arith.ML"
24075
366d4d234814 arith method setup: proper context;
wenzelm
parents: 23879
diff changeset
   484
declaration {* K rat_arith_setup *}
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 14378
diff changeset
   485
23342
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   486
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   487
subsection {* Embedding from Rationals to other Fields *}
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   488
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   489
class field_char_0 = field + ring_char_0
23342
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   490
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   491
instance ordered_field < field_char_0 ..
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   492
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   493
definition
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   494
  of_rat :: "rat \<Rightarrow> 'a::field_char_0"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   495
where
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   496
  [code func del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
23342
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   497
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   498
lemma of_rat_congruent:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   499
  "(\<lambda>(a, b). {of_int a / of_int b::'a::field_char_0}) respects ratrel"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   500
apply (rule congruent.intro)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   501
apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   502
apply (simp only: of_int_mult [symmetric])
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   503
done
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   504
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   505
lemma of_rat_rat:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   506
  "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   507
unfolding Fract_def of_rat_def
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   508
by (simp add: UN_ratrel of_rat_congruent)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   509
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   510
lemma of_rat_0 [simp]: "of_rat 0 = 0"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   511
by (simp add: Zero_rat_def of_rat_rat)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   512
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   513
lemma of_rat_1 [simp]: "of_rat 1 = 1"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   514
by (simp add: One_rat_def of_rat_rat)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   515
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   516
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   517
by (induct a, induct b, simp add: add_rat of_rat_rat add_frac_eq)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   518
23343
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   519
lemma of_rat_minus: "of_rat (- a) = - of_rat a"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   520
by (induct a, simp add: minus_rat of_rat_rat)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   521
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   522
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   523
by (simp only: diff_minus of_rat_add of_rat_minus)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   524
23342
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   525
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   526
apply (induct a, induct b, simp add: mult_rat of_rat_rat)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   527
apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   528
done
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   529
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   530
lemma nonzero_of_rat_inverse:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   531
  "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
23343
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   532
apply (rule inverse_unique [symmetric])
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   533
apply (simp add: of_rat_mult [symmetric])
23342
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   534
done
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   535
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   536
lemma of_rat_inverse:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   537
  "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   538
   inverse (of_rat a)"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   539
by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   540
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   541
lemma nonzero_of_rat_divide:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   542
  "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   543
by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   544
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   545
lemma of_rat_divide:
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   546
  "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   547
   = of_rat a / of_rat b"
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   548
by (cases "b = 0", simp_all add: nonzero_of_rat_divide)
0261d2da0b1c add function of_rat and related lemmas
huffman
parents: 22456
diff changeset
   549
23343
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   550
lemma of_rat_power:
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   551
  "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   552
by (induct n) (simp_all add: of_rat_mult power_Suc)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   553
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   554
lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   555
apply (induct a, induct b)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   556
apply (simp add: of_rat_rat eq_rat)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   557
apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   558
apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   559
done
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   560
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   561
lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   562
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   563
lemma of_rat_eq_id [simp]: "of_rat = (id :: rat \<Rightarrow> rat)"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   564
proof
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   565
  fix a
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   566
  show "of_rat a = id a"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   567
  by (induct a)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   568
     (simp add: of_rat_rat divide_rat Fract_of_int_eq [symmetric])
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   569
qed
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   570
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   571
text{*Collapse nested embeddings*}
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   572
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   573
by (induct n) (simp_all add: of_rat_add)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   574
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   575
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23343
diff changeset
   576
by (cases z rule: int_diff_cases, simp add: of_rat_diff)
23343
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   577
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   578
lemma of_rat_number_of_eq [simp]:
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   579
  "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   580
by (simp add: number_of_eq)
6a83ca5fe282 more of_rat lemmas
huffman
parents: 23342
diff changeset
   581
23879
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   582
lemmas zero_rat = Zero_rat_def
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   583
lemmas one_rat = One_rat_def
4776af8be741 split class abs from class minus
haftmann
parents: 23429
diff changeset
   584
24198
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   585
abbreviation
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   586
  rat_of_nat :: "nat \<Rightarrow> rat"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   587
where
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   588
  "rat_of_nat \<equiv> of_nat"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   589
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   590
abbreviation
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   591
  rat_of_int :: "int \<Rightarrow> rat"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   592
where
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   593
  "rat_of_int \<equiv> of_int"
4031da6d8ba3 adaptions for code generation
haftmann
parents: 24075
diff changeset
   594
24533
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   595
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   596
subsection {* Implementation of rational numbers as pairs of integers *}
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   597
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   598
definition
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   599
  RatC :: "int \<times> int \<Rightarrow> rat"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   600
where
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   601
  "RatC = INum"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   602
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   603
code_datatype RatC
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   604
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   605
lemma RatC_simp:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   606
  "RatC (k, l) = rat_of_int k / rat_of_int l"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   607
  unfolding RatC_def INum_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   608
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   609
lemma RatC_zero [simp]: "RatC 0\<^sub>N = 0"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   610
  by (simp add: RatC_simp)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   611
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   612
lemma RatC_lit [simp]: "RatC i\<^sub>N = rat_of_int i"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   613
  by (simp add: RatC_simp)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   614
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   615
lemma zero_rat_code [code, code unfold]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   616
  "0 = RatC 0\<^sub>N" by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   617
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   618
lemma zero_rat_code [code, code unfold]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   619
  "1 = RatC 1\<^sub>N" by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   620
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   621
lemma [code, code unfold]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   622
  "number_of k = rat_of_int (number_of k)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   623
  by (simp add: number_of_is_id rat_number_of_def)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   624
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   625
definition
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   626
  [code func del]: "Fract' (b\<Colon>bool) k l = Fract k l"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   627
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   628
lemma [code]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   629
  "Fract k l = Fract' (l \<noteq> 0) k l"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   630
  unfolding Fract'_def ..
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   631
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   632
lemma [code]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   633
  "Fract' True k l = (if l \<noteq> 0 then RatC (k, l) else Fract 1 0)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   634
  by (simp add: Fract'_def RatC_simp Fract_of_int_quotient [of k l])
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   635
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   636
lemma [code]:
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   637
  "of_rat (RatC (k, l)) = (if l \<noteq> 0 then of_int k / of_int l else 0)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   638
  by (cases "l = 0")
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   639
    (auto simp add: RatC_simp of_rat_rat [simplified Fract_of_int_quotient [of k l], symmetric])
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   640
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   641
instance rat :: eq ..
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   642
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   643
lemma rat_eq_code [code]: "RatC x = RatC y \<longleftrightarrow> normNum x = normNum y"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   644
  unfolding RatC_def INum_normNum_iff ..
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   645
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   646
lemma rat_less_eq_code [code]: "RatC x \<le> RatC y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   647
proof -
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   648
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> RatC (normNum x) \<le> RatC (normNum y)" 
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   649
    by (simp add: RatC_def del: normNum)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   650
  also have "\<dots> = (RatC x \<le> RatC y)" by (simp add: RatC_def)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   651
  finally show ?thesis by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   652
qed
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   653
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   654
lemma rat_less_code [code]: "RatC x < RatC y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   655
proof -
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   656
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> RatC (normNum x) < RatC (normNum y)" 
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   657
    by (simp add: RatC_def del: normNum)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   658
  also have "\<dots> = (RatC x < RatC y)" by (simp add: RatC_def)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   659
  finally show ?thesis by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   660
qed
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   661
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   662
lemma rat_add_code [code]: "RatC x + RatC y = RatC (x +\<^sub>N y)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   663
  unfolding RatC_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   664
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   665
lemma rat_mul_code [code]: "RatC x * RatC y = RatC (x *\<^sub>N y)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   666
  unfolding RatC_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   667
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   668
lemma rat_neg_code [code]: "- RatC x = RatC (~\<^sub>N x)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   669
  unfolding RatC_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   670
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   671
lemma rat_sub_code [code]: "RatC x - RatC y = RatC (x -\<^sub>N y)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   672
  unfolding RatC_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   673
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   674
lemma rat_inv_code [code]: "inverse (RatC x) = RatC (Ninv x)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   675
  unfolding RatC_def Ninv divide_rat_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   676
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   677
lemma rat_div_code [code]: "RatC x / RatC y = RatC (x \<div>\<^sub>N y)"
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   678
  unfolding RatC_def by simp
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   679
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   680
text {* Setup for old code generator *}
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   681
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   682
types_code
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   683
  rat ("(int */ int)")
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   684
attach (term_of) {*
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   685
fun term_of_rat (p, q) =
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   686
  let val rT = Type ("Rational.rat", [])
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   687
  in
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   688
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   689
    else Const ("HOL.inverse_class.divide", [rT, rT] ---> rT) $
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   690
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   691
  end;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   692
*}
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   693
attach (test) {*
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   694
fun gen_rat i =
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   695
  let
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   696
    val p = random_range 0 i;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   697
    val q = random_range 1 (i + 1);
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   698
    val g = Integer.gcd p q;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   699
    val p' = Integer.div p g;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   700
    val q' = Integer.div q g;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   701
  in
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   702
    (if one_of [true, false] then p' else ~ p',
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   703
     if p' = 0 then 0 else q')
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   704
  end;
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   705
*}
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   706
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   707
consts_code
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   708
  RatC ("(_)")
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   709
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   710
consts_code
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   711
  "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   712
attach {*
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   713
fun rat_of_int 0 = (0, 0)
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   714
  | rat_of_int i = (i, 1);
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   715
*}
fe1f93f6a15a Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents: 24506
diff changeset
   716
14365
3d4df8c166ae replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff changeset
   717
end