src/HOL/Rat.thy
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 47108 2a1953f0d20d
child 47906 09a896d295bd
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
haftmann@35372
     1
(*  Title:  HOL/Rat.thy
paulson@14365
     2
    Author: Markus Wenzel, TU Muenchen
paulson@14365
     3
*)
paulson@14365
     4
wenzelm@14691
     5
header {* Rational numbers *}
paulson@14365
     6
haftmann@35372
     7
theory Rat
huffman@30097
     8
imports GCD Archimedean_Field
huffman@35343
     9
uses ("Tools/float_syntax.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"
haftmann@40815
    47
  by (rule equivI [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
wenzelm@45694
    57
definition "Rat = {x. snd x \<noteq> 0} // ratrel"
wenzelm@45694
    58
wenzelm@45694
    59
typedef (open) rat = Rat
wenzelm@45694
    60
  morphisms Rep_Rat Abs_Rat
wenzelm@45694
    61
  unfolding Rat_def
haftmann@27551
    62
proof
haftmann@27551
    63
  have "(0::int, 1::int) \<in> {x. snd x \<noteq> 0}" by simp
haftmann@27551
    64
  then show "ratrel `` {(0, 1)} \<in> {x. snd x \<noteq> 0} // ratrel" by (rule quotientI)
haftmann@27551
    65
qed
haftmann@27551
    66
haftmann@27551
    67
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel `` {x} \<in> Rat"
haftmann@27551
    68
  by (simp add: Rat_def quotientI)
haftmann@27551
    69
haftmann@27551
    70
declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp]
haftmann@27551
    71
haftmann@27551
    72
haftmann@27551
    73
subsubsection {* Representation and basic operations *}
haftmann@27551
    74
haftmann@27551
    75
definition
haftmann@27551
    76
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
haftmann@35369
    77
  "Fract a b = Abs_Rat (ratrel `` {if b = 0 then (0, 1) else (a, b)})"
haftmann@27551
    78
haftmann@27551
    79
lemma eq_rat:
haftmann@27551
    80
  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
    81
  and "\<And>a. Fract a 0 = Fract 0 1"
haftmann@27652
    82
  and "\<And>a c. Fract 0 a = Fract 0 c"
haftmann@27551
    83
  by (simp_all add: Fract_def)
haftmann@27551
    84
haftmann@35369
    85
lemma Rat_cases [case_names Fract, cases type: rat]:
haftmann@35369
    86
  assumes "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
haftmann@35369
    87
  shows C
haftmann@35369
    88
proof -
haftmann@35369
    89
  obtain a b :: int where "q = Fract a b" and "b \<noteq> 0"
haftmann@35369
    90
    by (cases q) (clarsimp simp add: Fract_def Rat_def quotient_def)
haftmann@35369
    91
  let ?a = "a div gcd a b"
haftmann@35369
    92
  let ?b = "b div gcd a b"
haftmann@35369
    93
  from `b \<noteq> 0` have "?b * gcd a b = b"
haftmann@35369
    94
    by (simp add: dvd_div_mult_self)
haftmann@35369
    95
  with `b \<noteq> 0` have "?b \<noteq> 0" by auto
haftmann@35369
    96
  from `q = Fract a b` `b \<noteq> 0` `?b \<noteq> 0` have q: "q = Fract ?a ?b"
haftmann@35369
    97
    by (simp add: eq_rat dvd_div_mult mult_commute [of a])
haftmann@35369
    98
  from `b \<noteq> 0` have coprime: "coprime ?a ?b"
haftmann@35369
    99
    by (auto intro: div_gcd_coprime_int)
haftmann@35369
   100
  show C proof (cases "b > 0")
haftmann@35369
   101
    case True
haftmann@35369
   102
    note assms
haftmann@35369
   103
    moreover note q
haftmann@35369
   104
    moreover from True have "?b > 0" by (simp add: nonneg1_imp_zdiv_pos_iff)
haftmann@35369
   105
    moreover note coprime
haftmann@35369
   106
    ultimately show C .
haftmann@35369
   107
  next
haftmann@35369
   108
    case False
haftmann@35369
   109
    note assms
haftmann@35369
   110
    moreover from q have "q = Fract (- ?a) (- ?b)" by (simp add: Fract_def)
haftmann@35369
   111
    moreover from False `b \<noteq> 0` have "- ?b > 0" by (simp add: pos_imp_zdiv_neg_iff)
haftmann@35369
   112
    moreover from coprime have "coprime (- ?a) (- ?b)" by simp
haftmann@35369
   113
    ultimately show C .
haftmann@35369
   114
  qed
haftmann@35369
   115
qed
haftmann@35369
   116
haftmann@35369
   117
lemma Rat_induct [case_names Fract, induct type: rat]:
haftmann@35369
   118
  assumes "\<And>a b. b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> P (Fract a b)"
haftmann@35369
   119
  shows "P q"
haftmann@35369
   120
  using assms by (cases q) simp
haftmann@35369
   121
haftmann@31017
   122
instantiation rat :: comm_ring_1
haftmann@25571
   123
begin
haftmann@25571
   124
haftmann@25571
   125
definition
haftmann@35369
   126
  Zero_rat_def: "0 = Fract 0 1"
paulson@14365
   127
haftmann@25571
   128
definition
haftmann@35369
   129
  One_rat_def: "1 = Fract 1 1"
huffman@18913
   130
haftmann@25571
   131
definition
haftmann@35369
   132
  add_rat_def:
haftmann@27551
   133
  "q + r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   134
    ratrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
haftmann@27551
   135
haftmann@27652
   136
lemma add_rat [simp]:
haftmann@27551
   137
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   138
  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
haftmann@27551
   139
proof -
haftmann@27551
   140
  have "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
haftmann@27551
   141
    respects2 ratrel"
haftmann@27551
   142
  by (rule equiv_ratrel [THEN congruent2_commuteI]) (simp_all add: left_distrib)
haftmann@27551
   143
  with assms show ?thesis by (simp add: Fract_def add_rat_def UN_ratrel2)
haftmann@27551
   144
qed
huffman@18913
   145
haftmann@25571
   146
definition
haftmann@35369
   147
  minus_rat_def:
haftmann@27551
   148
  "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel `` {(- fst x, snd x)})"
haftmann@27551
   149
haftmann@35369
   150
lemma minus_rat [simp]: "- Fract a b = Fract (- a) b"
haftmann@27551
   151
proof -
haftmann@27551
   152
  have "(\<lambda>x. ratrel `` {(- fst x, snd x)}) respects ratrel"
haftmann@40819
   153
    by (simp add: congruent_def split_paired_all)
haftmann@27551
   154
  then show ?thesis by (simp add: Fract_def minus_rat_def UN_ratrel)
haftmann@27551
   155
qed
haftmann@27551
   156
haftmann@27652
   157
lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
haftmann@27551
   158
  by (cases "b = 0") (simp_all add: eq_rat)
haftmann@25571
   159
haftmann@25571
   160
definition
haftmann@35369
   161
  diff_rat_def: "q - r = q + - (r::rat)"
huffman@18913
   162
haftmann@27652
   163
lemma diff_rat [simp]:
haftmann@27551
   164
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   165
  shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
haftmann@27652
   166
  using assms by (simp add: diff_rat_def)
haftmann@25571
   167
haftmann@25571
   168
definition
haftmann@35369
   169
  mult_rat_def:
haftmann@27551
   170
  "q * r = Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   171
    ratrel``{(fst x * fst y, snd x * snd y)})"
paulson@14365
   172
haftmann@27652
   173
lemma mult_rat [simp]: "Fract a b * Fract c d = Fract (a * c) (b * d)"
haftmann@27551
   174
proof -
haftmann@27551
   175
  have "(\<lambda>x y. ratrel `` {(fst x * fst y, snd x * snd y)}) respects2 ratrel"
haftmann@27551
   176
    by (rule equiv_ratrel [THEN congruent2_commuteI]) simp_all
haftmann@27551
   177
  then show ?thesis by (simp add: Fract_def mult_rat_def UN_ratrel2)
paulson@14365
   178
qed
paulson@14365
   179
haftmann@27652
   180
lemma mult_rat_cancel:
haftmann@27551
   181
  assumes "c \<noteq> 0"
haftmann@27551
   182
  shows "Fract (c * a) (c * b) = Fract a b"
haftmann@27551
   183
proof -
haftmann@27551
   184
  from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
haftmann@27652
   185
  then show ?thesis by (simp add: mult_rat [symmetric])
haftmann@27551
   186
qed
huffman@27509
   187
huffman@27509
   188
instance proof
chaieb@27668
   189
  fix q r s :: rat show "(q * r) * s = q * (r * s)" 
haftmann@27652
   190
    by (cases q, cases r, cases s) (simp add: eq_rat)
haftmann@27551
   191
next
haftmann@27551
   192
  fix q r :: rat show "q * r = r * q"
haftmann@27652
   193
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   194
next
haftmann@27551
   195
  fix q :: rat show "1 * q = q"
haftmann@27652
   196
    by (cases q) (simp add: One_rat_def eq_rat)
haftmann@27551
   197
next
haftmann@27551
   198
  fix q r s :: rat show "(q + r) + s = q + (r + s)"
nipkow@29667
   199
    by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
haftmann@27551
   200
next
haftmann@27551
   201
  fix q r :: rat show "q + r = r + q"
haftmann@27652
   202
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   203
next
haftmann@27551
   204
  fix q :: rat show "0 + q = q"
haftmann@27652
   205
    by (cases q) (simp add: Zero_rat_def eq_rat)
haftmann@27551
   206
next
haftmann@27551
   207
  fix q :: rat show "- q + q = 0"
haftmann@27652
   208
    by (cases q) (simp add: Zero_rat_def eq_rat)
haftmann@27551
   209
next
haftmann@27551
   210
  fix q r :: rat show "q - r = q + - r"
haftmann@27652
   211
    by (cases q, cases r) (simp add: eq_rat)
haftmann@27551
   212
next
haftmann@27551
   213
  fix q r s :: rat show "(q + r) * s = q * s + r * s"
nipkow@29667
   214
    by (cases q, cases r, cases s) (simp add: eq_rat algebra_simps)
haftmann@27551
   215
next
haftmann@27551
   216
  show "(0::rat) \<noteq> 1" by (simp add: Zero_rat_def One_rat_def eq_rat)
huffman@27509
   217
qed
huffman@27509
   218
huffman@27509
   219
end
huffman@27509
   220
haftmann@27551
   221
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
haftmann@27652
   222
  by (induct k) (simp_all add: Zero_rat_def One_rat_def)
haftmann@27551
   223
haftmann@27551
   224
lemma of_int_rat: "of_int k = Fract k 1"
haftmann@27652
   225
  by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
haftmann@27551
   226
haftmann@27551
   227
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
haftmann@27551
   228
  by (rule of_nat_rat [symmetric])
haftmann@27551
   229
haftmann@27551
   230
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
haftmann@27551
   231
  by (rule of_int_rat [symmetric])
haftmann@27551
   232
haftmann@35369
   233
lemma rat_number_collapse:
haftmann@27551
   234
  "Fract 0 k = 0"
haftmann@27551
   235
  "Fract 1 1 = 1"
huffman@47108
   236
  "Fract (numeral w) 1 = numeral w"
huffman@47108
   237
  "Fract (neg_numeral w) 1 = neg_numeral w"
haftmann@27551
   238
  "Fract k 0 = 0"
huffman@47108
   239
  using Fract_of_int_eq [of "numeral w"]
huffman@47108
   240
  using Fract_of_int_eq [of "neg_numeral w"]
huffman@47108
   241
  by (simp_all add: Zero_rat_def One_rat_def eq_rat)
haftmann@27551
   242
huffman@47108
   243
lemma rat_number_expand:
haftmann@27551
   244
  "0 = Fract 0 1"
haftmann@27551
   245
  "1 = Fract 1 1"
huffman@47108
   246
  "numeral k = Fract (numeral k) 1"
huffman@47108
   247
  "neg_numeral k = Fract (neg_numeral k) 1"
haftmann@27551
   248
  by (simp_all add: rat_number_collapse)
haftmann@27551
   249
haftmann@27551
   250
lemma Rat_cases_nonzero [case_names Fract 0]:
haftmann@35369
   251
  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
haftmann@27551
   252
  assumes 0: "q = 0 \<Longrightarrow> C"
haftmann@27551
   253
  shows C
haftmann@27551
   254
proof (cases "q = 0")
haftmann@27551
   255
  case True then show C using 0 by auto
haftmann@27551
   256
next
haftmann@27551
   257
  case False
haftmann@35369
   258
  then obtain a b where "q = Fract a b" and "b > 0" and "coprime a b" by (cases q) auto
haftmann@27551
   259
  moreover with False have "0 \<noteq> Fract a b" by simp
haftmann@35369
   260
  with `b > 0` have "a \<noteq> 0" by (simp add: Zero_rat_def eq_rat)
haftmann@35369
   261
  with Fract `q = Fract a b` `b > 0` `coprime a b` show C by blast
haftmann@27551
   262
qed
haftmann@27551
   263
nipkow@33805
   264
subsubsection {* Function @{text normalize} *}
nipkow@33805
   265
haftmann@35369
   266
lemma Fract_coprime: "Fract (a div gcd a b) (b div gcd a b) = Fract a b"
haftmann@35369
   267
proof (cases "b = 0")
haftmann@35369
   268
  case True then show ?thesis by (simp add: eq_rat)
haftmann@35369
   269
next
haftmann@35369
   270
  case False
haftmann@35369
   271
  moreover have "b div gcd a b * gcd a b = b"
haftmann@35369
   272
    by (rule dvd_div_mult_self) simp
haftmann@35369
   273
  ultimately have "b div gcd a b \<noteq> 0" by auto
haftmann@35369
   274
  with False show ?thesis by (simp add: eq_rat dvd_div_mult mult_commute [of a])
haftmann@35369
   275
qed
nipkow@33805
   276
haftmann@35369
   277
definition normalize :: "int \<times> int \<Rightarrow> int \<times> int" where
haftmann@35369
   278
  "normalize p = (if snd p > 0 then (let a = gcd (fst p) (snd p) in (fst p div a, snd p div a))
haftmann@35369
   279
    else if snd p = 0 then (0, 1)
haftmann@35369
   280
    else (let a = - gcd (fst p) (snd p) in (fst p div a, snd p div a)))"
