src/HOL/Real/Rational.thy
author haftmann
Fri Jul 25 12:03:34 2008 +0200 (2008-07-25)
changeset 27682 25aceefd4786
parent 27668 6eb20b2cecf8
child 28001 4642317e0deb
permissions -rw-r--r--
added class preorder
haftmann@27551
     1
(*  Title:  HOL/Library/Rational.thy
haftmann@27551
     2
    ID:     $Id$
paulson@14365
     3
    Author: Markus Wenzel, TU Muenchen
paulson@14365
     4
*)
paulson@14365
     5
wenzelm@14691
     6
header {* Rational numbers *}
paulson@14365
     7
nipkow@15131
     8
theory Rational
haftmann@27652
     9
imports "../Presburger" GCD
haftmann@16417
    10
uses ("rat_arith.ML")
nipkow@15131
    11
begin
paulson@14365
    12
haftmann@27551
    13
subsection {* Rational numbers as quotient *}
paulson@14365
    14
haftmann@27551
    15
subsubsection {* Construction of the type of rational numbers *}
huffman@18913
    16
wenzelm@21404
    17
definition
wenzelm@21404
    18
  ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
haftmann@27551
    19
  "ratrel = {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
paulson@14365
    20
huffman@18913
    21
lemma ratrel_iff [simp]:
haftmann@27551
    22
  "(x, y) \<in> ratrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
haftmann@27551
    23
  by (simp add: ratrel_def)
paulson@14365
    24
haftmann@27551
    25
lemma refl_ratrel: "refl {x. snd x \<noteq> 0} ratrel"
haftmann@27551
    26
  by (auto simp add: refl_def ratrel_def)
huffman@18913
    27
huffman@18913
    28
lemma sym_ratrel: "sym ratrel"
haftmann@27551
    29
  by (simp add: ratrel_def sym_def)
paulson@14365
    30
huffman@18913
    31
lemma trans_ratrel: "trans ratrel"
haftmann@27551
    32
proof (rule transI, unfold split_paired_all)
haftmann@27551
    33
  fix a b a' b' a'' b'' :: int
haftmann@27551
    34
  assume A: "((a, b), (a', b')) \<in> ratrel"
haftmann@27551
    35
  assume B: "((a', b'), (a'', b'')) \<in> ratrel"
haftmann@27551
    36
  have "b' * (a * b'') = b'' * (a * b')" by simp
haftmann@27551
    37
  also from A have "a * b' = a' * b" by auto
haftmann@27551
    38
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
haftmann@27551
    39
  also from B have "a' * b'' = a'' * b'" by auto
haftmann@27551
    40
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
haftmann@27551
    41
  finally have "b' * (a * b'') = b' * (a'' * b)" .
haftmann@27551
    42
  moreover from B have "b' \<noteq> 0" by auto
haftmann@27551
    43
  ultimately have "a * b'' = a'' * b" by simp
haftmann@27551
    44
  with A B show "((a, b), (a'', b'')) \<in> ratrel" by auto
paulson@14365
    45
qed
haftmann@27551
    46
  
haftmann@27551
    47
lemma equiv_ratrel: "equiv {x. snd x \<noteq> 0} ratrel"
haftmann@27551
    48
  by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
paulson@14365
    49
huffman@18913
    50
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
huffman@18913
    51
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
paulson@14365
    52
haftmann@27551
    53
lemma equiv_ratrel_iff [iff]: 
haftmann@27551
    54
  assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
haftmann@27551
    55
  shows "ratrel `` {x} = ratrel `` {y} \<longleftrightarrow> (x, y) \<in> ratrel"
haftmann@27551
    56
  by (rule eq_equiv_class_iff, rule equiv_ratrel) (auto simp add: assms)
paulson@14365
    57
haftmann@27551
    58
typedef (Rat) rat = "{x. snd x \<noteq> 0} // ratrel"
haftmann@27551
    59
proof
haftmann@27551
    60
  have "(0::int, 1::int) \<in> {x. snd x \<noteq> 0}" by simp
haftmann@27551
    61
  then show "ratrel `` {(0, 1)} \<in> {x. snd x \<noteq> 0} // ratrel" by (rule quotientI)
haftmann@27551
    62
qed
haftmann@27551
    63
haftmann@27551
    64
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel `` {x} \<in> Rat"
haftmann@27551
    65
  by (simp add: Rat_def quotientI)
haftmann@27551
    66
haftmann@27551
    67
declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp]
haftmann@27551
    68
haftmann@27551
    69
haftmann@27551
    70
subsubsection {* Representation and basic operations *}
haftmann@27551
    71
haftmann@27551
    72
definition
haftmann@27551
    73
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
haftmann@27551
    74
  [code func del]: "Fract a b = Abs_Rat (ratrel `` {if b = 0 then (0, 1) else (a, b)})"
paulson@14365
    75
haftmann@27551
    76
code_datatype Fract
haftmann@27551
    77
haftmann@27551
    78
lemma Rat_cases [case_names Fract, cases type: rat]:
haftmann@27551
    79
  assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
haftmann@27551
    80
  shows C
haftmann@27551
    81
  using assms by (cases q) (clarsimp simp add: Fract_def Rat_def quotient_def)
haftmann@27551
    82
haftmann@27551
    83
lemma Rat_induct [case_names Fract, induct type: rat]:
haftmann@27551
    84
  assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
haftmann@27551
    85
  shows "P q"
haftmann@27551
    86
  using assms by (cases q) simp
haftmann@27551
    87
haftmann@27551
    88
lemma eq_rat:
haftmann@27551
    89
  shows "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
haftmann@27652
    90
  and "\<And>a. Fract a 0 = Fract 0 1"
haftmann@27652
    91
  and "\<And>a c. Fract 0 a = Fract 0 c"
haftmann@27551
    92
  by (simp_all add: Fract_def)
haftmann@27551
    93
haftmann@27551
    94
instantiation rat :: "{comm_ring_1, recpower}"
haftmann@25571
    95
begin
haftmann@25571
    96
haftmann@25571
    97
