src/HOL/Library/Fraction_Field.thy
author haftmann
Wed Feb 24 14:34:40 2010 +0100 (2010-02-24)
changeset 35372 ca158c7b1144
parent 31998 2c7a24f74db9
child 36312 26eea417ccc4
permissions -rw-r--r--
renamed theory Rational to Rat
haftmann@35372
     1
(*  Title:      HOL/Library/Fraction_Field.thy
chaieb@31761
     2
    Author:     Amine Chaieb, University of Cambridge
chaieb@31761
     3
*)
chaieb@31761
     4
chaieb@31761
     5
header{* A formalization of the fraction field of any integral domain 
haftmann@35372
     6
         A generalization of Rat.thy 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
chaieb@31761
    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"
chaieb@31761
    46
  by (rule equiv.intro [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
chaieb@31761
    56
typedef 'a fract = "{(x::'a\<times>'a). snd x \<noteq> (0::'a::idom)} // fractrel"
chaieb@31761
    57
proof
chaieb@31761
    58
  have "(0::'a, 1::'a) \<in> {x. snd x \<noteq> 0}" by simp
chaieb@31761
    59
  then show "fractrel `` {(0::'a, 1)} \<in> {x. snd x \<noteq> 0} // fractrel" by (rule quotientI)
chaieb@31761
    60
qed
chaieb@31761
    61
chaieb@31761
    62
lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
chaieb@31761
    63
  by (simp add: fract_def quotientI)
chaieb@31761
    64
chaieb@31761
    65
declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
chaieb@31761
    66
chaieb@31761
    67
chaieb@31761
    68
subsubsection {* Representation and basic operations *}
chaieb@31761
    69
chaieb@31761
    70
definition
chaieb@31761
    71
  Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
chaieb@31761
    72
  [code del]: "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
chaieb@31761
    73
chaieb@31761
    74
code_datatype Fract
chaieb@31761
    75
chaieb@31761
    76
lemma Fract_cases [case_names Fract, cases type: fract]:
chaieb@31761
    77
  assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
chaieb@31761
    78
  shows C
chaieb@31761
    79
  using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
chaieb@31761
    80
chaieb@31761
    81
lemma Fract_induct [case_names Fract, induct type: fract]:
chaieb@31761
    82
  assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
chaieb@31761
    83
  shows "P q"
chaieb@31761
    84
  using assms by (cases q) simp
chaieb@31761
    85
chaieb@31761
    86
lemma eq_fract:
chaieb@31761
    87
  shows "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
chaieb@31761
    88
  and "\<And>a. Fract a 0 = Fract 0 1"
chaieb@31761
    89
  and "\<And>a c. Fract 0 a = Fract 0 c"
chaieb@31761
    90
  by (simp_all add: Fract_def)
chaieb@31761
    91
chaieb@31761
    92
instantiation fract :: (idom) "{comm_ring_1, power}"
chaieb@31761
    93
begin
chaieb@31761
    94
chaieb@31761
    95
definition
haftmann@31998
    96
  Zero_fract_def [code, code_unfold]: "0 = Fract 0 1"
chaieb@31761
    97
chaieb@31761
    98
definition
haftmann@31998
    99
  One_fract_def [code, code_unfold]: "1 = Fract 1 1"
chaieb@31761
   100
chaieb@31761
   101
definition
chaieb@31761
   102
  add_fract_def [code del]:
chaieb@31761
   103
  "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761
   104
    fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
chaieb@31761
   105
chaieb@31761
   106
lemma add_fract [simp]:
chaieb@31761
   107
  assumes "b \<noteq> (0::'a::idom)" and "d \<noteq> 0"
chaieb@31761
   108
  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
chaieb@31761
   109
proof -
chaieb@31761
   110
  have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
chaieb@31761
   111
    respects2 fractrel"
chaieb@31761
   112
  apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761
   113
  unfolding mult_assoc[symmetric] .
chaieb@31761
   114
  with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
chaieb@31761
   115
qed
chaieb@31761
   116
chaieb@31761
   117
definition
chaieb@31761
   118
  minus_fract_def [code del]:
chaieb@31761
   119
  "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
chaieb@31761
   120
chaieb@31761
   121
lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
chaieb@31761
   122
proof -
chaieb@31761
   123
  have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
chaieb@31761
   124
    by (simp add: congruent_def)
chaieb@31761
   125
  then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
chaieb@31761
   126
qed
chaieb@31761
   127
chaieb@31761
   128
lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
chaieb@31761
   129
  by (cases "b = 0") (simp_all add: eq_fract)
chaieb@31761
   130
chaieb@31761
   131
definition
chaieb@31761
   132
  diff_fract_def [code del]: "q - r = q + - (r::'a fract)"
chaieb@31761
   133
chaieb@31761
   134
lemma diff_fract [simp]:
chaieb@31761
   135
  assumes "b \<noteq> 0" and "d \<noteq> 0"
chaieb@31761
   136
  shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
chaieb@31761
   137
  using assms by (simp add: diff_fract_def diff_minus)
chaieb@31761
   138
chaieb@31761
   139
definition
chaieb@31761
   140
  mult_fract_def [code del]:
chaieb@31761
   141
  "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
chaieb@31761
   142
    fractrel``{(fst x * fst y, snd x * snd y)})"