haftmann@35369
   281
haftmann@35369
   282
lemma normalize_crossproduct:
haftmann@35369
   283
  assumes "q \<noteq> 0" "s \<noteq> 0"
haftmann@35369
   284
  assumes "normalize (p, q) = normalize (r, s)"
haftmann@35369
   285
  shows "p * s = r * q"
haftmann@35369
   286
proof -
haftmann@35369
   287
  have aux: "p * gcd r s = sgn (q * s) * r * gcd p q \<Longrightarrow> q * gcd r s = sgn (q * s) * s * gcd p q \<Longrightarrow> p * s = q * r"
haftmann@35369
   288
  proof -
haftmann@35369
   289
    assume "p * gcd r s = sgn (q * s) * r * gcd p q" and "q * gcd r s = sgn (q * s) * s * gcd p q"
haftmann@35369
   290
    then have "(p * gcd r s) * (sgn (q * s) * s * gcd p q) = (q * gcd r s) * (sgn (q * s) * r * gcd p q)" by simp
haftmann@35369
   291
    with assms show "p * s = q * r" by (auto simp add: mult_ac sgn_times sgn_0_0)
haftmann@35369
   292
  qed
haftmann@35369
   293
  from assms show ?thesis
haftmann@35369
   294
    by (auto simp add: normalize_def Let_def dvd_div_div_eq_mult mult_commute sgn_times split: if_splits intro: aux)
nipkow@33805
   295
qed
nipkow@33805
   296
haftmann@35369
   297
lemma normalize_eq: "normalize (a, b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
haftmann@35369
   298
  by (auto simp add: normalize_def Let_def Fract_coprime dvd_div_neg rat_number_collapse
haftmann@35369
   299
    split:split_if_asm)
haftmann@35369
   300
haftmann@35369
   301
lemma normalize_denom_pos: "normalize r = (p, q) \<Longrightarrow> q > 0"
haftmann@35369
   302
  by (auto simp add: normalize_def Let_def dvd_div_neg pos_imp_zdiv_neg_iff nonneg1_imp_zdiv_pos_iff
haftmann@35369
   303
    split:split_if_asm)
haftmann@35369
   304
haftmann@35369
   305
lemma normalize_coprime: "normalize r = (p, q) \<Longrightarrow> coprime p q"
haftmann@35369
   306
  by (auto simp add: normalize_def Let_def dvd_div_neg div_gcd_coprime_int
haftmann@35369
   307
    split:split_if_asm)
haftmann@35369
   308
haftmann@35369
   309
lemma normalize_stable [simp]:
haftmann@35369
   310
  "q > 0 \<Longrightarrow> coprime p q \<Longrightarrow> normalize (p, q) = (p, q)"
haftmann@35369
   311
  by (simp add: normalize_def)
haftmann@35369
   312
haftmann@35369
   313
lemma normalize_denom_zero [simp]:
haftmann@35369
   314
  "normalize (p, 0) = (0, 1)"
haftmann@35369
   315
  by (simp add: normalize_def)
haftmann@35369
   316
haftmann@35369
   317
lemma normalize_negative [simp]:
haftmann@35369
   318
  "q < 0 \<Longrightarrow> normalize (p, q) = normalize (- p, - q)"
haftmann@35369
   319
  by (simp add: normalize_def Let_def dvd_div_neg dvd_neg_div)
haftmann@35369
   320
haftmann@35369
   321