definition
haftmann@27551
    98
  Zero_rat_def [code, code unfold]: "0 = Fract 0 1"
paulson@14365
    99
haftmann@25571
   100
definition
haftmann@27551
   101
  One_rat_def [code, code unfold]: "1 = Fract 1 1"
huffman@18913
   102
haftmann@25571
   103
definition
haftmann@25571
   104
  add_rat_def [code func del]:
haftmann@27551
   105
  "q + r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   106
    ratrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
haftmann@27551
   107
haftmann@27652
   108
lemma add_rat [simp]:
haftmann@27551
   109
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   110
  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
haftmann@27551
   111
proof -
haftmann@27551
   112
  have "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
haftmann@27551
   113
    respects2 ratrel"
haftmann@27551
   114
  by (rule equiv_ratrel [THEN congruent2_commuteI]) (simp_all add: left_distrib)
haftmann@27551
   115
  with assms show ?thesis by (simp add: Fract_def add_rat_def UN_ratrel2)
haftmann@27551
   116
qed
huffman@18913
   117
haftmann@25571
   118
definition
haftmann@25571
   119
  minus_rat_def [code func del]:
haftmann@27551
   120
  "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel `` {(- fst x, snd x)})"
haftmann@27551
   121
haftmann@27652
   122
lemma minus_rat [simp, code]: "- Fract a b = Fract (- a) b"
haftmann@27551
   123
proof -
haftmann@27551
   124
  have "(\<lambda>x. ratrel `` {(- fst x, snd x)}) respects ratrel"
haftmann@27551
   125
    by (simp add: congruent_def)
haftmann@27551
   126
  then show ?thesis by (simp add: Fract_def minus_rat_def UN_ratrel)
haftmann@27551
   127
qed
haftmann@27551
   128
haftmann@27652
   129
lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
haftmann@27551
   130
  by (cases "b = 0") (simp_all add: eq_rat)
haftmann@25571
   131
haftmann@25571
   132
definition
haftmann@25571
   133
  diff_rat_def [code func del]: "q - r = q + - (r::rat)"
huffman@18913
   134
haftmann@27652
   135
lemma diff_rat [simp]:
haftmann@27551
   136
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   137
  shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
haftmann@27652
   138
  using assms by (simp add: diff_rat_def)
haftmann@25571
   139
haftmann@25571
   140
definition
haftmann@27551
   141
  mult_rat_def [code func del]:
haftmann@27551
   142
  "q * r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   143
    ratrel``{(fst x * fst y, snd x * snd y)})"
paulson@14365
   144
haftmann@27652
   145
lemma mult_rat [simp]: "Fract a b * Fract c d = Fract (a * c) (b * d)"
haftmann@27551
   146
proof -
haftmann@27551
   147
  have "(\<lambda>x y. ratrel `` {(fst x * fst y, snd x * snd y)}) respects2 ratrel"
haftmann@27551
   148
    by (rule equiv_ratrel [THEN congruent2_commuteI]) simp_all
haftmann@27551
   149
  then show ?thesis by (simp add: Fract_def mult_rat_def UN_ratrel2)
paulson@14365
   150
qed
paulson@14365
   151
haftmann@27652
   152
lemma mult_rat_cancel:
haftmann@27551
   153
  assumes "c \<noteq> 0"
haftmann@27551
   154
  shows "Fract (c * a) (c * b) = Fract a b"
haftmann@27551
   155
proof -
haftmann@27551
   156
  from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
haftmann@27652
   157
  then show ?thesis by (simp add: mult_rat [symmetric])
haftmann@27551
   158
qed
huffman@27509
   159
huffman@27509
   160
primrec power_rat
huffman@27509
   161
where
haftmann@27551
   162
  rat_power_0:     "q ^ 0 = (1\<Colon>rat)"
haftmann@27551
   163
  | rat_power_Suc: "q ^ Suc n = (q\<Colon>rat) * (q ^ n)"
huffman@27509
   164
huffman@27509
   165
instance proof
chaieb@27668
   166
  fix q r s :: rat show "(q * r) * s = q * (r * s)" 
haftmann@27652
   167
    by (cases q, cases r, cases s) (simp add: eq_rat)
haftmann@27551
   168
next
haftmann@27551
   169
  fix q r :: rat show "q * r = r * q"
haftmann@27652
   170
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   171
next
haftmann@27551
   172
  fix q :: rat show "1 * q = q"
haftmann@27652
   173
    by (cases q) (simp add: One_rat_def eq_rat)
haftmann@27551
   174
next
haftmann@27551
   175
  fix q r s :: rat show "(q + r) + s = q + (r + s)"
haftmann@27652
   176
    by (cases q, cases r, cases s) (simp add: eq_rat ring_simps)
haftmann@27551
   177
next
haftmann@27551
   178
  fix q r :: rat show "q + r = r + q"
haftmann@27652
   179
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   180
next
haftmann@27551
   181
  fix q :: rat show "0 + q = q"
haftmann@27652
   182
    by (cases q) (simp add: Zero_rat_def eq_rat)
haftmann@27551
   183
next
haftmann@27551
   184
  fix q :: rat show "- q + q = 0"
haftmann@27652
   185
    by (cases q) (simp add: Zero_rat_def eq_rat)
haftmann@27551
   186
next
haftmann@27551
   187
  fix q r :: rat show "q - r = q + - r"
haftmann@27652
   188
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   189
next
haftmann@27551
   190
  fix q r s :: rat show "(q + r) * s = q * s + r * s"
haftmann@27652
   191
    by (cases q, cases r, cases s) (simp add: eq_rat ring_simps)
haftmann@27551
   192
next
haftmann@27551
   193
  show "(0::rat) \<noteq> 1" by (simp add: Zero_rat_def One_rat_def eq_rat)
haftmann@27551
   194
next
haftmann@27551
   195
  fix q :: rat show "q * 1 = q"
haftmann@27652
   196
    by (cases q) (simp add: One_rat_def eq_rat)
haftmann@27551
   197
next
huffman@27509
   198
  fix q :: rat
huffman@27509
   199
  fix n :: nat
huffman@27509
   200
  show "q ^ 0 = 1" by simp
