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