text{*
haftmann@35369
   322
  Decompose a fraction into normalized, i.e. coprime numerator and denominator:
haftmann@35369
   323
*}
haftmann@35369
   324
haftmann@35369
   325
definition quotient_of :: "rat \<Rightarrow> int \<times> int" where
haftmann@35369
   326
  "quotient_of x = (THE pair. x = Fract (fst pair) (snd pair) &
haftmann@35369
   327
                   snd pair > 0 & coprime (fst pair) (snd pair))"
haftmann@35369
   328
haftmann@35369
   329
lemma quotient_of_unique:
haftmann@35369
   330
  "\<exists>!p. r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
haftmann@35369
   331
proof (cases r)
haftmann@35369
   332
  case (Fract a b)
haftmann@35369
   333
  then have "r = Fract (fst (a, b)) (snd (a, b)) \<and> snd (a, b) > 0 \<and> coprime (fst (a, b)) (snd (a, b))" by auto
haftmann@35369
   334
  then show ?thesis proof (rule ex1I)
haftmann@35369
   335
    fix p
haftmann@35369
   336
    obtain c d :: int where p: "p = (c, d)" by (cases p)
haftmann@35369
   337
    assume "r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
haftmann@35369
   338
    with p have Fract': "r = Fract c d" "d > 0" "coprime c d" by simp_all
haftmann@35369
   339
    have "c = a \<and> d = b"
haftmann@35369
   340
    proof (cases "a = 0")
haftmann@35369
   341
      case True with Fract Fract' show ?thesis by (simp add: eq_rat)
haftmann@35369
   342
    next
haftmann@35369
   343
      case False
haftmann@35369
   344
      with Fract Fract' have *: "c * b = a * d" and "c \<noteq> 0" by (auto simp add: eq_rat)
haftmann@35369
   345
      then have "c * b > 0 \<longleftrightarrow> a * d > 0" by auto
haftmann@35369
   346
      with `b > 0` `d > 0` have "a > 0 \<longleftrightarrow> c > 0" by (simp add: zero_less_mult_iff)
haftmann@35369
   347
      with `a \<noteq> 0` `c \<noteq> 0` have sgn: "sgn a = sgn c" by (auto simp add: not_less)
haftmann@35369
   348
      from `coprime a b` `coprime c d` have "\<bar>a\<bar> * \<bar>d\<bar> = \<bar>c\<bar> * \<bar>b\<bar> \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> \<bar>d\<bar> = \<bar>b\<bar>"
haftmann@35369
   349
        by (simp add: coprime_crossproduct_int)
haftmann@35369
   350
      with `b > 0` `d > 0` have "\<bar>a\<bar> * d = \<bar>c\<bar> * b \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> d = b" by simp
haftmann@35369
   351
      then have "a * sgn a * d = c * sgn c * b \<longleftrightarrow> a * sgn a = c * sgn c \<and> d = b" by (simp add: abs_sgn)
haftmann@35369
   352
      with sgn * show ?thesis by (auto simp add: sgn_0_0)
nipkow@33805
   353
    qed
haftmann@35369
   354
    with p show "p = (a, b)" by simp
nipkow@33805
   355
  qed
nipkow@33805
   356
qed
nipkow@33805
   357
haftmann@35369
   358
lemma quotient_of_Fract [code]:
haftmann@35369
   359
  "quotient_of (Fract a b) = normalize (a, b)"
haftmann@35369
   360
proof -
haftmann@35369
   361
  have "Fract a b = Fract (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?Fract)
haftmann@35369
   362
    by (rule sym) (auto intro: normalize_eq)
haftmann@35369
   363
  moreover have "0 < snd (normalize (a, b))" (is ?denom_pos) 
haftmann@35369
   364
    by (cases "normalize (a, b)") (rule normalize_denom_pos, simp)
haftmann@35369
   365
  moreover have "coprime (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?coprime)
haftmann@35369
   366
    by (rule normalize_coprime) simp
haftmann@35369
   367
  ultimately have "?Fract \<and> ?denom_pos \<and> ?coprime" by blast
haftmann@35369
   368
  with quotient_of_unique have
haftmann@35369
   369
    "(THE p. Fract a b = Fract (fst p) (snd p) \<and> 0 < snd p \<and> coprime (fst p) (snd p)) = normalize (a, b)"
haftmann@35369
   370
    by (rule the1_equality)
haftmann@35369
   371
  then show ?thesis by (simp add: quotient_of_def)
haftmann@35369
   372
qed
haftmann@35369
   373
haftmann@35369
   374
lemma quotient_of_number [simp]:
haftmann@35369
   375
  "quotient_of 0 = (0, 1)"
haftmann@35369
   376
  "quotient_of 1 = (1, 1)"
huffman@47108
   377
  "quotient_of (numeral k) = (numeral k, 1)"
huffman@47108
   378
  "quotient_of (neg_numeral k) = (neg_numeral k, 1)"
haftmann@35369
   379
  by (simp_all add: rat_number_expand quotient_of_Fract)
nipkow@33805
   380
haftmann@35369
   381
lemma quotient_of_eq: "quotient_of (Fract a b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
haftmann@35369
   382
  by (simp add: quotient_of_Fract normalize_eq)
haftmann@35369
   383
haftmann@35369
   384
lemma quotient_of_denom_pos: "quotient_of r = (p, q) \<Longrightarrow> q > 0"
haftmann@35369
   385
  by (cases r) (simp add: quotient_of_Fract normalize_denom_pos)
haftmann@35369
   386
haftmann@35369
   387
lemma quotient_of_coprime: "quotient_of r = (p, q) \<Longrightarrow> coprime p q"
haftmann@35369
   388
  by (cases r) (simp add: quotient_of_Fract normalize_coprime)
nipkow@33805
   389
haftmann@35369
   390
lemma quotient_of_inject:
haftmann@35369
   391
  assumes "quotient_of a = quotient_of b"
haftmann@35369
   392
  shows "a = b"
haftmann@35369
   393
proof -
haftmann@35369
   394
  obtain p q r s where a: "a = Fract p q"
haftmann@35369
   395
    and b: "b = Fract r s"
haftmann@35369
   396
    and "q > 0" and "s > 0" by (cases a, cases b)
haftmann@35369
   397
  with assms show ?thesis by (simp add: eq_rat quotient_of_Fract normalize_crossproduct)
haftmann@35369
   398
qed
haftmann@35369
   399
haftmann@35369
   400
lemma quotient_of_inject_eq:
haftmann@35369
   401
  "quotient_of a = quotient_of b \<longleftrightarrow> a = b"
haftmann@35369
   402
  by (auto simp add: quotient_of_inject)
nipkow@33805
   403
haftmann@27551
   404
haftmann@27551
   405
subsubsection {* The field of rational numbers *}
haftmann@27551
   406
haftmann@36409
   407
instantiation rat :: field_inverse_zero
haftmann@27551
   408
begin
haftmann@27551
   409
haftmann@27551
   410
definition
haftmann@35369
   411
  inverse_rat_def:
haftmann@27551
   412
  "inverse q = Abs_Rat (\<Union>x \<in> Rep_Rat q.
haftmann@27551
   413
     ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
haftmann@27551
   414
haftmann@27652
   415
lemma inverse_rat [simp]: "inverse (Fract a b) = Fract b a"
haftmann@27551
   416
proof -
haftmann@27551
   417
  have "(\<lambda>x. ratrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)}) respects ratrel"
haftmann@27551
   418
    by (auto simp add: congruent_def mult_commute)
haftmann@27551
   419
  then show ?thesis by (simp add: Fract_def inverse_rat_def UN_ratrel)
huffman@27509
   420
qed
huffman@27509
   421
haftmann@27551
   422
definition
haftmann@35369
   423
  divide_rat_def: "q / r = q * inverse (r::rat)"
haftmann@27551
   424
haftmann@27652
   425
lemma divide_rat [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
haftmann@27652
   426
  by (simp add: divide_rat_def)
haftmann@27551
   427
haftmann@27551
   428
instance proof
haftmann@27551
   429
  fix q :: rat
haftmann@27551
   430
  assume "q \<noteq> 0"
haftmann@27551
   431
  then show "inverse q * q = 1" by (cases q rule: Rat_cases_nonzero)
huffman@35216
   432
   (simp_all add: rat_number_expand eq_rat)
haftmann@27551
   433
next
haftmann@27551
   434
  fix q r :: rat
haftmann@27551
   435
  show "q / r = q * inverse r" by (simp add: divide_rat_def)
haftmann@36415
   436
next
haftmann@36415
   437
  show "inverse 0 = (0::rat)" by (simp add: rat_number_expand, simp add: rat_number_collapse)
haftmann@36415
   438
qed
haftmann@27551
   439
haftmann@27551
   440
end
haftmann@27551
   441
haftmann@27551
   442
haftmann@27551
   443
subsubsection {* Various *}
haftmann@27551
   444
haftmann@27551
   445
lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
haftmann@27652
   446
  by (simp add: Fract_of_int_eq [symmetric])
haftmann@27551
   447
huffman@47108
   448
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
huffman@47108
   449
  by (simp add: rat_number_expand)
haftmann@27551
   450
haftmann@27551
   451
haftmann@27551
   452
subsubsection {* The ordered field of rational numbers *}
huffman@27509
   453
huffman@27509
   454
instantiation rat :: linorder
huffman@27509
   455
begin
huffman@27509
   456
huffman@27509
   457
definition
haftmann@35369
   458
  le_rat_def:
haftmann@39910
   459
   "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@27551
   460
      {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})"