huffman@27509
   201
  show "q ^ (Suc n) = q * (q ^ n)" by simp
huffman@27509
   202
qed
huffman@27509
   203
huffman@27509
   204
end
huffman@27509
   205
haftmann@27551
   206
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
haftmann@27652
   207
  by (induct k) (simp_all add: Zero_rat_def One_rat_def)
haftmann@27551
   208
haftmann@27551
   209
lemma of_int_rat: "of_int k = Fract k 1"
haftmann@27652
   210
  by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
haftmann@27551
   211
haftmann@27551
   212
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
haftmann@27551
   213
  by (rule of_nat_rat [symmetric])
haftmann@27551
   214
haftmann@27551
   215
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
haftmann@27551
   216
  by (rule of_int_rat [symmetric])
haftmann@27551
   217
haftmann@27551
   218
instantiation rat :: number_ring
haftmann@27551
   219
begin
haftmann@27551
   220
haftmann@27551
   221
definition
haftmann@27551
   222
  rat_number_of_def [code func del]: "number_of w = Fract w 1"
haftmann@27551
   223
haftmann@27551
   224
instance by intro_classes (simp add: rat_number_of_def of_int_rat)
haftmann@27551
   225
haftmann@27551
   226
end
haftmann@27551
   227
haftmann@27551
   228
lemma rat_number_collapse [code post]:
haftmann@27551
   229
  "Fract 0 k = 0"
haftmann@27551
   230
  "Fract 1 1 = 1"
haftmann@27551
   231
  "Fract (number_of k) 1 = number_of k"
haftmann@27551
   232
  "Fract k 0 = 0"
haftmann@27551
   233
  by (cases "k = 0")
haftmann@27551
   234
    (simp_all add: Zero_rat_def One_rat_def number_of_is_id number_of_eq of_int_rat eq_rat Fract_def)
haftmann@27551
   235
haftmann@27551
   236
lemma rat_number_expand [code unfold]:
haftmann@27551
   237
  "0 = Fract 0 1"
haftmann@27551
   238
  "1 = Fract 1 1"
haftmann@27551
   239
  "number_of k = Fract (number_of k) 1"
haftmann@27551
   240
  by (simp_all add: rat_number_collapse)
haftmann@27551
   241
haftmann@27551
   242
lemma iszero_rat [simp]:
haftmann@27551
   243
  "iszero (number_of k :: rat) \<longleftrightarrow> iszero (number_of k :: int)"
haftmann@27551
   244
  by (simp add: iszero_def rat_number_expand number_of_is_id eq_rat)
haftmann@27551
   245
haftmann@27551
   246
lemma Rat_cases_nonzero [case_names Fract 0]:
haftmann@27551
   247
  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
haftmann@27551
   248
  assumes 0: "q = 0 \<Longrightarrow> C"
haftmann@27551
   249
  shows C
haftmann@27551
   250
proof (cases "q = 0")
haftmann@27551
   251
  case True then show C using 0 by auto
haftmann@27551
   252
next
haftmann@27551
   253
  case False
haftmann@27551
   254
  then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
haftmann@27551
   255
  moreover with False have "0 \<noteq> Fract a b" by simp
haftmann@27551
   256
  with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_rat_def eq_rat)
haftmann@27551
   257
  with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
haftmann@27551
   258
qed
haftmann@27551
   259
  
haftmann@27551
   260
haftmann@27551
   261
haftmann@27551
   262
subsubsection {* The field of rational numbers *}
haftmann@27551
   263
haftmann@27551
   264
instantiation rat :: "{field, division_by_zero}"
haftmann@27551
   265
begin
haftmann@27551
   266
haftmann@27551
   267
definition
haftmann@27551
   268
  inverse_rat_def [code func del]:
haftmann@27551
   269
  "inverse q = Abs_Rat (\<Union>x \<in> Rep_Rat q.
haftmann@27551
   270
     ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
haftmann@27551
   271
haftmann@27652
   272
lemma inverse_rat [simp]: "inverse (Fract a b) = Fract b a"
haftmann@27551
   273
proof -
haftmann@27551
   274
  have "(\<lambda>x. ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)}) respects ratrel"
haftmann@27551
   275
    by (auto simp add: congruent_def mult_commute)
haftmann@27551
   276
  then show ?thesis by (simp add: Fract_def inverse_rat_def UN_ratrel)
huffman@27509
   277
qed
huffman@27509
   278
haftmann@27551
   279
definition
haftmann@27551
   280
  divide_rat_def [code func del]: "q / r = q * inverse (r::rat)"
haftmann@27551
   281
haftmann@27652
   282
