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