haftmann@27551
   461
haftmann@27652
   462
lemma le_rat [simp]:
haftmann@27551
   463
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   464
  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27551
   465
proof -
haftmann@27551
   466
  have "(\<lambda>x y. {(fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)})
haftmann@27551
   467
    respects2 ratrel"
haftmann@27551
   468
  proof (clarsimp simp add: congruent2_def)
haftmann@27551
   469
    fix a b a' b' c d c' d'::int
haftmann@27551
   470
    assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
haftmann@27551
   471
    assume eq1: "a * b' = a' * b"
haftmann@27551
   472
    assume eq2: "c * d' = c' * d"
haftmann@27551
   473
haftmann@27551
   474
    let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
haftmann@27551
   475
    {
haftmann@27551
   476
      fix a b c d x :: int assume x: "x \<noteq> 0"
haftmann@27551
   477
      have "?le a b c d = ?le (a * x) (b * x) c d"
haftmann@27551
   478
      proof -
haftmann@27551
   479
        from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
haftmann@27551
   480
        hence "?le a b c d =
haftmann@27551
   481
            ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
haftmann@27551
   482
          by (simp add: mult_le_cancel_right)
haftmann@27551
   483
        also have "... = ?le (a * x) (b * x) c d"
haftmann@27551
   484
          by (simp add: mult_ac)
haftmann@27551
   485
        finally show ?thesis .
haftmann@27551
   486
      qed
haftmann@27551
   487
    } note le_factor = this
haftmann@27551
   488
haftmann@27551
   489
    let ?D = "b * d" and ?D' = "b' * d'"
haftmann@27551
   490
    from neq have D: "?D \<noteq> 0" by simp
haftmann@27551
   491
    from neq have "?D' \<noteq> 0" by simp
haftmann@27551
   492
    hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
haftmann@27551
   493
      by (rule le_factor)
chaieb@27668
   494
    also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')" 
haftmann@27551
   495
      by (simp add: mult_ac)
haftmann@27551
   496
    also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
haftmann@27551
   497
      by (simp only: eq1 eq2)
haftmann@27551
   498
    also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
haftmann@27551
   499
      by (simp add: mult_ac)
haftmann@27551
   500
    also from D have "... = ?le a' b' c' d'"
haftmann@27551
   501
      by (rule le_factor [symmetric])
haftmann@27551
   502
    finally show "?le a b c d = ?le a' b' c' d'" .
haftmann@27551
   503
  qed
haftmann@27551
   504
  with assms show ?thesis by (simp add: Fract_def le_rat_def UN_ratrel2)
haftmann@27551
   505
qed
huffman@27509
   506
huffman@27509
   507
definition
haftmann@35369
   508
  less_rat_def: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
huffman@27509
   509
haftmann@27652
   510
lemma less_rat [simp]:
haftmann@27551
   511
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   512
  shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
haftmann@27652
   513
  using assms by (simp add: less_rat_def eq_rat order_less_le)
huffman@27509
   514
huffman@27509
   515
instance proof
paulson@14365
   516
  fix q r s :: rat
paulson@14365
   517
  {
paulson@14365
   518
    assume "q \<le> r" and "r \<le> s"
haftmann@35369
   519
    then show "q \<le> s" 
haftmann@35369
   520
    proof (induct q, induct r, induct s)
paulson@14365
   521
      fix a b c d e f :: int
haftmann@35369
   522
      assume neq: "b > 0"  "d > 0"  "f > 0"
paulson@14365
   523
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365
   524
      show "Fract a b \<le> Fract e f"
paulson@14365
   525
      proof -
paulson@14365
   526
        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365
   527
          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365
   528
        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365
   529
        proof -
paulson@14365
   530
          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   531
            by simp
paulson@14365
   532
          with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   533
        qed
chaieb@27668
   534
        also have "... = (c * f) * (d * f) * (b * b)" by algebra
paulson@14365
   535
        also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365
   536
        proof -
paulson@14365
   537
          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
haftmann@27652
   538
            by simp
paulson@14365
   539
          with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   540
        qed
paulson@14365
   541
        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365
   542
          by (simp only: mult_ac)
paulson@14365
   543
        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365
   544
          by (simp add: mult_le_cancel_right)
haftmann@27652
   545
        with neq show ?thesis by simp
paulson@14365
   546
      qed
paulson@14365
   547
    qed
paulson@14365
   548
  next
paulson@14365
   549
    assume "q \<le> r" and "r \<le> q"
haftmann@35369
   550
    then show "q = r"
haftmann@35369
   551
    proof (induct q, induct r)
paulson@14365
   552
      fix a b c d :: int
haftmann@35369
   553
      assume neq: "b > 0"  "d > 0"
paulson@14365
   554
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365
   555
      show "Fract a b = Fract c d"
paulson@14365
   556
      proof -
paulson@14365
   557
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   558
          by simp
paulson@14365
   559
        also have "... \<le> (a * d) * (b * d)"
paulson@14365
   560
        proof -
paulson@14365
   561
          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
haftmann@27652
   562
            by simp
paulson@14365
   563
          thus ?thesis by (simp only: mult_ac)
paulson@14365
   564
        qed
paulson@14365
   565
        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365
   566
        moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365
   567
        ultimately have "a * d = c * b" by simp
paulson@14365
   568
        with neq show ?thesis by (simp add: eq_rat)
paulson@14365
   569
      qed
paulson@14365
   570
    qed
paulson@14365
   571
  next
paulson@14365
   572
    show "q \<le> q"
haftmann@27652
   573
      by (induct q) simp
haftmann@27682
   574
    show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
haftmann@27682
   575
      by (induct q, induct r) (auto simp add: le_less mult_commute)
paulson@14365
   576
    show "q \<le> r \<or> r \<le> q"
huffman@18913
   577
      by (induct q, induct r)
haftmann@27652
   578
         (simp add: mult_commute, rule linorder_linear)
paulson@14365
   579
  }
paulson@14365
   580
qed
paulson@14365
   581
huffman@27509
   582
end
huffman@27509
   583
haftmann@27551
   584
instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
haftmann@25571
   585
begin
haftmann@25571
   586
haftmann@25571
   587
definition
haftmann@35369
   588
  abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
haftmann@27551
   589
haftmann@27652
   590
lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
huffman@35216
   591
  by (auto simp add: abs_rat_def zabs_def Zero_rat_def not_less le_less eq_rat zero_less_mult_iff)
haftmann@27551
   592
haftmann@27551
   593
definition
haftmann@35369
   594
  sgn_rat_def: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
haftmann@27551
   595
haftmann@27652
   596
lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
haftmann@27551
   597
  unfolding Fract_of_int_eq
haftmann@27652
   598
  by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
