src/HOL/Real.thy
changeset 60758 d8d85a8172b5
parent 60429 d3d1e185cd63
child 61070 b72a990adfe2
equal deleted inserted replaced
60757:c09598a97436 60758:d8d85a8172b5
     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
    50     from r show "0 < r/2" by simp
    50     from r show "0 < r/2" by simp
    51     from r show "0 < r/2" by simp
    51     from r show "0 < r/2" by simp
    52     show "r = r/2 + r/2" by simp
    52     show "r = r/2 + r/2" by simp
    53 qed
    53 qed
    54 
    54 
    55 subsection {* Sequences that converge to zero *}
    55 subsection \<open>Sequences that converge to zero\<close>
    56 
    56 
    57 definition
    57 definition
    58   vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    58   vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    59 where
    59 where
    60   "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
    60   "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
   118   have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   118   have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   119     by (simp add: b(2) abs_mult mult_strict_mono' a k)
   119     by (simp add: b(2) abs_mult mult_strict_mono' a k)
   120   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   120   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   121 qed
   121 qed
   122 
   122 
   123 subsection {* Cauchy sequences *}
   123 subsection \<open>Cauchy sequences\<close>
   124 
   124 
   125 definition
   125 definition
   126   cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   126   cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   127 where
   127 where
   128   "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   128   "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   194       assume "k \<le> n"
   194       assume "k \<le> n"
   195       have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   195       have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   196       also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   196       also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   197         by (rule abs_triangle_ineq)
   197         by (rule abs_triangle_ineq)
   198       also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   198       also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   199         by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
   199         by (rule add_le_less_mono, simp, simp add: k \<open>k \<le> n\<close>)
   200       finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   200       finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   201     qed
   201     qed
   202   qed
   202   qed
   203 qed
   203 qed
   204 
   204 
   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 
   280   obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   280   obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   281     using cauchy_not_vanishes [OF X nz] by fast
   281     using cauchy_not_vanishes [OF X nz] by fast
   282   from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   282   from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   283   obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   283   obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   284   proof
   284   proof
   285     show "0 < b * r * b" by (simp add: `0 < r` b)
   285     show "0 < b * r * b" by (simp add: \<open>0 < r\<close> b)
   286     show "r = inverse b * (b * r * b) * inverse b"
   286     show "r = inverse b * (b * r * b) * inverse b"
   287       using b by simp
   287       using b by simp
   288   qed
   288   qed
   289   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   289   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   290     using cauchyD [OF X s] ..
   290     using cauchyD [OF X s] ..
   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?]:
   366 
   366 
   367 lemma part_equivp_realrel: "part_equivp realrel"
   367 lemma part_equivp_realrel: "part_equivp realrel"
   368   by (fast intro: part_equivpI symp_realrel transp_realrel
   368   by (fast intro: part_equivpI symp_realrel transp_realrel
   369     realrel_refl cauchy_const)
   369     realrel_refl cauchy_const)
   370 
   370 
   371 subsection {* The field of real numbers *}
   371 subsection \<open>The field of real numbers\<close>
   372 
   372 
   373 quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   373 quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   374   morphisms rep_real Real
   374   morphisms rep_real Real
   375   by (rule part_equivp_realrel)
   375   by (rule part_equivp_realrel)
   376 
   376 
   507     by transfer (simp add: realrel_def)
   507     by transfer (simp add: realrel_def)
   508 qed
   508 qed
   509 
   509 
   510 end
   510 end
   511 
   511 
   512 subsection {* Positive reals *}
   512 subsection \<open>Positive reals\<close>
   513 
   513 
   514 lift_definition positive :: "real \<Rightarrow> bool"
   514 lift_definition positive :: "real \<Rightarrow> bool"
   515   is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   515   is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   516 proof -
   516 proof -
   517   { fix X Y
   517   { fix X Y
   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"
   682     unfolding floor_real_def using floor_exists1 by (rule theI')
   682     unfolding floor_real_def using floor_exists1 by (rule theI')
   683 qed
   683 qed
   684 
   684 
   685 end
   685 end
   686 
   686 
   687 subsection {* Completeness *}
   687 subsection \<open>Completeness\<close>
   688 
   688 
   689 lemma not_positive_Real:
   689 lemma not_positive_Real:
   690   assumes X: "cauchy X"
   690   assumes X: "cauchy X"
   691   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   691   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   692 unfolding positive_Real [OF X]
   692 unfolding positive_Real [OF X]
   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)"
   978 
   978 
   979 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
   979 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
   980 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
   980 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
   981 
   981 
   982 
   982 
   983 subsection {* Embedding numbers into the Reals *}
   983 subsection \<open>Embedding numbers into the Reals\<close>
   984 
   984 
   985 abbreviation
   985 abbreviation
   986   real_of_nat :: "nat \<Rightarrow> real"
   986   real_of_nat :: "nat \<Rightarrow> real"
   987 where
   987 where
   988   "real_of_nat \<equiv> of_nat"
   988   "real_of_nat \<equiv> of_nat"
  1165 
  1165 
  1166 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1166 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1167 unfolding real_of_int_def by (rule Ints_of_int)
  1167 unfolding real_of_int_def by (rule Ints_of_int)
  1168 
  1168 
  1169 
  1169 
  1170 subsection{*Embedding the Naturals into the Reals*}
  1170 subsection\<open>Embedding the Naturals into the Reals\<close>
  1171 
  1171 
  1172 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1172 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1173 by (simp add: real_of_nat_def)
  1173 by (simp add: real_of_nat_def)
  1174 
  1174 
  1175 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1175 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1304 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1304 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1305 
  1305 
  1306 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1306 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1307 unfolding real_of_nat_def by (rule Ints_of_nat)
  1307 unfolding real_of_nat_def by (rule Ints_of_nat)
  1308 
  1308 
  1309 subsection {* The Archimedean Property of the Reals *}
  1309 subsection \<open>The Archimedean Property of the Reals\<close>
  1310 
  1310 
  1311 theorem reals_Archimedean:
  1311 theorem reals_Archimedean:
  1312   assumes x_pos: "0 < x"
  1312   assumes x_pos: "0 < x"
  1313   shows "\<exists>n. inverse (real (Suc n)) < x"
  1313   shows "\<exists>n. inverse (real (Suc n)) < x"
  1314   unfolding real_of_nat_def using x_pos
  1314   unfolding real_of_nat_def using x_pos
  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:
  1336   proof
  1336   proof
  1337     fix x::real assume "x : \<rat>"
  1337     fix x::real assume "x : \<rat>"
  1338     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1338     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1339     have "of_rat r : ?S"
  1339     have "of_rat r : ?S"
  1340       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1340       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1341     thus "x : ?S" using `x = of_rat r` by simp
  1341     thus "x : ?S" using \<open>x = of_rat r\<close> by simp
  1342   qed
  1342   qed
  1343 next
  1343 next
  1344   show "?S \<subseteq> \<rat>"
  1344   show "?S \<subseteq> \<rat>"
  1345   proof(auto simp:Rats_def)
  1345   proof(auto simp:Rats_def)
  1346     fix i j :: int assume "j \<noteq> 0"
  1346     fix i j :: int assume "j \<noteq> 0"
  1360     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1360     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1361       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1361       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1362     thus ?thesis by blast
  1362     thus ?thesis by blast
  1363   next
  1363   next
  1364     assume "~ j>0"
  1364     assume "~ j>0"
  1365     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1365     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using \<open>j\<noteq>0\<close>
  1366       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1366       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1367     thus ?thesis by blast
  1367     thus ?thesis by blast
  1368   qed
  1368   qed
  1369 next
  1369 next
  1370   fix i::int and n::nat assume "0 < n"
  1370   fix i::int and n::nat assume "0 < n"
  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 *)
  1645  
  1645  
  1646 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1646 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1647 by simp
  1647 by simp
  1648 
  1648 
  1649 
  1649 
  1650 subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
  1650 subsection\<open>Floor and Ceiling Functions from the Reals to the Integers\<close>
  1651 
  1651 
  1652 (* FIXME: theorems for negative numerals *)
  1652 (* FIXME: theorems for negative numerals *)
  1653 lemma numeral_less_real_of_int_iff [simp]:
  1653 lemma numeral_less_real_of_int_iff [simp]:
  1654      "((numeral n) < real (m::int)) = (numeral n < m)"
  1654      "((numeral n) < real (m::int)) = (numeral n < m)"
  1655 apply auto
  1655 apply auto
  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"
  1854 
  1854 
  1855 lemma ceiling_minus_divide_eq_div_numeral [simp]:
  1855 lemma ceiling_minus_divide_eq_div_numeral [simp]:
  1856   "\<lceil>- (numeral a / numeral b :: real)\<rceil> = - (numeral a div numeral b)"
  1856   "\<lceil>- (numeral a / numeral b :: real)\<rceil> = - (numeral a div numeral b)"
  1857   using ceiling_divide_eq_div[of "- numeral a" "numeral b"] by simp
  1857   using ceiling_divide_eq_div[of "- numeral a" "numeral b"] by simp
  1858 
  1858 
  1859 text{* The following lemmas are remnants of the erstwhile functions natfloor
  1859 text\<open>The following lemmas are remnants of the erstwhile functions natfloor
  1860 and natceiling. *}
  1860 and natceiling.\<close>
  1861 
  1861 
  1862 lemma nat_floor_neg: "(x::real) <= 0 ==> nat(floor x) = 0"
  1862 lemma nat_floor_neg: "(x::real) <= 0 ==> nat(floor x) = 0"
  1863   by linarith
  1863   by linarith
  1864 
  1864 
  1865 lemma le_nat_floor: "real x <= a ==> x <= nat(floor a)"
  1865 lemma le_nat_floor: "real x <= a ==> x <= nat(floor a)"
  1884   apply (auto intro!: bexI[of _ "of_int (floor x - 1)"])
  1884   apply (auto intro!: bexI[of _ "of_int (floor x - 1)"])
  1885   apply (rule less_le_trans[OF _ of_int_floor_le])
  1885   apply (rule less_le_trans[OF _ of_int_floor_le])
  1886   apply simp
  1886   apply simp
  1887   done
  1887   done
  1888 
  1888 
  1889 subsection {* Exponentiation with floor *}
  1889 subsection \<open>Exponentiation with floor\<close>
  1890 
  1890 
  1891 lemma floor_power:
  1891 lemma floor_power:
  1892   assumes "x = real (floor x)"
  1892   assumes "x = real (floor x)"
  1893   shows "floor (x ^ n) = floor x ^ n"
  1893   shows "floor (x ^ n) = floor x ^ n"
  1894 proof -
  1894 proof -
  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 
  1960   "(of_rat (- (1 / numeral k)) :: real) = - (1 / numeral k)"
  1960   "(of_rat (- (1 / numeral k)) :: real) = - (1 / numeral k)"
  1961   "(of_rat (- (numeral k / numeral l)) :: real) = - (numeral k / numeral l)"
  1961   "(of_rat (- (numeral k / numeral l)) :: real) = - (numeral k / numeral l)"
  1962   by (simp_all add: of_rat_divide of_rat_minus)
  1962   by (simp_all add: of_rat_divide of_rat_minus)
  1963 
  1963 
  1964 
  1964 
  1965 text {* Operations *}
  1965 text \<open>Operations\<close>
  1966 
  1966 
  1967 lemma zero_real_code [code]:
  1967 lemma zero_real_code [code]:
  1968   "0 = Ratreal 0"
  1968   "0 = Ratreal 0"
  1969 by simp
  1969 by simp
  1970 
  1970 
  2016 
  2016 
  2017 lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  2017 lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  2018   by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff of_int_floor_le of_rat_of_int_eq real_less_eq_code)
  2018   by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff of_int_floor_le of_rat_of_int_eq real_less_eq_code)
  2019 
  2019 
  2020 
  2020 
  2021 text {* Quickcheck *}
  2021 text \<open>Quickcheck\<close>
  2022 
  2022 
  2023 definition (in term_syntax)
  2023 definition (in term_syntax)
  2024   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  2024   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  2025   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  2025   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  2026 
  2026 
  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]: