src/HOL/Real.thy
changeset 51539 625d2ec0bbff
parent 48891 c0eafbd55de3
parent 51523 97b5e8a1291c
child 51773 9328c6681f3c
     1.1 --- a/src/HOL/Real.thy	Tue Mar 26 14:38:44 2013 +0100
     1.2 +++ b/src/HOL/Real.thy	Tue Mar 26 15:10:28 2013 +0100
     1.3 @@ -1,7 +1,2228 @@
     1.4 +(*  Title:      HOL/Real.thy
     1.5 +    Author:     Jacques D. Fleuriot, University of Edinburgh, 1998
     1.6 +    Author:     Larry Paulson, University of Cambridge
     1.7 +    Author:     Jeremy Avigad, Carnegie Mellon University
     1.8 +    Author:     Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
     1.9 +    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
    1.10 +    Construction of Cauchy Reals by Brian Huffman, 2010
    1.11 +*)
    1.12 +
    1.13 +header {* Development of the Reals using Cauchy Sequences *}
    1.14 +
    1.15  theory Real
    1.16 -imports RComplete RealVector
    1.17 +imports Rat Conditional_Complete_Lattices
    1.18 +begin
    1.19 +
    1.20 +text {*
    1.21 +  This theory contains a formalization of the real numbers as
    1.22 +  equivalence classes of Cauchy sequences of rationals.  See
    1.23 +  @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative
    1.24 +  construction using Dedekind cuts.
    1.25 +*}
    1.26 +
    1.27 +subsection {* Preliminary lemmas *}
    1.28 +
    1.29 +lemma add_diff_add:
    1.30 +  fixes a b c d :: "'a::ab_group_add"
    1.31 +  shows "(a + c) - (b + d) = (a - b) + (c - d)"
    1.32 +  by simp
    1.33 +
    1.34 +lemma minus_diff_minus:
    1.35 +  fixes a b :: "'a::ab_group_add"
    1.36 +  shows "- a - - b = - (a - b)"
    1.37 +  by simp
    1.38 +
    1.39 +lemma mult_diff_mult:
    1.40 +  fixes x y a b :: "'a::ring"
    1.41 +  shows "(x * y - a * b) = x * (y - b) + (x - a) * b"
    1.42 +  by (simp add: algebra_simps)
    1.43 +
    1.44 +lemma inverse_diff_inverse:
    1.45 +  fixes a b :: "'a::division_ring"
    1.46 +  assumes "a \<noteq> 0" and "b \<noteq> 0"
    1.47 +  shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
    1.48 +  using assms by (simp add: algebra_simps)
    1.49 +
    1.50 +lemma obtain_pos_sum:
    1.51 +  fixes r :: rat assumes r: "0 < r"
    1.52 +  obtains s t where "0 < s" and "0 < t" and "r = s + t"
    1.53 +proof
    1.54 +    from r show "0 < r/2" by simp
    1.55 +    from r show "0 < r/2" by simp
    1.56 +    show "r = r/2 + r/2" by simp
    1.57 +qed
    1.58 +
    1.59 +subsection {* Sequences that converge to zero *}
    1.60 +
    1.61 +definition
    1.62 +  vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    1.63 +where
    1.64 +  "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
    1.65 +
    1.66 +lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
    1.67 +  unfolding vanishes_def by simp
    1.68 +
    1.69 +lemma vanishesD: "\<lbrakk>vanishes X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
    1.70 +  unfolding vanishes_def by simp
    1.71 +
    1.72 +lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
    1.73 +  unfolding vanishes_def
    1.74 +  apply (cases "c = 0", auto)
    1.75 +  apply (rule exI [where x="\<bar>c\<bar>"], auto)
    1.76 +  done
    1.77 +
    1.78 +lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
    1.79 +  unfolding vanishes_def by simp
    1.80 +
    1.81 +lemma vanishes_add:
    1.82 +  assumes X: "vanishes X" and Y: "vanishes Y"
    1.83 +  shows "vanishes (\<lambda>n. X n + Y n)"
    1.84 +proof (rule vanishesI)
    1.85 +  fix r :: rat assume "0 < r"
    1.86 +  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
    1.87 +    by (rule obtain_pos_sum)
    1.88 +  obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
    1.89 +    using vanishesD [OF X s] ..
    1.90 +  obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
    1.91 +    using vanishesD [OF Y t] ..
    1.92 +  have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
    1.93 +  proof (clarsimp)
    1.94 +    fix n assume n: "i \<le> n" "j \<le> n"
    1.95 +    have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>" by (rule abs_triangle_ineq)
    1.96 +    also have "\<dots> < s + t" by (simp add: add_strict_mono i j n)
    1.97 +    finally show "\<bar>X n + Y n\<bar> < r" unfolding r .
    1.98 +  qed
    1.99 +  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
   1.100 +qed
   1.101 +
   1.102 +lemma vanishes_diff:
   1.103 +  assumes X: "vanishes X" and Y: "vanishes Y"
   1.104 +  shows "vanishes (\<lambda>n. X n - Y n)"
   1.105 +unfolding diff_minus by (intro vanishes_add vanishes_minus X Y)
   1.106 +
   1.107 +lemma vanishes_mult_bounded:
   1.108 +  assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
   1.109 +  assumes Y: "vanishes (\<lambda>n. Y n)"
   1.110 +  shows "vanishes (\<lambda>n. X n * Y n)"
   1.111 +proof (rule vanishesI)
   1.112 +  fix r :: rat assume r: "0 < r"
   1.113 +  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   1.114 +    using X by fast
   1.115 +  obtain b where b: "0 < b" "r = a * b"
   1.116 +  proof
   1.117 +    show "0 < r / a" using r a by (simp add: divide_pos_pos)
   1.118 +    show "r = a * (r / a)" using a by simp
   1.119 +  qed
   1.120 +  obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
   1.121 +    using vanishesD [OF Y b(1)] ..
   1.122 +  have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   1.123 +    by (simp add: b(2) abs_mult mult_strict_mono' a k)
   1.124 +  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   1.125 +qed
   1.126 +
   1.127 +subsection {* Cauchy sequences *}
   1.128 +
   1.129 +definition
   1.130 +  cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   1.131 +where
   1.132 +  "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   1.133 +
   1.134 +lemma cauchyI:
   1.135 +  "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r) \<Longrightarrow> cauchy X"
   1.136 +  unfolding cauchy_def by simp
   1.137 +
   1.138 +lemma cauchyD:
   1.139 +  "\<lbrakk>cauchy X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
   1.140 +  unfolding cauchy_def by simp
   1.141 +
   1.142 +lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
   1.143 +  unfolding cauchy_def by simp
   1.144 +
   1.145 +lemma cauchy_add [simp]:
   1.146 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.147 +  shows "cauchy (\<lambda>n. X n + Y n)"
   1.148 +proof (rule cauchyI)
   1.149 +  fix r :: rat assume "0 < r"
   1.150 +  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   1.151 +    by (rule obtain_pos_sum)
   1.152 +  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   1.153 +    using cauchyD [OF X s] ..
   1.154 +  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   1.155 +    using cauchyD [OF Y t] ..
   1.156 +  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
   1.157 +  proof (clarsimp)
   1.158 +    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   1.159 +    have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
   1.160 +      unfolding add_diff_add by (rule abs_triangle_ineq)
   1.161 +    also have "\<dots> < s + t"
   1.162 +      by (rule add_strict_mono, simp_all add: i j *)
   1.163 +    finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" unfolding r .
   1.164 +  qed
   1.165 +  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
   1.166 +qed
   1.167 +
   1.168 +lemma cauchy_minus [simp]:
   1.169 +  assumes X: "cauchy X"
   1.170 +  shows "cauchy (\<lambda>n. - X n)"
   1.171 +using assms unfolding cauchy_def
   1.172 +unfolding minus_diff_minus abs_minus_cancel .
   1.173 +
   1.174 +lemma cauchy_diff [simp]:
   1.175 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.176 +  shows "cauchy (\<lambda>n. X n - Y n)"
   1.177 +using assms unfolding diff_minus by simp
   1.178 +
   1.179 +lemma cauchy_imp_bounded:
   1.180 +  assumes "cauchy X" shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   1.181 +proof -
   1.182 +  obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
   1.183 +    using cauchyD [OF assms zero_less_one] ..
   1.184 +  show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   1.185 +  proof (intro exI conjI allI)
   1.186 +    have "0 \<le> \<bar>X 0\<bar>" by simp
   1.187 +    also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
   1.188 +    finally have "0 \<le> Max (abs ` X ` {..k})" .
   1.189 +    thus "0 < Max (abs ` X ` {..k}) + 1" by simp
   1.190 +  next
   1.191 +    fix n :: nat
   1.192 +    show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
   1.193 +    proof (rule linorder_le_cases)
   1.194 +      assume "n \<le> k"
   1.195 +      hence "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
   1.196 +      thus "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
   1.197 +    next
   1.198 +      assume "k \<le> n"
   1.199 +      have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   1.200 +      also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   1.201 +        by (rule abs_triangle_ineq)
   1.202 +      also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   1.203 +        by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
   1.204 +      finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   1.205 +    qed
   1.206 +  qed
   1.207 +qed
   1.208 +
   1.209 +lemma cauchy_mult [simp]:
   1.210 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.211 +  shows "cauchy (\<lambda>n. X n * Y n)"
   1.212 +proof (rule cauchyI)
   1.213 +  fix r :: rat assume "0 < r"
   1.214 +  then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
   1.215 +    by (rule obtain_pos_sum)
   1.216 +  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   1.217 +    using cauchy_imp_bounded [OF X] by fast
   1.218 +  obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
   1.219 +    using cauchy_imp_bounded [OF Y] by fast
   1.220 +  obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
   1.221 +  proof
   1.222 +    show "0 < v/b" using v b(1) by (rule divide_pos_pos)
   1.223 +    show "0 < u/a" using u a(1) by (rule divide_pos_pos)
   1.224 +    show "r = a * (u/a) + (v/b) * b"
   1.225 +      using a(1) b(1) `r = u + v` by simp
   1.226 +  qed
   1.227 +  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   1.228 +    using cauchyD [OF X s] ..
   1.229 +  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   1.230 +    using cauchyD [OF Y t] ..
   1.231 +  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
   1.232 +  proof (clarsimp)
   1.233 +    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   1.234 +    have "\<bar>X m * Y m - X n * Y n\<bar> = \<bar>X m * (Y m - Y n) + (X m - X n) * Y n\<bar>"
   1.235 +      unfolding mult_diff_mult ..
   1.236 +    also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
   1.237 +      by (rule abs_triangle_ineq)
   1.238 +    also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
   1.239 +      unfolding abs_mult ..
   1.240 +    also have "\<dots> < a * t + s * b"
   1.241 +      by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
   1.242 +    finally show "\<bar>X m * Y m - X n * Y n\<bar> < r" unfolding r .
   1.243 +  qed
   1.244 +  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
   1.245 +qed
   1.246 +
   1.247 +lemma cauchy_not_vanishes_cases:
   1.248 +  assumes X: "cauchy X"
   1.249 +  assumes nz: "\<not> vanishes X"
   1.250 +  shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
   1.251 +proof -
   1.252 +  obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
   1.253 +    using nz unfolding vanishes_def by (auto simp add: not_less)
   1.254 +  obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
   1.255 +    using `0 < r` by (rule obtain_pos_sum)
   1.256 +  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   1.257 +    using cauchyD [OF X s] ..
   1.258 +  obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
   1.259 +    using r by fast
   1.260 +  have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
   1.261 +    using i `i \<le> k` by auto
   1.262 +  have "X k \<le> - r \<or> r \<le> X k"
   1.263 +    using `r \<le> \<bar>X k\<bar>` by auto
   1.264 +  hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   1.265 +    unfolding `r = s + t` using k by auto
   1.266 +  hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
   1.267 +  thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   1.268 +    using t by auto
   1.269 +qed
   1.270 +
   1.271 +lemma cauchy_not_vanishes:
   1.272 +  assumes X: "cauchy X"
   1.273 +  assumes nz: "\<not> vanishes X"
   1.274 +  shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
   1.275 +using cauchy_not_vanishes_cases [OF assms]
   1.276 +by clarify (rule exI, erule conjI, rule_tac x=k in exI, auto)
   1.277 +
   1.278 +lemma cauchy_inverse [simp]:
   1.279 +  assumes X: "cauchy X"
   1.280 +  assumes nz: "\<not> vanishes X"
   1.281 +  shows "cauchy (\<lambda>n. inverse (X n))"
   1.282 +proof (rule cauchyI)
   1.283 +  fix r :: rat assume "0 < r"
   1.284 +  obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   1.285 +    using cauchy_not_vanishes [OF X nz] by fast
   1.286 +  from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   1.287 +  obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   1.288 +  proof
   1.289 +    show "0 < b * r * b"
   1.290 +      by (simp add: `0 < r` b mult_pos_pos)
   1.291 +    show "r = inverse b * (b * r * b) * inverse b"
   1.292 +      using b by simp
   1.293 +  qed
   1.294 +  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   1.295 +    using cauchyD [OF X s] ..
   1.296 +  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
   1.297 +  proof (clarsimp)
   1.298 +    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   1.299 +    have "\<bar>inverse (X m) - inverse (X n)\<bar> =
   1.300 +          inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
   1.301 +      by (simp add: inverse_diff_inverse nz * abs_mult)
   1.302 +    also have "\<dots> < inverse b * s * inverse b"
   1.303 +      by (simp add: mult_strict_mono less_imp_inverse_less
   1.304 +                    mult_pos_pos i j b * s)
   1.305 +    finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" unfolding r .
   1.306 +  qed
   1.307 +  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
   1.308 +qed
   1.309 +
   1.310 +lemma vanishes_diff_inverse:
   1.311 +  assumes X: "cauchy X" "\<not> vanishes X"
   1.312 +  assumes Y: "cauchy Y" "\<not> vanishes Y"
   1.313 +  assumes XY: "vanishes (\<lambda>n. X n - Y n)"
   1.314 +  shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
   1.315 +proof (rule vanishesI)
   1.316 +  fix r :: rat assume r: "0 < r"
   1.317 +  obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
   1.318 +    using cauchy_not_vanishes [OF X] by fast
   1.319 +  obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
   1.320 +    using cauchy_not_vanishes [OF Y] by fast
   1.321 +  obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
   1.322 +  proof
   1.323 +    show "0 < a * r * b"
   1.324 +      using a r b by (simp add: mult_pos_pos)
   1.325 +    show "inverse a * (a * r * b) * inverse b = r"
   1.326 +      using a r b by simp
   1.327 +  qed
   1.328 +  obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
   1.329 +    using vanishesD [OF XY s] ..
   1.330 +  have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
   1.331 +  proof (clarsimp)
   1.332 +    fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
   1.333 +    have "X n \<noteq> 0" and "Y n \<noteq> 0"
   1.334 +      using i j a b n by auto
   1.335 +    hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
   1.336 +        inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
   1.337 +      by (simp add: inverse_diff_inverse abs_mult)
   1.338 +    also have "\<dots> < inverse a * s * inverse b"
   1.339 +      apply (intro mult_strict_mono' less_imp_inverse_less)
   1.340 +      apply (simp_all add: a b i j k n mult_nonneg_nonneg)
   1.341 +      done
   1.342 +    also note `inverse a * s * inverse b = r`
   1.343 +    finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
   1.344 +  qed
   1.345 +  thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
   1.346 +qed
   1.347 +
   1.348 +subsection {* Equivalence relation on Cauchy sequences *}
   1.349 +
   1.350 +definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
   1.351 +  where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
   1.352 +
   1.353 +lemma realrelI [intro?]:
   1.354 +  assumes "cauchy X" and "cauchy Y" and "vanishes (\<lambda>n. X n - Y n)"
   1.355 +  shows "realrel X Y"
   1.356 +  using assms unfolding realrel_def by simp
   1.357 +
   1.358 +lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
   1.359 +  unfolding realrel_def by simp
   1.360 +
   1.361 +lemma symp_realrel: "symp realrel"
   1.362 +  unfolding realrel_def
   1.363 +  by (rule sympI, clarify, drule vanishes_minus, simp)
   1.364 +
   1.365 +lemma transp_realrel: "transp realrel"
   1.366 +  unfolding realrel_def
   1.367 +  apply (rule transpI, clarify)
   1.368 +  apply (drule (1) vanishes_add)
   1.369 +  apply (simp add: algebra_simps)
   1.370 +  done
   1.371 +
   1.372 +lemma part_equivp_realrel: "part_equivp realrel"
   1.373 +  by (fast intro: part_equivpI symp_realrel transp_realrel
   1.374 +    realrel_refl cauchy_const)
   1.375 +
   1.376 +subsection {* The field of real numbers *}
   1.377 +
   1.378 +quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   1.379 +  morphisms rep_real Real
   1.380 +  by (rule part_equivp_realrel)
   1.381 +
   1.382 +lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
   1.383 +  unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
   1.384 +
   1.385 +lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
   1.386 +  assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)" shows "P x"
   1.387 +proof (induct x)
   1.388 +  case (1 X)
   1.389 +  hence "cauchy X" by (simp add: realrel_def)
   1.390 +  thus "P (Real X)" by (rule assms)
   1.391 +qed
   1.392 +
   1.393 +lemma eq_Real:
   1.394 +  "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
   1.395 +  using real.rel_eq_transfer
   1.396 +  unfolding real.pcr_cr_eq cr_real_def fun_rel_def realrel_def by simp
   1.397 +
   1.398 +declare real.forall_transfer [transfer_rule del]
   1.399 +
   1.400 +lemma forall_real_transfer [transfer_rule]: (* TODO: generate automatically *)
   1.401 +  "(fun_rel (fun_rel pcr_real op =) op =)
   1.402 +    (transfer_bforall cauchy) transfer_forall"
   1.403 +  using real.forall_transfer
   1.404 +  by (simp add: realrel_def)
   1.405 +
   1.406 +instantiation real :: field_inverse_zero
   1.407 +begin
   1.408 +
   1.409 +lift_definition zero_real :: "real" is "\<lambda>n. 0"
   1.410 +  by (simp add: realrel_refl)
   1.411 +
   1.412 +lift_definition one_real :: "real" is "\<lambda>n. 1"
   1.413 +  by (simp add: realrel_refl)
   1.414 +
   1.415 +lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
   1.416 +  unfolding realrel_def add_diff_add
   1.417 +  by (simp only: cauchy_add vanishes_add simp_thms)
   1.418 +
   1.419 +lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
   1.420 +  unfolding realrel_def minus_diff_minus
   1.421 +  by (simp only: cauchy_minus vanishes_minus simp_thms)
   1.422 +
   1.423 +lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
   1.424 +  unfolding realrel_def mult_diff_mult
   1.425 +  by (subst (4) mult_commute, simp only: cauchy_mult vanishes_add
   1.426 +    vanishes_mult_bounded cauchy_imp_bounded simp_thms)
   1.427 +
   1.428 +lift_definition inverse_real :: "real \<Rightarrow> real"
   1.429 +  is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
   1.430 +proof -
   1.431 +  fix X Y assume "realrel X Y"
   1.432 +  hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   1.433 +    unfolding realrel_def by simp_all
   1.434 +  have "vanishes X \<longleftrightarrow> vanishes Y"
   1.435 +  proof
   1.436 +    assume "vanishes X"
   1.437 +    from vanishes_diff [OF this XY] show "vanishes Y" by simp
   1.438 +  next
   1.439 +    assume "vanishes Y"
   1.440 +    from vanishes_add [OF this XY] show "vanishes X" by simp
   1.441 +  qed
   1.442 +  thus "?thesis X Y"
   1.443 +    unfolding realrel_def
   1.444 +    by (simp add: vanishes_diff_inverse X Y XY)
   1.445 +qed
   1.446 +
   1.447 +definition
   1.448 +  "x - y = (x::real) + - y"
   1.449 +
   1.450 +definition
   1.451 +  "x / y = (x::real) * inverse y"
   1.452 +
   1.453 +lemma add_Real:
   1.454 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.455 +  shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
   1.456 +  using assms plus_real.transfer
   1.457 +  unfolding cr_real_eq fun_rel_def by simp
   1.458 +
   1.459 +lemma minus_Real:
   1.460 +  assumes X: "cauchy X"
   1.461 +  shows "- Real X = Real (\<lambda>n. - X n)"
   1.462 +  using assms uminus_real.transfer
   1.463 +  unfolding cr_real_eq fun_rel_def by simp
   1.464 +
   1.465 +lemma diff_Real:
   1.466 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.467 +  shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
   1.468 +  unfolding minus_real_def diff_minus
   1.469 +  by (simp add: minus_Real add_Real X Y)
   1.470 +
   1.471 +lemma mult_Real:
   1.472 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.473 +  shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
   1.474 +  using assms times_real.transfer
   1.475 +  unfolding cr_real_eq fun_rel_def by simp
   1.476 +
   1.477 +lemma inverse_Real:
   1.478 +  assumes X: "cauchy X"
   1.479 +  shows "inverse (Real X) =
   1.480 +    (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   1.481 +  using assms inverse_real.transfer zero_real.transfer
   1.482 +  unfolding cr_real_eq fun_rel_def by (simp split: split_if_asm, metis)
   1.483 +
   1.484 +instance proof
   1.485 +  fix a b c :: real
   1.486 +  show "a + b = b + a"
   1.487 +    by transfer (simp add: add_ac realrel_def)
   1.488 +  show "(a + b) + c = a + (b + c)"
   1.489 +    by transfer (simp add: add_ac realrel_def)
   1.490 +  show "0 + a = a"
   1.491 +    by transfer (simp add: realrel_def)
   1.492 +  show "- a + a = 0"
   1.493 +    by transfer (simp add: realrel_def)
   1.494 +  show "a - b = a + - b"
   1.495 +    by (rule minus_real_def)
   1.496 +  show "(a * b) * c = a * (b * c)"
   1.497 +    by transfer (simp add: mult_ac realrel_def)
   1.498 +  show "a * b = b * a"
   1.499 +    by transfer (simp add: mult_ac realrel_def)
   1.500 +  show "1 * a = a"
   1.501 +    by transfer (simp add: mult_ac realrel_def)
   1.502 +  show "(a + b) * c = a * c + b * c"
   1.503 +    by transfer (simp add: distrib_right realrel_def)
   1.504 +  show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
   1.505 +    by transfer (simp add: realrel_def)
   1.506 +  show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   1.507 +    apply transfer
   1.508 +    apply (simp add: realrel_def)
   1.509 +    apply (rule vanishesI)
   1.510 +    apply (frule (1) cauchy_not_vanishes, clarify)
   1.511 +    apply (rule_tac x=k in exI, clarify)
   1.512 +    apply (drule_tac x=n in spec, simp)
   1.513 +    done
   1.514 +  show "a / b = a * inverse b"
   1.515 +    by (rule divide_real_def)
   1.516 +  show "inverse (0::real) = 0"
   1.517 +    by transfer (simp add: realrel_def)
   1.518 +qed
   1.519 +
   1.520 +end
   1.521 +
   1.522 +subsection {* Positive reals *}
   1.523 +
   1.524 +lift_definition positive :: "real \<Rightarrow> bool"
   1.525 +  is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   1.526 +proof -
   1.527 +  { fix X Y
   1.528 +    assume "realrel X Y"
   1.529 +    hence XY: "vanishes (\<lambda>n. X n - Y n)"
   1.530 +      unfolding realrel_def by simp_all
   1.531 +    assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   1.532 +    then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
   1.533 +      by fast
   1.534 +    obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   1.535 +      using `0 < r` by (rule obtain_pos_sum)
   1.536 +    obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
   1.537 +      using vanishesD [OF XY s] ..
   1.538 +    have "\<forall>n\<ge>max i j. t < Y n"
   1.539 +    proof (clarsimp)
   1.540 +      fix n assume n: "i \<le> n" "j \<le> n"
   1.541 +      have "\<bar>X n - Y n\<bar> < s" and "r < X n"
   1.542 +        using i j n by simp_all
   1.543 +      thus "t < Y n" unfolding r by simp
   1.544 +    qed
   1.545 +    hence "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
   1.546 +  } note 1 = this
   1.547 +  fix X Y assume "realrel X Y"
   1.548 +  hence "realrel X Y" and "realrel Y X"
   1.549 +    using symp_realrel unfolding symp_def by auto
   1.550 +  thus "?thesis X Y"
   1.551 +    by (safe elim!: 1)
   1.552 +qed
   1.553 +
   1.554 +lemma positive_Real:
   1.555 +  assumes X: "cauchy X"
   1.556 +  shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   1.557 +  using assms positive.transfer
   1.558 +  unfolding cr_real_eq fun_rel_def by simp
   1.559 +
   1.560 +lemma positive_zero: "\<not> positive 0"
   1.561 +  by transfer auto
   1.562 +
   1.563 +lemma positive_add:
   1.564 +  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
   1.565 +apply transfer
   1.566 +apply (clarify, rename_tac a b i j)
   1.567 +apply (rule_tac x="a + b" in exI, simp)
   1.568 +apply (rule_tac x="max i j" in exI, clarsimp)
   1.569 +apply (simp add: add_strict_mono)
   1.570 +done
   1.571 +
   1.572 +lemma positive_mult:
   1.573 +  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
   1.574 +apply transfer
   1.575 +apply (clarify, rename_tac a b i j)
   1.576 +apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
   1.577 +apply (rule_tac x="max i j" in exI, clarsimp)
   1.578 +apply (rule mult_strict_mono, auto)
   1.579 +done
   1.580 +
   1.581 +lemma positive_minus:
   1.582 +  "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
   1.583 +apply transfer
   1.584 +apply (simp add: realrel_def)
   1.585 +apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
   1.586 +done
   1.587 +
   1.588 +instantiation real :: linordered_field_inverse_zero
   1.589 +begin
   1.590 +
   1.591 +definition
   1.592 +  "x < y \<longleftrightarrow> positive (y - x)"
   1.593 +
   1.594 +definition
   1.595 +  "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
   1.596 +
   1.597 +definition
   1.598 +  "abs (a::real) = (if a < 0 then - a else a)"
   1.599 +
   1.600 +definition
   1.601 +  "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   1.602 +
   1.603 +instance proof
   1.604 +  fix a b c :: real
   1.605 +  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   1.606 +    by (rule abs_real_def)
   1.607 +  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   1.608 +    unfolding less_eq_real_def less_real_def
   1.609 +    by (auto, drule (1) positive_add, simp_all add: positive_zero)
   1.610 +  show "a \<le> a"
   1.611 +    unfolding less_eq_real_def by simp
   1.612 +  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   1.613 +    unfolding less_eq_real_def less_real_def
   1.614 +    by (auto, drule (1) positive_add, simp add: algebra_simps)
   1.615 +  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   1.616 +    unfolding less_eq_real_def less_real_def
   1.617 +    by (auto, drule (1) positive_add, simp add: positive_zero)
   1.618 +  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   1.619 +    unfolding less_eq_real_def less_real_def by (auto simp: diff_minus) (* by auto *)
   1.620 +    (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
   1.621 +    (* Should produce c + b - (c + a) \<equiv> b - a *)
   1.622 +  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   1.623 +    by (rule sgn_real_def)
   1.624 +  show "a \<le> b \<or> b \<le> a"
   1.625 +    unfolding less_eq_real_def less_real_def
   1.626 +    by (auto dest!: positive_minus)
   1.627 +  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   1.628 +    unfolding less_real_def
   1.629 +    by (drule (1) positive_mult, simp add: algebra_simps)
   1.630 +qed
   1.631 +
   1.632 +end
   1.633 +
   1.634 +instantiation real :: distrib_lattice
   1.635 +begin
   1.636 +
   1.637 +definition
   1.638 +  "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
   1.639 +
   1.640 +definition
   1.641 +  "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
   1.642 +
   1.643 +instance proof
   1.644 +qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
   1.645 +
   1.646 +end
   1.647 +
   1.648 +lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   1.649 +apply (induct x)
   1.650 +apply (simp add: zero_real_def)
   1.651 +apply (simp add: one_real_def add_Real)
   1.652 +done
   1.653 +
   1.654 +lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
   1.655 +apply (cases x rule: int_diff_cases)
   1.656 +apply (simp add: of_nat_Real diff_Real)
   1.657 +done
   1.658 +
   1.659 +lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
   1.660 +apply (induct x)
   1.661 +apply (simp add: Fract_of_int_quotient of_rat_divide)
   1.662 +apply (simp add: of_int_Real divide_inverse)
   1.663 +apply (simp add: inverse_Real mult_Real)
   1.664 +done
   1.665 +
   1.666 +instance real :: archimedean_field
   1.667 +proof
   1.668 +  fix x :: real
   1.669 +  show "\<exists>z. x \<le> of_int z"
   1.670 +    apply (induct x)
   1.671 +    apply (frule cauchy_imp_bounded, clarify)
   1.672 +    apply (rule_tac x="ceiling b + 1" in exI)
   1.673 +    apply (rule less_imp_le)
   1.674 +    apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
   1.675 +    apply (rule_tac x=1 in exI, simp add: algebra_simps)
   1.676 +    apply (rule_tac x=0 in exI, clarsimp)
   1.677 +    apply (rule le_less_trans [OF abs_ge_self])
   1.678 +    apply (rule less_le_trans [OF _ le_of_int_ceiling])
   1.679 +    apply simp
   1.680 +    done
   1.681 +qed
   1.682 +
   1.683 +instantiation real :: floor_ceiling
   1.684 +begin
   1.685 +
   1.686 +definition [code del]:
   1.687 +  "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
   1.688 +
   1.689 +instance proof
   1.690 +  fix x :: real
   1.691 +  show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   1.692 +    unfolding floor_real_def using floor_exists1 by (rule theI')
   1.693 +qed
   1.694 +
   1.695 +end
   1.696 +
   1.697 +subsection {* Completeness *}
   1.698 +
   1.699 +lemma not_positive_Real:
   1.700 +  assumes X: "cauchy X"
   1.701 +  shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   1.702 +unfolding positive_Real [OF X]
   1.703 +apply (auto, unfold not_less)
   1.704 +apply (erule obtain_pos_sum)
   1.705 +apply (drule_tac x=s in spec, simp)
   1.706 +apply (drule_tac r=t in cauchyD [OF X], clarify)
   1.707 +apply (drule_tac x=k in spec, clarsimp)
   1.708 +apply (rule_tac x=n in exI, clarify, rename_tac m)
   1.709 +apply (drule_tac x=m in spec, simp)
   1.710 +apply (drule_tac x=n in spec, simp)
   1.711 +apply (drule spec, drule (1) mp, clarify, rename_tac i)
   1.712 +apply (rule_tac x="max i k" in exI, simp)
   1.713 +done
   1.714 +
   1.715 +lemma le_Real:
   1.716 +  assumes X: "cauchy X" and Y: "cauchy Y"
   1.717 +  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   1.718 +unfolding not_less [symmetric, where 'a=real] less_real_def
   1.719 +apply (simp add: diff_Real not_positive_Real X Y)
   1.720 +apply (simp add: diff_le_eq add_ac)
   1.721 +done
   1.722 +
   1.723 +lemma le_RealI:
   1.724 +  assumes Y: "cauchy Y"
   1.725 +  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   1.726 +proof (induct x)
   1.727 +  fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   1.728 +  hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   1.729 +    by (simp add: of_rat_Real le_Real)
   1.730 +  {
   1.731 +    fix r :: rat assume "0 < r"
   1.732 +    then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   1.733 +      by (rule obtain_pos_sum)
   1.734 +    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   1.735 +      using cauchyD [OF Y s] ..
   1.736 +    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   1.737 +      using le [OF t] ..
   1.738 +    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   1.739 +    proof (clarsimp)
   1.740 +      fix n assume n: "i \<le> n" "j \<le> n"
   1.741 +      have "X n \<le> Y i + t" using n j by simp
   1.742 +      moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
   1.743 +      ultimately show "X n \<le> Y n + r" unfolding r by simp
   1.744 +    qed
   1.745 +    hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
   1.746 +  }
   1.747 +  thus "Real X \<le> Real Y"
   1.748 +    by (simp add: of_rat_Real le_Real X Y)
   1.749 +qed
   1.750 +
   1.751 +lemma Real_leI:
   1.752 +  assumes X: "cauchy X"
   1.753 +  assumes le: "\<forall>n. of_rat (X n) \<le> y"
   1.754 +  shows "Real X \<le> y"
   1.755 +proof -
   1.756 +  have "- y \<le> - Real X"
   1.757 +    by (simp add: minus_Real X le_RealI of_rat_minus le)
   1.758 +  thus ?thesis by simp
   1.759 +qed
   1.760 +
   1.761 +lemma less_RealD:
   1.762 +  assumes Y: "cauchy Y"
   1.763 +  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   1.764 +by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   1.765 +
   1.766 +lemma of_nat_less_two_power:
   1.767 +  "of_nat n < (2::'a::linordered_idom) ^ n"
   1.768 +apply (induct n)
   1.769 +apply simp
   1.770 +apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   1.771 +apply (drule (1) add_le_less_mono, simp)
   1.772 +apply simp
   1.773 +done
   1.774 +
   1.775 +lemma complete_real:
   1.776 +  fixes S :: "real set"
   1.777 +  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   1.778 +  shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   1.779 +proof -
   1.780 +  obtain x where x: "x \<in> S" using assms(1) ..
   1.781 +  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   1.782 +
   1.783 +  def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
   1.784 +  obtain a where a: "\<not> P a"
   1.785 +  proof
   1.786 +    have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
   1.787 +    also have "x - 1 < x" by simp
   1.788 +    finally have "of_int (floor (x - 1)) < x" .
   1.789 +    hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
   1.790 +    then show "\<not> P (of_int (floor (x - 1)))"
   1.791 +      unfolding P_def of_rat_of_int_eq using x by fast
   1.792 +  qed
   1.793 +  obtain b where b: "P b"
   1.794 +  proof
   1.795 +    show "P (of_int (ceiling z))"
   1.796 +    unfolding P_def of_rat_of_int_eq
   1.797 +    proof
   1.798 +      fix y assume "y \<in> S"
   1.799 +      hence "y \<le> z" using z by simp
   1.800 +      also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
   1.801 +      finally show "y \<le> of_int (ceiling z)" .
   1.802 +    qed
   1.803 +  qed
   1.804 +
   1.805 +  def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
   1.806 +  def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
   1.807 +  def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
   1.808 +  def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
   1.809 +  def C \<equiv> "\<lambda>n. avg (A n) (B n)"
   1.810 +  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   1.811 +  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   1.812 +  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   1.813 +    unfolding A_def B_def C_def bisect_def split_def by simp
   1.814 +  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   1.815 +    unfolding A_def B_def C_def bisect_def split_def by simp
   1.816 +
   1.817 +  have width: "\<And>n. B n - A n = (b - a) / 2^n"
   1.818 +    apply (simp add: eq_divide_eq)
   1.819 +    apply (induct_tac n, simp)
   1.820 +    apply (simp add: C_def avg_def algebra_simps)
   1.821 +    done
   1.822 +
   1.823 +  have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
   1.824 +    apply (simp add: divide_less_eq)
   1.825 +    apply (subst mult_commute)
   1.826 +    apply (frule_tac y=y in ex_less_of_nat_mult)
   1.827 +    apply clarify
   1.828 +    apply (rule_tac x=n in exI)
   1.829 +    apply (erule less_trans)
   1.830 +    apply (rule mult_strict_right_mono)
   1.831 +    apply (rule le_less_trans [OF _ of_nat_less_two_power])
   1.832 +    apply simp
   1.833 +    apply assumption
   1.834 +    done
   1.835 +
   1.836 +  have PA: "\<And>n. \<not> P (A n)"
   1.837 +    by (induct_tac n, simp_all add: a)
   1.838 +  have PB: "\<And>n. P (B n)"
   1.839 +    by (induct_tac n, simp_all add: b)
   1.840 +  have ab: "a < b"
   1.841 +    using a b unfolding P_def
   1.842 +    apply (clarsimp simp add: not_le)
   1.843 +    apply (drule (1) bspec)
   1.844 +    apply (drule (1) less_le_trans)
   1.845 +    apply (simp add: of_rat_less)
   1.846 +    done
   1.847 +  have AB: "\<And>n. A n < B n"
   1.848 +    by (induct_tac n, simp add: ab, simp add: C_def avg_def)
   1.849 +  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   1.850 +    apply (auto simp add: le_less [where 'a=nat])
   1.851 +    apply (erule less_Suc_induct)
   1.852 +    apply (clarsimp simp add: C_def avg_def)
   1.853 +    apply (simp add: add_divide_distrib [symmetric])
   1.854 +    apply (rule AB [THEN less_imp_le])
   1.855 +    apply simp
   1.856 +    done
   1.857 +  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   1.858 +    apply (auto simp add: le_less [where 'a=nat])
   1.859 +    apply (erule less_Suc_induct)
   1.860 +    apply (clarsimp simp add: C_def avg_def)
   1.861 +    apply (simp add: add_divide_distrib [symmetric])
   1.862 +    apply (rule AB [THEN less_imp_le])
   1.863 +    apply simp
   1.864 +    done
   1.865 +  have cauchy_lemma:
   1.866 +    "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   1.867 +    apply (rule cauchyI)
   1.868 +    apply (drule twos [where y="b - a"])
   1.869 +    apply (erule exE)
   1.870 +    apply (rule_tac x=n in exI, clarify, rename_tac i j)
   1.871 +    apply (rule_tac y="B n - A n" in le_less_trans) defer
   1.872 +    apply (simp add: width)
   1.873 +    apply (drule_tac x=n in spec)
   1.874 +    apply (frule_tac x=i in spec, drule (1) mp)
   1.875 +    apply (frule_tac x=j in spec, drule (1) mp)
   1.876 +    apply (frule A_mono, drule B_mono)
   1.877 +    apply (frule A_mono, drule B_mono)
   1.878 +    apply arith
   1.879 +    done
   1.880 +  have "cauchy A"
   1.881 +    apply (rule cauchy_lemma [rule_format])
   1.882 +    apply (simp add: A_mono)
   1.883 +    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   1.884 +    done
   1.885 +  have "cauchy B"
   1.886 +    apply (rule cauchy_lemma [rule_format])
   1.887 +    apply (simp add: B_mono)
   1.888 +    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
   1.889 +    done
   1.890 +  have 1: "\<forall>x\<in>S. x \<le> Real B"
   1.891 +  proof
   1.892 +    fix x assume "x \<in> S"
   1.893 +    then show "x \<le> Real B"
   1.894 +      using PB [unfolded P_def] `cauchy B`
   1.895 +      by (simp add: le_RealI)
   1.896 +  qed
   1.897 +  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
   1.898 +    apply clarify
   1.899 +    apply (erule contrapos_pp)
   1.900 +    apply (simp add: not_le)
   1.901 +    apply (drule less_RealD [OF `cauchy A`], clarify)
   1.902 +    apply (subgoal_tac "\<not> P (A n)")
   1.903 +    apply (simp add: P_def not_le, clarify)
   1.904 +    apply (erule rev_bexI)
   1.905 +    apply (erule (1) less_trans)
   1.906 +    apply (simp add: PA)
   1.907 +    done
   1.908 +  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
   1.909 +  proof (rule vanishesI)
   1.910 +    fix r :: rat assume "0 < r"
   1.911 +    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
   1.912 +      using twos by fast
   1.913 +    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
   1.914 +    proof (clarify)
   1.915 +      fix n assume n: "k \<le> n"
   1.916 +      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
   1.917 +        by simp
   1.918 +      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
   1.919 +        using n by (simp add: divide_left_mono mult_pos_pos)
   1.920 +      also note k
   1.921 +      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
   1.922 +    qed
   1.923 +    thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
   1.924 +  qed
   1.925 +  hence 3: "Real B = Real A"
   1.926 +    by (simp add: eq_Real `cauchy A` `cauchy B` width)
   1.927 +  show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   1.928 +    using 1 2 3 by (rule_tac x="Real B" in exI, simp)
   1.929 +qed
   1.930 +
   1.931 +
   1.932 +instantiation real :: conditional_complete_linorder
   1.933  begin
   1.934  
   1.935 +subsection{*Supremum of a set of reals*}
   1.936 +
   1.937 +definition
   1.938 +  Sup_real_def: "Sup X \<equiv> LEAST z::real. \<forall>x\<in>X. x\<le>z"
   1.939 +
   1.940 +definition
   1.941 +  Inf_real_def: "Inf (X::real set) \<equiv> - Sup (uminus ` X)"
   1.942 +
   1.943 +instance
   1.944 +proof
   1.945 +  { fix z x :: real and X :: "real set"
   1.946 +    assume x: "x \<in> X" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
   1.947 +    then obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
   1.948 +      using complete_real[of X] by blast
   1.949 +    then show "x \<le> Sup X"
   1.950 +      unfolding Sup_real_def by (rule LeastI2_order) (auto simp: x) }
   1.951 +  note Sup_upper = this
   1.952 +
   1.953 +  { fix z :: real and X :: "real set"
   1.954 +    assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
   1.955 +    then obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
   1.956 +      using complete_real[of X] by blast
   1.957 +    then have "Sup X = s"
   1.958 +      unfolding Sup_real_def by (best intro: Least_equality)  
   1.959 +    also with s z have "... \<le> z"
   1.960 +      by blast
   1.961 +    finally show "Sup X \<le> z" . }
   1.962 +  note Sup_least = this
   1.963 +
   1.964 +  { fix x z :: real and X :: "real set"
   1.965 +    assume x: "x \<in> X" and z: "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
   1.966 +    have "-x \<le> Sup (uminus ` X)"
   1.967 +      by (rule Sup_upper[of _ _ "- z"]) (auto simp add: image_iff x z)
   1.968 +    then show "Inf X \<le> x" 
   1.969 +      by (auto simp add: Inf_real_def) }
   1.970 +
   1.971 +  { fix z :: real and X :: "real set"
   1.972 +    assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
   1.973 +    have "Sup (uminus ` X) \<le> -z"
   1.974 +      using x z by (force intro: Sup_least)
   1.975 +    then show "z \<le> Inf X" 
   1.976 +        by (auto simp add: Inf_real_def) }
   1.977 +qed
   1.978 +end
   1.979 +
   1.980 +text {*
   1.981 +  \medskip Completeness properties using @{text "isUb"}, @{text "isLub"}:
   1.982 +*}
   1.983 +
   1.984 +lemma reals_complete: "\<exists>X. X \<in> S \<Longrightarrow> \<exists>Y. isUb (UNIV::real set) S Y \<Longrightarrow> \<exists>t. isLub (UNIV :: real set) S t"
   1.985 +  by (intro exI[of _ "Sup S"] isLub_cSup) (auto simp: setle_def isUb_def intro: cSup_upper)
   1.986 +
   1.987 +
   1.988 +subsection {* Hiding implementation details *}
   1.989 +
   1.990 +hide_const (open) vanishes cauchy positive Real
   1.991 +
   1.992 +declare Real_induct [induct del]
   1.993 +declare Abs_real_induct [induct del]
   1.994 +declare Abs_real_cases [cases del]
   1.995 +
   1.996 +lemmas [transfer_rule del] =
   1.997 +  real.All_transfer real.Ex_transfer real.rel_eq_transfer forall_real_transfer
   1.998 +  zero_real.transfer one_real.transfer plus_real.transfer uminus_real.transfer
   1.999 +  times_real.transfer inverse_real.transfer positive.transfer real.right_unique
  1.1000 +  real.right_total
  1.1001 +
  1.1002 +subsection{*More Lemmas*}
  1.1003 +
  1.1004 +text {* BH: These lemmas should not be necessary; they should be
  1.1005 +covered by existing simp rules and simplification procedures. *}
  1.1006 +
  1.1007 +lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
  1.1008 +by simp (* redundant with mult_cancel_left *)
  1.1009 +
  1.1010 +lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
  1.1011 +by simp (* redundant with mult_cancel_right *)
  1.1012 +
  1.1013 +lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
  1.1014 +by simp (* solved by linordered_ring_less_cancel_factor simproc *)
  1.1015 +
  1.1016 +lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
  1.1017 +by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1.1018 +
  1.1019 +lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
  1.1020 +by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1.1021 +
  1.1022 +
  1.1023 +subsection {* Embedding numbers into the Reals *}
  1.1024 +
  1.1025 +abbreviation
  1.1026 +  real_of_nat :: "nat \<Rightarrow> real"
  1.1027 +where
  1.1028 +  "real_of_nat \<equiv> of_nat"
  1.1029 +
  1.1030 +abbreviation
  1.1031 +  real_of_int :: "int \<Rightarrow> real"
  1.1032 +where
  1.1033 +  "real_of_int \<equiv> of_int"
  1.1034 +
  1.1035 +abbreviation
  1.1036 +  real_of_rat :: "rat \<Rightarrow> real"
  1.1037 +where
  1.1038 +  "real_of_rat \<equiv> of_rat"
  1.1039 +
  1.1040 +consts
  1.1041 +  (*overloaded constant for injecting other types into "real"*)
  1.1042 +  real :: "'a => real"
  1.1043 +
  1.1044 +defs (overloaded)
  1.1045 +  real_of_nat_def [code_unfold]: "real == real_of_nat"
  1.1046 +  real_of_int_def [code_unfold]: "real == real_of_int"
  1.1047 +
  1.1048 +declare [[coercion_enabled]]
  1.1049 +declare [[coercion "real::nat\<Rightarrow>real"]]
  1.1050 +declare [[coercion "real::int\<Rightarrow>real"]]
  1.1051 +declare [[coercion "int"]]
  1.1052 +
  1.1053 +declare [[coercion_map map]]
  1.1054 +declare [[coercion_map "% f g h x. g (h (f x))"]]
  1.1055 +declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
  1.1056 +
  1.1057 +lemma real_eq_of_nat: "real = of_nat"
  1.1058 +  unfolding real_of_nat_def ..
  1.1059 +
  1.1060 +lemma real_eq_of_int: "real = of_int"
  1.1061 +  unfolding real_of_int_def ..
  1.1062 +
  1.1063 +lemma real_of_int_zero [simp]: "real (0::int) = 0"  
  1.1064 +by (simp add: real_of_int_def) 
  1.1065 +
  1.1066 +lemma real_of_one [simp]: "real (1::int) = (1::real)"
  1.1067 +by (simp add: real_of_int_def) 
  1.1068 +
  1.1069 +lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
  1.1070 +by (simp add: real_of_int_def) 
  1.1071 +
  1.1072 +lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
  1.1073 +by (simp add: real_of_int_def) 
  1.1074 +
  1.1075 +lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
  1.1076 +by (simp add: real_of_int_def) 
  1.1077 +
  1.1078 +lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
  1.1079 +by (simp add: real_of_int_def) 
  1.1080 +
  1.1081 +lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
  1.1082 +by (simp add: real_of_int_def of_int_power)
  1.1083 +
  1.1084 +lemmas power_real_of_int = real_of_int_power [symmetric]
  1.1085 +
  1.1086 +lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
  1.1087 +  apply (subst real_eq_of_int)+
  1.1088 +  apply (rule of_int_setsum)
  1.1089 +done
  1.1090 +
  1.1091 +lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
  1.1092 +    (PROD x:A. real(f x))"
  1.1093 +  apply (subst real_eq_of_int)+
  1.1094 +  apply (rule of_int_setprod)
  1.1095 +done
  1.1096 +
  1.1097 +lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
  1.1098 +by (simp add: real_of_int_def) 
  1.1099 +
  1.1100 +lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
  1.1101 +by (simp add: real_of_int_def) 
  1.1102 +
  1.1103 +lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
  1.1104 +by (simp add: real_of_int_def) 
  1.1105 +
  1.1106 +lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
  1.1107 +by (simp add: real_of_int_def) 
  1.1108 +
  1.1109 +lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
  1.1110 +by (simp add: real_of_int_def) 
  1.1111 +
  1.1112 +lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
  1.1113 +by (simp add: real_of_int_def) 
  1.1114 +
  1.1115 +lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
  1.1116 +by (simp add: real_of_int_def)
  1.1117 +
  1.1118 +lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
  1.1119 +by (simp add: real_of_int_def)
  1.1120 +
  1.1121 +lemma one_less_real_of_int_cancel_iff: "1 < real (i :: int) \<longleftrightarrow> 1 < i"
  1.1122 +  unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1.1123 +
  1.1124 +lemma one_le_real_of_int_cancel_iff: "1 \<le> real (i :: int) \<longleftrightarrow> 1 \<le> i"
  1.1125 +  unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1.1126 +
  1.1127 +lemma real_of_int_less_one_cancel_iff: "real (i :: int) < 1 \<longleftrightarrow> i < 1"
  1.1128 +  unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1.1129 +
  1.1130 +lemma real_of_int_le_one_cancel_iff: "real (i :: int) \<le> 1 \<longleftrightarrow> i \<le> 1"
  1.1131 +  unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1.1132 +
  1.1133 +lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
  1.1134 +by (auto simp add: abs_if)
  1.1135 +
  1.1136 +lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
  1.1137 +  apply (subgoal_tac "real n + 1 = real (n + 1)")
  1.1138 +  apply (simp del: real_of_int_add)
  1.1139 +  apply auto
  1.1140 +done
  1.1141 +
  1.1142 +lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
  1.1143 +  apply (subgoal_tac "real m + 1 = real (m + 1)")
  1.1144 +  apply (simp del: real_of_int_add)
  1.1145 +  apply simp
  1.1146 +done
  1.1147 +
  1.1148 +lemma real_of_int_div_aux: "(real (x::int)) / (real d) = 
  1.1149 +    real (x div d) + (real (x mod d)) / (real d)"
  1.1150 +proof -
  1.1151 +  have "x = (x div d) * d + x mod d"
  1.1152 +    by auto
  1.1153 +  then have "real x = real (x div d) * real d + real(x mod d)"
  1.1154 +    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
  1.1155 +  then have "real x / real d = ... / real d"
  1.1156 +    by simp
  1.1157 +  then show ?thesis
  1.1158 +    by (auto simp add: add_divide_distrib algebra_simps)
  1.1159 +qed
  1.1160 +
  1.1161 +lemma real_of_int_div: "(d :: int) dvd n ==>
  1.1162 +    real(n div d) = real n / real d"
  1.1163 +  apply (subst real_of_int_div_aux)
  1.1164 +  apply simp
  1.1165 +  apply (simp add: dvd_eq_mod_eq_0)
  1.1166 +done
  1.1167 +
  1.1168 +lemma real_of_int_div2:
  1.1169 +  "0 <= real (n::int) / real (x) - real (n div x)"
  1.1170 +  apply (case_tac "x = 0")
  1.1171 +  apply simp
  1.1172 +  apply (case_tac "0 < x")
  1.1173 +  apply (simp add: algebra_simps)
  1.1174 +  apply (subst real_of_int_div_aux)
  1.1175 +  apply simp
  1.1176 +  apply (subst zero_le_divide_iff)
  1.1177 +  apply auto
  1.1178 +  apply (simp add: algebra_simps)
  1.1179 +  apply (subst real_of_int_div_aux)
  1.1180 +  apply simp
  1.1181 +  apply (subst zero_le_divide_iff)
  1.1182 +  apply auto
  1.1183 +done
  1.1184 +
  1.1185 +lemma real_of_int_div3:
  1.1186 +  "real (n::int) / real (x) - real (n div x) <= 1"
  1.1187 +  apply (simp add: algebra_simps)
  1.1188 +  apply (subst real_of_int_div_aux)
  1.1189 +  apply (auto simp add: divide_le_eq intro: order_less_imp_le)
  1.1190 +done
  1.1191 +
  1.1192 +lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
  1.1193 +by (insert real_of_int_div2 [of n x], simp)
  1.1194 +
  1.1195 +lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1.1196 +unfolding real_of_int_def by (rule Ints_of_int)
  1.1197 +
  1.1198 +
  1.1199 +subsection{*Embedding the Naturals into the Reals*}
  1.1200 +
  1.1201 +lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1.1202 +by (simp add: real_of_nat_def)
  1.1203 +
  1.1204 +lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1.1205 +by (simp add: real_of_nat_def)
  1.1206 +
  1.1207 +lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
  1.1208 +by (simp add: real_of_nat_def)
  1.1209 +
  1.1210 +lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
  1.1211 +by (simp add: real_of_nat_def)
  1.1212 +
  1.1213 +(*Not for addsimps: often the LHS is used to represent a positive natural*)
  1.1214 +lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
  1.1215 +by (simp add: real_of_nat_def)
  1.1216 +
  1.1217 +lemma real_of_nat_less_iff [iff]: 
  1.1218 +     "(real (n::nat) < real m) = (n < m)"
  1.1219 +by (simp add: real_of_nat_def)
  1.1220 +
  1.1221 +lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
  1.1222 +by (simp add: real_of_nat_def)
  1.1223 +
  1.1224 +lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
  1.1225 +by (simp add: real_of_nat_def)
  1.1226 +
  1.1227 +lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
  1.1228 +by (simp add: real_of_nat_def del: of_nat_Suc)
  1.1229 +
  1.1230 +lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
  1.1231 +by (simp add: real_of_nat_def of_nat_mult)
  1.1232 +
  1.1233 +lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
  1.1234 +by (simp add: real_of_nat_def of_nat_power)
  1.1235 +
  1.1236 +lemmas power_real_of_nat = real_of_nat_power [symmetric]
  1.1237 +
  1.1238 +lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
  1.1239 +    (SUM x:A. real(f x))"
  1.1240 +  apply (subst real_eq_of_nat)+
  1.1241 +  apply (rule of_nat_setsum)
  1.1242 +done
  1.1243 +
  1.1244 +lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
  1.1245 +    (PROD x:A. real(f x))"
  1.1246 +  apply (subst real_eq_of_nat)+
  1.1247 +  apply (rule of_nat_setprod)
  1.1248 +done
  1.1249 +
  1.1250 +lemma real_of_card: "real (card A) = setsum (%x.1) A"
  1.1251 +  apply (subst card_eq_setsum)
  1.1252 +  apply (subst real_of_nat_setsum)
  1.1253 +  apply simp
  1.1254 +done
  1.1255 +
  1.1256 +lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
  1.1257 +by (simp add: real_of_nat_def)
  1.1258 +
  1.1259 +lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
  1.1260 +by (simp add: real_of_nat_def)
  1.1261 +
  1.1262 +lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
  1.1263 +by (simp add: add: real_of_nat_def of_nat_diff)
  1.1264 +
  1.1265 +lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
  1.1266 +by (auto simp: real_of_nat_def)
  1.1267 +
  1.1268 +lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
  1.1269 +by (simp add: add: real_of_nat_def)
  1.1270 +
  1.1271 +lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
  1.1272 +by (simp add: add: real_of_nat_def)
  1.1273 +
  1.1274 +lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
  1.1275 +  apply (subgoal_tac "real n + 1 = real (Suc n)")
  1.1276 +  apply simp
  1.1277 +  apply (auto simp add: real_of_nat_Suc)
  1.1278 +done
  1.1279 +
  1.1280 +lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
  1.1281 +  apply (subgoal_tac "real m + 1 = real (Suc m)")
  1.1282 +  apply (simp add: less_Suc_eq_le)
  1.1283 +  apply (simp add: real_of_nat_Suc)
  1.1284 +done
  1.1285 +
  1.1286 +lemma real_of_nat_div_aux: "(real (x::nat)) / (real d) = 
  1.1287 +    real (x div d) + (real (x mod d)) / (real d)"
  1.1288 +proof -
  1.1289 +  have "x = (x div d) * d + x mod d"
  1.1290 +    by auto
  1.1291 +  then have "real x = real (x div d) * real d + real(x mod d)"
  1.1292 +    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
  1.1293 +  then have "real x / real d = \<dots> / real d"
  1.1294 +    by simp
  1.1295 +  then show ?thesis
  1.1296 +    by (auto simp add: add_divide_distrib algebra_simps)
  1.1297 +qed
  1.1298 +
  1.1299 +lemma real_of_nat_div: "(d :: nat) dvd n ==>
  1.1300 +    real(n div d) = real n / real d"
  1.1301 +  by (subst real_of_nat_div_aux)
  1.1302 +    (auto simp add: dvd_eq_mod_eq_0 [symmetric])
  1.1303 +
  1.1304 +lemma real_of_nat_div2:
  1.1305 +  "0 <= real (n::nat) / real (x) - real (n div x)"
  1.1306 +apply (simp add: algebra_simps)
  1.1307 +apply (subst real_of_nat_div_aux)
  1.1308 +apply simp
  1.1309 +apply (subst zero_le_divide_iff)
  1.1310 +apply simp
  1.1311 +done
  1.1312 +
  1.1313 +lemma real_of_nat_div3:
  1.1314 +  "real (n::nat) / real (x) - real (n div x) <= 1"
  1.1315 +apply(case_tac "x = 0")
  1.1316 +apply (simp)
  1.1317 +apply (simp add: algebra_simps)
  1.1318 +apply (subst real_of_nat_div_aux)
  1.1319 +apply simp
  1.1320 +done
  1.1321 +
  1.1322 +lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
  1.1323 +by (insert real_of_nat_div2 [of n x], simp)
  1.1324 +
  1.1325 +lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
  1.1326 +by (simp add: real_of_int_def real_of_nat_def)
  1.1327 +
  1.1328 +lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
  1.1329 +  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
  1.1330 +  apply force
  1.1331 +  apply (simp only: real_of_int_of_nat_eq)
  1.1332 +done
  1.1333 +
  1.1334 +lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
  1.1335 +unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1.1336 +
  1.1337 +lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1.1338 +unfolding real_of_nat_def by (rule Ints_of_nat)
  1.1339 +
  1.1340 +subsection {* The Archimedean Property of the Reals *}
  1.1341 +
  1.1342 +theorem reals_Archimedean:
  1.1343 +  assumes x_pos: "0 < x"
  1.1344 +  shows "\<exists>n. inverse (real (Suc n)) < x"
  1.1345 +  unfolding real_of_nat_def using x_pos
  1.1346 +  by (rule ex_inverse_of_nat_Suc_less)
  1.1347 +
  1.1348 +lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
  1.1349 +  unfolding real_of_nat_def by (rule ex_less_of_nat)
  1.1350 +
  1.1351 +lemma reals_Archimedean3:
  1.1352 +  assumes x_greater_zero: "0 < x"
  1.1353 +  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
  1.1354 +  unfolding real_of_nat_def using `0 < x`
  1.1355 +  by (auto intro: ex_less_of_nat_mult)
  1.1356 +
  1.1357 +
  1.1358 +subsection{* Rationals *}
  1.1359 +
  1.1360 +lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
  1.1361 +by (simp add: real_eq_of_nat)
  1.1362 +
  1.1363 +
  1.1364 +lemma Rats_eq_int_div_int:
  1.1365 +  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
  1.1366 +proof
  1.1367 +  show "\<rat> \<subseteq> ?S"
  1.1368 +  proof
  1.1369 +    fix x::real assume "x : \<rat>"
  1.1370 +    then obtain r where "x = of_rat r" unfolding Rats_def ..
  1.1371 +    have "of_rat r : ?S"
  1.1372 +      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1.1373 +    thus "x : ?S" using `x = of_rat r` by simp
  1.1374 +  qed
  1.1375 +next
  1.1376 +  show "?S \<subseteq> \<rat>"
  1.1377 +  proof(auto simp:Rats_def)
  1.1378 +    fix i j :: int assume "j \<noteq> 0"
  1.1379 +    hence "real i / real j = of_rat(Fract i j)"
  1.1380 +      by (simp add:of_rat_rat real_eq_of_int)
  1.1381 +    thus "real i / real j \<in> range of_rat" by blast
  1.1382 +  qed
  1.1383 +qed
  1.1384 +
  1.1385 +lemma Rats_eq_int_div_nat:
  1.1386 +  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
  1.1387 +proof(auto simp:Rats_eq_int_div_int)
  1.1388 +  fix i j::int assume "j \<noteq> 0"
  1.1389 +  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
  1.1390 +  proof cases
  1.1391 +    assume "j>0"
  1.1392 +    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1.1393 +      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1.1394 +    thus ?thesis by blast
  1.1395 +  next
  1.1396 +    assume "~ j>0"
  1.1397 +    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1.1398 +      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1.1399 +    thus ?thesis by blast
  1.1400 +  qed
  1.1401 +next
  1.1402 +  fix i::int and n::nat assume "0 < n"
  1.1403 +  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
  1.1404 +  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
  1.1405 +qed
  1.1406 +
  1.1407 +lemma Rats_abs_nat_div_natE:
  1.1408 +  assumes "x \<in> \<rat>"
  1.1409 +  obtains m n :: nat
  1.1410 +  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1.1411 +proof -
  1.1412 +  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
  1.1413 +    by(auto simp add: Rats_eq_int_div_nat)
  1.1414 +  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
  1.1415 +  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1.1416 +  let ?gcd = "gcd m n"
  1.1417 +  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
  1.1418 +  let ?k = "m div ?gcd"
  1.1419 +  let ?l = "n div ?gcd"
  1.1420 +  let ?gcd' = "gcd ?k ?l"
  1.1421 +  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
  1.1422 +    by (rule dvd_mult_div_cancel)
  1.1423 +  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
  1.1424 +    by (rule dvd_mult_div_cancel)
  1.1425 +  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
  1.1426 +  moreover
  1.1427 +  have "\<bar>x\<bar> = real ?k / real ?l"
  1.1428 +  proof -
  1.1429 +    from gcd have "real ?k / real ?l =
  1.1430 +        real (?gcd * ?k) / real (?gcd * ?l)" by simp
  1.1431 +    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1.1432 +    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1.1433 +    finally show ?thesis ..
  1.1434 +  qed
  1.1435 +  moreover
  1.1436 +  have "?gcd' = 1"
  1.1437 +  proof -
  1.1438 +    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1.1439 +      by (rule gcd_mult_distrib_nat)
  1.1440 +    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1.1441 +    with gcd show ?thesis by auto
  1.1442 +  qed
  1.1443 +  ultimately show ?thesis ..
  1.1444 +qed
  1.1445 +
  1.1446 +subsection{*Density of the Rational Reals in the Reals*}
  1.1447 +
  1.1448 +text{* This density proof is due to Stefan Richter and was ported by TN.  The
  1.1449 +original source is \emph{Real Analysis} by H.L. Royden.
  1.1450 +It employs the Archimedean property of the reals. *}
  1.1451 +
  1.1452 +lemma Rats_dense_in_real:
  1.1453 +  fixes x :: real
  1.1454 +  assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
  1.1455 +proof -
  1.1456 +  from `x<y` have "0 < y-x" by simp
  1.1457 +  with reals_Archimedean obtain q::nat 
  1.1458 +    where q: "inverse (real q) < y-x" and "0 < q" by auto
  1.1459 +  def p \<equiv> "ceiling (y * real q) - 1"
  1.1460 +  def r \<equiv> "of_int p / real q"
  1.1461 +  from q have "x < y - inverse (real q)" by simp
  1.1462 +  also have "y - inverse (real q) \<le> r"
  1.1463 +    unfolding r_def p_def
  1.1464 +    by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
  1.1465 +  finally have "x < r" .
  1.1466 +  moreover have "r < y"
  1.1467 +    unfolding r_def p_def
  1.1468 +    by (simp add: divide_less_eq diff_less_eq `0 < q`
  1.1469 +      less_ceiling_iff [symmetric])
  1.1470 +  moreover from r_def have "r \<in> \<rat>" by simp
  1.1471 +  ultimately show ?thesis by fast
  1.1472 +qed
  1.1473 +
  1.1474 +
  1.1475 +
  1.1476 +subsection{*Numerals and Arithmetic*}
  1.1477 +
  1.1478 +lemma [code_abbrev]:
  1.1479 +  "real_of_int (numeral k) = numeral k"
  1.1480 +  "real_of_int (neg_numeral k) = neg_numeral k"
  1.1481 +  by simp_all
  1.1482 +
  1.1483 +text{*Collapse applications of @{term real} to @{term number_of}*}
  1.1484 +lemma real_numeral [simp]:
  1.1485 +  "real (numeral v :: int) = numeral v"
  1.1486 +  "real (neg_numeral v :: int) = neg_numeral v"
  1.1487 +by (simp_all add: real_of_int_def)
  1.1488 +
  1.1489 +lemma real_of_nat_numeral [simp]:
  1.1490 +  "real (numeral v :: nat) = numeral v"
  1.1491 +by (simp add: real_of_nat_def)
  1.1492 +
  1.1493 +declaration {*
  1.1494 +  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
  1.1495 +    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
  1.1496 +  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
  1.1497 +    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
  1.1498 +  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
  1.1499 +      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
  1.1500 +      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
  1.1501 +      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
  1.1502 +      @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
  1.1503 +  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
  1.1504 +  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
  1.1505 +*}
  1.1506 +
  1.1507 +
  1.1508 +subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
  1.1509 +
  1.1510 +lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
  1.1511 +by arith
  1.1512 +
  1.1513 +text {* FIXME: redundant with @{text add_eq_0_iff} below *}
  1.1514 +lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
  1.1515 +by auto
  1.1516 +
  1.1517 +lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
  1.1518 +by auto
  1.1519 +
  1.1520 +lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
  1.1521 +by auto
  1.1522 +
  1.1523 +lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
  1.1524 +by auto
  1.1525 +
  1.1526 +lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
  1.1527 +by auto
  1.1528 +
  1.1529 +subsection {* Lemmas about powers *}
  1.1530 +
  1.1531 +text {* FIXME: declare this in Rings.thy or not at all *}
  1.1532 +declare abs_mult_self [simp]
  1.1533 +
  1.1534 +(* used by Import/HOL/real.imp *)
  1.1535 +lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1.1536 +by simp
  1.1537 +
  1.1538 +lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
  1.1539 +apply (induct "n")
  1.1540 +apply (auto simp add: real_of_nat_Suc)
  1.1541 +apply (subst mult_2)
  1.1542 +apply (erule add_less_le_mono)
  1.1543 +apply (rule two_realpow_ge_one)
  1.1544 +done
  1.1545 +
  1.1546 +text {* TODO: no longer real-specific; rename and move elsewhere *}
  1.1547 +lemma realpow_Suc_le_self:
  1.1548 +  fixes r :: "'a::linordered_semidom"
  1.1549 +  shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
  1.1550 +by (insert power_decreasing [of 1 "Suc n" r], simp)
  1.1551 +
  1.1552 +text {* TODO: no longer real-specific; rename and move elsewhere *}
  1.1553 +lemma realpow_minus_mult:
  1.1554 +  fixes x :: "'a::monoid_mult"
  1.1555 +  shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
  1.1556 +by (simp add: power_commutes split add: nat_diff_split)
  1.1557 +
  1.1558 +text {* FIXME: declare this [simp] for all types, or not at all *}
  1.1559 +lemma real_two_squares_add_zero_iff [simp]:
  1.1560 +  "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
  1.1561 +by (rule sum_squares_eq_zero_iff)
  1.1562 +
  1.1563 +text {* FIXME: declare this [simp] for all types, or not at all *}
  1.1564 +lemma realpow_two_sum_zero_iff [simp]:
  1.1565 +     "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
  1.1566 +by (rule sum_power2_eq_zero_iff)
  1.1567 +
  1.1568 +lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
  1.1569 +by (rule_tac y = 0 in order_trans, auto)
  1.1570 +
  1.1571 +lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
  1.1572 +by (auto simp add: power2_eq_square)
  1.1573 +
  1.1574 +
  1.1575 +lemma numeral_power_le_real_of_nat_cancel_iff[simp]:
  1.1576 +  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::nat) ^ n \<le> a"
  1.1577 +  unfolding real_of_nat_le_iff[symmetric] by simp
  1.1578 +
  1.1579 +lemma real_of_nat_le_numeral_power_cancel_iff[simp]:
  1.1580 +  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::nat) ^ n"
  1.1581 +  unfolding real_of_nat_le_iff[symmetric] by simp
  1.1582 +
  1.1583 +lemma numeral_power_le_real_of_int_cancel_iff[simp]:
  1.1584 +  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::int) ^ n \<le> a"
  1.1585 +  unfolding real_of_int_le_iff[symmetric] by simp
  1.1586 +
  1.1587 +lemma real_of_int_le_numeral_power_cancel_iff[simp]:
  1.1588 +  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::int) ^ n"
  1.1589 +  unfolding real_of_int_le_iff[symmetric] by simp
  1.1590 +
  1.1591 +lemma neg_numeral_power_le_real_of_int_cancel_iff[simp]:
  1.1592 +  "(neg_numeral x::real) ^ n \<le> real a \<longleftrightarrow> (neg_numeral x::int) ^ n \<le> a"
  1.1593 +  unfolding real_of_int_le_iff[symmetric] by simp
  1.1594 +
  1.1595 +lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]:
  1.1596 +  "real a \<le> (neg_numeral x::real) ^ n \<longleftrightarrow> a \<le> (neg_numeral x::int) ^ n"
  1.1597 +  unfolding real_of_int_le_iff[symmetric] by simp
  1.1598 +
  1.1599 +subsection{*Density of the Reals*}
  1.1600 +
  1.1601 +lemma real_lbound_gt_zero:
  1.1602 +     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
  1.1603 +apply (rule_tac x = " (min d1 d2) /2" in exI)
  1.1604 +apply (simp add: min_def)
  1.1605 +done
  1.1606 +
  1.1607 +
  1.1608 +text{*Similar results are proved in @{text Fields}*}
  1.1609 +lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
  1.1610 +  by auto
  1.1611 +
  1.1612 +lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1.1613 +  by auto
  1.1614 +
  1.1615 +lemma real_sum_of_halves: "x/2 + x/2 = (x::real)"
  1.1616 +  by simp
  1.1617 +
  1.1618 +subsection{*Absolute Value Function for the Reals*}
  1.1619 +
  1.1620 +lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
  1.1621 +by (simp add: abs_if)
  1.1622 +
  1.1623 +(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
  1.1624 +lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
  1.1625 +by (force simp add: abs_le_iff)
  1.1626 +
  1.1627 +lemma abs_add_one_gt_zero: "(0::real) < 1 + abs(x)"
  1.1628 +by (simp add: abs_if)
  1.1629 +
  1.1630 +lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
  1.1631 +by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
  1.1632 +
  1.1633 +lemma abs_add_one_not_less_self: "~ abs(x) + (1::real) < x"
  1.1634 +by simp
  1.1635 + 
  1.1636 +lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1.1637 +by simp
  1.1638 +
  1.1639 +
  1.1640 +subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
  1.1641 +
  1.1642 +(* FIXME: theorems for negative numerals *)
  1.1643 +lemma numeral_less_real_of_int_iff [simp]:
  1.1644 +     "((numeral n) < real (m::int)) = (numeral n < m)"
  1.1645 +apply auto
  1.1646 +apply (rule real_of_int_less_iff [THEN iffD1])
  1.1647 +apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
  1.1648 +done
  1.1649 +
  1.1650 +lemma numeral_less_real_of_int_iff2 [simp]:
  1.1651 +     "(real (m::int) < (numeral n)) = (m < numeral n)"
  1.1652 +apply auto
  1.1653 +apply (rule real_of_int_less_iff [THEN iffD1])
  1.1654 +apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
  1.1655 +done
  1.1656 +
  1.1657 +lemma numeral_le_real_of_int_iff [simp]:
  1.1658 +     "((numeral n) \<le> real (m::int)) = (numeral n \<le> m)"
  1.1659 +by (simp add: linorder_not_less [symmetric])
  1.1660 +
  1.1661 +lemma numeral_le_real_of_int_iff2 [simp]:
  1.1662 +     "(real (m::int) \<le> (numeral n)) = (m \<le> numeral n)"
  1.1663 +by (simp add: linorder_not_less [symmetric])
  1.1664 +
  1.1665 +lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
  1.1666 +unfolding real_of_nat_def by simp
  1.1667 +
  1.1668 +lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
  1.1669 +unfolding real_of_nat_def by (simp add: floor_minus)
  1.1670 +
  1.1671 +lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
  1.1672 +unfolding real_of_int_def by simp
  1.1673 +
  1.1674 +lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
  1.1675 +unfolding real_of_int_def by (simp add: floor_minus)
  1.1676 +
  1.1677 +lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
  1.1678 +unfolding real_of_int_def by (rule floor_exists)
  1.1679 +
  1.1680 +lemma lemma_floor:
  1.1681 +  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
  1.1682 +  shows "m \<le> (n::int)"
  1.1683 +proof -
  1.1684 +  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
  1.1685 +  also have "... = real (n + 1)" by simp
  1.1686 +  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
  1.1687 +  thus ?thesis by arith
  1.1688 +qed
  1.1689 +
  1.1690 +lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
  1.1691 +unfolding real_of_int_def by (rule of_int_floor_le)
  1.1692 +
  1.1693 +lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
  1.1694 +by (auto intro: lemma_floor)
  1.1695 +
  1.1696 +lemma real_of_int_floor_cancel [simp]:
  1.1697 +    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
  1.1698 +  using floor_real_of_int by metis
  1.1699 +
  1.1700 +lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
  1.1701 +  unfolding real_of_int_def using floor_unique [of n x] by simp
  1.1702 +
  1.1703 +lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
  1.1704 +  unfolding real_of_int_def by (rule floor_unique)
  1.1705 +
  1.1706 +lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
  1.1707 +apply (rule inj_int [THEN injD])
  1.1708 +apply (simp add: real_of_nat_Suc)
  1.1709 +apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
  1.1710 +done
  1.1711 +
  1.1712 +lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
  1.1713 +apply (drule order_le_imp_less_or_eq)
  1.1714 +apply (auto intro: floor_eq3)
  1.1715 +done
  1.1716 +
  1.1717 +lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
  1.1718 +  unfolding real_of_int_def using floor_correct [of r] by simp
  1.1719 +
  1.1720 +lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
  1.1721 +  unfolding real_of_int_def using floor_correct [of r] by simp
  1.1722 +
  1.1723 +lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
  1.1724 +  unfolding real_of_int_def using floor_correct [of r] by simp
  1.1725 +
  1.1726 +lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
  1.1727 +  unfolding real_of_int_def using floor_correct [of r] by simp
  1.1728 +
  1.1729 +lemma le_floor: "real a <= x ==> a <= floor x"
  1.1730 +  unfolding real_of_int_def by (simp add: le_floor_iff)
  1.1731 +
  1.1732 +lemma real_le_floor: "a <= floor x ==> real a <= x"
  1.1733 +  unfolding real_of_int_def by (simp add: le_floor_iff)
  1.1734 +
  1.1735 +lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
  1.1736 +  unfolding real_of_int_def by (rule le_floor_iff)
  1.1737 +
  1.1738 +lemma floor_less_eq: "(floor x < a) = (x < real a)"
  1.1739 +  unfolding real_of_int_def by (rule floor_less_iff)
  1.1740 +
  1.1741 +lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
  1.1742 +  unfolding real_of_int_def by (rule less_floor_iff)
  1.1743 +
  1.1744 +lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
  1.1745 +  unfolding real_of_int_def by (rule floor_le_iff)
  1.1746 +
  1.1747 +lemma floor_add [simp]: "floor (x + real a) = floor x + a"
  1.1748 +  unfolding real_of_int_def by (rule floor_add_of_int)
  1.1749 +
  1.1750 +lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
  1.1751 +  unfolding real_of_int_def by (rule floor_diff_of_int)
  1.1752 +
  1.1753 +lemma le_mult_floor:
  1.1754 +  assumes "0 \<le> (a :: real)" and "0 \<le> b"
  1.1755 +  shows "floor a * floor b \<le> floor (a * b)"
  1.1756 +proof -
  1.1757 +  have "real (floor a) \<le> a"
  1.1758 +    and "real (floor b) \<le> b" by auto
  1.1759 +  hence "real (floor a * floor b) \<le> a * b"
  1.1760 +    using assms by (auto intro!: mult_mono)
  1.1761 +  also have "a * b < real (floor (a * b) + 1)" by auto
  1.1762 +  finally show ?thesis unfolding real_of_int_less_iff by simp
  1.1763 +qed
  1.1764 +
  1.1765 +lemma floor_divide_eq_div:
  1.1766 +  "floor (real a / real b) = a div b"
  1.1767 +proof cases
  1.1768 +  assume "b \<noteq> 0 \<or> b dvd a"
  1.1769 +  with real_of_int_div3[of a b] show ?thesis
  1.1770 +    by (auto simp: real_of_int_div[symmetric] intro!: floor_eq2 real_of_int_div4 neq_le_trans)
  1.1771 +       (metis add_left_cancel zero_neq_one real_of_int_div_aux real_of_int_inject
  1.1772 +              real_of_int_zero_cancel right_inverse_eq div_self mod_div_trivial)
  1.1773 +qed (auto simp: real_of_int_div)
  1.1774 +
  1.1775 +lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
  1.1776 +  unfolding real_of_nat_def by simp
  1.1777 +
  1.1778 +lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
  1.1779 +  unfolding real_of_int_def by (rule le_of_int_ceiling)
  1.1780 +
  1.1781 +lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
  1.1782 +  unfolding real_of_int_def by simp
  1.1783 +
  1.1784 +lemma real_of_int_ceiling_cancel [simp]:
  1.1785 +     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
  1.1786 +  using ceiling_real_of_int by metis
  1.1787 +
  1.1788 +lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
  1.1789 +  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
  1.1790 +
  1.1791 +lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
  1.1792 +  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
  1.1793 +
  1.1794 +lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
  1.1795 +  unfolding real_of_int_def using ceiling_unique [of n x] by simp
  1.1796 +
  1.1797 +lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
  1.1798 +  unfolding real_of_int_def using ceiling_correct [of r] by simp
  1.1799 +
  1.1800 +lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
  1.1801 +  unfolding real_of_int_def using ceiling_correct [of r] by simp
  1.1802 +
  1.1803 +lemma ceiling_le: "x <= real a ==> ceiling x <= a"
  1.1804 +  unfolding real_of_int_def by (simp add: ceiling_le_iff)
  1.1805 +
  1.1806 +lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
  1.1807 +  unfolding real_of_int_def by (simp add: ceiling_le_iff)
  1.1808 +
  1.1809 +lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
  1.1810 +  unfolding real_of_int_def by (rule ceiling_le_iff)
  1.1811 +
  1.1812 +lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
  1.1813 +  unfolding real_of_int_def by (rule less_ceiling_iff)
  1.1814 +
  1.1815 +lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
  1.1816 +  unfolding real_of_int_def by (rule ceiling_less_iff)
  1.1817 +
  1.1818 +lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
  1.1819 +  unfolding real_of_int_def by (rule le_ceiling_iff)
  1.1820 +
  1.1821 +lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
  1.1822 +  unfolding real_of_int_def by (rule ceiling_add_of_int)
  1.1823 +
  1.1824 +lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
  1.1825 +  unfolding real_of_int_def by (rule ceiling_diff_of_int)
  1.1826 +
  1.1827 +
  1.1828 +subsubsection {* Versions for the natural numbers *}
  1.1829 +
  1.1830 +definition
  1.1831 +  natfloor :: "real => nat" where
  1.1832 +  "natfloor x = nat(floor x)"
  1.1833 +
  1.1834 +definition
  1.1835 +  natceiling :: "real => nat" where
  1.1836 +  "natceiling x = nat(ceiling x)"
  1.1837 +
  1.1838 +lemma natfloor_zero [simp]: "natfloor 0 = 0"
  1.1839 +  by (unfold natfloor_def, simp)
  1.1840 +
  1.1841 +lemma natfloor_one [simp]: "natfloor 1 = 1"
  1.1842 +  by (unfold natfloor_def, simp)
  1.1843 +
  1.1844 +lemma zero_le_natfloor [simp]: "0 <= natfloor x"
  1.1845 +  by (unfold natfloor_def, simp)
  1.1846 +
  1.1847 +lemma natfloor_numeral_eq [simp]: "natfloor (numeral n) = numeral n"
  1.1848 +  by (unfold natfloor_def, simp)
  1.1849 +
  1.1850 +lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
  1.1851 +  by (unfold natfloor_def, simp)
  1.1852 +
  1.1853 +lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
  1.1854 +  by (unfold natfloor_def, simp)
  1.1855 +
  1.1856 +lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
  1.1857 +  unfolding natfloor_def by simp
  1.1858 +
  1.1859 +lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
  1.1860 +  unfolding natfloor_def by (intro nat_mono floor_mono)
  1.1861 +
  1.1862 +lemma le_natfloor: "real x <= a ==> x <= natfloor a"
  1.1863 +  apply (unfold natfloor_def)
  1.1864 +  apply (subst nat_int [THEN sym])
  1.1865 +  apply (rule nat_mono)
  1.1866 +  apply (rule le_floor)
  1.1867 +  apply simp
  1.1868 +done
  1.1869 +
  1.1870 +lemma natfloor_less_iff: "0 \<le> x \<Longrightarrow> natfloor x < n \<longleftrightarrow> x < real n"
  1.1871 +  unfolding natfloor_def real_of_nat_def
  1.1872 +  by (simp add: nat_less_iff floor_less_iff)
  1.1873 +
  1.1874 +lemma less_natfloor:
  1.1875 +  assumes "0 \<le> x" and "x < real (n :: nat)"
  1.1876 +  shows "natfloor x < n"
  1.1877 +  using assms by (simp add: natfloor_less_iff)
  1.1878 +
  1.1879 +lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
  1.1880 +  apply (rule iffI)
  1.1881 +  apply (rule order_trans)
  1.1882 +  prefer 2
  1.1883 +  apply (erule real_natfloor_le)
  1.1884 +  apply (subst real_of_nat_le_iff)
  1.1885 +  apply assumption
  1.1886 +  apply (erule le_natfloor)
  1.1887 +done
  1.1888 +
  1.1889 +lemma le_natfloor_eq_numeral [simp]:
  1.1890 +    "~ neg((numeral n)::int) ==> 0 <= x ==>
  1.1891 +      (numeral n <= natfloor x) = (numeral n <= x)"
  1.1892 +  apply (subst le_natfloor_eq, assumption)
  1.1893 +  apply simp
  1.1894 +done
  1.1895 +
  1.1896 +lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
  1.1897 +  apply (case_tac "0 <= x")
  1.1898 +  apply (subst le_natfloor_eq, assumption, simp)
  1.1899 +  apply (rule iffI)
  1.1900 +  apply (subgoal_tac "natfloor x <= natfloor 0")
  1.1901 +  apply simp
  1.1902 +  apply (rule natfloor_mono)
  1.1903 +  apply simp
  1.1904 +  apply simp
  1.1905 +done
  1.1906 +
  1.1907 +lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
  1.1908 +  unfolding natfloor_def by (simp add: floor_eq2 [where n="int n"])
  1.1909 +
  1.1910 +lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
  1.1911 +  apply (case_tac "0 <= x")
  1.1912 +  apply (unfold natfloor_def)
  1.1913 +  apply simp
  1.1914 +  apply simp_all
  1.1915 +done
  1.1916 +
  1.1917 +lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
  1.1918 +using real_natfloor_add_one_gt by (simp add: algebra_simps)
  1.1919 +
  1.1920 +lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
  1.1921 +  apply (subgoal_tac "z < real(natfloor z) + 1")
  1.1922 +  apply arith
  1.1923 +  apply (rule real_natfloor_add_one_gt)
  1.1924 +done
  1.1925 +
  1.1926 +lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
  1.1927 +  unfolding natfloor_def
  1.1928 +  unfolding real_of_int_of_nat_eq [symmetric] floor_add
  1.1929 +  by (simp add: nat_add_distrib)
  1.1930 +
  1.1931 +lemma natfloor_add_numeral [simp]:
  1.1932 +    "~neg ((numeral n)::int) ==> 0 <= x ==>
  1.1933 +      natfloor (x + numeral n) = natfloor x + numeral n"
  1.1934 +  by (simp add: natfloor_add [symmetric])
  1.1935 +
  1.1936 +lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
  1.1937 +  by (simp add: natfloor_add [symmetric] del: One_nat_def)
  1.1938 +
  1.1939 +lemma natfloor_subtract [simp]:
  1.1940 +    "natfloor(x - real a) = natfloor x - a"
  1.1941 +  unfolding natfloor_def real_of_int_of_nat_eq [symmetric] floor_subtract
  1.1942 +  by simp
  1.1943 +
  1.1944 +lemma natfloor_div_nat:
  1.1945 +  assumes "1 <= x" and "y > 0"
  1.1946 +  shows "natfloor (x / real y) = natfloor x div y"
  1.1947 +proof (rule natfloor_eq)
  1.1948 +  have "(natfloor x) div y * y \<le> natfloor x"
  1.1949 +    by (rule add_leD1 [where k="natfloor x mod y"], simp)
  1.1950 +  thus "real (natfloor x div y) \<le> x / real y"
  1.1951 +    using assms by (simp add: le_divide_eq le_natfloor_eq)
  1.1952 +  have "natfloor x < (natfloor x) div y * y + y"
  1.1953 +    apply (subst mod_div_equality [symmetric])
  1.1954 +    apply (rule add_strict_left_mono)
  1.1955 +    apply (rule mod_less_divisor)
  1.1956 +    apply fact
  1.1957 +    done
  1.1958 +  thus "x / real y < real (natfloor x div y) + 1"
  1.1959 +    using assms
  1.1960 +    by (simp add: divide_less_eq natfloor_less_iff distrib_right)
  1.1961 +qed
  1.1962 +
  1.1963 +lemma le_mult_natfloor:
  1.1964 +  shows "natfloor a * natfloor b \<le> natfloor (a * b)"
  1.1965 +  by (cases "0 <= a & 0 <= b")
  1.1966 +    (auto simp add: le_natfloor_eq mult_nonneg_nonneg mult_mono' real_natfloor_le natfloor_neg)
  1.1967 +
  1.1968 +lemma natceiling_zero [simp]: "natceiling 0 = 0"
  1.1969 +  by (unfold natceiling_def, simp)
  1.1970 +
  1.1971 +lemma natceiling_one [simp]: "natceiling 1 = 1"
  1.1972 +  by (unfold natceiling_def, simp)
  1.1973 +
  1.1974 +lemma zero_le_natceiling [simp]: "0 <= natceiling x"
  1.1975 +  by (unfold natceiling_def, simp)
  1.1976 +
  1.1977 +lemma natceiling_numeral_eq [simp]: "natceiling (numeral n) = numeral n"
  1.1978 +  by (unfold natceiling_def, simp)
  1.1979 +
  1.1980 +lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
  1.1981 +  by (unfold natceiling_def, simp)
  1.1982 +
  1.1983 +lemma real_natceiling_ge: "x <= real(natceiling x)"
  1.1984 +  unfolding natceiling_def by (cases "x < 0", simp_all)
  1.1985 +
  1.1986 +lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
  1.1987 +  unfolding natceiling_def by simp
  1.1988 +
  1.1989 +lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
  1.1990 +  unfolding natceiling_def by (intro nat_mono ceiling_mono)
  1.1991 +
  1.1992 +lemma natceiling_le: "x <= real a ==> natceiling x <= a"
  1.1993 +  unfolding natceiling_def real_of_nat_def
  1.1994 +  by (simp add: nat_le_iff ceiling_le_iff)
  1.1995 +
  1.1996 +lemma natceiling_le_eq: "(natceiling x <= a) = (x <= real a)"
  1.1997 +  unfolding natceiling_def real_of_nat_def
  1.1998 +  by (simp add: nat_le_iff ceiling_le_iff)
  1.1999 +
  1.2000 +lemma natceiling_le_eq_numeral [simp]:
  1.2001 +    "~ neg((numeral n)::int) ==>
  1.2002 +      (natceiling x <= numeral n) = (x <= numeral n)"
  1.2003 +  by (simp add: natceiling_le_eq)
  1.2004 +
  1.2005 +lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
  1.2006 +  unfolding natceiling_def
  1.2007 +  by (simp add: nat_le_iff ceiling_le_iff)
  1.2008 +
  1.2009 +lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
  1.2010 +  unfolding natceiling_def
  1.2011 +  by (simp add: ceiling_eq2 [where n="int n"])
  1.2012 +
  1.2013 +lemma natceiling_add [simp]: "0 <= x ==>
  1.2014 +    natceiling (x + real a) = natceiling x + a"
  1.2015 +  unfolding natceiling_def
  1.2016 +  unfolding real_of_int_of_nat_eq [symmetric] ceiling_add
  1.2017 +  by (simp add: nat_add_distrib)
  1.2018 +
  1.2019 +lemma natceiling_add_numeral [simp]:
  1.2020 +    "~ neg ((numeral n)::int) ==> 0 <= x ==>
  1.2021 +      natceiling (x + numeral n) = natceiling x + numeral n"
  1.2022 +  by (simp add: natceiling_add [symmetric])
  1.2023 +
  1.2024 +lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
  1.2025 +  by (simp add: natceiling_add [symmetric] del: One_nat_def)
  1.2026 +
  1.2027 +lemma natceiling_subtract [simp]: "natceiling(x - real a) = natceiling x - a"
  1.2028 +  unfolding natceiling_def real_of_int_of_nat_eq [symmetric] ceiling_subtract
  1.2029 +  by simp
  1.2030 +
  1.2031 +subsection {* Exponentiation with floor *}
  1.2032 +
  1.2033 +lemma floor_power:
  1.2034 +  assumes "x = real (floor x)"
  1.2035 +  shows "floor (x ^ n) = floor x ^ n"
  1.2036 +proof -
  1.2037 +  have *: "x ^ n = real (floor x ^ n)"
  1.2038 +    using assms by (induct n arbitrary: x) simp_all
  1.2039 +  show ?thesis unfolding real_of_int_inject[symmetric]
  1.2040 +    unfolding * floor_real_of_int ..
  1.2041 +qed
  1.2042 +
  1.2043 +lemma natfloor_power:
  1.2044 +  assumes "x = real (natfloor x)"
  1.2045 +  shows "natfloor (x ^ n) = natfloor x ^ n"
  1.2046 +proof -
  1.2047 +  from assms have "0 \<le> floor x" by auto
  1.2048 +  note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
  1.2049 +  from floor_power[OF this]
  1.2050 +  show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
  1.2051 +    by simp
  1.2052 +qed
  1.2053 +
  1.2054 +
  1.2055 +subsection {* Implementation of rational real numbers *}
  1.2056 +
  1.2057 +text {* Formal constructor *}
  1.2058 +
  1.2059 +definition Ratreal :: "rat \<Rightarrow> real" where
  1.2060 +  [code_abbrev, simp]: "Ratreal = of_rat"
  1.2061 +
  1.2062 +code_datatype Ratreal
  1.2063 +
  1.2064 +
  1.2065 +text {* Numerals *}
  1.2066 +
  1.2067 +lemma [code_abbrev]:
  1.2068 +  "(of_rat (of_int a) :: real) = of_int a"
  1.2069 +  by simp
  1.2070 +
  1.2071 +lemma [code_abbrev]:
  1.2072 +  "(of_rat 0 :: real) = 0"
  1.2073 +  by simp
  1.2074 +
  1.2075 +lemma [code_abbrev]:
  1.2076 +  "(of_rat 1 :: real) = 1"
  1.2077 +  by simp
  1.2078 +
  1.2079 +lemma [code_abbrev]:
  1.2080 +  "(of_rat (numeral k) :: real) = numeral k"
  1.2081 +  by simp
  1.2082 +
  1.2083 +lemma [code_abbrev]:
  1.2084 +  "(of_rat (neg_numeral k) :: real) = neg_numeral k"
  1.2085 +  by simp
  1.2086 +
  1.2087 +lemma [code_post]:
  1.2088 +  "(of_rat (0 / r)  :: real) = 0"
  1.2089 +  "(of_rat (r / 0)  :: real) = 0"
  1.2090 +  "(of_rat (1 / 1)  :: real) = 1"
  1.2091 +  "(of_rat (numeral k / 1) :: real) = numeral k"
  1.2092 +  "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
  1.2093 +  "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
  1.2094 +  "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
  1.2095 +  "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
  1.2096 +  "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
  1.2097 +  "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
  1.2098 +  "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
  1.2099 +  by (simp_all add: of_rat_divide)
  1.2100 +
  1.2101 +
  1.2102 +text {* Operations *}
  1.2103 +
  1.2104 +lemma zero_real_code [code]:
  1.2105 +  "0 = Ratreal 0"
  1.2106 +by simp
  1.2107 +
  1.2108 +lemma one_real_code [code]:
  1.2109 +  "1 = Ratreal 1"
  1.2110 +by simp
  1.2111 +
  1.2112 +instantiation real :: equal
  1.2113 +begin
  1.2114 +
  1.2115 +definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
  1.2116 +
  1.2117 +instance proof
  1.2118 +qed (simp add: equal_real_def)
  1.2119 +
  1.2120 +lemma real_equal_code [code]:
  1.2121 +  "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
  1.2122 +  by (simp add: equal_real_def equal)
  1.2123 +
  1.2124 +lemma [code nbe]:
  1.2125 +  "HOL.equal (x::real) x \<longleftrightarrow> True"
  1.2126 +  by (rule equal_refl)
  1.2127 +
  1.2128 +end
  1.2129 +
  1.2130 +lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  1.2131 +  by (simp add: of_rat_less_eq)
  1.2132 +
  1.2133 +lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  1.2134 +  by (simp add: of_rat_less)
  1.2135 +
  1.2136 +lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  1.2137 +  by (simp add: of_rat_add)
  1.2138 +
  1.2139 +lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  1.2140 +  by (simp add: of_rat_mult)
  1.2141 +
  1.2142 +lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  1.2143 +  by (simp add: of_rat_minus)
  1.2144 +
  1.2145 +lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  1.2146 +  by (simp add: of_rat_diff)
  1.2147 +
  1.2148 +lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  1.2149 +  by (simp add: of_rat_inverse)
  1.2150 + 
  1.2151 +lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  1.2152 +  by (simp add: of_rat_divide)
  1.2153 +
  1.2154 +lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  1.2155 +  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)
  1.2156 +
  1.2157 +
  1.2158 +text {* Quickcheck *}
  1.2159 +
  1.2160 +definition (in term_syntax)
  1.2161 +  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  1.2162 +  [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  1.2163 +
  1.2164 +notation fcomp (infixl "\<circ>>" 60)
  1.2165 +notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1.2166 +
  1.2167 +instantiation real :: random
  1.2168 +begin
  1.2169 +
  1.2170 +definition
  1.2171 +  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  1.2172 +
  1.2173 +instance ..
  1.2174 +
  1.2175 +end
  1.2176 +
  1.2177 +no_notation fcomp (infixl "\<circ>>" 60)
  1.2178 +no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1.2179 +
  1.2180 +instantiation real :: exhaustive
  1.2181 +begin
  1.2182 +
  1.2183 +definition
  1.2184 +  "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
  1.2185 +
  1.2186 +instance ..
  1.2187 +
  1.2188 +end
  1.2189 +
  1.2190 +instantiation real :: full_exhaustive
  1.2191 +begin
  1.2192 +
  1.2193 +definition
  1.2194 +  "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
  1.2195 +
  1.2196 +instance ..
  1.2197 +
  1.2198 +end
  1.2199 +
  1.2200 +instantiation real :: narrowing
  1.2201 +begin
  1.2202 +
  1.2203 +definition
  1.2204 +  "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
  1.2205 +
  1.2206 +instance ..
  1.2207 +
  1.2208 +end
  1.2209 +
  1.2210 +
  1.2211 +subsection {* Setup for Nitpick *}
  1.2212 +
  1.2213 +declaration {*
  1.2214 +  Nitpick_HOL.register_frac_type @{type_name real}
  1.2215 +   [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  1.2216 +    (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  1.2217 +    (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  1.2218 +    (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  1.2219 +    (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  1.2220 +    (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  1.2221 +    (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
  1.2222 +    (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  1.2223 +*}
  1.2224 +
  1.2225 +lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
  1.2226 +    ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  1.2227 +    times_real_inst.times_real uminus_real_inst.uminus_real
  1.2228 +    zero_real_inst.zero_real
  1.2229 +
  1.2230  ML_file "Tools/SMT/smt_real.ML"
  1.2231  setup SMT_Real.setup
  1.2232