chaieb@31761
   143
chaieb@31761
   144
lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
chaieb@31761
   145
proof -
chaieb@31761
   146
  have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
chaieb@31761
   147
    apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
chaieb@31761
   148
    unfolding mult_assoc[symmetric] .
chaieb@31761
   149
  then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
chaieb@31761
   150
qed
chaieb@31761
   151
chaieb@31761
   152
lemma mult_fract_cancel:
chaieb@31761
   153
  assumes "c \<noteq> 0"
chaieb@31761
   154
  shows "Fract (c * a) (c * b) = Fract a b"
chaieb@31761
   155
proof -
chaieb@31761
   156
  from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
chaieb@31761
   157
  then show ?thesis by (simp add: mult_fract [symmetric])
chaieb@31761
   158
qed
chaieb@31761
   159
chaieb@31761
   160
instance proof
chaieb@31761
   161
  fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)" 
chaieb@31761
   162
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   163
next
chaieb@31761
   164
  fix q r :: "'a fract" show "q * r = r * q"
chaieb@31761
   165
    by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761
   166
next
chaieb@31761
   167
  fix q :: "'a fract" show "1 * q = q"
chaieb@31761
   168
    by (cases q) (simp add: One_fract_def eq_fract)
chaieb@31761
   169
next
chaieb@31761
   170
  fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
chaieb@31761
   171
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   172
next
chaieb@31761
   173
  fix q r :: "'a fract" show "q + r = r + q"
chaieb@31761
   174
    by (cases q, cases r) (simp add: eq_fract algebra_simps)
chaieb@31761
   175
next
chaieb@31761
   176
  fix q :: "'a fract" show "0 + q = q"
chaieb@31761
   177
    by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761
   178
next
chaieb@31761
   179
  fix q :: "'a fract" show "- q + q = 0"
chaieb@31761
   180
    by (cases q) (simp add: Zero_fract_def eq_fract)
chaieb@31761
   181
next
chaieb@31761
   182
  fix q r :: "'a fract" show "q - r = q + - r"
chaieb@31761
   183
    by (cases q, cases r) (simp add: eq_fract)
chaieb@31761
   184
next
chaieb@31761
   185
  fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
chaieb@31761
   186
    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
chaieb@31761
   187
next
chaieb@31761
   188
  show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
chaieb@31761
   189
qed
chaieb@31761
   190
chaieb@31761
   191
end
chaieb@31761
   192
chaieb@31761
   193
lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
chaieb@31761
   194
  by (induct k) (simp_all add: Zero_fract_def One_fract_def)
chaieb@31761
   195