lemma divide_rat [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
haftmann@27652
   283
  by (simp add: divide_rat_def)
haftmann@27551
   284
haftmann@27551
   285
instance proof
haftmann@27652
   286
  show "inverse 0 = (0::rat)" by (simp add: rat_number_expand)
haftmann@27551
   287
    (simp add: rat_number_collapse)
haftmann@27551
   288
next
haftmann@27551
   289
  fix q :: rat
haftmann@27551
   290
  assume "q \<noteq> 0"
haftmann@27551
   291
  then show "inverse q * q = 1" by (cases q rule: Rat_cases_nonzero)
haftmann@27551
   292
   (simp_all add: mult_rat  inverse_rat rat_number_expand eq_rat)
haftmann@27551
   293
next
haftmann@27551
   294
  fix q r :: rat
haftmann@27551
   295
  show "q / r = q * inverse r" by (simp add: divide_rat_def)
haftmann@27551
   296
qed
haftmann@27551
   297
haftmann@27551
   298
end
haftmann@27551
   299
haftmann@27551
   300
haftmann@27551
   301
subsubsection {* Various *}
haftmann@27551
   302
haftmann@27551
   303
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
haftmann@27652
   304
  by (simp add: rat_number_expand)
haftmann@27551
   305
haftmann@27551
   306
lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
haftmann@27652
   307
  by (simp add: Fract_of_int_eq [symmetric])
haftmann@27551
   308
haftmann@27551
   309
lemma Fract_number_of_quotient [code post]:
haftmann@27551
   310
  "Fract (number_of k) (number_of l) = number_of k / number_of l"
haftmann@27551
   311
  unfolding Fract_of_int_quotient number_of_is_id number_of_eq ..
haftmann@27551
   312
haftmann@27652
   313
lemma Fract_1_number_of [code post]:
haftmann@27652
   314
  "Fract 1 (number_of k) = 1 / number_of k"
haftmann@27652
   315
  unfolding Fract_of_int_quotient number_of_eq by simp
haftmann@27551
   316
haftmann@27551
   317
subsubsection {* The ordered field of rational numbers *}
huffman@27509
   318
huffman@27509
   319
instantiation rat :: linorder
huffman@27509
   320
begin
huffman@27509
   321
huffman@27509
   322
definition
huffman@27509
   323
  le_rat_def [code func del]:
huffman@27509
   324
   "q \<le> r \<longleftrightarrow> contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   325
      {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
haftmann@27551
   326
haftmann@27652
   327
lemma le_rat [simp]:
haftmann@27551
   328
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   329
  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27551
   330
proof -
haftmann@27551
   331
  have "(\<lambda>x y. {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})
haftmann@27551
   332
    respects2 ratrel"
haftmann@27551
   333
  proof (clarsimp simp add: congruent2_def)
haftmann@27551
   334
    fix a b a' b' c d c' d'::int
haftmann@27551
   335
    assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
haftmann@27551
   336
    assume eq1: "a * b' = a' * b"
haftmann@27551
   337
    assume eq2: "c * d' = c' * d"
haftmann@27551
   338
haftmann@27551
   339
    let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
haftmann@27551
   340
    {
haftmann@27551
   341
      fix a b c d x :: int assume x: "x \<noteq> 0"
haftmann@27551
   342
      have "?le a b c d = ?le (a * x) (b * x) c d"
haftmann@27551
   343
      proof -
haftmann@27551
   344
        from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
haftmann@27551
   345
        hence "?le a b c d =
haftmann@27551
   346
            ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
haftmann@27551
   347
          by (simp add: mult_le_cancel_right)
haftmann@27551
   348
        also have "... = ?le (a * x) (b * x) c d"
haftmann@27551
   349
          by (simp add: mult_ac)
haftmann@27551
   350
        finally show ?thesis .
haftmann@27551
   351
      qed
haftmann@27551
   352
    } note le_factor = this
haftmann@27551
   353
haftmann@27551
   354
    let ?D = "b * d" and ?D' = "b' * d'"
haftmann@27551
   355
    from neq have D: "?D \<noteq> 0" by simp
haftmann@27551
   356
    from neq have "?D' \<noteq> 0" by simp
haftmann@27551
   357
    hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
haftmann@27551
   358
      by (rule le_factor)
chaieb@27668
   359
    also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')" 
haftmann@27551
   360
      by (simp add: mult_ac)
haftmann@27551
   361
    also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
haftmann@27551
   362
      by (simp only: eq1 eq2)
haftmann@27551
   363
    also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
haftmann@27551
   364
      by (simp add: mult_ac)
haftmann@27551
   365
    also from D have "... = ?le a' b' c' d'"
haftmann@27551
   366
      by (rule le_factor [symmetric])
haftmann@27551
   367
    finally show "?le a b c d = ?le a' b' c' d'" .
haftmann@27551
   368
  qed
haftmann@27551
   369
  with assms show ?thesis by (simp add: Fract_def le_rat_def UN_ratrel2)
haftmann@27551
   370
qed
huffman@27509
   371
huffman@27509
   372
definition
huffman@27509
   373
  less_rat_def [code func del]: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
huffman@27509
   374
haftmann@27652
   375
lemma less_rat [simp]:
haftmann@27551
   376
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   377
  shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
haftmann@27652
   378
  using assms by (simp add: less_rat_def eq_rat order_less_le)
huffman@27509
   379
huffman@27509
   380
instance proof
paulson@14365
   381
  fix q r s :: rat
paulson@14365
   382
  {
paulson@14365
   383
    assume "q \<le> r" and "r \<le> s"
paulson@14365
   384
    show "q \<le> s"
paulson@14365
   385
    proof (insert prems, induct q, induct r, induct s)
paulson@14365
   386
      fix a b c d e f :: int
paulson@14365
   387
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   388
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365
   389
      show "Fract a b \<le> Fract e f"
paulson@14365
   390
      proof -
paulson@14365
   391
        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365
   392
          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365
   393
        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365
   394
        proof -
paulson@14365
   395
          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   396
            by simp
paulson@14365
   397
          with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   398
        qed
chaieb@27668
   399
        also have "... = (c * f) * (d * f) * (b * b)" by algebra
paulson@14365
   400
        also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365
   401
        proof -
paulson@14365
   402
          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
haftmann@27652
   403
            by simp
paulson@14365
   404
          with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   405
        qed
paulson@14365
   406
        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365
   407
          by (simp only: mult_ac)
paulson@14365
   408
        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365
   409
          by (simp add: mult_le_cancel_right)
haftmann@27652
   410
        with neq show ?thesis by simp
paulson@14365
   411
      qed
paulson@14365
   412
    qed
paulson@14365
   413
  next
paulson@14365
   414
    assume "q \<le> r" and "r \<le> q"
paulson@14365
   415
    show "q = r"
paulson@14365
   416
    proof (insert prems, induct q, induct r)
paulson@14365
   417
      fix a b c d :: int
paulson@14365
   418
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   419
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365
   420
      show "Fract a b = Fract c d"
paulson@14365
   421
      proof -
paulson@14365
   422
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   423
          by simp
paulson@14365
   424
        also have "... \<le> (a * d) * (b * d)"
paulson@14365
   425
        proof -
paulson@14365
   426
          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
haftmann@27652
   427
            by simp
paulson@14365
   428
          thus ?thesis by (simp only: mult_ac)
paulson@14365
   429
        qed
paulson@14365
   430
        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365
   431
        moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365
   432
        ultimately have "a * d = c * b" by simp
paulson@14365
   433
        with neq show ?thesis by (simp add: eq_rat)
paulson@14365
   434
      qed
paulson@14365
   435
    qed
paulson@14365
   436
  next
paulson@14365
   437
    show "q \<le> q"
haftmann@27652
   438
      by (induct q) simp
haftmann@27682
   439
    show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
haftmann@27682
   440
      by (induct q, induct r) (auto simp add: le_less mult_commute)
paulson@14365
   441
    show "q \<le> r \<or> r \<le> q"
huffman@18913
   442
      by (induct q, induct r)
haftmann@27652
   443
         (simp add: mult_commute, rule linorder_linear)
paulson@14365
   444
  }
paulson@14365
   445
qed
paulson@14365
   446
huffman@27509
   447
end
huffman@27509
   448
haftmann@27551
   449
instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
haftmann@25571
   450
begin
haftmann@25571
   451
haftmann@25571
   452
definition
haftmann@27652
   453
  abs_rat_def [code func del]: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
haftmann@27551
   454
haftmann@27652
   455
lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
haftmann@27551
   456
  by (auto simp add: abs_rat_def zabs_def Zero_rat_def less_rat not_less le_less minus_rat eq_rat zero_compare_simps)
haftmann@27551
   457
haftmann@27551
   458
definition
haftmann@27652
   459
  sgn_rat_def [code func del]: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
haftmann@27551
   460
haftmann@27652
   461
lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
haftmann@27551
   462
  unfolding Fract_of_int_eq
haftmann@27652
   463
  by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
haftmann@27551
   464
    (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
haftmann@27551
   465
haftmann@27551
   466
definition
haftmann@25571
   467
  "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
haftmann@25571
   468
haftmann@25571
   469
definition
haftmann@25571
   470
  "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
haftmann@25571
   471
haftmann@27551
   472
instance by intro_classes
haftmann@27551
   473
  (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
haftmann@22456
   474
haftmann@25571
   475
end
haftmann@25571
   476
haftmann@27551
   477
instance rat :: ordered_field
haftmann@27551
   478
proof
paulson@14365
   479
  fix q r s :: rat
paulson@14365
   480
  show "q \<le> r ==> s + q \<le> s + r"
paulson@14365
   481
  proof (induct q, induct r, induct s)
paulson@14365
   482
    fix a b c d e f :: int
paulson@14365
   483
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   484
    assume le: "Fract a b \<le> Fract c d"
paulson@14365
   485
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365
   486
    proof -
paulson@14365
   487
      let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365
   488
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   489
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   490
        by simp
paulson@14365
   491
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365
   492
        by (simp add: mult_le_cancel_right)
haftmann@27652
   493
      with neq show ?thesis by (simp add: mult_ac int_distrib)
paulson@14365
   494
    qed
paulson@14365
   495
  qed
paulson@14365
   496
  show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365
   497
  proof (induct q, induct r, induct s)
paulson@14365
   498
    fix a b c d e f :: int
paulson@14365
   499
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   500
    assume le: "Fract a b < Fract c d"
paulson@14365
   501
    assume gt: "0 < Fract e f"
paulson@14365
   502
    show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365
   503
    proof -
paulson@14365
   504
      let ?E = "e * f" and ?F = "f * f"
paulson@14365
   505
      from neq gt have "0 < ?E"
haftmann@27652
   506
        by (auto simp add: Zero_rat_def order_less_le eq_rat)
paulson@14365
   507
      moreover from neq have "0 < ?F"
paulson@14365
   508
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   509
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
haftmann@27652
   510
        by simp
paulson@14365
   511
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365
   512
        by (simp add: mult_less_cancel_right)
paulson@14365
   513
      with neq show ?thesis
haftmann@27652
   514
        by (simp add: mult_ac)
paulson@14365
   515
    qed
paulson@14365
   516
  qed
haftmann@27551
   517
qed auto
paulson@14365
   518
haftmann@27551
   519
lemma Rat_induct_pos [case_names Fract, induct type: rat]:
haftmann@27551
   520
  assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
haftmann@27551
   521
  shows "P q"
paulson@14365
   522
proof (cases q)
haftmann@27551
   523
  have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
paulson@14365
   524
  proof -
paulson@14365
   525
    fix a::int and b::int
paulson@14365
   526
    assume b: "b < 0"
paulson@14365
   527
    hence "0 < -b" by simp
paulson@14365
   528
    hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365
   529
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   530
  qed
paulson@14365
   531
  case (Fract a b)
paulson@14365
   532
  thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365
   533
qed
paulson@14365
   534
paulson@14365
   535
lemma zero_less_Fract_iff:
haftmann@27652
   536
  "0 < b ==> (0 < Fract a b) = (0 < a)"
haftmann@27652
   537
by (simp add: Zero_rat_def order_less_imp_not_eq2 zero_less_mult_iff)
paulson@14365
   538
paulson@14378
   539
haftmann@27551
   540
subsection {* Arithmetic setup *}
paulson@14387
   541
paulson@14387
   542
use "rat_arith.ML"
wenzelm@24075
   543
declaration {* K rat_arith_setup *}
paulson@14387
   544
huffman@23342
   545
huffman@23342
   546
subsection {* Embedding from Rationals to other Fields *}
huffman@23342
   547
haftmann@24198
   548
class field_char_0 = field + ring_char_0
huffman@23342
   549
haftmann@27551
   550
subclass (in ordered_field) field_char_0 ..
huffman@23342
   551
haftmann@27551
   552
context field_char_0
haftmann@27551
   553
begin
haftmann@27551
   554
haftmann@27551
   555
definition of_rat :: "rat \<Rightarrow> 'a" where
haftmann@24198
   556
  [code func del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
huffman@23342
   557
haftmann@27551
   558
end
haftmann@27551
   559
huffman@23342
   560
lemma of_rat_congruent:
haftmann@27551
   561
  "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
huffman@23342
   562
apply (rule congruent.intro)
huffman@23342
   563
apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23342
   564
apply (simp only: of_int_mult [symmetric])
huffman@23342
   565
done
huffman@23342
   566
haftmann@27551
   567
lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
haftmann@27551
   568
  unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
huffman@23342
   569
huffman@23342
   570
lemma of_rat_0 [simp]: "of_rat 0 = 0"
huffman@23342
   571
by (simp add: Zero_rat_def of_rat_rat)
huffman@23342
   572
huffman@23342
   573
lemma of_rat_1 [simp]: "of_rat 1 = 1"
huffman@23342
   574
by (simp add: One_rat_def of_rat_rat)
huffman@23342
   575
huffman@23342
   576
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
haftmann@27652
   577
by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
huffman@23342
   578
huffman@23343
   579
lemma of_rat_minus: "of_rat (- a) = - of_rat a"
haftmann@27652
   580
by (induct a, simp add: of_rat_rat)
huffman@23343
   581
huffman@23343
   582
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
huffman@23343
   583
by (simp only: diff_minus of_rat_add of_rat_minus)
huffman@23343
   584
huffman@23342
   585
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
haftmann@27652
   586
apply (induct a, induct b, simp add: of_rat_rat)
huffman@23342
   587
apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
huffman@23342
   588
done
huffman@23342
   589
huffman@23342
   590
lemma nonzero_of_rat_inverse:
huffman@23342
   591
  "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
huffman@23343
   592
apply (rule inverse_unique [symmetric])
huffman@23343
   593
apply (simp add: of_rat_mult [symmetric])
huffman@23342
   594
done
huffman@23342
   595
huffman@23342
   596
lemma of_rat_inverse:
huffman@23342
   597
  "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
huffman@23342
   598
   inverse (of_rat a)"
huffman@23342
   599
by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
huffman@23342
   600
huffman@23342
   601
lemma nonzero_of_rat_divide:
huffman@23342
   602
  "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
huffman@23342
   603
by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
huffman@23342
   604
huffman@23342
   605
lemma of_rat_divide:
huffman@23342
   606
  "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
huffman@23342
   607
   = of_rat a / of_rat b"
haftmann@27652
   608
by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
huffman@23342
   609
huffman@23343
   610
lemma of_rat_power:
huffman@23343
   611
  "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
huffman@23343
   612
by (induct n) (simp_all add: of_rat_mult power_Suc)
huffman@23343
   613
huffman@23343
   614
lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
huffman@23343
   615
apply (induct a, induct b)
huffman@23343
   616
apply (simp add: of_rat_rat eq_rat)
huffman@23343
   617
apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23343
   618
apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
huffman@23343
   619
done
huffman@23343
   620
haftmann@27652
   621
lemma of_rat_less:
haftmann@27652
   622
  "(of_rat r :: 'a::ordered_field) < of_rat s \<longleftrightarrow> r < s"
haftmann@27652
   623
proof (induct r, induct s)
haftmann@27652
   624
  fix a b c d :: int
haftmann@27652
   625
  assume not_zero: "b > 0" "d > 0"
haftmann@27652
   626
  then have "b * d > 0" by (rule mult_pos_pos)
haftmann@27652
   627
  have of_int_divide_less_eq:
haftmann@27652
   628
    "(of_int a :: 'a) / of_int b < of_int c / of_int d
haftmann@27652
   629
      \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
haftmann@27652
   630
    using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
haftmann@27652
   631
  show "(of_rat (Fract a b) :: 'a::ordered_field) < of_rat (Fract c d)
haftmann@27652
   632
    \<longleftrightarrow> Fract a b < Fract c d"
haftmann@27652
   633
    using not_zero `b * d > 0`
haftmann@27652
   634
    by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
haftmann@27652
   635
      (auto intro: mult_strict_right_mono mult_right_less_imp_less)
haftmann@27652
   636
qed
haftmann@27652
   637
haftmann@27652
   638
lemma of_rat_less_eq:
haftmann@27652
   639
  "(of_rat r :: 'a::ordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
haftmann@27652
   640
  unfolding le_less by (auto simp add: of_rat_less)
haftmann@27652
   641
huffman@23343
   642
lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
huffman@23343
   643
haftmann@27652
   644
lemma of_rat_eq_id [simp]: "of_rat = id"
huffman@23343
   645
proof
huffman@23343
   646
  fix a
huffman@23343
   647
  show "of_rat a = id a"
huffman@23343
   648
  by (induct a)
haftmann@27652
   649
     (simp add: of_rat_rat Fract_of_int_eq [symmetric])
huffman@23343
   650
qed
huffman@23343
   651
huffman@23343
   652
text{*Collapse nested embeddings*}
huffman@23343
   653
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
huffman@23343
   654
by (induct n) (simp_all add: of_rat_add)
huffman@23343
   655
huffman@23343
   656
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
haftmann@27652
   657
by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
huffman@23343
   658
huffman@23343
   659
lemma of_rat_number_of_eq [simp]:
huffman@23343
   660
  "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
huffman@23343
   661
by (simp add: number_of_eq)
huffman@23343
   662
haftmann@23879
   663
lemmas zero_rat = Zero_rat_def
haftmann@23879
   664
lemmas one_rat = One_rat_def
haftmann@23879
   665
haftmann@24198
   666
abbreviation
haftmann@24198
   667
  rat_of_nat :: "nat \<Rightarrow> rat"
haftmann@24198
   668
where
haftmann@24198
   669
  "rat_of_nat \<equiv> of_nat"
haftmann@24198
   670
haftmann@24198
   671
abbreviation
haftmann@24198
   672
  rat_of_int :: "int \<Rightarrow> rat"
haftmann@24198
   673
where
haftmann@24198
   674
  "rat_of_int \<equiv> of_int"
haftmann@24198
   675
berghofe@24533
   676
berghofe@24533
   677
subsection {* Implementation of rational numbers as pairs of integers *}
berghofe@24533
   678
haftmann@27652
   679
lemma Fract_norm: "Fract (a div zgcd a b) (b div zgcd a b) = Fract a b"
haftmann@27652
   680
proof (cases "a = 0 \<or> b = 0")
haftmann@27652
   681
  case True then show ?thesis by (auto simp add: eq_rat)
haftmann@27652
   682
next
haftmann@27652
   683
  let ?c = "zgcd a b"
haftmann@27652
   684
  case False then have "a \<noteq> 0" and "b \<noteq> 0" by auto
haftmann@27652
   685
  then have "?c \<noteq> 0" by simp
haftmann@27652
   686
  then have "Fract ?c ?c = Fract 1 1" by (simp add: eq_rat)
haftmann@27652
   687
  moreover have "Fract (a div ?c * ?c + a mod ?c) (b div ?c * ?c + b mod ?c) = Fract a b"
haftmann@27652
   688
   by (simp add: times_div_mod_plus_zero_one.mod_div_equality)
haftmann@27652
   689
  moreover have "a mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
haftmann@27652
   690
  moreover have "b mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
haftmann@27652
   691
  ultimately show ?thesis
haftmann@27652
   692
    by (simp add: mult_rat [symmetric])
haftmann@27652
   693
qed
berghofe@24533
   694
haftmann@27652
   695
definition Fract_norm :: "int \<Rightarrow> int \<Rightarrow> rat" where
haftmann@27652
   696
  [simp, code func del]: "Fract_norm a b = Fract a b"
haftmann@27652
   697
haftmann@27652
   698
lemma [code func]: "Fract_norm a b = (if a = 0 \<or> b = 0 then 0 else let c = zgcd a b in
haftmann@27652
   699
  if b > 0 then Fract (a div c) (b div c) else Fract (- (a div c)) (- (b div c)))"
haftmann@27652
   700
  by (simp add: eq_rat Zero_rat_def Let_def Fract_norm)
berghofe@24533
   701
berghofe@24533
   702
lemma [code]:
haftmann@27652
   703
  "of_rat (Fract a b) = (if b \<noteq> 0 then of_int a / of_int b else 0)"
haftmann@27652
   704
  by (cases "b = 0") (simp_all add: rat_number_collapse of_rat_rat)
berghofe@24533
   705
haftmann@26513
   706
instantiation rat :: eq
haftmann@26513
   707
begin
haftmann@26513
   708
haftmann@27652
   709
definition [code func del]: "eq_class.eq (a\<Colon>rat) b \<longleftrightarrow> a - b = 0"
berghofe@24533
   710
haftmann@26513
   711
instance by default (simp add: eq_rat_def)
haftmann@26513
   712
haftmann@27652
   713
lemma rat_eq_code [code]:
haftmann@27652
   714
  "eq_class.eq (Fract a b) (Fract c d) \<longleftrightarrow> (if b = 0
haftmann@27652
   715
       then c = 0 \<or> d = 0
haftmann@27652
   716
     else if d = 0
haftmann@27652
   717
       then a = 0 \<or> b = 0
haftmann@27652
   718
     else a * d =  b * c)"
haftmann@27652
   719
  by (auto simp add: eq eq_rat)
haftmann@26513
   720
haftmann@26513
   721
end
berghofe@24533
   722
haftmann@27652
   723
lemma le_rat':
haftmann@27652
   724
  assumes "b \<noteq> 0"
haftmann@27652
   725
    and "d \<noteq> 0"
haftmann@27652
   726
  shows "Fract a b \<le> Fract c d \<longleftrightarrow> a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d"
berghofe@24533
   727
proof -
haftmann@27652
   728
  have abs_sgn: "\<And>k::int. \<bar>k\<bar> = k * sgn k" unfolding abs_if sgn_if by simp
haftmann@27652
   729
  have "a * d * (b * d) \<le> c * b * (b * d) \<longleftrightarrow> a * d * (sgn b * sgn d) \<le> c * b * (sgn b * sgn d)"
haftmann@27652
   730
  proof (cases "b * d > 0")
haftmann@27652
   731
    case True
haftmann@27652
   732
    moreover from True have "sgn b * sgn d = 1"
haftmann@27652
   733
      by (simp add: sgn_times [symmetric] sgn_1_pos)
haftmann@27652
   734
    ultimately show ?thesis by (simp add: mult_le_cancel_right)
haftmann@27652
   735
  next
haftmann@27652
   736
    case False with assms have "b * d < 0" by (simp add: less_le)
haftmann@27652
   737
    moreover from this have "sgn b * sgn d = - 1"
haftmann@27652
   738
      by (simp only: sgn_times [symmetric] sgn_1_neg)
haftmann@27652
   739
    ultimately show ?thesis by (simp add: mult_le_cancel_right)
haftmann@27652
   740
  qed
haftmann@27652
   741
  also have "\<dots> \<longleftrightarrow> a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d"
haftmann@27652
   742
    by (simp add: abs_sgn mult_ac)
haftmann@27652
   743
  finally show ?thesis using assms by simp
berghofe@24533
   744
qed
berghofe@24533
   745
haftmann@27652
   746
lemma less_rat': 
haftmann@27652
   747
  assumes "b \<noteq> 0"
haftmann@27652
   748
    and "d \<noteq> 0"
haftmann@27652
   749
  shows "Fract a b < Fract c d \<longleftrightarrow> a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d"
berghofe@24533
   750
proof -
haftmann@27652
   751
  have abs_sgn: "\<And>k::int. \<bar>k\<bar> = k * sgn k" unfolding abs_if sgn_if by simp
haftmann@27652
   752
  have "a * d * (b * d) < c * b * (b * d) \<longleftrightarrow> a * d * (sgn b * sgn d) < c * b * (sgn b * sgn d)"
haftmann@27652
   753
  proof (cases "b * d > 0")
haftmann@27652
   754
    case True
haftmann@27652
   755
    moreover from True have "sgn b * sgn d = 1"
haftmann@27652
   756
      by (simp add: sgn_times [symmetric] sgn_1_pos)
haftmann@27652
   757
    ultimately show ?thesis by (simp add: mult_less_cancel_right)
haftmann@27652
   758
  next
haftmann@27652
   759
    case False with assms have "b * d < 0" by (simp add: less_le)
haftmann@27652
   760
    moreover from this have "sgn b * sgn d = - 1"
haftmann@27652
   761
      by (simp only: sgn_times [symmetric] sgn_1_neg)
haftmann@27652
   762
    ultimately show ?thesis by (simp add: mult_less_cancel_right)
haftmann@27652
   763
  qed
haftmann@27652
   764
  also have "\<dots> \<longleftrightarrow> a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d"
haftmann@27652
   765
    by (simp add: abs_sgn mult_ac)
haftmann@27652
   766
  finally show ?thesis using assms by simp
berghofe@24533
   767
qed
berghofe@24533
   768
haftmann@27652
   769
lemma rat_less_eq_code [code]:
haftmann@27652
   770
  "Fract a b \<le> Fract c d \<longleftrightarrow> (if b = 0
haftmann@27652
   771
       then sgn c * sgn d \<ge> 0
haftmann@27652
   772
     else if d = 0
haftmann@27652
   773
       then sgn a * sgn b \<le> 0
haftmann@27652
   774
     else a * \<bar>d\<bar> * sgn b \<le> c * \<bar>b\<bar> * sgn d)"
haftmann@27652
   775
by (auto simp add: sgn_times mult_le_0_iff zero_le_mult_iff le_rat' eq_rat simp del: le_rat)
haftmann@27652
   776
  (auto simp add: sgn_times sgn_0_0 le_less sgn_1_pos [symmetric] sgn_1_neg [symmetric])
berghofe@24533
   777
haftmann@27652
   778
lemma rat_le_eq_code [code]:
haftmann@27652
   779
  "Fract a b < Fract c d \<longleftrightarrow> (if b = 0
haftmann@27652
   780
       then sgn c * sgn d > 0
haftmann@27652
   781
     else if d = 0
haftmann@27652
   782
       then sgn a * sgn b < 0
haftmann@27652
   783
     else a * \<bar>d\<bar> * sgn b < c * \<bar>b\<bar> * sgn d)"
haftmann@27652
   784
by (auto simp add: sgn_times mult_less_0_iff zero_less_mult_iff less_rat' eq_rat simp del: less_rat)
haftmann@27652
   785
   (auto simp add: sgn_times sgn_0_0 sgn_1_pos [symmetric] sgn_1_neg [symmetric],
haftmann@27652
   786
     auto simp add: sgn_1_pos)
berghofe@24533
   787
haftmann@27652
   788
lemma rat_plus_code [code]:
haftmann@27652
   789
  "Fract a b + Fract c d = (if b = 0
haftmann@27652
   790
     then Fract c d
haftmann@27652
   791
   else if d = 0
haftmann@27652
   792
     then Fract a b
haftmann@27652
   793
   else Fract_norm (a * d + c * b) (b * d))"
haftmann@27652
   794
  by (simp add: eq_rat, simp add: Zero_rat_def)
haftmann@27652
   795
haftmann@27652
   796
lemma rat_times_code [code]:
haftmann@27652
   797
  "Fract a b * Fract c d = Fract_norm (a * c) (b * d)"
haftmann@27652
   798
  by simp
berghofe@24533
   799
haftmann@27652
   800
lemma rat_minus_code [code]:
haftmann@27652
   801
  "Fract a b - Fract c d = (if b = 0
haftmann@27652
   802
     then Fract (- c) d
haftmann@27652
   803
   else if d = 0
haftmann@27652
   804
     then Fract a b
haftmann@27652
   805
   else Fract_norm (a * d - c * b) (b * d))"
haftmann@27652
   806
  by (simp add: eq_rat, simp add: Zero_rat_def)
berghofe@24533
   807
haftmann@27652
   808
lemma rat_inverse_code [code]:
haftmann@27652
   809
  "inverse (Fract a b) = (if b = 0 then Fract 1 0
haftmann@27652
   810
    else if a < 0 then Fract (- b) (- a)
haftmann@27652
   811
    else Fract b a)"