haftmann@27551
   599
    (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
haftmann@27551
   600
haftmann@27551
   601
definition
haftmann@25571
   602
  "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
haftmann@25571
   603
haftmann@25571
   604
definition
haftmann@25571
   605
  "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
haftmann@25571
   606
haftmann@27551
   607
instance by intro_classes
haftmann@27551
   608
  (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
haftmann@22456
   609
haftmann@25571
   610
end
haftmann@25571
   611
haftmann@36409
   612
instance rat :: linordered_field_inverse_zero
haftmann@27551
   613
proof
paulson@14365
   614
  fix q r s :: rat
paulson@14365
   615
  show "q \<le> r ==> s + q \<le> s + r"
paulson@14365
   616
  proof (induct q, induct r, induct s)
paulson@14365
   617
    fix a b c d e f :: int
haftmann@35369
   618
    assume neq: "b > 0"  "d > 0"  "f > 0"
paulson@14365
   619
    assume le: "Fract a b \<le> Fract c d"
paulson@14365
   620
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365
   621
    proof -
paulson@14365
   622
      let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365
   623
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   624
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
haftmann@27652
   625
        by simp
paulson@14365
   626
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365
   627
        by (simp add: mult_le_cancel_right)
haftmann@27652
   628
      with neq show ?thesis by (simp add: mult_ac int_distrib)
paulson@14365
   629
    qed
paulson@14365
   630
  qed
paulson@14365
   631
  show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365
   632
  proof (induct q, induct r, induct s)
paulson@14365
   633
    fix a b c d e f :: int
haftmann@35369
   634
    assume neq: "b > 0"  "d > 0"  "f > 0"
paulson@14365
   635
    assume le: "Fract a b < Fract c d"
paulson@14365
   636
    assume gt: "0 < Fract e f"
paulson@14365
   637
    show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365
   638
    proof -
paulson@14365
   639
      let ?E = "e * f" and ?F = "f * f"
paulson@14365
   640
      from neq gt have "0 < ?E"
haftmann@27652
   641
        by (auto simp add: Zero_rat_def order_less_le eq_rat)
paulson@14365
   642
      moreover from neq have "0 < ?F"
paulson@14365
   643
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   644
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
haftmann@27652
   645
        by simp
paulson@14365
   646
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365
   647
        by (simp add: mult_less_cancel_right)
paulson@14365
   648
      with neq show ?thesis
haftmann@27652
   649
        by (simp add: mult_ac)
paulson@14365
   650
    qed
paulson@14365
   651
  qed
haftmann@27551
   652
qed auto
paulson@14365
   653
haftmann@27551
   654
lemma Rat_induct_pos [case_names Fract, induct type: rat]:
haftmann@27551
   655
  assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
haftmann@27551
   656
  shows "P q"
paulson@14365
   657
proof (cases q)
haftmann@27551
   658
  have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
paulson@14365
   659
  proof -
paulson@14365
   660
    fix a::int and b::int
paulson@14365
   661
    assume b: "b < 0"
paulson@14365
   662
    hence "0 < -b" by simp
paulson@14365
   663
    hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365
   664
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   665
  qed
paulson@14365
   666
  case (Fract a b)
paulson@14365
   667
  thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365
   668
qed
paulson@14365
   669
paulson@14365
   670
lemma zero_less_Fract_iff:
huffman@30095
   671
  "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
huffman@30095
   672
  by (simp add: Zero_rat_def zero_less_mult_iff)
huffman@30095
   673
huffman@30095
   674
lemma Fract_less_zero_iff:
huffman@30095
   675
  "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
huffman@30095
   676
  by (simp add: Zero_rat_def mult_less_0_iff)
huffman@30095
   677
huffman@30095
   678
lemma zero_le_Fract_iff:
huffman@30095
   679
  "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
huffman@30095
   680
  by (simp add: Zero_rat_def zero_le_mult_iff)
huffman@30095
   681
huffman@30095
   682
lemma Fract_le_zero_iff:
huffman@30095
   683
  "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
huffman@30095
   684
  by (simp add: Zero_rat_def mult_le_0_iff)
huffman@30095
   685
huffman@30095
   686
lemma one_less_Fract_iff:
huffman@30095
   687
  "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
huffman@30095
   688
  by (simp add: One_rat_def mult_less_cancel_right_disj)
huffman@30095
   689
huffman@30095
   690
lemma Fract_less_one_iff:
huffman@30095
   691
  "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
huffman@30095
   692
  by (simp add: One_rat_def mult_less_cancel_right_disj)
huffman@30095
   693
huffman@30095
   694
lemma one_le_Fract_iff:
huffman@30095
   695
  "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
huffman@30095
   696
  by (simp add: One_rat_def mult_le_cancel_right)
huffman@30095
   697
huffman@30095
   698
lemma Fract_le_one_iff:
huffman@30095
   699
  "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
huffman@30095
   700
  by (simp add: One_rat_def mult_le_cancel_right)
paulson@14365
   701
paulson@14378
   702
huffman@30097
   703
subsubsection {* Rationals are an Archimedean field *}
huffman@30097
   704
huffman@30097
   705
lemma rat_floor_lemma:
huffman@30097
   706
  shows "of_int (a div b) \<le> Fract a b \<and> Fract a b < of_int (a div b + 1)"
huffman@30097
   707
proof -
huffman@30097
   708
  have "Fract a b = of_int (a div b) + Fract (a mod b) b"
huffman@35293
   709
    by (cases "b = 0", simp, simp add: of_int_rat)
huffman@30097
   710
  moreover have "0 \<le> Fract (a mod b) b \<and> Fract (a mod b) b < 1"
huffman@35293
   711
    unfolding Fract_of_int_quotient
haftmann@36409
   712
    by (rule linorder_cases [of b 0]) (simp add: divide_nonpos_neg, simp, simp add: divide_nonneg_pos)
huffman@30097
   713
  ultimately show ?thesis by simp
huffman@30097
   714
qed
huffman@30097
   715
huffman@30097
   716
instance rat :: archimedean_field
huffman@30097
   717
proof
huffman@30097
   718
  fix r :: rat
huffman@30097
   719
  show "\<exists>z. r \<le> of_int z"
huffman@30097
   720
  proof (induct r)
huffman@30097
   721
    case (Fract a b)
huffman@35293
   722
    have "Fract a b \<le> of_int (a div b + 1)"
huffman@35293
   723
      using rat_floor_lemma [of a b] by simp
huffman@30097
   724
    then show "\<exists>z. Fract a b \<le> of_int z" ..
huffman@30097
   725
  qed
huffman@30097
   726
qed
huffman@30097
   727
bulwahn@43732
   728
instantiation rat :: floor_ceiling
bulwahn@43732
   729
begin
bulwahn@43732
   730
bulwahn@43732
   731
definition [code del]:
bulwahn@43732
   732
  "floor (x::rat) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
bulwahn@43732
   733
bulwahn@43732
   734
instance proof
bulwahn@43732
   735
  fix x :: rat
bulwahn@43732
   736
  show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
bulwahn@43732
   737
    unfolding floor_rat_def using floor_exists1 by (rule theI')
bulwahn@43732
   738
qed
bulwahn@43732
   739
bulwahn@43732
   740
end
bulwahn@43732
   741
huffman@35293
   742
lemma floor_Fract: "floor (Fract a b) = a div b"
huffman@35293
   743
  using rat_floor_lemma [of a b]
huffman@30097
   744
  by (simp add: floor_unique)
huffman@30097
   745
huffman@30097
   746
haftmann@31100
   747
subsection {* Linear arithmetic setup *}
paulson@14387
   748
haftmann@31100
   749
declaration {*
haftmann@31100
   750
  K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
haftmann@31100
   751
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
haftmann@31100
   752
  #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_int_eq_iff} RS iffD2]
haftmann@31100
   753
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
haftmann@31100
   754
  #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
haftmann@31100
   755
      @{thm True_implies_equals},
huffman@47108
   756
      read_instantiate @{context} [(("a", 0), "(numeral ?v)")] @{thm right_distrib},
huffman@47108
   757
      read_instantiate @{context} [(("a", 0), "(neg_numeral ?v)")] @{thm right_distrib},
haftmann@31100
   758
      @{thm divide_1}, @{thm divide_zero_left},
haftmann@31100
   759
      @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
haftmann@31100
   760
      @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
haftmann@31100
   761
      @{thm of_int_minus}, @{thm of_int_diff},
haftmann@31100
   762
      @{thm of_int_of_nat_eq}]
haftmann@31100
   763
  #> Lin_Arith.add_simprocs Numeral_Simprocs.field_cancel_numeral_factors
haftmann@31100
   764
  #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat => rat"})
haftmann@31100
   765
  #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int => rat"}))
haftmann@31100
   766
*}
paulson@14387
   767
huffman@23342
   768
huffman@23342
   769
subsection {* Embedding from Rationals to other Fields *}
huffman@23342
   770
haftmann@24198
   771
class field_char_0 = field + ring_char_0
huffman@23342
   772
haftmann@35028
   773
subclass (in linordered_field) field_char_0 ..
huffman@23342
   774
haftmann@27551
   775
context field_char_0
haftmann@27551
   776
begin
haftmann@27551
   777
haftmann@27551
   778
definition of_rat :: "rat \<Rightarrow> 'a" where
haftmann@39910
   779
  "of_rat q = the_elem (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
huffman@23342
   780
haftmann@27551
   781
end
haftmann@27551
   782
huffman@23342
   783
lemma of_rat_congruent:
haftmann@27551
   784
  "(\<lambda>(a, b). {of_int a / of_int b :: 'a::field_char_0}) respects ratrel"
haftmann@40816
   785
apply (rule congruentI)
huffman@23342
   786
apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23342
   787
apply (simp only: of_int_mult [symmetric])
huffman@23342
   788
done
huffman@23342
   789
haftmann@27551
   790
lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
haftmann@27551
   791
  unfolding Fract_def of_rat_def by (simp add: UN_ratrel of_rat_congruent)
huffman@23342
   792
huffman@23342
   793
lemma of_rat_0 [simp]: "of_rat 0 = 0"
huffman@23342
   794
by (simp add: Zero_rat_def of_rat_rat)
huffman@23342
   795
huffman@23342
   796
lemma of_rat_1 [simp]: "of_rat 1 = 1"
huffman@23342
   797
by (simp add: One_rat_def of_rat_rat)
huffman@23342
   798
huffman@23342
   799
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
haftmann@27652
   800
by (induct a, induct b, simp add: of_rat_rat add_frac_eq)
huffman@23342
   801
huffman@23343
   802
lemma of_rat_minus: "of_rat (- a) = - of_rat a"
haftmann@27652
   803
by (induct a, simp add: of_rat_rat)
huffman@23343
   804
huffman@23343
   805
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
huffman@23343
   806
by (simp only: diff_minus of_rat_add of_rat_minus)
huffman@23343
   807
huffman@23342
   808
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
haftmann@27652
   809
apply (induct a, induct b, simp add: of_rat_rat)
huffman@23342
   810
apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
huffman@23342
   811
done
huffman@23342
   812
huffman@23342
   813
lemma nonzero_of_rat_inverse:
huffman@23342
   814
  "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
huffman@23343
   815
apply (rule inverse_unique [symmetric])
huffman@23343
   816
apply (simp add: of_rat_mult [symmetric])
huffman@23342
   817
done
huffman@23342
   818
huffman@23342
   819
lemma of_rat_inverse:
haftmann@36409
   820
  "(of_rat (inverse a)::'a::{field_char_0, field_inverse_zero}) =
huffman@23342
   821
   inverse (of_rat a)"
huffman@23342
   822
by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
huffman@23342
   823
huffman@23342
   824
lemma nonzero_of_rat_divide:
huffman@23342
   825
  "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
huffman@23342
   826
by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
huffman@23342
   827
huffman@23342
   828
lemma of_rat_divide:
haftmann@36409
   829
  "(of_rat (a / b)::'a::{field_char_0, field_inverse_zero})
huffman@23342
   830
   = of_rat a / of_rat b"
haftmann@27652
   831
by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
huffman@23342
   832
huffman@23343
   833
lemma of_rat_power:
haftmann@31017
   834
  "(of_rat (a ^ n)::'a::field_char_0) = of_rat a ^ n"
huffman@30273
   835
by (induct n) (simp_all add: of_rat_mult)
huffman@23343
   836
huffman@23343
   837
lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
huffman@23343
   838
apply (induct a, induct b)
huffman@23343
   839
apply (simp add: of_rat_rat eq_rat)
huffman@23343
   840
apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23343
   841
apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
huffman@23343
   842
done
huffman@23343
   843
haftmann@27652
   844
lemma of_rat_less:
haftmann@35028
   845
  "(of_rat r :: 'a::linordered_field) < of_rat s \<longleftrightarrow> r < s"
haftmann@27652
   846
proof (induct r, induct s)
haftmann@27652
   847
  fix a b c d :: int
haftmann@27652
   848
  assume not_zero: "b > 0" "d > 0"
haftmann@27652
   849
  then have "b * d > 0" by (rule mult_pos_pos)
haftmann@27652
   850
  have of_int_divide_less_eq:
haftmann@27652
   851
    "(of_int a :: 'a) / of_int b < of_int c / of_int d
haftmann@27652
   852
      \<longleftrightarrow> (of_int a :: 'a) * of_int d < of_int c * of_int b"
haftmann@27652
   853
    using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
haftmann@35028
   854
  show "(of_rat (Fract a b) :: 'a::linordered_field) < of_rat (Fract c d)
haftmann@27652
   855
    \<longleftrightarrow> Fract a b < Fract c d"
haftmann@27652
   856
    using not_zero `b * d > 0`
haftmann@27652
   857
    by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
haftmann@27652
   858
qed
haftmann@27652
   859
haftmann@27652
   860
lemma of_rat_less_eq:
haftmann@35028
   861
  "(of_rat r :: 'a::linordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
haftmann@27652
   862
  unfolding le_less by (auto simp add: of_rat_less)
haftmann@27652
   863
huffman@23343
   864
lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
huffman@23343
   865
haftmann@27652
   866
lemma of_rat_eq_id [simp]: "of_rat = id"
huffman@23343
   867
proof
huffman@23343
   868
  fix a
huffman@23343
   869
  show "of_rat a = id a"
huffman@23343
   870
  by (induct a)
haftmann@27652
   871
     (simp add: of_rat_rat Fract_of_int_eq [symmetric])
huffman@23343
   872
qed
huffman@23343
   873
huffman@23343
   874
text{*Collapse nested embeddings*}
huffman@23343
   875
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
huffman@23343
   876
by (induct n) (simp_all add: of_rat_add)
huffman@23343
   877
huffman@23343
   878
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
haftmann@27652
   879
by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
huffman@23343
   880
huffman@47108
   881
lemma of_rat_numeral_eq [simp]:
huffman@47108
   882
  "of_rat (numeral w) = numeral w"
huffman@47108
   883
using of_rat_of_int_eq [of "numeral w"] by simp
huffman@47108
   884
huffman@47108
   885
lemma of_rat_neg_numeral_eq [simp]:
huffman@47108
   886
  "of_rat (neg_numeral w) = neg_numeral w"
huffman@47108
   887
using of_rat_of_int_eq [of "neg_numeral w"] by simp
huffman@23343
   888
haftmann@23879
   889
lemmas zero_rat = Zero_rat_def
haftmann@23879
   890
lemmas one_rat = One_rat_def
haftmann@23879
   891
haftmann@24198
   892
abbreviation
haftmann@24198
   893
  rat_of_nat :: "nat \<Rightarrow> rat"
haftmann@24198
   894
where
haftmann@24198
   895
  "rat_of_nat \<equiv> of_nat"
haftmann@24198
   896
haftmann@24198
   897
abbreviation
haftmann@24198
   898
  rat_of_int :: "int \<Rightarrow> rat"
haftmann@24198
   899
where
haftmann@24198
   900
  "rat_of_int \<equiv> of_int"
haftmann@24198
   901
huffman@28010
   902
subsection {* The Set of Rational Numbers *}
berghofe@24533
   903
nipkow@28001
   904
context field_char_0
nipkow@28001
   905
begin
nipkow@28001
   906
nipkow@28001
   907
definition
nipkow@28001
   908
  Rats  :: "'a set" where
haftmann@35369
   909
  "Rats = range of_rat"
nipkow@28001
   910
nipkow@28001
   911
notation (xsymbols)
nipkow@28001
   912
  Rats  ("\<rat>")
nipkow@28001
   913
nipkow@28001
   914
end
nipkow@28001
   915
huffman@28010
   916
lemma Rats_of_rat [simp]: "of_rat r \<in> Rats"
huffman@28010
   917
by (simp add: Rats_def)
huffman@28010
   918
huffman@28010
   919
lemma Rats_of_int [simp]: "of_int z \<in> Rats"
huffman@28010
   920
by (subst of_rat_of_int_eq [symmetric], rule Rats_of_rat)
huffman@28010
   921
huffman@28010
   922
lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
huffman@28010
   923
by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
huffman@28010
   924
huffman@47108
   925
lemma Rats_number_of [simp]: "numeral w \<in> Rats"
huffman@47108
   926
by (subst of_rat_numeral_eq [symmetric], rule Rats_of_rat)
huffman@47108
   927
huffman@47108
   928
lemma Rats_neg_number_of [simp]: "neg_numeral w \<in> Rats"
huffman@47108
   929
by (subst of_rat_neg_numeral_eq [symmetric], rule Rats_of_rat)
huffman@28010
   930
huffman@28010
   931
lemma Rats_0 [simp]: "0 \<in> Rats"
huffman@28010
   932
apply (unfold Rats_def)
huffman@28010
   933
apply (rule range_eqI)
huffman@28010
   934
apply (rule of_rat_0 [symmetric])
huffman@28010
   935
done
huffman@28010
   936
huffman@28010
   937
lemma Rats_1 [simp]: "1 \<in> Rats"
huffman@28010
   938
apply (unfold Rats_def)
huffman@28010
   939
apply (rule range_eqI)
huffman@28010
   940
apply (rule of_rat_1 [symmetric])
huffman@28010
   941
done
huffman@28010
   942
huffman@28010
   943
lemma Rats_add [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a + b \<in> Rats"
huffman@28010
   944
apply (auto simp add: Rats_def)
huffman@28010
   945
apply (rule range_eqI)
huffman@28010
   946
apply (rule of_rat_add [symmetric])
huffman@28010
   947
done
huffman@28010
   948
huffman@28010
   949
lemma Rats_minus [simp]: "a \<in> Rats \<Longrightarrow> - a \<in> Rats"
huffman@28010
   950
apply (auto simp add: Rats_def)
huffman@28010
   951
apply (rule range_eqI)
huffman@28010
   952
apply (rule of_rat_minus [symmetric])
huffman@28010
   953
done
huffman@28010
   954
huffman@28010
   955
lemma Rats_diff [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a - b \<in> Rats"
huffman@28010
   956
apply (auto simp add: Rats_def)
huffman@28010
   957
apply (rule range_eqI)
huffman@28010
   958
apply (rule of_rat_diff [symmetric])
huffman@28010
   959
done
huffman@28010
   960
huffman@28010
   961
lemma Rats_mult [simp]: "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a * b \<in> Rats"
huffman@28010
   962
apply (auto simp add: Rats_def)
huffman@28010
   963
apply (rule range_eqI)
huffman@28010
   964
apply (rule of_rat_mult [symmetric])
huffman@28010
   965
done
huffman@28010
   966
huffman@28010
   967
lemma nonzero_Rats_inverse:
huffman@28010
   968
  fixes a :: "'a::field_char_0"
huffman@28010
   969
  shows "\<lbrakk>a \<in> Rats; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Rats"
huffman@28010
   970
apply (auto simp add: Rats_def)
huffman@28010
   971
apply (rule range_eqI)
huffman@28010
   972
apply (erule nonzero_of_rat_inverse [symmetric])
huffman@28010
   973
done
huffman@28010
   974
huffman@28010
   975
lemma Rats_inverse [simp]:
haftmann@36409
   976
  fixes a :: "'a::{field_char_0, field_inverse_zero}"
huffman@28010
   977
  shows "a \<in> Rats \<Longrightarrow> inverse a \<in> Rats"
huffman@28010
   978
apply (auto simp add: Rats_def)
huffman@28010
   979
apply (rule range_eqI)
huffman@28010
   980
apply (rule of_rat_inverse [symmetric])
huffman@28010
   981
done
huffman@28010
   982
huffman@28010
   983
lemma nonzero_Rats_divide:
huffman@28010
   984
  fixes a b :: "'a::field_char_0"
huffman@28010
   985
  shows "\<lbrakk>a \<in> Rats; b \<in> Rats; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
huffman@28010
   986
apply (auto simp add: Rats_def)
huffman@28010
   987
apply (rule range_eqI)
huffman@28010
   988
apply (erule nonzero_of_rat_divide [symmetric])
huffman@28010
   989
done
huffman@28010
   990
huffman@28010
   991
lemma Rats_divide [simp]:
haftmann@36409
   992
  fixes a b :: "'a::{field_char_0, field_inverse_zero}"
huffman@28010
   993
  shows "\<lbrakk>a \<in> Rats; b \<in> Rats\<rbrakk> \<Longrightarrow> a / b \<in> Rats"
huffman@28010
   994
apply (auto simp add: Rats_def)
huffman@28010
   995
apply (rule range_eqI)
huffman@28010
   996
apply (rule of_rat_divide [symmetric])
huffman@28010
   997
done
huffman@28010
   998
huffman@28010
   999
lemma Rats_power [simp]:
haftmann@31017
  1000
  fixes a :: "'a::field_char_0"
huffman@28010
  1001
  shows "a \<in> Rats \<Longrightarrow> a ^ n \<in> Rats"
huffman@28010
  1002
apply (auto simp add: Rats_def)
huffman@28010
  1003
apply (rule range_eqI)
huffman@28010
  1004
apply (rule of_rat_power [symmetric])
huffman@28010
  1005
done
huffman@28010
  1006
huffman@28010
  1007
lemma Rats_cases [cases set: Rats]:
huffman@28010
  1008
  assumes "q \<in> \<rat>"
huffman@28010
  1009
  obtains (of_rat) r where "q = of_rat r"
huffman@28010
  1010
  unfolding Rats_def
huffman@28010
  1011
proof -
huffman@28010
  1012
  from `q \<in> \<rat>` have "q \<in> range of_rat" unfolding Rats_def .
huffman@28010
  1013
  then obtain r where "q = of_rat r" ..
huffman@28010
  1014
  then show thesis ..
huffman@28010
  1015
qed
huffman@28010
  1016
huffman@28010
  1017
lemma Rats_induct [case_names of_rat, induct set: Rats]:
huffman@28010
  1018
  "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
huffman@28010
  1019
  by (rule Rats_cases) auto
huffman@28010
  1020
nipkow@28001
  1021
berghofe@24533
  1022
subsection {* Implementation of rational numbers as pairs of integers *}
berghofe@24533
  1023
huffman@47108
  1024
text {* Formal constructor *}
huffman@47108
  1025
haftmann@35369
  1026
definition Frct :: "int \<times> int \<Rightarrow> rat" where
haftmann@35369
  1027
  [simp]: "Frct p = Fract (fst p) (snd p)"
haftmann@35369
  1028
haftmann@36112
  1029
lemma [code abstype]:
haftmann@36112
  1030
  "Frct (quotient_of q) = q"
haftmann@36112
  1031
  by (cases q) (auto intro: quotient_of_eq)
haftmann@35369
  1032
huffman@47108
  1033
huffman@47108
  1034
text {* Numerals *}
haftmann@35369
  1035
haftmann@35369
  1036
declare quotient_of_Fract [code abstract]
haftmann@35369
  1037
huffman@47108
  1038
definition of_int :: "int \<Rightarrow> rat"
huffman@47108
  1039
where
huffman@47108
  1040
  [code_abbrev]: "of_int = Int.of_int"
huffman@47108
  1041
hide_const (open) of_int
huffman@47108
  1042
huffman@47108
  1043
lemma quotient_of_int [code abstract]:
huffman@47108
  1044
  "quotient_of (Rat.of_int a) = (a, 1)"
huffman@47108
  1045
  by (simp add: of_int_def of_int_rat quotient_of_Fract)
huffman@47108
  1046
huffman@47108
  1047
lemma [code_unfold]:
huffman@47108
  1048
  "numeral k = Rat.of_int (numeral k)"
huffman@47108
  1049
  by (simp add: Rat.of_int_def)
huffman@47108
  1050
huffman@47108
  1051
lemma [code_unfold]:
huffman@47108
  1052
  "neg_numeral k = Rat.of_int (neg_numeral k)"
huffman@47108
  1053
  by (simp add: Rat.of_int_def)
huffman@47108
  1054
huffman@47108
  1055
lemma Frct_code_post [code_post]:
huffman@47108
  1056
  "Frct (0, a) = 0"
huffman@47108
  1057
  "Frct (a, 0) = 0"
huffman@47108
  1058
  "Frct (1, 1) = 1"
huffman@47108
  1059
  "Frct (numeral k, 1) = numeral k"
huffman@47108
  1060
  "Frct (neg_numeral k, 1) = neg_numeral k"
huffman@47108
  1061
  "Frct (1, numeral k) = 1 / numeral k"
huffman@47108
  1062
  "Frct (1, neg_numeral k) = 1 / neg_numeral k"
huffman@47108
  1063
  "Frct (numeral k, numeral l) = numeral k / numeral l"
huffman@47108
  1064
  "Frct (numeral k, neg_numeral l) = numeral k / neg_numeral l"
huffman@47108
  1065
  "Frct (neg_numeral k, numeral l) = neg_numeral k / numeral l"
huffman@47108
  1066
  "Frct (neg_numeral k, neg_numeral l) = neg_numeral k / neg_numeral l"
huffman@47108
  1067
  by (simp_all add: Fract_of_int_quotient)
huffman@47108
  1068
huffman@47108
  1069
huffman@47108
  1070
text {* Operations *}
huffman@47108
  1071
haftmann@35369
  1072
lemma rat_zero_code [code abstract]:
haftmann@35369
  1073
  "quotient_of 0 = (0, 1)"
haftmann@35369
  1074
  by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
haftmann@35369
  1075
haftmann@35369
  1076
lemma rat_one_code [code abstract]:
haftmann@35369
  1077
  "quotient_of 1 = (1, 1)"
haftmann@35369
  1078
  by (simp add: One_rat_def quotient_of_Fract normalize_def)
haftmann@35369
  1079
haftmann@35369
  1080
lemma rat_plus_code [code abstract]:
haftmann@35369
  1081
  "quotient_of (p + q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
  1082
     in normalize (a * d + b * c, c * d))"
haftmann@35369
  1083
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@27652
  1084
haftmann@35369
  1085
lemma rat_uminus_code [code abstract]:
haftmann@35369
  1086
  "quotient_of (- p) = (let (a, b) = quotient_of p in (- a, b))"
haftmann@35369
  1087
  by (cases p) (simp add: quotient_of_Fract)
haftmann@35369
  1088
haftmann@35369
  1089
lemma rat_minus_code [code abstract]:
haftmann@35369
  1090
  "quotient_of (p - q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
  1091
     in normalize (a * d - b * c, c * d))"
haftmann@35369
  1092
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@35369
  1093
haftmann@35369
  1094
lemma rat_times_code [code abstract]:
haftmann@35369
  1095
  "quotient_of (p * q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
  1096
     in normalize (a * b, c * d))"
haftmann@35369
  1097
  by (cases p, cases q) (simp add: quotient_of_Fract)
berghofe@24533
  1098
haftmann@35369
  1099
lemma rat_inverse_code [code abstract]:
haftmann@35369
  1100
  "quotient_of (inverse p) = (let (a, b) = quotient_of p
haftmann@35369
  1101
    in if a = 0 then (0, 1) else (sgn a * b, \<bar>a\<bar>))"
haftmann@35369
  1102
proof (cases p)
haftmann@35369
  1103
  case (Fract a b) then show ?thesis
haftmann@35369
  1104
    by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract gcd_int.commute)
