| author | wenzelm | 
| Tue, 25 Aug 2020 13:44:09 +0200 | |
| changeset 72204 | cb746b19e1d7 | 
| parent 71398 | e0237f2eb49d | 
| child 74362 | 0135a0c77b64 | 
| permissions | -rw-r--r-- | 
| 65435 | 1 | (* Title: HOL/Computational_Algebra/Normalized_Fraction.thy | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64240diff
changeset | 2 | Author: Manuel Eberl | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64240diff
changeset | 3 | *) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64240diff
changeset | 4 | |
| 63500 | 5 | theory Normalized_Fraction | 
| 6 | imports | |
| 7 | Main | |
| 65417 | 8 | Euclidean_Algorithm | 
| 65366 | 9 | Fraction_Field | 
| 63500 | 10 | begin | 
| 11 | ||
| 12 | definition quot_to_fract :: "'a :: {idom} \<times> 'a \<Rightarrow> 'a fract" where
 | |
| 13 | "quot_to_fract = (\<lambda>(a,b). Fraction_Field.Fract a b)" | |
| 14 | ||
| 71398 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
67078diff
changeset | 15 | definition normalize_quot :: "'a :: {ring_gcd,idom_divide,semiring_gcd_mult_normalize} \<times> 'a \<Rightarrow> 'a \<times> 'a" where
 | 
| 63500 | 16 | "normalize_quot = | 
| 17 | (\<lambda>(a,b). if b = 0 then (0,1) else let d = gcd a b * unit_factor b in (a div d, b div d))" | |
| 18 | ||
| 67051 | 19 | lemma normalize_quot_zero [simp]: | 
| 20 | "normalize_quot (a, 0) = (0, 1)" | |
| 21 | by (simp add: normalize_quot_def) | |
| 22 | ||
| 67078 
6a85b8a9c28c
removed overambitious simp rules from e7e54a0b9197
 haftmann parents: 
