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