haftmann@35369
  1105
qed
haftmann@35369
  1106
haftmann@35369
  1107
lemma rat_divide_code [code abstract]:
haftmann@35369
  1108
  "quotient_of (p / q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
  1109
     in normalize (a * d, c * b))"
haftmann@35369
  1110
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@35369
  1111
haftmann@35369
  1112
lemma rat_abs_code [code abstract]:
haftmann@35369
  1113
  "quotient_of \<bar>p\<bar> = (let (a, b) = quotient_of p in (\<bar>a\<bar>, b))"
haftmann@35369
  1114
  by (cases p) (simp add: quotient_of_Fract)
haftmann@35369
  1115
haftmann@35369
  1116
lemma rat_sgn_code [code abstract]:
haftmann@35369
  1117
  "quotient_of (sgn p) = (sgn (fst (quotient_of p)), 1)"
haftmann@35369
  1118
proof (cases p)
haftmann@35369
  1119
  case (Fract a b) then show ?thesis
haftmann@35369
  1120
  by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract)
haftmann@35369
  1121
qed
berghofe@24533
  1122
bulwahn@43733
  1123
lemma rat_floor_code [code]:
bulwahn@43733
  1124
  "floor p = (let (a, b) = quotient_of p in a div b)"
bulwahn@43733
  1125