67051diff
changeset | 23 | lemma normalize_quot_proj: | 
| 67051 | 24 | "fst (normalize_quot (a, b)) = a div (gcd a b * unit_factor b)" | 
| 25 | "snd (normalize_quot (a, b)) = normalize b div gcd a b" if "b \<noteq> 0" | |
| 26 | using that by (simp_all add: normalize_quot_def Let_def mult.commute [of _ "unit_factor b"] dvd_div_mult2_eq mult_unit_dvd_iff') | |
| 27 | ||
| 63500 | 28 | definition normalized_fracts :: "('a :: {ring_gcd,idom_divide} \<times> 'a) set" where
 | 
| 29 |   "normalized_fracts = {(a,b). coprime a b \<and> unit_factor b = 1}"
 | |
| 30 | ||
| 31 | lemma not_normalized_fracts_0_denom [simp]: "(a, 0) \<notin> normalized_fracts" | |
| 32 | by (auto simp: normalized_fracts_def) | |
| 33 | ||
| 34 | lemma unit_factor_snd_normalize_quot [simp]: | |
| 35 | "unit_factor (snd (normalize_quot x)) = 1" | |
| 36 | by (simp add: normalize_quot_def case_prod_unfold Let_def dvd_unit_factor_div | |
| 37 | mult_unit_dvd_iff unit_factor_mult unit_factor_gcd) | |
| 38 | ||
| 39 | lemma snd_normalize_quot_nonzero [simp]: "snd (normalize_quot x) \<noteq> 0" | |
| 40 | using unit_factor_snd_normalize_quot[of x] | |
| 41 | by (auto simp del: unit_factor_snd_normalize_quot) | |
| 42 | ||
| 43 | lemma normalize_quot_aux: | |
| 44 | fixes a b | |
| 45 | assumes "b \<noteq> 0" | |
| 46 | defines "d \<equiv> gcd a b * unit_factor b" | |
| 47 | shows "a = fst (normalize_quot (a,b)) * d" "b = snd (normalize_quot (a,b)) * d" | |
| 48 | "d dvd a" "d dvd b" "d \<noteq> 0" | |
| 49 | proof - | |
| 50 | from assms show "d dvd a" "d dvd b" | |
| 51 | by (simp_all add: d_def mult_unit_dvd_iff) | |
| 52 | thus "a = fst (normalize_quot (a,b)) * d" "b = snd (normalize_quot (a,b)) * d" "d \<noteq> 0" | |
| 53 | by (auto simp: normalize_quot_def Let_def d_def \<open>b \<noteq> 0\<close>) | |
| 54 | qed | |
| 55 | ||
| 56 | lemma normalize_quotE: | |
| 57 | assumes "b \<noteq> 0" | |
| 58 | obtains d where "a = fst (normalize_quot (a,b)) * d" "b = snd (normalize_quot (a,b)) * d" | |
| 59 | "d dvd a" "d dvd b" "d \<noteq> 0" | |
| 60 | using that[OF normalize_quot_aux[OF assms]] . | |
| 61 | ||
| 62 | lemma normalize_quotE': | |
| 63 | assumes "snd x \<noteq> 0" | |
| 64 | obtains d where "fst x = fst (normalize_quot x) * d" "snd x = snd (normalize_quot x) * d" | |
| 65 | "d dvd fst x" "d dvd snd x" "d \<noteq> 0" | |
| 66 | proof - | |
| 67 | from normalize_quotE[OF assms, of "fst x"] guess d . | |
| 68 | from this show ?thesis unfolding prod.collapse by (intro that[of d]) | |
| 69 | qed | |
| 70 | ||
| 71 | lemma coprime_normalize_quot: | |
| 72 | "coprime (fst (normalize_quot x)) (snd (normalize_quot x))" | |
| 67051 | 73 | by (simp add: normalize_quot_def case_prod_unfold div_mult_unit2) | 
| 74 | (metis coprime_mult_self_right_iff div_gcd_coprime unit_div_mult_self unit_factor_is_unit) | |
| 63500 | 75 | |
| 76 | lemma normalize_quot_in_normalized_fracts [simp]: "normalize_quot x \<in> normalized_fracts" | |
| 77 | by (simp add: normalized_fracts_def coprime_normalize_quot case_prod_unfold) | |
| 78 | ||
| 79 | lemma normalize_quot_eq_iff: | |
| 80 | assumes "b \<noteq> 0" "d \<noteq> 0" | |
| 81 | shows "normalize_quot (a,b) = normalize_quot (c,d) \<longleftrightarrow> a * d = b * c" | |
| 82 | proof - | |
| 83 | define x y where "x = normalize_quot (a,b)" and "y = normalize_quot (c,d)" | |
| 84 | from normalize_quotE[OF assms(1), of a] normalize_quotE[OF assms(2), of c] | |
| 85 | obtain d1 d2 | |
| 86 | where "a = fst x * d1" "b = snd x * d1" "c = fst y * d2" "d = snd y * d2" "d1 \<noteq> 0" "d2 \<noteq> 0" | |
| 87 | unfolding x_def y_def by metis | |
| 88 | hence "a * d = b * c \<longleftrightarrow> fst x * snd y = snd x * fst y" by simp | |
| 89 | also have "\<dots> \<longleftrightarrow> fst x = fst y \<and> snd x = snd y" | |
| 90 | by (intro coprime_crossproduct') (simp_all add: x_def y_def coprime_normalize_quot) | |
| 91 | also have "\<dots> \<longleftrightarrow> x = y" using prod_eqI by blast | |
| 92 | finally show "x = y \<longleftrightarrow> a * d = b * c" .. | |
| 93 | qed | |
| 94 | ||
| 95 | lemma normalize_quot_eq_iff': | |
| 96 | assumes "snd x \<noteq> 0" "snd y \<noteq> 0" | |
| 97 | shows "normalize_quot x = normalize_quot y \<longleftrightarrow> fst x * snd y = snd x * fst y" | |
| 98 | using assms by (cases x, cases y, hypsubst) (subst normalize_quot_eq_iff, simp_all) | |
| 99 | ||
| 100 | lemma normalize_quot_id: "x \<in> normalized_fracts \<Longrightarrow> normalize_quot x = x" | |
| 101 | by (auto simp: normalized_fracts_def normalize_quot_def case_prod_unfold) | |
| 102 | ||
| 103 | lemma normalize_quot_idem [simp]: "normalize_quot (normalize_quot x) = normalize_quot x" | |
| 104 | by (rule normalize_quot_id) simp_all | |
| 105 | ||
| 106 | lemma fractrel_iff_normalize_quot_eq: | |
| 107 | "fractrel x y \<longleftrightarrow> normalize_quot x = normalize_quot y \<and> snd x \<noteq> 0 \<and> snd y \<noteq> 0" | |
| 108 | by (cases x, cases y) (auto simp: fractrel_def normalize_quot_eq_iff) | |
| 109 | ||
| 110 | lemma fractrel_normalize_quot_left: | |
| 111 | assumes "snd x \<noteq> 0" | |
| 112 | shows "fractrel (normalize_quot x) y \<longleftrightarrow> fractrel x y" | |
| 113 | using assms by (subst (1 2) fractrel_iff_normalize_quot_eq) auto | |
| 114 | ||
| 115 | lemma fractrel_normalize_quot_right: | |
| 116 | assumes "snd x \<noteq> 0" | |
| 117 | shows "fractrel y (normalize_quot x) \<longleftrightarrow> fractrel y x" | |
| 118 | using assms by (subst (1 2) fractrel_iff_normalize_quot_eq) auto | |
| 119 | ||
| 120 | ||
| 71398 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
67078diff
changeset | 121 | lift_definition quot_of_fract :: | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
67078diff
changeset | 122 |   "'a :: {ring_gcd,idom_divide,semiring_gcd_mult_normalize} fract \<Rightarrow> 'a \<times> 'a" 
 | 
| 63500 | 123 | is normalize_quot | 
| 124 | by (subst (asm) fractrel_iff_normalize_quot_eq) simp_all | |
| 125 | ||
| 126 | lemma quot_to_fract_quot_of_fract [simp]: "quot_to_fract (quot_of_fract x) = x" | |
| 127 | unfolding quot_to_fract_def | |
| 128 | proof transfer | |
| 129 | fix x :: "'a \<times> 'a" assume rel: "fractrel x x" | |
| 130 | define x' where "x' = normalize_quot x" | |
| 131 | obtain a b where [simp]: "x = (a, b)" by (cases x) | |
| 132 | from rel have "b \<noteq> 0" by simp | |
| 133 | from normalize_quotE[OF this, of a] guess d . | |
| 134 | hence "a = fst x' * d" "b = snd x' * d" "d \<noteq> 0" "snd x' \<noteq> 0" by (simp_all add: x'_def) | |
| 135 | thus "fractrel (case x' of (a, b) \<Rightarrow> if b = 0 then (0, 1) else (a, b)) x" | |
| 136 | by (auto simp add: case_prod_unfold) | |
| 137 | qed | |
| 138 | ||
| 139 | lemma quot_of_fract_quot_to_fract: "quot_of_fract (quot_to_fract x) = normalize_quot x" | |
| 140 | proof (cases "snd x = 0") | |
| 141 | case True | |
| 142 | thus ?thesis unfolding quot_to_fract_def | |
| 143 | by transfer (simp add: case_prod_unfold normalize_quot_def) | |
| 144 | next | |
| 145 | case False | |
| 146 | thus ?thesis unfolding quot_to_fract_def by transfer (simp add: case_prod_unfold) | |
| 147 | qed | |
| 148 | ||
| 149 | lemma quot_of_fract_quot_to_fract': | |
| 150 | "x \<in> normalized_fracts \<Longrightarrow> quot_of_fract (quot_to_fract x) = x" | |
| 151 | unfolding quot_to_fract_def by transfer (auto simp: normalize_quot_id) | |
| 152 | ||
| 153 | lemma quot_of_fract_in_normalized_fracts [simp]: "quot_of_fract x \<in> normalized_fracts" | |
| 154 | by transfer simp | |
| 155 | ||
| 156 | lemma normalize_quotI: | |
| 157 | assumes "a * d = b * c" "b \<noteq> 0" "(c, d) \<in> normalized_fracts" | |
| 158 | shows "normalize_quot (a, b) = (c, d)" | |
| 159 | proof - | |
| 160 | from assms have "normalize_quot (a, b) = normalize_quot (c, d)" | |
| 161 | by (subst normalize_quot_eq_iff) auto | |
| 162 | also have "\<dots> = (c, d)" by (intro normalize_quot_id) fact | |
| 163 | finally show ?thesis . | |
| 164 | qed | |
| 165 | ||
| 166 | lemma td_normalized_fract: | |
| 167 | "type_definition quot_of_fract quot_to_fract normalized_fracts" | |
| 168 | by standard (simp_all add: quot_of_fract_quot_to_fract') | |
| 169 | ||
| 170 | lemma quot_of_fract_add_aux: | |
| 171 | assumes "snd x \<noteq> 0" "snd y \<noteq> 0" | |
| 172 | shows "(fst x * snd y + fst y * snd x) * (snd (normalize_quot x) * snd (normalize_quot y)) = | |
| 173 | snd x * snd y * (fst (normalize_quot x) * snd (normalize_quot y) + | |
| 174 | snd (normalize_quot x) * fst (normalize_quot y))" | |
| 175 | proof - | |
| 176 | from normalize_quotE'[OF assms(1)] guess d . note d = this | |
| 177 | from normalize_quotE'[OF assms(2)] guess e . note e = this | |
| 178 | show ?thesis by (simp_all add: d e algebra_simps) | |
| 179 | qed | |
| 180 | ||
| 181 | ||
| 182 | locale fract_as_normalized_quot | |
| 183 | begin | |
| 184 | setup_lifting td_normalized_fract | |
| 185 | end | |
| 186 | ||
| 187 | ||
| 188 | lemma quot_of_fract_add: | |
| 189 | "quot_of_fract (x + y) = | |
| 190 | (let (a,b) = quot_of_fract x; (c,d) = quot_of_fract y | |
| 191 | in normalize_quot (a * d + b * c, b * d))" | |
| 192 | by transfer (insert quot_of_fract_add_aux, | |
| 193 | simp_all add: Let_def case_prod_unfold normalize_quot_eq_iff) | |
| 194 | ||
| 195 | lemma quot_of_fract_uminus: | |
| 196 | "quot_of_fract (-x) = (let (a,b) = quot_of_fract x in (-a, b))" | |
| 64592 
7759f1766189
more fine-grained type class hierarchy for div and mod
 haftmann parents: 
64591diff
changeset | 197 | by transfer (auto simp: case_prod_unfold Let_def normalize_quot_def dvd_neg_div mult_unit_dvd_iff) | 
| 63500 | 198 | |
| 199 | lemma quot_of_fract_diff: | |
| 200 | "quot_of_fract (x - y) = | |
| 201 | (let (a,b) = quot_of_fract x; (c,d) = quot_of_fract y | |
| 202 | in normalize_quot (a * d - b * c, b * d))" (is "_ = ?rhs") | |
| 203 | proof - | |
| 204 | have "x - y = x + -y" by simp | |
| 205 | also have "quot_of_fract \<dots> = ?rhs" | |
| 206 | by (simp only: quot_of_fract_add quot_of_fract_uminus Let_def case_prod_unfold) simp_all | |
| 207 | finally show ?thesis . | |
| 208 | qed | |
| 209 | ||
| 210 | lemma normalize_quot_mult_coprime: | |
| 211 | assumes "coprime a b" "coprime c d" "unit_factor b = 1" "unit_factor d = 1" | |
| 212 | defines "e \<equiv> fst (normalize_quot (a, d))" and "f \<equiv> snd (normalize_quot (a, d))" | |
| 213 | and "g \<equiv> fst (normalize_quot (c, b))" and "h \<equiv> snd (normalize_quot (c, b))" | |
| 214 | shows "normalize_quot (a * c, b * d) = (e * g, f * h)" | |
| 215 | proof (rule normalize_quotI) | |
| 67051 | 216 | from assms have "gcd a b = 1" "gcd c d = 1" | 
| 217 | by simp_all | |
| 63500 | 218 | from assms have "b \<noteq> 0" "d \<noteq> 0" by auto | 
| 67051 | 219 | with assms have "normalize b = b" "normalize d = d" | 
| 220 | by (auto intro: normalize_unit_factor_eqI) | |
| 221 | from normalize_quotE [OF \<open>b \<noteq> 0\<close>, of c] guess k . | |
| 222 | note k = this [folded \<open>gcd a b = 1\<close> \<open>gcd c d = 1\<close> assms(3) assms(4)] | |
| 223 | from normalize_quotE [OF \<open>d \<noteq> 0\<close>, of a] guess l . | |
| 224 | note l = this [folded \<open>gcd a b = 1\<close> \<open>gcd c d = 1\<close> assms(3) assms(4)] | |
| 225 | from k l show "a * c * (f * h) = b * d * (e * g)" | |
| 226 | by (metis e_def f_def g_def h_def mult.commute mult.left_commute) | |
| 63500 | 227 | from assms have [simp]: "unit_factor f = 1" "unit_factor h = 1" | 
| 228 | by simp_all | |
| 229 | from assms have "coprime e f" "coprime g h" by (simp_all add: coprime_normalize_quot) | |
| 67051 | 230 | with k l assms(1,2) \<open>b \<noteq> 0\<close> \<open>d \<noteq> 0\<close> \<open>unit_factor b = 1\<close> \<open>unit_factor d = 1\<close> | 
| 231 | \<open>normalize b = b\<close> \<open>normalize d = d\<close> | |
| 232 | show "(e * g, f * h) \<in> normalized_fracts" | |
| 233 | by (simp add: normalized_fracts_def unit_factor_mult e_def f_def g_def h_def | |
| 234 | coprime_normalize_quot dvd_unit_factor_div unit_factor_gcd) | |
| 235 | (metis coprime_mult_left_iff coprime_mult_right_iff) | |
| 63500 | 236 | qed (insert assms(3,4), auto) | 
| 237 | ||
| 238 | lemma normalize_quot_mult: | |
| 239 | assumes "snd x \<noteq> 0" "snd y \<noteq> 0" | |
| 240 | shows "normalize_quot (fst x * fst y, snd x * snd y) = normalize_quot | |
| 241 | (fst (normalize_quot x) * fst (normalize_quot y), | |
| 242 | snd (normalize_quot x) * snd (normalize_quot y))" | |
| 243 | proof - | |
| 244 | from normalize_quotE'[OF assms(1)] guess d . note d = this | |
| 245 | from normalize_quotE'[OF assms(2)] guess e . note e = this | |
| 246 | show ?thesis by (simp_all add: d e algebra_simps normalize_quot_eq_iff) | |
| 247 | qed | |
| 248 | ||
| 249 | lemma quot_of_fract_mult: | |
| 250 | "quot_of_fract (x * y) = | |
| 251 | (let (a,b) = quot_of_fract x; (c,d) = quot_of_fract y; | |
| 252 | (e,f) = normalize_quot (a,d); (g,h) = normalize_quot (c,b) | |
| 253 | in (e*g, f*h))" | |
| 67051 | 254 | by transfer | 
| 255 | (simp add: split_def Let_def coprime_normalize_quot normalize_quot_mult normalize_quot_mult_coprime) | |
| 63500 | 256 | |
| 257 | lemma normalize_quot_0 [simp]: | |
| 258 | "normalize_quot (0, x) = (0, 1)" "normalize_quot (x, 0) = (0, 1)" | |
| 259 | by (simp_all add: normalize_quot_def) | |
| 260 | ||
| 261 | lemma normalize_quot_eq_0_iff [simp]: "fst (normalize_quot x) = 0 \<longleftrightarrow> fst x = 0 \<or> snd x = 0" | |
| 262 | by (auto simp: normalize_quot_def case_prod_unfold Let_def div_mult_unit2 dvd_div_eq_0_iff) | |
| 263 | ||
| 264 | lemma fst_quot_of_fract_0_imp: "fst (quot_of_fract x) = 0 \<Longrightarrow> snd (quot_of_fract x) = 1" | |
| 265 | by transfer auto | |
| 266 | ||
| 267 | lemma normalize_quot_swap: | |
| 268 | assumes "a \<noteq> 0" "b \<noteq> 0" | |
| 269 | defines "a' \<equiv> fst (normalize_quot (a, b))" and "b' \<equiv> snd (normalize_quot (a, b))" | |
| 270 | shows "normalize_quot (b, a) = (b' div unit_factor a', a' div unit_factor a')" | |
| 271 | proof (rule normalize_quotI) | |
| 272 | from normalize_quotE[OF assms(2), of a] guess d . note d = this [folded assms(3,4)] | |
| 273 | show "b * (a' div unit_factor a') = a * (b' div unit_factor a')" | |
| 274 | using assms(1,2) d | |
| 275 | by (simp add: div_unit_factor [symmetric] unit_div_mult_swap mult_ac del: div_unit_factor) | |
| 276 | have "coprime a' b'" by (simp add: a'_def b'_def coprime_normalize_quot) | |
| 277 | thus "(b' div unit_factor a', a' div unit_factor a') \<in> normalized_fracts" | |
| 67051 | 278 | using assms(1,2) d | 
| 67078 
6a85b8a9c28c
removed overambitious simp rules from e7e54a0b9197
 haftmann parents: 
67051diff
changeset | 279 | by (auto simp add: normalized_fracts_def ac_simps dvd_div_unit_iff elim: coprime_imp_coprime) | 
| 63500 | 280 | qed fact+ | 
| 281 | ||
| 282 | lemma quot_of_fract_inverse: | |
| 283 | "quot_of_fract (inverse x) = | |
| 284 | (let (a,b) = quot_of_fract x; d = unit_factor a | |
| 285 | in if d = 0 then (0, 1) else (b div d, a div d))" | |
| 286 | proof (transfer, goal_cases) | |
| 287 | case (1 x) | |
| 288 | from normalize_quot_swap[of "fst x" "snd x"] show ?case | |
| 289 | by (auto simp: Let_def case_prod_unfold) | |
| 290 | qed | |
| 291 | ||
| 292 | lemma normalize_quot_div_unit_left: | |
| 293 | fixes x y u | |
| 294 | assumes "is_unit u" | |
| 295 | defines "x' \<equiv> fst (normalize_quot (x, y))" and "y' \<equiv> snd (normalize_quot (x, y))" | |
| 296 | shows "normalize_quot (x div u, y) = (x' div u, y')" | |
| 297 | proof (cases "y = 0") | |
| 298 | case False | |
| 67051 | 299 | define v where "v = 1 div u" | 
| 300 | with \<open>is_unit u\<close> have "is_unit v" and u: "\<And>a. a div u = a * v" | |
| 301 | by simp_all | |
| 302 | from \<open>is_unit v\<close> have "coprime v = top" | |
| 303 | by (simp add: fun_eq_iff is_unit_left_imp_coprime) | |
| 304 | from normalize_quotE[OF False, of x] guess d . | |
| 305 | note d = this[folded assms(2,3)] | |
| 306 | from assms have "coprime x' y'" "unit_factor y' = 1" | |
| 307 | by (simp_all add: coprime_normalize_quot) | |
| 308 | with d \<open>coprime v = top\<close> have "normalize_quot (x * v, y) = (x' * v, y')" | |
| 309 | by (auto simp: normalized_fracts_def intro: normalize_quotI) | |
| 310 | then show ?thesis | |
| 311 | by (simp add: u) | |
| 63500 | 312 | qed (simp_all add: assms) | 
| 313 | ||
| 314 | lemma normalize_quot_div_unit_right: | |
| 315 | fixes x y u | |
| 316 | assumes "is_unit u" | |
| 317 | defines "x' \<equiv> fst (normalize_quot (x, y))" and "y' \<equiv> snd (normalize_quot (x, y))" | |
| 318 | shows "normalize_quot (x, y div u) = (x' * u, y')" | |
| 319 | proof (cases "y = 0") | |
| 320 | case False | |
| 321 | from normalize_quotE[OF this, of x] guess d . note d = this[folded assms(2,3)] | |
| 322 | from assms have "coprime x' y'" "unit_factor y' = 1" by (simp_all add: coprime_normalize_quot) | |
| 67051 | 323 | with d \<open>is_unit u\<close> show ?thesis | 
| 324 | by (auto simp add: normalized_fracts_def is_unit_left_imp_coprime unit_div_eq_0_iff intro: normalize_quotI) | |
| 63500 | 325 | qed (simp_all add: assms) | 
| 326 | ||
| 327 | lemma normalize_quot_normalize_left: | |
| 328 | fixes x y u | |
| 329 | defines "x' \<equiv> fst (normalize_quot (x, y))" and "y' \<equiv> snd (normalize_quot (x, y))" | |
| 330 | shows "normalize_quot (normalize x, y) = (x' div unit_factor x, y')" | |
| 331 | using normalize_quot_div_unit_left[of "unit_factor x" x y] | |
| 332 | by (cases "x = 0") (simp_all add: assms) | |
| 333 | ||
| 334 | lemma normalize_quot_normalize_right: | |
| 335 | fixes x y u | |
| 336 | defines "x' \<equiv> fst (normalize_quot (x, y))" and "y' \<equiv> snd (normalize_quot (x, y))" | |
| 337 | shows "normalize_quot (x, normalize y) = (x' * unit_factor y, y')" | |
| 338 | using normalize_quot_div_unit_right[of "unit_factor y" x y] | |
| 339 | by (cases "y = 0") (simp_all add: assms) | |
| 340 | ||
| 341 | lemma quot_of_fract_0 [simp]: "quot_of_fract 0 = (0, 1)" | |
| 342 | by transfer auto | |
| 343 | ||
| 344 | lemma quot_of_fract_1 [simp]: "quot_of_fract 1 = (1, 1)" | |
| 345 | by transfer (rule normalize_quotI, simp_all add: normalized_fracts_def) | |
| 346 | ||
| 347 | lemma quot_of_fract_divide: | |
| 348 | "quot_of_fract (x / y) = (if y = 0 then (0, 1) else | |
| 349 | (let (a,b) = quot_of_fract x; (c,d) = quot_of_fract y; | |
| 350 | (e,f) = normalize_quot (a,c); (g,h) = normalize_quot (d,b) | |
| 351 | in (e * g, f * h)))" (is "_ = ?rhs") | |
| 352 | proof (cases "y = 0") | |
| 353 | case False | |
| 354 | hence A: "fst (quot_of_fract y) \<noteq> 0" by transfer auto | |
| 355 | have "x / y = x * inverse y" by (simp add: divide_inverse) | |
| 356 | also from False A have "quot_of_fract \<dots> = ?rhs" | |
| 357 | by (simp only: quot_of_fract_mult quot_of_fract_inverse) | |
| 358 | (simp_all add: Let_def case_prod_unfold fst_quot_of_fract_0_imp | |
| 359 | normalize_quot_div_unit_left normalize_quot_div_unit_right | |
| 360 | normalize_quot_normalize_right normalize_quot_normalize_left) | |
| 361 | finally show ?thesis . | |
| 362 | qed simp_all | |
| 363 | ||
| 364 | end |