src/HOL/Library/Fraction_Field.thy
author wenzelm
Tue Feb 21 16:28:18 2012 +0100 (2012-02-21)
changeset 46573 8c4c5c8dcf7a
parent 45694 4a8743618257
child 47252 3a096e7a1871
permissions -rw-r--r--
misc tuning;
more indentation;
haftmann@35372
     1
(*  Title:      HOL/Library/Fraction_Field.thy
chaieb@31761
     2
    Author:     Amine Chaieb, University of Cambridge
chaieb@31761
     3
*)
chaieb@31761
     4
wenzelm@46573
     5
header{* A formalization of the fraction field of any integral domain;
wenzelm@46573
     6
         generalization of theory Rat from int to any integral domain *}
chaieb@31761
     7
chaieb@31761
     8
theory Fraction_Field
haftmann@35372
     9
imports Main
chaieb@31761
    10
begin
chaieb@31761
    11
chaieb@31761
    12
subsection {* General fractions construction *}
chaieb@31761
    13
chaieb@31761
    14
subsubsection {* Construction of the type of fractions *}
chaieb@31761
    15
chaieb@31761
    16
definition fractrel :: "(('a::idom * 'a ) * ('a * 'a)) set" where
wenzelm@46573
    17
  "fractrel = {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
chaieb@31761
    18
chaieb@31761
    19
lemma fractrel_iff [simp]:
chaieb@31761
    20
  "(x, y) \<in> fractrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
chaieb@31761
    21
  by (simp add: fractrel_def)
chaieb@31761
    22
chaieb@31761
    23
lemma refl_fractrel: "refl_on {x. snd x \<noteq> 0} fractrel"
chaieb@31761
    24
  by (auto simp add: refl_on_def fractrel_def)
chaieb@31761
    25
chaieb@31761
    26
lemma sym_fractrel: "sym fractrel"
chaieb@31761
    27
  by (simp add: fractrel_def sym_def)
chaieb@31761
    28
chaieb@31761
    29
lemma trans_fractrel: "trans fractrel"
chaieb@31761
    30
proof (rule transI, unfold split_paired_all)
chaieb@31761
    31
  fix a b a' b' a'' b'' :: 'a
chaieb@31761
    32
  assume A: "((a, b), (a', b')) \<in> fractrel"
chaieb@31761
    33
  assume B: "((a', b'), (a'', b'')) \<in> fractrel"
chaieb@31761
    34
  have "b' * (a * b'') = b'' * (a * b')" by (simp add: mult_ac)
chaieb@31761
    35
  also from A have "a * b' = a' * b" by auto
chaieb@31761
    36
  also have "b'' * (a' * b) = b * (a' * b'')" by (simp add: mult_ac)
chaieb@31761
    37
  also from B have "a' * b'' = a'' * b'" by auto
chaieb@31761
    38
  also have "b * (a'' * b') = b' * (a'' * b)" by (simp add: mult_ac)
chaieb@31761
    39
  finally have "b' * (a * b'') = b' * (a'' * b)" .
chaieb@31761
    40
  moreover from B have "b' \<noteq> 0" by auto
chaieb@31761
    41
  ultimately have "a * b'' = a'' * b" by simp
chaieb@31761
    42
  with A B show "((a, b), (a'', b'')) \<in> fractrel" by auto
chaieb@31761
    43
qed
chaieb@31761
    44
  
chaieb@31761
    45
lemma equiv_fractrel: "equiv {x. snd x \<noteq> 0} fractrel"
haftmann@40815
    46
  by (rule equivI [OF refl_fractrel sym_fractrel trans_fractrel])
chaieb@31761
    47
chaieb@31761
    48
lemmas UN_fractrel = UN_equiv_class [OF equiv_fractrel]
chaieb@31761
    49
lemmas UN_fractrel2 = UN_equiv_class2 [OF equiv_fractrel equiv_fractrel]
chaieb@31761
    50
chaieb@31761
    51
lemma equiv_fractrel_iff [iff]: 
chaieb@31761
    52
  assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
chaieb@31761
    53
  shows "fractrel `` {x} = fractrel `` {y} \<longleftrightarrow> (x, y) \<in> fractrel"