chaieb@31761
   196
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
chaieb@31761
   197
  by (rule of_nat_fract [symmetric])
chaieb@31761
   198
haftmann@31998
   199
lemma fract_collapse [code_post]:
chaieb@31761
   200
  "Fract 0 k = 0"
chaieb@31761
   201
  "Fract 1 1 = 1"
chaieb@31761
   202
  "Fract k 0 = 0"
chaieb@31761
   203
  by (cases "k = 0")
chaieb@31761
   204
    (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
chaieb@31761
   205
haftmann@31998
   206
lemma fract_expand [code_unfold]:
chaieb@31761
   207
  "0 = Fract 0 1"
chaieb@31761
   208
  "1 = Fract 1 1"
chaieb@31761
   209
  by (simp_all add: fract_collapse)
chaieb@31761
   210
chaieb@31761
   211
lemma Fract_cases_nonzero [case_names Fract 0]:
chaieb@31761
   212
  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
chaieb@31761
   213
  assumes 0: "q = 0 \<Longrightarrow> C"
chaieb@31761
   214
  shows C
chaieb@31761
   215
proof (cases "q = 0")
chaieb@31761
   216
  case True then show C using 0 by auto
chaieb@31761
   217
next
chaieb@31761
   218
  case False
chaieb@31761
   219
  then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
chaieb@31761
   220
  moreover with False have "0 \<noteq> Fract a b" by simp
chaieb@31761
   221
  with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
chaieb@31761
   222
  with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
chaieb@31761
   223
qed
chaieb@31761
   224
  
chaieb@31761
   225
chaieb@31761
   226
chaieb@31761
   227
subsubsection {* The field of rational numbers *}
chaieb@31761
   228
chaieb@31761
   229
context idom
chaieb@31761
   230
begin
chaieb@31761
   231
subclass ring_no_zero_divisors ..
chaieb@31761
   232
thm mult_eq_0_iff
chaieb@31761
   233
end
chaieb@31761
   234
chaieb@31761
   235
instantiation fract :: (idom) "{field, division_by_zero}"
chaieb@31761
   236
begin
chaieb@31761
   237
chaieb@31761
   238
definition
chaieb@31761
   239
  inverse_fract_def [code del]:
chaieb@31761
   240
  "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
chaieb@31761
   241
     fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
chaieb@31761
   242
chaieb@31761
   243
chaieb@31761
   244
lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
chaieb@31761
   245
proof -
chaieb@31761
   246
  have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
chaieb@31761
   247
  have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
chaieb@31761
   248
    by (auto simp add: congruent_def stupid algebra_simps)
chaieb@31761
   249
  then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
chaieb@31761
   250
qed
chaieb@31761
   251
chaieb@31761
   252
definition
chaieb@31761
   253
  divide_fract_def [code del]: "q / r = q * inverse (r:: 'a fract)"
chaieb@31761
   254
chaieb@31761
   255
lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
chaieb@31761
   256
  by (simp add: divide_fract_def)
chaieb@31761
   257
chaieb@31761
   258
instance proof
chaieb@31761
   259
  show "inverse 0 = (0:: 'a fract)" by (simp add: fract_expand)
chaieb@31761
   260
    (simp add: fract_collapse)
chaieb@31761
   261
next
chaieb@31761
   262
  fix q :: "'a fract"
chaieb@31761
   263
  assume "q \<noteq> 0"
chaieb@31761
   264
  then show "inverse q * q = 1" apply (cases q rule: Fract_cases_nonzero)
chaieb@31761
   265
    by (simp_all add: mult_fract  inverse_fract fract_expand eq_fract mult_commute)
chaieb@31761
   266
next
chaieb@31761
   267
  fix q r :: "'a fract"
chaieb@31761
   268
  show "q / r = q * inverse r" by (simp add: divide_fract_def)
chaieb@31761
   269
qed
chaieb@31761
   270
chaieb@31761
   271
end
chaieb@31761
   272
chaieb@31761
   273
chaieb@31761
   274
end