by (cases p) (simp add: quotient_of_Fract floor_Fract)
bulwahn@43733
  1126
haftmann@38857
  1127
instantiation rat :: equal
haftmann@26513
  1128
begin
haftmann@26513
  1129
haftmann@35369
  1130
definition [code]:
haftmann@38857
  1131
  "HOL.equal a b \<longleftrightarrow> quotient_of a = quotient_of b"
haftmann@26513
  1132
haftmann@35369
  1133
instance proof
haftmann@38857
  1134
qed (simp add: equal_rat_def quotient_of_inject_eq)
haftmann@26513
  1135
haftmann@28351
  1136
lemma rat_eq_refl [code nbe]:
haftmann@38857
  1137
  "HOL.equal (r::rat) r \<longleftrightarrow> True"
haftmann@38857
  1138
  by (rule equal_refl)
haftmann@28351
  1139
haftmann@26513
  1140
end
berghofe@24533
  1141
haftmann@35369
  1142
lemma rat_less_eq_code [code]:
haftmann@35369
  1143
  "p \<le> q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d \<le> c * b)"
haftmann@35726
  1144
  by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
berghofe@24533
  1145
haftmann@35369
  1146
lemma rat_less_code [code]:
haftmann@35369
  1147
  "p < q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d < c * b)"
