src/HOL/RealDef.thy
author hoelzl
Thu Jan 31 11:31:27 2013 +0100 (2013-01-31)
changeset 50999 3de230ed0547
parent 49962 a8cc904a6820
child 51126 df86080de4cb
permissions -rw-r--r--
introduce order topology
     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 lemma vanishes_diff_inverse:
   305   assumes X: "cauchy X" "\<not> vanishes X"
   306   assumes Y: "cauchy Y" "\<not> vanishes Y"
   307   assumes XY: "vanishes (\<lambda>n. X n - Y n)"
   308   shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
   309 proof (rule vanishesI)
   310   fix r :: rat assume r: "0 < r"
   311   obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
   312     using cauchy_not_vanishes [OF X] by fast
   313   obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
   314     using cauchy_not_vanishes [OF Y] by fast
   315   obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
   316   proof
   317     show "0 < a * r * b"
   318       using a r b by (simp add: mult_pos_pos)
   319     show "inverse a * (a * r * b) * inverse b = r"
   320       using a r b by simp
   321   qed
   322   obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
   323     using vanishesD [OF XY s] ..
   324   have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
   325   proof (clarsimp)
   326     fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
   327     have "X n \<noteq> 0" and "Y n \<noteq> 0"
   328       using i j a b n by auto
   329     hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
   330         inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
   331       by (simp add: inverse_diff_inverse abs_mult)
   332     also have "\<dots> < inverse a * s * inverse b"
   333       apply (intro mult_strict_mono' less_imp_inverse_less)
   334       apply (simp_all add: a b i j k n mult_nonneg_nonneg)
   335       done
   336     also note `inverse a * s * inverse b = r`
   337     finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
   338   qed
   339   thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
   340 qed
   341 
   342 subsection {* Equivalence relation on Cauchy sequences *}
   343 
   344 definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
   345   where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
   346 
   347 lemma realrelI [intro?]:
   348   assumes "cauchy X" and "cauchy Y" and "vanishes (\<lambda>n. X n - Y n)"
   349   shows "realrel X Y"
   350   using assms unfolding realrel_def by simp
   351 
   352 lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
   353   unfolding realrel_def by simp
   354 
   355 lemma symp_realrel: "symp realrel"
   356   unfolding realrel_def
   357   by (rule sympI, clarify, drule vanishes_minus, simp)
   358 
   359 lemma transp_realrel: "transp realrel"
   360   unfolding realrel_def
   361   apply (rule transpI, clarify)
   362   apply (drule (1) vanishes_add)
   363   apply (simp add: algebra_simps)
   364   done
   365 
   366 lemma part_equivp_realrel: "part_equivp realrel"
   367   by (fast intro: part_equivpI symp_realrel transp_realrel
   368     realrel_refl cauchy_const)
   369 
   370 subsection {* The field of real numbers *}
   371 
   372 quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
   373   morphisms rep_real Real
   374   by (rule part_equivp_realrel)
   375 
   376 lemma cr_real_eq: "cr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
   377   unfolding cr_real_def realrel_def by simp
   378 
   379 lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
   380   assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)" shows "P x"
   381 proof (induct x)
   382   case (1 X)
   383   hence "cauchy X" by (simp add: realrel_def)
   384   thus "P (Real X)" by (rule assms)
   385 qed
   386 
   387 lemma eq_Real:
   388   "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
   389   using real.rel_eq_transfer
   390   unfolding cr_real_def fun_rel_def realrel_def by simp
   391 
   392 declare real.forall_transfer [transfer_rule del]
   393 
   394 lemma forall_real_transfer [transfer_rule]: (* TODO: generate automatically *)
   395   "(fun_rel (fun_rel cr_real op =) op =)
   396     (transfer_bforall cauchy) transfer_forall"
   397   using Quotient_forall_transfer [OF Quotient_real]
   398   by (simp add: realrel_def)
   399 
   400 instantiation real :: field_inverse_zero
   401 begin
   402 
   403 lift_definition zero_real :: "real" is "\<lambda>n. 0"
   404   by (simp add: realrel_refl)
   405 
   406 lift_definition one_real :: "real" is "\<lambda>n. 1"
   407   by (simp add: realrel_refl)
   408 
   409 lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
   410   unfolding realrel_def add_diff_add
   411   by (simp only: cauchy_add vanishes_add simp_thms)
   412 
   413 lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
   414   unfolding realrel_def minus_diff_minus
   415   by (simp only: cauchy_minus vanishes_minus simp_thms)
   416 
   417 lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
   418   unfolding realrel_def mult_diff_mult
   419   by (subst (4) mult_commute, simp only: cauchy_mult vanishes_add
   420     vanishes_mult_bounded cauchy_imp_bounded simp_thms)
   421 
   422 lift_definition inverse_real :: "real \<Rightarrow> real"
   423   is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
   424 proof -
   425   fix X Y assume "realrel X Y"
   426   hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
   427     unfolding realrel_def by simp_all
   428   have "vanishes X \<longleftrightarrow> vanishes Y"
   429   proof
   430     assume "vanishes X"
   431     from vanishes_diff [OF this XY] show "vanishes Y" by simp
   432   next
   433     assume "vanishes Y"
   434     from vanishes_add [OF this XY] show "vanishes X" by simp
   435   qed
   436   thus "?thesis X Y"
   437     unfolding realrel_def
   438     by (simp add: vanishes_diff_inverse X Y XY)
   439 qed
   440 
   441 definition
   442   "x - y = (x::real) + - y"
   443 
   444 definition
   445   "x / y = (x::real) * inverse y"
   446 
   447 lemma add_Real:
   448   assumes X: "cauchy X" and Y: "cauchy Y"
   449   shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
   450   using assms plus_real.transfer
   451   unfolding cr_real_eq fun_rel_def by simp
   452 
   453 lemma minus_Real:
   454   assumes X: "cauchy X"
   455   shows "- Real X = Real (\<lambda>n. - X n)"
   456   using assms uminus_real.transfer
   457   unfolding cr_real_eq fun_rel_def by simp
   458 
   459 lemma diff_Real:
   460   assumes X: "cauchy X" and Y: "cauchy Y"
   461   shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
   462   unfolding minus_real_def diff_minus
   463   by (simp add: minus_Real add_Real X Y)
   464 
   465 lemma mult_Real:
   466   assumes X: "cauchy X" and Y: "cauchy Y"
   467   shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
   468   using assms times_real.transfer
   469   unfolding cr_real_eq fun_rel_def by simp
   470 
   471 lemma inverse_Real:
   472   assumes X: "cauchy X"
   473   shows "inverse (Real X) =
   474     (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
   475   using assms inverse_real.transfer zero_real.transfer
   476   unfolding cr_real_eq fun_rel_def by (simp split: split_if_asm, metis)
   477 
   478 instance proof
   479   fix a b c :: real
   480   show "a + b = b + a"
   481     by transfer (simp add: add_ac realrel_def)
   482   show "(a + b) + c = a + (b + c)"
   483     by transfer (simp add: add_ac realrel_def)
   484   show "0 + a = a"
   485     by transfer (simp add: realrel_def)
   486   show "- a + a = 0"
   487     by transfer (simp add: realrel_def)
   488   show "a - b = a + - b"
   489     by (rule minus_real_def)
   490   show "(a * b) * c = a * (b * c)"
   491     by transfer (simp add: mult_ac realrel_def)
   492   show "a * b = b * a"
   493     by transfer (simp add: mult_ac realrel_def)
   494   show "1 * a = a"
   495     by transfer (simp add: mult_ac realrel_def)
   496   show "(a + b) * c = a * c + b * c"
   497     by transfer (simp add: distrib_right realrel_def)
   498   show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
   499     by transfer (simp add: realrel_def)
   500   show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   501     apply transfer
   502     apply (simp add: realrel_def)
   503     apply (rule vanishesI)
   504     apply (frule (1) cauchy_not_vanishes, clarify)
   505     apply (rule_tac x=k in exI, clarify)
   506     apply (drule_tac x=n in spec, simp)
   507     done
   508   show "a / b = a * inverse b"
   509     by (rule divide_real_def)
   510   show "inverse (0::real) = 0"
   511     by transfer (simp add: realrel_def)
   512 qed
   513 
   514 end
   515 
   516 subsection {* Positive reals *}
   517 
   518 lift_definition positive :: "real \<Rightarrow> bool"
   519   is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   520 proof -
   521   { fix X Y
   522     assume "realrel X Y"
   523     hence XY: "vanishes (\<lambda>n. X n - Y n)"
   524       unfolding realrel_def by simp_all
   525     assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
   526     then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
   527       by fast
   528     obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   529       using `0 < r` by (rule obtain_pos_sum)
   530     obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
   531       using vanishesD [OF XY s] ..
   532     have "\<forall>n\<ge>max i j. t < Y n"
   533     proof (clarsimp)
   534       fix n assume n: "i \<le> n" "j \<le> n"
   535       have "\<bar>X n - Y n\<bar> < s" and "r < X n"
   536         using i j n by simp_all
   537       thus "t < Y n" unfolding r by simp
   538     qed
   539     hence "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
   540   } note 1 = this
   541   fix X Y assume "realrel X Y"
   542   hence "realrel X Y" and "realrel Y X"
   543     using symp_realrel unfolding symp_def by auto
   544   thus "?thesis X Y"
   545     by (safe elim!: 1)
   546 qed
   547 
   548 lemma positive_Real:
   549   assumes X: "cauchy X"
   550   shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
   551   using assms positive.transfer
   552   unfolding cr_real_eq fun_rel_def by simp
   553 
   554 lemma positive_zero: "\<not> positive 0"
   555   by transfer auto
   556 
   557 lemma positive_add:
   558   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
   559 apply transfer
   560 apply (clarify, rename_tac a b i j)
   561 apply (rule_tac x="a + b" in exI, simp)
   562 apply (rule_tac x="max i j" in exI, clarsimp)
   563 apply (simp add: add_strict_mono)
   564 done
   565 
   566 lemma positive_mult:
   567   "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
   568 apply transfer
   569 apply (clarify, rename_tac a b i j)
   570 apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
   571 apply (rule_tac x="max i j" in exI, clarsimp)
   572 apply (rule mult_strict_mono, auto)
   573 done
   574 
   575 lemma positive_minus:
   576   "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
   577 apply transfer
   578 apply (simp add: realrel_def)
   579 apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
   580 done
   581 
   582 instantiation real :: linordered_field_inverse_zero
   583 begin
   584 
   585 definition
   586   "x < y \<longleftrightarrow> positive (y - x)"
   587 
   588 definition
   589   "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
   590 
   591 definition
   592   "abs (a::real) = (if a < 0 then - a else a)"
   593 
   594 definition
   595   "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   596 
   597 instance proof
   598   fix a b c :: real
   599   show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   600     by (rule abs_real_def)
   601   show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   602     unfolding less_eq_real_def less_real_def
   603     by (auto, drule (1) positive_add, simp_all add: positive_zero)
   604   show "a \<le> a"
   605     unfolding less_eq_real_def by simp
   606   show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   607     unfolding less_eq_real_def less_real_def
   608     by (auto, drule (1) positive_add, simp add: algebra_simps)
   609   show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   610     unfolding less_eq_real_def less_real_def
   611     by (auto, drule (1) positive_add, simp add: positive_zero)
   612   show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   613     unfolding less_eq_real_def less_real_def by (auto simp: diff_minus) (* by auto *)
   614     (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
   615     (* Should produce c + b - (c + a) \<equiv> b - a *)
   616   show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   617     by (rule sgn_real_def)
   618   show "a \<le> b \<or> b \<le> a"
   619     unfolding less_eq_real_def less_real_def
   620     by (auto dest!: positive_minus)
   621   show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   622     unfolding less_real_def
   623     by (drule (1) positive_mult, simp add: algebra_simps)
   624 qed
   625 
   626 end
   627 
   628 instantiation real :: distrib_lattice
   629 begin
   630 
   631 definition
   632   "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
   633 
   634 definition
   635   "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
   636 
   637 instance proof
   638 qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
   639 
   640 end
   641 
   642 lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   643 apply (induct x)
   644 apply (simp add: zero_real_def)
   645 apply (simp add: one_real_def add_Real)
   646 done
   647 
   648 lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
   649 apply (cases x rule: int_diff_cases)
   650 apply (simp add: of_nat_Real diff_Real)
   651 done
   652 
   653 lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
   654 apply (induct x)
   655 apply (simp add: Fract_of_int_quotient of_rat_divide)
   656 apply (simp add: of_int_Real divide_inverse)
   657 apply (simp add: inverse_Real mult_Real)
   658 done
   659 
   660 instance real :: archimedean_field
   661 proof
   662   fix x :: real
   663   show "\<exists>z. x \<le> of_int z"
   664     apply (induct x)
   665     apply (frule cauchy_imp_bounded, clarify)
   666     apply (rule_tac x="ceiling b + 1" in exI)
   667     apply (rule less_imp_le)
   668     apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
   669     apply (rule_tac x=1 in exI, simp add: algebra_simps)
   670     apply (rule_tac x=0 in exI, clarsimp)
   671     apply (rule le_less_trans [OF abs_ge_self])
   672     apply (rule less_le_trans [OF _ le_of_int_ceiling])
   673     apply simp
   674     done
   675 qed
   676 
   677 instantiation real :: floor_ceiling
   678 begin
   679 
   680 definition [code del]:
   681   "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
   682 
   683 instance proof
   684   fix x :: real
   685   show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   686     unfolding floor_real_def using floor_exists1 by (rule theI')
   687 qed
   688 
   689 end
   690 
   691 subsection {* Completeness *}
   692 
   693 lemma not_positive_Real:
   694   assumes X: "cauchy X"
   695   shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
   696 unfolding positive_Real [OF X]
   697 apply (auto, unfold not_less)
   698 apply (erule obtain_pos_sum)
   699 apply (drule_tac x=s in spec, simp)
   700 apply (drule_tac r=t in cauchyD [OF X], clarify)
   701 apply (drule_tac x=k in spec, clarsimp)
   702 apply (rule_tac x=n in exI, clarify, rename_tac m)
   703 apply (drule_tac x=m in spec, simp)
   704 apply (drule_tac x=n in spec, simp)
   705 apply (drule spec, drule (1) mp, clarify, rename_tac i)
   706 apply (rule_tac x="max i k" in exI, simp)
   707 done
   708 
   709 lemma le_Real:
   710   assumes X: "cauchy X" and Y: "cauchy Y"
   711   shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
   712 unfolding not_less [symmetric, where 'a=real] less_real_def
   713 apply (simp add: diff_Real not_positive_Real X Y)
   714 apply (simp add: diff_le_eq add_ac)
   715 done
   716 
   717 lemma le_RealI:
   718   assumes Y: "cauchy Y"
   719   shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
   720 proof (induct x)
   721   fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
   722   hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
   723     by (simp add: of_rat_Real le_Real)
   724   {
   725     fix r :: rat assume "0 < r"
   726     then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
   727       by (rule obtain_pos_sum)
   728     obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
   729       using cauchyD [OF Y s] ..
   730     obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
   731       using le [OF t] ..
   732     have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
   733     proof (clarsimp)
   734       fix n assume n: "i \<le> n" "j \<le> n"
   735       have "X n \<le> Y i + t" using n j by simp
   736       moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
   737       ultimately show "X n \<le> Y n + r" unfolding r by simp
   738     qed
   739     hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
   740   }
   741   thus "Real X \<le> Real Y"
   742     by (simp add: of_rat_Real le_Real X Y)
   743 qed
   744 
   745 lemma Real_leI:
   746   assumes X: "cauchy X"
   747   assumes le: "\<forall>n. of_rat (X n) \<le> y"
   748   shows "Real X \<le> y"
   749 proof -
   750   have "- y \<le> - Real X"
   751     by (simp add: minus_Real X le_RealI of_rat_minus le)
   752   thus ?thesis by simp
   753 qed
   754 
   755 lemma less_RealD:
   756   assumes Y: "cauchy Y"
   757   shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
   758 by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   759 
   760 lemma of_nat_less_two_power:
   761   "of_nat n < (2::'a::linordered_idom) ^ n"
   762 apply (induct n)
   763 apply simp
   764 apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   765 apply (drule (1) add_le_less_mono, simp)
   766 apply simp
   767 done
   768 
   769 lemma complete_real:
   770   fixes S :: "real set"
   771   assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
   772   shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   773 proof -
   774   obtain x where x: "x \<in> S" using assms(1) ..
   775   obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
   776 
   777   def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
   778   obtain a where a: "\<not> P a"
   779   proof
   780     have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
   781     also have "x - 1 < x" by simp
   782     finally have "of_int (floor (x - 1)) < x" .
   783     hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
   784     then show "\<not> P (of_int (floor (x - 1)))"
   785       unfolding P_def of_rat_of_int_eq using x by fast
   786   qed
   787   obtain b where b: "P b"
   788   proof
   789     show "P (of_int (ceiling z))"
   790     unfolding P_def of_rat_of_int_eq
   791     proof
   792       fix y assume "y \<in> S"
   793       hence "y \<le> z" using z by simp
   794       also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
   795       finally show "y \<le> of_int (ceiling z)" .
   796     qed
   797   qed
   798 
   799   def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
   800   def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
   801   def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
   802   def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
   803   def C \<equiv> "\<lambda>n. avg (A n) (B n)"
   804   have A_0 [simp]: "A 0 = a" unfolding A_def by simp
   805   have B_0 [simp]: "B 0 = b" unfolding B_def by simp
   806   have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
   807     unfolding A_def B_def C_def bisect_def split_def by simp
   808   have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
   809     unfolding A_def B_def C_def bisect_def split_def by simp
   810 
   811   have width: "\<And>n. B n - A n = (b - a) / 2^n"
   812     apply (simp add: eq_divide_eq)
   813     apply (induct_tac n, simp)
   814     apply (simp add: C_def avg_def algebra_simps)
   815     done
   816 
   817   have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
   818     apply (simp add: divide_less_eq)
   819     apply (subst mult_commute)
   820     apply (frule_tac y=y in ex_less_of_nat_mult)
   821     apply clarify
   822     apply (rule_tac x=n in exI)
   823     apply (erule less_trans)
   824     apply (rule mult_strict_right_mono)
   825     apply (rule le_less_trans [OF _ of_nat_less_two_power])
   826     apply simp
   827     apply assumption
   828     done
   829 
   830   have PA: "\<And>n. \<not> P (A n)"
   831     by (induct_tac n, simp_all add: a)
   832   have PB: "\<And>n. P (B n)"
   833     by (induct_tac n, simp_all add: b)
   834   have ab: "a < b"
   835     using a b unfolding P_def
   836     apply (clarsimp simp add: not_le)
   837     apply (drule (1) bspec)
   838     apply (drule (1) less_le_trans)
   839     apply (simp add: of_rat_less)
   840     done
   841   have AB: "\<And>n. A n < B n"
   842     by (induct_tac n, simp add: ab, simp add: C_def avg_def)
   843   have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
   844     apply (auto simp add: le_less [where 'a=nat])
   845     apply (erule less_Suc_induct)
   846     apply (clarsimp simp add: C_def avg_def)
   847     apply (simp add: add_divide_distrib [symmetric])
   848     apply (rule AB [THEN less_imp_le])
   849     apply simp
   850     done
   851   have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
   852     apply (auto simp add: le_less [where 'a=nat])
   853     apply (erule less_Suc_induct)
   854     apply (clarsimp simp add: C_def avg_def)
   855     apply (simp add: add_divide_distrib [symmetric])
   856     apply (rule AB [THEN less_imp_le])
   857     apply simp
   858     done
   859   have cauchy_lemma:
   860     "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
   861     apply (rule cauchyI)
   862     apply (drule twos [where y="b - a"])
   863     apply (erule exE)
   864     apply (rule_tac x=n in exI, clarify, rename_tac i j)
   865     apply (rule_tac y="B n - A n" in le_less_trans) defer
   866     apply (simp add: width)
   867     apply (drule_tac x=n in spec)
   868     apply (frule_tac x=i in spec, drule (1) mp)
   869     apply (frule_tac x=j in spec, drule (1) mp)
   870     apply (frule A_mono, drule B_mono)
   871     apply (frule A_mono, drule B_mono)
   872     apply arith
   873     done
   874   have "cauchy A"
   875     apply (rule cauchy_lemma [rule_format])
   876     apply (simp add: A_mono)
   877     apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
   878     done
   879   have "cauchy B"
   880     apply (rule cauchy_lemma [rule_format])
   881     apply (simp add: B_mono)
   882     apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
   883     done
   884   have 1: "\<forall>x\<in>S. x \<le> Real B"
   885   proof
   886     fix x assume "x \<in> S"
   887     then show "x \<le> Real B"
   888       using PB [unfolded P_def] `cauchy B`
   889       by (simp add: le_RealI)
   890   qed
   891   have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
   892     apply clarify
   893     apply (erule contrapos_pp)
   894     apply (simp add: not_le)
   895     apply (drule less_RealD [OF `cauchy A`], clarify)
   896     apply (subgoal_tac "\<not> P (A n)")
   897     apply (simp add: P_def not_le, clarify)
   898     apply (erule rev_bexI)
   899     apply (erule (1) less_trans)
   900     apply (simp add: PA)
   901     done
   902   have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
   903   proof (rule vanishesI)
   904     fix r :: rat assume "0 < r"
   905     then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
   906       using twos by fast
   907     have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
   908     proof (clarify)
   909       fix n assume n: "k \<le> n"
   910       have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
   911         by simp
   912       also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
   913         using n by (simp add: divide_left_mono mult_pos_pos)
   914       also note k
   915       finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
   916     qed
   917     thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
   918   qed
   919   hence 3: "Real B = Real A"
   920     by (simp add: eq_Real `cauchy A` `cauchy B` width)
   921   show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
   922     using 1 2 3 by (rule_tac x="Real B" in exI, simp)
   923 qed
   924 
   925 subsection {* Hiding implementation details *}
   926 
   927 hide_const (open) vanishes cauchy positive Real
   928 
   929 declare Real_induct [induct del]
   930 declare Abs_real_induct [induct del]
   931 declare Abs_real_cases [cases del]
   932 
   933 lemmas [transfer_rule del] =
   934   real.All_transfer real.Ex_transfer real.rel_eq_transfer forall_real_transfer
   935   zero_real.transfer one_real.transfer plus_real.transfer uminus_real.transfer
   936   times_real.transfer inverse_real.transfer positive.transfer
   937 
   938 subsection{*More Lemmas*}
   939 
   940 text {* BH: These lemmas should not be necessary; they should be
   941 covered by existing simp rules and simplification procedures. *}
   942 
   943 lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
   944 by simp (* redundant with mult_cancel_left *)
   945 
   946 lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
   947 by simp (* redundant with mult_cancel_right *)
   948 
   949 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
   950 by simp (* solved by linordered_ring_less_cancel_factor simproc *)
   951 
   952 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
   953 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
   954 
   955 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
   956 by simp (* solved by linordered_ring_le_cancel_factor simproc *)
   957 
   958 
   959 subsection {* Embedding numbers into the Reals *}
   960 
   961 abbreviation
   962   real_of_nat :: "nat \<Rightarrow> real"
   963 where
   964   "real_of_nat \<equiv> of_nat"
   965 
   966 abbreviation
   967   real_of_int :: "int \<Rightarrow> real"
   968 where
   969   "real_of_int \<equiv> of_int"
   970 
   971 abbreviation
   972   real_of_rat :: "rat \<Rightarrow> real"
   973 where
   974   "real_of_rat \<equiv> of_rat"
   975 
   976 consts
   977   (*overloaded constant for injecting other types into "real"*)
   978   real :: "'a => real"
   979 
   980 defs (overloaded)
   981   real_of_nat_def [code_unfold]: "real == real_of_nat"
   982   real_of_int_def [code_unfold]: "real == real_of_int"
   983 
   984 declare [[coercion_enabled]]
   985 declare [[coercion "real::nat\<Rightarrow>real"]]
   986 declare [[coercion "real::int\<Rightarrow>real"]]
   987 declare [[coercion "int"]]
   988 
   989 declare [[coercion_map map]]
   990 declare [[coercion_map "% f g h x. g (h (f x))"]]
   991 declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
   992 
   993 lemma real_eq_of_nat: "real = of_nat"
   994   unfolding real_of_nat_def ..
   995 
   996 lemma real_eq_of_int: "real = of_int"
   997   unfolding real_of_int_def ..
   998 
   999 lemma real_of_int_zero [simp]: "real (0::int) = 0"  
  1000 by (simp add: real_of_int_def) 
  1001 
  1002 lemma real_of_one [simp]: "real (1::int) = (1::real)"
  1003 by (simp add: real_of_int_def) 
  1004 
  1005 lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
  1006 by (simp add: real_of_int_def) 
  1007 
  1008 lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
  1009 by (simp add: real_of_int_def) 
  1010 
  1011 lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
  1012 by (simp add: real_of_int_def) 
  1013 
  1014 lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
  1015 by (simp add: real_of_int_def) 
  1016 
  1017 lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
  1018 by (simp add: real_of_int_def of_int_power)
  1019 
  1020 lemmas power_real_of_int = real_of_int_power [symmetric]
  1021 
  1022 lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
  1023   apply (subst real_eq_of_int)+
  1024   apply (rule of_int_setsum)
  1025 done
  1026 
  1027 lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
  1028     (PROD x:A. real(f x))"
  1029   apply (subst real_eq_of_int)+
  1030   apply (rule of_int_setprod)
  1031 done
  1032 
  1033 lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
  1034 by (simp add: real_of_int_def) 
  1035 
  1036 lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
  1037 by (simp add: real_of_int_def) 
  1038 
  1039 lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
  1040 by (simp add: real_of_int_def) 
  1041 
  1042 lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
  1043 by (simp add: real_of_int_def) 
  1044 
  1045 lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
  1046 by (simp add: real_of_int_def) 
  1047 
  1048 lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
  1049 by (simp add: real_of_int_def) 
  1050 
  1051 lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
  1052 by (simp add: real_of_int_def)
  1053 
  1054 lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
  1055 by (simp add: real_of_int_def)
  1056 
  1057 lemma one_less_real_of_int_cancel_iff: "1 < real (i :: int) \<longleftrightarrow> 1 < i"
  1058   unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1059 
  1060 lemma one_le_real_of_int_cancel_iff: "1 \<le> real (i :: int) \<longleftrightarrow> 1 \<le> i"
  1061   unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1062 
  1063 lemma real_of_int_less_one_cancel_iff: "real (i :: int) < 1 \<longleftrightarrow> i < 1"
  1064   unfolding real_of_one[symmetric] real_of_int_less_iff ..
  1065 
  1066 lemma real_of_int_le_one_cancel_iff: "real (i :: int) \<le> 1 \<longleftrightarrow> i \<le> 1"
  1067   unfolding real_of_one[symmetric] real_of_int_le_iff ..
  1068 
  1069 lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
  1070 by (auto simp add: abs_if)
  1071 
  1072 lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
  1073   apply (subgoal_tac "real n + 1 = real (n + 1)")
  1074   apply (simp del: real_of_int_add)
  1075   apply auto
  1076 done
  1077 
  1078 lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
  1079   apply (subgoal_tac "real m + 1 = real (m + 1)")
  1080   apply (simp del: real_of_int_add)
  1081   apply simp
  1082 done
  1083 
  1084 lemma real_of_int_div_aux: "(real (x::int)) / (real d) = 
  1085     real (x div d) + (real (x mod d)) / (real d)"
  1086 proof -
  1087   have "x = (x div d) * d + x mod d"
  1088     by auto
  1089   then have "real x = real (x div d) * real d + real(x mod d)"
  1090     by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
  1091   then have "real x / real d = ... / real d"
  1092     by simp
  1093   then show ?thesis
  1094     by (auto simp add: add_divide_distrib algebra_simps)
  1095 qed
  1096 
  1097 lemma real_of_int_div: "(d :: int) dvd n ==>
  1098     real(n div d) = real n / real d"
  1099   apply (subst real_of_int_div_aux)
  1100   apply simp
  1101   apply (simp add: dvd_eq_mod_eq_0)
  1102 done
  1103 
  1104 lemma real_of_int_div2:
  1105   "0 <= real (n::int) / real (x) - real (n div x)"
  1106   apply (case_tac "x = 0")
  1107   apply simp
  1108   apply (case_tac "0 < x")
  1109   apply (simp add: algebra_simps)
  1110   apply (subst real_of_int_div_aux)
  1111   apply simp
  1112   apply (subst zero_le_divide_iff)
  1113   apply auto
  1114   apply (simp add: algebra_simps)
  1115   apply (subst real_of_int_div_aux)
  1116   apply simp
  1117   apply (subst zero_le_divide_iff)
  1118   apply auto
  1119 done
  1120 
  1121 lemma real_of_int_div3:
  1122   "real (n::int) / real (x) - real (n div x) <= 1"
  1123   apply (simp add: algebra_simps)
  1124   apply (subst real_of_int_div_aux)
  1125   apply (auto simp add: divide_le_eq intro: order_less_imp_le)
  1126 done
  1127 
  1128 lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
  1129 by (insert real_of_int_div2 [of n x], simp)
  1130 
  1131 lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
  1132 unfolding real_of_int_def by (rule Ints_of_int)
  1133 
  1134 
  1135 subsection{*Embedding the Naturals into the Reals*}
  1136 
  1137 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
  1138 by (simp add: real_of_nat_def)
  1139 
  1140 lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
  1141 by (simp add: real_of_nat_def)
  1142 
  1143 lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
  1144 by (simp add: real_of_nat_def)
  1145 
  1146 lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
  1147 by (simp add: real_of_nat_def)
  1148 
  1149 (*Not for addsimps: often the LHS is used to represent a positive natural*)
  1150 lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
  1151 by (simp add: real_of_nat_def)
  1152 
  1153 lemma real_of_nat_less_iff [iff]: 
  1154      "(real (n::nat) < real m) = (n < m)"
  1155 by (simp add: real_of_nat_def)
  1156 
  1157 lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
  1158 by (simp add: real_of_nat_def)
  1159 
  1160 lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
  1161 by (simp add: real_of_nat_def)
  1162 
  1163 lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
  1164 by (simp add: real_of_nat_def del: of_nat_Suc)
  1165 
  1166 lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
  1167 by (simp add: real_of_nat_def of_nat_mult)
  1168 
  1169 lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
  1170 by (simp add: real_of_nat_def of_nat_power)
  1171 
  1172 lemmas power_real_of_nat = real_of_nat_power [symmetric]
  1173 
  1174 lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
  1175     (SUM x:A. real(f x))"
  1176   apply (subst real_eq_of_nat)+
  1177   apply (rule of_nat_setsum)
  1178 done
  1179 
  1180 lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
  1181     (PROD x:A. real(f x))"
  1182   apply (subst real_eq_of_nat)+
  1183   apply (rule of_nat_setprod)
  1184 done
  1185 
  1186 lemma real_of_card: "real (card A) = setsum (%x.1) A"
  1187   apply (subst card_eq_setsum)
  1188   apply (subst real_of_nat_setsum)
  1189   apply simp
  1190 done
  1191 
  1192 lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
  1193 by (simp add: real_of_nat_def)
  1194 
  1195 lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
  1196 by (simp add: real_of_nat_def)
  1197 
  1198 lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
  1199 by (simp add: add: real_of_nat_def of_nat_diff)
  1200 
  1201 lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
  1202 by (auto simp: real_of_nat_def)
  1203 
  1204 lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
  1205 by (simp add: add: real_of_nat_def)
  1206 
  1207 lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
  1208 by (simp add: add: real_of_nat_def)
  1209 
  1210 lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
  1211   apply (subgoal_tac "real n + 1 = real (Suc n)")
  1212   apply simp
  1213   apply (auto simp add: real_of_nat_Suc)
  1214 done
  1215 
  1216 lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
  1217   apply (subgoal_tac "real m + 1 = real (Suc m)")
  1218   apply (simp add: less_Suc_eq_le)
  1219   apply (simp add: real_of_nat_Suc)
  1220 done
  1221 
  1222 lemma real_of_nat_div_aux: "(real (x::nat)) / (real d) = 
  1223     real (x div d) + (real (x mod d)) / (real d)"
  1224 proof -
  1225   have "x = (x div d) * d + x mod d"
  1226     by auto
  1227   then have "real x = real (x div d) * real d + real(x mod d)"
  1228     by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
  1229   then have "real x / real d = \<dots> / real d"
  1230     by simp
  1231   then show ?thesis
  1232     by (auto simp add: add_divide_distrib algebra_simps)
  1233 qed
  1234 
  1235 lemma real_of_nat_div: "(d :: nat) dvd n ==>
  1236     real(n div d) = real n / real d"
  1237   by (subst real_of_nat_div_aux)
  1238     (auto simp add: dvd_eq_mod_eq_0 [symmetric])
  1239 
  1240 lemma real_of_nat_div2:
  1241   "0 <= real (n::nat) / real (x) - real (n div x)"
  1242 apply (simp add: algebra_simps)
  1243 apply (subst real_of_nat_div_aux)
  1244 apply simp
  1245 apply (subst zero_le_divide_iff)
  1246 apply simp
  1247 done
  1248 
  1249 lemma real_of_nat_div3:
  1250   "real (n::nat) / real (x) - real (n div x) <= 1"
  1251 apply(case_tac "x = 0")
  1252 apply (simp)
  1253 apply (simp add: algebra_simps)
  1254 apply (subst real_of_nat_div_aux)
  1255 apply simp
  1256 done
  1257 
  1258 lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
  1259 by (insert real_of_nat_div2 [of n x], simp)
  1260 
  1261 lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
  1262 by (simp add: real_of_int_def real_of_nat_def)
  1263 
  1264 lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
  1265   apply (subgoal_tac "real(int(nat x)) = real(nat x)")
  1266   apply force
  1267   apply (simp only: real_of_int_of_nat_eq)
  1268 done
  1269 
  1270 lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
  1271 unfolding real_of_nat_def by (rule of_nat_in_Nats)
  1272 
  1273 lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
  1274 unfolding real_of_nat_def by (rule Ints_of_nat)
  1275 
  1276 
  1277 subsection{* Rationals *}
  1278 
  1279 lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
  1280 by (simp add: real_eq_of_nat)
  1281 
  1282 
  1283 lemma Rats_eq_int_div_int:
  1284   "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
  1285 proof
  1286   show "\<rat> \<subseteq> ?S"
  1287   proof
  1288     fix x::real assume "x : \<rat>"
  1289     then obtain r where "x = of_rat r" unfolding Rats_def ..
  1290     have "of_rat r : ?S"
  1291       by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
  1292     thus "x : ?S" using `x = of_rat r` by simp
  1293   qed
  1294 next
  1295   show "?S \<subseteq> \<rat>"
  1296   proof(auto simp:Rats_def)
  1297     fix i j :: int assume "j \<noteq> 0"
  1298     hence "real i / real j = of_rat(Fract i j)"
  1299       by (simp add:of_rat_rat real_eq_of_int)
  1300     thus "real i / real j \<in> range of_rat" by blast
  1301   qed
  1302 qed
  1303 
  1304 lemma Rats_eq_int_div_nat:
  1305   "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
  1306 proof(auto simp:Rats_eq_int_div_int)
  1307   fix i j::int assume "j \<noteq> 0"
  1308   show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
  1309   proof cases
  1310     assume "j>0"
  1311     hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
  1312       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1313     thus ?thesis by blast
  1314   next
  1315     assume "~ j>0"
  1316     hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
  1317       by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
  1318     thus ?thesis by blast
  1319   qed
  1320 next
  1321   fix i::int and n::nat assume "0 < n"
  1322   hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
  1323   thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
  1324 qed
  1325 
  1326 lemma Rats_abs_nat_div_natE:
  1327   assumes "x \<in> \<rat>"
  1328   obtains m n :: nat
  1329   where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
  1330 proof -
  1331   from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
  1332     by(auto simp add: Rats_eq_int_div_nat)
  1333   hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
  1334   then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
  1335   let ?gcd = "gcd m n"
  1336   from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
  1337   let ?k = "m div ?gcd"
  1338   let ?l = "n div ?gcd"
  1339   let ?gcd' = "gcd ?k ?l"
  1340   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
  1341     by (rule dvd_mult_div_cancel)
  1342   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
  1343     by (rule dvd_mult_div_cancel)
  1344   from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
  1345   moreover
  1346   have "\<bar>x\<bar> = real ?k / real ?l"
  1347   proof -
  1348     from gcd have "real ?k / real ?l =
  1349         real (?gcd * ?k) / real (?gcd * ?l)" by simp
  1350     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
  1351     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
  1352     finally show ?thesis ..
  1353   qed
  1354   moreover
  1355   have "?gcd' = 1"
  1356   proof -
  1357     have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
  1358       by (rule gcd_mult_distrib_nat)
  1359     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
  1360     with gcd show ?thesis by auto
  1361   qed
  1362   ultimately show ?thesis ..
  1363 qed
  1364 
  1365 
  1366 subsection{*Numerals and Arithmetic*}
  1367 
  1368 lemma [code_abbrev]:
  1369   "real_of_int (numeral k) = numeral k"
  1370   "real_of_int (neg_numeral k) = neg_numeral k"
  1371   by simp_all
  1372 
  1373 text{*Collapse applications of @{term real} to @{term number_of}*}
  1374 lemma real_numeral [simp]:
  1375   "real (numeral v :: int) = numeral v"
  1376   "real (neg_numeral v :: int) = neg_numeral v"
  1377 by (simp_all add: real_of_int_def)
  1378 
  1379 lemma real_of_nat_numeral [simp]:
  1380   "real (numeral v :: nat) = numeral v"
  1381 by (simp add: real_of_nat_def)
  1382 
  1383 declaration {*
  1384   K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
  1385     (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
  1386   #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
  1387     (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
  1388   #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
  1389       @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
  1390       @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
  1391       @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
  1392       @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
  1393   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
  1394   #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
  1395 *}
  1396 
  1397 
  1398 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
  1399 
  1400 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
  1401 by arith
  1402 
  1403 text {* FIXME: redundant with @{text add_eq_0_iff} below *}
  1404 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
  1405 by auto
  1406 
  1407 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
  1408 by auto
  1409 
  1410 lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
  1411 by auto
  1412 
  1413 lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
  1414 by auto
  1415 
  1416 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
  1417 by auto
  1418 
  1419 subsection {* Lemmas about powers *}
  1420 
  1421 text {* FIXME: declare this in Rings.thy or not at all *}
  1422 declare abs_mult_self [simp]
  1423 
  1424 (* used by Import/HOL/real.imp *)
  1425 lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
  1426 by simp
  1427 
  1428 lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
  1429 apply (induct "n")
  1430 apply (auto simp add: real_of_nat_Suc)
  1431 apply (subst mult_2)
  1432 apply (erule add_less_le_mono)
  1433 apply (rule two_realpow_ge_one)
  1434 done
  1435 
  1436 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1437 lemma realpow_Suc_le_self:
  1438   fixes r :: "'a::linordered_semidom"
  1439   shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
  1440 by (insert power_decreasing [of 1 "Suc n" r], simp)
  1441 
  1442 text {* TODO: no longer real-specific; rename and move elsewhere *}
  1443 lemma realpow_minus_mult:
  1444   fixes x :: "'a::monoid_mult"
  1445   shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
  1446 by (simp add: power_commutes split add: nat_diff_split)
  1447 
  1448 text {* FIXME: declare this [simp] for all types, or not at all *}
  1449 lemma real_two_squares_add_zero_iff [simp]:
  1450   "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
  1451 by (rule sum_squares_eq_zero_iff)
  1452 
  1453 text {* FIXME: declare this [simp] for all types, or not at all *}
  1454 lemma realpow_two_sum_zero_iff [simp]:
  1455      "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
  1456 by (rule sum_power2_eq_zero_iff)
  1457 
  1458 lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
  1459 by (rule_tac y = 0 in order_trans, auto)
  1460 
  1461 lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
  1462 by (auto simp add: power2_eq_square)
  1463 
  1464 
  1465 lemma numeral_power_le_real_of_nat_cancel_iff[simp]:
  1466   "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::nat) ^ n \<le> a"
  1467   unfolding real_of_nat_le_iff[symmetric] by simp
  1468 
  1469 lemma real_of_nat_le_numeral_power_cancel_iff[simp]:
  1470   "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::nat) ^ n"
  1471   unfolding real_of_nat_le_iff[symmetric] by simp
  1472 
  1473 lemma numeral_power_le_real_of_int_cancel_iff[simp]:
  1474   "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::int) ^ n \<le> a"
  1475   unfolding real_of_int_le_iff[symmetric] by simp
  1476 
  1477 lemma real_of_int_le_numeral_power_cancel_iff[simp]:
  1478   "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::int) ^ n"
  1479   unfolding real_of_int_le_iff[symmetric] by simp
  1480 
  1481 lemma neg_numeral_power_le_real_of_int_cancel_iff[simp]:
  1482   "(neg_numeral x::real) ^ n \<le> real a \<longleftrightarrow> (neg_numeral x::int) ^ n \<le> a"
  1483   unfolding real_of_int_le_iff[symmetric] by simp
  1484 
  1485 lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]:
  1486   "real a \<le> (neg_numeral x::real) ^ n \<longleftrightarrow> a \<le> (neg_numeral x::int) ^ n"
  1487   unfolding real_of_int_le_iff[symmetric] by simp
  1488 
  1489 subsection{*Density of the Reals*}
  1490 
  1491 lemma real_lbound_gt_zero:
  1492      "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
  1493 apply (rule_tac x = " (min d1 d2) /2" in exI)
  1494 apply (simp add: min_def)
  1495 done
  1496 
  1497 
  1498 text{*Similar results are proved in @{text Fields}*}
  1499 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
  1500   by auto
  1501 
  1502 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1503   by auto
  1504 
  1505 
  1506 subsection{*Absolute Value Function for the Reals*}
  1507 
  1508 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
  1509 by (simp add: abs_if)
  1510 
  1511 (* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
  1512 lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
  1513 by (force simp add: abs_le_iff)
  1514 
  1515 lemma abs_add_one_gt_zero: "(0::real) < 1 + abs(x)"
  1516 by (simp add: abs_if)
  1517 
  1518 lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
  1519 by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
  1520 
  1521 lemma abs_add_one_not_less_self: "~ abs(x) + (1::real) < x"
  1522 by simp
  1523  
  1524 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1525 by simp
  1526 
  1527 
  1528 subsection {* Implementation of rational real numbers *}
  1529 
  1530 text {* Formal constructor *}
  1531 
  1532 definition Ratreal :: "rat \<Rightarrow> real" where
  1533   [code_abbrev, simp]: "Ratreal = of_rat"
  1534 
  1535 code_datatype Ratreal
  1536 
  1537 
  1538 text {* Numerals *}
  1539 
  1540 lemma [code_abbrev]:
  1541   "(of_rat (of_int a) :: real) = of_int a"
  1542   by simp
  1543 
  1544 lemma [code_abbrev]:
  1545   "(of_rat 0 :: real) = 0"
  1546   by simp
  1547 
  1548 lemma [code_abbrev]:
  1549   "(of_rat 1 :: real) = 1"
  1550   by simp
  1551 
  1552 lemma [code_abbrev]:
  1553   "(of_rat (numeral k) :: real) = numeral k"
  1554   by simp
  1555 
  1556 lemma [code_abbrev]:
  1557   "(of_rat (neg_numeral k) :: real) = neg_numeral k"
  1558   by simp
  1559 
  1560 lemma [code_post]:
  1561   "(of_rat (0 / r)  :: real) = 0"
  1562   "(of_rat (r / 0)  :: real) = 0"
  1563   "(of_rat (1 / 1)  :: real) = 1"
  1564   "(of_rat (numeral k / 1) :: real) = numeral k"
  1565   "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
  1566   "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
  1567   "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
  1568   "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
  1569   "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
  1570   "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
  1571   "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
  1572   by (simp_all add: of_rat_divide)
  1573 
  1574 
  1575 text {* Operations *}
  1576 
  1577 lemma zero_real_code [code]:
  1578   "0 = Ratreal 0"
  1579 by simp
  1580 
  1581 lemma one_real_code [code]:
  1582   "1 = Ratreal 1"
  1583 by simp
  1584 
  1585 instantiation real :: equal
  1586 begin
  1587 
  1588 definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
  1589 
  1590 instance proof
  1591 qed (simp add: equal_real_def)
  1592 
  1593 lemma real_equal_code [code]:
  1594   "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
  1595   by (simp add: equal_real_def equal)
  1596 
  1597 lemma [code nbe]:
  1598   "HOL.equal (x::real) x \<longleftrightarrow> True"
  1599   by (rule equal_refl)
  1600 
  1601 end
  1602 
  1603 lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
  1604   by (simp add: of_rat_less_eq)
  1605 
  1606 lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
  1607   by (simp add: of_rat_less)
  1608 
  1609 lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
  1610   by (simp add: of_rat_add)
  1611 
  1612 lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
  1613   by (simp add: of_rat_mult)
  1614 
  1615 lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
  1616   by (simp add: of_rat_minus)
  1617 
  1618 lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
  1619   by (simp add: of_rat_diff)
  1620 
  1621 lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
  1622   by (simp add: of_rat_inverse)
  1623  
  1624 lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
  1625   by (simp add: of_rat_divide)
  1626 
  1627 lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  1628   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)
  1629 
  1630 
  1631 text {* Quickcheck *}
  1632 
  1633 definition (in term_syntax)
  1634   valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  1635   [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  1636 
  1637 notation fcomp (infixl "\<circ>>" 60)
  1638 notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1639 
  1640 instantiation real :: random
  1641 begin
  1642 
  1643 definition
  1644   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
  1645 
  1646 instance ..
  1647 
  1648 end
  1649 
  1650 no_notation fcomp (infixl "\<circ>>" 60)
  1651 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
  1652 
  1653 instantiation real :: exhaustive
  1654 begin
  1655 
  1656 definition
  1657   "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
  1658 
  1659 instance ..
  1660 
  1661 end
  1662 
  1663 instantiation real :: full_exhaustive
  1664 begin
  1665 
  1666 definition
  1667   "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
  1668 
  1669 instance ..
  1670 
  1671 end
  1672 
  1673 instantiation real :: narrowing
  1674 begin
  1675 
  1676 definition
  1677   "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
  1678 
  1679 instance ..
  1680 
  1681 end
  1682 
  1683 
  1684 subsection {* Setup for Nitpick *}
  1685 
  1686 declaration {*
  1687   Nitpick_HOL.register_frac_type @{type_name real}
  1688    [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
  1689     (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
  1690     (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  1691     (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  1692     (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  1693     (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  1694     (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
  1695     (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  1696 *}
  1697 
  1698 lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
  1699     ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  1700     times_real_inst.times_real uminus_real_inst.uminus_real
  1701     zero_real_inst.zero_real
  1702 
  1703 end