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