src/HOL/Real.thy
author hoelzl
Tue Nov 05 09:45:00 2013 +0100 (2013-11-05)
changeset 54262 326fd7103cb4
parent 54258 adfc759263ab
child 54263 c4159fe6fa46
permissions -rw-r--r--
generalize bdd_above/below_uminus to ordered_ab_group_add
     1 (*  Title:      HOL/Real.thy
     2     Author:     Jacques D. Fleuriot, University of Edinburgh, 1998
     3     Author:     Larry Paulson, University of Cambridge
     4     Author:     Jeremy Avigad, Carnegie Mellon University
     5     Author:     Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
     6     Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
     7     Construction of Cauchy Reals by Brian Huffman, 2010
     8 *)
     9 
    10 header {* Development of the Reals using Cauchy Sequences *}
    11 
    12 theory Real
    13 imports Rat Conditionally_Complete_Lattices
    14 begin
    15 
    16 text {*
    17   This theory contains a formalization of the real numbers as
    18   equivalence classes of Cauchy sequences of rationals.  See
    19   @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative
    20   construction using Dedekind cuts.
    21 *}
    22 
    23 subsection {* Preliminary lemmas *}
    24 
    25 lemma add_diff_add:
    26   fixes a b c d :: "'a::ab_group_add"
    27   shows "(a + c) - (b + d) = (a - b) + (c - d)"
    28   by simp
    29 
    30 lemma minus_diff_minus:
    31   fixes a b :: "'a::ab_group_add"
    32   shows "- a - - b = - (a - b)"
    33   by simp
    34 
    35 lemma mult_diff_mult:
    36   fixes x y a b :: "'a::ring"
    37   shows "(x * y - a * b) = x * (y - b) + (x - a) * b"
    38   by (simp add: algebra_simps)
    39 
    40 lemma inverse_diff_inverse:
    41   fixes a b :: "'a::division_ring"
    42   assumes "a \<noteq> 0" and "b \<noteq> 0"
    43   shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
    44   using assms by (simp add: algebra_simps)
    45 
    46 lemma obtain_pos_sum:
    47   fixes r :: rat assumes r: "0 < r"
    48   obtains s t where "0 < s" and "0 < t" and "r = s + t"
    49 proof
    50     from r show "0 < r/2" by simp
    51     from r show "0 < r/2" by simp
    52     show "r = r/2 + r/2" by simp
    53 qed
    54 
    55 subsection {* Sequences that converge to zero *}
    56 
    57 definition
    58   vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    59 where
    60   "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
    61 
    62 lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
    63   unfolding vanishes_def by simp
    64 
    65 lemma vanishesD: "\<lbrakk>vanishes X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
    66   unfolding vanishes_def by simp
    67 
    68 lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
    69   unfolding vanishes_def
    70   apply (cases "c = 0", auto)
    71   apply (rule exI [where x="\<bar>c\<bar>"], auto)
    72   done
    73 
    74 lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
    75   unfolding vanishes_def by simp
    76 
    77 lemma vanishes_add:
    78   assumes X: "vanishes X" and Y: "vanishes Y"
    79   shows "vanishes (\<lambda>n. X n + Y n)"
    80 proof (rule vanishesI)
    81   fix r :: rat assume "0 < r"
    82   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
    83     by (rule obtain_pos_sum)
    84   obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
    85     using vanishesD [OF X s] ..
    86   obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
    87     using vanishesD [OF Y t] ..
    88   have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
    89   proof (clarsimp)
    90     fix n assume n: "i \<le> n" "j \<le> n"
    91     have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>" by (rule abs_triangle_ineq)
    92     also have "\<dots> < s + t" by (simp add: add_strict_mono i j n)
    93     finally show "\<bar>X n + Y n\<bar> < r" unfolding r .
    94   qed
    95   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
    96 qed
    97 
    98 lemma vanishes_diff:
    99   assumes X: "vanishes X" and Y: "vanishes Y"
   100   shows "vanishes (\<lambda>n. X n - Y n)"
   101   unfolding diff_conv_add_uminus by (intro vanishes_add vanishes_minus X Y)
   102 
   103 lemma vanishes_mult_bounded:
   104   assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
   105   assumes Y: "vanishes (\<lambda>n. Y n)"
   106   shows "vanishes (\<lambda>n. X n * Y n)"
   107 proof (rule vanishesI)
   108   fix r :: rat assume r: "0 < r"
   109   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   110     using X by fast
   111   obtain b where b: "0 < b" "r = a * b"
   112   proof
   113     show "0 < r / a" using r a by (simp add: divide_pos_pos)
   114     show "r = a * (r / a)" using a by simp
   115   qed
   116   obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
   117     using vanishesD [OF Y b(1)] ..
   118   have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   119     by (simp add: b(2) abs_mult mult_strict_mono' a k)
   120   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   121 qed
   122 
   123 subsection {* Cauchy sequences *}
   124 
   125 definition
   126   cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   127 where
   128   "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   129 
   130 lemma cauchyI:
   131   "(\<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"
   132   unfolding cauchy_def by simp
   133 
   134 lemma cauchyD:
   135   "\<lbrakk>cauchy X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
   136   unfolding cauchy_def by simp
   137 
   138 lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
   139   unfolding cauchy_def by simp
   140 
   141 lemma cauchy_add [simp]:
   142   assumes X: "cauchy X" and Y: "cauchy Y"
   143   shows "cauchy (\<lambda>n. X n + Y n)"
   144 proof (rule cauchyI)
   145   fix r :: rat assume "0 < r"
   146   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   147     by (rule obtain_pos_sum)
   148   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   149     using cauchyD [OF X s] ..
   150   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   151     using cauchyD [OF Y t] ..
   152   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
   153   proof (clarsimp)
   154     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   155     have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
   156       unfolding add_diff_add by (rule abs_triangle_ineq)
   157     also have "\<dots> < s + t"
   158       by (rule add_strict_mono, simp_all add: i j *)
   159     finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" unfolding r .
   160   qed
   161   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
   162 qed
   163 
   164 lemma cauchy_minus [simp]:
   165   assumes X: "cauchy X"
   166   shows "cauchy (\<lambda>n. - X n)"
   167 using assms unfolding cauchy_def
   168 unfolding minus_diff_minus abs_minus_cancel .
   169 
   170 lemma cauchy_diff [simp]:
   171   assumes X: "cauchy X" and Y: "cauchy Y"
   172   shows "cauchy (\<lambda>n. X n - Y n)"
   173   using assms unfolding diff_conv_add_uminus by (simp del: add_uminus_conv_diff)
   174 
   175 lemma cauchy_imp_bounded:
   176   assumes "cauchy X" shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   177 proof -
   178   obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
   179     using cauchyD [OF assms zero_less_one] ..
   180   show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   181   proof (intro exI conjI allI)
   182     have "0 \<le> \<bar>X 0\<bar>" by simp
   183     also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
   184     finally have "0 \<le> Max (abs ` X ` {..k})" .
   185     thus "0 < Max (abs ` X ` {..k}) + 1" by simp
   186   next
   187     fix n :: nat
   188     show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
   189     proof (rule linorder_le_cases)
   190       assume "n \<le> k"
   191       hence "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
   192       thus "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
   193     next
   194       assume "k \<le> n"
   195       have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   196       also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   197         by (rule abs_triangle_ineq)
   198       also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   199         by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
   200       finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   201     qed
   202   qed
   203 qed
   204 
   205 lemma cauchy_mult [simp]:
   206   assumes X: "cauchy X" and Y: "cauchy Y"
   207   shows "cauchy (\<lambda>n. X n * Y n)"
   208 proof (rule cauchyI)
   209   fix r :: rat assume "0 < r"
   210   then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
   211     by (rule obtain_pos_sum)
   212   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   213     using cauchy_imp_bounded [OF X] by fast
   214   obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
   215     using cauchy_imp_bounded [OF Y] by fast
   216   obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
   217   proof
   218     show "0 < v/b" using v b(1) by (rule divide_pos_pos)
   219     show "0 < u/a" using u a(1) by (rule divide_pos_pos)
   220     show "r = a * (u/a) + (v/b) * b"
   221       using a(1) b(1) `r = u + v` by simp
   222   qed
   223   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   224     using cauchyD [OF X s] ..
   225   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   226     using cauchyD [OF Y t] ..
   227   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
   228   proof (clarsimp)
   229     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   230     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>"
   231       unfolding mult_diff_mult ..
   232     also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
   233       by (rule abs_triangle_ineq)
   234     also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
   235       unfolding abs_mult ..
   236     also have "\<dots> < a * t + s * b"
   237       by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
   238     finally show "\<bar>X m * Y m - X n * Y n\<bar> < r" unfolding r .
   239   qed
   240   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
   241 qed
   242 
   243 lemma cauchy_not_vanishes_cases:
   244   assumes X: "cauchy X"
   245   assumes nz: "\<not> vanishes X"
   246   shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
   247 proof -
   248   obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
   249     using nz unfolding vanishes_def by (auto simp add: not_less)
   250   obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
   251     using `0 < r` by (rule obtain_pos_sum)
   252   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   253     using cauchyD [OF X s] ..
   254   obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
   255     using r by fast
   256   have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
   257     using i `i \<le> k` by auto
   258   have "X k \<le> - r \<or> r \<le> X k"
   259     using `r \<le> \<bar>X k\<bar>` by auto
   260   hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   261     unfolding `r = s + t` using k by auto
   262   hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
   263   thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   264     using t by auto
   265 qed
   266 
   267 lemma cauchy_not_vanishes:
   268   assumes X: "cauchy X"
   269   assumes nz: "\<not> vanishes X"
   270   shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
   271 using cauchy_not_vanishes_cases [OF assms]
   272 by clarify (rule exI, erule conjI, rule_tac x=k in exI, auto)
   273 
   274 lemma cauchy_inverse [simp]:
   275   assumes X: "cauchy X"
   276   assumes nz: "\<not> vanishes X"
   277   shows "cauchy (\<lambda>n. inverse (X n))"
   278 proof (rule cauchyI)
   279   fix r :: rat assume "0 < r"
   280   obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   281     using cauchy_not_vanishes [OF X nz] by fast
   282   from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   283   obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   284   proof
   285     show "0 < b * r * b"
   286       by (simp add: `0 < r` b mult_pos_pos)
   287     show "r = inverse b * (b * r * b) * inverse b"
   288       using b by simp
   289   qed
   290   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   291     using cauchyD [OF X s] ..
   292   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
   293   proof (clarsimp)
   294     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   295     have "\<bar>inverse (X m) - inverse (X n)\<bar> =
   296           inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
   297       by (simp add: inverse_diff_inverse nz * abs_mult)
   298     also have "\<dots> < inverse b * s * inverse b"
   299       by (simp add: mult_strict_mono less_imp_inverse_less
   300                     mult_pos_pos i j b * s)
   301     finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" unfolding r .
   302   qed
   303   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
   304 qed
   305 
   306 lemma vanishes_diff_inverse:
   307   assumes X: "cauchy X" "\<not> vanishes X"
   308   assumes Y: "cauchy Y" "\<not> vanishes Y"
   309   assumes XY: "vanishes (\<lambda>n. X n - Y n)"
   310   shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
   311 proof (rule vanishesI)
   312   fix r :: rat assume r: "0 < r"
   313   obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
   314     using cauchy_not_vanishes [OF X] by fast
   315   obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
   316     using cauchy_not_vanishes [OF Y] by fast
   317   obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
   318   proof
   319     show "0 < a * r * b"
   320       using a r b by (simp add: mult_pos_pos)
   321     show "inverse a * (a * r * b) * inverse b = r"
   322       using a r b by simp
   323   qed
   324   obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
   325     using vanishesD [OF XY s] ..
   326   have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
   327   proof (clarsimp)
   328     fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
   329     have "X n \<noteq> 0" and "Y n \<noteq> 0"
   330       using i j a b n by auto
   331     hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
   332         inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
   333       by (simp add: inverse_diff_inverse abs_mult)
   334     also have "\<dots> < inverse a * s * inverse b"
   335       apply (intro mult_strict_mono' less_imp_inverse_less)
   336       apply (simp_all add: a b i j k n mult_nonneg_nonneg)
   337       done
   338     also note `inverse a * s * inverse b = r`
   339     finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
   340   qed
   341   thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
   342 qed
   343 
   344 subsection {* Equivalence relation on Cauchy sequences *}
   345 
   346 definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
   347   where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
   348 
   349 lemma realrelI [intro?]:
   350   assumes "cauchy X" and "cauchy Y" and "vanishes (\<lambda>n. X n - Y n)"
   351   shows "realrel X Y"
   352   using assms unfolding realrel_def by simp
   353 
   354 lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
   355   unfolding realrel_def by simp
   356 
   357 lemma symp_realrel: "symp realrel"
   358   unfolding realrel_def
   359   by (rule sympI, clarify, drule vanishes_minus, simp)
   360 
   361 lemma transp_realrel: "transp realrel"
   362   unfolding realrel_def
   363   apply (rule transpI, clarify)
   364   apply (drule (1) vanishes_add)
   365   apply (simp add: algebra_simps)
   366   done
   367 
   368 lemma part_equivp_realrel: "part_equivp realrel"
   369   by (fast intro: part_equivpI symp_realrel transp_realrel
   370     realrel_refl cauchy_const)
   371 
   372 subsection {* The field of real numbers *}
   373 
   374 quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   375   morphisms rep_real Real
   376   by (rule part_equivp_realrel)
   377 
   378 lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
   379   unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
   380 
   381 lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
   382   assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)" shows "P x"
   383 proof (induct x)
   384   case (1 X)
   385   hence "cauchy X" by (simp add: realrel_def)
   386   thus "P (Real X)" by (rule assms)
   387 qed
   388 
   389 lemma eq_Real:
   390   "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
   391   using real.rel_eq_transfer
   392   unfolding real.pcr_cr_eq cr_real_def fun_rel_def realrel_def by simp
   393 
   394 lemma Domainp_pcr_real [transfer_domain_rule]: "Domainp pcr_real = cauchy"
   395 by (simp add: real.domain_eq realrel_def)
   396 
   397 instantiation real :: field_inverse_zero
   398 begin
   399 
   400 lift_definition zero_real :: "real" is "\<lambda>n. 0"
   401   by (simp add: realrel_refl)
   402 
   403 lift_definition one_real :: "real" is "\<lambda>n. 1"
   404   by (simp add: realrel_refl)
   405 
   406 lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
   407   unfolding realrel_def add_diff_add
   408   by (simp only: cauchy_add vanishes_add simp_thms)
   409 
   410 lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
   411   unfolding realrel_def minus_diff_minus
   412   by (simp only: cauchy_minus vanishes_minus simp_thms)
   413 
   414 lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
   415   unfolding realrel_def mult_diff_mult
   416   by (subst (4) mult_commute, simp only: cauchy_mult vanishes_add
   417     vanishes_mult_bounded cauchy_imp_bounded simp_thms)
   418 
   419 lift_definition inverse_real :: "real \<Rightarrow> real"
   420   is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
   421 proof -
   422   fix X Y assume "realrel X Y"
   423   hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   424     unfolding realrel_def by simp_all
   425   have "vanishes X \<longleftrightarrow> vanishes Y"
   426   proof
   427     assume "vanishes X"
   428     from vanishes_diff [OF this XY] show "vanishes Y" by simp
   429   next
   430     assume "vanishes Y"
   431     from vanishes_add [OF this XY] show "vanishes X" by simp
   432   qed
   433   thus "?thesis X Y"
   434     unfolding realrel_def
   435     by (simp add: vanishes_diff_inverse X Y XY)
   436 qed
   437 
   438 definition
   439   "x - y = (x::real) + - y"
   440 
   441 definition
   442   "x / y = (x::real) * inverse y"
   443 
   444 lemma add_Real:
   445   assumes X: "cauchy X" and Y: "cauchy Y"
   446   shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
   447   using assms plus_real.transfer
   448   unfolding cr_real_eq fun_rel_def by simp
   449 
   450 lemma minus_Real:
   451   assumes X: "cauchy X"
   452   shows "- Real X = Real (\<lambda>n. - X n)"
   453   using assms uminus_real.transfer
   454   unfolding cr_real_eq fun_rel_def by simp
   455 
   456 lemma diff_Real:
   457   assumes X: "cauchy X" and Y: "cauchy Y"
   458   shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
   459   unfolding minus_real_def
   460   by (simp add: minus_Real add_Real X Y)
   461 
   462 lemma mult_Real:
   463   assumes X: "cauchy X" and Y: "cauchy Y"
   464   shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
   465   using assms times_real.transfer
   466   unfolding cr_real_eq fun_rel_def by simp
   467 
   468 lemma inverse_Real:
   469   assumes X: "cauchy X"
   470   shows "inverse (Real X) =
   471     (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   472   using assms inverse_real.transfer zero_real.transfer
   473   unfolding cr_real_eq fun_rel_def by (simp split: split_if_asm, metis)
   474 
   475 instance proof
   476   fix a b c :: real
   477   show "a + b = b + a"
   478     by transfer (simp add: add_ac realrel_def)
   479   show "(a + b) + c = a + (b + c)"
   480     by transfer (simp add: add_ac realrel_def)
   481   show "0 + a = a"
   482     by transfer (simp add: realrel_def)
   483   show "- a + a = 0"
   484     by transfer (simp add: realrel_def)
   485   show "a - b = a + - b"
   486     by (rule minus_real_def)
   487   show "(a * b) * c = a * (b * c)"
   488     by transfer (simp add: mult_ac realrel_def)
   489   show "a * b = b * a"
   490     by transfer (simp add: mult_ac realrel_def)
   491   show "1 * a = a"
   492     by transfer (simp add: mult_ac realrel_def)
   493   show "(a + b) * c = a * c + b * c"
   494     by transfer (simp add: distrib_right realrel_def)
   495   show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
   496     by transfer (simp add: realrel_def)
   497   show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   498     apply transfer
   499     apply (simp add: realrel_def)
   500     apply (rule vanishesI)
   501     apply (frule (1) cauchy_not_vanishes, clarify)
   502     apply (rule_tac x=k in exI, clarify)
   503     apply (drule_tac x=n in spec, simp)
   504     done
   505   show "a / b = a * inverse b"
   506     by (rule divide_real_def)
   507   show "inverse (0::real) = 0"
   508     by transfer (simp add: realrel_def)
   509 qed
   510 
   511 end
   512 
   513 subsection {* Positive reals *}
   514 
   515 lift_definition positive :: "real \<Rightarrow> bool"
   516   is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   517 proof -
   518   { fix X Y
   519     assume "realrel X Y"
   520     hence XY: "vanishes (\<lambda>n. X n - Y n)"
   521       unfolding realrel_def by simp_all
   522     assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   523     then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
   524       by fast
   525     obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   526       using `0 < r` by (rule obtain_pos_sum)
   527     obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
   528       using vanishesD [OF XY s] ..
   529     have "\<forall>n\<ge>max i j. t < Y n"
   530     proof (clarsimp)
   531       fix n assume n: "i \<le> n" "j \<le> n"
   532       have "\<bar>X n - Y n\<bar> < s" and "r < X n"
   533         using i j n by simp_all
   534       thus "t < Y n" unfolding r by simp
   535     qed
   536     hence "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
   537   } note 1 = this
   538   fix X Y assume "realrel X Y"
   539   hence "realrel X Y" and "realrel Y X"
   540     using symp_realrel unfolding symp_def by auto
   541   thus "?thesis X Y"
   542     by (safe elim!: 1)
   543 qed
   544 
   545 lemma positive_Real:
   546   assumes X: "cauchy X"
   547   shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   548   using assms positive.transfer
   549   unfolding cr_real_eq fun_rel_def by simp
   550 
   551 lemma positive_zero: "\<not> positive 0"
   552   by transfer auto
   553 
   554 lemma positive_add:
   555   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
   556 apply transfer
   557 apply (clarify, rename_tac a b i j)
   558 apply (rule_tac x="a + b" in exI, simp)
   559 apply (rule_tac x="max i j" in exI, clarsimp)
   560 apply (simp add: add_strict_mono)
   561 done
   562 
   563 lemma positive_mult:
   564   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
   565 apply transfer
   566 apply (clarify, rename_tac a b i j)
   567 apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
   568 apply (rule_tac x="max i j" in exI, clarsimp)
   569 apply (rule mult_strict_mono, auto)
   570 done
   571 
   572 lemma positive_minus:
   573   "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
   574 apply transfer
   575 apply (simp add: realrel_def)
   576 apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
   577 done
   578 
   579 instantiation real :: linordered_field_inverse_zero
   580 begin
   581 
   582 definition
   583   "x < y \<longleftrightarrow> positive (y - x)"
   584 
   585 definition
   586   "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
   587 
   588 definition
   589   "abs (a::real) = (if a < 0 then - a else a)"
   590 
   591 definition
   592   "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   593 
   594 instance proof
   595   fix a b c :: real
   596   show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   597     by (rule abs_real_def)
   598   show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   599     unfolding less_eq_real_def less_real_def
   600     by (auto, drule (1) positive_add, simp_all add: positive_zero)
   601   show "a \<le> a"
   602     unfolding less_eq_real_def by simp
   603   show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   604     unfolding less_eq_real_def less_real_def
   605     by (auto, drule (1) positive_add, simp add: algebra_simps)
   606   show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   607     unfolding less_eq_real_def less_real_def
   608     by (auto, drule (1) positive_add, simp add: positive_zero)
   609   show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   610     unfolding less_eq_real_def less_real_def by auto
   611     (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
   612     (* Should produce c + b - (c + a) \<equiv> b - a *)
   613   show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   614     by (rule sgn_real_def)
   615   show "a \<le> b \<or> b \<le> a"
   616     unfolding less_eq_real_def less_real_def
   617     by (auto dest!: positive_minus)
   618   show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   619     unfolding less_real_def
   620     by (drule (1) positive_mult, simp add: algebra_simps)
   621 qed
   622 
   623 end
   624 
   625 instantiation real :: distrib_lattice
   626 begin
   627 
   628 definition
   629   "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
   630 
   631 definition
   632   "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
   633 
   634 instance proof
   635 qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
   636 
   637 end
   638 
   639 lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   640 apply (induct x)
   641 apply (simp add: zero_real_def)
   642 apply (simp add: one_real_def add_Real)
   643 done
   644 
   645 lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
   646 apply (cases x rule: int_diff_cases)
   647 apply (simp add: of_nat_Real diff_Real)
   648 done
   649 
   650 lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
   651 apply (induct x)
   652 apply (simp add: Fract_of_int_quotient of_rat_divide)
   653 apply (simp add: of_int_Real divide_inverse)
   654 apply (simp add: inverse_Real mult_Real)
   655 done
   656 
   657 instance real :: archimedean_field
   658 proof
   659   fix x :: real
   660   show "\<exists>z. x \<le> of_int z"
   661     apply (induct x)
   662     apply (frule cauchy_imp_bounded, clarify)
   663     apply (rule_tac x="ceiling b + 1" in exI)
   664     apply (rule less_imp_le)
   665     apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
   666     apply (rule_tac x=1 in exI, simp add: algebra_simps)
   667     apply (rule_tac x=0 in exI, clarsimp)
   668     apply (rule le_less_trans [OF abs_ge_self])
   669     apply (rule less_le_trans [OF _ le_of_int_ceiling])
   670     apply simp
   671     done
   672 qed
   673 
   674 instantiation real :: floor_ceiling
   675 begin
   676 
   677 definition [code del]:
   678   "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
   679 
   680 instance proof
   681   fix x :: real
   682   show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   683     unfolding floor_real_def using floor_exists1 by (rule theI')
   684 qed
   685 
   686 end
   687 
   688 subsection {* Completeness *}
   689 
   690 lemma not_positive_Real:
   691   assumes X: "cauchy X"
   692   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   693 unfolding positive_Real [OF X]
   694 apply (auto, unfold not_less)
   695 apply (erule obtain_pos_sum)
   696 apply (drule_tac x=s in spec, simp)
   697 apply (drule_tac r=t in cauchyD [OF X], clarify)
   698 apply (drule_tac x=k in spec, clarsimp)
   699 apply (rule_tac x=n in exI, clarify, rename_tac m)
   700 apply (drule_tac x=m in spec, simp)
   701 apply (drule_tac x=n in spec, simp)
   702 apply (drule spec, drule (1) mp, clarify, rename_tac i)
   703 apply (rule_tac x="max i k" in exI, simp)
   704 done
   705 
   706 lemma le_Real:
   707   assumes X: "cauchy X" and Y: "cauchy Y"
   708   shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   709 unfolding not_less [symmetric, where 'a=real] less_real_def
   710 apply (simp add: diff_Real not_positive_Real X Y)
   711 apply (simp add: diff_le_eq add_ac)
   712 done
   713 
   714 lemma le_RealI:
   715   assumes Y: "cauchy Y"
   716   shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   717 proof (induct x)
   718   fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   719   hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   720     by (simp add: of_rat_Real le_Real)
   721   {
   722     fix r :: rat assume "0 < r"
   723     then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   724       by (rule obtain_pos_sum)
   725     obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   726       using cauchyD [OF Y s] ..
   727     obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   728       using le [OF t] ..
   729     have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   730     proof (clarsimp)
   731       fix n assume n: "i \<le> n" "j \<le> n"
   732       have "X n \<le> Y i + t" using n j by simp
   733       moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
   734       ultimately show "X n \<le> Y n + r" unfolding r by simp
   735     qed
   736     hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
   737   }
   738   thus "Real X \<le> Real Y"
   739     by (simp add: of_rat_Real le_Real X Y)
   740 qed
   741 
   742 lemma Real_leI:
   743   assumes X: "cauchy X"
   744   assumes le: "\<forall>n. of_rat (X n) \<le> y"
   745   shows "Real X \<le> y"
   746 proof -
   747   have "- y \<le> - Real X"
   748     by (simp add: minus_Real X le_RealI of_rat_minus le)
   749   thus ?thesis by simp
   750 qed
   751 
   752 lemma less_RealD:
   753   assumes Y: "cauchy Y"
   754   shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   755 by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   756 
   757 lemma of_nat_less_two_power:
   758   "of_nat n < (2::'a::linordered_idom) ^ n"
   759 apply (induct n)
   760 apply simp
   761 apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   762 apply (drule (1) add_le_less_mono, simp)
   763 apply simp
   764 done
   765 
   766 lemma complete_real:
   767   fixes S :: "real set"
   768   assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   769   shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   770 proof -
   771   obtain x where x: "x \<in> S" using assms(1) ..
   772   obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   773 
   774   def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
   775   obtain a where a: "\<not> P a"
   776   proof
   777     have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
   778     also have "x - 1 < x" by simp
   779     finally have "of_int (floor (x - 1)) < x" .
   780     hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
   781     then show "\<not> P (of_int (floor (x - 1)))"
   782       unfolding P_def of_rat_of_int_eq using x by fast
   783   qed
   784   obtain b where b: "P b"
   785   proof
   786     show "P (of_int (ceiling z))"
   787     unfolding P_def of_rat_of_int_eq
   788     proof
   789       fix y assume "y \<in> S"
   790       hence "y \<le> z" using z by simp
   791       also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
   792       finally show "y \<le> of_int (ceiling z)" .
   793     qed
   794   qed
   795 
   796   def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
   797   def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
   798   def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
   799   def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
   800   def C \<equiv> "\<lambda>n. avg (A n) (B n)"
   801   have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   802   have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   803   have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   804     unfolding A_def B_def C_def bisect_def split_def by simp
   805   have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   806     unfolding A_def B_def C_def bisect_def split_def by simp
   807 
   808   have width: "\<And>n. B n - A n = (b - a) / 2^n"
   809     apply (simp add: eq_divide_eq)
   810     apply (induct_tac n, simp)
   811     apply (simp add: C_def avg_def algebra_simps)
   812     done
   813 
   814   have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
   815     apply (simp add: divide_less_eq)
   816     apply (subst mult_commute)
   817     apply (frule_tac y=y in ex_less_of_nat_mult)
   818     apply clarify
   819     apply (rule_tac x=n in exI)
   820     apply (erule less_trans)
   821     apply (rule mult_strict_right_mono)
   822     apply (rule le_less_trans [OF _ of_nat_less_two_power])
   823     apply simp
   824     apply assumption
   825     done
   826 
   827   have PA: "\<And>n. \<not> P (A n)"
   828     by (induct_tac n, simp_all add: a)
   829   have PB: "\<And>n. P (B n)"
   830     by (induct_tac n, simp_all add: b)
   831   have ab: "a < b"
   832     using a b unfolding P_def
   833     apply (clarsimp simp add: not_le)
   834     apply (drule (1) bspec)
   835     apply (drule (1) less_le_trans)
   836     apply (simp add: of_rat_less)
   837     done
   838   have AB: "\<And>n. A n < B n"
   839     by (induct_tac n, simp add: ab, simp add: C_def avg_def)
   840   have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   841     apply (auto simp add: le_less [where 'a=nat])
   842     apply (erule less_Suc_induct)
   843     apply (clarsimp simp add: C_def avg_def)
   844     apply (simp add: add_divide_distrib [symmetric])
   845     apply (rule AB [THEN less_imp_le])
   846     apply simp
   847     done
   848   have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   849     apply (auto simp add: le_less [where 'a=nat])
   850     apply (erule less_Suc_induct)
   851     apply (clarsimp simp add: C_def avg_def)
   852     apply (simp add: add_divide_distrib [symmetric])
   853     apply (rule AB [THEN less_imp_le])
   854     apply simp
   855     done
   856   have cauchy_lemma:
   857     "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   858     apply (rule cauchyI)
   859     apply (drule twos [where y="b - a"])
   860     apply (erule exE)
   861     apply (rule_tac x=n in exI, clarify, rename_tac i j)
   862     apply (rule_tac y="B n - A n" in le_less_trans) defer
   863     apply (simp add: width)
   864     apply (drule_tac x=n in spec)
   865     apply (frule_tac x=i in spec, drule (1) mp)
   866     apply (frule_tac x=j in spec, drule (1) mp)
   867     apply (frule A_mono, drule B_mono)
   868     apply (frule A_mono, drule B_mono)
   869     apply arith
   870     done
   871   have "cauchy A"
   872     apply (rule cauchy_lemma [rule_format])
   873     apply (simp add: A_mono)
   874     apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   875     done
   876   have "cauchy B"
   877     apply (rule cauchy_lemma [rule_format])
   878     apply (simp add: B_mono)
   879     apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
   880     done
   881   have 1: "\<forall>x\<in>S. x \<le> Real B"
   882   proof
   883     fix x assume "x \<in> S"
   884     then show "x \<le> Real B"
   885       using PB [unfolded P_def] `cauchy B`
   886       by (simp add: le_RealI)
   887   qed
   888   have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
   889     apply clarify
   890     apply (erule contrapos_pp)
   891     apply (simp add: not_le)
   892     apply (drule less_RealD [OF `cauchy A`], clarify)
   893     apply (subgoal_tac "\<not> P (A n)")
   894     apply (simp add: P_def not_le, clarify)
   895     apply (erule rev_bexI)
   896     apply (erule (1) less_trans)
   897     apply (simp add: PA)
   898     done
   899   have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
   900   proof (rule vanishesI)
   901     fix r :: rat assume "0 < r"
   902     then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
   903       using twos by fast
   904     have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
   905     proof (clarify)
   906       fix n assume n: "k \<le> n"
   907       have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
   908         by simp
   909       also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
   910         using n by (simp add: divide_left_mono mult_pos_pos)
   911       also note k
   912       finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
   913     qed
   914     thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
   915   qed
   916   hence 3: "Real B = Real A"
   917     by (simp add: eq_Real `cauchy A` `cauchy B` width)
   918   show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   919     using 1 2 3 by (rule_tac x="Real B" in exI, simp)
   920 qed
   921 
   922 instantiation real :: linear_continuum
   923 begin
   924 
   925 subsection{*Supremum of a set of reals*}
   926 
   927 definition
   928   Sup_real_def: "Sup X \<equiv> LEAST z::real. \<forall>x\<in>X. x\<le>z"
   929 
   930 definition
   931   Inf_real_def: "Inf (X::real set) \<equiv> - Sup (uminus ` X)"
   932 
   933 instance
   934 proof
   935   { fix x :: real and X :: "real set"
   936     assume x: "x \<in> X" "bdd_above X"
   937     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"
   938       using complete_real[of X] unfolding bdd_above_def by blast
   939     then show "x \<le> Sup X"
   940       unfolding Sup_real_def by (rule LeastI2_order) (auto simp: x) }
   941   note Sup_upper = this
   942 
   943   { fix z :: real and X :: "real set"
   944     assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
   945     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"
   946       using complete_real[of X] by blast
   947     then have "Sup X = s"
   948       unfolding Sup_real_def by (best intro: Least_equality)  
   949     also from s z have "... \<le> z"
   950       by blast
   951     finally show "Sup X \<le> z" . }
   952   note Sup_least = this
   953 
   954   { fix x z :: real and X :: "real set"
   955     assume x: "x \<in> X" and [simp]: "bdd_below X"
   956     have "-x \<le> Sup (uminus ` X)"
   957       by (rule Sup_upper) (auto simp add: image_iff x)
   958     then show "Inf X \<le> x" 
   959       by (auto simp add: Inf_real_def) }
   960 
   961   { fix z :: real and X :: "real set"
   962     assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
   963     have "Sup (uminus ` X) \<le> -z"
   964       using x z by (force intro: Sup_least)
   965     then show "z \<le> Inf X" 
   966         by (auto simp add: Inf_real_def) }
   967 
   968   show "\<exists>a b::real. a \<noteq> b"
   969     using zero_neq_one by blast
   970 qed
   971 end
   972 
   973 text {*
   974   \medskip Completeness properties using @{text "isUb"}, @{text "isLub"}:
   975 *}
   976 
   977 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"
   978   by (intro exI[of _ "Sup S"] isLub_cSup) (auto simp: setle_def isUb_def intro!: cSup_upper)
   979 
   980 
   981 subsection {* Hiding implementation details *}
   982 
   983 hide_const (open) vanishes cauchy positive Real
   984 
   985 declare Real_induct [induct del]
   986 declare Abs_real_induct [induct del]
   987 declare Abs_real_cases [cases del]
   988 
   989 lifting_update real.lifting
   990 lifting_forget real.lifting
   991   
   992 subsection{*More Lemmas*}
   993 
   994 text {* BH: These lemmas should not be necessary; they should be
   995 covered by existing simp rules and simplification procedures. *}
   996 
   997 lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
   998 by simp (* redundant with mult_cancel_left *)
   999 
  1000 lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
  1001 by simp (* redundant with mult_cancel_right *)
  1002 
  1003 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
  1004 by simp (* solved by linordered_ring_less_cancel_factor simproc *)
  1005 
  1006 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
  1007 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1008 
  1009 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
  1010 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1011 
  1012 
  1013 subsection {* Embedding numbers into the Reals *}
  1014 
  1015 abbreviation
  1016   real_of_nat :: "nat \<Rightarrow> real"
  1017 where
  1018   "real_of_nat \<equiv> of_nat"
  1019 
  1020 abbreviation
  1021   real_of_int :: "int \<Rightarrow> real"
  1022 where
  1023   "real_of_int \<equiv> of_int"
  1024 
  1025 abbreviation
  1026   real_of_rat :: "rat \<Rightarrow> real"
  1027 where
  1028   "real_of_rat \<equiv> of_rat"
  1029 
  1030 consts
  1031   (*overloaded constant for injecting other types into "real"*)
  1032   real :: "'a => real"
  1033 
  1034 defs (overloaded)
  1035   real_of_nat_def [code_unfold]: "real == real_of_nat"
  1036   real_of_int_def [code_unfold]: "real == real_of_int"
  1037 
  1038 declare [[coercion_enabled]]
  1039 declare [[coercion "real::nat\<Rightarrow>real"]]
  1040 declare [[coercion "real::int\<Rightarrow>real"]]
  1041 declare [[coercion "int"]]
  1042 
  1043 declare [[coercion_map map]]
  1044 declare [[coercion_map "% f g h x. g (h (f x))"]]
  1045 declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
  1046 
  1047 lemma real_eq_of_nat: "real = of_nat"
  1048   unfolding real_of_nat_def ..
  1049 
  1050 lemma real_eq_of_int: "real = of_int"
  1051   unfolding real_of_int_def ..
  1052 
  1053 lemma real_of_int_zero [simp]: "real (0::int) = 0"  
  1054 by (simp add: real_of_int_def) 
  1055 
  1056 lemma real_of_one [simp]: "real (1::int) = (1::real)"
  1057 by (simp add: real_of_int_def) 
  1058 
  1059 lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
  1060 by (simp add: real_of_int_def) 
  1061 
  1062 lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
  1063 by (simp add: real_of_int_def) 
  1064 
  1065 lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
  1066 by (simp add: real_of_int_def) 
  1067 
  1068 lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
  1069 by (simp add: real_of_int_def) 
  1070 
  1071 lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
  1072 by (simp add: real_of_int_def of_int_power)
  1073 
  1074 lemmas power_real_of_int = real_of_int_power [symmetric]
  1075 
  1076 lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
  1077   apply (subst real_eq_of_int)+
  1078   apply (rule of_int_setsum)
  1079 done
  1080 
  1081 lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
  1082     (PROD x:A. real(f x))"
  1083   apply (subst real_eq_of_int)+
  1084   apply (rule of_int_setprod)
  1085 done
  1086 
  1087 lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
  1088 by (simp add: real_of_int_def) 
  1089 
  1090 lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
  1091 by (simp add: real_of_int_def) 
  1092 
  1093 lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
  1094 by (simp add: real_of_int_def) 
  1095 
  1096 lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
  1097 by (simp add: real_of_int_def) 
  1098 
  1099 lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
  1100 by (simp add: real_of_int_def) 
  1101 
  1102 lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
  1103 by (simp add: real_of_int_def) 
  1104 
  1105 lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
  1106 by (simp add: real_of_int_def)
  1107 
  1108 lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
  1109 by (simp add: real_of_int_def)
  1110 
  1111 lemma one_less_real_of_int_cancel_iff: "1 < real (i :: int) \<longleftrightarrow> 1 < i"
  1112   unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1113 
  1114 lemma one_le_real_of_int_cancel_iff: "1 \<le> real (i :: int) \<longleftrightarrow> 1 \<le> i"
  1115   unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1116 
  1117 lemma real_of_int_less_one_cancel_iff: "real (i :: int) < 1 \<longleftrightarrow> i < 1"
  1118   unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1119 
  1120 lemma real_of_int_le_one_cancel_iff: "real (i :: int) \<le> 1 \<longleftrightarrow> i \<le> 1"
  1121   unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1122 
  1123 lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
  1124 by (auto simp add: abs_if)
  1125 
  1126 lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
  1127   apply (subgoal_tac "real n + 1 = real (n + 1)")
  1128   apply (simp del: real_of_int_add)
  1129   apply auto
  1130 done
  1131 
  1132 lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
  1133   apply (subgoal_tac "real m + 1 = real (m + 1)")
  1134   apply (simp del: real_of_int_add)
  1135   apply simp
  1136 done
  1137 
  1138 lemma real_of_int_div_aux: "(real (x::int)) / (real d) = 
  1139     real (x div d) + (real (x mod d)) / (real d)"
  1140 proof -
  1141   have "x = (x div d) * d + x mod d"
  1142     by auto
  1143   then have "real x = real (x div d) * real d + real(x mod d)"
  1144     by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
  1145   then have "real x / real d = ... / real d"
  1146     by simp
  1147   then show ?thesis
  1148     by (auto simp add: add_divide_distrib algebra_simps)
  1149 qed
  1150 
  1151 lemma real_of_int_div: "(d :: int) dvd n ==>
  1152     real(n div d) = real n / real d"
  1153   apply (subst real_of_int_div_aux)
  1154   apply simp
  1155   apply (simp add: dvd_eq_mod_eq_0)
  1156 done
  1157 
  1158 lemma real_of_int_div2:
  1159   "0 <= real (n::int) / real (x) - real (n div x)"
  1160   apply (case_tac "x = 0")
  1161   apply simp
  1162   apply (case_tac "0 < x")
  1163   apply (simp add: algebra_simps)
  1164   apply (subst real_of_int_div_aux)
  1165   apply simp
  1166   apply (subst zero_le_divide_iff)
  1167   apply auto
  1168   apply (simp add: algebra_simps)
  1169   apply (subst real_of_int_div_aux)
  1170   apply simp
  1171   apply (subst zero_le_divide_iff)
  1172   apply auto
  1173 done
  1174 
  1175 lemma real_of_int_div3:
  1176   "real (n::int) / real (x) - real (n div x) <= 1"
  1177   apply (simp add: algebra_simps)
  1178   apply (subst real_of_int_div_aux)
  1179   apply (auto simp add: divide_le_eq intro: order_less_imp_le)
  1180 done
  1181 
  1182 lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
  1183 by (insert real_of_int_div2 [of n x], simp)
  1184 
  1185 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1186 unfolding real_of_int_def by (rule Ints_of_int)
  1187 
  1188 
  1189 subsection{*Embedding the Naturals into the Reals*}
  1190 
  1191 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1192 by (simp add: real_of_nat_def)
  1193 
  1194 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1195 by (simp add: real_of_nat_def)
  1196 
  1197 lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
  1198 by (simp add: real_of_nat_def)
  1199 
  1200 lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
  1201 by (simp add: real_of_nat_def)
  1202 
  1203 (*Not for addsimps: often the LHS is used to represent a positive natural*)
  1204 lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
  1205 by (simp add: real_of_nat_def)
  1206 
  1207 lemma real_of_nat_less_iff [iff]: 
  1208      "(real (n::nat) < real m) = (n < m)"
  1209 by (simp add: real_of_nat_def)
  1210 
  1211 lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
  1212 by (simp add: real_of_nat_def)
  1213 
  1214 lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
  1215 by (simp add: real_of_nat_def)
  1216 
  1217 lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
  1218 by (simp add: real_of_nat_def del: of_nat_Suc)
  1219 
  1220 lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
  1221 by (simp add: real_of_nat_def of_nat_mult)
  1222 
  1223 lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
  1224 by (simp add: real_of_nat_def of_nat_power)
  1225 
  1226 lemmas power_real_of_nat = real_of_nat_power [symmetric]
  1227 
  1228 lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
  1229     (SUM x:A. real(f x))"
  1230   apply (subst real_eq_of_nat)+
  1231   apply (rule of_nat_setsum)
  1232 done
  1233 
  1234 lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
  1235     (PROD x:A. real(f x))"
  1236   apply (subst real_eq_of_nat)+
  1237   apply (rule of_nat_setprod)
  1238 done
  1239 
  1240 lemma real_of_card: "real (card A) = setsum (%x.1) A"
  1241   apply (subst card_eq_setsum)
  1242   apply (subst real_of_nat_setsum)
  1243   apply simp
  1244 done
  1245 
  1246 lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
  1247 by (simp add: real_of_nat_def)
  1248 
  1249 lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
  1250 by (simp add: real_of_nat_def)
  1251 
  1252 lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
  1253 by (simp add: add: real_of_nat_def of_nat_diff)
  1254 
  1255 lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
  1256 by (auto simp: real_of_nat_def)
  1257 
  1258 lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
  1259 by (simp add: add: real_of_nat_def)
  1260 
  1261 lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
  1262 by (simp add: add: real_of_nat_def)
  1263 
  1264 lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
  1265   apply (subgoal_tac "real n + 1 = real (Suc n)")
  1266   apply simp
  1267   apply (auto simp add: real_of_nat_Suc)
  1268 done
  1269 
  1270 lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
  1271   apply (subgoal_tac "real m + 1 = real (Suc m)")
  1272   apply (simp add: less_Suc_eq_le)
  1273   apply (simp add: real_of_nat_Suc)
  1274 done
  1275 
  1276 lemma real_of_nat_div_aux: "(real (x::nat)) / (real d) = 
  1277     real (x div d) + (real (x mod d)) / (real d)"
  1278 proof -
  1279   have "x = (x div d) * d + x mod d"
  1280     by auto
  1281   then have "real x = real (x div d) * real d + real(x mod d)"
  1282     by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
  1283   then have "real x / real d = \<dots> / real d"
  1284     by simp
  1285   then show ?thesis
  1286     by (auto simp add: add_divide_distrib algebra_simps)
  1287 qed
  1288 
  1289 lemma real_of_nat_div: "(d :: nat) dvd n ==>
  1290     real(n div d) = real n / real d"
  1291   by (subst real_of_nat_div_aux)
  1292     (auto simp add: dvd_eq_mod_eq_0 [symmetric])
  1293 
  1294 lemma real_of_nat_div2:
  1295   "0 <= real (n::nat) / real (x) - real (n div x)"
  1296 apply (simp add: algebra_simps)
  1297 apply (subst real_of_nat_div_aux)
  1298 apply simp
  1299 apply (subst zero_le_divide_iff)
  1300 apply simp
  1301 done
  1302 
  1303 lemma real_of_nat_div3:
  1304   "real (n::nat) / real (x) - real (n div x) <= 1"
  1305 apply(case_tac "x = 0")
  1306 apply (simp)
  1307 apply (simp add: algebra_simps)
  1308 apply (subst real_of_nat_div_aux)
  1309 apply simp
  1310 done
  1311 
  1312 lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
  1313 by (insert real_of_nat_div2 [of n x], simp)
  1314 
  1315 lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
  1316 by (simp add: real_of_int_def real_of_nat_def)
  1317 
  1318 lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
  1319   apply (subgoal_tac "real(int(nat x)) = real(nat x)")
  1320   apply force
  1321   apply (simp only: real_of_int_of_nat_eq)
  1322 done
  1323 
  1324 lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
  1325 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1326 
  1327 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1328 unfolding real_of_nat_def by (rule Ints_of_nat)
  1329 
  1330 subsection {* The Archimedean Property of the Reals *}
  1331 
  1332 theorem reals_Archimedean:
  1333   assumes x_pos: "0 < x"
  1334   shows "\<exists>n. inverse (real (Suc n)) < x"
  1335   unfolding real_of_nat_def using x_pos
  1336   by (rule ex_inverse_of_nat_Suc_less)
  1337 
  1338 lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
  1339   unfolding real_of_nat_def by (rule ex_less_of_nat)
  1340 
  1341 lemma reals_Archimedean3:
  1342   assumes x_greater_zero: "0 < x"
  1343   shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
  1344   unfolding real_of_nat_def using `0 < x`
  1345   by (auto intro: ex_less_of_nat_mult)
  1346 
  1347 
  1348 subsection{* Rationals *}
  1349 
  1350 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
  1351 by (simp add: real_eq_of_nat)
  1352 
  1353 
  1354 lemma Rats_eq_int_div_int:
  1355   "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
  1356 proof
  1357   show "\<rat> \<subseteq> ?S"
  1358   proof
  1359     fix x::real assume "x : \<rat>"
  1360     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1361     have "of_rat r : ?S"
  1362       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1363     thus "x : ?S" using `x = of_rat r` by simp
  1364   qed
  1365 next
  1366   show "?S \<subseteq> \<rat>"
  1367   proof(auto simp:Rats_def)
  1368     fix i j :: int assume "j \<noteq> 0"
  1369     hence "real i / real j = of_rat(Fract i j)"
  1370       by (simp add:of_rat_rat real_eq_of_int)
  1371     thus "real i / real j \<in> range of_rat" by blast
  1372   qed
  1373 qed
  1374 
  1375 lemma Rats_eq_int_div_nat:
  1376   "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
  1377 proof(auto simp:Rats_eq_int_div_int)
  1378   fix i j::int assume "j \<noteq> 0"
  1379   show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
  1380   proof cases
  1381     assume "j>0"
  1382     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1383       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1384     thus ?thesis by blast
  1385   next
  1386     assume "~ j>0"
  1387     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1388       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1389     thus ?thesis by blast
  1390   qed
  1391 next
  1392   fix i::int and n::nat assume "0 < n"
  1393   hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
  1394   thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
  1395 qed
  1396 
  1397 lemma Rats_abs_nat_div_natE:
  1398   assumes "x \<in> \<rat>"
  1399   obtains m n :: nat
  1400   where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1401 proof -
  1402   from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
  1403     by(auto simp add: Rats_eq_int_div_nat)
  1404   hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
  1405   then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1406   let ?gcd = "gcd m n"
  1407   from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
  1408   let ?k = "m div ?gcd"
  1409   let ?l = "n div ?gcd"
  1410   let ?gcd' = "gcd ?k ?l"
  1411   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
  1412     by (rule dvd_mult_div_cancel)
  1413   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
  1414     by (rule dvd_mult_div_cancel)
  1415   from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
  1416   moreover
  1417   have "\<bar>x\<bar> = real ?k / real ?l"
  1418   proof -
  1419     from gcd have "real ?k / real ?l =
  1420         real (?gcd * ?k) / real (?gcd * ?l)" by simp
  1421     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1422     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1423     finally show ?thesis ..
  1424   qed
  1425   moreover
  1426   have "?gcd' = 1"
  1427   proof -
  1428     have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1429       by (rule gcd_mult_distrib_nat)
  1430     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1431     with gcd show ?thesis by auto
  1432   qed
  1433   ultimately show ?thesis ..
  1434 qed
  1435 
  1436 subsection{*Density of the Rational Reals in the Reals*}
  1437 
  1438 text{* This density proof is due to Stefan Richter and was ported by TN.  The
  1439 original source is \emph{Real Analysis} by H.L. Royden.
  1440 It employs the Archimedean property of the reals. *}
  1441 
  1442 lemma Rats_dense_in_real:
  1443   fixes x :: real
  1444   assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
  1445 proof -
  1446   from `x<y` have "0 < y-x" by simp
  1447   with reals_Archimedean obtain q::nat 
  1448     where q: "inverse (real q) < y-x" and "0 < q" by auto
  1449   def p \<equiv> "ceiling (y * real q) - 1"
  1450   def r \<equiv> "of_int p / real q"
  1451   from q have "x < y - inverse (real q)" by simp
  1452   also have "y - inverse (real q) \<le> r"
  1453     unfolding r_def p_def
  1454     by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
  1455   finally have "x < r" .
  1456   moreover have "r < y"
  1457     unfolding r_def p_def
  1458     by (simp add: divide_less_eq diff_less_eq `0 < q`
  1459       less_ceiling_iff [symmetric])
  1460   moreover from r_def have "r \<in> \<rat>" by simp
  1461   ultimately show ?thesis by fast
  1462 qed
  1463 
  1464 
  1465 
  1466 subsection{*Numerals and Arithmetic*}
  1467 
  1468 lemma [code_abbrev]:
  1469   "real_of_int (numeral k) = numeral k"
  1470   "real_of_int (neg_numeral k) = neg_numeral k"
  1471   by simp_all
  1472 
  1473 text{*Collapse applications of @{term real} to @{term number_of}*}
  1474 lemma real_numeral [simp]:
  1475   "real (numeral v :: int) = numeral v"
  1476   "real (neg_numeral v :: int) = neg_numeral v"
  1477 by (simp_all add: real_of_int_def)
  1478 
  1479 lemma real_of_nat_numeral [simp]:
  1480   "real (numeral v :: nat) = numeral v"
  1481 by (simp add: real_of_nat_def)
  1482 
  1483 declaration {*
  1484   K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
  1485     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
  1486   #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
  1487     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
  1488   #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
  1489       @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
  1490       @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
  1491       @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
  1492       @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
  1493   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
  1494   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
  1495 *}
  1496 
  1497 
  1498 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
  1499 
  1500 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
  1501 by arith
  1502 
  1503 text {* FIXME: redundant with @{text add_eq_0_iff} below *}
  1504 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
  1505 by auto
  1506 
  1507 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
  1508 by auto
  1509 
  1510 lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
  1511 by auto
  1512 
  1513 lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
  1514 by auto
  1515 
  1516 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
  1517 by auto
  1518 
  1519 subsection {* Lemmas about powers *}
  1520 
  1521 text {* FIXME: declare this in Rings.thy or not at all *}
  1522 declare abs_mult_self [simp]
  1523 
  1524 (* used by Import/HOL/real.imp *)
  1525 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1526 by simp
  1527 
  1528 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
  1529 apply (induct "n")
  1530 apply (auto simp add: real_of_nat_Suc)
  1531 apply (subst mult_2)
  1532 apply (erule add_less_le_mono)
  1533 apply (rule two_realpow_ge_one)
  1534 done
  1535 
  1536 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1537 lemma realpow_Suc_le_self:
  1538   fixes r :: "'a::linordered_semidom"
  1539   shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
  1540 by (insert power_decreasing [of 1 "Suc n" r], simp)
  1541 
  1542 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1543 lemma realpow_minus_mult:
  1544   fixes x :: "'a::monoid_mult"
  1545   shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
  1546 by (simp add: power_commutes split add: nat_diff_split)
  1547 
  1548 text {* FIXME: declare this [simp] for all types, or not at all *}
  1549 lemma real_two_squares_add_zero_iff [simp]:
  1550   "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
  1551 by (rule sum_squares_eq_zero_iff)
  1552 
  1553 text {* FIXME: declare this [simp] for all types, or not at all *}
  1554 lemma realpow_two_sum_zero_iff [simp]:
  1555      "(x\<^sup>2 + y\<^sup>2 = (0::real)) = (x = 0 & y = 0)"
  1556 by (rule sum_power2_eq_zero_iff)
  1557 
  1558 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
  1559 by (rule_tac y = 0 in order_trans, auto)
  1560 
  1561 lemma realpow_square_minus_le [simp]: "- u\<^sup>2 \<le> (x::real)\<^sup>2"
  1562 by (auto simp add: power2_eq_square)
  1563 
  1564 
  1565 lemma numeral_power_le_real_of_nat_cancel_iff[simp]:
  1566   "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::nat) ^ n \<le> a"
  1567   unfolding real_of_nat_le_iff[symmetric] by simp
  1568 
  1569 lemma real_of_nat_le_numeral_power_cancel_iff[simp]:
  1570   "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::nat) ^ n"
  1571   unfolding real_of_nat_le_iff[symmetric] by simp
  1572 
  1573 lemma numeral_power_le_real_of_int_cancel_iff[simp]:
  1574   "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::int) ^ n \<le> a"
  1575   unfolding real_of_int_le_iff[symmetric] by simp
  1576 
  1577 lemma real_of_int_le_numeral_power_cancel_iff[simp]:
  1578   "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::int) ^ n"
  1579   unfolding real_of_int_le_iff[symmetric] by simp
  1580 
  1581 lemma neg_numeral_power_le_real_of_int_cancel_iff[simp]:
  1582   "(neg_numeral x::real) ^ n \<le> real a \<longleftrightarrow> (neg_numeral x::int) ^ n \<le> a"
  1583   unfolding real_of_int_le_iff[symmetric] by simp
  1584 
  1585 lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]:
  1586   "real a \<le> (neg_numeral x::real) ^ n \<longleftrightarrow> a \<le> (neg_numeral x::int) ^ n"
  1587   unfolding real_of_int_le_iff[symmetric] by simp
  1588 
  1589 subsection{*Density of the Reals*}
  1590 
  1591 lemma real_lbound_gt_zero:
  1592      "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
  1593 apply (rule_tac x = " (min d1 d2) /2" in exI)
  1594 apply (simp add: min_def)
  1595 done
  1596 
  1597 
  1598 text{*Similar results are proved in @{text Fields}*}
  1599 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
  1600   by auto
  1601 
  1602 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1603   by auto
  1604 
  1605 lemma real_sum_of_halves: "x/2 + x/2 = (x::real)"
  1606   by simp
  1607 
  1608 subsection{*Absolute Value Function for the Reals*}
  1609 
  1610 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
  1611 by (simp add: abs_if)
  1612 
  1613 (* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
  1614 lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
  1615 by (force simp add: abs_le_iff)
  1616 
  1617 lemma abs_add_one_gt_zero: "(0::real) < 1 + abs(x)"
  1618 by (simp add: abs_if)
  1619 
  1620 lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
  1621 by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
  1622 
  1623 lemma abs_add_one_not_less_self: "~ abs(x) + (1::real) < x"
  1624 by simp
  1625  
  1626 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1627 by simp
  1628 
  1629 
  1630 subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
  1631 
  1632 (* FIXME: theorems for negative numerals *)
  1633 lemma numeral_less_real_of_int_iff [simp]:
  1634      "((numeral n) < real (m::int)) = (numeral n < m)"
  1635 apply auto
  1636 apply (rule real_of_int_less_iff [THEN iffD1])
  1637 apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
  1638 done
  1639 
  1640 lemma numeral_less_real_of_int_iff2 [simp]:
  1641      "(real (m::int) < (numeral n)) = (m < numeral n)"
  1642 apply auto
  1643 apply (rule real_of_int_less_iff [THEN iffD1])
  1644 apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
  1645 done
  1646 
  1647 lemma numeral_le_real_of_int_iff [simp]:
  1648      "((numeral n) \<le> real (m::int)) = (numeral n \<le> m)"
  1649 by (simp add: linorder_not_less [symmetric])
  1650 
  1651 lemma numeral_le_real_of_int_iff2 [simp]:
  1652      "(real (m::int) \<le> (numeral n)) = (m \<le> numeral n)"
  1653 by (simp add: linorder_not_less [symmetric])
  1654 
  1655 lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
  1656 unfolding real_of_nat_def by simp
  1657 
  1658 lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
  1659 unfolding real_of_nat_def by (simp add: floor_minus)
  1660 
  1661 lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
  1662 unfolding real_of_int_def by simp
  1663 
  1664 lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
  1665 unfolding real_of_int_def by (simp add: floor_minus)
  1666 
  1667 lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
  1668 unfolding real_of_int_def by (rule floor_exists)
  1669 
  1670 lemma lemma_floor:
  1671   assumes a1: "real m \<le> r" and a2: "r < real n + 1"
  1672   shows "m \<le> (n::int)"
  1673 proof -
  1674   have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
  1675   also have "... = real (n + 1)" by simp
  1676   finally have "m < n + 1" by (simp only: real_of_int_less_iff)
  1677   thus ?thesis by arith
  1678 qed
  1679 
  1680 lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
  1681 unfolding real_of_int_def by (rule of_int_floor_le)
  1682 
  1683 lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
  1684 by (auto intro: lemma_floor)
  1685 
  1686 lemma real_of_int_floor_cancel [simp]:
  1687     "(real (floor x) = x) = (\<exists>n::int. x = real n)"
  1688   using floor_real_of_int by metis
  1689 
  1690 lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
  1691   unfolding real_of_int_def using floor_unique [of n x] by simp
  1692 
  1693 lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
  1694   unfolding real_of_int_def by (rule floor_unique)
  1695 
  1696 lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
  1697 apply (rule inj_int [THEN injD])
  1698 apply (simp add: real_of_nat_Suc)
  1699 apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
  1700 done
  1701 
  1702 lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
  1703 apply (drule order_le_imp_less_or_eq)
  1704 apply (auto intro: floor_eq3)
  1705 done
  1706 
  1707 lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
  1708   unfolding real_of_int_def using floor_correct [of r] by simp
  1709 
  1710 lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
  1711   unfolding real_of_int_def using floor_correct [of r] by simp
  1712 
  1713 lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
  1714   unfolding real_of_int_def using floor_correct [of r] by simp
  1715 
  1716 lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
  1717   unfolding real_of_int_def using floor_correct [of r] by simp
  1718 
  1719 lemma le_floor: "real a <= x ==> a <= floor x"
  1720   unfolding real_of_int_def by (simp add: le_floor_iff)
  1721 
  1722 lemma real_le_floor: "a <= floor x ==> real a <= x"
  1723   unfolding real_of_int_def by (simp add: le_floor_iff)
  1724 
  1725 lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
  1726   unfolding real_of_int_def by (rule le_floor_iff)
  1727 
  1728 lemma floor_less_eq: "(floor x < a) = (x < real a)"
  1729   unfolding real_of_int_def by (rule floor_less_iff)
  1730 
  1731 lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
  1732   unfolding real_of_int_def by (rule less_floor_iff)
  1733 
  1734 lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
  1735   unfolding real_of_int_def by (rule floor_le_iff)
  1736 
  1737 lemma floor_add [simp]: "floor (x + real a) = floor x + a"
  1738   unfolding real_of_int_def by (rule floor_add_of_int)
  1739 
  1740 lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
  1741   unfolding real_of_int_def by (rule floor_diff_of_int)
  1742 
  1743 lemma le_mult_floor:
  1744   assumes "0 \<le> (a :: real)" and "0 \<le> b"
  1745   shows "floor a * floor b \<le> floor (a * b)"
  1746 proof -
  1747   have "real (floor a) \<le> a"
  1748     and "real (floor b) \<le> b" by auto
  1749   hence "real (floor a * floor b) \<le> a * b"
  1750     using assms by (auto intro!: mult_mono)
  1751   also have "a * b < real (floor (a * b) + 1)" by auto
  1752   finally show ?thesis unfolding real_of_int_less_iff by simp
  1753 qed
  1754 
  1755 lemma floor_divide_eq_div:
  1756   "floor (real a / real b) = a div b"
  1757 proof cases
  1758   assume "b \<noteq> 0 \<or> b dvd a"
  1759   with real_of_int_div3[of a b] show ?thesis
  1760     by (auto simp: real_of_int_div[symmetric] intro!: floor_eq2 real_of_int_div4 neq_le_trans)
  1761        (metis add_left_cancel zero_neq_one real_of_int_div_aux real_of_int_inject
  1762               real_of_int_zero_cancel right_inverse_eq div_self mod_div_trivial)
  1763 qed (auto simp: real_of_int_div)
  1764 
  1765 lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
  1766   unfolding real_of_nat_def by simp
  1767 
  1768 lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
  1769   unfolding real_of_int_def by (rule le_of_int_ceiling)
  1770 
  1771 lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
  1772   unfolding real_of_int_def by simp
  1773 
  1774 lemma real_of_int_ceiling_cancel [simp]:
  1775      "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
  1776   using ceiling_real_of_int by metis
  1777 
  1778 lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
  1779   unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
  1780 
  1781 lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
  1782   unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
  1783 
  1784 lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
  1785   unfolding real_of_int_def using ceiling_unique [of n x] by simp
  1786 
  1787 lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
  1788   unfolding real_of_int_def using ceiling_correct [of r] by simp
  1789 
  1790 lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
  1791   unfolding real_of_int_def using ceiling_correct [of r] by simp
  1792 
  1793 lemma ceiling_le: "x <= real a ==> ceiling x <= a"
  1794   unfolding real_of_int_def by (simp add: ceiling_le_iff)
  1795 
  1796 lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
  1797   unfolding real_of_int_def by (simp add: ceiling_le_iff)
  1798 
  1799 lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
  1800   unfolding real_of_int_def by (rule ceiling_le_iff)
  1801 
  1802 lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
  1803   unfolding real_of_int_def by (rule less_ceiling_iff)
  1804 
  1805 lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
  1806   unfolding real_of_int_def by (rule ceiling_less_iff)
  1807 
  1808 lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
  1809   unfolding real_of_int_def by (rule le_ceiling_iff)
  1810 
  1811 lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
  1812   unfolding real_of_int_def by (rule ceiling_add_of_int)
  1813 
  1814 lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
  1815   unfolding real_of_int_def by (rule ceiling_diff_of_int)
  1816 
  1817 
  1818 subsubsection {* Versions for the natural numbers *}
  1819 
  1820 definition
  1821   natfloor :: "real => nat" where
  1822   "natfloor x = nat(floor x)"
  1823 
  1824 definition
  1825   natceiling :: "real => nat" where
  1826   "natceiling x = nat(ceiling x)"
  1827 
  1828 lemma natfloor_zero [simp]: "natfloor 0 = 0"
  1829   by (unfold natfloor_def, simp)
  1830 
  1831 lemma natfloor_one [simp]: "natfloor 1 = 1"
  1832   by (unfold natfloor_def, simp)
  1833 
  1834 lemma zero_le_natfloor [simp]: "0 <= natfloor x"
  1835   by (unfold natfloor_def, simp)
  1836 
  1837 lemma natfloor_numeral_eq [simp]: "natfloor (numeral n) = numeral n"
  1838   by (unfold natfloor_def, simp)
  1839 
  1840 lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
  1841   by (unfold natfloor_def, simp)
  1842 
  1843 lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
  1844   by (unfold natfloor_def, simp)
  1845 
  1846 lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
  1847   unfolding natfloor_def by simp
  1848 
  1849 lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
  1850   unfolding natfloor_def by (intro nat_mono floor_mono)
  1851 
  1852 lemma le_natfloor: "real x <= a ==> x <= natfloor a"
  1853   apply (unfold natfloor_def)
  1854   apply (subst nat_int [THEN sym])
  1855   apply (rule nat_mono)
  1856   apply (rule le_floor)
  1857   apply simp
  1858 done
  1859 
  1860 lemma natfloor_less_iff: "0 \<le> x \<Longrightarrow> natfloor x < n \<longleftrightarrow> x < real n"
  1861   unfolding natfloor_def real_of_nat_def
  1862   by (simp add: nat_less_iff floor_less_iff)
  1863 
  1864 lemma less_natfloor:
  1865   assumes "0 \<le> x" and "x < real (n :: nat)"
  1866   shows "natfloor x < n"
  1867   using assms by (simp add: natfloor_less_iff)
  1868 
  1869 lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
  1870   apply (rule iffI)
  1871   apply (rule order_trans)
  1872   prefer 2
  1873   apply (erule real_natfloor_le)
  1874   apply (subst real_of_nat_le_iff)
  1875   apply assumption
  1876   apply (erule le_natfloor)
  1877 done
  1878 
  1879 lemma le_natfloor_eq_numeral [simp]:
  1880     "~ neg((numeral n)::int) ==> 0 <= x ==>
  1881       (numeral n <= natfloor x) = (numeral n <= x)"
  1882   apply (subst le_natfloor_eq, assumption)
  1883   apply simp
  1884 done
  1885 
  1886 lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
  1887   apply (case_tac "0 <= x")
  1888   apply (subst le_natfloor_eq, assumption, simp)
  1889   apply (rule iffI)
  1890   apply (subgoal_tac "natfloor x <= natfloor 0")
  1891   apply simp
  1892   apply (rule natfloor_mono)
  1893   apply simp
  1894   apply simp
  1895 done
  1896 
  1897 lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
  1898   unfolding natfloor_def by (simp add: floor_eq2 [where n="int n"])
  1899 
  1900 lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
  1901   apply (case_tac "0 <= x")
  1902   apply (unfold natfloor_def)
  1903   apply simp
  1904   apply simp_all
  1905 done
  1906 
  1907 lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
  1908 using real_natfloor_add_one_gt by (simp add: algebra_simps)
  1909 
  1910 lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
  1911   apply (subgoal_tac "z < real(natfloor z) + 1")
  1912   apply arith
  1913   apply (rule real_natfloor_add_one_gt)
  1914 done
  1915 
  1916 lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
  1917   unfolding natfloor_def
  1918   unfolding real_of_int_of_nat_eq [symmetric] floor_add
  1919   by (simp add: nat_add_distrib)
  1920 
  1921 lemma natfloor_add_numeral [simp]:
  1922     "~neg ((numeral n)::int) ==> 0 <= x ==>
  1923       natfloor (x + numeral n) = natfloor x + numeral n"
  1924   by (simp add: natfloor_add [symmetric])
  1925 
  1926 lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
  1927   by (simp add: natfloor_add [symmetric] del: One_nat_def)
  1928 
  1929 lemma natfloor_subtract [simp]:
  1930     "natfloor(x - real a) = natfloor x - a"
  1931   unfolding natfloor_def real_of_int_of_nat_eq [symmetric] floor_subtract
  1932   by simp
  1933 
  1934 lemma natfloor_div_nat:
  1935   assumes "1 <= x" and "y > 0"
  1936   shows "natfloor (x / real y) = natfloor x div y"
  1937 proof (rule natfloor_eq)
  1938   have "(natfloor x) div y * y \<le> natfloor x"
  1939     by (rule add_leD1 [where k="natfloor x mod y"], simp)
  1940   thus "real (natfloor x div y) \<le> x / real y"
  1941     using assms by (simp add: le_divide_eq le_natfloor_eq)
  1942   have "natfloor x < (natfloor x) div y * y + y"
  1943     apply (subst mod_div_equality [symmetric])
  1944     apply (rule add_strict_left_mono)
  1945     apply (rule mod_less_divisor)
  1946     apply fact
  1947     done
  1948   thus "x / real y < real (natfloor x div y) + 1"
  1949     using assms
  1950     by (simp add: divide_less_eq natfloor_less_iff distrib_right)
  1951 qed
  1952 
  1953 lemma le_mult_natfloor:
  1954   shows "natfloor a * natfloor b \<le> natfloor (a * b)"
  1955   by (cases "0 <= a & 0 <= b")
  1956     (auto simp add: le_natfloor_eq mult_nonneg_nonneg mult_mono' real_natfloor_le natfloor_neg)
  1957 
  1958 lemma natceiling_zero [simp]: "natceiling 0 = 0"
  1959   by (unfold natceiling_def, simp)
  1960 
  1961 lemma natceiling_one [simp]: "natceiling 1 = 1"
  1962   by (unfold natceiling_def, simp)
  1963 
  1964 lemma zero_le_natceiling [simp]: "0 <= natceiling x"
  1965   by (unfold natceiling_def, simp)
  1966 
  1967 lemma natceiling_numeral_eq [simp]: "natceiling (numeral n) = numeral n"
  1968   by (unfold natceiling_def, simp)
  1969 
  1970 lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
  1971   by (unfold natceiling_def, simp)
  1972 
  1973 lemma real_natceiling_ge: "x <= real(natceiling x)"
  1974   unfolding natceiling_def by (cases "x < 0", simp_all)
  1975 
  1976 lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
  1977   unfolding natceiling_def by simp
  1978 
  1979 lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
  1980   unfolding natceiling_def by (intro nat_mono ceiling_mono)
  1981 
  1982 lemma natceiling_le: "x <= real a ==> natceiling x <= a"
  1983   unfolding natceiling_def real_of_nat_def
  1984   by (simp add: nat_le_iff ceiling_le_iff)
  1985 
  1986 lemma natceiling_le_eq: "(natceiling x <= a) = (x <= real a)"
  1987   unfolding natceiling_def real_of_nat_def
  1988   by (simp add: nat_le_iff ceiling_le_iff)
  1989 
  1990 lemma natceiling_le_eq_numeral [simp]:
  1991     "~ neg((numeral n)::int) ==>
  1992       (natceiling x <= numeral n) = (x <= numeral n)"
  1993   by (simp add: natceiling_le_eq)
  1994 
  1995 lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
  1996   unfolding natceiling_def
  1997   by (simp add: nat_le_iff ceiling_le_iff)
  1998 
  1999 lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
  2000   unfolding natceiling_def
  2001   by (simp add: ceiling_eq2 [where n="int n"])
  2002 
  2003 lemma natceiling_add [simp]: "0 <= x ==>
  2004     natceiling (x + real a) = natceiling x + a"
  2005   unfolding natceiling_def
  2006   unfolding real_of_int_of_nat_eq [symmetric] ceiling_add
  2007   by (simp add: nat_add_distrib)
  2008 
  2009 lemma natceiling_add_numeral [simp]:
  2010     "~ neg ((numeral n)::int) ==> 0 <= x ==>
  2011       natceiling (x + numeral n) = natceiling x + numeral n"
  2012   by (simp add: natceiling_add [symmetric])
  2013 
  2014 lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
  2015   by (simp add: natceiling_add [symmetric] del: One_nat_def)
  2016 
  2017 lemma natceiling_subtract [simp]: "natceiling(x - real a) = natceiling x - a"
  2018   unfolding natceiling_def real_of_int_of_nat_eq [symmetric] ceiling_subtract
  2019   by simp
  2020 
  2021 subsection {* Exponentiation with floor *}
  2022 
  2023 lemma floor_power:
  2024   assumes "x = real (floor x)"
  2025   shows "floor (x ^ n) = floor x ^ n"
  2026 proof -
  2027   have *: "x ^ n = real (floor x ^ n)"
  2028     using assms by (induct n arbitrary: x) simp_all
  2029   show ?thesis unfolding real_of_int_inject[symmetric]
  2030     unfolding * floor_real_of_int ..
  2031 qed
  2032 
  2033 lemma natfloor_power:
  2034   assumes "x = real (natfloor x)"
  2035   shows "natfloor (x ^ n) = natfloor x ^ n"
  2036 proof -
  2037   from assms have "0 \<le> floor x" by auto
  2038   note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
  2039   from floor_power[OF this]
  2040   show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
  2041     by simp
  2042 qed
  2043 
  2044 
  2045 subsection {* Implementation of rational real numbers *}
  2046 
  2047 text {* Formal constructor *}
  2048 
  2049 definition Ratreal :: "rat \<Rightarrow> real" where
  2050   [code_abbrev, simp]: "Ratreal = of_rat"
  2051 
  2052 code_datatype Ratreal
  2053 
  2054 
  2055 text {* Numerals *}
  2056 
  2057 lemma [code_abbrev]:
  2058   "(of_rat (of_int a) :: real) = of_int a"
  2059   by simp
  2060 
  2061 lemma [code_abbrev]:
  2062   "(of_rat 0 :: real) = 0"
  2063   by simp
  2064 
  2065 lemma [code_abbrev]:
  2066   "(of_rat 1 :: real) = 1"
  2067   by simp
  2068 
  2069 lemma [code_abbrev]:
  2070   "(of_rat (numeral k) :: real) = numeral k"
  2071   by simp
  2072 
  2073 lemma [code_abbrev]:
  2074   "(of_rat (neg_numeral k) :: real) = neg_numeral k"
  2075   by simp
  2076 
  2077 lemma [code_post]:
  2078   "(of_rat (0 / r)  :: real) = 0"
  2079   "(of_rat (r / 0)  :: real) = 0"
  2080   "(of_rat (1 / 1)  :: real) = 1"
  2081   "(of_rat (numeral k / 1) :: real) = numeral k"
  2082   "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
  2083   "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
  2084   "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
  2085   "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
  2086   "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
  2087   "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
  2088   "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
  2089   by (simp_all add: of_rat_divide)
  2090 
  2091 
  2092 text {* Operations *}
  2093 
  2094 lemma zero_real_code [code]:
  2095   "0 = Ratreal 0"
  2096 by simp
  2097 
  2098 lemma one_real_code [code]:
  2099   "1 = Ratreal 1"
  2100 by simp
  2101 
  2102 instantiation real :: equal
  2103 begin
  2104 
  2105 definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
  2106 
  2107 instance proof
  2108 qed (simp add: equal_real_def)
  2109 
  2110 lemma real_equal_code [code]:
  2111   "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
  2112   by (simp add: equal_real_def equal)
  2113 
  2114 lemma [code nbe]:
  2115   "HOL.equal (x::real) x \<longleftrightarrow> True"
  2116   by (rule equal_refl)
  2117 
  2118 end
  2119 
  2120 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  2121   by (simp add: of_rat_less_eq)
  2122 
  2123 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  2124   by (simp add: of_rat_less)
  2125 
  2126 lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  2127   by (simp add: of_rat_add)
  2128 
  2129 lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  2130   by (simp add: of_rat_mult)
  2131 
  2132 lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  2133   by (simp add: of_rat_minus)
  2134 
  2135 lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  2136   by (simp add: of_rat_diff)
  2137 
  2138 lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  2139   by (simp add: of_rat_inverse)
  2140  
  2141 lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  2142   by (simp add: of_rat_divide)
  2143 
  2144 lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  2145   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)
  2146 
  2147 
  2148 text {* Quickcheck *}
  2149 
  2150 definition (in term_syntax)
  2151   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  2152   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  2153 
  2154 notation fcomp (infixl "\<circ>>" 60)
  2155 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  2156 
  2157 instantiation real :: random
  2158 begin
  2159 
  2160 definition
  2161   "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  2162 
  2163 instance ..
  2164 
  2165 end
  2166 
  2167 no_notation fcomp (infixl "\<circ>>" 60)
  2168 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  2169 
  2170 instantiation real :: exhaustive
  2171 begin
  2172 
  2173 definition
  2174   "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
  2175 
  2176 instance ..
  2177 
  2178 end
  2179 
  2180 instantiation real :: full_exhaustive
  2181 begin
  2182 
  2183 definition
  2184   "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
  2185 
  2186 instance ..
  2187 
  2188 end
  2189 
  2190 instantiation real :: narrowing
  2191 begin
  2192 
  2193 definition
  2194   "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
  2195 
  2196 instance ..
  2197 
  2198 end
  2199 
  2200 
  2201 subsection {* Setup for Nitpick *}
  2202 
  2203 declaration {*
  2204   Nitpick_HOL.register_frac_type @{type_name real}
  2205    [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  2206     (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  2207     (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  2208     (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  2209     (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  2210     (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  2211     (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
  2212     (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  2213 *}
  2214 
  2215 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
  2216     ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  2217     times_real_inst.times_real uminus_real_inst.uminus_real
  2218     zero_real_inst.zero_real
  2219 
  2220 ML_file "Tools/SMT/smt_real.ML"
  2221 setup SMT_Real.setup
  2222 
  2223 end