haftmann@27652
   812
  by (simp add: eq_rat)
haftmann@27652
   813
haftmann@27652
   814
lemma rat_divide_code [code]:
haftmann@27652
   815
  "Fract a b / Fract c d = Fract_norm (a * d) (b * c)"
haftmann@27652
   816
  by simp
haftmann@27652
   817
haftmann@27652
   818
hide (open) const Fract_norm
berghofe@24533
   819
haftmann@24622
   820
text {* Setup for SML code generator *}
berghofe@24533
   821
berghofe@24533
   822
types_code
berghofe@24533
   823
  rat ("(int */ int)")
berghofe@24533
   824
attach (term_of) {*
berghofe@24533
   825
fun term_of_rat (p, q) =
haftmann@24622
   826
  let
haftmann@24661
   827
    val rT = Type ("Rational.rat", [])
berghofe@24533
   828
  in
berghofe@24533
   829
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
berghofe@25885
   830
    else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} $
berghofe@24533
   831
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
berghofe@24533
   832
  end;
berghofe@24533
   833
*}
berghofe@24533
   834
attach (test) {*
berghofe@24533
   835
fun gen_rat i =
berghofe@24533
   836
  let
berghofe@24533
   837
    val p = random_range 0 i;
berghofe@24533
   838
    val q = random_range 1 (i + 1);
berghofe@24533
   839
    val g = Integer.gcd p q;
wenzelm@24630
   840
    val p' = p div g;
wenzelm@24630
   841
    val q' = q div g;
berghofe@25885
   842
    val r = (if one_of [true, false] then p' else ~ p',
berghofe@25885
   843
      if p' = 0 then 0 else q')
berghofe@24533
   844
  in
berghofe@25885
   845
    (r, fn () => term_of_rat r)
berghofe@24533
   846
  end;
berghofe@24533
   847
*}
berghofe@24533
   848
berghofe@24533
   849
consts_code
haftmann@27551
   850
  Fract ("(_,/ _)")
berghofe@24533
   851
berghofe@24533
   852
consts_code
berghofe@24533
   853
  "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
berghofe@24533
   854
attach {*
berghofe@24533
   855
fun rat_of_int 0 = (0, 0)
berghofe@24533
   856
  | rat_of_int i = (i, 1);
berghofe@24533
   857
*}
berghofe@24533
   858
haftmann@27652
   859
end