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