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