src/HOL/Real.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (19 months ago)
changeset 67022 49309fe530fd
parent 66912 a99a7cbf0fb5
child 67051 e7e54a0b9197
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
     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 section \<open>Development of the Reals using Cauchy Sequences\<close>
    11 
    12 theory Real
    13 imports Rat
    14 begin
    15 
    16 text \<open>
    17   This theory contains a formalization of the real numbers as equivalence
    18   classes of Cauchy sequences of rationals. See
    19   \<^file>\<open>~~/src/HOL/ex/Dedekind_Real.thy\<close> for an alternative construction using
    20   Dedekind cuts.
    21 \<close>
    22 
    23 
    24 subsection \<open>Preliminary lemmas\<close>
    25 
    26 text{*Useful in convergence arguments*}
    27 lemma inverse_of_nat_le:
    28   fixes n::nat shows "\<lbrakk>n \<le> m; n\<noteq>0\<rbrakk> \<Longrightarrow> 1 / of_nat m \<le> (1::'a::linordered_field) / of_nat n"
    29   by (simp add: frac_le)
    30 
    31 lemma inj_add_left [simp]: "inj (op + x)"
    32   for x :: "'a::cancel_semigroup_add"
    33   by (meson add_left_imp_eq injI)
    34 
    35 lemma inj_mult_left [simp]: "inj (op * x) \<longleftrightarrow> x \<noteq> 0"
    36   for x :: "'a::idom"
    37   by (metis injI mult_cancel_left the_inv_f_f zero_neq_one)
    38 
    39 lemma add_diff_add: "(a + c) - (b + d) = (a - b) + (c - d)"
    40   for a b c d :: "'a::ab_group_add"
    41   by simp
    42 
    43 lemma minus_diff_minus: "- a - - b = - (a - b)"
    44   for a b :: "'a::ab_group_add"
    45   by simp
    46 
    47 lemma mult_diff_mult: "(x * y - a * b) = x * (y - b) + (x - a) * b"
    48   for x y a b :: "'a::ring"
    49   by (simp add: algebra_simps)
    50 
    51 lemma inverse_diff_inverse:
    52   fixes a b :: "'a::division_ring"
    53   assumes "a \<noteq> 0" and "b \<noteq> 0"
    54   shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
    55   using assms by (simp add: algebra_simps)
    56 
    57 lemma obtain_pos_sum:
    58   fixes r :: rat assumes r: "0 < r"
    59   obtains s t where "0 < s" and "0 < t" and "r = s + t"
    60 proof
    61   from r show "0 < r/2" by simp
    62   from r show "0 < r/2" by simp
    63   show "r = r/2 + r/2" by simp
    64 qed
    65 
    66 
    67 subsection \<open>Sequences that converge to zero\<close>
    68 
    69 definition vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    70   where "vanishes X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
    71 
    72 lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
    73   unfolding vanishes_def by simp
    74 
    75 lemma vanishesD: "vanishes X \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
    76   unfolding vanishes_def by simp
    77 
    78 lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
    79   unfolding vanishes_def
    80   apply (cases "c = 0")
    81    apply auto
    82   apply (rule exI [where x = "\<bar>c\<bar>"])
    83   apply auto
    84   done
    85 
    86 lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
    87   unfolding vanishes_def by simp
    88 
    89 lemma vanishes_add:
    90   assumes X: "vanishes X"
    91     and Y: "vanishes Y"
    92   shows "vanishes (\<lambda>n. X n + Y n)"
    93 proof (rule vanishesI)
    94   fix r :: rat
    95   assume "0 < r"
    96   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
    97     by (rule obtain_pos_sum)
    98   obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
    99     using vanishesD [OF X s] ..
   100   obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
   101     using vanishesD [OF Y t] ..
   102   have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
   103   proof clarsimp
   104     fix n
   105     assume n: "i \<le> n" "j \<le> n"
   106     have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>"
   107       by (rule abs_triangle_ineq)
   108     also have "\<dots> < s + t"
   109       by (simp add: add_strict_mono i j n)
   110     finally show "\<bar>X n + Y n\<bar> < r"
   111       by (simp only: r)
   112   qed
   113   then show "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
   114 qed
   115 
   116 lemma vanishes_diff:
   117   assumes "vanishes X" "vanishes Y"
   118   shows "vanishes (\<lambda>n. X n - Y n)"
   119   unfolding diff_conv_add_uminus by (intro vanishes_add vanishes_minus assms)
   120 
   121 lemma vanishes_mult_bounded:
   122   assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
   123   assumes Y: "vanishes (\<lambda>n. Y n)"
   124   shows "vanishes (\<lambda>n. X n * Y n)"
   125 proof (rule vanishesI)
   126   fix r :: rat
   127   assume r: "0 < r"
   128   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   129     using X by blast
   130   obtain b where b: "0 < b" "r = a * b"
   131   proof
   132     show "0 < r / a" using r a by simp
   133     show "r = a * (r / a)" using a by simp
   134   qed
   135   obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
   136     using vanishesD [OF Y b(1)] ..
   137   have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   138     by (simp add: b(2) abs_mult mult_strict_mono' a k)
   139   then show "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   140 qed
   141 
   142 
   143 subsection \<open>Cauchy sequences\<close>
   144 
   145 definition cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   146   where "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   147 
   148 lemma cauchyI: "(\<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"
   149   unfolding cauchy_def by simp
   150 
   151 lemma cauchyD: "cauchy X \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
   152   unfolding cauchy_def by simp
   153 
   154 lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
   155   unfolding cauchy_def by simp
   156 
   157 lemma cauchy_add [simp]:
   158   assumes X: "cauchy X" and Y: "cauchy Y"
   159   shows "cauchy (\<lambda>n. X n + Y n)"
   160 proof (rule cauchyI)
   161   fix r :: rat
   162   assume "0 < r"
   163   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   164     by (rule obtain_pos_sum)
   165   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   166     using cauchyD [OF X s] ..
   167   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   168     using cauchyD [OF Y t] ..
   169   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
   170   proof clarsimp
   171     fix m n
   172     assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   173     have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
   174       unfolding add_diff_add by (rule abs_triangle_ineq)
   175     also have "\<dots> < s + t"
   176       by (rule add_strict_mono) (simp_all add: i j *)
   177     finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" by (simp only: r)
   178   qed
   179   then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
   180 qed
   181 
   182 lemma cauchy_minus [simp]:
   183   assumes X: "cauchy X"
   184   shows "cauchy (\<lambda>n. - X n)"
   185   using assms unfolding cauchy_def
   186   unfolding minus_diff_minus abs_minus_cancel .
   187 
   188 lemma cauchy_diff [simp]:
   189   assumes "cauchy X" "cauchy Y"
   190   shows "cauchy (\<lambda>n. X n - Y n)"
   191   using assms unfolding diff_conv_add_uminus by (simp del: add_uminus_conv_diff)
   192 
   193 lemma cauchy_imp_bounded:
   194   assumes "cauchy X"
   195   shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   196 proof -
   197   obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
   198     using cauchyD [OF assms zero_less_one] ..
   199   show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   200   proof (intro exI conjI allI)
   201     have "0 \<le> \<bar>X 0\<bar>" by simp
   202     also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
   203     finally have "0 \<le> Max (abs ` X ` {..k})" .
   204     then show "0 < Max (abs ` X ` {..k}) + 1" by simp
   205   next
   206     fix n :: nat
   207     show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
   208     proof (rule linorder_le_cases)
   209       assume "n \<le> k"
   210       then have "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
   211       then show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
   212     next
   213       assume "k \<le> n"
   214       have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   215       also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   216         by (rule abs_triangle_ineq)
   217       also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   218         by (rule add_le_less_mono) (simp_all add: k \<open>k \<le> n\<close>)
   219       finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   220     qed
   221   qed
   222 qed
   223 
   224 lemma cauchy_mult [simp]:
   225   assumes X: "cauchy X" and Y: "cauchy Y"
   226   shows "cauchy (\<lambda>n. X n * Y n)"
   227 proof (rule cauchyI)
   228   fix r :: rat assume "0 < r"
   229   then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
   230     by (rule obtain_pos_sum)
   231   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   232     using cauchy_imp_bounded [OF X] by blast
   233   obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
   234     using cauchy_imp_bounded [OF Y] by blast
   235   obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
   236   proof
   237     show "0 < v/b" using v b(1) by simp
   238     show "0 < u/a" using u a(1) by simp
   239     show "r = a * (u/a) + (v/b) * b"
   240       using a(1) b(1) \<open>r = u + v\<close> by simp
   241   qed
   242   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   243     using cauchyD [OF X s] ..
   244   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   245     using cauchyD [OF Y t] ..
   246   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
   247   proof clarsimp
   248     fix m n
   249     assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   250     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>"
   251       unfolding mult_diff_mult ..
   252     also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
   253       by (rule abs_triangle_ineq)
   254     also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
   255       unfolding abs_mult ..
   256     also have "\<dots> < a * t + s * b"
   257       by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
   258     finally show "\<bar>X m * Y m - X n * Y n\<bar> < r"
   259       by (simp only: r)
   260   qed
   261   then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
   262 qed
   263 
   264 lemma cauchy_not_vanishes_cases:
   265   assumes X: "cauchy X"
   266   assumes nz: "\<not> vanishes X"
   267   shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
   268 proof -
   269   obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
   270     using nz unfolding vanishes_def by (auto simp add: not_less)
   271   obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
   272     using \<open>0 < r\<close> by (rule obtain_pos_sum)
   273   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   274     using cauchyD [OF X s] ..
   275   obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
   276     using r by blast
   277   have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
   278     using i \<open>i \<le> k\<close> by auto
   279   have "X k \<le> - r \<or> r \<le> X k"
   280     using \<open>r \<le> \<bar>X k\<bar>\<close> by auto
   281   then have "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   282     unfolding \<open>r = s + t\<close> using k by auto
   283   then have "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
   284   then show "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   285     using t by auto
   286 qed
   287 
   288 lemma cauchy_not_vanishes:
   289   assumes X: "cauchy X"
   290     and nz: "\<not> vanishes X"
   291   shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
   292   using cauchy_not_vanishes_cases [OF assms]
   293   apply clarify
   294   apply (rule exI)
   295   apply (erule conjI)
   296   apply (rule_tac x = k in exI)
   297   apply auto
   298   done
   299 
   300 lemma cauchy_inverse [simp]:
   301   assumes X: "cauchy X"
   302     and nz: "\<not> vanishes X"
   303   shows "cauchy (\<lambda>n. inverse (X n))"
   304 proof (rule cauchyI)
   305   fix r :: rat
   306   assume "0 < r"
   307   obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   308     using cauchy_not_vanishes [OF X nz] by blast
   309   from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   310   obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   311   proof
   312     show "0 < b * r * b" by (simp add: \<open>0 < r\<close> b)
   313     show "r = inverse b * (b * r * b) * inverse b"
   314       using b by simp
   315   qed
   316   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   317     using cauchyD [OF X s] ..
   318   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
   319   proof clarsimp
   320     fix m n
   321     assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   322     have "\<bar>inverse (X m) - inverse (X n)\<bar> = inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
   323       by (simp add: inverse_diff_inverse nz * abs_mult)
   324     also have "\<dots> < inverse b * s * inverse b"
   325       by (simp add: mult_strict_mono less_imp_inverse_less i j b * s)
   326     finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" by (simp only: r)
   327   qed
   328   then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
   329 qed
   330 
   331 lemma vanishes_diff_inverse:
   332   assumes X: "cauchy X" "\<not> vanishes X"
   333     and Y: "cauchy Y" "\<not> vanishes Y"
   334     and XY: "vanishes (\<lambda>n. X n - Y n)"
   335   shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
   336 proof (rule vanishesI)
   337   fix r :: rat
   338   assume r: "0 < r"
   339   obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
   340     using cauchy_not_vanishes [OF X] by blast
   341   obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
   342     using cauchy_not_vanishes [OF Y] by blast
   343   obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
   344   proof
   345     show "0 < a * r * b"
   346       using a r b by simp
   347     show "inverse a * (a * r * b) * inverse b = r"
   348       using a r b by simp
   349   qed
   350   obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
   351     using vanishesD [OF XY s] ..
   352   have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
   353   proof clarsimp
   354     fix n
   355     assume n: "i \<le> n" "j \<le> n" "k \<le> n"
   356     with i j a b have "X n \<noteq> 0" and "Y n \<noteq> 0"
   357       by auto
   358     then have "\<bar>inverse (X n) - inverse (Y n)\<bar> = inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
   359       by (simp add: inverse_diff_inverse abs_mult)
   360     also have "\<dots> < inverse a * s * inverse b"
   361       by (intro mult_strict_mono' less_imp_inverse_less) (simp_all add: a b i j k n)
   362     also note \<open>inverse a * s * inverse b = r\<close>
   363     finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
   364   qed
   365   then show "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
   366 qed
   367 
   368 
   369 subsection \<open>Equivalence relation on Cauchy sequences\<close>
   370 
   371 definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
   372   where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
   373 
   374 lemma realrelI [intro?]: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> vanishes (\<lambda>n. X n - Y n) \<Longrightarrow> realrel X Y"
   375   by (simp add: realrel_def)
   376 
   377 lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
   378   by (simp add: realrel_def)
   379 
   380 lemma symp_realrel: "symp realrel"
   381   unfolding realrel_def
   382   apply (rule sympI)
   383   apply clarify
   384   apply (drule vanishes_minus)
   385   apply simp
   386   done
   387 
   388 lemma transp_realrel: "transp realrel"
   389   unfolding realrel_def
   390   apply (rule transpI)
   391   apply clarify
   392   apply (drule (1) vanishes_add)
   393   apply (simp add: algebra_simps)
   394   done
   395 
   396 lemma part_equivp_realrel: "part_equivp realrel"
   397   by (blast intro: part_equivpI symp_realrel transp_realrel realrel_refl cauchy_const)
   398 
   399 
   400 subsection \<open>The field of real numbers\<close>
   401 
   402 quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   403   morphisms rep_real Real
   404   by (rule part_equivp_realrel)
   405 
   406 lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
   407   unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
   408 
   409 lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
   410   assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)"
   411   shows "P x"
   412 proof (induct x)
   413   case (1 X)
   414   then have "cauchy X" by (simp add: realrel_def)
   415   then show "P (Real X)" by (rule assms)
   416 qed
   417 
   418 lemma eq_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
   419   using real.rel_eq_transfer
   420   unfolding real.pcr_cr_eq cr_real_def rel_fun_def realrel_def by simp
   421 
   422 lemma Domainp_pcr_real [transfer_domain_rule]: "Domainp pcr_real = cauchy"
   423   by (simp add: real.domain_eq realrel_def)
   424 
   425 instantiation real :: field
   426 begin
   427 
   428 lift_definition zero_real :: "real" is "\<lambda>n. 0"
   429   by (simp add: realrel_refl)
   430 
   431 lift_definition one_real :: "real" is "\<lambda>n. 1"
   432   by (simp add: realrel_refl)
   433 
   434 lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
   435   unfolding realrel_def add_diff_add
   436   by (simp only: cauchy_add vanishes_add simp_thms)
   437 
   438 lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
   439   unfolding realrel_def minus_diff_minus
   440   by (simp only: cauchy_minus vanishes_minus simp_thms)
   441 
   442 lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
   443   unfolding realrel_def mult_diff_mult
   444   apply (subst (4) mult.commute)
   445   apply (simp only: cauchy_mult vanishes_add vanishes_mult_bounded cauchy_imp_bounded simp_thms)
   446   done
   447 
   448 lift_definition inverse_real :: "real \<Rightarrow> real"
   449   is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
   450 proof -
   451   fix X Y
   452   assume "realrel X Y"
   453   then have X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   454     by (simp_all add: realrel_def)
   455   have "vanishes X \<longleftrightarrow> vanishes Y"
   456   proof
   457     assume "vanishes X"
   458     from vanishes_diff [OF this XY] show "vanishes Y"
   459       by simp
   460   next
   461     assume "vanishes Y"
   462     from vanishes_add [OF this XY] show "vanishes X"
   463       by simp
   464   qed
   465   then show "?thesis X Y"
   466     by (simp add: vanishes_diff_inverse X Y XY realrel_def)
   467 qed
   468 
   469 definition "x - y = x + - y" for x y :: real
   470 
   471 definition "x div y = x * inverse y" for x y :: real
   472 
   473 lemma add_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X + Real Y = Real (\<lambda>n. X n + Y n)"
   474   using plus_real.transfer by (simp add: cr_real_eq rel_fun_def)
   475 
   476 lemma minus_Real: "cauchy X \<Longrightarrow> - Real X = Real (\<lambda>n. - X n)"
   477   using uminus_real.transfer by (simp add: cr_real_eq rel_fun_def)
   478 
   479 lemma diff_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X - Real Y = Real (\<lambda>n. X n - Y n)"
   480   by (simp add: minus_Real add_Real minus_real_def)
   481 
   482 lemma mult_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X * Real Y = Real (\<lambda>n. X n * Y n)"
   483   using times_real.transfer by (simp add: cr_real_eq rel_fun_def)
   484 
   485 lemma inverse_Real:
   486   "cauchy X \<Longrightarrow> inverse (Real X) = (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   487   using inverse_real.transfer zero_real.transfer
   488   unfolding cr_real_eq rel_fun_def by (simp split: if_split_asm, metis)
   489 
   490 instance
   491 proof
   492   fix a b c :: real
   493   show "a + b = b + a"
   494     by transfer (simp add: ac_simps realrel_def)
   495   show "(a + b) + c = a + (b + c)"
   496     by transfer (simp add: ac_simps realrel_def)
   497   show "0 + a = a"
   498     by transfer (simp add: realrel_def)
   499   show "- a + a = 0"
   500     by transfer (simp add: realrel_def)
   501   show "a - b = a + - b"
   502     by (rule minus_real_def)
   503   show "(a * b) * c = a * (b * c)"
   504     by transfer (simp add: ac_simps realrel_def)
   505   show "a * b = b * a"
   506     by transfer (simp add: ac_simps realrel_def)
   507   show "1 * a = a"
   508     by transfer (simp add: ac_simps realrel_def)
   509   show "(a + b) * c = a * c + b * c"
   510     by transfer (simp add: distrib_right realrel_def)
   511   show "(0::real) \<noteq> (1::real)"
   512     by transfer (simp add: realrel_def)
   513   show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   514     apply transfer
   515     apply (simp add: realrel_def)
   516     apply (rule vanishesI)
   517     apply (frule (1) cauchy_not_vanishes)
   518     apply clarify
   519     apply (rule_tac x=k in exI)
   520     apply clarify
   521     apply (drule_tac x=n in spec)
   522     apply simp
   523     done
   524   show "a div b = a * inverse b"
   525     by (rule divide_real_def)
   526   show "inverse (0::real) = 0"
   527     by transfer (simp add: realrel_def)
   528 qed
   529 
   530 end
   531 
   532 
   533 subsection \<open>Positive reals\<close>
   534 
   535 lift_definition positive :: "real \<Rightarrow> bool"
   536   is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   537 proof -
   538   have 1: "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n"
   539     if *: "realrel X Y" and **: "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n" for X Y
   540   proof -
   541     from * have XY: "vanishes (\<lambda>n. X n - Y n)"
   542       by (simp_all add: realrel_def)
   543     from ** obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
   544       by blast
   545     obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   546       using \<open>0 < r\<close> by (rule obtain_pos_sum)
   547     obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
   548       using vanishesD [OF XY s] ..
   549     have "\<forall>n\<ge>max i j. t < Y n"
   550     proof clarsimp
   551       fix n
   552       assume n: "i \<le> n" "j \<le> n"
   553       have "\<bar>X n - Y n\<bar> < s" and "r < X n"
   554         using i j n by simp_all
   555       then show "t < Y n" by (simp add: r)
   556     qed
   557     then show ?thesis using t by blast
   558   qed
   559   fix X Y assume "realrel X Y"
   560   then have "realrel X Y" and "realrel Y X"
   561     using symp_realrel by (auto simp: symp_def)
   562   then show "?thesis X Y"
   563     by (safe elim!: 1)
   564 qed
   565 
   566 lemma positive_Real: "cauchy X \<Longrightarrow> positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   567   using positive.transfer by (simp add: cr_real_eq rel_fun_def)
   568 
   569 lemma positive_zero: "\<not> positive 0"
   570   by transfer auto
   571 
   572 lemma positive_add: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
   573   apply transfer
   574   apply clarify
   575   apply (rename_tac a b i j)
   576   apply (rule_tac x = "a + b" in exI)
   577   apply simp
   578   apply (rule_tac x = "max i j" in exI)
   579   apply clarsimp
   580   apply (simp add: add_strict_mono)
   581   done
   582 
   583 lemma positive_mult: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
   584   apply transfer
   585   apply clarify
   586   apply (rename_tac a b i j)
   587   apply (rule_tac x = "a * b" in exI)
   588   apply simp
   589   apply (rule_tac x = "max i j" in exI)
   590   apply clarsimp
   591   apply (rule mult_strict_mono)
   592      apply auto
   593   done
   594 
   595 lemma positive_minus: "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
   596   apply transfer
   597   apply (simp add: realrel_def)
   598   apply (drule (1) cauchy_not_vanishes_cases)
   599   apply safe
   600    apply blast+
   601   done
   602 
   603 instantiation real :: linordered_field
   604 begin
   605 
   606 definition "x < y \<longleftrightarrow> positive (y - x)"
   607 
   608 definition "x \<le> y \<longleftrightarrow> x < y \<or> x = y" for x y :: real
   609 
   610 definition "\<bar>a\<bar> = (if a < 0 then - a else a)" for a :: real
   611 
   612 definition "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)" for a :: real
   613 
   614 instance
   615 proof
   616   fix a b c :: real
   617   show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   618     by (rule abs_real_def)
   619   show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   620     unfolding less_eq_real_def less_real_def
   621     apply auto
   622      apply (drule (1) positive_add)
   623      apply (simp_all add: positive_zero)
   624     done
   625   show "a \<le> a"
   626     unfolding less_eq_real_def by simp
   627   show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   628     unfolding less_eq_real_def less_real_def
   629     apply auto
   630     apply (drule (1) positive_add)
   631     apply (simp add: algebra_simps)
   632     done
   633   show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   634     unfolding less_eq_real_def less_real_def
   635     apply auto
   636     apply (drule (1) positive_add)
   637     apply (simp add: positive_zero)
   638     done
   639   show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   640     by (auto simp: less_eq_real_def less_real_def)
   641     (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
   642     (* Should produce c + b - (c + a) \<equiv> b - a *)
   643   show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   644     by (rule sgn_real_def)
   645   show "a \<le> b \<or> b \<le> a"
   646     by (auto dest!: positive_minus simp: less_eq_real_def less_real_def)
   647   show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   648     unfolding less_real_def
   649     apply (drule (1) positive_mult)
   650     apply (simp add: algebra_simps)
   651     done
   652 qed
   653 
   654 end
   655 
   656 instantiation real :: distrib_lattice
   657 begin
   658 
   659 definition "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
   660 
   661 definition "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
   662 
   663 instance
   664   by standard (auto simp add: inf_real_def sup_real_def max_min_distrib2)
   665 
   666 end
   667 
   668 lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   669   by (induct x) (simp_all add: zero_real_def one_real_def add_Real)
   670 
   671 lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
   672   by (cases x rule: int_diff_cases) (simp add: of_nat_Real diff_Real)
   673 
   674 lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
   675   apply (induct x)
   676   apply (simp add: Fract_of_int_quotient of_rat_divide)
   677   apply (simp add: of_int_Real divide_inverse)
   678   apply (simp add: inverse_Real mult_Real)
   679   done
   680 
   681 instance real :: archimedean_field
   682 proof
   683   show "\<exists>z. x \<le> of_int z" for x :: real
   684     apply (induct x)
   685     apply (frule cauchy_imp_bounded, clarify)
   686     apply (rule_tac x="\<lceil>b\<rceil> + 1" in exI)
   687     apply (rule less_imp_le)
   688     apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
   689     apply (rule_tac x=1 in exI)
   690     apply (simp add: algebra_simps)
   691     apply (rule_tac x=0 in exI)
   692     apply clarsimp
   693     apply (rule le_less_trans [OF abs_ge_self])
   694     apply (rule less_le_trans [OF _ le_of_int_ceiling])
   695     apply simp
   696     done
   697 qed
   698 
   699 instantiation real :: floor_ceiling
   700 begin
   701 
   702 definition [code del]: "\<lfloor>x::real\<rfloor> = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
   703 
   704 instance
   705 proof
   706   show "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)" for x :: real
   707     unfolding floor_real_def using floor_exists1 by (rule theI')
   708 qed
   709 
   710 end
   711 
   712 
   713 subsection \<open>Completeness\<close>
   714 
   715 lemma not_positive_Real: "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)" if "cauchy X"
   716   apply (simp only: positive_Real [OF that])
   717   apply auto
   718    apply (unfold not_less)
   719    apply (erule obtain_pos_sum)
   720    apply (drule_tac x=s in spec)
   721    apply simp
   722    apply (drule_tac r=t in cauchyD [OF that])
   723    apply clarify
   724    apply (drule_tac x=k in spec)
   725    apply clarsimp
   726    apply (rule_tac x=n in exI)
   727    apply clarify
   728    apply (rename_tac m)
   729    apply (drule_tac x=m in spec)
   730    apply simp
   731    apply (drule_tac x=n in spec)
   732    apply simp
   733   apply (drule spec)
   734   apply (drule (1) mp)
   735   apply clarify
   736   apply (rename_tac i)
   737   apply (rule_tac x = "max i k" in exI)
   738   apply simp
   739   done
   740 
   741 lemma le_Real:
   742   assumes "cauchy X" "cauchy Y"
   743   shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   744   unfolding not_less [symmetric, where 'a=real] less_real_def
   745   apply (simp add: diff_Real not_positive_Real assms)
   746   apply (simp add: diff_le_eq ac_simps)
   747   done
   748 
   749 lemma le_RealI:
   750   assumes Y: "cauchy Y"
   751   shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   752 proof (induct x)
   753   fix X
   754   assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   755   then have le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   756     by (simp add: of_rat_Real le_Real)
   757   then have "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" if "0 < r" for r :: rat
   758   proof -
   759     from that obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   760       by (rule obtain_pos_sum)
   761     obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   762       using cauchyD [OF Y s] ..
   763     obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   764       using le [OF t] ..
   765     have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   766     proof clarsimp
   767       fix n
   768       assume n: "i \<le> n" "j \<le> n"
   769       have "X n \<le> Y i + t"
   770         using n j by simp
   771       moreover have "\<bar>Y i - Y n\<bar> < s"
   772         using n i by simp
   773       ultimately show "X n \<le> Y n + r"
   774         unfolding r by simp
   775     qed
   776     then show ?thesis ..
   777   qed
   778   then show "Real X \<le> Real Y"
   779     by (simp add: of_rat_Real le_Real X Y)
   780 qed
   781 
   782 lemma Real_leI:
   783   assumes X: "cauchy X"
   784   assumes le: "\<forall>n. of_rat (X n) \<le> y"
   785   shows "Real X \<le> y"
   786 proof -
   787   have "- y \<le> - Real X"
   788     by (simp add: minus_Real X le_RealI of_rat_minus le)
   789   then show ?thesis by simp
   790 qed
   791 
   792 lemma less_RealD:
   793   assumes "cauchy Y"
   794   shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   795   apply (erule contrapos_pp)
   796   apply (simp add: not_less)
   797   apply (erule Real_leI [OF assms])
   798   done
   799 
   800 lemma of_nat_less_two_power [simp]: "of_nat n < (2::'a::linordered_idom) ^ n"
   801   apply (induct n)
   802    apply simp
   803   apply (metis add_le_less_mono mult_2 of_nat_Suc one_le_numeral one_le_power power_Suc)
   804   done
   805 
   806 lemma complete_real:
   807   fixes S :: "real set"
   808   assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   809   shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   810 proof -
   811   obtain x where x: "x \<in> S" using assms(1) ..
   812   obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   813 
   814   define P where "P x \<longleftrightarrow> (\<forall>y\<in>S. y \<le> of_rat x)" for x
   815   obtain a where a: "\<not> P a"
   816   proof
   817     have "of_int \<lfloor>x - 1\<rfloor> \<le> x - 1" by (rule of_int_floor_le)
   818     also have "x - 1 < x" by simp
   819     finally have "of_int \<lfloor>x - 1\<rfloor> < x" .
   820     then have "\<not> x \<le> of_int \<lfloor>x - 1\<rfloor>" by (simp only: not_le)
   821     then show "\<not> P (of_int \<lfloor>x - 1\<rfloor>)"
   822       unfolding P_def of_rat_of_int_eq using x by blast
   823   qed
   824   obtain b where b: "P b"
   825   proof
   826     show "P (of_int \<lceil>z\<rceil>)"
   827     unfolding P_def of_rat_of_int_eq
   828     proof
   829       fix y assume "y \<in> S"
   830       then have "y \<le> z" using z by simp
   831       also have "z \<le> of_int \<lceil>z\<rceil>" by (rule le_of_int_ceiling)
   832       finally show "y \<le> of_int \<lceil>z\<rceil>" .
   833     qed
   834   qed
   835 
   836   define avg where "avg x y = x/2 + y/2" for x y :: rat
   837   define bisect where "bisect = (\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y))"
   838   define A where "A n = fst ((bisect ^^ n) (a, b))" for n
   839   define B where "B n = snd ((bisect ^^ n) (a, b))" for n
   840   define C where "C n = avg (A n) (B n)" for n
   841   have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   842   have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   843   have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   844     unfolding A_def B_def C_def bisect_def split_def by simp
   845   have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   846     unfolding A_def B_def C_def bisect_def split_def by simp
   847 
   848   have width: "B n - A n = (b - a) / 2^n" for n
   849     apply (induct n)
   850      apply (simp_all add: eq_divide_eq)
   851     apply (simp_all add: C_def avg_def algebra_simps)
   852     done
   853 
   854   have twos: "0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r" for y r :: rat
   855     apply (simp add: divide_less_eq)
   856     apply (subst mult.commute)
   857     apply (frule_tac y=y in ex_less_of_nat_mult)
   858     apply clarify
   859     apply (rule_tac x=n in exI)
   860     apply (erule less_trans)
   861     apply (rule mult_strict_right_mono)
   862      apply (rule le_less_trans [OF _ of_nat_less_two_power])
   863      apply simp
   864     apply assumption
   865     done
   866 
   867   have PA: "\<not> P (A n)" for n
   868     by (induct n) (simp_all add: a)
   869   have PB: "P (B n)" for n
   870     by (induct n) (simp_all add: b)
   871   have ab: "a < b"
   872     using a b unfolding P_def
   873     apply (clarsimp simp add: not_le)
   874     apply (drule (1) bspec)
   875     apply (drule (1) less_le_trans)
   876     apply (simp add: of_rat_less)
   877     done
   878   have AB: "A n < B n" for n
   879     by (induct n) (simp_all add: ab C_def avg_def)
   880   have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   881     apply (auto simp add: le_less [where 'a=nat])
   882     apply (erule less_Suc_induct)
   883      apply (clarsimp simp add: C_def avg_def)
   884      apply (simp add: add_divide_distrib [symmetric])
   885      apply (rule AB [THEN less_imp_le])
   886     apply simp
   887     done
   888   have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   889     apply (auto simp add: le_less [where 'a=nat])
   890     apply (erule less_Suc_induct)
   891      apply (clarsimp simp add: C_def avg_def)
   892      apply (simp add: add_divide_distrib [symmetric])
   893      apply (rule AB [THEN less_imp_le])
   894     apply simp
   895     done
   896   have cauchy_lemma: "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   897     apply (rule cauchyI)
   898     apply (drule twos [where y="b - a"])
   899     apply (erule exE)
   900     apply (rule_tac x=n in exI, clarify, rename_tac i j)
   901     apply (rule_tac y="B n - A n" in le_less_trans) defer
   902      apply (simp add: width)
   903     apply (drule_tac x=n in spec)
   904     apply (frule_tac x=i in spec, drule (1) mp)
   905     apply (frule_tac x=j in spec, drule (1) mp)
   906     apply (frule A_mono, drule B_mono)
   907     apply (frule A_mono, drule B_mono)
   908     apply arith
   909     done
   910   have "cauchy A"
   911     apply (rule cauchy_lemma [rule_format])
   912     apply (simp add: A_mono)
   913     apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   914     done
   915   have "cauchy B"
   916     apply (rule cauchy_lemma [rule_format])
   917     apply (simp add: B_mono)
   918     apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
   919     done
   920   have 1: "\<forall>x\<in>S. x \<le> Real B"
   921   proof
   922     fix x
   923     assume "x \<in> S"
   924     then show "x \<le> Real B"
   925       using PB [unfolded P_def] \<open>cauchy B\<close>
   926       by (simp add: le_RealI)
   927   qed
   928   have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
   929     apply clarify
   930     apply (erule contrapos_pp)
   931     apply (simp add: not_le)
   932     apply (drule less_RealD [OF \<open>cauchy A\<close>])
   933     apply clarify
   934     apply (subgoal_tac "\<not> P (A n)")
   935      apply (simp add: P_def not_le)
   936      apply clarify
   937      apply (erule rev_bexI)
   938      apply (erule (1) less_trans)
   939     apply (simp add: PA)
   940     done
   941   have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
   942   proof (rule vanishesI)
   943     fix r :: rat
   944     assume "0 < r"
   945     then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
   946       using twos by blast
   947     have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
   948     proof clarify
   949       fix n
   950       assume n: "k \<le> n"
   951       have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
   952         by simp
   953       also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
   954         using n by (simp add: divide_left_mono)
   955       also note k
   956       finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
   957     qed
   958     then show "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
   959   qed
   960   then have 3: "Real B = Real A"
   961     by (simp add: eq_Real \<open>cauchy A\<close> \<open>cauchy B\<close> width)
   962   show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   963     apply (rule exI [where x = "Real B"])
   964     using 1 2 3
   965     apply simp
   966     done
   967 qed
   968 
   969 instantiation real :: linear_continuum
   970 begin
   971 
   972 subsection \<open>Supremum of a set of reals\<close>
   973 
   974 definition "Sup X = (LEAST z::real. \<forall>x\<in>X. x \<le> z)"
   975 definition "Inf X = - Sup (uminus ` X)" for X :: "real set"
   976 
   977 instance
   978 proof
   979   show Sup_upper: "x \<le> Sup X"
   980     if "x \<in> X" "bdd_above X"
   981     for x :: real and X :: "real set"
   982   proof -
   983     from that obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
   984       using complete_real[of X] unfolding bdd_above_def by blast
   985     then show ?thesis
   986       unfolding Sup_real_def by (rule LeastI2_order) (auto simp: that)
   987   qed
   988   show Sup_least: "Sup X \<le> z"
   989     if "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
   990     for z :: real and X :: "real set"
   991   proof -
   992     from that obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
   993       using complete_real [of X] by blast
   994     then have "Sup X = s"
   995       unfolding Sup_real_def by (best intro: Least_equality)
   996     also from s z have "\<dots> \<le> z"
   997       by blast
   998     finally show ?thesis .
   999   qed
  1000   show "Inf X \<le> x" if "x \<in> X" "bdd_below X"
  1001     for x :: real and X :: "real set"
  1002     using Sup_upper [of "-x" "uminus ` X"] by (auto simp: Inf_real_def that)
  1003   show "z \<le> Inf X" if "X \<noteq> {}" "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
  1004     for z :: real and X :: "real set"
  1005     using Sup_least [of "uminus ` X" "- z"] by (force simp: Inf_real_def that)
  1006   show "\<exists>a b::real. a \<noteq> b"
  1007     using zero_neq_one by blast
  1008 qed
  1009 
  1010 end
  1011 
  1012 
  1013 subsection \<open>Hiding implementation details\<close>
  1014 
  1015 hide_const (open) vanishes cauchy positive Real
  1016 
  1017 declare Real_induct [induct del]
  1018 declare Abs_real_induct [induct del]
  1019 declare Abs_real_cases [cases del]
  1020 
  1021 lifting_update real.lifting
  1022 lifting_forget real.lifting
  1023 
  1024 
  1025 subsection \<open>More Lemmas\<close>
  1026 
  1027 text \<open>BH: These lemmas should not be necessary; they should be
  1028   covered by existing simp rules and simplification procedures.\<close>
  1029 
  1030 lemma real_mult_less_iff1 [simp]: "0 < z \<Longrightarrow> x * z < y * z \<longleftrightarrow> x < y"
  1031   for x y z :: real
  1032   by simp (* solved by linordered_ring_less_cancel_factor simproc *)
  1033 
  1034 lemma real_mult_le_cancel_iff1 [simp]: "0 < z \<Longrightarrow> x * z \<le> y * z \<longleftrightarrow> x \<le> y"
  1035   for x y z :: real
  1036   by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1037 
  1038 lemma real_mult_le_cancel_iff2 [simp]: "0 < z \<Longrightarrow> z * x \<le> z * y \<longleftrightarrow> x \<le> y"
  1039   for x y z :: real
  1040   by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1041 
  1042 
  1043 subsection \<open>Embedding numbers into the Reals\<close>
  1044 
  1045 abbreviation real_of_nat :: "nat \<Rightarrow> real"
  1046   where "real_of_nat \<equiv> of_nat"
  1047 
  1048 abbreviation real :: "nat \<Rightarrow> real"
  1049   where "real \<equiv> of_nat"
  1050 
  1051 abbreviation real_of_int :: "int \<Rightarrow> real"
  1052   where "real_of_int \<equiv> of_int"
  1053 
  1054 abbreviation real_of_rat :: "rat \<Rightarrow> real"
  1055   where "real_of_rat \<equiv> of_rat"
  1056 
  1057 declare [[coercion_enabled]]
  1058 
  1059 declare [[coercion "of_nat :: nat \<Rightarrow> int"]]
  1060 declare [[coercion "of_nat :: nat \<Rightarrow> real"]]
  1061 declare [[coercion "of_int :: int \<Rightarrow> real"]]
  1062 
  1063 (* We do not add rat to the coerced types, this has often unpleasant side effects when writing
  1064 inverse (Suc n) which sometimes gets two coercions: of_rat (inverse (of_nat (Suc n))) *)
  1065 
  1066 declare [[coercion_map map]]
  1067 declare [[coercion_map "\<lambda>f g h x. g (h (f x))"]]
  1068 declare [[coercion_map "\<lambda>f g (x,y). (f x, g y)"]]
  1069 
  1070 declare of_int_eq_0_iff [algebra, presburger]
  1071 declare of_int_eq_1_iff [algebra, presburger]
  1072 declare of_int_eq_iff [algebra, presburger]
  1073 declare of_int_less_0_iff [algebra, presburger]
  1074 declare of_int_less_1_iff [algebra, presburger]
  1075 declare of_int_less_iff [algebra, presburger]
  1076 declare of_int_le_0_iff [algebra, presburger]
  1077 declare of_int_le_1_iff [algebra, presburger]
  1078 declare of_int_le_iff [algebra, presburger]
  1079 declare of_int_0_less_iff [algebra, presburger]
  1080 declare of_int_0_le_iff [algebra, presburger]
  1081 declare of_int_1_less_iff [algebra, presburger]
  1082 declare of_int_1_le_iff [algebra, presburger]
  1083 
  1084 lemma int_less_real_le: "n < m \<longleftrightarrow> real_of_int n + 1 \<le> real_of_int m"
  1085 proof -
  1086   have "(0::real) \<le> 1"
  1087     by (metis less_eq_real_def zero_less_one)
  1088   then show ?thesis
  1089     by (metis floor_of_int less_floor_iff)
  1090 qed
  1091 
  1092 lemma int_le_real_less: "n \<le> m \<longleftrightarrow> real_of_int n < real_of_int m + 1"
  1093   by (meson int_less_real_le not_le)
  1094 
  1095 lemma real_of_int_div_aux:
  1096   "(real_of_int x) / (real_of_int d) =
  1097     real_of_int (x div d) + (real_of_int (x mod d)) / (real_of_int d)"
  1098 proof -
  1099   have "x = (x div d) * d + x mod d"
  1100     by auto
  1101   then have "real_of_int x = real_of_int (x div d) * real_of_int d + real_of_int(x mod d)"
  1102     by (metis of_int_add of_int_mult)
  1103   then have "real_of_int x / real_of_int d = \<dots> / real_of_int d"
  1104     by simp
  1105   then show ?thesis
  1106     by (auto simp add: add_divide_distrib algebra_simps)
  1107 qed
  1108 
  1109 lemma real_of_int_div:
  1110   "d dvd n \<Longrightarrow> real_of_int (n div d) = real_of_int n / real_of_int d" for d n :: int
  1111   by (simp add: real_of_int_div_aux)
  1112 
  1113 lemma real_of_int_div2: "0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)"
  1114   apply (cases "x = 0")
  1115    apply simp
  1116   apply (cases "0 < x")
  1117    apply (metis add.left_neutral floor_correct floor_divide_of_int_eq le_diff_eq)
  1118   apply (metis add.left_neutral floor_correct floor_divide_of_int_eq le_diff_eq)
  1119   done
  1120 
  1121 lemma real_of_int_div3: "real_of_int n / real_of_int x - real_of_int (n div x) \<le> 1"
  1122   apply (simp add: algebra_simps)
  1123   apply (subst real_of_int_div_aux)
  1124   apply (auto simp add: divide_le_eq intro: order_less_imp_le)
  1125   done
  1126 
  1127 lemma real_of_int_div4: "real_of_int (n div x) \<le> real_of_int n / real_of_int x"
  1128   using real_of_int_div2 [of n x] by simp
  1129 
  1130 
  1131 subsection \<open>Embedding the Naturals into the Reals\<close>
  1132 
  1133 lemma real_of_card: "real (card A) = sum (\<lambda>x. 1) A"
  1134   by simp
  1135 
  1136 lemma nat_less_real_le: "n < m \<longleftrightarrow> real n + 1 \<le> real m"
  1137   by (metis discrete of_nat_1 of_nat_add of_nat_le_iff)
  1138 
  1139 lemma nat_le_real_less: "n \<le> m \<longleftrightarrow> real n < real m + 1"
  1140   for m n :: nat
  1141   by (meson nat_less_real_le not_le)
  1142 
  1143 lemma real_of_nat_div_aux: "real x / real d = real (x div d) + real (x mod d) / real d"
  1144 proof -
  1145   have "x = (x div d) * d + x mod d"
  1146     by auto
  1147   then have "real x = real (x div d) * real d + real(x mod d)"
  1148     by (metis of_nat_add of_nat_mult)
  1149   then have "real x / real d = \<dots> / real d"
  1150     by simp
  1151   then show ?thesis
  1152     by (auto simp add: add_divide_distrib algebra_simps)
  1153 qed
  1154 
  1155 lemma real_of_nat_div: "d dvd n \<Longrightarrow> real(n div d) = real n / real d"
  1156   by (subst real_of_nat_div_aux) (auto simp add: dvd_eq_mod_eq_0 [symmetric])
  1157 
  1158 lemma real_of_nat_div2: "0 \<le> real n / real x - real (n div x)" for n x :: nat
  1159   apply (simp add: algebra_simps)
  1160   apply (subst real_of_nat_div_aux)
  1161   apply simp
  1162   done
  1163 
  1164 lemma real_of_nat_div3: "real n / real x - real (n div x) \<le> 1" for n x :: nat
  1165   apply (cases "x = 0")
  1166    apply simp
  1167   apply (simp add: algebra_simps)
  1168   apply (subst real_of_nat_div_aux)
  1169   apply simp
  1170   done
  1171 
  1172 lemma real_of_nat_div4: "real (n div x) \<le> real n / real x" for n x :: nat
  1173   using real_of_nat_div2 [of n x] by simp
  1174 
  1175 
  1176 subsection \<open>The Archimedean Property of the Reals\<close>
  1177 
  1178 lemma real_arch_inverse: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  1179   using reals_Archimedean[of e] less_trans[of 0 "1 / real n" e for n::nat]
  1180   by (auto simp add: field_simps cong: conj_cong simp del: of_nat_Suc)
  1181 
  1182 lemma reals_Archimedean3: "0 < x \<Longrightarrow> \<forall>y. \<exists>n. y < real n * x"
  1183   by (auto intro: ex_less_of_nat_mult)
  1184 
  1185 lemma real_archimedian_rdiv_eq_0:
  1186   assumes x0: "x \<ge> 0"
  1187     and c: "c \<ge> 0"
  1188     and xc: "\<And>m::nat. m > 0 \<Longrightarrow> real m * x \<le> c"
  1189   shows "x = 0"
  1190   by (metis reals_Archimedean3 dual_order.order_iff_strict le0 le_less_trans not_le x0 xc)
  1191 
  1192 
  1193 subsection \<open>Rationals\<close>
  1194 
  1195 lemma Rats_eq_int_div_int: "\<rat> = {real_of_int i / real_of_int j | i j. j \<noteq> 0}"  (is "_ = ?S")
  1196 proof
  1197   show "\<rat> \<subseteq> ?S"
  1198   proof
  1199     fix x :: real
  1200     assume "x \<in> \<rat>"
  1201     then obtain r where "x = of_rat r"
  1202       unfolding Rats_def ..
  1203     have "of_rat r \<in> ?S"
  1204       by (cases r) (auto simp add: of_rat_rat)
  1205     then show "x \<in> ?S"
  1206       using \<open>x = of_rat r\<close> by simp
  1207   qed
  1208 next
  1209   show "?S \<subseteq> \<rat>"
  1210   proof (auto simp: Rats_def)
  1211     fix i j :: int
  1212     assume "j \<noteq> 0"
  1213     then have "real_of_int i / real_of_int j = of_rat (Fract i j)"
  1214       by (simp add: of_rat_rat)
  1215     then show "real_of_int i / real_of_int j \<in> range of_rat"
  1216       by blast
  1217   qed
  1218 qed
  1219 
  1220 lemma Rats_eq_int_div_nat: "\<rat> = { real_of_int i / real n | i n. n \<noteq> 0}"
  1221 proof (auto simp: Rats_eq_int_div_int)
  1222   fix i j :: int
  1223   assume "j \<noteq> 0"
  1224   show "\<exists>(i'::int) (n::nat). real_of_int i / real_of_int j = real_of_int i' / real n \<and> 0 < n"
  1225   proof (cases "j > 0")
  1226     case True
  1227     then have "real_of_int i / real_of_int j = real_of_int i / real (nat j) \<and> 0 < nat j"
  1228       by simp
  1229     then show ?thesis by blast
  1230   next
  1231     case False
  1232     with \<open>j \<noteq> 0\<close>
  1233     have "real_of_int i / real_of_int j = real_of_int (- i) / real (nat (- j)) \<and> 0 < nat (- j)"
  1234       by simp
  1235     then show ?thesis by blast
  1236   qed
  1237 next
  1238   fix i :: int and n :: nat
  1239   assume "0 < n"
  1240   then have "real_of_int i / real n = real_of_int i / real_of_int(int n) \<and> int n \<noteq> 0"
  1241     by simp
  1242   then show "\<exists>i' j. real_of_int i / real n = real_of_int i' / real_of_int j \<and> j \<noteq> 0"
  1243     by blast
  1244 qed
  1245 
  1246 lemma Rats_abs_nat_div_natE:
  1247   assumes "x \<in> \<rat>"
  1248   obtains m n :: nat where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1249 proof -
  1250   from \<open>x \<in> \<rat>\<close> obtain i :: int and n :: nat where "n \<noteq> 0" and "x = real_of_int i / real n"
  1251     by (auto simp add: Rats_eq_int_div_nat)
  1252   then have "\<bar>x\<bar> = real (nat \<bar>i\<bar>) / real n" by simp
  1253   then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1254   let ?gcd = "gcd m n"
  1255   from \<open>n \<noteq> 0\<close> have gcd: "?gcd \<noteq> 0" by simp
  1256   let ?k = "m div ?gcd"
  1257   let ?l = "n div ?gcd"
  1258   let ?gcd' = "gcd ?k ?l"
  1259   have "?gcd dvd m" ..
  1260   then have gcd_k: "?gcd * ?k = m"
  1261     by (rule dvd_mult_div_cancel)
  1262   have "?gcd dvd n" ..
  1263   then have gcd_l: "?gcd * ?l = n"
  1264     by (rule dvd_mult_div_cancel)
  1265   from \<open>n \<noteq> 0\<close> and gcd_l have "?gcd * ?l \<noteq> 0" by simp
  1266   then have "?l \<noteq> 0" by (blast dest!: mult_not_zero)
  1267   moreover
  1268   have "\<bar>x\<bar> = real ?k / real ?l"
  1269   proof -
  1270     from gcd have "real ?k / real ?l = real (?gcd * ?k) / real (?gcd * ?l)"
  1271       by (simp add: real_of_nat_div)
  1272     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1273     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1274     finally show ?thesis ..
  1275   qed
  1276   moreover
  1277   have "?gcd' = 1"
  1278   proof -
  1279     have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1280       by (rule gcd_mult_distrib_nat)
  1281     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1282     with gcd show ?thesis by auto
  1283   qed
  1284   ultimately show ?thesis ..
  1285 qed
  1286 
  1287 
  1288 subsection \<open>Density of the Rational Reals in the Reals\<close>
  1289 
  1290 text \<open>
  1291   This density proof is due to Stefan Richter and was ported by TN.  The
  1292   original source is \<^emph>\<open>Real Analysis\<close> by H.L. Royden.
  1293   It employs the Archimedean property of the reals.\<close>
  1294 
  1295 lemma Rats_dense_in_real:
  1296   fixes x :: real
  1297   assumes "x < y"
  1298   shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
  1299 proof -
  1300   from \<open>x < y\<close> have "0 < y - x" by simp
  1301   with reals_Archimedean obtain q :: nat where q: "inverse (real q) < y - x" and "0 < q"
  1302     by blast
  1303   define p where "p = \<lceil>y * real q\<rceil> - 1"
  1304   define r where "r = of_int p / real q"
  1305   from q have "x < y - inverse (real q)"
  1306     by simp
  1307   also from \<open>0 < q\<close> have "y - inverse (real q) \<le> r"
  1308     by (simp add: r_def p_def le_divide_eq left_diff_distrib)
  1309   finally have "x < r" .
  1310   moreover from \<open>0 < q\<close> have "r < y"
  1311     by (simp add: r_def p_def divide_less_eq diff_less_eq less_ceiling_iff [symmetric])
  1312   moreover have "r \<in> \<rat>"
  1313     by (simp add: r_def)
  1314   ultimately show ?thesis by blast
  1315 qed
  1316 
  1317 lemma of_rat_dense:
  1318   fixes x y :: real
  1319   assumes "x < y"
  1320   shows "\<exists>q :: rat. x < of_rat q \<and> of_rat q < y"
  1321   using Rats_dense_in_real [OF \<open>x < y\<close>]
  1322   by (auto elim: Rats_cases)
  1323 
  1324 
  1325 subsection \<open>Numerals and Arithmetic\<close>
  1326 
  1327 declaration \<open>
  1328   K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
  1329     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
  1330   #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
  1331     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
  1332   #> Lin_Arith.add_simps [@{thm of_nat_0}, @{thm of_nat_Suc}, @{thm of_nat_add},
  1333       @{thm of_nat_mult}, @{thm of_int_0}, @{thm of_int_1},
  1334       @{thm of_int_add}, @{thm of_int_minus}, @{thm of_int_diff},
  1335       @{thm of_int_mult}, @{thm of_int_of_nat_eq},
  1336       @{thm of_nat_numeral}, @{thm of_nat_numeral}, @{thm of_int_neg_numeral}]
  1337   #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat \<Rightarrow> real"})
  1338   #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int \<Rightarrow> real"}))
  1339 \<close>
  1340 
  1341 
  1342 subsection \<open>Simprules combining \<open>x + y\<close> and \<open>0\<close>\<close> (* FIXME ARE THEY NEEDED? *)
  1343 
  1344 lemma real_add_minus_iff [simp]: "x + - a = 0 \<longleftrightarrow> x = a"
  1345   for x a :: real
  1346   by arith
  1347 
  1348 lemma real_add_less_0_iff: "x + y < 0 \<longleftrightarrow> y < - x"
  1349   for x y :: real
  1350   by auto
  1351 
  1352 lemma real_0_less_add_iff: "0 < x + y \<longleftrightarrow> - x < y"
  1353   for x y :: real
  1354   by auto
  1355 
  1356 lemma real_add_le_0_iff: "x + y \<le> 0 \<longleftrightarrow> y \<le> - x"
  1357   for x y :: real
  1358   by auto
  1359 
  1360 lemma real_0_le_add_iff: "0 \<le> x + y \<longleftrightarrow> - x \<le> y"
  1361   for x y :: real
  1362   by auto
  1363 
  1364 
  1365 subsection \<open>Lemmas about powers\<close>
  1366 
  1367 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1368   by simp
  1369 
  1370 (* FIXME: declare this [simp] for all types, or not at all *)
  1371 declare sum_squares_eq_zero_iff [simp] sum_power2_eq_zero_iff [simp]
  1372 
  1373 lemma real_minus_mult_self_le [simp]: "- (u * u) \<le> x * x"
  1374   for u x :: real
  1375   by (rule order_trans [where y = 0]) auto
  1376 
  1377 lemma realpow_square_minus_le [simp]: "- u\<^sup>2 \<le> x\<^sup>2"
  1378   for u x :: real
  1379   by (auto simp add: power2_eq_square)
  1380 
  1381 
  1382 subsection \<open>Density of the Reals\<close>
  1383 
  1384 lemma real_lbound_gt_zero: "0 < d1 \<Longrightarrow> 0 < d2 \<Longrightarrow> \<exists>e. 0 < e \<and> e < d1 \<and> e < d2"
  1385   for d1 d2 :: real
  1386   by (rule exI [where x = "min d1 d2 / 2"]) (simp add: min_def)
  1387 
  1388 text \<open>Similar results are proved in @{theory Fields}\<close>
  1389 lemma real_less_half_sum: "x < y \<Longrightarrow> x < (x + y) / 2"
  1390   for x y :: real
  1391   by auto
  1392 
  1393 lemma real_gt_half_sum: "x < y \<Longrightarrow> (x + y) / 2 < y"
  1394   for x y :: real
  1395   by auto
  1396 
  1397 lemma real_sum_of_halves: "x / 2 + x / 2 = x"
  1398   for x :: real
  1399   by simp
  1400 
  1401 
  1402 subsection \<open>Floor and Ceiling Functions from the Reals to the Integers\<close>
  1403 
  1404 (* FIXME: theorems for negative numerals. Many duplicates, e.g. from Archimedean_Field.thy. *)
  1405 
  1406 lemma real_of_nat_less_numeral_iff [simp]: "real n < numeral w \<longleftrightarrow> n < numeral w"
  1407   for n :: nat
  1408   by (metis of_nat_less_iff of_nat_numeral)
  1409 
  1410 lemma numeral_less_real_of_nat_iff [simp]: "numeral w < real n \<longleftrightarrow> numeral w < n"
  1411   for n :: nat
  1412   by (metis of_nat_less_iff of_nat_numeral)
  1413 
  1414 lemma numeral_le_real_of_nat_iff [simp]: "numeral n \<le> real m \<longleftrightarrow> numeral n \<le> m"
  1415   for m :: nat
  1416   by (metis not_le real_of_nat_less_numeral_iff)
  1417 
  1418 declare of_int_floor_le [simp]  (* FIXME duplicate!? *)
  1419 
  1420 lemma of_int_floor_cancel [simp]: "of_int \<lfloor>x\<rfloor> = x \<longleftrightarrow> (\<exists>n::int. x = of_int n)"
  1421   by (metis floor_of_int)
  1422 
  1423 lemma floor_eq: "real_of_int n < x \<Longrightarrow> x < real_of_int n + 1 \<Longrightarrow> \<lfloor>x\<rfloor> = n"
  1424   by linarith
  1425 
  1426 lemma floor_eq2: "real_of_int n \<le> x \<Longrightarrow> x < real_of_int n + 1 \<Longrightarrow> \<lfloor>x\<rfloor> = n"
  1427   by linarith
  1428 
  1429 lemma floor_eq3: "real n < x \<Longrightarrow> x < real (Suc n) \<Longrightarrow> nat \<lfloor>x\<rfloor> = n"
  1430   by linarith
  1431 
  1432 lemma floor_eq4: "real n \<le> x \<Longrightarrow> x < real (Suc n) \<Longrightarrow> nat \<lfloor>x\<rfloor> = n"
  1433   by linarith
  1434 
  1435 lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real_of_int \<lfloor>r\<rfloor>"
  1436   by linarith
  1437 
  1438 lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real_of_int \<lfloor>r\<rfloor>"
  1439   by linarith
  1440 
  1441 lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real_of_int \<lfloor>r\<rfloor> + 1"
  1442   by linarith
  1443 
  1444 lemma real_of_int_floor_add_one_gt [simp]: "r < real_of_int \<lfloor>r\<rfloor> + 1"
  1445   by linarith
  1446 
  1447 lemma floor_divide_real_eq_div:
  1448   assumes "0 \<le> b"
  1449   shows "\<lfloor>a / real_of_int b\<rfloor> = \<lfloor>a\<rfloor> div b"
  1450 proof (cases "b = 0")
  1451   case True
  1452   then show ?thesis by simp
  1453 next
  1454   case False
  1455   with assms have b: "b > 0" by simp
  1456   have "j = i div b"
  1457     if "real_of_int i \<le> a" "a < 1 + real_of_int i"
  1458       "real_of_int j * real_of_int b \<le> a" "a < real_of_int b + real_of_int j * real_of_int b"
  1459     for i j :: int
  1460   proof -
  1461     from that have "i < b + j * b"
  1462       by (metis le_less_trans of_int_add of_int_less_iff of_int_mult)
  1463     moreover have "j * b < 1 + i"
  1464     proof -
  1465       have "real_of_int (j * b) < real_of_int i + 1"
  1466         using \<open>a < 1 + real_of_int i\<close> \<open>real_of_int j * real_of_int b \<le> a\<close> by force
  1467       then show "j * b < 1 + i" by linarith
  1468     qed
  1469     ultimately have "(j - i div b) * b \<le> i mod b" "i mod b < ((j - i div b) + 1) * b"
  1470       by (auto simp: field_simps)
  1471     then have "(j - i div b) * b < 1 * b" "0 * b < ((j - i div b) + 1) * b"
  1472       using pos_mod_bound [OF b, of i] pos_mod_sign [OF b, of i]
  1473       by linarith+
  1474     then show ?thesis using b unfolding mult_less_cancel_right by auto
  1475   qed
  1476   with b show ?thesis by (auto split: floor_split simp: field_simps)
  1477 qed
  1478 
  1479 lemma floor_one_divide_eq_div_numeral [simp]:
  1480   "\<lfloor>1 / numeral b::real\<rfloor> = 1 div numeral b"
  1481 by (metis floor_divide_of_int_eq of_int_1 of_int_numeral)
  1482 
  1483 lemma floor_minus_one_divide_eq_div_numeral [simp]:
  1484   "\<lfloor>- (1 / numeral b)::real\<rfloor> = - 1 div numeral b"
  1485 by (metis (mono_tags, hide_lams) div_minus_right minus_divide_right
  1486     floor_divide_of_int_eq of_int_neg_numeral of_int_1)
  1487 
  1488 lemma floor_divide_eq_div_numeral [simp]:
  1489   "\<lfloor>numeral a / numeral b::real\<rfloor> = numeral a div numeral b"
  1490 by (metis floor_divide_of_int_eq of_int_numeral)
  1491 
  1492 lemma floor_minus_divide_eq_div_numeral [simp]:
  1493   "\<lfloor>- (numeral a / numeral b)::real\<rfloor> = - numeral a div numeral b"
  1494 by (metis divide_minus_left floor_divide_of_int_eq of_int_neg_numeral of_int_numeral)
  1495 
  1496 lemma of_int_ceiling_cancel [simp]: "of_int \<lceil>x\<rceil> = x \<longleftrightarrow> (\<exists>n::int. x = of_int n)"
  1497   using ceiling_of_int by metis
  1498 
  1499 lemma ceiling_eq: "of_int n < x \<Longrightarrow> x \<le> of_int n + 1 \<Longrightarrow> \<lceil>x\<rceil> = n + 1"
  1500   by (simp add: ceiling_unique)
  1501 
  1502 lemma of_int_ceiling_diff_one_le [simp]: "of_int \<lceil>r\<rceil> - 1 \<le> r"
  1503   by linarith
  1504 
  1505 lemma of_int_ceiling_le_add_one [simp]: "of_int \<lceil>r\<rceil> \<le> r + 1"
  1506   by linarith
  1507 
  1508 lemma ceiling_le: "x \<le> of_int a \<Longrightarrow> \<lceil>x\<rceil> \<le> a"
  1509   by (simp add: ceiling_le_iff)
  1510 
  1511 lemma ceiling_divide_eq_div: "\<lceil>of_int a / of_int b\<rceil> = - (- a div b)"
  1512   by (metis ceiling_def floor_divide_of_int_eq minus_divide_left of_int_minus)
  1513 
  1514 lemma ceiling_divide_eq_div_numeral [simp]:
  1515   "\<lceil>numeral a / numeral b :: real\<rceil> = - (- numeral a div numeral b)"
  1516   using ceiling_divide_eq_div[of "numeral a" "numeral b"] by simp
  1517 
  1518 lemma ceiling_minus_divide_eq_div_numeral [simp]:
  1519   "\<lceil>- (numeral a / numeral b :: real)\<rceil> = - (numeral a div numeral b)"
  1520   using ceiling_divide_eq_div[of "- numeral a" "numeral b"] by simp
  1521 
  1522 text \<open>
  1523   The following lemmas are remnants of the erstwhile functions natfloor
  1524   and natceiling.
  1525 \<close>
  1526 
  1527 lemma nat_floor_neg: "x \<le> 0 \<Longrightarrow> nat \<lfloor>x\<rfloor> = 0"
  1528   for x :: real
  1529   by linarith
  1530 
  1531 lemma le_nat_floor: "real x \<le> a \<Longrightarrow> x \<le> nat \<lfloor>a\<rfloor>"
  1532   by linarith
  1533 
  1534 lemma le_mult_nat_floor: "nat \<lfloor>a\<rfloor> * nat \<lfloor>b\<rfloor> \<le> nat \<lfloor>a * b\<rfloor>"
  1535   by (cases "0 \<le> a \<and> 0 \<le> b")
  1536      (auto simp add: nat_mult_distrib[symmetric] nat_mono le_mult_floor)
  1537 
  1538 lemma nat_ceiling_le_eq [simp]: "nat \<lceil>x\<rceil> \<le> a \<longleftrightarrow> x \<le> real a"
  1539   by linarith
  1540 
  1541 lemma real_nat_ceiling_ge: "x \<le> real (nat \<lceil>x\<rceil>)"
  1542   by linarith
  1543 
  1544 lemma Rats_no_top_le: "\<exists>q \<in> \<rat>. x \<le> q"
  1545   for x :: real
  1546   by (auto intro!: bexI[of _ "of_nat (nat \<lceil>x\<rceil>)"]) linarith
  1547 
  1548 lemma Rats_no_bot_less: "\<exists>q \<in> \<rat>. q < x" for x :: real
  1549   apply (auto intro!: bexI[of _ "of_int (\<lfloor>x\<rfloor> - 1)"])
  1550   apply (rule less_le_trans[OF _ of_int_floor_le])
  1551   apply simp
  1552   done
  1553 
  1554 
  1555 subsection \<open>Exponentiation with floor\<close>
  1556 
  1557 lemma floor_power:
  1558   assumes "x = of_int \<lfloor>x\<rfloor>"
  1559   shows "\<lfloor>x ^ n\<rfloor> = \<lfloor>x\<rfloor> ^ n"
  1560 proof -
  1561   have "x ^ n = of_int (\<lfloor>x\<rfloor> ^ n)"
  1562     using assms by (induct n arbitrary: x) simp_all
  1563   then show ?thesis by (metis floor_of_int)
  1564 qed
  1565 
  1566 lemma floor_numeral_power [simp]: "\<lfloor>numeral x ^ n\<rfloor> = numeral x ^ n"
  1567   by (metis floor_of_int of_int_numeral of_int_power)
  1568 
  1569 lemma ceiling_numeral_power [simp]: "\<lceil>numeral x ^ n\<rceil> = numeral x ^ n"
  1570   by (metis ceiling_of_int of_int_numeral of_int_power)
  1571 
  1572 
  1573 subsection \<open>Implementation of rational real numbers\<close>
  1574 
  1575 text \<open>Formal constructor\<close>
  1576 
  1577 definition Ratreal :: "rat \<Rightarrow> real"
  1578   where [code_abbrev, simp]: "Ratreal = real_of_rat"
  1579 
  1580 code_datatype Ratreal
  1581 
  1582 
  1583 text \<open>Quasi-Numerals\<close>
  1584 
  1585 lemma [code_abbrev]:
  1586   "real_of_rat (numeral k) = numeral k"
  1587   "real_of_rat (- numeral k) = - numeral k"
  1588   "real_of_rat (rat_of_int a) = real_of_int a"
  1589   by simp_all
  1590 
  1591 lemma [code_post]:
  1592   "real_of_rat 0 = 0"
  1593   "real_of_rat 1 = 1"
  1594   "real_of_rat (- 1) = - 1"
  1595   "real_of_rat (1 / numeral k) = 1 / numeral k"
  1596   "real_of_rat (numeral k / numeral l) = numeral k / numeral l"
  1597   "real_of_rat (- (1 / numeral k)) = - (1 / numeral k)"
  1598   "real_of_rat (- (numeral k / numeral l)) = - (numeral k / numeral l)"
  1599   by (simp_all add: of_rat_divide of_rat_minus)
  1600 
  1601 text \<open>Operations\<close>
  1602 
  1603 lemma zero_real_code [code]: "0 = Ratreal 0"
  1604   by simp
  1605 
  1606 lemma one_real_code [code]: "1 = Ratreal 1"
  1607   by simp
  1608 
  1609 instantiation real :: equal
  1610 begin
  1611 
  1612 definition "HOL.equal x y \<longleftrightarrow> x - y = 0" for x :: real
  1613 
  1614 instance by standard (simp add: equal_real_def)
  1615 
  1616 lemma real_equal_code [code]: "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
  1617   by (simp add: equal_real_def equal)
  1618 
  1619 lemma [code nbe]: "HOL.equal x x \<longleftrightarrow> True"
  1620   for x :: real
  1621   by (rule equal_refl)
  1622 
  1623 end
  1624 
  1625 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  1626   by (simp add: of_rat_less_eq)
  1627 
  1628 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  1629   by (simp add: of_rat_less)
  1630 
  1631 lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  1632   by (simp add: of_rat_add)
  1633 
  1634 lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  1635   by (simp add: of_rat_mult)
  1636 
  1637 lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  1638   by (simp add: of_rat_minus)
  1639 
  1640 lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  1641   by (simp add: of_rat_diff)
  1642 
  1643 lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  1644   by (simp add: of_rat_inverse)
  1645 
  1646 lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  1647   by (simp add: of_rat_divide)
  1648 
  1649 lemma real_floor_code [code]: "\<lfloor>Ratreal x\<rfloor> = \<lfloor>x\<rfloor>"
  1650   by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff
  1651       of_int_floor_le of_rat_of_int_eq real_less_eq_code)
  1652 
  1653 
  1654 text \<open>Quickcheck\<close>
  1655 
  1656 definition (in term_syntax)
  1657   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)"
  1658   where [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  1659 
  1660 notation fcomp (infixl "\<circ>>" 60)
  1661 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1662 
  1663 instantiation real :: random
  1664 begin
  1665 
  1666 definition
  1667   "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  1668 
  1669 instance ..
  1670 
  1671 end
  1672 
  1673 no_notation fcomp (infixl "\<circ>>" 60)
  1674 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1675 
  1676 instantiation real :: exhaustive
  1677 begin
  1678 
  1679 definition
  1680   "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (\<lambda>r. f (Ratreal r)) d"
  1681 
  1682 instance ..
  1683 
  1684 end
  1685 
  1686 instantiation real :: full_exhaustive
  1687 begin
  1688 
  1689 definition
  1690   "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (\<lambda>r. f (valterm_ratreal r)) d"
  1691 
  1692 instance ..
  1693 
  1694 end
  1695 
  1696 instantiation real :: narrowing
  1697 begin
  1698 
  1699 definition
  1700   "narrowing_real = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
  1701 
  1702 instance ..
  1703 
  1704 end
  1705 
  1706 
  1707 subsection \<open>Setup for Nitpick\<close>
  1708 
  1709 declaration \<open>
  1710   Nitpick_HOL.register_frac_type @{type_name real}
  1711     [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  1712      (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  1713      (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  1714      (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  1715      (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  1716      (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  1717      (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
  1718      (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  1719 \<close>
  1720 
  1721 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
  1722   ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  1723   times_real_inst.times_real uminus_real_inst.uminus_real
  1724   zero_real_inst.zero_real
  1725 
  1726 
  1727 subsection \<open>Setup for SMT\<close>
  1728 
  1729 ML_file "Tools/SMT/smt_real.ML"
  1730 ML_file "Tools/SMT/z3_real.ML"
  1731 
  1732 lemma [z3_rule]:
  1733   "0 + x = x"
  1734   "x + 0 = x"
  1735   "0 * x = 0"
  1736   "1 * x = x"
  1737   "-x = -1 * x"
  1738   "x + y = y + x"
  1739   for x y :: real
  1740   by auto
  1741 
  1742 
  1743 subsection \<open>Setup for Argo\<close>
  1744 
  1745 ML_file "Tools/Argo/argo_real.ML"
  1746 
  1747 end