src/HOL/Library/Fraction_Field.thy
changeset 31761 3585bebe49a8
child 31998 2c7a24f74db9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Fraction_Field.thy	Tue Jun 23 10:22:11 2009 +0200
     1.3 @@ -0,0 +1,274 @@
     1.4 +(*  Title:      Fraction_Field.thy
     1.5 +    Author:     Amine Chaieb, University of Cambridge
     1.6 +*)
     1.7 +
     1.8 +header{* A formalization of the fraction field of any integral domain 
     1.9 +         A generalization of Rational.thy from int to any integral domain *}
    1.10 +
    1.11 +theory Fraction_Field
    1.12 +imports Main (* Equiv_Relations Plain *)
    1.13 +begin
    1.14 +
    1.15 +subsection {* General fractions construction *}
    1.16 +
    1.17 +subsubsection {* Construction of the type of fractions *}
    1.18 +
    1.19 +definition fractrel :: "(('a::idom * 'a ) * ('a * 'a)) set" where
    1.20 +  "fractrel == {(x, y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
    1.21 +
    1.22 +lemma fractrel_iff [simp]:
    1.23 +  "(x, y) \<in> fractrel \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
    1.24 +  by (simp add: fractrel_def)
    1.25 +
    1.26 +lemma refl_fractrel: "refl_on {x. snd x \<noteq> 0} fractrel"
    1.27 +  by (auto simp add: refl_on_def fractrel_def)
    1.28 +
    1.29 +lemma sym_fractrel: "sym fractrel"
    1.30 +  by (simp add: fractrel_def sym_def)
    1.31 +
    1.32 +lemma trans_fractrel: "trans fractrel"
    1.33 +proof (rule transI, unfold split_paired_all)
    1.34 +  fix a b a' b' a'' b'' :: 'a
    1.35 +  assume A: "((a, b), (a', b')) \<in> fractrel"
    1.36 +  assume B: "((a', b'), (a'', b'')) \<in> fractrel"
    1.37 +  have "b' * (a * b'') = b'' * (a * b')" by (simp add: mult_ac)
    1.38 +  also from A have "a * b' = a' * b" by auto
    1.39 +  also have "b'' * (a' * b) = b * (a' * b'')" by (simp add: mult_ac)
    1.40 +  also from B have "a' * b'' = a'' * b'" by auto
    1.41 +  also have "b * (a'' * b') = b' * (a'' * b)" by (simp add: mult_ac)
    1.42 +  finally have "b' * (a * b'') = b' * (a'' * b)" .
    1.43 +  moreover from B have "b' \<noteq> 0" by auto
    1.44 +  ultimately have "a * b'' = a'' * b" by simp
    1.45 +  with A B show "((a, b), (a'', b'')) \<in> fractrel" by auto
    1.46 +qed
    1.47 +  
    1.48 +lemma equiv_fractrel: "equiv {x. snd x \<noteq> 0} fractrel"
    1.49 +  by (rule equiv.intro [OF refl_fractrel sym_fractrel trans_fractrel])
    1.50 +
    1.51 +lemmas UN_fractrel = UN_equiv_class [OF equiv_fractrel]
    1.52 +lemmas UN_fractrel2 = UN_equiv_class2 [OF equiv_fractrel equiv_fractrel]
    1.53 +
    1.54 +lemma equiv_fractrel_iff [iff]: 
    1.55 +  assumes "snd x \<noteq> 0" and "snd y \<noteq> 0"
    1.56 +  shows "fractrel `` {x} = fractrel `` {y} \<longleftrightarrow> (x, y) \<in> fractrel"
    1.57 +  by (rule eq_equiv_class_iff, rule equiv_fractrel) (auto simp add: assms)
    1.58 +
    1.59 +typedef 'a fract = "{(x::'a\<times>'a). snd x \<noteq> (0::'a::idom)} // fractrel"
    1.60 +proof
    1.61 +  have "(0::'a, 1::'a) \<in> {x. snd x \<noteq> 0}" by simp
    1.62 +  then show "fractrel `` {(0::'a, 1)} \<in> {x. snd x \<noteq> 0} // fractrel" by (rule quotientI)
    1.63 +qed
    1.64 +
    1.65 +lemma fractrel_in_fract [simp]: "snd x \<noteq> 0 \<Longrightarrow> fractrel `` {x} \<in> fract"
    1.66 +  by (simp add: fract_def quotientI)
    1.67 +
    1.68 +declare Abs_fract_inject [simp] Abs_fract_inverse [simp]
    1.69 +
    1.70 +
    1.71 +subsubsection {* Representation and basic operations *}
    1.72 +
    1.73 +definition
    1.74 +  Fract :: "'a::idom \<Rightarrow> 'a \<Rightarrow> 'a fract" where
    1.75 +  [code del]: "Fract a b = Abs_fract (fractrel `` {if b = 0 then (0, 1) else (a, b)})"
    1.76 +
    1.77 +code_datatype Fract
    1.78 +
    1.79 +lemma Fract_cases [case_names Fract, cases type: fract]:
    1.80 +  assumes "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> C"
    1.81 +  shows C
    1.82 +  using assms by (cases q) (clarsimp simp add: Fract_def fract_def quotient_def)
    1.83 +
    1.84 +lemma Fract_induct [case_names Fract, induct type: fract]:
    1.85 +  assumes "\<And>a b. b \<noteq> 0 \<Longrightarrow> P (Fract a b)"
    1.86 +  shows "P q"
    1.87 +  using assms by (cases q) simp
    1.88 +
    1.89 +lemma eq_fract:
    1.90 +  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"
    1.91 +  and "\<And>a. Fract a 0 = Fract 0 1"
    1.92 +  and "\<And>a c. Fract 0 a = Fract 0 c"
    1.93 +  by (simp_all add: Fract_def)
    1.94 +
    1.95 +instantiation fract :: (idom) "{comm_ring_1, power}"
    1.96 +begin
    1.97 +
    1.98 +definition
    1.99 +  Zero_fract_def [code, code unfold]: "0 = Fract 0 1"
   1.100 +
   1.101 +definition
   1.102 +  One_fract_def [code, code unfold]: "1 = Fract 1 1"
   1.103 +
   1.104 +definition
   1.105 +  add_fract_def [code del]:
   1.106 +  "q + r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
   1.107 +    fractrel `` {(fst x * snd y + fst y * snd x, snd x * snd y)})"
   1.108 +
   1.109 +lemma add_fract [simp]:
   1.110 +  assumes "b \<noteq> (0::'a::idom)" and "d \<noteq> 0"
   1.111 +  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
   1.112 +proof -
   1.113 +  have "(\<lambda>x y. fractrel``{(fst x * snd y + fst y * snd x, snd x * snd y :: 'a)})
   1.114 +    respects2 fractrel"
   1.115 +  apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
   1.116 +  unfolding mult_assoc[symmetric] .
   1.117 +  with assms show ?thesis by (simp add: Fract_def add_fract_def UN_fractrel2)
   1.118 +qed
   1.119 +
   1.120 +definition
   1.121 +  minus_fract_def [code del]:
   1.122 +  "- q = Abs_fract (\<Union>x \<in> Rep_fract q. fractrel `` {(- fst x, snd x)})"
   1.123 +
   1.124 +lemma minus_fract [simp, code]: "- Fract a b = Fract (- a) (b::'a::idom)"
   1.125 +proof -
   1.126 +  have "(\<lambda>x. fractrel `` {(- fst x, snd x :: 'a)}) respects fractrel"
   1.127 +    by (simp add: congruent_def)
   1.128 +  then show ?thesis by (simp add: Fract_def minus_fract_def UN_fractrel)
   1.129 +qed
   1.130 +
   1.131 +lemma minus_fract_cancel [simp]: "Fract (- a) (- b) = Fract a b"
   1.132 +  by (cases "b = 0") (simp_all add: eq_fract)
   1.133 +
   1.134 +definition
   1.135 +  diff_fract_def [code del]: "q - r = q + - (r::'a fract)"
   1.136 +
   1.137 +lemma diff_fract [simp]:
   1.138 +  assumes "b \<noteq> 0" and "d \<noteq> 0"
   1.139 +  shows "Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
   1.140 +  using assms by (simp add: diff_fract_def diff_minus)
   1.141 +
   1.142 +definition
   1.143 +  mult_fract_def [code del]:
   1.144 +  "q * r = Abs_fract (\<Union>x \<in> Rep_fract q. \<Union>y \<in> Rep_fract r.
   1.145 +    fractrel``{(fst x * fst y, snd x * snd y)})"
   1.146 +
   1.147 +lemma mult_fract [simp]: "Fract (a::'a::idom) b * Fract c d = Fract (a * c) (b * d)"
   1.148 +proof -
   1.149 +  have "(\<lambda>x y. fractrel `` {(fst x * fst y, snd x * snd y :: 'a)}) respects2 fractrel"
   1.150 +    apply (rule equiv_fractrel [THEN congruent2_commuteI]) apply (auto simp add: algebra_simps)
   1.151 +    unfolding mult_assoc[symmetric] .
   1.152 +  then show ?thesis by (simp add: Fract_def mult_fract_def UN_fractrel2)
   1.153 +qed
   1.154 +
   1.155 +lemma mult_fract_cancel:
   1.156 +  assumes "c \<noteq> 0"
   1.157 +  shows "Fract (c * a) (c * b) = Fract a b"
   1.158 +proof -
   1.159 +  from assms have "Fract c c = Fract 1 1" by (simp add: Fract_def)
   1.160 +  then show ?thesis by (simp add: mult_fract [symmetric])
   1.161 +qed
   1.162 +
   1.163 +instance proof
   1.164 +  fix q r s :: "'a fract" show "(q * r) * s = q * (r * s)" 
   1.165 +    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
   1.166 +next
   1.167 +  fix q r :: "'a fract" show "q * r = r * q"
   1.168 +    by (cases q, cases r) (simp add: eq_fract algebra_simps)
   1.169 +next
   1.170 +  fix q :: "'a fract" show "1 * q = q"
   1.171 +    by (cases q) (simp add: One_fract_def eq_fract)
   1.172 +next
   1.173 +  fix q r s :: "'a fract" show "(q + r) + s = q + (r + s)"
   1.174 +    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
   1.175 +next
   1.176 +  fix q r :: "'a fract" show "q + r = r + q"
   1.177 +    by (cases q, cases r) (simp add: eq_fract algebra_simps)
   1.178 +next
   1.179 +  fix q :: "'a fract" show "0 + q = q"
   1.180 +    by (cases q) (simp add: Zero_fract_def eq_fract)
   1.181 +next
   1.182 +  fix q :: "'a fract" show "- q + q = 0"
   1.183 +    by (cases q) (simp add: Zero_fract_def eq_fract)
   1.184 +next
   1.185 +  fix q r :: "'a fract" show "q - r = q + - r"
   1.186 +    by (cases q, cases r) (simp add: eq_fract)
   1.187 +next
   1.188 +  fix q r s :: "'a fract" show "(q + r) * s = q * s + r * s"
   1.189 +    by (cases q, cases r, cases s) (simp add: eq_fract algebra_simps)
   1.190 +next
   1.191 +  show "(0::'a fract) \<noteq> 1" by (simp add: Zero_fract_def One_fract_def eq_fract)
   1.192 +qed
   1.193 +
   1.194 +end
   1.195 +
   1.196 +lemma of_nat_fract: "of_nat k = Fract (of_nat k) 1"
   1.197 +  by (induct k) (simp_all add: Zero_fract_def One_fract_def)
   1.198 +
   1.199 +lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
   1.200 +  by (rule of_nat_fract [symmetric])
   1.201 +
   1.202 +lemma fract_collapse [code post]:
   1.203 +  "Fract 0 k = 0"
   1.204 +  "Fract 1 1 = 1"
   1.205 +  "Fract k 0 = 0"
   1.206 +  by (cases "k = 0")
   1.207 +    (simp_all add: Zero_fract_def One_fract_def eq_fract Fract_def)
   1.208 +
   1.209 +lemma fract_expand [code unfold]:
   1.210 +  "0 = Fract 0 1"
   1.211 +  "1 = Fract 1 1"
   1.212 +  by (simp_all add: fract_collapse)
   1.213 +
   1.214 +lemma Fract_cases_nonzero [case_names Fract 0]:
   1.215 +  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> C"
   1.216 +  assumes 0: "q = 0 \<Longrightarrow> C"
   1.217 +  shows C
   1.218 +proof (cases "q = 0")
   1.219 +  case True then show C using 0 by auto
   1.220 +next
   1.221 +  case False
   1.222 +  then obtain a b where "q = Fract a b" and "b \<noteq> 0" by (cases q) auto
   1.223 +  moreover with False have "0 \<noteq> Fract a b" by simp
   1.224 +  with `b \<noteq> 0` have "a \<noteq> 0" by (simp add: Zero_fract_def eq_fract)
   1.225 +  with Fract `q = Fract a b` `b \<noteq> 0` show C by auto
   1.226 +qed
   1.227 +  
   1.228 +
   1.229 +
   1.230 +subsubsection {* The field of rational numbers *}
   1.231 +
   1.232 +context idom
   1.233 +begin
   1.234 +subclass ring_no_zero_divisors ..
   1.235 +thm mult_eq_0_iff
   1.236 +end
   1.237 +
   1.238 +instantiation fract :: (idom) "{field, division_by_zero}"
   1.239 +begin
   1.240 +
   1.241 +definition
   1.242 +  inverse_fract_def [code del]:
   1.243 +  "inverse q = Abs_fract (\<Union>x \<in> Rep_fract q.
   1.244 +     fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x)})"
   1.245 +
   1.246 +
   1.247 +lemma inverse_fract [simp]: "inverse (Fract a b) = Fract (b::'a::idom) a"
   1.248 +proof -
   1.249 +  have stupid: "\<And>x. (0::'a) = x \<longleftrightarrow> x = 0" by auto
   1.250 +  have "(\<lambda>x. fractrel `` {if fst x = 0 then (0, 1) else (snd x, fst x :: 'a)}) respects fractrel"
   1.251 +    by (auto simp add: congruent_def stupid algebra_simps)
   1.252 +  then show ?thesis by (simp add: Fract_def inverse_fract_def UN_fractrel)
   1.253 +qed
   1.254 +
   1.255 +definition
   1.256 +  divide_fract_def [code del]: "q / r = q * inverse (r:: 'a fract)"
   1.257 +
   1.258 +lemma divide_fract [simp]: "Fract a b / Fract c d = Fract (a * d) (b * c)"
   1.259 +  by (simp add: divide_fract_def)
   1.260 +
   1.261 +instance proof
   1.262 +  show "inverse 0 = (0:: 'a fract)" by (simp add: fract_expand)
   1.263 +    (simp add: fract_collapse)
   1.264 +next
   1.265 +  fix q :: "'a fract"
   1.266 +  assume "q \<noteq> 0"
   1.267 +  then show "inverse q * q = 1" apply (cases q rule: Fract_cases_nonzero)
   1.268 +    by (simp_all add: mult_fract  inverse_fract fract_expand eq_fract mult_commute)
   1.269 +next
   1.270 +  fix q r :: "'a fract"
   1.271 +  show "q / r = q * inverse r" by (simp add: divide_fract_def)
   1.272 +qed
   1.273 +
   1.274 +end
   1.275 +
   1.276 +
   1.277 +end
   1.278 \ No newline at end of file