haftmann@35726
  1148
  by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
berghofe@24533
  1149
haftmann@35369
  1150
lemma [code]:
haftmann@35369
  1151
  "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
haftmann@35369
  1152
  by (cases p) (simp add: quotient_of_Fract of_rat_rat)
haftmann@27652
  1153
huffman@47108
  1154
huffman@47108
  1155
text {* Quickcheck *}
huffman@47108
  1156
haftmann@31203
  1157
definition (in term_syntax)
haftmann@32657
  1158
  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
  1159
  [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
haftmann@31203
  1160
haftmann@37751
  1161
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1162
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1163
haftmann@31203
  1164
instantiation rat :: random
haftmann@31203
  1165
begin
haftmann@31203
  1166
haftmann@31203
  1167
definition
haftmann@37751
  1168
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>num. Random.range i \<circ>\<rightarrow> (\<lambda>denom. Pair (
haftmann@31205
  1169
     let j = Code_Numeral.int_of (denom + 1)
haftmann@32657
  1170
     in valterm_fract num (j, \<lambda>u. Code_Evaluation.term_of j))))"
haftmann@31203
  1171
haftmann@31203
  1172
instance ..
haftmann@31203
  1173
haftmann@31203
  1174
end
haftmann@31203
  1175
haftmann@37751
  1176
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1177
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1178
bulwahn@41920
  1179
instantiation rat :: exhaustive
bulwahn@41231
  1180
begin
bulwahn@41231
  1181
bulwahn@41231
  1182
definition
bulwahn@45818
  1183
  "exhaustive_rat f d = Quickcheck_Exhaustive.exhaustive (%l. Quickcheck_Exhaustive.exhaustive (%k. f (Fract k (Code_Numeral.int_of l + 1))) d) d"
bulwahn@42311
  1184
bulwahn@42311
  1185
instance ..
bulwahn@42311
  1186
bulwahn@42311
  1187
end
bulwahn@42311
  1188
bulwahn@42311
  1189
instantiation rat :: full_exhaustive
bulwahn@42311
  1190
begin
bulwahn@42311
  1191
bulwahn@42311
  1192
definition
bulwahn@45818
  1193
  "full_exhaustive_rat f d = Quickcheck_Exhaustive.full_exhaustive (%(l, _). Quickcheck_Exhaustive.full_exhaustive (%k.
bulwahn@45507
  1194
     f (let j = Code_Numeral.int_of l + 1
bulwahn@45507
  1195
        in valterm_fract k (j, %_. Code_Evaluation.term_of j))) d) d"
bulwahn@41231
  1196
bulwahn@41231
  1197
instance ..
bulwahn@41231
  1198
bulwahn@41231
  1199
end
bulwahn@41231
  1200
bulwahn@43889
  1201
instantiation rat :: partial_term_of
bulwahn@43889
  1202
begin
bulwahn@43889
  1203
bulwahn@43889
  1204
instance ..
bulwahn@43889
  1205
bulwahn@43889
  1206
end
bulwahn@43889
  1207
bulwahn@43889
  1208
lemma [code]:
bulwahn@46758
  1209
  "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_variable p tt) == Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Rat.rat'') [])"
bulwahn@46758
  1210
  "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_constructor 0 [l, k]) ==
bulwahn@45507
  1211
     Code_Evaluation.App (Code_Evaluation.Const (STR ''Rat.Frct'')
bulwahn@45507
  1212
     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Product_Type.prod'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []],
bulwahn@45507
  1213
        Typerep.Typerep (STR ''Rat.rat'') []])) (Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.Const (STR ''Product_Type.Pair'') (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Product_Type.prod'') [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []]]])) (partial_term_of (TYPE(int)) l)) (partial_term_of (TYPE(int)) k))"
bulwahn@43889
  1214
by (rule partial_term_of_anything)+
bulwahn@43889
  1215
bulwahn@43887
  1216
instantiation rat :: narrowing
bulwahn@43887
  1217
begin
bulwahn@43887
  1218
bulwahn@43887
  1219
definition
bulwahn@45507
  1220
  "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.apply
bulwahn@45507
  1221
    (Quickcheck_Narrowing.cons (%nom denom. Fract nom denom)) narrowing) narrowing"
bulwahn@43887
  1222
bulwahn@43887
  1223
instance ..
bulwahn@43887
  1224
bulwahn@43887
  1225
end
bulwahn@43887
  1226
bulwahn@43887
  1227
bulwahn@45183
  1228
subsection {* Setup for Nitpick *}
berghofe@24533
  1229
blanchet@38287
  1230
declaration {*
blanchet@38287
  1231
  Nitpick_HOL.register_frac_type @{type_name rat}
wenzelm@33209
  1232
   [(@{const_name zero_rat_inst.zero_rat}, @{const_name Nitpick.zero_frac}),
wenzelm@33209
  1233
    (@{const_name one_rat_inst.one_rat}, @{const_name Nitpick.one_frac}),
wenzelm@33209
  1234
    (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
wenzelm@33209
  1235
    (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
wenzelm@33209
  1236
    (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
wenzelm@33209
  1237
    (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
blanchet@37397
  1238
    (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
wenzelm@33209
  1239
    (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
blanchet@45478
  1240
    (@{const_name field_char_0_class.of_rat}, @{const_name Nitpick.of_frac})]
blanchet@33197
  1241
*}
blanchet@33197
  1242
blanchet@41792
  1243
lemmas [nitpick_unfold] = inverse_rat_inst.inverse_rat
huffman@47108
  1244
  one_rat_inst.one_rat ord_rat_inst.less_rat
blanchet@37397
  1245
  ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
blanchet@37397
  1246
  uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
blanchet@33197
  1247
huffman@35343
  1248
subsection{* Float syntax *}
huffman@35343
  1249
huffman@35343
  1250
syntax "_Float" :: "float_const \<Rightarrow> 'a"    ("_")
huffman@35343
  1251
huffman@35343
  1252
use "Tools/float_syntax.ML"
huffman@35343
  1253
setup Float_Syntax.setup
huffman@35343
  1254
huffman@35343
  1255
text{* Test: *}
huffman@35343
  1256
lemma "123.456 = -111.111 + 200 + 30 + 4 + 5/10 + 6/100 + (7/1000::rat)"
huffman@35343
  1257
by simp
huffman@35343
  1258
wenzelm@37143
  1259
wenzelm@37143
  1260
hide_const (open) normalize
wenzelm@37143
  1261
huffman@29880
  1262
end