chaieb@31761
    54
  by (rule eq_equiv_class_iff, rule equiv_fractrel) (auto simp add: assms)
chaieb@31761
    55
wenzelm@45694
    56
definition "fract = {(x::'a\<times>'a). snd x \<noteq> (0::'a::idom)} // fractrel"
wenzelm@45694
    57
wenzelm@45694
    58
typedef (open) 'a fract = "fract :: ('a * 'a::idom) set set"
wenzelm@45694
    59
  unfolding fract_def
chaieb@31761
    60
proof
chaieb@31761
    61
  have "(0::'a, 1::'a) \<in> {x. snd x \<noteq> 0}" by simp
chaieb@31761
    62
  then show "fractrel `` {(0::'a, 1)} \<in> {x. snd x \<noteq> 0} // fractrel" by (rule quotientI)
chaieb@31761
    63
qed
chaieb@31761
    64
chaieb@31761
    65
lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
chaieb@31761
    66
  by (simp add: fract_def quotientI)
chaieb@31761
    67
chaieb@31761
    68
declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
chaieb@31761
    69
chaieb@31761
    70
chaieb@31761
    71
subsubsection {* Representation and basic operations *}
chaieb@31761
    72
wenzelm@46573
    73
definition Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
haftmann@37765
    74
  "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
chaieb@31761
    75
chaieb@31761
    76
code_datatype Fract
chaieb@31761
    77
chaieb@31761
    78
lemma Fract_cases [case_names Fract, cases type: fract]:
chaieb@31761
    79
  assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
chaieb@31761
    80
  shows C
chaieb@31761
    81
  using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
chaieb@31761
    82
chaieb@31761
    83
lemma Fract_induct [case_names Fract, induct type: fract]:
chaieb@31761
    84
  assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
chaieb@31761
    85
  shows "P q"
chaieb@31761
    86
  using assms by (cases q) simp
chaieb@31761
    87
chaieb@31761
    88
lemma eq_fract:
chaieb@31761
    89
  shows "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
chaieb@31761
    90
  and "\<And>a. Fract a 0 = Fract 0 1"
chaieb@31761
    91
  and "\<And>a c. Fract 0 a = Fract 0 c"
chaieb@31761
    92
  by (simp_all add: Fract_def)
chaieb@31761
    93
chaieb@31761
    94
instantiation fract :: (idom) "{comm_ring_1, power}"
chaieb@31761
    95
begin
chaieb@31761
    96
wenzelm@46573
    97
definition Zero_fract_def [code_unfold]: "0 = Fract 0 1"
chaieb@31761
    98
wenzelm@46573
    99
definition One_fract_def [code_unfold]: "1 = Fract 1 1"
chaieb@31761
   100
wenzelm@46573
   101
definition add_fract_def:
chaieb@31761
   102
  "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761
   103
    fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
chaieb@31761
   104
chaieb@31761
   105
lemma add_fract [simp]:
chaieb@31761
   106
  assumes "b \<noteq> (0::'a::idom)" and "d \<noteq> 0"
chaieb@31761
   107
  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
chaieb@31761
   108
proof -
chaieb@31761
   109
  have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
chaieb@31761
   110
    respects2 fractrel"
chaieb@31761
   111
  apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761
   112
  unfolding mult_assoc[symmetric] .
chaieb@31761
   113
  with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
chaieb@31761
   114
qed
chaieb@31761
   115
wenzelm@46573
   116
definition minus_fract_def:
chaieb@31761
   117
  "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
chaieb@31761
   118
chaieb@31761
   119
lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
chaieb@31761
   120
proof -
chaieb@31761
   121
  have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
haftmann@40822
   122
    by (simp add: congruent_def split_paired_all)
chaieb@31761
   123
  then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
chaieb@31761
   124
qed
chaieb@31761
   125
chaieb@31761
   126
lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
chaieb@31761
   127
  by (cases "b = 0") (simp_all add: eq_fract)
chaieb@31761
   128
wenzelm@46573
   129
definition diff_fract_def: "q - r = q + - (r::'a fract)"
chaieb@31761
   130
chaieb@31761
   131
lemma diff_fract [simp]:
chaieb@31761
   132
  assumes "b \<noteq> 0" and "d \<noteq> 0"
chaieb@31761
   133
  shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
chaieb@31761
   134
  using assms by (simp add: diff_fract_def diff_minus)
chaieb@31761
   135
wenzelm@46573
   136
definition mult_fract_def:
chaieb@31761
   137
  "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761
   138
    fractrel``{(fst x * fst y, snd x * snd y)})"
chaieb@31761
   139
chaieb@31761
   140
lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
chaieb@31761
   141
proof -
chaieb@31761
   142
  have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
chaieb@31761
   143
    apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761
   144
    unfolding mult_assoc[symmetric] .
chaieb@31761
   145
  then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
chaieb@31761
   146
qed
chaieb@31761
   147
chaieb@31761
   148
lemma mult_fract_cancel:
chaieb@31761
   149
  assumes "c \<noteq> 0"
chaieb@31761
   150
  shows "Fract (c * a) (c * b) = Fract a b"
chaieb@31761
   151
proof -
chaieb@31761
   152
  from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
chaieb@31761
   153
  then show ?thesis by (simp add: mult_fract [symmetric])
chaieb@31761
   154
qed
chaieb@31761
   155
chaieb@31761
   156
instance proof
chaieb@31761
   157
  fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)" 
chaieb@31761
   158
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   159
next
chaieb@31761
   160
  fix q r :: "'a fract" show "q * r = r * q"
chaieb@31761
   161
    by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761
   162
next
chaieb@31761
   163
  fix q :: "'a fract" show "1 * q = q"
chaieb@31761
   164
    by (cases q) (simp add: One_fract_def eq_fract)
chaieb@31761
   165
next
chaieb@31761
   166
  fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
chaieb@31761
   167
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   168
next
chaieb@31761
   169
  fix q r :: "'a fract" show "q + r = r + q"
chaieb@31761
   170
    by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761
   171
next
chaieb@31761
   172
  fix q :: "'a fract" show "0 + q = q"
chaieb@31761
   173
    by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761
   174
next
chaieb@31761
   175
  fix q :: "'a fract" show "- q + q = 0"
chaieb@31761
   176
    by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761
   177
next
chaieb@31761
   178
  fix q r :: "'a fract" show "q - r = q + - r"
chaieb@31761
   179
    by (cases q, cases r) (simp add: eq_fract)
chaieb@31761
   180
next
chaieb@31761
   181
  fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
chaieb@31761
   182
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   183
next
chaieb@31761
   184
  show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
chaieb@31761
   185
qed
chaieb@31761
   186
chaieb@31761
   187
end
chaieb@31761
   188
chaieb@31761
   189
lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
chaieb@31761
   190
  by (induct k) (simp_all add: Zero_fract_def One_fract_def)
chaieb@31761
   191
chaieb@31761
   192
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
chaieb@31761
   193
  by (rule of_nat_fract [symmetric])
chaieb@31761
   194
haftmann@31998
   195
lemma fract_collapse [code_post]:
chaieb@31761
   196
  "Fract 0 k = 0"
chaieb@31761
   197
  "Fract 1 1 = 1"
chaieb@31761
   198
  "Fract k 0 = 0"
chaieb@31761
   199
  by (cases "k = 0")
