src/HOL/RealDef.thy
author haftmann
Fri Jul 09 08:11:10 2010 +0200 (2010-07-09)
changeset 37751 89e16802b6cc
parent 37397 18000f9d783e
child 37765 26bdfb7b680b
permissions -rw-r--r--
nicer xsymbol syntax for fcomp and scomp
     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   \url{HOL/ex/Dedekind_Real.thy} for an alternative construction
    18   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) set"
   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 equiv.intro)
   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 congruent.congruent [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 congruent.intro)
   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 congruent2.congruent2 [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 congruent.intro)
   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 congruent.intro)
   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 congruent.intro)
   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 subsection {* Completeness *}
   797 
   798 lemma not_positive_Real:
   799   assumes X: "cauchy X"
   800   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   801 unfolding positive_Real [OF X]
   802 apply (auto, unfold not_less)
   803 apply (erule obtain_pos_sum)
   804 apply (drule_tac x=s in spec, simp)
   805 apply (drule_tac r=t in cauchyD [OF X], clarify)
   806 apply (drule_tac x=k in spec, clarsimp)
   807 apply (rule_tac x=n in exI, clarify, rename_tac m)
   808 apply (drule_tac x=m in spec, simp)
   809 apply (drule_tac x=n in spec, simp)
   810 apply (drule spec, drule (1) mp, clarify, rename_tac i)
   811 apply (rule_tac x="max i k" in exI, simp)
   812 done
   813 
   814 lemma le_Real:
   815   assumes X: "cauchy X" and Y: "cauchy Y"
   816   shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   817 unfolding not_less [symmetric, where 'a=real] less_real_def
   818 apply (simp add: diff_Real not_positive_Real X Y)
   819 apply (simp add: diff_le_eq add_ac)
   820 done
   821 
   822 lemma le_RealI:
   823   assumes Y: "cauchy Y"
   824   shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   825 proof (induct x)
   826   fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   827   hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   828     by (simp add: of_rat_Real le_Real)
   829   {
   830     fix r :: rat assume "0 < r"
   831     then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   832       by (rule obtain_pos_sum)
   833     obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   834       using cauchyD [OF Y s] ..
   835     obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   836       using le [OF t] ..
   837     have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   838     proof (clarsimp)
   839       fix n assume n: "i \<le> n" "j \<le> n"
   840       have "X n \<le> Y i + t" using n j by simp
   841       moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
   842       ultimately show "X n \<le> Y n + r" unfolding r by simp
   843     qed
   844     hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
   845   }
   846   thus "Real X \<le> Real Y"
   847     by (simp add: of_rat_Real le_Real X Y)
   848 qed
   849 
   850 lemma Real_leI:
   851   assumes X: "cauchy X"
   852   assumes le: "\<forall>n. of_rat (X n) \<le> y"
   853   shows "Real X \<le> y"
   854 proof -
   855   have "- y \<le> - Real X"
   856     by (simp add: minus_Real X le_RealI of_rat_minus le)
   857   thus ?thesis by simp
   858 qed
   859 
   860 lemma less_RealD:
   861   assumes Y: "cauchy Y"
   862   shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   863 by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   864 
   865 lemma of_nat_less_two_power:
   866   "of_nat n < (2::'a::{linordered_idom,number_ring}) ^ n"
   867 apply (induct n)
   868 apply simp
   869 apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   870 apply (drule (1) add_le_less_mono, simp)
   871 apply simp
   872 done
   873 
   874 lemma complete_real:
   875   fixes S :: "real set"
   876   assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   877   shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   878 proof -
   879   obtain x where x: "x \<in> S" using assms(1) ..
   880   obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   881 
   882   def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
   883   obtain a where a: "\<not> P a"
   884   proof
   885     have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
   886     also have "x - 1 < x" by simp
   887     finally have "of_int (floor (x - 1)) < x" .
   888     hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
   889     then show "\<not> P (of_int (floor (x - 1)))"
   890       unfolding P_def of_rat_of_int_eq using x by fast
   891   qed
   892   obtain b where b: "P b"
   893   proof
   894     show "P (of_int (ceiling z))"
   895     unfolding P_def of_rat_of_int_eq
   896     proof
   897       fix y assume "y \<in> S"
   898       hence "y \<le> z" using z by simp
   899       also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
   900       finally show "y \<le> of_int (ceiling z)" .
   901     qed
   902   qed
   903 
   904   def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
   905   def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
   906   def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
   907   def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
   908   def C \<equiv> "\<lambda>n. avg (A n) (B n)"
   909   have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   910   have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   911   have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   912     unfolding A_def B_def C_def bisect_def split_def by simp
   913   have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   914     unfolding A_def B_def C_def bisect_def split_def by simp
   915 
   916   have width: "\<And>n. B n - A n = (b - a) / 2^n"
   917     apply (simp add: eq_divide_eq)
   918     apply (induct_tac n, simp)
   919     apply (simp add: C_def avg_def algebra_simps)
   920     done
   921 
   922   have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
   923     apply (simp add: divide_less_eq)
   924     apply (subst mult_commute)
   925     apply (frule_tac y=y in ex_less_of_nat_mult)
   926     apply clarify
   927     apply (rule_tac x=n in exI)
   928     apply (erule less_trans)
   929     apply (rule mult_strict_right_mono)
   930     apply (rule le_less_trans [OF _ of_nat_less_two_power])
   931     apply simp
   932     apply assumption
   933     done
   934 
   935   have PA: "\<And>n. \<not> P (A n)"
   936     by (induct_tac n, simp_all add: a)
   937   have PB: "\<And>n. P (B n)"
   938     by (induct_tac n, simp_all add: b)
   939   have ab: "a < b"
   940     using a b unfolding P_def
   941     apply (clarsimp simp add: not_le)
   942     apply (drule (1) bspec)
   943     apply (drule (1) less_le_trans)
   944     apply (simp add: of_rat_less)
   945     done
   946   have AB: "\<And>n. A n < B n"
   947     by (induct_tac n, simp add: ab, simp add: C_def avg_def)
   948   have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   949     apply (auto simp add: le_less [where 'a=nat])
   950     apply (erule less_Suc_induct)
   951     apply (clarsimp simp add: C_def avg_def)
   952     apply (simp add: add_divide_distrib [symmetric])
   953     apply (rule AB [THEN less_imp_le])
   954     apply simp
   955     done
   956   have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   957     apply (auto simp add: le_less [where 'a=nat])
   958     apply (erule less_Suc_induct)
   959     apply (clarsimp simp add: C_def avg_def)
   960     apply (simp add: add_divide_distrib [symmetric])
   961     apply (rule AB [THEN less_imp_le])
   962     apply simp
   963     done
   964   have cauchy_lemma:
   965     "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   966     apply (rule cauchyI)
   967     apply (drule twos [where y="b - a"])
   968     apply (erule exE)
   969     apply (rule_tac x=n in exI, clarify, rename_tac i j)
   970     apply (rule_tac y="B n - A n" in le_less_trans) defer
   971     apply (simp add: width)
   972     apply (drule_tac x=n in spec)
   973     apply (frule_tac x=i in spec, drule (1) mp)
   974     apply (frule_tac x=j in spec, drule (1) mp)
   975     apply (frule A_mono, drule B_mono)
   976     apply (frule A_mono, drule B_mono)
   977     apply arith
   978     done
   979   have "cauchy A"
   980     apply (rule cauchy_lemma [rule_format])
   981     apply (simp add: A_mono)
   982     apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   983     done
   984   have "cauchy B"
   985     apply (rule cauchy_lemma [rule_format])
   986     apply (simp add: B_mono)
   987     apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
   988     done
   989   have 1: "\<forall>x\<in>S. x \<le> Real B"
   990   proof
   991     fix x assume "x \<in> S"
   992     then show "x \<le> Real B"
   993       using PB [unfolded P_def] `cauchy B`
   994       by (simp add: le_RealI)
   995   qed
   996   have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
   997     apply clarify
   998     apply (erule contrapos_pp)
   999     apply (simp add: not_le)
  1000     apply (drule less_RealD [OF `cauchy A`], clarify)
  1001     apply (subgoal_tac "\<not> P (A n)")
  1002     apply (simp add: P_def not_le, clarify)
  1003     apply (erule rev_bexI)
  1004     apply (erule (1) less_trans)
  1005     apply (simp add: PA)
  1006     done
  1007   have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
  1008   proof (rule vanishesI)
  1009     fix r :: rat assume "0 < r"
  1010     then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
  1011       using twos by fast
  1012     have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
  1013     proof (clarify)
  1014       fix n assume n: "k \<le> n"
  1015       have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
  1016         by simp
  1017       also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
  1018         using n by (simp add: divide_left_mono mult_pos_pos)
  1019       also note k
  1020       finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
  1021     qed
  1022     thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
  1023   qed
  1024   hence 3: "Real B = Real A"
  1025     by (simp add: eq_Real `cauchy A` `cauchy B` width)
  1026   show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
  1027     using 1 2 3 by (rule_tac x="Real B" in exI, simp)
  1028 qed
  1029 
  1030 subsection {* Hiding implementation details *}
  1031 
  1032 hide_const (open) vanishes cauchy positive Real real_case
  1033 
  1034 declare Real_induct [induct del]
  1035 declare Abs_real_induct [induct del]
  1036 declare Abs_real_cases [cases del]
  1037 
  1038 subsection {* Legacy theorem names *}
  1039 
  1040 text {* TODO: Could we have a way to mark theorem names as deprecated,
  1041 and have Isabelle issue a warning and display the preferred name? *}
  1042 
  1043 lemmas real_diff_def = minus_real_def [of "r" "s", standard]
  1044 lemmas real_divide_def = divide_real_def [of "R" "S", standard]
  1045 lemmas real_less_def = less_le [of "x::real" "y", standard]
  1046 lemmas real_abs_def = abs_real_def [of "r", standard]
  1047 lemmas real_sgn_def = sgn_real_def [of "x", standard]
  1048 lemmas real_mult_commute = mult_commute [of "z::real" "w", standard]
  1049 lemmas real_mult_assoc = mult_assoc [of "z1::real" "z2" "z3", standard]
  1050 lemmas real_mult_1 = mult_1_left [of "z::real", standard]
  1051 lemmas real_add_mult_distrib = left_distrib [of "z1::real" "z2" "w", standard]
  1052 lemmas real_zero_not_eq_one = zero_neq_one [where 'a=real]
  1053 lemmas real_mult_inverse_left = left_inverse [of "x::real", standard]
  1054 lemmas INVERSE_ZERO = inverse_zero [where 'a=real]
  1055 lemmas real_le_refl = order_refl [of "w::real", standard]
  1056 lemmas real_le_antisym = order_antisym [of "z::real" "w", standard]
  1057 lemmas real_le_trans = order_trans [of "i::real" "j" "k", standard]
  1058 lemmas real_le_linear = linear [of "z::real" "w", standard]
  1059 lemmas real_le_eq_diff = le_iff_diff_le_0 [of "x::real" "y", standard]
  1060 lemmas real_add_left_mono = add_left_mono [of "x::real" "y" "z", standard]
  1061 lemmas real_mult_order = mult_pos_pos [of "x::real" "y", standard]
  1062 lemmas real_mult_less_mono2 =
  1063   mult_strict_left_mono [of "x::real" "y" "z", COMP swap_prems_rl, standard]
  1064 
  1065 subsection{*More Lemmas*}
  1066 
  1067 text {* BH: These lemmas should not be necessary; they should be
  1068 covered by existing simp rules and simplification procedures. *}
  1069 
  1070 lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
  1071 by simp (* redundant with mult_cancel_left *)
  1072 
  1073 lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
  1074 by simp (* redundant with mult_cancel_right *)
  1075 
  1076 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
  1077 by simp (* solved by linordered_ring_less_cancel_factor simproc *)
  1078 
  1079 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
  1080 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
  1081 
  1082 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
  1083 by (rule mult_le_cancel_left_pos)
  1084 (* BH: Why doesn't "simp" prove this one, like it does the last one? *)
  1085 
  1086 
  1087 subsection {* Embedding numbers into the Reals *}
  1088 
  1089 abbreviation
  1090   real_of_nat :: "nat \<Rightarrow> real"
  1091 where
  1092   "real_of_nat \<equiv> of_nat"
  1093 
  1094 abbreviation
  1095   real_of_int :: "int \<Rightarrow> real"
  1096 where
  1097   "real_of_int \<equiv> of_int"
  1098 
  1099 abbreviation
  1100   real_of_rat :: "rat \<Rightarrow> real"
  1101 where
  1102   "real_of_rat \<equiv> of_rat"
  1103 
  1104 consts
  1105   (*overloaded constant for injecting other types into "real"*)
  1106   real :: "'a => real"
  1107 
  1108 defs (overloaded)
  1109   real_of_nat_def [code_unfold]: "real == real_of_nat"
  1110   real_of_int_def [code_unfold]: "real == real_of_int"
  1111 
  1112 lemma real_eq_of_nat: "real = of_nat"
  1113   unfolding real_of_nat_def ..
  1114 
  1115 lemma real_eq_of_int: "real = of_int"
  1116   unfolding real_of_int_def ..
  1117 
  1118 lemma real_of_int_zero [simp]: "real (0::int) = 0"  
  1119 by (simp add: real_of_int_def) 
  1120 
  1121 lemma real_of_one [simp]: "real (1::int) = (1::real)"
  1122 by (simp add: real_of_int_def) 
  1123 
  1124 lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
  1125 by (simp add: real_of_int_def) 
  1126 
  1127 lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
  1128 by (simp add: real_of_int_def) 
  1129 
  1130 lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
  1131 by (simp add: real_of_int_def) 
  1132 
  1133 lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
  1134 by (simp add: real_of_int_def) 
  1135 
  1136 lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
  1137 by (simp add: real_of_int_def of_int_power)
  1138 
  1139 lemmas power_real_of_int = real_of_int_power [symmetric]
  1140 
  1141 lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
  1142   apply (subst real_eq_of_int)+
  1143   apply (rule of_int_setsum)
  1144 done
  1145 
  1146 lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
  1147     (PROD x:A. real(f x))"
  1148   apply (subst real_eq_of_int)+
  1149   apply (rule of_int_setprod)
  1150 done
  1151 
  1152 lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
  1153 by (simp add: real_of_int_def) 
  1154 
  1155 lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
  1156 by (simp add: real_of_int_def) 
  1157 
  1158 lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
  1159 by (simp add: real_of_int_def) 
  1160 
  1161 lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
  1162 by (simp add: real_of_int_def) 
  1163 
  1164 lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
  1165 by (simp add: real_of_int_def) 
  1166 
  1167 lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
  1168 by (simp add: real_of_int_def) 
  1169 
  1170 lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
  1171 by (simp add: real_of_int_def)
  1172 
  1173 lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
  1174 by (simp add: real_of_int_def)
  1175 
  1176 lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
  1177 by (auto simp add: abs_if)
  1178 
  1179 lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
  1180   apply (subgoal_tac "real n + 1 = real (n + 1)")
  1181   apply (simp del: real_of_int_add)
  1182   apply auto
  1183 done
  1184 
  1185 lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
  1186   apply (subgoal_tac "real m + 1 = real (m + 1)")
  1187   apply (simp del: real_of_int_add)
  1188   apply simp
  1189 done
  1190 
  1191 lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
  1192     real (x div d) + (real (x mod d)) / (real d)"
  1193 proof -
  1194   assume "d ~= 0"
  1195   have "x = (x div d) * d + x mod d"
  1196     by auto
  1197   then have "real x = real (x div d) * real d + real(x mod d)"
  1198     by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
  1199   then have "real x / real d = ... / real d"
  1200     by simp
  1201   then show ?thesis
  1202     by (auto simp add: add_divide_distrib algebra_simps prems)
  1203 qed
  1204 
  1205 lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
  1206     real(n div d) = real n / real d"
  1207   apply (frule real_of_int_div_aux [of d n])
  1208   apply simp
  1209   apply (simp add: dvd_eq_mod_eq_0)
  1210 done
  1211 
  1212 lemma real_of_int_div2:
  1213   "0 <= real (n::int) / real (x) - real (n div x)"
  1214   apply (case_tac "x = 0")
  1215   apply simp
  1216   apply (case_tac "0 < x")
  1217   apply (simp add: algebra_simps)
  1218   apply (subst real_of_int_div_aux)
  1219   apply simp
  1220   apply simp
  1221   apply (subst zero_le_divide_iff)
  1222   apply auto
  1223   apply (simp add: algebra_simps)
  1224   apply (subst real_of_int_div_aux)
  1225   apply simp
  1226   apply simp
  1227   apply (subst zero_le_divide_iff)
  1228   apply auto
  1229 done
  1230 
  1231 lemma real_of_int_div3:
  1232   "real (n::int) / real (x) - real (n div x) <= 1"
  1233   apply(case_tac "x = 0")
  1234   apply simp
  1235   apply (simp add: algebra_simps)
  1236   apply (subst real_of_int_div_aux)
  1237   apply assumption
  1238   apply simp
  1239   apply (subst divide_le_eq)
  1240   apply clarsimp
  1241   apply (rule conjI)
  1242   apply (rule impI)
  1243   apply (rule order_less_imp_le)
  1244   apply simp
  1245   apply (rule impI)
  1246   apply (rule order_less_imp_le)
  1247   apply simp
  1248 done
  1249 
  1250 lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
  1251 by (insert real_of_int_div2 [of n x], simp)
  1252 
  1253 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1254 unfolding real_of_int_def by (rule Ints_of_int)
  1255 
  1256 
  1257 subsection{*Embedding the Naturals into the Reals*}
  1258 
  1259 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1260 by (simp add: real_of_nat_def)
  1261 
  1262 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1263 by (simp add: real_of_nat_def)
  1264 
  1265 lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
  1266 by (simp add: real_of_nat_def)
  1267 
  1268 lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
  1269 by (simp add: real_of_nat_def)
  1270 
  1271 (*Not for addsimps: often the LHS is used to represent a positive natural*)
  1272 lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
  1273 by (simp add: real_of_nat_def)
  1274 
  1275 lemma real_of_nat_less_iff [iff]: 
  1276      "(real (n::nat) < real m) = (n < m)"
  1277 by (simp add: real_of_nat_def)
  1278 
  1279 lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
  1280 by (simp add: real_of_nat_def)
  1281 
  1282 lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
  1283 by (simp add: real_of_nat_def zero_le_imp_of_nat)
  1284 
  1285 lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
  1286 by (simp add: real_of_nat_def del: of_nat_Suc)
  1287 
  1288 lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
  1289 by (simp add: real_of_nat_def of_nat_mult)
  1290 
  1291 lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
  1292 by (simp add: real_of_nat_def of_nat_power)
  1293 
  1294 lemmas power_real_of_nat = real_of_nat_power [symmetric]
  1295 
  1296 lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
  1297     (SUM x:A. real(f x))"
  1298   apply (subst real_eq_of_nat)+
  1299   apply (rule of_nat_setsum)
  1300 done
  1301 
  1302 lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
  1303     (PROD x:A. real(f x))"
  1304   apply (subst real_eq_of_nat)+
  1305   apply (rule of_nat_setprod)
  1306 done
  1307 
  1308 lemma real_of_card: "real (card A) = setsum (%x.1) A"
  1309   apply (subst card_eq_setsum)
  1310   apply (subst real_of_nat_setsum)
  1311   apply simp
  1312 done
  1313 
  1314 lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
  1315 by (simp add: real_of_nat_def)
  1316 
  1317 lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
  1318 by (simp add: real_of_nat_def)
  1319 
  1320 lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
  1321 by (simp add: add: real_of_nat_def of_nat_diff)
  1322 
  1323 lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
  1324 by (auto simp: real_of_nat_def)
  1325 
  1326 lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
  1327 by (simp add: add: real_of_nat_def)
  1328 
  1329 lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
  1330 by (simp add: add: real_of_nat_def)
  1331 
  1332 lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
  1333   apply (subgoal_tac "real n + 1 = real (Suc n)")
  1334   apply simp
  1335   apply (auto simp add: real_of_nat_Suc)
  1336 done
  1337 
  1338 lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
  1339   apply (subgoal_tac "real m + 1 = real (Suc m)")
  1340   apply (simp add: less_Suc_eq_le)
  1341   apply (simp add: real_of_nat_Suc)
  1342 done
  1343 
  1344 lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
  1345     real (x div d) + (real (x mod d)) / (real d)"
  1346 proof -
  1347   assume "0 < d"
  1348   have "x = (x div d) * d + x mod d"
  1349     by auto
  1350   then have "real x = real (x div d) * real d + real(x mod d)"
  1351     by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
  1352   then have "real x / real d = \<dots> / real d"
  1353     by simp
  1354   then show ?thesis
  1355     by (auto simp add: add_divide_distrib algebra_simps prems)
  1356 qed
  1357 
  1358 lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
  1359     real(n div d) = real n / real d"
  1360   apply (frule real_of_nat_div_aux [of d n])
  1361   apply simp
  1362   apply (subst dvd_eq_mod_eq_0 [THEN sym])
  1363   apply assumption
  1364 done
  1365 
  1366 lemma real_of_nat_div2:
  1367   "0 <= real (n::nat) / real (x) - real (n div x)"
  1368 apply(case_tac "x = 0")
  1369  apply (simp)
  1370 apply (simp add: algebra_simps)
  1371 apply (subst real_of_nat_div_aux)
  1372  apply simp
  1373 apply simp
  1374 apply (subst zero_le_divide_iff)
  1375 apply simp
  1376 done
  1377 
  1378 lemma real_of_nat_div3:
  1379   "real (n::nat) / real (x) - real (n div x) <= 1"
  1380 apply(case_tac "x = 0")
  1381 apply (simp)
  1382 apply (simp add: algebra_simps)
  1383 apply (subst real_of_nat_div_aux)
  1384  apply simp
  1385 apply simp
  1386 done
  1387 
  1388 lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
  1389 by (insert real_of_nat_div2 [of n x], simp)
  1390 
  1391 lemma real_of_int_real_of_nat: "real (int n) = real n"
  1392 by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
  1393 
  1394 lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
  1395 by (simp add: real_of_int_def real_of_nat_def)
  1396 
  1397 lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
  1398   apply (subgoal_tac "real(int(nat x)) = real(nat x)")
  1399   apply force
  1400   apply (simp only: real_of_int_real_of_nat)
  1401 done
  1402 
  1403 lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
  1404 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1405 
  1406 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1407 unfolding real_of_nat_def by (rule Ints_of_nat)
  1408 
  1409 
  1410 subsection{* Rationals *}
  1411 
  1412 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
  1413 by (simp add: real_eq_of_nat)
  1414 
  1415 
  1416 lemma Rats_eq_int_div_int:
  1417   "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
  1418 proof
  1419   show "\<rat> \<subseteq> ?S"
  1420   proof
  1421     fix x::real assume "x : \<rat>"
  1422     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1423     have "of_rat r : ?S"
  1424       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1425     thus "x : ?S" using `x = of_rat r` by simp
  1426   qed
  1427 next
  1428   show "?S \<subseteq> \<rat>"
  1429   proof(auto simp:Rats_def)
  1430     fix i j :: int assume "j \<noteq> 0"
  1431     hence "real i / real j = of_rat(Fract i j)"
  1432       by (simp add:of_rat_rat real_eq_of_int)
  1433     thus "real i / real j \<in> range of_rat" by blast
  1434   qed
  1435 qed
  1436 
  1437 lemma Rats_eq_int_div_nat:
  1438   "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
  1439 proof(auto simp:Rats_eq_int_div_int)
  1440   fix i j::int assume "j \<noteq> 0"
  1441   show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
  1442   proof cases
  1443     assume "j>0"
  1444     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1445       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1446     thus ?thesis by blast
  1447   next
  1448     assume "~ j>0"
  1449     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1450       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1451     thus ?thesis by blast
  1452   qed
  1453 next
  1454   fix i::int and n::nat assume "0 < n"
  1455   hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
  1456   thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
  1457 qed
  1458 
  1459 lemma Rats_abs_nat_div_natE:
  1460   assumes "x \<in> \<rat>"
  1461   obtains m n :: nat
  1462   where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1463 proof -
  1464   from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
  1465     by(auto simp add: Rats_eq_int_div_nat)
  1466   hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
  1467   then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1468   let ?gcd = "gcd m n"
  1469   from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
  1470   let ?k = "m div ?gcd"
  1471   let ?l = "n div ?gcd"
  1472   let ?gcd' = "gcd ?k ?l"
  1473   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
  1474     by (rule dvd_mult_div_cancel)
  1475   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
  1476     by (rule dvd_mult_div_cancel)
  1477   from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
  1478   moreover
  1479   have "\<bar>x\<bar> = real ?k / real ?l"
  1480   proof -
  1481     from gcd have "real ?k / real ?l =
  1482         real (?gcd * ?k) / real (?gcd * ?l)" by simp
  1483     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1484     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1485     finally show ?thesis ..
  1486   qed
  1487   moreover
  1488   have "?gcd' = 1"
  1489   proof -
  1490     have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1491       by (rule gcd_mult_distrib_nat)
  1492     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1493     with gcd show ?thesis by auto
  1494   qed
  1495   ultimately show ?thesis ..
  1496 qed
  1497 
  1498 
  1499 subsection{*Numerals and Arithmetic*}
  1500 
  1501 declare number_of_real_def [code del]
  1502 
  1503 lemma [code_unfold_post]:
  1504   "number_of k = real_of_int (number_of k)"
  1505   unfolding number_of_is_id number_of_real_def ..
  1506 
  1507 
  1508 text{*Collapse applications of @{term real} to @{term number_of}*}
  1509 lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
  1510 by (simp add: real_of_int_def)
  1511 
  1512 lemma real_of_nat_number_of [simp]:
  1513      "real (number_of v :: nat) =  
  1514         (if neg (number_of v :: int) then 0  
  1515          else (number_of v :: real))"
  1516 by (simp add: real_of_int_real_of_nat [symmetric])
  1517 
  1518 declaration {*
  1519   K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
  1520     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
  1521   #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
  1522     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
  1523   #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
  1524       @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
  1525       @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
  1526       @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
  1527       @{thm real_of_nat_number_of}, @{thm real_number_of}]
  1528   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
  1529   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
  1530 *}
  1531 
  1532 
  1533 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
  1534 
  1535 text{*Needed in this non-standard form by Hyperreal/Transcendental*}
  1536 lemma real_0_le_divide_iff:
  1537      "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
  1538 by (auto simp add: zero_le_divide_iff)
  1539 
  1540 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
  1541 by arith
  1542 
  1543 text {* FIXME: redundant with @{text add_eq_0_iff} below *}
  1544 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
  1545 by auto
  1546 
  1547 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
  1548 by auto
  1549 
  1550 lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
  1551 by auto
  1552 
  1553 lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
  1554 by auto
  1555 
  1556 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
  1557 by auto
  1558 
  1559 subsection {* Lemmas about powers *}
  1560 
  1561 text {* FIXME: declare this in Rings.thy or not at all *}
  1562 declare abs_mult_self [simp]
  1563 
  1564 (* used by Import/HOL/real.imp *)
  1565 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1566 by simp
  1567 
  1568 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
  1569 apply (induct "n")
  1570 apply (auto simp add: real_of_nat_Suc)
  1571 apply (subst mult_2)
  1572 apply (erule add_less_le_mono)
  1573 apply (rule two_realpow_ge_one)
  1574 done
  1575 
  1576 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1577 lemma realpow_Suc_le_self:
  1578   fixes r :: "'a::linordered_semidom"
  1579   shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
  1580 by (insert power_decreasing [of 1 "Suc n" r], simp)
  1581 
  1582 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1583 lemma realpow_minus_mult:
  1584   fixes x :: "'a::monoid_mult"
  1585   shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
  1586 by (simp add: power_commutes split add: nat_diff_split)
  1587 
  1588 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1589 lemma realpow_two_diff:
  1590   fixes x :: "'a::comm_ring_1"
  1591   shows "x^Suc (Suc 0) - y^Suc (Suc 0) = (x - y) * (x + y)"
  1592 by (simp add: algebra_simps)
  1593 
  1594 text {* TODO: move elsewhere *}
  1595 lemma add_eq_0_iff:
  1596   fixes x y :: "'a::group_add"
  1597   shows "x + y = 0 \<longleftrightarrow> y = - x"
  1598 by (auto dest: minus_unique)
  1599 
  1600 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1601 lemma realpow_two_disj:
  1602   fixes x :: "'a::idom"
  1603   shows "(x^Suc (Suc 0) = y^Suc (Suc 0)) = (x = y | x = -y)"
  1604 using realpow_two_diff [of x y]
  1605 by (auto simp add: add_eq_0_iff)
  1606 
  1607 text {* FIXME: declare this [simp] for all types, or not at all *}
  1608 lemma real_two_squares_add_zero_iff [simp]:
  1609   "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
  1610 by (rule sum_squares_eq_zero_iff)
  1611 
  1612 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1613 lemma real_squared_diff_one_factored:
  1614   fixes x :: "'a::ring_1"
  1615   shows "x * x - 1 = (x + 1) * (x - 1)"
  1616 by (simp add: algebra_simps)
  1617 
  1618 text {* FIXME: declare this [simp] for all types, or not at all *}
  1619 lemma realpow_two_sum_zero_iff [simp]:
  1620      "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
  1621 by (rule sum_power2_eq_zero_iff)
  1622 
  1623 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
  1624 by (rule_tac y = 0 in order_trans, auto)
  1625 
  1626 lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
  1627 by (auto simp add: power2_eq_square)
  1628 
  1629 
  1630 subsection{*Density of the Reals*}
  1631 
  1632 lemma real_lbound_gt_zero:
  1633      "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
  1634 apply (rule_tac x = " (min d1 d2) /2" in exI)
  1635 apply (simp add: min_def)
  1636 done
  1637 
  1638 
  1639 text{*Similar results are proved in @{text Fields}*}
  1640 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
  1641   by auto
  1642 
  1643 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1644   by auto
  1645 
  1646 
  1647 subsection{*Absolute Value Function for the Reals*}
  1648 
  1649 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
  1650 by (simp add: abs_if)
  1651 
  1652 (* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
  1653 lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
  1654 by (force simp add: abs_le_iff)
  1655 
  1656 lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
  1657 by (simp add: abs_if)
  1658 
  1659 lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
  1660 by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
  1661 
  1662 lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
  1663 by simp
  1664  
  1665 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1666 by simp
  1667 
  1668 
  1669 subsection {* Implementation of rational real numbers *}
  1670 
  1671 definition Ratreal :: "rat \<Rightarrow> real" where
  1672   [simp]: "Ratreal = of_rat"
  1673 
  1674 code_datatype Ratreal
  1675 
  1676 lemma Ratreal_number_collapse [code_post]:
  1677   "Ratreal 0 = 0"
  1678   "Ratreal 1 = 1"
  1679   "Ratreal (number_of k) = number_of k"
  1680 by simp_all
  1681 
  1682 lemma zero_real_code [code, code_unfold]:
  1683   "0 = Ratreal 0"
  1684 by simp
  1685 
  1686 lemma one_real_code [code, code_unfold]:
  1687   "1 = Ratreal 1"
  1688 by simp
  1689 
  1690 lemma number_of_real_code [code_unfold]:
  1691   "number_of k = Ratreal (number_of k)"
  1692 by simp
  1693 
  1694 lemma Ratreal_number_of_quotient [code_post]:
  1695   "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
  1696 by simp
  1697 
  1698 lemma Ratreal_number_of_quotient2 [code_post]:
  1699   "Ratreal (number_of r / number_of s) = number_of r / number_of s"
  1700 unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
  1701 
  1702 instantiation real :: eq
  1703 begin
  1704 
  1705 definition "eq_class.eq (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
  1706 
  1707 instance by default (simp add: eq_real_def)
  1708 
  1709 lemma real_eq_code [code]: "eq_class.eq (Ratreal x) (Ratreal y) \<longleftrightarrow> eq_class.eq x y"
  1710   by (simp add: eq_real_def eq)
  1711 
  1712 lemma real_eq_refl [code nbe]:
  1713   "eq_class.eq (x::real) x \<longleftrightarrow> True"
  1714   by (rule HOL.eq_refl)
  1715 
  1716 end
  1717 
  1718 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  1719   by (simp add: of_rat_less_eq)
  1720 
  1721 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  1722   by (simp add: of_rat_less)
  1723 
  1724 lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  1725   by (simp add: of_rat_add)
  1726 
  1727 lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  1728   by (simp add: of_rat_mult)
  1729 
  1730 lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  1731   by (simp add: of_rat_minus)
  1732 
  1733 lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  1734   by (simp add: of_rat_diff)
  1735 
  1736 lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  1737   by (simp add: of_rat_inverse)
  1738  
  1739 lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  1740   by (simp add: of_rat_divide)
  1741 
  1742 definition (in term_syntax)
  1743   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  1744   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  1745 
  1746 notation fcomp (infixl "\<circ>>" 60)
  1747 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1748 
  1749 instantiation real :: random
  1750 begin
  1751 
  1752 definition
  1753   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  1754 
  1755 instance ..
  1756 
  1757 end
  1758 
  1759 no_notation fcomp (infixl "\<circ>>" 60)
  1760 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1761 
  1762 text {* Setup for SML code generator *}
  1763 
  1764 types_code
  1765   real ("(int */ int)")
  1766 attach (term_of) {*
  1767 fun term_of_real (p, q) =
  1768   let
  1769     val rT = HOLogic.realT
  1770   in
  1771     if q = 1 orelse p = 0 then HOLogic.mk_number rT p
  1772     else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
  1773       HOLogic.mk_number rT p $ HOLogic.mk_number rT q
  1774   end;
  1775 *}
  1776 attach (test) {*
  1777 fun gen_real i =
  1778   let
  1779     val p = random_range 0 i;
  1780     val q = random_range 1 (i + 1);
  1781     val g = Integer.gcd p q;
  1782     val p' = p div g;
  1783     val q' = q div g;
  1784     val r = (if one_of [true, false] then p' else ~ p',
  1785       if p' = 0 then 1 else q')
  1786   in
  1787     (r, fn () => term_of_real r)
  1788   end;
  1789 *}
  1790 
  1791 consts_code
  1792   Ratreal ("(_)")
  1793 
  1794 consts_code
  1795   "of_int :: int \<Rightarrow> real" ("\<module>real'_of'_int")
  1796 attach {*
  1797 fun real_of_int i = (i, 1);
  1798 *}
  1799 
  1800 setup {*
  1801   Nitpick.register_frac_type @{type_name real}
  1802    [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  1803     (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  1804     (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  1805     (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  1806     (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  1807     (@{const_name number_real_inst.number_of_real}, @{const_name Nitpick.number_of_frac}),
  1808     (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  1809     (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_eq_frac}),
  1810     (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  1811 *}
  1812 
  1813 lemmas [nitpick_def] = inverse_real_inst.inverse_real
  1814     number_real_inst.number_of_real one_real_inst.one_real
  1815     ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  1816     times_real_inst.times_real uminus_real_inst.uminus_real
  1817     zero_real_inst.zero_real
  1818 
  1819 end