5 Author: Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen |
5 Author: Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen |
6 Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4 |
6 Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4 |
7 Construction of Cauchy Reals by Brian Huffman, 2010 |
7 Construction of Cauchy Reals by Brian Huffman, 2010 |
8 *) |
8 *) |
9 |
9 |
10 section {* Development of the Reals using Cauchy Sequences *} |
10 section \<open>Development of the Reals using Cauchy Sequences\<close> |
11 |
11 |
12 theory Real |
12 theory Real |
13 imports Rat Conditionally_Complete_Lattices |
13 imports Rat Conditionally_Complete_Lattices |
14 begin |
14 begin |
15 |
15 |
16 text {* |
16 text \<open> |
17 This theory contains a formalization of the real numbers as |
17 This theory contains a formalization of the real numbers as |
18 equivalence classes of Cauchy sequences of rationals. See |
18 equivalence classes of Cauchy sequences of rationals. See |
19 @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative |
19 @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative |
20 construction using Dedekind cuts. |
20 construction using Dedekind cuts. |
21 *} |
21 \<close> |
22 |
22 |
23 subsection {* Preliminary lemmas *} |
23 subsection \<open>Preliminary lemmas\<close> |
24 |
24 |
25 lemma add_diff_add: |
25 lemma add_diff_add: |
26 fixes a b c d :: "'a::ab_group_add" |
26 fixes a b c d :: "'a::ab_group_add" |
27 shows "(a + c) - (b + d) = (a - b) + (c - d)" |
27 shows "(a + c) - (b + d) = (a - b) + (c - d)" |
28 by simp |
28 by simp |
216 obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b" |
216 obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b" |
217 proof |
217 proof |
218 show "0 < v/b" using v b(1) by simp |
218 show "0 < v/b" using v b(1) by simp |
219 show "0 < u/a" using u a(1) by simp |
219 show "0 < u/a" using u a(1) by simp |
220 show "r = a * (u/a) + (v/b) * b" |
220 show "r = a * (u/a) + (v/b) * b" |
221 using a(1) b(1) `r = u + v` by simp |
221 using a(1) b(1) \<open>r = u + v\<close> by simp |
222 qed |
222 qed |
223 obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s" |
223 obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s" |
224 using cauchyD [OF X s] .. |
224 using cauchyD [OF X s] .. |
225 obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t" |
225 obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t" |
226 using cauchyD [OF Y t] .. |
226 using cauchyD [OF Y t] .. |
246 shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)" |
246 shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)" |
247 proof - |
247 proof - |
248 obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>" |
248 obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>" |
249 using nz unfolding vanishes_def by (auto simp add: not_less) |
249 using nz unfolding vanishes_def by (auto simp add: not_less) |
250 obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t" |
250 obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t" |
251 using `0 < r` by (rule obtain_pos_sum) |
251 using \<open>0 < r\<close> by (rule obtain_pos_sum) |
252 obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s" |
252 obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s" |
253 using cauchyD [OF X s] .. |
253 using cauchyD [OF X s] .. |
254 obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>" |
254 obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>" |
255 using r by fast |
255 using r by fast |
256 have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s" |
256 have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s" |
257 using i `i \<le> k` by auto |
257 using i \<open>i \<le> k\<close> by auto |
258 have "X k \<le> - r \<or> r \<le> X k" |
258 have "X k \<le> - r \<or> r \<le> X k" |
259 using `r \<le> \<bar>X k\<bar>` by auto |
259 using \<open>r \<le> \<bar>X k\<bar>\<close> by auto |
260 hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" |
260 hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" |
261 unfolding `r = s + t` using k by auto |
261 unfolding \<open>r = s + t\<close> using k by auto |
262 hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" .. |
262 hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" .. |
263 thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" |
263 thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" |
264 using t by auto |
264 using t by auto |
265 qed |
265 qed |
266 |
266 |
332 by (simp add: inverse_diff_inverse abs_mult) |
332 by (simp add: inverse_diff_inverse abs_mult) |
333 also have "\<dots> < inverse a * s * inverse b" |
333 also have "\<dots> < inverse a * s * inverse b" |
334 apply (intro mult_strict_mono' less_imp_inverse_less) |
334 apply (intro mult_strict_mono' less_imp_inverse_less) |
335 apply (simp_all add: a b i j k n) |
335 apply (simp_all add: a b i j k n) |
336 done |
336 done |
337 also note `inverse a * s * inverse b = r` |
337 also note \<open>inverse a * s * inverse b = r\<close> |
338 finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" . |
338 finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" . |
339 qed |
339 qed |
340 thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" .. |
340 thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" .. |
341 qed |
341 qed |
342 |
342 |
343 subsection {* Equivalence relation on Cauchy sequences *} |
343 subsection \<open>Equivalence relation on Cauchy sequences\<close> |
344 |
344 |
345 definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool" |
345 definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool" |
346 where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))" |
346 where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))" |
347 |
347 |
348 lemma realrelI [intro?]: |
348 lemma realrelI [intro?]: |
520 unfolding realrel_def by simp_all |
520 unfolding realrel_def by simp_all |
521 assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n" |
521 assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n" |
522 then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n" |
522 then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n" |
523 by fast |
523 by fast |
524 obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" |
524 obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t" |
525 using `0 < r` by (rule obtain_pos_sum) |
525 using \<open>0 < r\<close> by (rule obtain_pos_sum) |
526 obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s" |
526 obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s" |
527 using vanishesD [OF XY s] .. |
527 using vanishesD [OF XY s] .. |
528 have "\<forall>n\<ge>max i j. t < Y n" |
528 have "\<forall>n\<ge>max i j. t < Y n" |
529 proof (clarsimp) |
529 proof (clarsimp) |
530 fix n assume n: "i \<le> n" "j \<le> n" |
530 fix n assume n: "i \<le> n" "j \<le> n" |
876 done |
876 done |
877 have 1: "\<forall>x\<in>S. x \<le> Real B" |
877 have 1: "\<forall>x\<in>S. x \<le> Real B" |
878 proof |
878 proof |
879 fix x assume "x \<in> S" |
879 fix x assume "x \<in> S" |
880 then show "x \<le> Real B" |
880 then show "x \<le> Real B" |
881 using PB [unfolded P_def] `cauchy B` |
881 using PB [unfolded P_def] \<open>cauchy B\<close> |
882 by (simp add: le_RealI) |
882 by (simp add: le_RealI) |
883 qed |
883 qed |
884 have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z" |
884 have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z" |
885 apply clarify |
885 apply clarify |
886 apply (erule contrapos_pp) |
886 apply (erule contrapos_pp) |
887 apply (simp add: not_le) |
887 apply (simp add: not_le) |
888 apply (drule less_RealD [OF `cauchy A`], clarify) |
888 apply (drule less_RealD [OF \<open>cauchy A\<close>], clarify) |
889 apply (subgoal_tac "\<not> P (A n)") |
889 apply (subgoal_tac "\<not> P (A n)") |
890 apply (simp add: P_def not_le, clarify) |
890 apply (simp add: P_def not_le, clarify) |
891 apply (erule rev_bexI) |
891 apply (erule rev_bexI) |
892 apply (erule (1) less_trans) |
892 apply (erule (1) less_trans) |
893 apply (simp add: PA) |
893 apply (simp add: PA) |
908 finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" . |
908 finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" . |
909 qed |
909 qed |
910 thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" .. |
910 thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" .. |
911 qed |
911 qed |
912 hence 3: "Real B = Real A" |
912 hence 3: "Real B = Real A" |
913 by (simp add: eq_Real `cauchy A` `cauchy B` width) |
913 by (simp add: eq_Real \<open>cauchy A\<close> \<open>cauchy B\<close> width) |
914 show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)" |
914 show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)" |
915 using 1 2 3 by (rule_tac x="Real B" in exI, simp) |
915 using 1 2 3 by (rule_tac x="Real B" in exI, simp) |
916 qed |
916 qed |
917 |
917 |
918 instantiation real :: linear_continuum |
918 instantiation real :: linear_continuum |
919 begin |
919 begin |
920 |
920 |
921 subsection{*Supremum of a set of reals*} |
921 subsection\<open>Supremum of a set of reals\<close> |
922 |
922 |
923 definition "Sup X = (LEAST z::real. \<forall>x\<in>X. x \<le> z)" |
923 definition "Sup X = (LEAST z::real. \<forall>x\<in>X. x \<le> z)" |
924 definition "Inf (X::real set) = - Sup (uminus ` X)" |
924 definition "Inf (X::real set) = - Sup (uminus ` X)" |
925 |
925 |
926 instance |
926 instance |
952 using zero_neq_one by blast |
952 using zero_neq_one by blast |
953 qed |
953 qed |
954 end |
954 end |
955 |
955 |
956 |
956 |
957 subsection {* Hiding implementation details *} |
957 subsection \<open>Hiding implementation details\<close> |
958 |
958 |
959 hide_const (open) vanishes cauchy positive Real |
959 hide_const (open) vanishes cauchy positive Real |
960 |
960 |
961 declare Real_induct [induct del] |
961 declare Real_induct [induct del] |
962 declare Abs_real_induct [induct del] |
962 declare Abs_real_induct [induct del] |
963 declare Abs_real_cases [cases del] |
963 declare Abs_real_cases [cases del] |
964 |
964 |
965 lifting_update real.lifting |
965 lifting_update real.lifting |
966 lifting_forget real.lifting |
966 lifting_forget real.lifting |
967 |
967 |
968 subsection{*More Lemmas*} |
968 subsection\<open>More Lemmas\<close> |
969 |
969 |
970 text {* BH: These lemmas should not be necessary; they should be |
970 text \<open>BH: These lemmas should not be necessary; they should be |
971 covered by existing simp rules and simplification procedures. *} |
971 covered by existing simp rules and simplification procedures.\<close> |
972 |
972 |
973 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)" |
973 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)" |
974 by simp (* solved by linordered_ring_less_cancel_factor simproc *) |
974 by simp (* solved by linordered_ring_less_cancel_factor simproc *) |
975 |
975 |
976 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)" |
976 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)" |
1318 unfolding real_of_nat_def by (rule ex_less_of_nat) |
1318 unfolding real_of_nat_def by (rule ex_less_of_nat) |
1319 |
1319 |
1320 lemma reals_Archimedean3: |
1320 lemma reals_Archimedean3: |
1321 assumes x_greater_zero: "0 < x" |
1321 assumes x_greater_zero: "0 < x" |
1322 shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x" |
1322 shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x" |
1323 unfolding real_of_nat_def using `0 < x` |
1323 unfolding real_of_nat_def using \<open>0 < x\<close> |
1324 by (auto intro: ex_less_of_nat_mult) |
1324 by (auto intro: ex_less_of_nat_mult) |
1325 |
1325 |
1326 |
1326 |
1327 subsection{* Rationals *} |
1327 subsection\<open>Rationals\<close> |
1328 |
1328 |
1329 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>" |
1329 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>" |
1330 by (simp add: real_eq_of_nat) |
1330 by (simp add: real_eq_of_nat) |
1331 |
1331 |
1332 lemma Rats_eq_int_div_int: |
1332 lemma Rats_eq_int_div_int: |
1375 lemma Rats_abs_nat_div_natE: |
1375 lemma Rats_abs_nat_div_natE: |
1376 assumes "x \<in> \<rat>" |
1376 assumes "x \<in> \<rat>" |
1377 obtains m n :: nat |
1377 obtains m n :: nat |
1378 where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1" |
1378 where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1" |
1379 proof - |
1379 proof - |
1380 from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n" |
1380 from \<open>x \<in> \<rat>\<close> obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n" |
1381 by(auto simp add: Rats_eq_int_div_nat) |
1381 by(auto simp add: Rats_eq_int_div_nat) |
1382 hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp |
1382 hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp |
1383 then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast |
1383 then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast |
1384 let ?gcd = "gcd m n" |
1384 let ?gcd = "gcd m n" |
1385 from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp |
1385 from \<open>n\<noteq>0\<close> have gcd: "?gcd \<noteq> 0" by simp |
1386 let ?k = "m div ?gcd" |
1386 let ?k = "m div ?gcd" |
1387 let ?l = "n div ?gcd" |
1387 let ?l = "n div ?gcd" |
1388 let ?gcd' = "gcd ?k ?l" |
1388 let ?gcd' = "gcd ?k ?l" |
1389 have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m" |
1389 have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m" |
1390 by (rule dvd_mult_div_cancel) |
1390 by (rule dvd_mult_div_cancel) |
1391 have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n" |
1391 have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n" |
1392 by (rule dvd_mult_div_cancel) |
1392 by (rule dvd_mult_div_cancel) |
1393 from `n \<noteq> 0` and gcd_l |
1393 from \<open>n \<noteq> 0\<close> and gcd_l |
1394 have "?gcd * ?l \<noteq> 0" by simp |
1394 have "?gcd * ?l \<noteq> 0" by simp |
1395 then have "?l \<noteq> 0" by (blast dest!: mult_not_zero) |
1395 then have "?l \<noteq> 0" by (blast dest!: mult_not_zero) |
1396 moreover |
1396 moreover |
1397 have "\<bar>x\<bar> = real ?k / real ?l" |
1397 have "\<bar>x\<bar> = real ?k / real ?l" |
1398 proof - |
1398 proof - |
1412 with gcd show ?thesis by auto |
1412 with gcd show ?thesis by auto |
1413 qed |
1413 qed |
1414 ultimately show ?thesis .. |
1414 ultimately show ?thesis .. |
1415 qed |
1415 qed |
1416 |
1416 |
1417 subsection{*Density of the Rational Reals in the Reals*} |
1417 subsection\<open>Density of the Rational Reals in the Reals\<close> |
1418 |
1418 |
1419 text{* This density proof is due to Stefan Richter and was ported by TN. The |
1419 text\<open>This density proof is due to Stefan Richter and was ported by TN. The |
1420 original source is \emph{Real Analysis} by H.L. Royden. |
1420 original source is \emph{Real Analysis} by H.L. Royden. |
1421 It employs the Archimedean property of the reals. *} |
1421 It employs the Archimedean property of the reals.\<close> |
1422 |
1422 |
1423 lemma Rats_dense_in_real: |
1423 lemma Rats_dense_in_real: |
1424 fixes x :: real |
1424 fixes x :: real |
1425 assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y" |
1425 assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y" |
1426 proof - |
1426 proof - |
1427 from `x<y` have "0 < y-x" by simp |
1427 from \<open>x<y\<close> have "0 < y-x" by simp |
1428 with reals_Archimedean obtain q::nat |
1428 with reals_Archimedean obtain q::nat |
1429 where q: "inverse (real q) < y-x" and "0 < q" by auto |
1429 where q: "inverse (real q) < y-x" and "0 < q" by auto |
1430 def p \<equiv> "ceiling (y * real q) - 1" |
1430 def p \<equiv> "ceiling (y * real q) - 1" |
1431 def r \<equiv> "of_int p / real q" |
1431 def r \<equiv> "of_int p / real q" |
1432 from q have "x < y - inverse (real q)" by simp |
1432 from q have "x < y - inverse (real q)" by simp |
1433 also have "y - inverse (real q) \<le> r" |
1433 also have "y - inverse (real q) \<le> r" |
1434 unfolding r_def p_def |
1434 unfolding r_def p_def |
1435 by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`) |
1435 by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling \<open>0 < q\<close>) |
1436 finally have "x < r" . |
1436 finally have "x < r" . |
1437 moreover have "r < y" |
1437 moreover have "r < y" |
1438 unfolding r_def p_def |
1438 unfolding r_def p_def |
1439 by (simp add: divide_less_eq diff_less_eq `0 < q` |
1439 by (simp add: divide_less_eq diff_less_eq \<open>0 < q\<close> |
1440 less_ceiling_iff [symmetric]) |
1440 less_ceiling_iff [symmetric]) |
1441 moreover from r_def have "r \<in> \<rat>" by simp |
1441 moreover from r_def have "r \<in> \<rat>" by simp |
1442 ultimately show ?thesis by fast |
1442 ultimately show ?thesis by fast |
1443 qed |
1443 qed |
1444 |
1444 |
1445 lemma of_rat_dense: |
1445 lemma of_rat_dense: |
1446 fixes x y :: real |
1446 fixes x y :: real |
1447 assumes "x < y" |
1447 assumes "x < y" |
1448 shows "\<exists>q :: rat. x < of_rat q \<and> of_rat q < y" |
1448 shows "\<exists>q :: rat. x < of_rat q \<and> of_rat q < y" |
1449 using Rats_dense_in_real [OF `x < y`] |
1449 using Rats_dense_in_real [OF \<open>x < y\<close>] |
1450 by (auto elim: Rats_cases) |
1450 by (auto elim: Rats_cases) |
1451 |
1451 |
1452 |
1452 |
1453 subsection{*Numerals and Arithmetic*} |
1453 subsection\<open>Numerals and Arithmetic\<close> |
1454 |
1454 |
1455 lemma [code_abbrev]: |
1455 lemma [code_abbrev]: |
1456 "real_of_int (numeral k) = numeral k" |
1456 "real_of_int (numeral k) = numeral k" |
1457 "real_of_int (- numeral k) = - numeral k" |
1457 "real_of_int (- numeral k) = - numeral k" |
1458 by simp_all |
1458 by simp_all |
1459 |
1459 |
1460 text{*Collapse applications of @{const real} to @{const numeral}*} |
1460 text\<open>Collapse applications of @{const real} to @{const numeral}\<close> |
1461 lemma real_numeral [simp]: |
1461 lemma real_numeral [simp]: |
1462 "real (numeral v :: int) = numeral v" |
1462 "real (numeral v :: int) = numeral v" |
1463 "real (- numeral v :: int) = - numeral v" |
1463 "real (- numeral v :: int) = - numeral v" |
1464 by (simp_all add: real_of_int_def) |
1464 by (simp_all add: real_of_int_def) |
1465 |
1465 |
1466 lemma real_of_nat_numeral [simp]: |
1466 lemma real_of_nat_numeral [simp]: |
1467 "real (numeral v :: nat) = numeral v" |
1467 "real (numeral v :: nat) = numeral v" |
1468 by (simp add: real_of_nat_def) |
1468 by (simp add: real_of_nat_def) |
1469 |
1469 |
1470 declaration {* |
1470 declaration \<open> |
1471 K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2] |
1471 K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2] |
1472 (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *) |
1472 (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *) |
1473 #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2] |
1473 #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2] |
1474 (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *) |
1474 (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *) |
1475 #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add}, |
1475 #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add}, |
1480 @{thm real_of_int_def[symmetric]}, @{thm real_of_nat_def[symmetric]}] |
1480 @{thm real_of_int_def[symmetric]}, @{thm real_of_nat_def[symmetric]}] |
1481 #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"}) |
1481 #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"}) |
1482 #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}) |
1482 #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}) |
1483 #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat \<Rightarrow> real"}) |
1483 #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat \<Rightarrow> real"}) |
1484 #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int \<Rightarrow> real"})) |
1484 #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int \<Rightarrow> real"})) |
1485 *} |
1485 \<close> |
1486 |
1486 |
1487 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*} |
1487 subsection\<open>Simprules combining x+y and 0: ARE THEY NEEDED?\<close> |
1488 |
1488 |
1489 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" |
1489 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" |
1490 by arith |
1490 by arith |
1491 |
1491 |
1492 text {* FIXME: redundant with @{text add_eq_0_iff} below *} |
1492 text \<open>FIXME: redundant with @{text add_eq_0_iff} below\<close> |
1493 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)" |
1493 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)" |
1494 by auto |
1494 by auto |
1495 |
1495 |
1496 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)" |
1496 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)" |
1497 by auto |
1497 by auto |
1503 by auto |
1503 by auto |
1504 |
1504 |
1505 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)" |
1505 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)" |
1506 by auto |
1506 by auto |
1507 |
1507 |
1508 subsection {* Lemmas about powers *} |
1508 subsection \<open>Lemmas about powers\<close> |
1509 |
1509 |
1510 text {* FIXME: declare this in Rings.thy or not at all *} |
1510 text \<open>FIXME: declare this in Rings.thy or not at all\<close> |
1511 declare abs_mult_self [simp] |
1511 declare abs_mult_self [simp] |
1512 |
1512 |
1513 (* used by Import/HOL/real.imp *) |
1513 (* used by Import/HOL/real.imp *) |
1514 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n" |
1514 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n" |
1515 by simp |
1515 by simp |
1516 |
1516 |
1517 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n" |
1517 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n" |
1518 by (simp add: of_nat_less_two_power real_of_nat_def) |
1518 by (simp add: of_nat_less_two_power real_of_nat_def) |
1519 |
1519 |
1520 text {* TODO: no longer real-specific; rename and move elsewhere *} |
1520 text \<open>TODO: no longer real-specific; rename and move elsewhere\<close> |
1521 lemma realpow_Suc_le_self: |
1521 lemma realpow_Suc_le_self: |
1522 fixes r :: "'a::linordered_semidom" |
1522 fixes r :: "'a::linordered_semidom" |
1523 shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r" |
1523 shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r" |
1524 by (insert power_decreasing [of 1 "Suc n" r], simp) |
1524 by (insert power_decreasing [of 1 "Suc n" r], simp) |
1525 |
1525 |
1526 text {* TODO: no longer real-specific; rename and move elsewhere *} |
1526 text \<open>TODO: no longer real-specific; rename and move elsewhere\<close> |
1527 lemma realpow_minus_mult: |
1527 lemma realpow_minus_mult: |
1528 fixes x :: "'a::monoid_mult" |
1528 fixes x :: "'a::monoid_mult" |
1529 shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n" |
1529 shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n" |
1530 by (simp add: power_Suc power_commutes split add: nat_diff_split) |
1530 by (simp add: power_Suc power_commutes split add: nat_diff_split) |
1531 |
1531 |
1532 text {* FIXME: declare this [simp] for all types, or not at all *} |
1532 text \<open>FIXME: declare this [simp] for all types, or not at all\<close> |
1533 lemma real_two_squares_add_zero_iff [simp]: |
1533 lemma real_two_squares_add_zero_iff [simp]: |
1534 "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)" |
1534 "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)" |
1535 by (rule sum_squares_eq_zero_iff) |
1535 by (rule sum_squares_eq_zero_iff) |
1536 |
1536 |
1537 text {* FIXME: declare this [simp] for all types, or not at all *} |
1537 text \<open>FIXME: declare this [simp] for all types, or not at all\<close> |
1538 lemma realpow_two_sum_zero_iff [simp]: |
1538 lemma realpow_two_sum_zero_iff [simp]: |
1539 "(x\<^sup>2 + y\<^sup>2 = (0::real)) = (x = 0 & y = 0)" |
1539 "(x\<^sup>2 + y\<^sup>2 = (0::real)) = (x = 0 & y = 0)" |
1540 by (rule sum_power2_eq_zero_iff) |
1540 by (rule sum_power2_eq_zero_iff) |
1541 |
1541 |
1542 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))" |
1542 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))" |
1604 lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]: |
1604 lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]: |
1605 "real a \<le> (- numeral x::real) ^ n \<longleftrightarrow> a \<le> (- numeral x::int) ^ n" |
1605 "real a \<le> (- numeral x::real) ^ n \<longleftrightarrow> a \<le> (- numeral x::int) ^ n" |
1606 unfolding real_of_int_le_iff[symmetric] by simp |
1606 unfolding real_of_int_le_iff[symmetric] by simp |
1607 |
1607 |
1608 |
1608 |
1609 subsection{*Density of the Reals*} |
1609 subsection\<open>Density of the Reals\<close> |
1610 |
1610 |
1611 lemma real_lbound_gt_zero: |
1611 lemma real_lbound_gt_zero: |
1612 "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2" |
1612 "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2" |
1613 apply (rule_tac x = " (min d1 d2) /2" in exI) |
1613 apply (rule_tac x = " (min d1 d2) /2" in exI) |
1614 apply (simp add: min_def) |
1614 apply (simp add: min_def) |
1615 done |
1615 done |
1616 |
1616 |
1617 |
1617 |
1618 text{*Similar results are proved in @{text Fields}*} |
1618 text\<open>Similar results are proved in @{text Fields}\<close> |
1619 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)" |
1619 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)" |
1620 by auto |
1620 by auto |
1621 |
1621 |
1622 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y" |
1622 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y" |
1623 by auto |
1623 by auto |
1624 |
1624 |
1625 lemma real_sum_of_halves: "x/2 + x/2 = (x::real)" |
1625 lemma real_sum_of_halves: "x/2 + x/2 = (x::real)" |
1626 by simp |
1626 by simp |
1627 |
1627 |
1628 subsection{*Absolute Value Function for the Reals*} |
1628 subsection\<open>Absolute Value Function for the Reals\<close> |
1629 |
1629 |
1630 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))" |
1630 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))" |
1631 by (simp add: abs_if) |
1631 by (simp add: abs_if) |
1632 |
1632 |
1633 (* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *) |
1633 (* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *) |
1774 then have "i < b + j * b" "j * b < 1 + i" |
1774 then have "i < b + j * b" "j * b < 1 + i" |
1775 unfolding real_of_int_less_iff[symmetric] by auto |
1775 unfolding real_of_int_less_iff[symmetric] by auto |
1776 then have "(j - i div b) * b \<le> i mod b" "i mod b < ((j - i div b) + 1) * b" |
1776 then have "(j - i div b) * b \<le> i mod b" "i mod b < ((j - i div b) + 1) * b" |
1777 by (auto simp: field_simps) |
1777 by (auto simp: field_simps) |
1778 then have "(j - i div b) * b < 1 * b" "0 * b < ((j - i div b) + 1) * b" |
1778 then have "(j - i div b) * b < 1 * b" "0 * b < ((j - i div b) + 1) * b" |
1779 using pos_mod_bound[OF `0<b`, of i] pos_mod_sign[OF `0<b`, of i] by linarith+ |
1779 using pos_mod_bound[OF \<open>0<b\<close>, of i] pos_mod_sign[OF \<open>0<b\<close>, of i] by linarith+ |
1780 then have "j = i div b" |
1780 then have "j = i div b" |
1781 using `0 < b` unfolding mult_less_cancel_right by auto } |
1781 using \<open>0 < b\<close> unfolding mult_less_cancel_right by auto } |
1782 with `0 < b` show ?thesis |
1782 with \<open>0 < b\<close> show ?thesis |
1783 by (auto split: floor_split simp: field_simps) |
1783 by (auto split: floor_split simp: field_simps) |
1784 qed auto |
1784 qed auto |
1785 |
1785 |
1786 lemma floor_divide_eq_div: |
1786 lemma floor_divide_eq_div: |
1787 "floor (real a / real b) = a div b" |
1787 "floor (real a / real b) = a div b" |
1916 lemma ceiling_numeral_power[simp]: |
1916 lemma ceiling_numeral_power[simp]: |
1917 "\<lceil>numeral x ^ n\<rceil> = numeral x ^ n" |
1917 "\<lceil>numeral x ^ n\<rceil> = numeral x ^ n" |
1918 by (metis ceiling_of_int of_int_numeral of_int_power) |
1918 by (metis ceiling_of_int of_int_numeral of_int_power) |
1919 |
1919 |
1920 |
1920 |
1921 subsection {* Implementation of rational real numbers *} |
1921 subsection \<open>Implementation of rational real numbers\<close> |
1922 |
1922 |
1923 text {* Formal constructor *} |
1923 text \<open>Formal constructor\<close> |
1924 |
1924 |
1925 definition Ratreal :: "rat \<Rightarrow> real" where |
1925 definition Ratreal :: "rat \<Rightarrow> real" where |
1926 [code_abbrev, simp]: "Ratreal = of_rat" |
1926 [code_abbrev, simp]: "Ratreal = of_rat" |
1927 |
1927 |
1928 code_datatype Ratreal |
1928 code_datatype Ratreal |
1929 |
1929 |
1930 |
1930 |
1931 text {* Numerals *} |
1931 text \<open>Numerals\<close> |
1932 |
1932 |
1933 lemma [code_abbrev]: |
1933 lemma [code_abbrev]: |
1934 "(of_rat (of_int a) :: real) = of_int a" |
1934 "(of_rat (of_int a) :: real) = of_int a" |
1935 by simp |
1935 by simp |
1936 |
1936 |
2069 instance .. |
2069 instance .. |
2070 |
2070 |
2071 end |
2071 end |
2072 |
2072 |
2073 |
2073 |
2074 subsection {* Setup for Nitpick *} |
2074 subsection \<open>Setup for Nitpick\<close> |
2075 |
2075 |
2076 declaration {* |
2076 declaration \<open> |
2077 Nitpick_HOL.register_frac_type @{type_name real} |
2077 Nitpick_HOL.register_frac_type @{type_name real} |
2078 [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}), |
2078 [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}), |
2079 (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}), |
2079 (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}), |
2080 (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}), |
2080 (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}), |
2081 (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}), |
2081 (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}), |
2082 (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}), |
2082 (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}), |
2083 (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}), |
2083 (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}), |
2084 (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}), |
2084 (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}), |
2085 (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})] |
2085 (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})] |
2086 *} |
2086 \<close> |
2087 |
2087 |
2088 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real |
2088 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real |
2089 ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real |
2089 ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real |
2090 times_real_inst.times_real uminus_real_inst.uminus_real |
2090 times_real_inst.times_real uminus_real_inst.uminus_real |
2091 zero_real_inst.zero_real |
2091 zero_real_inst.zero_real |
2092 |
2092 |
2093 |
2093 |
2094 subsection {* Setup for SMT *} |
2094 subsection \<open>Setup for SMT\<close> |
2095 |
2095 |
2096 ML_file "Tools/SMT/smt_real.ML" |
2096 ML_file "Tools/SMT/smt_real.ML" |
2097 ML_file "Tools/SMT/z3_real.ML" |
2097 ML_file "Tools/SMT/z3_real.ML" |
2098 |
2098 |
2099 lemma [z3_rule]: |
2099 lemma [z3_rule]: |