chaieb@31761
   200
    (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
chaieb@31761
   201
haftmann@31998
   202
lemma fract_expand [code_unfold]:
chaieb@31761
   203
  "0 = Fract 0 1"
chaieb@31761
   204
  "1 = Fract 1 1"
chaieb@31761
   205
  by (simp_all add: fract_collapse)
chaieb@31761
   206
chaieb@31761
   207
lemma Fract_cases_nonzero [case_names Fract 0]:
chaieb@31761
   208
  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
chaieb@31761
   209
  assumes 0: "q = 0 \<Longrightarrow> C"
chaieb@31761
   210
  shows C
chaieb@31761
   211
proof (cases "q = 0")
chaieb@31761
   212
  case True then show C using 0 by auto
chaieb@31761
   213
next
chaieb@31761
   214
  case False
chaieb@31761
   215
  then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
chaieb@31761
   216
  moreover with False have "0 \<noteq> Fract a b" by simp
chaieb@31761
   217
  with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
chaieb@31761
   218
  with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
chaieb@31761
   219
qed
chaieb@31761
   220
  
chaieb@31761
   221
chaieb@31761
   222
chaieb@31761
   223
subsubsection {* The field of rational numbers *}
chaieb@31761
   224
chaieb@31761
   225
context idom
chaieb@31761
   226
begin
chaieb@31761
   227
subclass ring_no_zero_divisors ..
chaieb@31761
   228
thm mult_eq_0_iff
chaieb@31761
   229
end
chaieb@31761
   230
haftmann@36409
   231
instantiation fract :: (idom) field_inverse_zero
chaieb@31761
   232
begin
chaieb@31761
   233
wenzelm@46573
   234
definition inverse_fract_def:
chaieb@31761
   235
  "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
chaieb@31761
   236
     fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
chaieb@31761
   237
chaieb@31761
   238
chaieb@31761
   239
lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
chaieb@31761
   240
proof -
chaieb@31761
   241
  have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
chaieb@31761
   242
  have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
chaieb@31761
   243
    by (auto simp add: congruent_def stupid algebra_simps)
chaieb@31761
   244
  then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
chaieb@31761
   245
qed
chaieb@31761
   246
wenzelm@46573
   247
definition divide_fract_def: "q / r = q * inverse (r:: 'a fract)"
chaieb@31761
   248
chaieb@31761
   249
lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
chaieb@31761
   250
  by (simp add: divide_fract_def)
chaieb@31761
   251
chaieb@31761
   252
instance proof
chaieb@31761
   253
  fix q :: "'a fract"
chaieb@31761
   254
  assume "q \<noteq> 0"
wenzelm@46573
   255
  then show "inverse q * q = 1"
wenzelm@46573
   256
    by (cases q rule: Fract_cases_nonzero)
wenzelm@46573
   257
      (simp_all add: fract_expand eq_fract mult_commute)
chaieb@31761
   258
next
chaieb@31761
   259
  fix q r :: "'a fract"
chaieb@31761
   260
  show "q / r = q * inverse r" by (simp add: divide_fract_def)
haftmann@36409
   261
next
wenzelm@46573
   262
  show "inverse 0 = (0:: 'a fract)"
wenzelm@46573
   263
    by (simp add: fract_expand) (simp add: fract_collapse)
chaieb@31761
   264
qed
chaieb@31761
   265
chaieb@31761
   266
end
chaieb@31761
   267
chaieb@31761
   268
huffman@36331
   269
subsubsection {* The ordered field of fractions over an ordered idom *}
huffman@36331
   270
huffman@36331
   271
lemma le_congruent2:
huffman@36331
   272
  "(\<lambda>x y::'a \<times> 'a::linordered_idom.
huffman@36331
   273
    {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
huffman@36331
   274
    respects2 fractrel"
huffman@36331
   275
proof (clarsimp simp add: congruent2_def)
huffman@36331
   276
  fix a b a' b' c d c' d' :: 'a
huffman@36331
   277
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
huffman@36331
   278
  assume eq1: "a * b' = a' * b"
huffman@36331
   279
  assume eq2: "c * d' = c' * d"
huffman@36331
   280
huffman@36331
   281
  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
huffman@36331
   282
  {
huffman@36331
   283
    fix a b c d x :: 'a assume x: "x \<noteq> 0"
huffman@36331
   284
    have "?le a b c d = ?le (a * x) (b * x) c d"
huffman@36331
   285
    proof -
huffman@36331
   286
      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
wenzelm@46573
   287
      then have "?le a b c d =
huffman@36331
   288
          ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
huffman@36331
   289
        by (simp add: mult_le_cancel_right)
huffman@36331
   290
      also have "... = ?le (a * x) (b * x) c d"
huffman@36331
   291
        by (simp add: mult_ac)
huffman@36331
   292
      finally show ?thesis .
huffman@36331
   293
    qed
huffman@36331
   294
  } note le_factor = this
huffman@36331
   295
huffman@36331
   296
  let ?D = "b * d" and ?D' = "b' * d'"
huffman@36331
   297
  from neq have D: "?D \<noteq> 0" by simp
huffman@36331
   298
  from neq have "?D' \<noteq> 0" by simp
wenzelm@46573
   299
  then have "?le a b c d = ?le (a * ?D') (b * ?D') c d"
huffman@36331
   300
    by (rule le_factor)
huffman@36331
   301
  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
huffman@36331
   302
    by (simp add: mult_ac)
huffman@36331
   303
  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
huffman@36331
   304
    by (simp only: eq1 eq2)
huffman@36331
   305
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
huffman@36331
   306
    by (simp add: mult_ac)
huffman@36331
   307
  also from D have "... = ?le a' b' c' d'"
huffman@36331
   308
    by (rule le_factor [symmetric])
huffman@36331
   309
  finally show "?le a b c d = ?le a' b' c' d'" .
huffman@36331
   310
qed
huffman@36331
   311
huffman@36331
   312
instantiation fract :: (linordered_idom) linorder
huffman@36331
   313
begin
huffman@36331
   314
wenzelm@46573
   315
definition le_fract_def:
haftmann@39910
   316
   "q \<le> r \<longleftrightarrow> the_elem (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
huffman@36331
   317
      {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
huffman@36331
   318
wenzelm@46573
   319
definition less_fract_def: "z < (w::'a fract) \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
huffman@36331
   320
huffman@36331
   321
lemma le_fract [simp]:
huffman@36331
   322
  assumes "b \<noteq> 0" and "d \<noteq> 0"
huffman@36331
   323
  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331
   324
by (simp add: Fract_def le_fract_def le_congruent2 UN_fractrel2 assms)
huffman@36331
   325
huffman@36331
   326
lemma less_fract [simp]:
huffman@36331
   327
  assumes "b \<noteq> 0" and "d \<noteq> 0"
huffman@36331
   328
  shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
huffman@36331
   329
by (simp add: less_fract_def less_le_not_le mult_ac assms)
huffman@36331
   330
huffman@36331
   331
instance proof
huffman@36331
   332
  fix q r s :: "'a fract"
huffman@36331
   333
  assume "q \<le> r" and "r \<le> s" thus "q \<le> s"
huffman@36331
   334
  proof (induct q, induct r, induct s)
huffman@36331
   335
    fix a b c d e f :: 'a
huffman@36331
   336
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331
   337
    assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
huffman@36331
   338
    show "Fract a b \<le> Fract e f"
huffman@36331
   339
    proof -
huffman@36331
   340
      from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
huffman@36331
   341
        by (auto simp add: zero_less_mult_iff linorder_neq_iff)
huffman@36331
   342
      have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
huffman@36331
   343
      proof -
huffman@36331
   344
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331
   345
          by simp
huffman@36331
   346
        with ff show ?thesis by (simp add: mult_le_cancel_right)
huffman@36331
   347
      qed
huffman@36331
   348
      also have "... = (c * f) * (d * f) * (b * b)"
huffman@36331
   349
        by (simp only: mult_ac)
huffman@36331
   350
      also have "... \<le> (e * d) * (d * f) * (b * b)"
huffman@36331
   351
      proof -
huffman@36331
   352
        from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
huffman@36331
   353
          by simp
huffman@36331
   354
        with bb show ?thesis by (simp add: mult_le_cancel_right)
huffman@36331
   355
      qed
huffman@36331
   356
      finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
huffman@36331
   357
        by (simp only: mult_ac)
huffman@36331
   358
      with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
huffman@36331
   359
        by (simp add: mult_le_cancel_right)
huffman@36331
   360
      with neq show ?thesis by simp
huffman@36331
   361
    qed
huffman@36331
   362
  qed
huffman@36331
   363
next
huffman@36331
   364
  fix q r :: "'a fract"
huffman@36331
   365
  assume "q \<le> r" and "r \<le> q" thus "q = r"
huffman@36331
   366
  proof (induct q, induct r)
huffman@36331
   367
    fix a b c d :: 'a
huffman@36331
   368
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"
huffman@36331
   369
    assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
huffman@36331
   370
    show "Fract a b = Fract c d"
huffman@36331
   371
    proof -
huffman@36331
   372
      from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331
   373
        by simp
huffman@36331
   374
      also have "... \<le> (a * d) * (b * d)"
huffman@36331
   375
      proof -
huffman@36331
   376
        from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
huffman@36331
   377
          by simp
huffman@36331
   378
        thus ?thesis by (simp only: mult_ac)
huffman@36331
   379
      qed
huffman@36331
   380
      finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
huffman@36331
   381
      moreover from neq have "b * d \<noteq> 0" by simp
huffman@36331
   382
      ultimately have "a * d = c * b" by simp
huffman@36331
   383
      with neq show ?thesis by (simp add: eq_fract)
huffman@36331
   384
    qed
huffman@36331
   385
  qed
huffman@36331
   386
next
huffman@36331
   387
  fix q r :: "'a fract"
huffman@36331
   388
  show "q \<le> q"
huffman@36331
   389
    by (induct q) simp
huffman@36331
   390
  show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
huffman@36331
   391
    by (simp only: less_fract_def)
huffman@36331
   392
  show "q \<le> r \<or> r \<le> q"
huffman@36331
   393
    by (induct q, induct r)
huffman@36331
   394
       (simp add: mult_commute, rule linorder_linear)
huffman@36331
   395
qed
huffman@36331
   396
huffman@36331
   397
end
huffman@36331
   398
huffman@36331
   399
instantiation fract :: (linordered_idom) "{distrib_lattice, abs_if, sgn_if}"
huffman@36331
   400
begin
huffman@36331
   401
wenzelm@46573
   402
definition abs_fract_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::'a fract))"
huffman@36331
   403
wenzelm@46573
   404
definition sgn_fract_def:
wenzelm@46573
   405
  "sgn (q::'a fract) = (if q=0 then 0 else if 0<q then 1 else - 1)"
huffman@36331
   406
huffman@36331
   407
theorem abs_fract [simp]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
huffman@36331
   408
  by (auto simp add: abs_fract_def Zero_fract_def le_less
huffman@36331
   409
      eq_fract zero_less_mult_iff mult_less_0_iff split: abs_split)
huffman@36331
   410
wenzelm@46573
   411
definition inf_fract_def:
wenzelm@46573
   412
  "(inf \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = min"
huffman@36331
   413
wenzelm@46573
   414
definition sup_fract_def:
wenzelm@46573
   415
  "(sup \<Colon> 'a fract \<Rightarrow> 'a fract \<Rightarrow> 'a fract) = max"
huffman@36331
   416
wenzelm@46573
   417
instance
wenzelm@46573
   418
  by intro_classes
wenzelm@46573
   419
    (auto simp add: abs_fract_def sgn_fract_def
wenzelm@46573
   420
      min_max.sup_inf_distrib1 inf_fract_def sup_fract_def)
huffman@36331
   421
huffman@36331
   422
end
huffman@36331
   423
haftmann@36414
   424
instance fract :: (linordered_idom) linordered_field_inverse_zero
huffman@36331
   425
proof
huffman@36331
   426
  fix q r s :: "'a fract"
huffman@36331
   427
  show "q \<le> r ==> s + q \<le> s + r"
huffman@36331
   428
  proof (induct q, induct r, induct s)
huffman@36331
   429
    fix a b c d e f :: 'a
huffman@36331
   430
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331
   431
    assume le: "Fract a b \<le> Fract c d"
huffman@36331
   432
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
huffman@36331
   433
    proof -
huffman@36331
   434
      let ?F = "f * f" from neq have F: "0 < ?F"
huffman@36331
   435
        by (auto simp add: zero_less_mult_iff)
huffman@36331
   436
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
huffman@36331
   437
        by simp
huffman@36331
   438
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
huffman@36331
   439
        by (simp add: mult_le_cancel_right)
haftmann@36348
   440
      with neq show ?thesis by (simp add: field_simps)
huffman@36331
   441
    qed
huffman@36331
   442
  qed
huffman@36331
   443
  show "q < r ==> 0 < s ==> s * q < s * r"
huffman@36331
   444
  proof (induct q, induct r, induct s)
huffman@36331
   445
    fix a b c d e f :: 'a
huffman@36331
   446
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
huffman@36331
   447
    assume le: "Fract a b < Fract c d"
huffman@36331
   448
    assume gt: "0 < Fract e f"
huffman@36331
   449
    show "Fract e f * Fract a b < Fract e f * Fract c d"
huffman@36331
   450
    proof -
huffman@36331
   451
      let ?E = "e * f" and ?F = "f * f"
huffman@36331
   452
      from neq gt have "0 < ?E"
huffman@36331
   453
        by (auto simp add: Zero_fract_def order_less_le eq_fract)
huffman@36331
   454
      moreover from neq have "0 < ?F"
huffman@36331
   455
        by (auto simp add: zero_less_mult_iff)
huffman@36331
   456
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
huffman@36331
   457
        by simp
huffman@36331
   458
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
huffman@36331
   459
        by (simp add: mult_less_cancel_right)
huffman@36331
   460
      with neq show ?thesis
huffman@36331
   461
        by (simp add: mult_ac)
huffman@36331
   462
    qed
huffman@36331
   463
  qed
huffman@36331
   464
qed
huffman@36331
   465
huffman@36331
   466
lemma fract_induct_pos [case_names Fract]:
huffman@36331
   467
  fixes P :: "'a::linordered_idom fract \<Rightarrow> bool"
huffman@36331
   468
  assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
huffman@36331
   469
  shows "P q"
huffman@36331
   470
proof (cases q)
huffman@36331
   471
  have step': "\<And>a b. b < 0 \<Longrightarrow> P (Fract a b)"
huffman@36331
   472
  proof -
huffman@36331
   473
    fix a::'a and b::'a
huffman@36331
   474
    assume b: "b < 0"
wenzelm@46573
   475
    then have "0 < -b" by simp
wenzelm@46573
   476
    then have "P (Fract (-a) (-b))" by (rule step)
huffman@36331
   477
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
huffman@36331
   478
  qed
huffman@36331
   479
  case (Fract a b)
huffman@36331
   480
  thus "P q" by (force simp add: linorder_neq_iff step step')
huffman@36331
   481
qed
huffman@36331
   482
huffman@36331
   483
lemma zero_less_Fract_iff:
huffman@36331
   484
  "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
huffman@36331
   485
  by (auto simp add: Zero_fract_def zero_less_mult_iff)
huffman@36331
   486
huffman@36331
   487
lemma Fract_less_zero_iff:
huffman@36331
   488
  "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
huffman@36331
   489
  by (auto simp add: Zero_fract_def mult_less_0_iff)
huffman@36331
   490
huffman@36331
   491
lemma zero_le_Fract_iff:
huffman@36331
   492
  "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
huffman@36331
   493
  by (auto simp add: Zero_fract_def zero_le_mult_iff)
huffman@36331
   494
huffman@36331
   495
lemma Fract_le_zero_iff:
huffman@36331
   496
  "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
huffman@36331
   497
  by (auto simp add: Zero_fract_def mult_le_0_iff)
huffman@36331
   498
huffman@36331
   499
lemma one_less_Fract_iff:
huffman@36331
   500
  "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
huffman@36331
   501
  by (auto simp add: One_fract_def mult_less_cancel_right_disj)
huffman@36331
   502
huffman@36331
   503
lemma Fract_less_one_iff:
huffman@36331
   504
  "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
huffman@36331
   505
  by (auto simp add: One_fract_def mult_less_cancel_right_disj)
huffman@36331
   506
huffman@36331
   507
lemma one_le_Fract_iff:
huffman@36331
   508
  "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
huffman@36331
   509
  by (auto simp add: One_fract_def mult_le_cancel_right)
huffman@36331
   510
huffman@36331
   511
lemma Fract_le_one_iff:
huffman@36331
   512
  "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
huffman@36331
   513
  by (auto simp add: One_fract_def mult_le_cancel_right)
huffman@36331
   514
huffman@36331
   515
end