src/HOL/RealDef.thy
author bulwahn
Mon Dec 12 13:45:54 2011 +0100 (2011-12-12)
changeset 45818 53a697f5454a
parent 45184 426dbd896c9e
child 45859 36ff12b5663b
permissions -rw-r--r--
hiding constants and facts in the Quickcheck_Exhaustive and Quickcheck_Narrowing theory;
     1 (*  Title       : HOL/RealDef.thy
     2     Author      : Jacques D. Fleuriot, 1998
     3     Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
     4     Additional contributions by Jeremy Avigad
     5     Construction of Cauchy Reals by Brian Huffman, 2010
     6 *)
     7 
     8 header {* Development of the Reals using Cauchy Sequences *}
     9 
    10 theory RealDef
    11 imports Rat
    12 begin
    13 
    14 text {*
    15   This theory contains a formalization of the real numbers as
    16   equivalence classes of Cauchy sequences of rationals.  See
    17   @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative
    18   construction using Dedekind cuts.
    19 *}
    20 
    21 subsection {* Preliminary lemmas *}
    22 
    23 lemma add_diff_add:
    24   fixes a b c d :: "'a::ab_group_add"
    25   shows "(a + c) - (b + d) = (a - b) + (c - d)"
    26   by simp
    27 
    28 lemma minus_diff_minus:
    29   fixes a b :: "'a::ab_group_add"
    30   shows "- a - - b = - (a - b)"
    31   by simp
    32 
    33 lemma mult_diff_mult:
    34   fixes x y a b :: "'a::ring"
    35   shows "(x * y - a * b) = x * (y - b) + (x - a) * b"
    36   by (simp add: algebra_simps)
    37 
    38 lemma inverse_diff_inverse:
    39   fixes a b :: "'a::division_ring"
    40   assumes "a \<noteq> 0" and "b \<noteq> 0"
    41   shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
    42   using assms by (simp add: algebra_simps)
    43 
    44 lemma obtain_pos_sum:
    45   fixes r :: rat assumes r: "0 < r"
    46   obtains s t where "0 < s" and "0 < t" and "r = s + t"
    47 proof
    48     from r show "0 < r/2" by simp
    49     from r show "0 < r/2" by simp
    50     show "r = r/2 + r/2" by simp
    51 qed
    52 
    53 subsection {* Sequences that converge to zero *}
    54 
    55 definition
    56   vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
    57 where
    58   "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
    59 
    60 lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
    61   unfolding vanishes_def by simp
    62 
    63 lemma vanishesD: "\<lbrakk>vanishes X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
    64   unfolding vanishes_def by simp
    65 
    66 lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
    67   unfolding vanishes_def
    68   apply (cases "c = 0", auto)
    69   apply (rule exI [where x="\<bar>c\<bar>"], auto)
    70   done
    71 
    72 lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
    73   unfolding vanishes_def by simp
    74 
    75 lemma vanishes_add:
    76   assumes X: "vanishes X" and Y: "vanishes Y"
    77   shows "vanishes (\<lambda>n. X n + Y n)"
    78 proof (rule vanishesI)
    79   fix r :: rat assume "0 < r"
    80   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
    81     by (rule obtain_pos_sum)
    82   obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
    83     using vanishesD [OF X s] ..
    84   obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
    85     using vanishesD [OF Y t] ..
    86   have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
    87   proof (clarsimp)
    88     fix n assume n: "i \<le> n" "j \<le> n"
    89     have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>" by (rule abs_triangle_ineq)
    90     also have "\<dots> < s + t" by (simp add: add_strict_mono i j n)
    91     finally show "\<bar>X n + Y n\<bar> < r" unfolding r .
    92   qed
    93   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
    94 qed
    95 
    96 lemma vanishes_diff:
    97   assumes X: "vanishes X" and Y: "vanishes Y"
    98   shows "vanishes (\<lambda>n. X n - Y n)"
    99 unfolding diff_minus by (intro vanishes_add vanishes_minus X Y)
   100 
   101 lemma vanishes_mult_bounded:
   102   assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
   103   assumes Y: "vanishes (\<lambda>n. Y n)"
   104   shows "vanishes (\<lambda>n. X n * Y n)"
   105 proof (rule vanishesI)
   106   fix r :: rat assume r: "0 < r"
   107   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   108     using X by fast
   109   obtain b where b: "0 < b" "r = a * b"
   110   proof
   111     show "0 < r / a" using r a by (simp add: divide_pos_pos)
   112     show "r = a * (r / a)" using a by simp
   113   qed
   114   obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
   115     using vanishesD [OF Y b(1)] ..
   116   have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
   117     by (simp add: b(2) abs_mult mult_strict_mono' a k)
   118   thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
   119 qed
   120 
   121 subsection {* Cauchy sequences *}
   122 
   123 definition
   124   cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
   125 where
   126   "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
   127 
   128 lemma cauchyI:
   129   "(\<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"
   130   unfolding cauchy_def by simp
   131 
   132 lemma cauchyD:
   133   "\<lbrakk>cauchy X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
   134   unfolding cauchy_def by simp
   135 
   136 lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
   137   unfolding cauchy_def by simp
   138 
   139 lemma cauchy_add [simp]:
   140   assumes X: "cauchy X" and Y: "cauchy Y"
   141   shows "cauchy (\<lambda>n. X n + Y n)"
   142 proof (rule cauchyI)
   143   fix r :: rat assume "0 < r"
   144   then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   145     by (rule obtain_pos_sum)
   146   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   147     using cauchyD [OF X s] ..
   148   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   149     using cauchyD [OF Y t] ..
   150   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
   151   proof (clarsimp)
   152     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   153     have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
   154       unfolding add_diff_add by (rule abs_triangle_ineq)
   155     also have "\<dots> < s + t"
   156       by (rule add_strict_mono, simp_all add: i j *)
   157     finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" unfolding r .
   158   qed
   159   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
   160 qed
   161 
   162 lemma cauchy_minus [simp]:
   163   assumes X: "cauchy X"
   164   shows "cauchy (\<lambda>n. - X n)"
   165 using assms unfolding cauchy_def
   166 unfolding minus_diff_minus abs_minus_cancel .
   167 
   168 lemma cauchy_diff [simp]:
   169   assumes X: "cauchy X" and Y: "cauchy Y"
   170   shows "cauchy (\<lambda>n. X n - Y n)"
   171 using assms unfolding diff_minus by simp
   172 
   173 lemma cauchy_imp_bounded:
   174   assumes "cauchy X" shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   175 proof -
   176   obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
   177     using cauchyD [OF assms zero_less_one] ..
   178   show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
   179   proof (intro exI conjI allI)
   180     have "0 \<le> \<bar>X 0\<bar>" by simp
   181     also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
   182     finally have "0 \<le> Max (abs ` X ` {..k})" .
   183     thus "0 < Max (abs ` X ` {..k}) + 1" by simp
   184   next
   185     fix n :: nat
   186     show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
   187     proof (rule linorder_le_cases)
   188       assume "n \<le> k"
   189       hence "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
   190       thus "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
   191     next
   192       assume "k \<le> n"
   193       have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
   194       also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
   195         by (rule abs_triangle_ineq)
   196       also have "\<dots> < Max (abs ` X ` {..k}) + 1"
   197         by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
   198       finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
   199     qed
   200   qed
   201 qed
   202 
   203 lemma cauchy_mult [simp]:
   204   assumes X: "cauchy X" and Y: "cauchy Y"
   205   shows "cauchy (\<lambda>n. X n * Y n)"
   206 proof (rule cauchyI)
   207   fix r :: rat assume "0 < r"
   208   then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
   209     by (rule obtain_pos_sum)
   210   obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
   211     using cauchy_imp_bounded [OF X] by fast
   212   obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
   213     using cauchy_imp_bounded [OF Y] by fast
   214   obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
   215   proof
   216     show "0 < v/b" using v b(1) by (rule divide_pos_pos)
   217     show "0 < u/a" using u a(1) by (rule divide_pos_pos)
   218     show "r = a * (u/a) + (v/b) * b"
   219       using a(1) b(1) `r = u + v` by simp
   220   qed
   221   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   222     using cauchyD [OF X s] ..
   223   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
   224     using cauchyD [OF Y t] ..
   225   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
   226   proof (clarsimp)
   227     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   228     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>"
   229       unfolding mult_diff_mult ..
   230     also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
   231       by (rule abs_triangle_ineq)
   232     also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
   233       unfolding abs_mult ..
   234     also have "\<dots> < a * t + s * b"
   235       by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
   236     finally show "\<bar>X m * Y m - X n * Y n\<bar> < r" unfolding r .
   237   qed
   238   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
   239 qed
   240 
   241 lemma cauchy_not_vanishes_cases:
   242   assumes X: "cauchy X"
   243   assumes nz: "\<not> vanishes X"
   244   shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
   245 proof -
   246   obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
   247     using nz unfolding vanishes_def by (auto simp add: not_less)
   248   obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
   249     using `0 < r` by (rule obtain_pos_sum)
   250   obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
   251     using cauchyD [OF X s] ..
   252   obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
   253     using r by fast
   254   have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
   255     using i `i \<le> k` by auto
   256   have "X k \<le> - r \<or> r \<le> X k"
   257     using `r \<le> \<bar>X k\<bar>` by auto
   258   hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   259     unfolding `r = s + t` using k by auto
   260   hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
   261   thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
   262     using t by auto
   263 qed
   264 
   265 lemma cauchy_not_vanishes:
   266   assumes X: "cauchy X"
   267   assumes nz: "\<not> vanishes X"
   268   shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
   269 using cauchy_not_vanishes_cases [OF assms]
   270 by clarify (rule exI, erule conjI, rule_tac x=k in exI, auto)
   271 
   272 lemma cauchy_inverse [simp]:
   273   assumes X: "cauchy X"
   274   assumes nz: "\<not> vanishes X"
   275   shows "cauchy (\<lambda>n. inverse (X n))"
   276 proof (rule cauchyI)
   277   fix r :: rat assume "0 < r"
   278   obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
   279     using cauchy_not_vanishes [OF X nz] by fast
   280   from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
   281   obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
   282   proof
   283     show "0 < b * r * b"
   284       by (simp add: `0 < r` b mult_pos_pos)
   285     show "r = inverse b * (b * r * b) * inverse b"
   286       using b by simp
   287   qed
   288   obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
   289     using cauchyD [OF X s] ..
   290   have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
   291   proof (clarsimp)
   292     fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
   293     have "\<bar>inverse (X m) - inverse (X n)\<bar> =
   294           inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
   295       by (simp add: inverse_diff_inverse nz * abs_mult)
   296     also have "\<dots> < inverse b * s * inverse b"
   297       by (simp add: mult_strict_mono less_imp_inverse_less
   298                     mult_pos_pos i j b * s)
   299     finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" unfolding r .
   300   qed
   301   thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
   302 qed
   303 
   304 subsection {* Equivalence relation on Cauchy sequences *}
   305 
   306 definition
   307   realrel :: "((nat \<Rightarrow> rat) \<times> (nat \<Rightarrow> rat)) set"
   308 where
   309   "realrel = {(X, Y). cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n)}"
   310 
   311 lemma refl_realrel: "refl_on {X. cauchy X} realrel"
   312   unfolding realrel_def by (rule refl_onI, clarsimp, simp)
   313 
   314 lemma sym_realrel: "sym realrel"
   315   unfolding realrel_def
   316   by (rule symI, clarify, drule vanishes_minus, simp)
   317 
   318 lemma trans_realrel: "trans realrel"
   319   unfolding realrel_def
   320   apply (rule transI, clarify)
   321   apply (drule (1) vanishes_add)
   322   apply (simp add: algebra_simps)
   323   done
   324 
   325 lemma equiv_realrel: "equiv {X. cauchy X} realrel"
   326   using refl_realrel sym_realrel trans_realrel
   327   by (rule equivI)
   328 
   329 subsection {* The field of real numbers *}
   330 
   331 typedef (open) real = "{X. cauchy X} // realrel"
   332   by (fast intro: quotientI cauchy_const)
   333 
   334 definition
   335   Real :: "(nat \<Rightarrow> rat) \<Rightarrow> real"
   336 where
   337   "Real X = Abs_real (realrel `` {X})"
   338 
   339 definition
   340   real_case :: "((nat \<Rightarrow> rat) \<Rightarrow> 'a) \<Rightarrow> real \<Rightarrow> 'a"
   341 where
   342   "real_case f x = (THE y. \<forall>X\<in>Rep_real x. y = f X)"
   343 
   344 lemma Real_induct [induct type: real]:
   345   "(\<And>X. cauchy X \<Longrightarrow> P (Real X)) \<Longrightarrow> P x"
   346   unfolding Real_def
   347   apply (induct x)
   348   apply (erule quotientE)
   349   apply (simp)
   350   done
   351 
   352 lemma real_case_1:
   353   assumes f: "congruent realrel f"
   354   assumes X: "cauchy X"
   355   shows "real_case f (Real X) = f X"
   356   unfolding real_case_def Real_def
   357   apply (subst Abs_real_inverse)
   358   apply (simp add: quotientI X)
   359   apply (rule the_equality)
   360   apply clarsimp
   361   apply (erule congruentD [OF f])
   362   apply (erule bspec)
   363   apply simp
   364   apply (rule refl_onD [OF refl_realrel])
   365   apply (simp add: X)
   366   done
   367 
   368 lemma real_case_2:
   369   assumes f: "congruent2 realrel realrel f"
   370   assumes X: "cauchy X" and Y: "cauchy Y"
   371   shows "real_case (\<lambda>X. real_case (\<lambda>Y. f X Y) (Real Y)) (Real X) = f X Y"
   372  apply (subst real_case_1 [OF _ X])
   373   apply (rule congruentI)
   374   apply (subst real_case_1 [OF _ Y])
   375    apply (rule congruent2_implies_congruent [OF equiv_realrel f])
   376    apply (simp add: realrel_def)
   377   apply (subst real_case_1 [OF _ Y])
   378    apply (rule congruent2_implies_congruent [OF equiv_realrel f])
   379    apply (simp add: realrel_def)
   380   apply (erule congruent2D [OF f])
   381   apply (rule refl_onD [OF refl_realrel])
   382   apply (simp add: Y)
   383   apply (rule real_case_1 [OF _ Y])
   384   apply (rule congruent2_implies_congruent [OF equiv_realrel f])
   385   apply (simp add: X)
   386   done
   387 
   388 lemma eq_Real:
   389   "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
   390   unfolding Real_def
   391   apply (subst Abs_real_inject)
   392   apply (simp add: quotientI)
   393   apply (simp add: quotientI)
   394   apply (simp add: eq_equiv_class_iff [OF equiv_realrel])
   395   apply (simp add: realrel_def)
   396   done
   397 
   398 lemma add_respects2_realrel:
   399   "(\<lambda>X Y. Real (\<lambda>n. X n + Y n)) respects2 realrel"
   400 proof (rule congruent2_commuteI [OF equiv_realrel, unfolded mem_Collect_eq])
   401   fix X Y show "Real (\<lambda>n. X n + Y n) = Real (\<lambda>n. Y n + X n)"
   402     by (simp add: add_commute)
   403 next
   404   fix X assume X: "cauchy X"
   405   fix Y Z assume "(Y, Z) \<in> realrel"
   406   hence Y: "cauchy Y" and Z: "cauchy Z" and YZ: "vanishes (\<lambda>n. Y n - Z n)"
   407     unfolding realrel_def by simp_all
   408   show "Real (\<lambda>n. X n + Y n) = Real (\<lambda>n. X n + Z n)"
   409   proof (rule eq_Real [THEN iffD2])
   410     show "cauchy (\<lambda>n. X n + Y n)" using X Y by (rule cauchy_add)
   411     show "cauchy (\<lambda>n. X n + Z n)" using X Z by (rule cauchy_add)
   412     show "vanishes (\<lambda>n. (X n + Y n) - (X n + Z n))"
   413       unfolding add_diff_add using YZ by simp
   414   qed
   415 qed
   416 
   417 lemma minus_respects_realrel:
   418   "(\<lambda>X. Real (\<lambda>n. - X n)) respects realrel"
   419 proof (rule congruentI)
   420   fix X Y assume "(X, Y) \<in> realrel"
   421   hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   422     unfolding realrel_def by simp_all
   423   show "Real (\<lambda>n. - X n) = Real (\<lambda>n. - Y n)"
   424   proof (rule eq_Real [THEN iffD2])
   425     show "cauchy (\<lambda>n. - X n)" using X by (rule cauchy_minus)
   426     show "cauchy (\<lambda>n. - Y n)" using Y by (rule cauchy_minus)
   427     show "vanishes (\<lambda>n. (- X n) - (- Y n))"
   428       unfolding minus_diff_minus using XY by (rule vanishes_minus)
   429   qed
   430 qed
   431 
   432 lemma mult_respects2_realrel:
   433   "(\<lambda>X Y. Real (\<lambda>n. X n * Y n)) respects2 realrel"
   434 proof (rule congruent2_commuteI [OF equiv_realrel, unfolded mem_Collect_eq])
   435   fix X Y
   436   show "Real (\<lambda>n. X n * Y n) = Real (\<lambda>n. Y n * X n)"
   437     by (simp add: mult_commute)
   438 next
   439   fix X assume X: "cauchy X"
   440   fix Y Z assume "(Y, Z) \<in> realrel"
   441   hence Y: "cauchy Y" and Z: "cauchy Z" and YZ: "vanishes (\<lambda>n. Y n - Z n)"
   442     unfolding realrel_def by simp_all
   443   show "Real (\<lambda>n. X n * Y n) = Real (\<lambda>n. X n * Z n)"
   444   proof (rule eq_Real [THEN iffD2])
   445     show "cauchy (\<lambda>n. X n * Y n)" using X Y by (rule cauchy_mult)
   446     show "cauchy (\<lambda>n. X n * Z n)" using X Z by (rule cauchy_mult)
   447     have "vanishes (\<lambda>n. X n * (Y n - Z n))"
   448       by (intro vanishes_mult_bounded cauchy_imp_bounded X YZ)
   449     thus "vanishes (\<lambda>n. X n * Y n - X n * Z n)"
   450       by (simp add: right_diff_distrib)
   451   qed
   452 qed
   453 
   454 lemma vanishes_diff_inverse:
   455   assumes X: "cauchy X" "\<not> vanishes X"
   456   assumes Y: "cauchy Y" "\<not> vanishes Y"
   457   assumes XY: "vanishes (\<lambda>n. X n - Y n)"
   458   shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
   459 proof (rule vanishesI)
   460   fix r :: rat assume r: "0 < r"
   461   obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
   462     using cauchy_not_vanishes [OF X] by fast
   463   obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
   464     using cauchy_not_vanishes [OF Y] by fast
   465   obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
   466   proof
   467     show "0 < a * r * b"
   468       using a r b by (simp add: mult_pos_pos)
   469     show "inverse a * (a * r * b) * inverse b = r"
   470       using a r b by simp
   471   qed
   472   obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
   473     using vanishesD [OF XY s] ..
   474   have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
   475   proof (clarsimp)
   476     fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
   477     have "X n \<noteq> 0" and "Y n \<noteq> 0"
   478       using i j a b n by auto
   479     hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
   480         inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
   481       by (simp add: inverse_diff_inverse abs_mult)
   482     also have "\<dots> < inverse a * s * inverse b"
   483       apply (intro mult_strict_mono' less_imp_inverse_less)
   484       apply (simp_all add: a b i j k n mult_nonneg_nonneg)
   485       done
   486     also note `inverse a * s * inverse b = r`
   487     finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
   488   qed
   489   thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
   490 qed
   491 
   492 lemma inverse_respects_realrel:
   493   "(\<lambda>X. if vanishes X then c else Real (\<lambda>n. inverse (X n))) respects realrel"
   494     (is "?inv respects realrel")
   495 proof (rule congruentI)
   496   fix X Y assume "(X, Y) \<in> realrel"
   497   hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   498     unfolding realrel_def by simp_all
   499   have "vanishes X \<longleftrightarrow> vanishes Y"
   500   proof
   501     assume "vanishes X"
   502     from vanishes_diff [OF this XY] show "vanishes Y" by simp
   503   next
   504     assume "vanishes Y"
   505     from vanishes_add [OF this XY] show "vanishes X" by simp
   506   qed
   507   thus "?inv X = ?inv Y"
   508     by (simp add: vanishes_diff_inverse eq_Real X Y XY)
   509 qed
   510 
   511 instantiation real :: field_inverse_zero
   512 begin
   513 
   514 definition
   515   "0 = Real (\<lambda>n. 0)"
   516 
   517 definition
   518   "1 = Real (\<lambda>n. 1)"
   519 
   520 definition
   521   "x + y = real_case (\<lambda>X. real_case (\<lambda>Y. Real (\<lambda>n. X n + Y n)) y) x"
   522 
   523 definition
   524   "- x = real_case (\<lambda>X. Real (\<lambda>n. - X n)) x"
   525 
   526 definition
   527   "x - y = (x::real) + - y"
   528 
   529 definition
   530   "x * y = real_case (\<lambda>X. real_case (\<lambda>Y. Real (\<lambda>n. X n * Y n)) y) x"
   531 
   532 definition
   533   "inverse =
   534     real_case (\<lambda>X. if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   535 
   536 definition
   537   "x / y = (x::real) * inverse y"
   538 
   539 lemma add_Real:
   540   assumes X: "cauchy X" and Y: "cauchy Y"
   541   shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
   542   unfolding plus_real_def
   543   by (rule real_case_2 [OF add_respects2_realrel X Y])
   544 
   545 lemma minus_Real:
   546   assumes X: "cauchy X"
   547   shows "- Real X = Real (\<lambda>n. - X n)"
   548   unfolding uminus_real_def
   549   by (rule real_case_1 [OF minus_respects_realrel X])
   550 
   551 lemma diff_Real:
   552   assumes X: "cauchy X" and Y: "cauchy Y"
   553   shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
   554   unfolding minus_real_def diff_minus
   555   by (simp add: minus_Real add_Real X Y)
   556 
   557 lemma mult_Real:
   558   assumes X: "cauchy X" and Y: "cauchy Y"
   559   shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
   560   unfolding times_real_def
   561   by (rule real_case_2 [OF mult_respects2_realrel X Y])
   562 
   563 lemma inverse_Real:
   564   assumes X: "cauchy X"
   565   shows "inverse (Real X) =
   566     (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   567   unfolding inverse_real_def
   568   by (rule real_case_1 [OF inverse_respects_realrel X])
   569 
   570 instance proof
   571   fix a b c :: real
   572   show "a + b = b + a"
   573     by (induct a, induct b) (simp add: add_Real add_ac)
   574   show "(a + b) + c = a + (b + c)"
   575     by (induct a, induct b, induct c) (simp add: add_Real add_ac)
   576   show "0 + a = a"
   577     unfolding zero_real_def
   578     by (induct a) (simp add: add_Real)
   579   show "- a + a = 0"
   580     unfolding zero_real_def
   581     by (induct a) (simp add: minus_Real add_Real)
   582   show "a - b = a + - b"
   583     by (rule minus_real_def)
   584   show "(a * b) * c = a * (b * c)"
   585     by (induct a, induct b, induct c) (simp add: mult_Real mult_ac)
   586   show "a * b = b * a"
   587     by (induct a, induct b) (simp add: mult_Real mult_ac)
   588   show "1 * a = a"
   589     unfolding one_real_def
   590     by (induct a) (simp add: mult_Real)
   591   show "(a + b) * c = a * c + b * c"
   592     by (induct a, induct b, induct c)
   593        (simp add: mult_Real add_Real algebra_simps)
   594   show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
   595     unfolding zero_real_def one_real_def
   596     by (simp add: eq_Real)
   597   show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   598     unfolding zero_real_def one_real_def
   599     apply (induct a)
   600     apply (simp add: eq_Real inverse_Real mult_Real)
   601     apply (rule vanishesI)
   602     apply (frule (1) cauchy_not_vanishes, clarify)
   603     apply (rule_tac x=k in exI, clarify)
   604     apply (drule_tac x=n in spec, simp)
   605     done
   606   show "a / b = a * inverse b"
   607     by (rule divide_real_def)
   608   show "inverse (0::real) = 0"
   609     by (simp add: zero_real_def inverse_Real)
   610 qed
   611 
   612 end
   613 
   614 subsection {* Positive reals *}
   615 
   616 definition
   617   positive :: "real \<Rightarrow> bool"
   618 where
   619   "positive = real_case (\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   620 
   621 lemma bool_congruentI:
   622   assumes sym: "sym r"
   623   assumes P: "\<And>x y. (x, y) \<in> r \<Longrightarrow> P x \<Longrightarrow> P y"
   624   shows "P respects r"
   625 apply (rule congruentI)
   626 apply (rule iffI)
   627 apply (erule (1) P)
   628 apply (erule (1) P [OF symD [OF sym]])
   629 done
   630 
   631 lemma positive_respects_realrel:
   632   "(\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n) respects realrel"
   633 proof (rule bool_congruentI)
   634   show "sym realrel" by (rule sym_realrel)
   635 next
   636   fix X Y assume "(X, Y) \<in> realrel"
   637   hence XY: "vanishes (\<lambda>n. X n - Y n)"
   638     unfolding realrel_def by simp_all
   639   assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   640   then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
   641     by fast
   642   obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   643     using `0 < r` by (rule obtain_pos_sum)
   644   obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
   645     using vanishesD [OF XY s] ..
   646   have "\<forall>n\<ge>max i j. t < Y n"
   647   proof (clarsimp)
   648     fix n assume n: "i \<le> n" "j \<le> n"
   649     have "\<bar>X n - Y n\<bar> < s" and "r < X n"
   650       using i j n by simp_all
   651     thus "t < Y n" unfolding r by simp
   652   qed
   653   thus "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
   654 qed
   655 
   656 lemma positive_Real:
   657   assumes X: "cauchy X"
   658   shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   659 unfolding positive_def
   660 by (rule real_case_1 [OF positive_respects_realrel X])
   661 
   662 lemma positive_zero: "\<not> positive 0"
   663 unfolding zero_real_def by (auto simp add: positive_Real)
   664 
   665 lemma positive_add:
   666   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
   667 apply (induct x, induct y, rename_tac Y X)
   668 apply (simp add: add_Real positive_Real)
   669 apply (clarify, rename_tac a b i j)
   670 apply (rule_tac x="a + b" in exI, simp)
   671 apply (rule_tac x="max i j" in exI, clarsimp)
   672 apply (simp add: add_strict_mono)
   673 done
   674 
   675 lemma positive_mult:
   676   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
   677 apply (induct x, induct y, rename_tac Y X)
   678 apply (simp add: mult_Real positive_Real)
   679 apply (clarify, rename_tac a b i j)
   680 apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
   681 apply (rule_tac x="max i j" in exI, clarsimp)
   682 apply (rule mult_strict_mono, auto)
   683 done
   684 
   685 lemma positive_minus:
   686   "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
   687 apply (induct x, rename_tac X)
   688 apply (simp add: zero_real_def eq_Real minus_Real positive_Real)
   689 apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
   690 done
   691 
   692 instantiation real :: linordered_field_inverse_zero
   693 begin
   694 
   695 definition
   696   "x < y \<longleftrightarrow> positive (y - x)"
   697 
   698 definition
   699   "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
   700 
   701 definition
   702   "abs (a::real) = (if a < 0 then - a else a)"
   703 
   704 definition
   705   "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   706 
   707 instance proof
   708   fix a b c :: real
   709   show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   710     by (rule abs_real_def)
   711   show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   712     unfolding less_eq_real_def less_real_def
   713     by (auto, drule (1) positive_add, simp_all add: positive_zero)
   714   show "a \<le> a"
   715     unfolding less_eq_real_def by simp
   716   show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   717     unfolding less_eq_real_def less_real_def
   718     by (auto, drule (1) positive_add, simp add: algebra_simps)
   719   show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   720     unfolding less_eq_real_def less_real_def
   721     by (auto, drule (1) positive_add, simp add: positive_zero)
   722   show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   723     unfolding less_eq_real_def less_real_def by auto
   724   show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   725     by (rule sgn_real_def)
   726   show "a \<le> b \<or> b \<le> a"
   727     unfolding less_eq_real_def less_real_def
   728     by (auto dest!: positive_minus)
   729   show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   730     unfolding less_real_def
   731     by (drule (1) positive_mult, simp add: algebra_simps)
   732 qed
   733 
   734 end
   735 
   736 instantiation real :: distrib_lattice
   737 begin
   738 
   739 definition
   740   "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
   741 
   742 definition
   743   "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
   744 
   745 instance proof
   746 qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
   747 
   748 end
   749 
   750 instantiation real :: number_ring
   751 begin
   752 
   753 definition
   754   "(number_of x :: real) = of_int x"
   755 
   756 instance proof
   757 qed (rule number_of_real_def)
   758 
   759 end
   760 
   761 lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   762 apply (induct x)
   763 apply (simp add: zero_real_def)
   764 apply (simp add: one_real_def add_Real)
   765 done
   766 
   767 lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
   768 apply (cases x rule: int_diff_cases)
   769 apply (simp add: of_nat_Real diff_Real)
   770 done
   771 
   772 lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
   773 apply (induct x)
   774 apply (simp add: Fract_of_int_quotient of_rat_divide)
   775 apply (simp add: of_int_Real divide_inverse)
   776 apply (simp add: inverse_Real mult_Real)
   777 done
   778 
   779 instance real :: archimedean_field
   780 proof
   781   fix x :: real
   782   show "\<exists>z. x \<le> of_int z"
   783     apply (induct x)
   784     apply (frule cauchy_imp_bounded, clarify)
   785     apply (rule_tac x="ceiling b + 1" in exI)
   786     apply (rule less_imp_le)
   787     apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
   788     apply (rule_tac x=1 in exI, simp add: algebra_simps)
   789     apply (rule_tac x=0 in exI, clarsimp)
   790     apply (rule le_less_trans [OF abs_ge_self])
   791     apply (rule less_le_trans [OF _ le_of_int_ceiling])
   792     apply simp
   793     done
   794 qed
   795 
   796 instantiation real :: floor_ceiling
   797 begin
   798 
   799 definition [code del]:
   800   "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
   801 
   802 instance proof
   803   fix x :: real
   804   show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   805     unfolding floor_real_def using floor_exists1 by (rule theI')
   806 qed
   807 
   808 end
   809 
   810 subsection {* Completeness *}
   811 
   812 lemma not_positive_Real:
   813   assumes X: "cauchy X"
   814   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   815 unfolding positive_Real [OF X]
   816 apply (auto, unfold not_less)
   817 apply (erule obtain_pos_sum)
   818 apply (drule_tac x=s in spec, simp)
   819 apply (drule_tac r=t in cauchyD [OF X], clarify)
   820 apply (drule_tac x=k in spec, clarsimp)
   821 apply (rule_tac x=n in exI, clarify, rename_tac m)
   822 apply (drule_tac x=m in spec, simp)
   823 apply (drule_tac x=n in spec, simp)
   824 apply (drule spec, drule (1) mp, clarify, rename_tac i)
   825 apply (rule_tac x="max i k" in exI, simp)
   826 done
   827 
   828 lemma le_Real:
   829   assumes X: "cauchy X" and Y: "cauchy Y"
   830   shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   831 unfolding not_less [symmetric, where 'a=real] less_real_def
   832 apply (simp add: diff_Real not_positive_Real X Y)
   833 apply (simp add: diff_le_eq add_ac)
   834 done
   835 
   836 lemma le_RealI:
   837   assumes Y: "cauchy Y"
   838   shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   839 proof (induct x)
   840   fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   841   hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   842     by (simp add: of_rat_Real le_Real)
   843   {
   844     fix r :: rat assume "0 < r"
   845     then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   846       by (rule obtain_pos_sum)
   847     obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   848       using cauchyD [OF Y s] ..
   849     obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   850       using le [OF t] ..
   851     have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   852     proof (clarsimp)
   853       fix n assume n: "i \<le> n" "j \<le> n"
   854       have "X n \<le> Y i + t" using n j by simp
   855       moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
   856       ultimately show "X n \<le> Y n + r" unfolding r by simp
   857     qed
   858     hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
   859   }
   860   thus "Real X \<le> Real Y"
   861     by (simp add: of_rat_Real le_Real X Y)
   862 qed
   863 
   864 lemma Real_leI:
   865   assumes X: "cauchy X"
   866   assumes le: "\<forall>n. of_rat (X n) \<le> y"
   867   shows "Real X \<le> y"
   868 proof -
   869   have "- y \<le> - Real X"
   870     by (simp add: minus_Real X le_RealI of_rat_minus le)
   871   thus ?thesis by simp
   872 qed
   873 
   874 lemma less_RealD:
   875   assumes Y: "cauchy Y"
   876   shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   877 by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   878 
   879 lemma of_nat_less_two_power:
   880   "of_nat n < (2::'a::{linordered_idom,number_ring}) ^ n"
   881 apply (induct n)
   882 apply simp
   883 apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   884 apply (drule (1) add_le_less_mono, simp)
   885 apply simp
   886 done
   887 
   888 lemma complete_real:
   889   fixes S :: "real set"
   890   assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   891   shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   892 proof -
   893   obtain x where x: "x \<in> S" using assms(1) ..
   894   obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   895 
   896   def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
   897   obtain a where a: "\<not> P a"
   898   proof
   899     have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
   900     also have "x - 1 < x" by simp
   901     finally have "of_int (floor (x - 1)) < x" .
   902     hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
   903     then show "\<not> P (of_int (floor (x - 1)))"
   904       unfolding P_def of_rat_of_int_eq using x by fast
   905   qed
   906   obtain b where b: "P b"
   907   proof
   908     show "P (of_int (ceiling z))"
   909     unfolding P_def of_rat_of_int_eq
   910     proof
   911       fix y assume "y \<in> S"
   912       hence "y \<le> z" using z by simp
   913       also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
   914       finally show "y \<le> of_int (ceiling z)" .
   915     qed
   916   qed
   917 
   918   def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
   919   def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
   920   def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
   921   def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
   922   def C \<equiv> "\<lambda>n. avg (A n) (B n)"
   923   have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   924   have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   925   have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   926     unfolding A_def B_def C_def bisect_def split_def by simp
   927   have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   928     unfolding A_def B_def C_def bisect_def split_def by simp
   929 
   930   have width: "\<And>n. B n - A n = (b - a) / 2^n"
   931     apply (simp add: eq_divide_eq)
   932     apply (induct_tac n, simp)
   933     apply (simp add: C_def avg_def algebra_simps)
   934     done
   935 
   936   have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
   937     apply (simp add: divide_less_eq)
   938     apply (subst mult_commute)
   939     apply (frule_tac y=y in ex_less_of_nat_mult)
   940     apply clarify
   941     apply (rule_tac x=n in exI)
   942     apply (erule less_trans)
   943     apply (rule mult_strict_right_mono)
   944     apply (rule le_less_trans [OF _ of_nat_less_two_power])
   945     apply simp
   946     apply assumption
   947     done
   948 
   949   have PA: "\<And>n. \<not> P (A n)"
   950     by (induct_tac n, simp_all add: a)
   951   have PB: "\<And>n. P (B n)"
   952     by (induct_tac n, simp_all add: b)
   953   have ab: "a < b"
   954     using a b unfolding P_def
   955     apply (clarsimp simp add: not_le)
   956     apply (drule (1) bspec)
   957     apply (drule (1) less_le_trans)
   958     apply (simp add: of_rat_less)
   959     done
   960   have AB: "\<And>n. A n < B n"
   961     by (induct_tac n, simp add: ab, simp add: C_def avg_def)
   962   have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   963     apply (auto simp add: le_less [where 'a=nat])
   964     apply (erule less_Suc_induct)
   965     apply (clarsimp simp add: C_def avg_def)
   966     apply (simp add: add_divide_distrib [symmetric])
   967     apply (rule AB [THEN less_imp_le])
   968     apply simp
   969     done
   970   have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   971     apply (auto simp add: le_less [where 'a=nat])
   972     apply (erule less_Suc_induct)
   973     apply (clarsimp simp add: C_def avg_def)
   974     apply (simp add: add_divide_distrib [symmetric])
   975     apply (rule AB [THEN less_imp_le])
   976     apply simp
   977     done
   978   have cauchy_lemma:
   979     "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   980     apply (rule cauchyI)
   981     apply (drule twos [where y="b - a"])
   982     apply (erule exE)
   983     apply (rule_tac x=n in exI, clarify, rename_tac i j)
   984     apply (rule_tac y="B n - A n" in le_less_trans) defer
   985     apply (simp add: width)
   986     apply (drule_tac x=n in spec)
   987     apply (frule_tac x=i in spec, drule (1) mp)
   988     apply (frule_tac x=j in spec, drule (1) mp)
   989     apply (frule A_mono, drule B_mono)
   990     apply (frule A_mono, drule B_mono)
   991     apply arith
   992     done
   993   have "cauchy A"
   994     apply (rule cauchy_lemma [rule_format])
   995     apply (simp add: A_mono)
   996     apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   997     done
   998   have "cauchy B"
   999     apply (rule cauchy_lemma [rule_format])
  1000     apply (simp add: B_mono)
  1001     apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
  1002     done
  1003   have 1: "\<forall>x\<in>S. x \<le> Real B"
  1004   proof
  1005     fix x assume "x \<in> S"
  1006     then show "x \<le> Real B"
  1007       using PB [unfolded P_def] `cauchy B`
  1008       by (simp add: le_RealI)
  1009   qed
  1010   have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
  1011     apply clarify
  1012     apply (erule contrapos_pp)
  1013     apply (simp add: not_le)
  1014     apply (drule less_RealD [OF `cauchy A`], clarify)
  1015     apply (subgoal_tac "\<not> P (A n)")
  1016     apply (simp add: P_def not_le, clarify)
  1017     apply (erule rev_bexI)
  1018     apply (erule (1) less_trans)
  1019     apply (simp add: PA)
  1020     done
  1021   have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
  1022   proof (rule vanishesI)
  1023     fix r :: rat assume "0 < r"
  1024     then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
  1025       using twos by fast
  1026     have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
  1027     proof (clarify)
  1028       fix n assume n: "k \<le> n"
  1029       have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
  1030         by simp
  1031       also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
  1032         using n by (simp add: divide_left_mono mult_pos_pos)
  1033       also note k
  1034       finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
  1035     qed
  1036     thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
  1037   qed
  1038   hence 3: "Real B = Real A"
  1039     by (simp add: eq_Real `cauchy A` `cauchy B` width)
  1040   show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
  1041     using 1 2 3 by (rule_tac x="Real B" in exI, simp)
  1042 qed
  1043 
  1044 subsection {* Hiding implementation details *}
  1045 
  1046 hide_const (open) vanishes cauchy positive Real real_case
  1047 
  1048 declare Real_induct [induct del]
  1049 declare Abs_real_induct [induct del]
  1050 declare Abs_real_cases [cases del]
  1051 
  1052 subsection{*More Lemmas*}
  1053 
  1054 text {* BH: These lemmas should not be necessary; they should be
  1055 covered by existing simp rules and simplification procedures. *}
  1056 
  1057 lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
  1058 by simp (* redundant with mult_cancel_left *)
  1059 
  1060 lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
  1061 by simp (* redundant with mult_cancel_right *)
  1062 
  1063 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
  1064 by simp (* solved by linordered_ring_less_cancel_factor simproc *)
  1065 
  1066 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
  1067 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1068 
  1069 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
  1070 by (rule mult_le_cancel_left_pos)
  1071 (* BH: Why doesn't "simp" prove this one, like it does the last one? *)
  1072 
  1073 
  1074 subsection {* Embedding numbers into the Reals *}
  1075 
  1076 abbreviation
  1077   real_of_nat :: "nat \<Rightarrow> real"
  1078 where
  1079   "real_of_nat \<equiv> of_nat"
  1080 
  1081 abbreviation
  1082   real_of_int :: "int \<Rightarrow> real"
  1083 where
  1084   "real_of_int \<equiv> of_int"
  1085 
  1086 abbreviation
  1087   real_of_rat :: "rat \<Rightarrow> real"
  1088 where
  1089   "real_of_rat \<equiv> of_rat"
  1090 
  1091 consts
  1092   (*overloaded constant for injecting other types into "real"*)
  1093   real :: "'a => real"
  1094 
  1095 defs (overloaded)
  1096   real_of_nat_def [code_unfold]: "real == real_of_nat"
  1097   real_of_int_def [code_unfold]: "real == real_of_int"
  1098 
  1099 declare [[coercion_enabled]]
  1100 declare [[coercion "real::nat\<Rightarrow>real"]]
  1101 declare [[coercion "real::int\<Rightarrow>real"]]
  1102 declare [[coercion "int"]]
  1103 
  1104 declare [[coercion_map map]]
  1105 declare [[coercion_map "% f g h x. g (h (f x))"]]
  1106 declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
  1107 
  1108 lemma real_eq_of_nat: "real = of_nat"
  1109   unfolding real_of_nat_def ..
  1110 
  1111 lemma real_eq_of_int: "real = of_int"
  1112   unfolding real_of_int_def ..
  1113 
  1114 lemma real_of_int_zero [simp]: "real (0::int) = 0"  
  1115 by (simp add: real_of_int_def) 
  1116 
  1117 lemma real_of_one [simp]: "real (1::int) = (1::real)"
  1118 by (simp add: real_of_int_def) 
  1119 
  1120 lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
  1121 by (simp add: real_of_int_def) 
  1122 
  1123 lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
  1124 by (simp add: real_of_int_def) 
  1125 
  1126 lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
  1127 by (simp add: real_of_int_def) 
  1128 
  1129 lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
  1130 by (simp add: real_of_int_def) 
  1131 
  1132 lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
  1133 by (simp add: real_of_int_def of_int_power)
  1134 
  1135 lemmas power_real_of_int = real_of_int_power [symmetric]
  1136 
  1137 lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
  1138   apply (subst real_eq_of_int)+
  1139   apply (rule of_int_setsum)
  1140 done
  1141 
  1142 lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
  1143     (PROD x:A. real(f x))"
  1144   apply (subst real_eq_of_int)+
  1145   apply (rule of_int_setprod)
  1146 done
  1147 
  1148 lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
  1149 by (simp add: real_of_int_def) 
  1150 
  1151 lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
  1152 by (simp add: real_of_int_def) 
  1153 
  1154 lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
  1155 by (simp add: real_of_int_def) 
  1156 
  1157 lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
  1158 by (simp add: real_of_int_def) 
  1159 
  1160 lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
  1161 by (simp add: real_of_int_def) 
  1162 
  1163 lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
  1164 by (simp add: real_of_int_def) 
  1165 
  1166 lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
  1167 by (simp add: real_of_int_def)
  1168 
  1169 lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
  1170 by (simp add: real_of_int_def)
  1171 
  1172 lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
  1173 by (auto simp add: abs_if)
  1174 
  1175 lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
  1176   apply (subgoal_tac "real n + 1 = real (n + 1)")
  1177   apply (simp del: real_of_int_add)
  1178   apply auto
  1179 done
  1180 
  1181 lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
  1182   apply (subgoal_tac "real m + 1 = real (m + 1)")
  1183   apply (simp del: real_of_int_add)
  1184   apply simp
  1185 done
  1186 
  1187 lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
  1188     real (x div d) + (real (x mod d)) / (real d)"
  1189 proof -
  1190   assume d: "d ~= 0"
  1191   have "x = (x div d) * d + x mod d"
  1192     by auto
  1193   then have "real x = real (x div d) * real d + real(x mod d)"
  1194     by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
  1195   then have "real x / real d = ... / real d"
  1196     by simp
  1197   then show ?thesis
  1198     by (auto simp add: add_divide_distrib algebra_simps d)
  1199 qed
  1200 
  1201 lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
  1202     real(n div d) = real n / real d"
  1203   apply (frule real_of_int_div_aux [of d n])
  1204   apply simp
  1205   apply (simp add: dvd_eq_mod_eq_0)
  1206 done
  1207 
  1208 lemma real_of_int_div2:
  1209   "0 <= real (n::int) / real (x) - real (n div x)"
  1210   apply (case_tac "x = 0")
  1211   apply simp
  1212   apply (case_tac "0 < x")
  1213   apply (simp add: algebra_simps)
  1214   apply (subst real_of_int_div_aux)
  1215   apply simp
  1216   apply simp
  1217   apply (subst zero_le_divide_iff)
  1218   apply auto
  1219   apply (simp add: algebra_simps)
  1220   apply (subst real_of_int_div_aux)
  1221   apply simp
  1222   apply simp
  1223   apply (subst zero_le_divide_iff)
  1224   apply auto
  1225 done
  1226 
  1227 lemma real_of_int_div3:
  1228   "real (n::int) / real (x) - real (n div x) <= 1"
  1229   apply(case_tac "x = 0")
  1230   apply simp
  1231   apply (simp add: algebra_simps)
  1232   apply (subst real_of_int_div_aux)
  1233   apply assumption
  1234   apply simp
  1235   apply (subst divide_le_eq)
  1236   apply clarsimp
  1237   apply (rule conjI)
  1238   apply (rule impI)
  1239   apply (rule order_less_imp_le)
  1240   apply simp
  1241   apply (rule impI)
  1242   apply (rule order_less_imp_le)
  1243   apply simp
  1244 done
  1245 
  1246 lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
  1247 by (insert real_of_int_div2 [of n x], simp)
  1248 
  1249 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1250 unfolding real_of_int_def by (rule Ints_of_int)
  1251 
  1252 
  1253 subsection{*Embedding the Naturals into the Reals*}
  1254 
  1255 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1256 by (simp add: real_of_nat_def)
  1257 
  1258 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1259 by (simp add: real_of_nat_def)
  1260 
  1261 lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
  1262 by (simp add: real_of_nat_def)
  1263 
  1264 lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
  1265 by (simp add: real_of_nat_def)
  1266 
  1267 (*Not for addsimps: often the LHS is used to represent a positive natural*)
  1268 lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
  1269 by (simp add: real_of_nat_def)
  1270 
  1271 lemma real_of_nat_less_iff [iff]: 
  1272      "(real (n::nat) < real m) = (n < m)"
  1273 by (simp add: real_of_nat_def)
  1274 
  1275 lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
  1276 by (simp add: real_of_nat_def)
  1277 
  1278 lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
  1279 by (simp add: real_of_nat_def zero_le_imp_of_nat)
  1280 
  1281 lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
  1282 by (simp add: real_of_nat_def del: of_nat_Suc)
  1283 
  1284 lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
  1285 by (simp add: real_of_nat_def of_nat_mult)
  1286 
  1287 lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
  1288 by (simp add: real_of_nat_def of_nat_power)
  1289 
  1290 lemmas power_real_of_nat = real_of_nat_power [symmetric]
  1291 
  1292 lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
  1293     (SUM x:A. real(f x))"
  1294   apply (subst real_eq_of_nat)+
  1295   apply (rule of_nat_setsum)
  1296 done
  1297 
  1298 lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
  1299     (PROD x:A. real(f x))"
  1300   apply (subst real_eq_of_nat)+
  1301   apply (rule of_nat_setprod)
  1302 done
  1303 
  1304 lemma real_of_card: "real (card A) = setsum (%x.1) A"
  1305   apply (subst card_eq_setsum)
  1306   apply (subst real_of_nat_setsum)
  1307   apply simp
  1308 done
  1309 
  1310 lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
  1311 by (simp add: real_of_nat_def)
  1312 
  1313 lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
  1314 by (simp add: real_of_nat_def)
  1315 
  1316 lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
  1317 by (simp add: add: real_of_nat_def of_nat_diff)
  1318 
  1319 lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
  1320 by (auto simp: real_of_nat_def)
  1321 
  1322 lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
  1323 by (simp add: add: real_of_nat_def)
  1324 
  1325 lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
  1326 by (simp add: add: real_of_nat_def)
  1327 
  1328 lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
  1329   apply (subgoal_tac "real n + 1 = real (Suc n)")
  1330   apply simp
  1331   apply (auto simp add: real_of_nat_Suc)
  1332 done
  1333 
  1334 lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
  1335   apply (subgoal_tac "real m + 1 = real (Suc m)")
  1336   apply (simp add: less_Suc_eq_le)
  1337   apply (simp add: real_of_nat_Suc)
  1338 done
  1339 
  1340 lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
  1341     real (x div d) + (real (x mod d)) / (real d)"
  1342 proof -
  1343   assume d: "0 < d"
  1344   have "x = (x div d) * d + x mod d"
  1345     by auto
  1346   then have "real x = real (x div d) * real d + real(x mod d)"
  1347     by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
  1348   then have "real x / real d = \<dots> / real d"
  1349     by simp
  1350   then show ?thesis
  1351     by (auto simp add: add_divide_distrib algebra_simps d)
  1352 qed
  1353 
  1354 lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
  1355     real(n div d) = real n / real d"
  1356   apply (frule real_of_nat_div_aux [of d n])
  1357   apply simp
  1358   apply (subst dvd_eq_mod_eq_0 [THEN sym])
  1359   apply assumption
  1360 done
  1361 
  1362 lemma real_of_nat_div2:
  1363   "0 <= real (n::nat) / real (x) - real (n div x)"
  1364 apply(case_tac "x = 0")
  1365  apply (simp)
  1366 apply (simp add: algebra_simps)
  1367 apply (subst real_of_nat_div_aux)
  1368  apply simp
  1369 apply simp
  1370 apply (subst zero_le_divide_iff)
  1371 apply simp
  1372 done
  1373 
  1374 lemma real_of_nat_div3:
  1375   "real (n::nat) / real (x) - real (n div x) <= 1"
  1376 apply(case_tac "x = 0")
  1377 apply (simp)
  1378 apply (simp add: algebra_simps)
  1379 apply (subst real_of_nat_div_aux)
  1380  apply simp
  1381 apply simp
  1382 done
  1383 
  1384 lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
  1385 by (insert real_of_nat_div2 [of n x], simp)
  1386 
  1387 lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
  1388 by (simp add: real_of_int_def real_of_nat_def)
  1389 
  1390 lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
  1391   apply (subgoal_tac "real(int(nat x)) = real(nat x)")
  1392   apply force
  1393   apply (simp only: real_of_int_of_nat_eq)
  1394 done
  1395 
  1396 lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
  1397 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1398 
  1399 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1400 unfolding real_of_nat_def by (rule Ints_of_nat)
  1401 
  1402 
  1403 subsection{* Rationals *}
  1404 
  1405 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
  1406 by (simp add: real_eq_of_nat)
  1407 
  1408 
  1409 lemma Rats_eq_int_div_int:
  1410   "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
  1411 proof
  1412   show "\<rat> \<subseteq> ?S"
  1413   proof
  1414     fix x::real assume "x : \<rat>"
  1415     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1416     have "of_rat r : ?S"
  1417       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1418     thus "x : ?S" using `x = of_rat r` by simp
  1419   qed
  1420 next
  1421   show "?S \<subseteq> \<rat>"
  1422   proof(auto simp:Rats_def)
  1423     fix i j :: int assume "j \<noteq> 0"
  1424     hence "real i / real j = of_rat(Fract i j)"
  1425       by (simp add:of_rat_rat real_eq_of_int)
  1426     thus "real i / real j \<in> range of_rat" by blast
  1427   qed
  1428 qed
  1429 
  1430 lemma Rats_eq_int_div_nat:
  1431   "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
  1432 proof(auto simp:Rats_eq_int_div_int)
  1433   fix i j::int assume "j \<noteq> 0"
  1434   show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
  1435   proof cases
  1436     assume "j>0"
  1437     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1438       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1439     thus ?thesis by blast
  1440   next
  1441     assume "~ j>0"
  1442     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1443       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1444     thus ?thesis by blast
  1445   qed
  1446 next
  1447   fix i::int and n::nat assume "0 < n"
  1448   hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
  1449   thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
  1450 qed
  1451 
  1452 lemma Rats_abs_nat_div_natE:
  1453   assumes "x \<in> \<rat>"
  1454   obtains m n :: nat
  1455   where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1456 proof -
  1457   from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
  1458     by(auto simp add: Rats_eq_int_div_nat)
  1459   hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
  1460   then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1461   let ?gcd = "gcd m n"
  1462   from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
  1463   let ?k = "m div ?gcd"
  1464   let ?l = "n div ?gcd"
  1465   let ?gcd' = "gcd ?k ?l"
  1466   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
  1467     by (rule dvd_mult_div_cancel)
  1468   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
  1469     by (rule dvd_mult_div_cancel)
  1470   from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
  1471   moreover
  1472   have "\<bar>x\<bar> = real ?k / real ?l"
  1473   proof -
  1474     from gcd have "real ?k / real ?l =
  1475         real (?gcd * ?k) / real (?gcd * ?l)" by simp
  1476     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1477     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1478     finally show ?thesis ..
  1479   qed
  1480   moreover
  1481   have "?gcd' = 1"
  1482   proof -
  1483     have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1484       by (rule gcd_mult_distrib_nat)
  1485     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1486     with gcd show ?thesis by auto
  1487   qed
  1488   ultimately show ?thesis ..
  1489 qed
  1490 
  1491 
  1492 subsection{*Numerals and Arithmetic*}
  1493 
  1494 lemma [code_unfold_post]:
  1495   "number_of k = real_of_int (number_of k)"
  1496   unfolding number_of_is_id number_of_real_def ..
  1497 
  1498 
  1499 text{*Collapse applications of @{term real} to @{term number_of}*}
  1500 lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
  1501 by (simp add: real_of_int_def)
  1502 
  1503 lemma real_of_nat_number_of [simp]:
  1504      "real (number_of v :: nat) =  
  1505         (if neg (number_of v :: int) then 0  
  1506          else (number_of v :: real))"
  1507 by (simp add: real_of_int_of_nat_eq [symmetric])
  1508 
  1509 declaration {*
  1510   K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
  1511     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
  1512   #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
  1513     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
  1514   #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
  1515       @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
  1516       @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
  1517       @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
  1518       @{thm real_of_nat_number_of}, @{thm real_number_of}]
  1519   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
  1520   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
  1521 *}
  1522 
  1523 
  1524 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
  1525 
  1526 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
  1527 by arith
  1528 
  1529 text {* FIXME: redundant with @{text add_eq_0_iff} below *}
  1530 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
  1531 by auto
  1532 
  1533 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
  1534 by auto
  1535 
  1536 lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
  1537 by auto
  1538 
  1539 lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
  1540 by auto
  1541 
  1542 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
  1543 by auto
  1544 
  1545 subsection {* Lemmas about powers *}
  1546 
  1547 text {* FIXME: declare this in Rings.thy or not at all *}
  1548 declare abs_mult_self [simp]
  1549 
  1550 (* used by Import/HOL/real.imp *)
  1551 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1552 by simp
  1553 
  1554 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
  1555 apply (induct "n")
  1556 apply (auto simp add: real_of_nat_Suc)
  1557 apply (subst mult_2)
  1558 apply (erule add_less_le_mono)
  1559 apply (rule two_realpow_ge_one)
  1560 done
  1561 
  1562 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1563 lemma realpow_Suc_le_self:
  1564   fixes r :: "'a::linordered_semidom"
  1565   shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
  1566 by (insert power_decreasing [of 1 "Suc n" r], simp)
  1567 
  1568 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1569 lemma realpow_minus_mult:
  1570   fixes x :: "'a::monoid_mult"
  1571   shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
  1572 by (simp add: power_commutes split add: nat_diff_split)
  1573 
  1574 text {* FIXME: declare this [simp] for all types, or not at all *}
  1575 lemma real_two_squares_add_zero_iff [simp]:
  1576   "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
  1577 by (rule sum_squares_eq_zero_iff)
  1578 
  1579 text {* FIXME: declare this [simp] for all types, or not at all *}
  1580 lemma realpow_two_sum_zero_iff [simp]:
  1581      "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
  1582 by (rule sum_power2_eq_zero_iff)
  1583 
  1584 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
  1585 by (rule_tac y = 0 in order_trans, auto)
  1586 
  1587 lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
  1588 by (auto simp add: power2_eq_square)
  1589 
  1590 
  1591 subsection{*Density of the Reals*}
  1592 
  1593 lemma real_lbound_gt_zero:
  1594      "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
  1595 apply (rule_tac x = " (min d1 d2) /2" in exI)
  1596 apply (simp add: min_def)
  1597 done
  1598 
  1599 
  1600 text{*Similar results are proved in @{text Fields}*}
  1601 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
  1602   by auto
  1603 
  1604 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1605   by auto
  1606 
  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 {* Implementation of rational real numbers *}
  1631 
  1632 definition Ratreal :: "rat \<Rightarrow> real" where
  1633   [simp]: "Ratreal = of_rat"
  1634 
  1635 code_datatype Ratreal
  1636 
  1637 lemma Ratreal_number_collapse [code_post]:
  1638   "Ratreal 0 = 0"
  1639   "Ratreal 1 = 1"
  1640   "Ratreal (number_of k) = number_of k"
  1641 by simp_all
  1642 
  1643 lemma zero_real_code [code, code_unfold]:
  1644   "0 = Ratreal 0"
  1645 by simp
  1646 
  1647 lemma one_real_code [code, code_unfold]:
  1648   "1 = Ratreal 1"
  1649 by simp
  1650 
  1651 lemma number_of_real_code [code_unfold]:
  1652   "number_of k = Ratreal (number_of k)"
  1653 by simp
  1654 
  1655 lemma Ratreal_number_of_quotient [code_post]:
  1656   "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
  1657 by simp
  1658 
  1659 lemma Ratreal_number_of_quotient2 [code_post]:
  1660   "Ratreal (number_of r / number_of s) = number_of r / number_of s"
  1661 unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
  1662 
  1663 instantiation real :: equal
  1664 begin
  1665 
  1666 definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
  1667 
  1668 instance proof
  1669 qed (simp add: equal_real_def)
  1670 
  1671 lemma real_equal_code [code]:
  1672   "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
  1673   by (simp add: equal_real_def equal)
  1674 
  1675 lemma [code nbe]:
  1676   "HOL.equal (x::real) x \<longleftrightarrow> True"
  1677   by (rule equal_refl)
  1678 
  1679 end
  1680 
  1681 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  1682   by (simp add: of_rat_less_eq)
  1683 
  1684 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  1685   by (simp add: of_rat_less)
  1686 
  1687 lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  1688   by (simp add: of_rat_add)
  1689 
  1690 lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  1691   by (simp add: of_rat_mult)
  1692 
  1693 lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  1694   by (simp add: of_rat_minus)
  1695 
  1696 lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  1697   by (simp add: of_rat_diff)
  1698 
  1699 lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  1700   by (simp add: of_rat_inverse)
  1701  
  1702 lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  1703   by (simp add: of_rat_divide)
  1704 
  1705 lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  1706   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)
  1707 
  1708 definition (in term_syntax)
  1709   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  1710   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  1711 
  1712 notation fcomp (infixl "\<circ>>" 60)
  1713 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1714 
  1715 instantiation real :: random
  1716 begin
  1717 
  1718 definition
  1719   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  1720 
  1721 instance ..
  1722 
  1723 end
  1724 
  1725 no_notation fcomp (infixl "\<circ>>" 60)
  1726 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1727 
  1728 instantiation real :: exhaustive
  1729 begin
  1730 
  1731 definition
  1732   "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
  1733 
  1734 instance ..
  1735 
  1736 end
  1737 
  1738 instantiation real :: full_exhaustive
  1739 begin
  1740 
  1741 definition
  1742   "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
  1743 
  1744 instance ..
  1745 
  1746 end
  1747 
  1748 instantiation real :: narrowing
  1749 begin
  1750 
  1751 definition
  1752   "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
  1753 
  1754 instance ..
  1755 
  1756 end
  1757 
  1758 
  1759 subsection {* Setup for Nitpick *}
  1760 
  1761 declaration {*
  1762   Nitpick_HOL.register_frac_type @{type_name real}
  1763    [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  1764     (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  1765     (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  1766     (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  1767     (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  1768     (@{const_name number_real_inst.number_of_real}, @{const_name Nitpick.number_of_frac}),
  1769     (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  1770     (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_eq_frac}),
  1771     (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  1772 *}
  1773 
  1774 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real
  1775     number_real_inst.number_of_real one_real_inst.one_real
  1776     ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  1777     times_real_inst.times_real uminus_real_inst.uminus_real
  1778     zero_real_inst.zero_real
  1779 
  1780 end