src/HOL/Real.thy
author haftmann
Sat Nov 11 18:41:08 2017 +0000 (19 months ago)
changeset 67051 e7e54a0b9197
parent 66912 a99a7cbf0fb5
child 67226 ec32cdaab97b
permissions -rw-r--r--
dedicated definition for coprimality
hoelzl@51523
     1
(*  Title:      HOL/Real.thy
hoelzl@51523
     2
    Author:     Jacques D. Fleuriot, University of Edinburgh, 1998
hoelzl@51523
     3
    Author:     Larry Paulson, University of Cambridge
hoelzl@51523
     4
    Author:     Jeremy Avigad, Carnegie Mellon University
hoelzl@51523
     5
    Author:     Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
hoelzl@51523
     6
    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
hoelzl@51523
     7
    Construction of Cauchy Reals by Brian Huffman, 2010
hoelzl@51523
     8
*)
hoelzl@51523
     9
wenzelm@60758
    10
section \<open>Development of the Reals using Cauchy Sequences\<close>
hoelzl@51523
    11
hoelzl@51523
    12
theory Real
boehmes@63961
    13
imports Rat
hoelzl@51523
    14
begin
hoelzl@51523
    15
wenzelm@60758
    16
text \<open>
wenzelm@63680
    17
  This theory contains a formalization of the real numbers as equivalence
wenzelm@63680
    18
  classes of Cauchy sequences of rationals. See
wenzelm@63680
    19
  \<^file>\<open>~~/src/HOL/ex/Dedekind_Real.thy\<close> for an alternative construction using
wenzelm@63680
    20
  Dedekind cuts.
wenzelm@60758
    21
\<close>
hoelzl@51523
    22
wenzelm@63353
    23
wenzelm@60758
    24
subsection \<open>Preliminary lemmas\<close>
hoelzl@51523
    25
lp15@66793
    26
text{*Useful in convergence arguments*}
lp15@66793
    27
lemma inverse_of_nat_le:
lp15@66793
    28
  fixes n::nat shows "\<lbrakk>n \<le> m; n\<noteq>0\<rbrakk> \<Longrightarrow> 1 / of_nat m \<le> (1::'a::linordered_field) / of_nat n"
lp15@66793
    29
  by (simp add: frac_le)
lp15@66793
    30
wenzelm@63494
    31
lemma inj_add_left [simp]: "inj (op + x)"
wenzelm@63494
    32
  for x :: "'a::cancel_semigroup_add"
wenzelm@63353
    33
  by (meson add_left_imp_eq injI)
paulson@61204
    34
wenzelm@63494
    35
lemma inj_mult_left [simp]: "inj (op * x) \<longleftrightarrow> x \<noteq> 0"
wenzelm@63494
    36
  for x :: "'a::idom"
paulson@61204
    37
  by (metis injI mult_cancel_left the_inv_f_f zero_neq_one)
paulson@61204
    38
wenzelm@63494
    39
lemma add_diff_add: "(a + c) - (b + d) = (a - b) + (c - d)"
wenzelm@63494
    40
  for a b c d :: "'a::ab_group_add"
hoelzl@51523
    41
  by simp
hoelzl@51523
    42
wenzelm@63494
    43
lemma minus_diff_minus: "- a - - b = - (a - b)"
wenzelm@63494
    44
  for a b :: "'a::ab_group_add"
hoelzl@51523
    45
  by simp
hoelzl@51523
    46
wenzelm@63494
    47
lemma mult_diff_mult: "(x * y - a * b) = x * (y - b) + (x - a) * b"
wenzelm@63494
    48
  for x y a b :: "'a::ring"
hoelzl@51523
    49
  by (simp add: algebra_simps)
hoelzl@51523
    50
hoelzl@51523
    51
lemma inverse_diff_inverse:
hoelzl@51523
    52
  fixes a b :: "'a::division_ring"
hoelzl@51523
    53
  assumes "a \<noteq> 0" and "b \<noteq> 0"
hoelzl@51523
    54
  shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
hoelzl@51523
    55
  using assms by (simp add: algebra_simps)
hoelzl@51523
    56
hoelzl@51523
    57
lemma obtain_pos_sum:
hoelzl@51523
    58
  fixes r :: rat assumes r: "0 < r"
hoelzl@51523
    59
  obtains s t where "0 < s" and "0 < t" and "r = s + t"
hoelzl@51523
    60
proof
wenzelm@63353
    61
  from r show "0 < r/2" by simp
wenzelm@63353
    62
  from r show "0 < r/2" by simp
wenzelm@63353
    63
  show "r = r/2 + r/2" by simp
hoelzl@51523
    64
qed
hoelzl@51523
    65
wenzelm@63353
    66
wenzelm@60758
    67
subsection \<open>Sequences that converge to zero\<close>
hoelzl@51523
    68
wenzelm@63353
    69
definition vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
wenzelm@63353
    70
  where "vanishes X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
hoelzl@51523
    71
hoelzl@51523
    72
lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
hoelzl@51523
    73
  unfolding vanishes_def by simp
hoelzl@51523
    74
wenzelm@63353
    75
lemma vanishesD: "vanishes X \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
hoelzl@51523
    76
  unfolding vanishes_def by simp
hoelzl@51523
    77
hoelzl@51523
    78
lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
hoelzl@51523
    79
  unfolding vanishes_def
wenzelm@63353
    80
  apply (cases "c = 0")
wenzelm@63494
    81
   apply auto
wenzelm@63353
    82
  apply (rule exI [where x = "\<bar>c\<bar>"])
wenzelm@63353
    83
  apply auto
hoelzl@51523
    84
  done
hoelzl@51523
    85
hoelzl@51523
    86
lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
hoelzl@51523
    87
  unfolding vanishes_def by simp
hoelzl@51523
    88
hoelzl@51523
    89
lemma vanishes_add:
wenzelm@63353
    90
  assumes X: "vanishes X"
wenzelm@63353
    91
    and Y: "vanishes Y"
hoelzl@51523
    92
  shows "vanishes (\<lambda>n. X n + Y n)"
hoelzl@51523
    93
proof (rule vanishesI)
wenzelm@63353
    94
  fix r :: rat
wenzelm@63353
    95
  assume "0 < r"
hoelzl@51523
    96
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@51523
    97
    by (rule obtain_pos_sum)
hoelzl@51523
    98
  obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
hoelzl@51523
    99
    using vanishesD [OF X s] ..
hoelzl@51523
   100
  obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
hoelzl@51523
   101
    using vanishesD [OF Y t] ..
hoelzl@51523
   102
  have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
wenzelm@63353
   103
  proof clarsimp
wenzelm@63353
   104
    fix n
wenzelm@63353
   105
    assume n: "i \<le> n" "j \<le> n"
wenzelm@63494
   106
    have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>"
wenzelm@63494
   107
      by (rule abs_triangle_ineq)
wenzelm@63494
   108
    also have "\<dots> < s + t"
wenzelm@63494
   109
      by (simp add: add_strict_mono i j n)
wenzelm@63494
   110
    finally show "\<bar>X n + Y n\<bar> < r"
wenzelm@63494
   111
      by (simp only: r)
hoelzl@51523
   112
  qed
wenzelm@63353
   113
  then show "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
hoelzl@51523
   114
qed
hoelzl@51523
   115
hoelzl@51523
   116
lemma vanishes_diff:
wenzelm@63353
   117
  assumes "vanishes X" "vanishes Y"
hoelzl@51523
   118
  shows "vanishes (\<lambda>n. X n - Y n)"
wenzelm@63353
   119
  unfolding diff_conv_add_uminus by (intro vanishes_add vanishes_minus assms)
hoelzl@51523
   120
hoelzl@51523
   121
lemma vanishes_mult_bounded:
hoelzl@51523
   122
  assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
hoelzl@51523
   123
  assumes Y: "vanishes (\<lambda>n. Y n)"
hoelzl@51523
   124
  shows "vanishes (\<lambda>n. X n * Y n)"
hoelzl@51523
   125
proof (rule vanishesI)
wenzelm@63353
   126
  fix r :: rat
wenzelm@63353
   127
  assume r: "0 < r"
hoelzl@51523
   128
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
lp15@61649
   129
    using X by blast
hoelzl@51523
   130
  obtain b where b: "0 < b" "r = a * b"
hoelzl@51523
   131
  proof
nipkow@56541
   132
    show "0 < r / a" using r a by simp
hoelzl@51523
   133
    show "r = a * (r / a)" using a by simp
hoelzl@51523
   134
  qed
hoelzl@51523
   135
  obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
hoelzl@51523
   136
    using vanishesD [OF Y b(1)] ..
hoelzl@51523
   137
  have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
hoelzl@51523
   138
    by (simp add: b(2) abs_mult mult_strict_mono' a k)
wenzelm@63353
   139
  then show "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
hoelzl@51523
   140
qed
hoelzl@51523
   141
wenzelm@63353
   142
wenzelm@60758
   143
subsection \<open>Cauchy sequences\<close>
hoelzl@51523
   144
wenzelm@63353
   145
definition cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
wenzelm@63353
   146
  where "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
hoelzl@51523
   147
wenzelm@63353
   148
lemma cauchyI: "(\<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"
hoelzl@51523
   149
  unfolding cauchy_def by simp
hoelzl@51523
   150
wenzelm@63353
   151
lemma cauchyD: "cauchy X \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
hoelzl@51523
   152
  unfolding cauchy_def by simp
hoelzl@51523
   153
hoelzl@51523
   154
lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
hoelzl@51523
   155
  unfolding cauchy_def by simp
hoelzl@51523
   156
hoelzl@51523
   157
lemma cauchy_add [simp]:
hoelzl@51523
   158
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@51523
   159
  shows "cauchy (\<lambda>n. X n + Y n)"
hoelzl@51523
   160
proof (rule cauchyI)
wenzelm@63353
   161
  fix r :: rat
wenzelm@63353
   162
  assume "0 < r"
hoelzl@51523
   163
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@51523
   164
    by (rule obtain_pos_sum)
hoelzl@51523
   165
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@51523
   166
    using cauchyD [OF X s] ..
hoelzl@51523
   167
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
hoelzl@51523
   168
    using cauchyD [OF Y t] ..
hoelzl@51523
   169
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
wenzelm@63353
   170
  proof clarsimp
wenzelm@63353
   171
    fix m n
wenzelm@63353
   172
    assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
hoelzl@51523
   173
    have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
hoelzl@51523
   174
      unfolding add_diff_add by (rule abs_triangle_ineq)
hoelzl@51523
   175
    also have "\<dots> < s + t"
wenzelm@63353
   176
      by (rule add_strict_mono) (simp_all add: i j *)
wenzelm@63353
   177
    finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" by (simp only: r)
hoelzl@51523
   178
  qed
wenzelm@63353
   179
  then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
hoelzl@51523
   180
qed
hoelzl@51523
   181
hoelzl@51523
   182
lemma cauchy_minus [simp]:
hoelzl@51523
   183
  assumes X: "cauchy X"
hoelzl@51523
   184
  shows "cauchy (\<lambda>n. - X n)"
wenzelm@63353
   185
  using assms unfolding cauchy_def
wenzelm@63353
   186
  unfolding minus_diff_minus abs_minus_cancel .
hoelzl@51523
   187
hoelzl@51523
   188
lemma cauchy_diff [simp]:
wenzelm@63353
   189
  assumes "cauchy X" "cauchy Y"
hoelzl@51523
   190
  shows "cauchy (\<lambda>n. X n - Y n)"
haftmann@54230
   191
  using assms unfolding diff_conv_add_uminus by (simp del: add_uminus_conv_diff)
hoelzl@51523
   192
hoelzl@51523
   193
lemma cauchy_imp_bounded:
wenzelm@63353
   194
  assumes "cauchy X"
wenzelm@63353
   195
  shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
hoelzl@51523
   196
proof -
hoelzl@51523
   197
  obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
hoelzl@51523
   198
    using cauchyD [OF assms zero_less_one] ..
hoelzl@51523
   199
  show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
hoelzl@51523
   200
  proof (intro exI conjI allI)
hoelzl@51523
   201
    have "0 \<le> \<bar>X 0\<bar>" by simp
hoelzl@51523
   202
    also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
hoelzl@51523
   203
    finally have "0 \<le> Max (abs ` X ` {..k})" .
wenzelm@63353
   204
    then show "0 < Max (abs ` X ` {..k}) + 1" by simp
hoelzl@51523
   205
  next
hoelzl@51523
   206
    fix n :: nat
hoelzl@51523
   207
    show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
hoelzl@51523
   208
    proof (rule linorder_le_cases)
hoelzl@51523
   209
      assume "n \<le> k"
wenzelm@63353
   210
      then have "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
wenzelm@63353
   211
      then show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
hoelzl@51523
   212
    next
hoelzl@51523
   213
      assume "k \<le> n"
hoelzl@51523
   214
      have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
hoelzl@51523
   215
      also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
hoelzl@51523
   216
        by (rule abs_triangle_ineq)
hoelzl@51523
   217
      also have "\<dots> < Max (abs ` X ` {..k}) + 1"
wenzelm@63353
   218
        by (rule add_le_less_mono) (simp_all add: k \<open>k \<le> n\<close>)
hoelzl@51523
   219
      finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
hoelzl@51523
   220
    qed
hoelzl@51523
   221
  qed
hoelzl@51523
   222
qed
hoelzl@51523
   223
hoelzl@51523
   224
lemma cauchy_mult [simp]:
hoelzl@51523
   225
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@51523
   226
  shows "cauchy (\<lambda>n. X n * Y n)"
hoelzl@51523
   227
proof (rule cauchyI)
hoelzl@51523
   228
  fix r :: rat assume "0 < r"
hoelzl@51523
   229
  then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
hoelzl@51523
   230
    by (rule obtain_pos_sum)
hoelzl@51523
   231
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
lp15@61649
   232
    using cauchy_imp_bounded [OF X] by blast
hoelzl@51523
   233
  obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
lp15@61649
   234
    using cauchy_imp_bounded [OF Y] by blast
hoelzl@51523
   235
  obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
hoelzl@51523
   236
  proof
nipkow@56541
   237
    show "0 < v/b" using v b(1) by simp
nipkow@56541
   238
    show "0 < u/a" using u a(1) by simp
hoelzl@51523
   239
    show "r = a * (u/a) + (v/b) * b"
wenzelm@60758
   240
      using a(1) b(1) \<open>r = u + v\<close> by simp
hoelzl@51523
   241
  qed
hoelzl@51523
   242
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@51523
   243
    using cauchyD [OF X s] ..
hoelzl@51523
   244
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
hoelzl@51523
   245
    using cauchyD [OF Y t] ..
hoelzl@51523
   246
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
wenzelm@63353
   247
  proof clarsimp
wenzelm@63353
   248
    fix m n
wenzelm@63353
   249
    assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
hoelzl@51523
   250
    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>"
hoelzl@51523
   251
      unfolding mult_diff_mult ..
hoelzl@51523
   252
    also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
hoelzl@51523
   253
      by (rule abs_triangle_ineq)
hoelzl@51523
   254
    also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
hoelzl@51523
   255
      unfolding abs_mult ..
hoelzl@51523
   256
    also have "\<dots> < a * t + s * b"
hoelzl@51523
   257
      by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
wenzelm@63494
   258
    finally show "\<bar>X m * Y m - X n * Y n\<bar> < r"
wenzelm@63494
   259
      by (simp only: r)
hoelzl@51523
   260
  qed
wenzelm@63353
   261
  then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
hoelzl@51523
   262
qed
hoelzl@51523
   263
hoelzl@51523
   264
lemma cauchy_not_vanishes_cases:
hoelzl@51523
   265
  assumes X: "cauchy X"
hoelzl@51523
   266
  assumes nz: "\<not> vanishes X"
hoelzl@51523
   267
  shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
hoelzl@51523
   268
proof -
hoelzl@51523
   269
  obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
hoelzl@51523
   270
    using nz unfolding vanishes_def by (auto simp add: not_less)
hoelzl@51523
   271
  obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
wenzelm@60758
   272
    using \<open>0 < r\<close> by (rule obtain_pos_sum)
hoelzl@51523
   273
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@51523
   274
    using cauchyD [OF X s] ..
hoelzl@51523
   275
  obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
lp15@61649
   276
    using r by blast
hoelzl@51523
   277
  have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
wenzelm@60758
   278
    using i \<open>i \<le> k\<close> by auto
hoelzl@51523
   279
  have "X k \<le> - r \<or> r \<le> X k"
wenzelm@60758
   280
    using \<open>r \<le> \<bar>X k\<bar>\<close> by auto
wenzelm@63353
   281
  then have "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
wenzelm@60758
   282
    unfolding \<open>r = s + t\<close> using k by auto
wenzelm@63353
   283
  then have "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
wenzelm@63353
   284
  then show "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
hoelzl@51523
   285
    using t by auto
hoelzl@51523
   286
qed
hoelzl@51523
   287
hoelzl@51523
   288
lemma cauchy_not_vanishes:
hoelzl@51523
   289
  assumes X: "cauchy X"
wenzelm@63494
   290
    and nz: "\<not> vanishes X"
hoelzl@51523
   291
  shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
wenzelm@63353
   292
  using cauchy_not_vanishes_cases [OF assms]
wenzelm@63353
   293
  apply clarify
wenzelm@63353
   294
  apply (rule exI)
wenzelm@63353
   295
  apply (erule conjI)
wenzelm@63353
   296
  apply (rule_tac x = k in exI)
wenzelm@63353
   297
  apply auto
wenzelm@63353
   298
  done
hoelzl@51523
   299
hoelzl@51523
   300
lemma cauchy_inverse [simp]:
hoelzl@51523
   301
  assumes X: "cauchy X"
wenzelm@63494
   302
    and nz: "\<not> vanishes X"
hoelzl@51523
   303
  shows "cauchy (\<lambda>n. inverse (X n))"
hoelzl@51523
   304
proof (rule cauchyI)
wenzelm@63353
   305
  fix r :: rat
wenzelm@63353
   306
  assume "0 < r"
hoelzl@51523
   307
  obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
lp15@61649
   308
    using cauchy_not_vanishes [OF X nz] by blast
hoelzl@51523
   309
  from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
hoelzl@51523
   310
  obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
hoelzl@51523
   311
  proof
wenzelm@60758
   312
    show "0 < b * r * b" by (simp add: \<open>0 < r\<close> b)
hoelzl@51523
   313
    show "r = inverse b * (b * r * b) * inverse b"
hoelzl@51523
   314
      using b by simp
hoelzl@51523
   315
  qed
hoelzl@51523
   316
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
hoelzl@51523
   317
    using cauchyD [OF X s] ..
hoelzl@51523
   318
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
wenzelm@63353
   319
  proof clarsimp
wenzelm@63353
   320
    fix m n
wenzelm@63353
   321
    assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
wenzelm@63353
   322
    have "\<bar>inverse (X m) - inverse (X n)\<bar> = inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
hoelzl@51523
   323
      by (simp add: inverse_diff_inverse nz * abs_mult)
hoelzl@51523
   324
    also have "\<dots> < inverse b * s * inverse b"
wenzelm@63353
   325
      by (simp add: mult_strict_mono less_imp_inverse_less i j b * s)
wenzelm@63353
   326
    finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" by (simp only: r)
hoelzl@51523
   327
  qed
wenzelm@63353
   328
  then show "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
hoelzl@51523
   329
qed
hoelzl@51523
   330
hoelzl@51523
   331
lemma vanishes_diff_inverse:
hoelzl@51523
   332
  assumes X: "cauchy X" "\<not> vanishes X"
wenzelm@63353
   333
    and Y: "cauchy Y" "\<not> vanishes Y"
wenzelm@63353
   334
    and XY: "vanishes (\<lambda>n. X n - Y n)"
hoelzl@51523
   335
  shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
hoelzl@51523
   336
proof (rule vanishesI)
wenzelm@63353
   337
  fix r :: rat
wenzelm@63353
   338
  assume r: "0 < r"
hoelzl@51523
   339
  obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
lp15@61649
   340
    using cauchy_not_vanishes [OF X] by blast
hoelzl@51523
   341
  obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
lp15@61649
   342
    using cauchy_not_vanishes [OF Y] by blast
hoelzl@51523
   343
  obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
hoelzl@51523
   344
  proof
wenzelm@63494
   345
    show "0 < a * r * b"
wenzelm@63494
   346
      using a r b by simp
wenzelm@63494
   347
    show "inverse a * (a * r * b) * inverse b = r"
wenzelm@63494
   348
      using a r b by simp
hoelzl@51523
   349
  qed
hoelzl@51523
   350
  obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
hoelzl@51523
   351
    using vanishesD [OF XY s] ..
hoelzl@51523
   352
  have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
wenzelm@63353
   353
  proof clarsimp
wenzelm@63353
   354
    fix n
wenzelm@63353
   355
    assume n: "i \<le> n" "j \<le> n" "k \<le> n"
wenzelm@63353
   356
    with i j a b have "X n \<noteq> 0" and "Y n \<noteq> 0"
wenzelm@63353
   357
      by auto
wenzelm@63353
   358
    then have "\<bar>inverse (X n) - inverse (Y n)\<bar> = inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
hoelzl@51523
   359
      by (simp add: inverse_diff_inverse abs_mult)
hoelzl@51523
   360
    also have "\<dots> < inverse a * s * inverse b"
wenzelm@63353
   361
      by (intro mult_strict_mono' less_imp_inverse_less) (simp_all add: a b i j k n)
wenzelm@60758
   362
    also note \<open>inverse a * s * inverse b = r\<close>
hoelzl@51523
   363
    finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
hoelzl@51523
   364
  qed
wenzelm@63353
   365
  then show "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
hoelzl@51523
   366
qed
hoelzl@51523
   367
wenzelm@63353
   368
wenzelm@60758
   369
subsection \<open>Equivalence relation on Cauchy sequences\<close>
hoelzl@51523
   370
hoelzl@51523
   371
definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
hoelzl@51523
   372
  where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
hoelzl@51523
   373
wenzelm@63353
   374
lemma realrelI [intro?]: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> vanishes (\<lambda>n. X n - Y n) \<Longrightarrow> realrel X Y"
wenzelm@63353
   375
  by (simp add: realrel_def)
hoelzl@51523
   376
hoelzl@51523
   377
lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
wenzelm@63353
   378
  by (simp add: realrel_def)
hoelzl@51523
   379
hoelzl@51523
   380
lemma symp_realrel: "symp realrel"
hoelzl@51523
   381
  unfolding realrel_def
wenzelm@63353
   382
  apply (rule sympI)
wenzelm@63353
   383
  apply clarify
wenzelm@63353
   384
  apply (drule vanishes_minus)
wenzelm@63353
   385
  apply simp
wenzelm@63353
   386
  done
hoelzl@51523
   387
hoelzl@51523
   388
lemma transp_realrel: "transp realrel"
hoelzl@51523
   389
  unfolding realrel_def
wenzelm@63353
   390
  apply (rule transpI)
wenzelm@63353
   391
  apply clarify
hoelzl@51523
   392
  apply (drule (1) vanishes_add)
hoelzl@51523
   393
  apply (simp add: algebra_simps)
hoelzl@51523
   394
  done
hoelzl@51523
   395
hoelzl@51523
   396
lemma part_equivp_realrel: "part_equivp realrel"
wenzelm@63353
   397
  by (blast intro: part_equivpI symp_realrel transp_realrel realrel_refl cauchy_const)
wenzelm@63353
   398
hoelzl@51523
   399
wenzelm@60758
   400
subsection \<open>The field of real numbers\<close>
hoelzl@51523
   401
hoelzl@51523
   402
quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
hoelzl@51523
   403
  morphisms rep_real Real
hoelzl@51523
   404
  by (rule part_equivp_realrel)
hoelzl@51523
   405
hoelzl@51523
   406
lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
hoelzl@51523
   407
  unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
hoelzl@51523
   408
hoelzl@51523
   409
lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
wenzelm@63353
   410
  assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)"
wenzelm@63353
   411
  shows "P x"
hoelzl@51523
   412
proof (induct x)
hoelzl@51523
   413
  case (1 X)
wenzelm@63353
   414
  then have "cauchy X" by (simp add: realrel_def)
wenzelm@63353
   415
  then show "P (Real X)" by (rule assms)
hoelzl@51523
   416
qed
hoelzl@51523
   417
wenzelm@63353
   418
lemma eq_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
hoelzl@51523
   419
  using real.rel_eq_transfer
blanchet@55945
   420
  unfolding real.pcr_cr_eq cr_real_def rel_fun_def realrel_def by simp
hoelzl@51523
   421
kuncar@51956
   422
lemma Domainp_pcr_real [transfer_domain_rule]: "Domainp pcr_real = cauchy"
wenzelm@63353
   423
  by (simp add: real.domain_eq realrel_def)
hoelzl@51523
   424
haftmann@59867
   425
instantiation real :: field
hoelzl@51523
   426
begin
hoelzl@51523
   427
hoelzl@51523
   428
lift_definition zero_real :: "real" is "\<lambda>n. 0"
hoelzl@51523
   429
  by (simp add: realrel_refl)
hoelzl@51523
   430
hoelzl@51523
   431
lift_definition one_real :: "real" is "\<lambda>n. 1"
hoelzl@51523
   432
  by (simp add: realrel_refl)
hoelzl@51523
   433
hoelzl@51523
   434
lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
hoelzl@51523
   435
  unfolding realrel_def add_diff_add
hoelzl@51523
   436
  by (simp only: cauchy_add vanishes_add simp_thms)
hoelzl@51523
   437
hoelzl@51523
   438
lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
hoelzl@51523
   439
  unfolding realrel_def minus_diff_minus
hoelzl@51523
   440
  by (simp only: cauchy_minus vanishes_minus simp_thms)
hoelzl@51523
   441
hoelzl@51523
   442
lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
hoelzl@51523
   443
  unfolding realrel_def mult_diff_mult
wenzelm@63353
   444
  apply (subst (4) mult.commute)
wenzelm@63353
   445
  apply (simp only: cauchy_mult vanishes_add vanishes_mult_bounded cauchy_imp_bounded simp_thms)
wenzelm@63353
   446
  done
hoelzl@51523
   447
hoelzl@51523
   448
lift_definition inverse_real :: "real \<Rightarrow> real"
hoelzl@51523
   449
  is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
hoelzl@51523
   450
proof -
wenzelm@63353
   451
  fix X Y
wenzelm@63353
   452
  assume "realrel X Y"
wenzelm@63353
   453
  then have X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
wenzelm@63494
   454
    by (simp_all add: realrel_def)
hoelzl@51523
   455
  have "vanishes X \<longleftrightarrow> vanishes Y"
hoelzl@51523
   456
  proof
hoelzl@51523
   457
    assume "vanishes X"
wenzelm@63494
   458
    from vanishes_diff [OF this XY] show "vanishes Y"
wenzelm@63494
   459
      by simp
hoelzl@51523
   460
  next
hoelzl@51523
   461
    assume "vanishes Y"
wenzelm@63494
   462
    from vanishes_add [OF this XY] show "vanishes X"
wenzelm@63494
   463
      by simp
hoelzl@51523
   464
  qed
wenzelm@63494
   465
  then show "?thesis X Y"
wenzelm@63494
   466
    by (simp add: vanishes_diff_inverse X Y XY realrel_def)
hoelzl@51523
   467
qed
hoelzl@51523
   468
wenzelm@63353
   469
definition "x - y = x + - y" for x y :: real
hoelzl@51523
   470
wenzelm@63353
   471
definition "x div y = x * inverse y" for x y :: real
wenzelm@63353
   472
wenzelm@63353
   473
lemma add_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X + Real Y = Real (\<lambda>n. X n + Y n)"
wenzelm@63353
   474
  using plus_real.transfer by (simp add: cr_real_eq rel_fun_def)
hoelzl@51523
   475
wenzelm@63353
   476
lemma minus_Real: "cauchy X \<Longrightarrow> - Real X = Real (\<lambda>n. - X n)"
wenzelm@63353
   477
  using uminus_real.transfer by (simp add: cr_real_eq rel_fun_def)
hoelzl@51523
   478
wenzelm@63353
   479
lemma diff_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X - Real Y = Real (\<lambda>n. X n - Y n)"
wenzelm@63353
   480
  by (simp add: minus_Real add_Real minus_real_def)
hoelzl@51523
   481
wenzelm@63353
   482
lemma mult_Real: "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X * Real Y = Real (\<lambda>n. X n * Y n)"
wenzelm@63353
   483
  using times_real.transfer by (simp add: cr_real_eq rel_fun_def)
hoelzl@51523
   484
hoelzl@51523
   485
lemma inverse_Real:
wenzelm@63353
   486
  "cauchy X \<Longrightarrow> inverse (Real X) = (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
wenzelm@63353
   487
  using inverse_real.transfer zero_real.transfer
nipkow@62390
   488
  unfolding cr_real_eq rel_fun_def by (simp split: if_split_asm, metis)
hoelzl@51523
   489
wenzelm@63353
   490
instance
wenzelm@63353
   491
proof
hoelzl@51523
   492
  fix a b c :: real
hoelzl@51523
   493
  show "a + b = b + a"
haftmann@57514
   494
    by transfer (simp add: ac_simps realrel_def)
hoelzl@51523
   495
  show "(a + b) + c = a + (b + c)"
haftmann@57514
   496
    by transfer (simp add: ac_simps realrel_def)
hoelzl@51523
   497
  show "0 + a = a"
hoelzl@51523
   498
    by transfer (simp add: realrel_def)
hoelzl@51523
   499
  show "- a + a = 0"
hoelzl@51523
   500
    by transfer (simp add: realrel_def)
hoelzl@51523
   501
  show "a - b = a + - b"
hoelzl@51523
   502
    by (rule minus_real_def)
hoelzl@51523
   503
  show "(a * b) * c = a * (b * c)"
haftmann@57514
   504
    by transfer (simp add: ac_simps realrel_def)
hoelzl@51523
   505
  show "a * b = b * a"
haftmann@57514
   506
    by transfer (simp add: ac_simps realrel_def)
hoelzl@51523
   507
  show "1 * a = a"
haftmann@57514
   508
    by transfer (simp add: ac_simps realrel_def)
hoelzl@51523
   509
  show "(a + b) * c = a * c + b * c"
hoelzl@51523
   510
    by transfer (simp add: distrib_right realrel_def)
wenzelm@61076
   511
  show "(0::real) \<noteq> (1::real)"
hoelzl@51523
   512
    by transfer (simp add: realrel_def)
hoelzl@51523
   513
  show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
hoelzl@51523
   514
    apply transfer
hoelzl@51523
   515
    apply (simp add: realrel_def)
hoelzl@51523
   516
    apply (rule vanishesI)
wenzelm@63494
   517
    apply (frule (1) cauchy_not_vanishes)
wenzelm@63494
   518
    apply clarify
wenzelm@63494
   519
    apply (rule_tac x=k in exI)
wenzelm@63494
   520
    apply clarify
wenzelm@63494
   521
    apply (drule_tac x=n in spec)
wenzelm@63494
   522
    apply simp
hoelzl@51523
   523
    done
haftmann@60429
   524
  show "a div b = a * inverse b"
hoelzl@51523
   525
    by (rule divide_real_def)
hoelzl@51523
   526
  show "inverse (0::real) = 0"
hoelzl@51523
   527
    by transfer (simp add: realrel_def)
hoelzl@51523
   528
qed
hoelzl@51523
   529
hoelzl@51523
   530
end
hoelzl@51523
   531
wenzelm@63353
   532
wenzelm@60758
   533
subsection \<open>Positive reals\<close>
hoelzl@51523
   534
hoelzl@51523
   535
lift_definition positive :: "real \<Rightarrow> bool"
hoelzl@51523
   536
  is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
hoelzl@51523
   537
proof -
wenzelm@63353
   538
  have 1: "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n"
wenzelm@63353
   539
    if *: "realrel X Y" and **: "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n" for X Y
wenzelm@63353
   540
  proof -
wenzelm@63353
   541
    from * have XY: "vanishes (\<lambda>n. X n - Y n)"
wenzelm@63353
   542
      by (simp_all add: realrel_def)
wenzelm@63353
   543
    from ** obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
lp15@61649
   544
      by blast
hoelzl@51523
   545
    obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
wenzelm@60758
   546
      using \<open>0 < r\<close> by (rule obtain_pos_sum)
hoelzl@51523
   547
    obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
hoelzl@51523
   548
      using vanishesD [OF XY s] ..
hoelzl@51523
   549
    have "\<forall>n\<ge>max i j. t < Y n"
wenzelm@63353
   550
    proof clarsimp
wenzelm@63353
   551
      fix n
wenzelm@63353
   552
      assume n: "i \<le> n" "j \<le> n"
hoelzl@51523
   553
      have "\<bar>X n - Y n\<bar> < s" and "r < X n"
hoelzl@51523
   554
        using i j n by simp_all
wenzelm@63353
   555
      then show "t < Y n" by (simp add: r)
hoelzl@51523
   556
    qed
wenzelm@63353
   557
    then show ?thesis using t by blast
wenzelm@63353
   558
  qed
hoelzl@51523
   559
  fix X Y assume "realrel X Y"
wenzelm@63353
   560
  then have "realrel X Y" and "realrel Y X"
wenzelm@63353
   561
    using symp_realrel by (auto simp: symp_def)
wenzelm@63353
   562
  then show "?thesis X Y"
hoelzl@51523
   563
    by (safe elim!: 1)
hoelzl@51523
   564
qed
hoelzl@51523
   565
wenzelm@63353
   566
lemma positive_Real: "cauchy X \<Longrightarrow> positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
wenzelm@63353
   567
  using positive.transfer by (simp add: cr_real_eq rel_fun_def)
hoelzl@51523
   568
hoelzl@51523
   569
lemma positive_zero: "\<not> positive 0"
hoelzl@51523
   570
  by transfer auto
hoelzl@51523
   571
wenzelm@63353
   572
lemma positive_add: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
wenzelm@63353
   573
  apply transfer
wenzelm@63353
   574
  apply clarify
wenzelm@63353
   575
  apply (rename_tac a b i j)
wenzelm@63353
   576
  apply (rule_tac x = "a + b" in exI)
wenzelm@63353
   577
  apply simp
wenzelm@63353
   578
  apply (rule_tac x = "max i j" in exI)
wenzelm@63353
   579
  apply clarsimp
wenzelm@63353
   580
  apply (simp add: add_strict_mono)
wenzelm@63353
   581
  done
hoelzl@51523
   582
wenzelm@63353
   583
lemma positive_mult: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
wenzelm@63353
   584
  apply transfer
wenzelm@63353
   585
  apply clarify
wenzelm@63353
   586
  apply (rename_tac a b i j)
wenzelm@63353
   587
  apply (rule_tac x = "a * b" in exI)
wenzelm@63353
   588
  apply simp
wenzelm@63353
   589
  apply (rule_tac x = "max i j" in exI)
wenzelm@63353
   590
  apply clarsimp
wenzelm@63353
   591
  apply (rule mult_strict_mono)
wenzelm@63494
   592
     apply auto
wenzelm@63353
   593
  done
hoelzl@51523
   594
wenzelm@63353
   595
lemma positive_minus: "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
wenzelm@63353
   596
  apply transfer
wenzelm@63353
   597
  apply (simp add: realrel_def)
wenzelm@63494
   598
  apply (drule (1) cauchy_not_vanishes_cases)
wenzelm@63494
   599
  apply safe
wenzelm@63494
   600
   apply blast+
wenzelm@63353
   601
  done
hoelzl@51523
   602
haftmann@59867
   603
instantiation real :: linordered_field
hoelzl@51523
   604
begin
hoelzl@51523
   605
wenzelm@63353
   606
definition "x < y \<longleftrightarrow> positive (y - x)"
hoelzl@51523
   607
wenzelm@63353
   608
definition "x \<le> y \<longleftrightarrow> x < y \<or> x = y" for x y :: real
hoelzl@51523
   609
wenzelm@63353
   610
definition "\<bar>a\<bar> = (if a < 0 then - a else a)" for a :: real
hoelzl@51523
   611
wenzelm@63353
   612
definition "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)" for a :: real
hoelzl@51523
   613
wenzelm@63353
   614
instance
wenzelm@63353
   615
proof
hoelzl@51523
   616
  fix a b c :: real
hoelzl@51523
   617
  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
hoelzl@51523
   618
    by (rule abs_real_def)
hoelzl@51523
   619
  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
hoelzl@51523
   620
    unfolding less_eq_real_def less_real_def
wenzelm@63353
   621
    apply auto
wenzelm@63494
   622
     apply (drule (1) positive_add)
wenzelm@63494
   623
     apply (simp_all add: positive_zero)
wenzelm@63353
   624
    done
hoelzl@51523
   625
  show "a \<le> a"
hoelzl@51523
   626
    unfolding less_eq_real_def by simp
hoelzl@51523
   627
  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
hoelzl@51523
   628
    unfolding less_eq_real_def less_real_def
wenzelm@63353
   629
    apply auto
wenzelm@63353
   630
    apply (drule (1) positive_add)
wenzelm@63353
   631
    apply (simp add: algebra_simps)
wenzelm@63353
   632
    done
hoelzl@51523
   633
  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
hoelzl@51523
   634
    unfolding less_eq_real_def less_real_def
wenzelm@63353
   635
    apply auto
wenzelm@63353
   636
    apply (drule (1) positive_add)
wenzelm@63353
   637
    apply (simp add: positive_zero)
wenzelm@63353
   638
    done
hoelzl@51523
   639
  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
wenzelm@63353
   640
    by (auto simp: less_eq_real_def less_real_def)
hoelzl@51523
   641
    (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
hoelzl@51523
   642
    (* Should produce c + b - (c + a) \<equiv> b - a *)
hoelzl@51523
   643
  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
hoelzl@51523
   644
    by (rule sgn_real_def)
hoelzl@51523
   645
  show "a \<le> b \<or> b \<le> a"
wenzelm@63353
   646
    by (auto dest!: positive_minus simp: less_eq_real_def less_real_def)
hoelzl@51523
   647
  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
hoelzl@51523
   648
    unfolding less_real_def
wenzelm@63353
   649
    apply (drule (1) positive_mult)
wenzelm@63353
   650
    apply (simp add: algebra_simps)
wenzelm@63353
   651
    done
hoelzl@51523
   652
qed
hoelzl@51523
   653
hoelzl@51523
   654
end
hoelzl@51523
   655
hoelzl@51523
   656
instantiation real :: distrib_lattice
hoelzl@51523
   657
begin
hoelzl@51523
   658
wenzelm@63353
   659
definition "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
hoelzl@51523
   660
wenzelm@63353
   661
definition "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
hoelzl@51523
   662
wenzelm@63494
   663
instance
wenzelm@63494
   664
  by standard (auto simp add: inf_real_def sup_real_def max_min_distrib2)
hoelzl@51523
   665
hoelzl@51523
   666
end
hoelzl@51523
   667
hoelzl@51523
   668
lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
wenzelm@63353
   669
  by (induct x) (simp_all add: zero_real_def one_real_def add_Real)
hoelzl@51523
   670
hoelzl@51523
   671
lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
wenzelm@63353
   672
  by (cases x rule: int_diff_cases) (simp add: of_nat_Real diff_Real)
hoelzl@51523
   673
hoelzl@51523
   674
lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
wenzelm@63353
   675
  apply (induct x)
wenzelm@63353
   676
  apply (simp add: Fract_of_int_quotient of_rat_divide)
wenzelm@63353
   677
  apply (simp add: of_int_Real divide_inverse)
wenzelm@63353
   678
  apply (simp add: inverse_Real mult_Real)
wenzelm@63353
   679
  done
hoelzl@51523
   680
hoelzl@51523
   681
instance real :: archimedean_field
hoelzl@51523
   682
proof
wenzelm@63494
   683
  show "\<exists>z. x \<le> of_int z" for x :: real
hoelzl@51523
   684
    apply (induct x)
hoelzl@51523
   685
    apply (frule cauchy_imp_bounded, clarify)
wenzelm@61942
   686
    apply (rule_tac x="\<lceil>b\<rceil> + 1" in exI)
hoelzl@51523
   687
    apply (rule less_imp_le)
hoelzl@51523
   688
    apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
wenzelm@63494
   689
    apply (rule_tac x=1 in exI)
wenzelm@63494
   690
    apply (simp add: algebra_simps)
wenzelm@63494
   691
    apply (rule_tac x=0 in exI)
wenzelm@63494
   692
    apply clarsimp
hoelzl@51523
   693
    apply (rule le_less_trans [OF abs_ge_self])
hoelzl@51523
   694
    apply (rule less_le_trans [OF _ le_of_int_ceiling])
hoelzl@51523
   695
    apply simp
hoelzl@51523
   696
    done
hoelzl@51523
   697
qed
hoelzl@51523
   698
hoelzl@51523
   699
instantiation real :: floor_ceiling
hoelzl@51523
   700
begin
hoelzl@51523
   701
wenzelm@63353
   702
definition [code del]: "\<lfloor>x::real\<rfloor> = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
hoelzl@51523
   703
wenzelm@61942
   704
instance
wenzelm@61942
   705
proof
wenzelm@63353
   706
  show "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)" for x :: real
hoelzl@51523
   707
    unfolding floor_real_def using floor_exists1 by (rule theI')
hoelzl@51523
   708
qed
hoelzl@51523
   709
hoelzl@51523
   710
end
hoelzl@51523
   711
wenzelm@63353
   712
wenzelm@60758
   713
subsection \<open>Completeness\<close>
hoelzl@51523
   714
wenzelm@63494
   715
lemma not_positive_Real: "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)" if "cauchy X"
wenzelm@63494
   716
  apply (simp only: positive_Real [OF that])
wenzelm@63353
   717
  apply auto
wenzelm@63494
   718
   apply (unfold not_less)
wenzelm@63494
   719
   apply (erule obtain_pos_sum)
wenzelm@63494
   720
   apply (drule_tac x=s in spec)
wenzelm@63494
   721
   apply simp
wenzelm@63494
   722
   apply (drule_tac r=t in cauchyD [OF that])
wenzelm@63494
   723
   apply clarify
wenzelm@63494
   724
   apply (drule_tac x=k in spec)
wenzelm@63494
   725
   apply clarsimp
wenzelm@63494
   726
   apply (rule_tac x=n in exI)
wenzelm@63494
   727
   apply clarify
wenzelm@63494
   728
   apply (rename_tac m)
wenzelm@63494
   729
   apply (drule_tac x=m in spec)
wenzelm@63494
   730
   apply simp
wenzelm@63494
   731
   apply (drule_tac x=n in spec)
wenzelm@63494
   732
   apply simp
wenzelm@63353
   733
  apply (drule spec)
wenzelm@63353
   734
  apply (drule (1) mp)
wenzelm@63353
   735
  apply clarify
wenzelm@63353
   736
  apply (rename_tac i)
wenzelm@63353
   737
  apply (rule_tac x = "max i k" in exI)
wenzelm@63353
   738
  apply simp
wenzelm@63353
   739
  done
hoelzl@51523
   740
hoelzl@51523
   741
lemma le_Real:
wenzelm@63353
   742
  assumes "cauchy X" "cauchy Y"
hoelzl@51523
   743
  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
wenzelm@63353
   744
  unfolding not_less [symmetric, where 'a=real] less_real_def
wenzelm@63353
   745
  apply (simp add: diff_Real not_positive_Real assms)
wenzelm@63353
   746
  apply (simp add: diff_le_eq ac_simps)
wenzelm@63353
   747
  done
hoelzl@51523
   748
hoelzl@51523
   749
lemma le_RealI:
hoelzl@51523
   750
  assumes Y: "cauchy Y"
hoelzl@51523
   751
  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
hoelzl@51523
   752
proof (induct x)
wenzelm@63353
   753
  fix X
wenzelm@63353
   754
  assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
wenzelm@63353
   755
  then have le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
hoelzl@51523
   756
    by (simp add: of_rat_Real le_Real)
wenzelm@63353
   757
  then have "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" if "0 < r" for r :: rat
wenzelm@63353
   758
  proof -
wenzelm@63353
   759
    from that obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@51523
   760
      by (rule obtain_pos_sum)
hoelzl@51523
   761
    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
hoelzl@51523
   762
      using cauchyD [OF Y s] ..
hoelzl@51523
   763
    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
hoelzl@51523
   764
      using le [OF t] ..
hoelzl@51523
   765
    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
wenzelm@63353
   766
    proof clarsimp
wenzelm@63353
   767
      fix n
wenzelm@63353
   768
      assume n: "i \<le> n" "j \<le> n"
wenzelm@63494
   769
      have "X n \<le> Y i + t"
wenzelm@63494
   770
        using n j by simp
wenzelm@63494
   771
      moreover have "\<bar>Y i - Y n\<bar> < s"
wenzelm@63494
   772
        using n i by simp
wenzelm@63494
   773
      ultimately show "X n \<le> Y n + r"
wenzelm@63494
   774
        unfolding r by simp
hoelzl@51523
   775
    qed
wenzelm@63353
   776
    then show ?thesis ..
wenzelm@63353
   777
  qed
wenzelm@63353
   778
  then show "Real X \<le> Real Y"
hoelzl@51523
   779
    by (simp add: of_rat_Real le_Real X Y)
hoelzl@51523
   780
qed
hoelzl@51523
   781
hoelzl@51523
   782
lemma Real_leI:
hoelzl@51523
   783
  assumes X: "cauchy X"
hoelzl@51523
   784
  assumes le: "\<forall>n. of_rat (X n) \<le> y"
hoelzl@51523
   785
  shows "Real X \<le> y"
hoelzl@51523
   786
proof -
hoelzl@51523
   787
  have "- y \<le> - Real X"
hoelzl@51523
   788
    by (simp add: minus_Real X le_RealI of_rat_minus le)
wenzelm@63353
   789
  then show ?thesis by simp
hoelzl@51523
   790
qed
hoelzl@51523
   791
hoelzl@51523
   792
lemma less_RealD:
wenzelm@63353
   793
  assumes "cauchy Y"
hoelzl@51523
   794
  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
wenzelm@63353
   795
  apply (erule contrapos_pp)
wenzelm@63353
   796
  apply (simp add: not_less)
wenzelm@63353
   797
  apply (erule Real_leI [OF assms])
wenzelm@63353
   798
  done
hoelzl@51523
   799
wenzelm@63353
   800
lemma of_nat_less_two_power [simp]: "of_nat n < (2::'a::linordered_idom) ^ n"
wenzelm@63353
   801
  apply (induct n)
wenzelm@63494
   802
   apply simp
wenzelm@63353
   803
  apply (metis add_le_less_mono mult_2 of_nat_Suc one_le_numeral one_le_power power_Suc)
wenzelm@63353
   804
  done
hoelzl@51523
   805
hoelzl@51523
   806
lemma complete_real:
hoelzl@51523
   807
  fixes S :: "real set"
hoelzl@51523
   808
  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
hoelzl@51523
   809
  shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
hoelzl@51523
   810
proof -
hoelzl@51523
   811
  obtain x where x: "x \<in> S" using assms(1) ..
hoelzl@51523
   812
  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
hoelzl@51523
   813
wenzelm@63040
   814
  define P where "P x \<longleftrightarrow> (\<forall>y\<in>S. y \<le> of_rat x)" for x
hoelzl@51523
   815
  obtain a where a: "\<not> P a"
hoelzl@51523
   816
  proof
wenzelm@61942
   817
    have "of_int \<lfloor>x - 1\<rfloor> \<le> x - 1" by (rule of_int_floor_le)
hoelzl@51523
   818
    also have "x - 1 < x" by simp
wenzelm@61942
   819
    finally have "of_int \<lfloor>x - 1\<rfloor> < x" .
wenzelm@63353
   820
    then have "\<not> x \<le> of_int \<lfloor>x - 1\<rfloor>" by (simp only: not_le)
wenzelm@61942
   821
    then show "\<not> P (of_int \<lfloor>x - 1\<rfloor>)"
lp15@61649
   822
      unfolding P_def of_rat_of_int_eq using x by blast
hoelzl@51523
   823
  qed
hoelzl@51523
   824
  obtain b where b: "P b"
hoelzl@51523
   825
  proof
wenzelm@61942
   826
    show "P (of_int \<lceil>z\<rceil>)"
hoelzl@51523
   827
    unfolding P_def of_rat_of_int_eq
hoelzl@51523
   828
    proof
hoelzl@51523
   829
      fix y assume "y \<in> S"
wenzelm@63353
   830
      then have "y \<le> z" using z by simp
wenzelm@61942
   831
      also have "z \<le> of_int \<lceil>z\<rceil>" by (rule le_of_int_ceiling)
wenzelm@61942
   832
      finally show "y \<le> of_int \<lceil>z\<rceil>" .
hoelzl@51523
   833
    qed
hoelzl@51523
   834
  qed
hoelzl@51523
   835
wenzelm@63040
   836
  define avg where "avg x y = x/2 + y/2" for x y :: rat
wenzelm@63040
   837
  define bisect where "bisect = (\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y))"
wenzelm@63040
   838
  define A where "A n = fst ((bisect ^^ n) (a, b))" for n
wenzelm@63040
   839
  define B where "B n = snd ((bisect ^^ n) (a, b))" for n
wenzelm@63040
   840
  define C where "C n = avg (A n) (B n)" for n
hoelzl@51523
   841
  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
hoelzl@51523
   842
  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
hoelzl@51523
   843
  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
hoelzl@51523
   844
    unfolding A_def B_def C_def bisect_def split_def by simp
hoelzl@51523
   845
  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
hoelzl@51523
   846
    unfolding A_def B_def C_def bisect_def split_def by simp
hoelzl@51523
   847
wenzelm@63353
   848
  have width: "B n - A n = (b - a) / 2^n" for n
wenzelm@63353
   849
    apply (induct n)
wenzelm@63494
   850
     apply (simp_all add: eq_divide_eq)
wenzelm@63353
   851
    apply (simp_all add: C_def avg_def algebra_simps)
hoelzl@51523
   852
    done
hoelzl@51523
   853
wenzelm@63353
   854
  have twos: "0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r" for y r :: rat
hoelzl@51523
   855
    apply (simp add: divide_less_eq)
haftmann@57512
   856
    apply (subst mult.commute)
hoelzl@51523
   857
    apply (frule_tac y=y in ex_less_of_nat_mult)
hoelzl@51523
   858
    apply clarify
hoelzl@51523
   859
    apply (rule_tac x=n in exI)
hoelzl@51523
   860
    apply (erule less_trans)
hoelzl@51523
   861
    apply (rule mult_strict_right_mono)
wenzelm@63494
   862
     apply (rule le_less_trans [OF _ of_nat_less_two_power])
wenzelm@63494
   863
     apply simp
hoelzl@51523
   864
    apply assumption
hoelzl@51523
   865
    done
hoelzl@51523
   866
wenzelm@63494
   867
  have PA: "\<not> P (A n)" for n
wenzelm@63494
   868
    by (induct n) (simp_all add: a)
wenzelm@63494
   869
  have PB: "P (B n)" for n
wenzelm@63494
   870
    by (induct n) (simp_all add: b)
hoelzl@51523
   871
  have ab: "a < b"
hoelzl@51523
   872
    using a b unfolding P_def
hoelzl@51523
   873
    apply (clarsimp simp add: not_le)
hoelzl@51523
   874
    apply (drule (1) bspec)
hoelzl@51523
   875
    apply (drule (1) less_le_trans)
hoelzl@51523
   876
    apply (simp add: of_rat_less)
hoelzl@51523
   877
    done
wenzelm@63494
   878
  have AB: "A n < B n" for n
wenzelm@63494
   879
    by (induct n) (simp_all add: ab C_def avg_def)
hoelzl@51523
   880
  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
hoelzl@51523
   881
    apply (auto simp add: le_less [where 'a=nat])
hoelzl@51523
   882
    apply (erule less_Suc_induct)
wenzelm@63494
   883
     apply (clarsimp simp add: C_def avg_def)
wenzelm@63494
   884
     apply (simp add: add_divide_distrib [symmetric])
wenzelm@63494
   885
     apply (rule AB [THEN less_imp_le])
hoelzl@51523
   886
    apply simp
hoelzl@51523
   887
    done
hoelzl@51523
   888
  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
hoelzl@51523
   889
    apply (auto simp add: le_less [where 'a=nat])
hoelzl@51523
   890
    apply (erule less_Suc_induct)
wenzelm@63494
   891
     apply (clarsimp simp add: C_def avg_def)
wenzelm@63494
   892
     apply (simp add: add_divide_distrib [symmetric])
wenzelm@63494
   893
     apply (rule AB [THEN less_imp_le])
hoelzl@51523
   894
    apply simp
hoelzl@51523
   895
    done
wenzelm@63353
   896
  have cauchy_lemma: "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
hoelzl@51523
   897
    apply (rule cauchyI)
hoelzl@51523
   898
    apply (drule twos [where y="b - a"])
hoelzl@51523
   899
    apply (erule exE)
hoelzl@51523
   900
    apply (rule_tac x=n in exI, clarify, rename_tac i j)
hoelzl@51523
   901
    apply (rule_tac y="B n - A n" in le_less_trans) defer
wenzelm@63494
   902
     apply (simp add: width)
hoelzl@51523
   903
    apply (drule_tac x=n in spec)
hoelzl@51523
   904
    apply (frule_tac x=i in spec, drule (1) mp)
hoelzl@51523
   905
    apply (frule_tac x=j in spec, drule (1) mp)
hoelzl@51523
   906
    apply (frule A_mono, drule B_mono)
hoelzl@51523
   907
    apply (frule A_mono, drule B_mono)
hoelzl@51523
   908
    apply arith
hoelzl@51523
   909
    done
hoelzl@51523
   910
  have "cauchy A"
hoelzl@51523
   911
    apply (rule cauchy_lemma [rule_format])
hoelzl@51523
   912
    apply (simp add: A_mono)
hoelzl@51523
   913
    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
hoelzl@51523
   914
    done
hoelzl@51523
   915
  have "cauchy B"
hoelzl@51523
   916
    apply (rule cauchy_lemma [rule_format])
hoelzl@51523
   917
    apply (simp add: B_mono)
hoelzl@51523
   918
    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
hoelzl@51523
   919
    done
hoelzl@51523
   920
  have 1: "\<forall>x\<in>S. x \<le> Real B"
hoelzl@51523
   921
  proof
wenzelm@63353
   922
    fix x
wenzelm@63353
   923
    assume "x \<in> S"
hoelzl@51523
   924
    then show "x \<le> Real B"
wenzelm@60758
   925
      using PB [unfolded P_def] \<open>cauchy B\<close>
hoelzl@51523
   926
      by (simp add: le_RealI)
hoelzl@51523
   927
  qed
hoelzl@51523
   928
  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
hoelzl@51523
   929
    apply clarify
hoelzl@51523
   930
    apply (erule contrapos_pp)
hoelzl@51523
   931
    apply (simp add: not_le)
wenzelm@63494
   932
    apply (drule less_RealD [OF \<open>cauchy A\<close>])
wenzelm@63494
   933
    apply clarify
hoelzl@51523
   934
    apply (subgoal_tac "\<not> P (A n)")
wenzelm@63494
   935
     apply (simp add: P_def not_le)
wenzelm@63494
   936
     apply clarify
wenzelm@63494
   937
     apply (erule rev_bexI)
wenzelm@63494
   938
     apply (erule (1) less_trans)
hoelzl@51523
   939
    apply (simp add: PA)
hoelzl@51523
   940
    done
hoelzl@51523
   941
  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
hoelzl@51523
   942
  proof (rule vanishesI)
wenzelm@63353
   943
    fix r :: rat
wenzelm@63353
   944
    assume "0 < r"
hoelzl@51523
   945
    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
lp15@61649
   946
      using twos by blast
hoelzl@51523
   947
    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
wenzelm@63353
   948
    proof clarify
wenzelm@63353
   949
      fix n
wenzelm@63353
   950
      assume n: "k \<le> n"
hoelzl@51523
   951
      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
hoelzl@51523
   952
        by simp
hoelzl@51523
   953
      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
nipkow@56544
   954
        using n by (simp add: divide_left_mono)
hoelzl@51523
   955
      also note k
hoelzl@51523
   956
      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
hoelzl@51523
   957
    qed
wenzelm@63353
   958
    then show "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
hoelzl@51523
   959
  qed
wenzelm@63353
   960
  then have 3: "Real B = Real A"
wenzelm@60758
   961
    by (simp add: eq_Real \<open>cauchy A\<close> \<open>cauchy B\<close> width)
hoelzl@51523
   962
  show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
wenzelm@63353
   963
    apply (rule exI [where x = "Real B"])
wenzelm@63353
   964
    using 1 2 3
wenzelm@63353
   965
    apply simp
wenzelm@63353
   966
    done
hoelzl@51523
   967
qed
hoelzl@51523
   968
hoelzl@51775
   969
instantiation real :: linear_continuum
hoelzl@51523
   970
begin
hoelzl@51523
   971
wenzelm@63353
   972
subsection \<open>Supremum of a set of reals\<close>
hoelzl@51523
   973
hoelzl@54281
   974
definition "Sup X = (LEAST z::real. \<forall>x\<in>X. x \<le> z)"
wenzelm@63353
   975
definition "Inf X = - Sup (uminus ` X)" for X :: "real set"
hoelzl@51523
   976
hoelzl@51523
   977
instance
hoelzl@51523
   978
proof
wenzelm@63494
   979
  show Sup_upper: "x \<le> Sup X"
wenzelm@63494
   980
    if "x \<in> X" "bdd_above X"
wenzelm@63494
   981
    for x :: real and X :: "real set"
wenzelm@63353
   982
  proof -
wenzelm@63353
   983
    from that obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
hoelzl@54258
   984
      using complete_real[of X] unfolding bdd_above_def by blast
wenzelm@63494
   985
    then show ?thesis
wenzelm@63494
   986
      unfolding Sup_real_def by (rule LeastI2_order) (auto simp: that)
wenzelm@63353
   987
  qed
wenzelm@63494
   988
  show Sup_least: "Sup X \<le> z"
wenzelm@63494
   989
    if "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
wenzelm@63353
   990
    for z :: real and X :: "real set"
wenzelm@63353
   991
  proof -
wenzelm@63353
   992
    from that obtain s where s: "\<forall>y\<in>X. y \<le> s" "\<And>z. \<forall>y\<in>X. y \<le> z \<Longrightarrow> s \<le> z"
wenzelm@63353
   993
      using complete_real [of X] by blast
hoelzl@51523
   994
    then have "Sup X = s"
lp15@61284
   995
      unfolding Sup_real_def by (best intro: Least_equality)
wenzelm@63353
   996
    also from s z have "\<dots> \<le> z"
hoelzl@51523
   997
      by blast
wenzelm@63353
   998
    finally show ?thesis .
wenzelm@63353
   999
  qed
wenzelm@63494
  1000
  show "Inf X \<le> x" if "x \<in> X" "bdd_below X"
wenzelm@63494
  1001
    for x :: real and X :: "real set"
wenzelm@63353
  1002
    using Sup_upper [of "-x" "uminus ` X"] by (auto simp: Inf_real_def that)
wenzelm@63494
  1003
  show "z \<le> Inf X" if "X \<noteq> {}" "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
wenzelm@63494
  1004
    for z :: real and X :: "real set"
wenzelm@63353
  1005
    using Sup_least [of "uminus ` X" "- z"] by (force simp: Inf_real_def that)
hoelzl@51775
  1006
  show "\<exists>a b::real. a \<noteq> b"
hoelzl@51775
  1007
    using zero_neq_one by blast
hoelzl@51523
  1008
qed
wenzelm@63353
  1009
hoelzl@51523
  1010
end
hoelzl@51523
  1011
wenzelm@63353
  1012
wenzelm@60758
  1013
subsection \<open>Hiding implementation details\<close>
hoelzl@51523
  1014
hoelzl@51523
  1015
hide_const (open) vanishes cauchy positive Real
hoelzl@51523
  1016
hoelzl@51523
  1017
declare Real_induct [induct del]
hoelzl@51523
  1018
declare Abs_real_induct [induct del]
hoelzl@51523
  1019
declare Abs_real_cases [cases del]
hoelzl@51523
  1020
kuncar@53652
  1021
lifting_update real.lifting
kuncar@53652
  1022
lifting_forget real.lifting
lp15@61284
  1023
wenzelm@63353
  1024
wenzelm@63353
  1025
subsection \<open>More Lemmas\<close>
hoelzl@51523
  1026
wenzelm@60758
  1027
text \<open>BH: These lemmas should not be necessary; they should be
wenzelm@63353
  1028
  covered by existing simp rules and simplification procedures.\<close>
hoelzl@51523
  1029
wenzelm@63494
  1030
lemma real_mult_less_iff1 [simp]: "0 < z \<Longrightarrow> x * z < y * z \<longleftrightarrow> x < y"
wenzelm@63494
  1031
  for x y z :: real
wenzelm@63353
  1032
  by simp (* solved by linordered_ring_less_cancel_factor simproc *)
hoelzl@51523
  1033
wenzelm@63494
  1034
lemma real_mult_le_cancel_iff1 [simp]: "0 < z \<Longrightarrow> x * z \<le> y * z \<longleftrightarrow> x \<le> y"
wenzelm@63494
  1035
  for x y z :: real
wenzelm@63353
  1036
  by simp (* solved by linordered_ring_le_cancel_factor simproc *)
hoelzl@51523
  1037
wenzelm@63494
  1038
lemma real_mult_le_cancel_iff2 [simp]: "0 < z \<Longrightarrow> z * x \<le> z * y \<longleftrightarrow> x \<le> y"
wenzelm@63494
  1039
  for x y z :: real
wenzelm@63353
  1040
  by simp (* solved by linordered_ring_le_cancel_factor simproc *)
hoelzl@51523
  1041
hoelzl@51523
  1042
wenzelm@60758
  1043
subsection \<open>Embedding numbers into the Reals\<close>
hoelzl@51523
  1044
wenzelm@63353
  1045
abbreviation real_of_nat :: "nat \<Rightarrow> real"
wenzelm@63353
  1046
  where "real_of_nat \<equiv> of_nat"
hoelzl@51523
  1047
wenzelm@63353
  1048
abbreviation real :: "nat \<Rightarrow> real"
wenzelm@63353
  1049
  where "real \<equiv> of_nat"
lp15@61609
  1050
wenzelm@63353
  1051
abbreviation real_of_int :: "int \<Rightarrow> real"
wenzelm@63353
  1052
  where "real_of_int \<equiv> of_int"
hoelzl@51523
  1053
wenzelm@63353
  1054
abbreviation real_of_rat :: "rat \<Rightarrow> real"
wenzelm@63353
  1055
  where "real_of_rat \<equiv> of_rat"
hoelzl@51523
  1056
hoelzl@51523
  1057
declare [[coercion_enabled]]
hoelzl@59000
  1058
hoelzl@59000
  1059
declare [[coercion "of_nat :: nat \<Rightarrow> int"]]
lp15@61609
  1060
declare [[coercion "of_nat :: nat \<Rightarrow> real"]]
lp15@61609
  1061
declare [[coercion "of_int :: int \<Rightarrow> real"]]
hoelzl@59000
  1062
hoelzl@59000
  1063
(* We do not add rat to the coerced types, this has often unpleasant side effects when writing
hoelzl@59000
  1064
inverse (Suc n) which sometimes gets two coercions: of_rat (inverse (of_nat (Suc n))) *)
hoelzl@51523
  1065
hoelzl@51523
  1066
declare [[coercion_map map]]
hoelzl@59000
  1067
declare [[coercion_map "\<lambda>f g h x. g (h (f x))"]]
hoelzl@59000
  1068
declare [[coercion_map "\<lambda>f g (x,y). (f x, g y)"]]
hoelzl@51523
  1069
lp15@61609
  1070
declare of_int_eq_0_iff [algebra, presburger]
lp15@61649
  1071
declare of_int_eq_1_iff [algebra, presburger]
lp15@61649
  1072
declare of_int_eq_iff [algebra, presburger]
lp15@61649
  1073
declare of_int_less_0_iff [algebra, presburger]
lp15@61649
  1074
declare of_int_less_1_iff [algebra, presburger]
lp15@61649
  1075
declare of_int_less_iff [algebra, presburger]
lp15@61649
  1076
declare of_int_le_0_iff [algebra, presburger]
lp15@61649
  1077
declare of_int_le_1_iff [algebra, presburger]
lp15@61649
  1078
declare of_int_le_iff [algebra, presburger]
lp15@61649
  1079
declare of_int_0_less_iff [algebra, presburger]
lp15@61649
  1080
declare of_int_0_le_iff [algebra, presburger]
lp15@61649
  1081
declare of_int_1_less_iff [algebra, presburger]
lp15@61649
  1082
declare of_int_1_le_iff [algebra, presburger]
hoelzl@51523
  1083
wenzelm@63353
  1084
lemma int_less_real_le: "n < m \<longleftrightarrow> real_of_int n + 1 \<le> real_of_int m"
lp15@61609
  1085
proof -
lp15@61609
  1086
  have "(0::real) \<le> 1"
lp15@61609
  1087
    by (metis less_eq_real_def zero_less_one)
wenzelm@63353
  1088
  then show ?thesis
lp15@61694
  1089
    by (metis floor_of_int less_floor_iff)
lp15@61609
  1090
qed
hoelzl@51523
  1091
wenzelm@63353
  1092
lemma int_le_real_less: "n \<le> m \<longleftrightarrow> real_of_int n < real_of_int m + 1"
lp15@61609
  1093
  by (meson int_less_real_le not_le)
hoelzl@51523
  1094
wenzelm@63353
  1095
lemma real_of_int_div_aux:
wenzelm@63353
  1096
  "(real_of_int x) / (real_of_int d) =
lp15@61609
  1097
    real_of_int (x div d) + (real_of_int (x mod d)) / (real_of_int d)"
hoelzl@51523
  1098
proof -
hoelzl@51523
  1099
  have "x = (x div d) * d + x mod d"
hoelzl@51523
  1100
    by auto
lp15@61609
  1101
  then have "real_of_int x = real_of_int (x div d) * real_of_int d + real_of_int(x mod d)"
lp15@61609
  1102
    by (metis of_int_add of_int_mult)
wenzelm@63353
  1103
  then have "real_of_int x / real_of_int d = \<dots> / real_of_int d"
hoelzl@51523
  1104
    by simp
hoelzl@51523
  1105
  then show ?thesis
hoelzl@51523
  1106
    by (auto simp add: add_divide_distrib algebra_simps)
hoelzl@51523
  1107
qed
hoelzl@51523
  1108
haftmann@58834
  1109
lemma real_of_int_div:
wenzelm@63353
  1110
  "d dvd n \<Longrightarrow> real_of_int (n div d) = real_of_int n / real_of_int d" for d n :: int
haftmann@58834
  1111
  by (simp add: real_of_int_div_aux)
hoelzl@51523
  1112
wenzelm@63353
  1113
lemma real_of_int_div2: "0 \<le> real_of_int n / real_of_int x - real_of_int (n div x)"
wenzelm@63353
  1114
  apply (cases "x = 0")
wenzelm@63494
  1115
   apply simp
wenzelm@63353
  1116
  apply (cases "0 < x")
lp15@61609
  1117
   apply (metis add.left_neutral floor_correct floor_divide_of_int_eq le_diff_eq)
lp15@61609
  1118
  apply (metis add.left_neutral floor_correct floor_divide_of_int_eq le_diff_eq)
lp15@61609
  1119
  done
hoelzl@51523
  1120
wenzelm@63353
  1121
lemma real_of_int_div3: "real_of_int n / real_of_int x - real_of_int (n div x) \<le> 1"
hoelzl@51523
  1122
  apply (simp add: algebra_simps)
hoelzl@51523
  1123
  apply (subst real_of_int_div_aux)
hoelzl@51523
  1124
  apply (auto simp add: divide_le_eq intro: order_less_imp_le)
wenzelm@63353
  1125
  done
hoelzl@51523
  1126
wenzelm@63353
  1127
lemma real_of_int_div4: "real_of_int (n div x) \<le> real_of_int n / real_of_int x"
wenzelm@63353
  1128
  using real_of_int_div2 [of n x] by simp
hoelzl@51523
  1129
hoelzl@51523
  1130
wenzelm@63353
  1131
subsection \<open>Embedding the Naturals into the Reals\<close>
hoelzl@51523
  1132
nipkow@64267
  1133
lemma real_of_card: "real (card A) = sum (\<lambda>x. 1) A"
lp15@61609
  1134
  by simp
hoelzl@51523
  1135
wenzelm@63353
  1136
lemma nat_less_real_le: "n < m \<longleftrightarrow> real n + 1 \<le> real m"
lp15@61609
  1137
  by (metis discrete of_nat_1 of_nat_add of_nat_le_iff)
hoelzl@51523
  1138
wenzelm@63494
  1139
lemma nat_le_real_less: "n \<le> m \<longleftrightarrow> real n < real m + 1"
wenzelm@63494
  1140
  for m n :: nat
lp15@61284
  1141
  by (meson nat_less_real_le not_le)
hoelzl@51523
  1142
wenzelm@63353
  1143
lemma real_of_nat_div_aux: "real x / real d = real (x div d) + real (x mod d) / real d"
hoelzl@51523
  1144
proof -
hoelzl@51523
  1145
  have "x = (x div d) * d + x mod d"
hoelzl@51523
  1146
    by auto
hoelzl@51523
  1147
  then have "real x = real (x div d) * real d + real(x mod d)"
lp15@61609
  1148
    by (metis of_nat_add of_nat_mult)
hoelzl@51523
  1149
  then have "real x / real d = \<dots> / real d"
hoelzl@51523
  1150
    by simp
hoelzl@51523
  1151
  then show ?thesis
hoelzl@51523
  1152
    by (auto simp add: add_divide_distrib algebra_simps)
hoelzl@51523
  1153
qed
hoelzl@51523
  1154
lp15@61609
  1155
lemma real_of_nat_div: "d dvd n \<Longrightarrow> real(n div d) = real n / real d"
wenzelm@63353
  1156
  by (subst real_of_nat_div_aux) (auto simp add: dvd_eq_mod_eq_0 [symmetric])
hoelzl@51523
  1157
wenzelm@63353
  1158
lemma real_of_nat_div2: "0 \<le> real n / real x - real (n div x)" for n x :: nat
wenzelm@63353
  1159
  apply (simp add: algebra_simps)
wenzelm@63353
  1160
  apply (subst real_of_nat_div_aux)
wenzelm@63353
  1161
  apply simp
wenzelm@63353
  1162
  done
hoelzl@51523
  1163
wenzelm@63353
  1164
lemma real_of_nat_div3: "real n / real x - real (n div x) \<le> 1" for n x :: nat
wenzelm@63353
  1165
  apply (cases "x = 0")
wenzelm@63494
  1166
   apply simp
wenzelm@63353
  1167
  apply (simp add: algebra_simps)
wenzelm@63353
  1168
  apply (subst real_of_nat_div_aux)
wenzelm@63353
  1169
  apply simp
wenzelm@63353
  1170
  done
hoelzl@51523
  1171
wenzelm@63353
  1172
lemma real_of_nat_div4: "real (n div x) \<le> real n / real x" for n x :: nat
wenzelm@63353
  1173
  using real_of_nat_div2 [of n x] by simp
wenzelm@63353
  1174
hoelzl@51523
  1175
wenzelm@60758
  1176
subsection \<open>The Archimedean Property of the Reals\<close>
hoelzl@51523
  1177
lp15@62623
  1178
lemma real_arch_inverse: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
lp15@62623
  1179
  using reals_Archimedean[of e] less_trans[of 0 "1 / real n" e for n::nat]
lp15@62623
  1180
  by (auto simp add: field_simps cong: conj_cong simp del: of_nat_Suc)
hoelzl@51523
  1181
wenzelm@63494
  1182
lemma reals_Archimedean3: "0 < x \<Longrightarrow> \<forall>y. \<exists>n. y < real n * x"
wenzelm@63494
  1183
  by (auto intro: ex_less_of_nat_mult)
hoelzl@51523
  1184
lp15@62397
  1185
lemma real_archimedian_rdiv_eq_0:
lp15@62397
  1186
  assumes x0: "x \<ge> 0"
wenzelm@63353
  1187
    and c: "c \<ge> 0"
wenzelm@63353
  1188
    and xc: "\<And>m::nat. m > 0 \<Longrightarrow> real m * x \<le> c"
wenzelm@63353
  1189
  shows "x = 0"
wenzelm@63353
  1190
  by (metis reals_Archimedean3 dual_order.order_iff_strict le0 le_less_trans not_le x0 xc)
lp15@62397
  1191
hoelzl@51523
  1192
wenzelm@63353
  1193
subsection \<open>Rationals\<close>
hoelzl@51523
  1194
wenzelm@63353
  1195
lemma Rats_eq_int_div_int: "\<rat> = {real_of_int i / real_of_int j | i j. j \<noteq> 0}"  (is "_ = ?S")
hoelzl@51523
  1196
proof
hoelzl@51523
  1197
  show "\<rat> \<subseteq> ?S"
hoelzl@51523
  1198
  proof
wenzelm@63353
  1199
    fix x :: real
wenzelm@63353
  1200
    assume "x \<in> \<rat>"
wenzelm@63353
  1201
    then obtain r where "x = of_rat r"
wenzelm@63353
  1202
      unfolding Rats_def ..
wenzelm@63353
  1203
    have "of_rat r \<in> ?S"
wenzelm@63353
  1204
      by (cases r) (auto simp add: of_rat_rat)
wenzelm@63353
  1205
    then show "x \<in> ?S"
wenzelm@63353
  1206
      using \<open>x = of_rat r\<close> by simp
hoelzl@51523
  1207
  qed
hoelzl@51523
  1208
next
hoelzl@51523
  1209
  show "?S \<subseteq> \<rat>"
wenzelm@63353
  1210
  proof (auto simp: Rats_def)
wenzelm@63353
  1211
    fix i j :: int
wenzelm@63353
  1212
    assume "j \<noteq> 0"
wenzelm@63353
  1213
    then have "real_of_int i / real_of_int j = of_rat (Fract i j)"
lp15@61609
  1214
      by (simp add: of_rat_rat)
wenzelm@63353
  1215
    then show "real_of_int i / real_of_int j \<in> range of_rat"
wenzelm@63353
  1216
      by blast
hoelzl@51523
  1217
  qed
hoelzl@51523
  1218
qed
hoelzl@51523
  1219
wenzelm@63353
  1220
lemma Rats_eq_int_div_nat: "\<rat> = { real_of_int i / real n | i n. n \<noteq> 0}"
wenzelm@63353
  1221
proof (auto simp: Rats_eq_int_div_int)
wenzelm@63353
  1222
  fix i j :: int
wenzelm@63353
  1223
  assume "j \<noteq> 0"
wenzelm@63353
  1224
  show "\<exists>(i'::int) (n::nat). real_of_int i / real_of_int j = real_of_int i' / real n \<and> 0 < n"
wenzelm@63353
  1225
  proof (cases "j > 0")
wenzelm@63353
  1226
    case True
wenzelm@63353
  1227
    then have "real_of_int i / real_of_int j = real_of_int i / real (nat j) \<and> 0 < nat j"
wenzelm@63353
  1228
      by simp
wenzelm@63353
  1229
    then show ?thesis by blast
hoelzl@51523
  1230
  next
wenzelm@63353
  1231
    case False
wenzelm@63353
  1232
    with \<open>j \<noteq> 0\<close>
wenzelm@63353
  1233
    have "real_of_int i / real_of_int j = real_of_int (- i) / real (nat (- j)) \<and> 0 < nat (- j)"
wenzelm@63353
  1234
      by simp
wenzelm@63353
  1235
    then show ?thesis by blast
hoelzl@51523
  1236
  qed
hoelzl@51523
  1237
next
wenzelm@63353
  1238
  fix i :: int and n :: nat
wenzelm@63353
  1239
  assume "0 < n"
wenzelm@63353
  1240
  then have "real_of_int i / real n = real_of_int i / real_of_int(int n) \<and> int n \<noteq> 0"
wenzelm@63353
  1241
    by simp
wenzelm@63353
  1242
  then show "\<exists>i' j. real_of_int i / real n = real_of_int i' / real_of_int j \<and> j \<noteq> 0"
wenzelm@63353
  1243
    by blast
hoelzl@51523
  1244
qed
hoelzl@51523
  1245
hoelzl@51523
  1246
lemma Rats_abs_nat_div_natE:
hoelzl@51523
  1247
  assumes "x \<in> \<rat>"
haftmann@67051
  1248
  obtains m n :: nat where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "coprime m n"
hoelzl@51523
  1249
proof -
wenzelm@63353
  1250
  from \<open>x \<in> \<rat>\<close> obtain i :: int and n :: nat where "n \<noteq> 0" and "x = real_of_int i / real n"
wenzelm@63353
  1251
    by (auto simp add: Rats_eq_int_div_nat)
wenzelm@63353
  1252
  then have "\<bar>x\<bar> = real (nat \<bar>i\<bar>) / real n" by simp
hoelzl@51523
  1253
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
hoelzl@51523
  1254
  let ?gcd = "gcd m n"
wenzelm@63353
  1255
  from \<open>n \<noteq> 0\<close> have gcd: "?gcd \<noteq> 0" by simp
hoelzl@51523
  1256
  let ?k = "m div ?gcd"
hoelzl@51523
  1257
  let ?l = "n div ?gcd"
hoelzl@51523
  1258
  let ?gcd' = "gcd ?k ?l"
wenzelm@63353
  1259
  have "?gcd dvd m" ..
wenzelm@63353
  1260
  then have gcd_k: "?gcd * ?k = m"
hoelzl@51523
  1261
    by (rule dvd_mult_div_cancel)
wenzelm@63353
  1262
  have "?gcd dvd n" ..
wenzelm@63353
  1263
  then have gcd_l: "?gcd * ?l = n"
hoelzl@51523
  1264
    by (rule dvd_mult_div_cancel)
wenzelm@63353
  1265
  from \<open>n \<noteq> 0\<close> and gcd_l have "?gcd * ?l \<noteq> 0" by simp
lp15@61284
  1266
  then have "?l \<noteq> 0" by (blast dest!: mult_not_zero)
hoelzl@51523
  1267
  moreover
hoelzl@51523
  1268
  have "\<bar>x\<bar> = real ?k / real ?l"
hoelzl@51523
  1269
  proof -
lp15@61609
  1270
    from gcd have "real ?k / real ?l = real (?gcd * ?k) / real (?gcd * ?l)"
lp15@61609
  1271
      by (simp add: real_of_nat_div)
hoelzl@51523
  1272
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
hoelzl@51523
  1273
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
hoelzl@51523
  1274
    finally show ?thesis ..
hoelzl@51523
  1275
  qed
hoelzl@51523
  1276
  moreover
hoelzl@51523
  1277
  have "?gcd' = 1"
hoelzl@51523
  1278
  proof -
hoelzl@51523
  1279
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
hoelzl@51523
  1280
      by (rule gcd_mult_distrib_nat)
hoelzl@51523
  1281
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
hoelzl@51523
  1282
    with gcd show ?thesis by auto
hoelzl@51523
  1283
  qed
haftmann@67051
  1284
  then have "coprime ?k ?l"
haftmann@67051
  1285
    by (simp only: coprime_iff_gcd_eq_1)
hoelzl@51523
  1286
  ultimately show ?thesis ..
hoelzl@51523
  1287
qed
hoelzl@51523
  1288
wenzelm@63353
  1289
wenzelm@63353
  1290
subsection \<open>Density of the Rational Reals in the Reals\<close>
hoelzl@51523
  1291
wenzelm@63353
  1292
text \<open>
wenzelm@63353
  1293
  This density proof is due to Stefan Richter and was ported by TN.  The
wenzelm@63494
  1294
  original source is \<^emph>\<open>Real Analysis\<close> by H.L. Royden.
wenzelm@63353
  1295
  It employs the Archimedean property of the reals.\<close>
hoelzl@51523
  1296
hoelzl@51523
  1297
lemma Rats_dense_in_real:
hoelzl@51523
  1298
  fixes x :: real
wenzelm@63353
  1299
  assumes "x < y"
wenzelm@63353
  1300
  shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
hoelzl@51523
  1301
proof -
wenzelm@63353
  1302
  from \<open>x < y\<close> have "0 < y - x" by simp
wenzelm@63353
  1303
  with reals_Archimedean obtain q :: nat where q: "inverse (real q) < y - x" and "0 < q"
wenzelm@63353
  1304
    by blast
wenzelm@63040
  1305
  define p where "p = \<lceil>y * real q\<rceil> - 1"
wenzelm@63040
  1306
  define r where "r = of_int p / real q"
wenzelm@63494
  1307
  from q have "x < y - inverse (real q)"
wenzelm@63494
  1308
    by simp
wenzelm@63494
  1309
  also from \<open>0 < q\<close> have "y - inverse (real q) \<le> r"
wenzelm@63494
  1310
    by (simp add: r_def p_def le_divide_eq left_diff_distrib)
hoelzl@51523
  1311
  finally have "x < r" .
wenzelm@63494
  1312
  moreover from \<open>0 < q\<close> have "r < y"
wenzelm@63494
  1313
    by (simp add: r_def p_def divide_less_eq diff_less_eq less_ceiling_iff [symmetric])
wenzelm@63494
  1314
  moreover have "r \<in> \<rat>"
wenzelm@63494
  1315
    by (simp add: r_def)
lp15@61649
  1316
  ultimately show ?thesis by blast
hoelzl@51523
  1317
qed
hoelzl@51523
  1318
hoelzl@57447
  1319
lemma of_rat_dense:
hoelzl@57447
  1320
  fixes x y :: real
hoelzl@57447
  1321
  assumes "x < y"
hoelzl@57447
  1322
  shows "\<exists>q :: rat. x < of_rat q \<and> of_rat q < y"
wenzelm@63353
  1323
  using Rats_dense_in_real [OF \<open>x < y\<close>]
wenzelm@63353
  1324
  by (auto elim: Rats_cases)
hoelzl@51523
  1325
hoelzl@51523
  1326
wenzelm@63353
  1327
subsection \<open>Numerals and Arithmetic\<close>
hoelzl@51523
  1328
wenzelm@60758
  1329
declaration \<open>
lp15@61609
  1330
  K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
lp15@61609
  1331
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
lp15@61609
  1332
  #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
lp15@61609
  1333
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
lp15@61609
  1334
  #> Lin_Arith.add_simps [@{thm of_nat_0}, @{thm of_nat_Suc}, @{thm of_nat_add},
lp15@61609
  1335
      @{thm of_nat_mult}, @{thm of_int_0}, @{thm of_int_1},
lp15@61609
  1336
      @{thm of_int_add}, @{thm of_int_minus}, @{thm of_int_diff},
lp15@61609
  1337
      @{thm of_int_mult}, @{thm of_int_of_nat_eq},
haftmann@62348
  1338
      @{thm of_nat_numeral}, @{thm of_nat_numeral}, @{thm of_int_neg_numeral}]
hoelzl@58040
  1339
  #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat \<Rightarrow> real"})
hoelzl@58040
  1340
  #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int \<Rightarrow> real"}))
wenzelm@60758
  1341
\<close>
hoelzl@51523
  1342
wenzelm@63353
  1343
wenzelm@63353
  1344
subsection \<open>Simprules combining \<open>x + y\<close> and \<open>0\<close>\<close> (* FIXME ARE THEY NEEDED? *)
hoelzl@51523
  1345
wenzelm@63494
  1346
lemma real_add_minus_iff [simp]: "x + - a = 0 \<longleftrightarrow> x = a"
wenzelm@63494
  1347
  for x a :: real
wenzelm@63353
  1348
  by arith
hoelzl@51523
  1349
wenzelm@63494
  1350
lemma real_add_less_0_iff: "x + y < 0 \<longleftrightarrow> y < - x"
wenzelm@63494
  1351
  for x y :: real
wenzelm@63353
  1352
  by auto
hoelzl@51523
  1353
wenzelm@63494
  1354
lemma real_0_less_add_iff: "0 < x + y \<longleftrightarrow> - x < y"
wenzelm@63494
  1355
  for x y :: real
wenzelm@63353
  1356
  by auto
hoelzl@51523
  1357
wenzelm@63494
  1358
lemma real_add_le_0_iff: "x + y \<le> 0 \<longleftrightarrow> y \<le> - x"
wenzelm@63494
  1359
  for x y :: real
wenzelm@63353
  1360
  by auto
hoelzl@51523
  1361
wenzelm@63494
  1362
lemma real_0_le_add_iff: "0 \<le> x + y \<longleftrightarrow> - x \<le> y"
wenzelm@63494
  1363
  for x y :: real
wenzelm@63353
  1364
  by auto
wenzelm@63353
  1365
hoelzl@51523
  1366
wenzelm@60758
  1367
subsection \<open>Lemmas about powers\<close>
hoelzl@51523
  1368
hoelzl@51523
  1369
lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
lp15@61609
  1370
  by simp
hoelzl@51523
  1371
wenzelm@63353
  1372
(* FIXME: declare this [simp] for all types, or not at all *)
lp15@61609
  1373
declare sum_squares_eq_zero_iff [simp] sum_power2_eq_zero_iff [simp]
hoelzl@51523
  1374
wenzelm@63494
  1375
lemma real_minus_mult_self_le [simp]: "- (u * u) \<le> x * x"
wenzelm@63494
  1376
  for u x :: real
wenzelm@63353
  1377
  by (rule order_trans [where y = 0]) auto
hoelzl@51523
  1378
wenzelm@63494
  1379
lemma realpow_square_minus_le [simp]: "- u\<^sup>2 \<le> x\<^sup>2"
wenzelm@63494
  1380
  for u x :: real
lp15@61609
  1381
  by (auto simp add: power2_eq_square)
hoelzl@51523
  1382
hoelzl@56889
  1383
wenzelm@63353
  1384
subsection \<open>Density of the Reals\<close>
wenzelm@63353
  1385
wenzelm@63494
  1386
lemma real_lbound_gt_zero: "0 < d1 \<Longrightarrow> 0 < d2 \<Longrightarrow> \<exists>e. 0 < e \<and> e < d1 \<and> e < d2"
wenzelm@63494
  1387
  for d1 d2 :: real
wenzelm@63353
  1388
  by (rule exI [where x = "min d1 d2 / 2"]) (simp add: min_def)
hoelzl@51523
  1389
wenzelm@63353
  1390
text \<open>Similar results are proved in @{theory Fields}\<close>
wenzelm@63494
  1391
lemma real_less_half_sum: "x < y \<Longrightarrow> x < (x + y) / 2"
wenzelm@63494
  1392
  for x y :: real
wenzelm@63353
  1393
  by auto
wenzelm@63353
  1394
wenzelm@63494
  1395
lemma real_gt_half_sum: "x < y \<Longrightarrow> (x + y) / 2 < y"
wenzelm@63494
  1396
  for x y :: real
wenzelm@63353
  1397
  by auto
wenzelm@63353
  1398
wenzelm@63494
  1399
lemma real_sum_of_halves: "x / 2 + x / 2 = x"
wenzelm@63494
  1400
  for x :: real
wenzelm@63353
  1401
  by simp
hoelzl@51523
  1402
hoelzl@51523
  1403
wenzelm@63353
  1404
subsection \<open>Floor and Ceiling Functions from the Reals to the Integers\<close>
hoelzl@51523
  1405
lp15@61609
  1406
(* FIXME: theorems for negative numerals. Many duplicates, e.g. from Archimedean_Field.thy. *)
hoelzl@51523
  1407
wenzelm@63494
  1408
lemma real_of_nat_less_numeral_iff [simp]: "real n < numeral w \<longleftrightarrow> n < numeral w"
wenzelm@63494
  1409
  for n :: nat
lp15@61609
  1410
  by (metis of_nat_less_iff of_nat_numeral)
hoelzl@56889
  1411
wenzelm@63494
  1412
lemma numeral_less_real_of_nat_iff [simp]: "numeral w < real n \<longleftrightarrow> numeral w < n"
wenzelm@63494
  1413
  for n :: nat
lp15@61609
  1414
  by (metis of_nat_less_iff of_nat_numeral)
hoelzl@56889
  1415
wenzelm@63494
  1416
lemma numeral_le_real_of_nat_iff [simp]: "numeral n \<le> real m \<longleftrightarrow> numeral n \<le> m"
wenzelm@63494
  1417
  for m :: nat
wenzelm@63353
  1418
  by (metis not_le real_of_nat_less_numeral_iff)
nipkow@59587
  1419
wenzelm@63353
  1420
lemma of_int_floor_cancel [simp]: "of_int \<lfloor>x\<rfloor> = x \<longleftrightarrow> (\<exists>n::int. x = of_int n)"
lp15@61609
  1421
  by (metis floor_of_int)
hoelzl@51523
  1422
wenzelm@63353
  1423
lemma floor_eq: "real_of_int n < x \<Longrightarrow> x < real_of_int n + 1 \<Longrightarrow> \<lfloor>x\<rfloor> = n"
hoelzl@58040
  1424
  by linarith
hoelzl@51523
  1425
wenzelm@63353
  1426
lemma floor_eq2: "real_of_int n \<le> x \<Longrightarrow> x < real_of_int n + 1 \<Longrightarrow> \<lfloor>x\<rfloor> = n"
haftmann@67051
  1427
  by (fact floor_unique)
hoelzl@51523
  1428
wenzelm@63353
  1429
lemma floor_eq3: "real n < x \<Longrightarrow> x < real (Suc n) \<Longrightarrow> nat \<lfloor>x\<rfloor> = n"
hoelzl@58040
  1430
  by linarith
hoelzl@51523
  1431
wenzelm@63353
  1432
lemma floor_eq4: "real n \<le> x \<Longrightarrow> x < real (Suc n) \<Longrightarrow> nat \<lfloor>x\<rfloor> = n"
hoelzl@58040
  1433
  by linarith
hoelzl@51523
  1434
wenzelm@61942
  1435
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real_of_int \<lfloor>r\<rfloor>"
hoelzl@58040
  1436
  by linarith
hoelzl@51523
  1437
wenzelm@61942
  1438
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real_of_int \<lfloor>r\<rfloor>"
hoelzl@58040
  1439
  by linarith
hoelzl@51523
  1440
wenzelm@61942
  1441
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real_of_int \<lfloor>r\<rfloor> + 1"
hoelzl@58040
  1442
  by linarith
hoelzl@51523
  1443
wenzelm@61942
  1444
lemma real_of_int_floor_add_one_gt [simp]: "r < real_of_int \<lfloor>r\<rfloor> + 1"
hoelzl@58040
  1445
  by linarith
hoelzl@51523
  1446
wenzelm@63353
  1447
lemma floor_divide_real_eq_div:
wenzelm@63353
  1448
  assumes "0 \<le> b"
wenzelm@63353
  1449
  shows "\<lfloor>a / real_of_int b\<rfloor> = \<lfloor>a\<rfloor> div b"
wenzelm@63353
  1450
proof (cases "b = 0")
wenzelm@63353
  1451
  case True
wenzelm@63353
  1452
  then show ?thesis by simp
wenzelm@63353
  1453
next
wenzelm@63353
  1454
  case False
wenzelm@63353
  1455
  with assms have b: "b > 0" by simp
wenzelm@63353
  1456
  have "j = i div b"
wenzelm@63353
  1457
    if "real_of_int i \<le> a" "a < 1 + real_of_int i"
lp15@61609
  1458
      "real_of_int j * real_of_int b \<le> a" "a < real_of_int b + real_of_int j * real_of_int b"
wenzelm@63353
  1459
    for i j :: int
wenzelm@63353
  1460
  proof -
wenzelm@63353
  1461
    from that have "i < b + j * b"
wenzelm@63353
  1462
      by (metis le_less_trans of_int_add of_int_less_iff of_int_mult)
lp15@61609
  1463
    moreover have "j * b < 1 + i"
lp15@61609
  1464
    proof -
lp15@61609
  1465
      have "real_of_int (j * b) < real_of_int i + 1"
wenzelm@61799
  1466
        using \<open>a < 1 + real_of_int i\<close> \<open>real_of_int j * real_of_int b \<le> a\<close> by force
nipkow@63597
  1467
      then show "j * b < 1 + i" by linarith
lp15@61609
  1468
    qed
lp15@61609
  1469
    ultimately have "(j - i div b) * b \<le> i mod b" "i mod b < ((j - i div b) + 1) * b"
hoelzl@58788
  1470
      by (auto simp: field_simps)
hoelzl@58788
  1471
    then have "(j - i div b) * b < 1 * b" "0 * b < ((j - i div b) + 1) * b"
wenzelm@63353
  1472
      using pos_mod_bound [OF b, of i] pos_mod_sign [OF b, of i]
wenzelm@63353
  1473
      by linarith+
nipkow@63597
  1474
    then show ?thesis using b unfolding mult_less_cancel_right by auto
wenzelm@63353
  1475
  qed
nipkow@63597
  1476
  with b show ?thesis by (auto split: floor_split simp: field_simps)
wenzelm@63353
  1477
qed
hoelzl@58788
  1478
nipkow@63601
  1479
lemma floor_one_divide_eq_div_numeral [simp]:
nipkow@63601
  1480
  "\<lfloor>1 / numeral b::real\<rfloor> = 1 div numeral b"
nipkow@63601
  1481
by (metis floor_divide_of_int_eq of_int_1 of_int_numeral)
nipkow@63601
  1482
nipkow@63601
  1483
lemma floor_minus_one_divide_eq_div_numeral [simp]:
nipkow@63601
  1484
  "\<lfloor>- (1 / numeral b)::real\<rfloor> = - 1 div numeral b"
nipkow@63601
  1485
by (metis (mono_tags, hide_lams) div_minus_right minus_divide_right
nipkow@63601
  1486
    floor_divide_of_int_eq of_int_neg_numeral of_int_1)
nipkow@63601
  1487
nipkow@63597
  1488
lemma floor_divide_eq_div_numeral [simp]:
nipkow@63597
  1489
  "\<lfloor>numeral a / numeral b::real\<rfloor> = numeral a div numeral b"
nipkow@63597
  1490
by (metis floor_divide_of_int_eq of_int_numeral)
hoelzl@58097
  1491
wenzelm@63353
  1492
lemma floor_minus_divide_eq_div_numeral [simp]:
wenzelm@63353
  1493
  "\<lfloor>- (numeral a / numeral b)::real\<rfloor> = - numeral a div numeral b"
nipkow@63597
  1494
by (metis divide_minus_left floor_divide_of_int_eq of_int_neg_numeral of_int_numeral)
hoelzl@51523
  1495
wenzelm@63353
  1496
lemma of_int_ceiling_cancel [simp]: "of_int \<lceil>x\<rceil> = x \<longleftrightarrow> (\<exists>n::int. x = of_int n)"
lp15@61609
  1497
  using ceiling_of_int by metis
hoelzl@51523
  1498
wenzelm@63353
  1499
lemma ceiling_eq: "of_int n < x \<Longrightarrow> x \<le> of_int n + 1 \<Longrightarrow> \<lceil>x\<rceil> = n + 1"
lp15@61694
  1500
  by (simp add: ceiling_unique)
hoelzl@51523
  1501
wenzelm@61942
  1502
lemma of_int_ceiling_diff_one_le [simp]: "of_int \<lceil>r\<rceil> - 1 \<le> r"
hoelzl@58040
  1503
  by linarith
hoelzl@51523
  1504
wenzelm@61942
  1505
lemma of_int_ceiling_le_add_one [simp]: "of_int \<lceil>r\<rceil> \<le> r + 1"
hoelzl@58040
  1506
  by linarith
hoelzl@51523
  1507
wenzelm@63353
  1508
lemma ceiling_le: "x \<le> of_int a \<Longrightarrow> \<lceil>x\<rceil> \<le> a"
lp15@61694
  1509
  by (simp add: ceiling_le_iff)
hoelzl@51523
  1510
lp15@61694
  1511
lemma ceiling_divide_eq_div: "\<lceil>of_int a / of_int b\<rceil> = - (- a div b)"
lp15@61609
  1512
  by (metis ceiling_def floor_divide_of_int_eq minus_divide_left of_int_minus)
hoelzl@58097
  1513
hoelzl@58097
  1514
lemma ceiling_divide_eq_div_numeral [simp]:
hoelzl@58097
  1515
  "\<lceil>numeral a / numeral b :: real\<rceil> = - (- numeral a div numeral b)"
hoelzl@58097
  1516
  using ceiling_divide_eq_div[of "numeral a" "numeral b"] by simp
hoelzl@58097
  1517
hoelzl@58097
  1518
lemma ceiling_minus_divide_eq_div_numeral [simp]:
hoelzl@58097
  1519
  "\<lceil>- (numeral a / numeral b :: real)\<rceil> = - (numeral a div numeral b)"
hoelzl@58097
  1520
  using ceiling_divide_eq_div[of "- numeral a" "numeral b"] by simp
hoelzl@51523
  1521
wenzelm@63353
  1522
text \<open>
wenzelm@63353
  1523
  The following lemmas are remnants of the erstwhile functions natfloor
wenzelm@63353
  1524
  and natceiling.
wenzelm@63353
  1525
\<close>
hoelzl@58040
  1526
wenzelm@63494
  1527
lemma nat_floor_neg: "x \<le> 0 \<Longrightarrow> nat \<lfloor>x\<rfloor> = 0"
wenzelm@63494
  1528
  for x :: real
hoelzl@58040
  1529
  by linarith
hoelzl@51523
  1530
wenzelm@63353
  1531
lemma le_nat_floor: "real x \<le> a \<Longrightarrow> x \<le> nat \<lfloor>a\<rfloor>"
hoelzl@58040
  1532
  by linarith
hoelzl@51523
  1533
wenzelm@61942
  1534
lemma le_mult_nat_floor: "nat \<lfloor>a\<rfloor> * nat \<lfloor>b\<rfloor> \<le> nat \<lfloor>a * b\<rfloor>"
wenzelm@63353
  1535
  by (cases "0 \<le> a \<and> 0 \<le> b")
nipkow@59587
  1536
     (auto simp add: nat_mult_distrib[symmetric] nat_mono le_mult_floor)
hoelzl@51523
  1537
wenzelm@63353
  1538
lemma nat_ceiling_le_eq [simp]: "nat \<lceil>x\<rceil> \<le> a \<longleftrightarrow> x \<le> real a"
hoelzl@58040
  1539
  by linarith
hoelzl@51523
  1540
wenzelm@63353
  1541
lemma real_nat_ceiling_ge: "x \<le> real (nat \<lceil>x\<rceil>)"
hoelzl@58040
  1542
  by linarith
hoelzl@51523
  1543
wenzelm@63494
  1544
lemma Rats_no_top_le: "\<exists>q \<in> \<rat>. x \<le> q"
wenzelm@63494
  1545
  for x :: real
wenzelm@61942
  1546
  by (auto intro!: bexI[of _ "of_nat (nat \<lceil>x\<rceil>)"]) linarith
hoelzl@57275
  1547
wenzelm@63353
  1548
lemma Rats_no_bot_less: "\<exists>q \<in> \<rat>. q < x" for x :: real
wenzelm@61942
  1549
  apply (auto intro!: bexI[of _ "of_int (\<lfloor>x\<rfloor> - 1)"])
hoelzl@57447
  1550
  apply (rule less_le_trans[OF _ of_int_floor_le])
hoelzl@57447
  1551
  apply simp
hoelzl@57447
  1552
  done
hoelzl@57447
  1553
wenzelm@63353
  1554
wenzelm@60758
  1555
subsection \<open>Exponentiation with floor\<close>
hoelzl@51523
  1556
hoelzl@51523
  1557
lemma floor_power:
wenzelm@61942
  1558
  assumes "x = of_int \<lfloor>x\<rfloor>"
wenzelm@61942
  1559
  shows "\<lfloor>x ^ n\<rfloor> = \<lfloor>x\<rfloor> ^ n"
hoelzl@51523
  1560
proof -
wenzelm@61942
  1561
  have "x ^ n = of_int (\<lfloor>x\<rfloor> ^ n)"
hoelzl@51523
  1562
    using assms by (induct n arbitrary: x) simp_all
lp15@62626
  1563
  then show ?thesis by (metis floor_of_int)
hoelzl@51523
  1564
qed
lp15@61609
  1565
wenzelm@63353
  1566
lemma floor_numeral_power [simp]: "\<lfloor>numeral x ^ n\<rfloor> = numeral x ^ n"
immler@58983
  1567
  by (metis floor_of_int of_int_numeral of_int_power)
immler@58983
  1568
wenzelm@63353
  1569
lemma ceiling_numeral_power [simp]: "\<lceil>numeral x ^ n\<rceil> = numeral x ^ n"
immler@58983
  1570
  by (metis ceiling_of_int of_int_numeral of_int_power)
immler@58983
  1571
wenzelm@63353
  1572
wenzelm@60758
  1573
subsection \<open>Implementation of rational real numbers\<close>
hoelzl@51523
  1574
wenzelm@60758
  1575
text \<open>Formal constructor\<close>
hoelzl@51523
  1576
wenzelm@63353
  1577
definition Ratreal :: "rat \<Rightarrow> real"
haftmann@66155
  1578
  where [code_abbrev, simp]: "Ratreal = real_of_rat"
hoelzl@51523
  1579
hoelzl@51523
  1580
code_datatype Ratreal
hoelzl@51523
  1581
hoelzl@51523
  1582
haftmann@66155
  1583
text \<open>Quasi-Numerals\<close>
hoelzl@51523
  1584
haftmann@66155
  1585
lemma [code_abbrev]:
haftmann@66155
  1586
  "real_of_rat (numeral k) = numeral k"
haftmann@66155
  1587
  "real_of_rat (- numeral k) = - numeral k"
haftmann@66155
  1588
  "real_of_rat (rat_of_int a) = real_of_int a"
haftmann@66155
  1589
  by simp_all
hoelzl@51523
  1590
hoelzl@51523
  1591
lemma [code_post]:
haftmann@66155
  1592
  "real_of_rat 0 = 0"
haftmann@66155
  1593
  "real_of_rat 1 = 1"
haftmann@66155
  1594
  "real_of_rat (- 1) = - 1"
haftmann@66155
  1595
  "real_of_rat (1 / numeral k) = 1 / numeral k"
haftmann@66155
  1596
  "real_of_rat (numeral k / numeral l) = numeral k / numeral l"
haftmann@66155
  1597
  "real_of_rat (- (1 / numeral k)) = - (1 / numeral k)"
haftmann@66155
  1598
  "real_of_rat (- (numeral k / numeral l)) = - (numeral k / numeral l)"
haftmann@54489
  1599
  by (simp_all add: of_rat_divide of_rat_minus)
hoelzl@51523
  1600
wenzelm@60758
  1601
text \<open>Operations\<close>
hoelzl@51523
  1602
wenzelm@63353
  1603
lemma zero_real_code [code]: "0 = Ratreal 0"
wenzelm@63494
  1604
  by simp
hoelzl@51523
  1605
wenzelm@63353
  1606
lemma one_real_code [code]: "1 = Ratreal 1"
wenzelm@63494
  1607
  by simp
hoelzl@51523
  1608
hoelzl@51523
  1609
instantiation real :: equal
hoelzl@51523
  1610
begin
hoelzl@51523
  1611
wenzelm@63353
  1612
definition "HOL.equal x y \<longleftrightarrow> x - y = 0" for x :: real
hoelzl@51523
  1613
wenzelm@63353
  1614
instance by standard (simp add: equal_real_def)
hoelzl@51523
  1615
wenzelm@63353
  1616
lemma real_equal_code [code]: "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
hoelzl@51523
  1617
  by (simp add: equal_real_def equal)
hoelzl@51523
  1618
wenzelm@63494
  1619
lemma [code nbe]: "HOL.equal x x \<longleftrightarrow> True"
wenzelm@63494
  1620
  for x :: real
hoelzl@51523
  1621
  by (rule equal_refl)
hoelzl@51523
  1622
hoelzl@51523
  1623
end
hoelzl@51523
  1624
hoelzl@51523
  1625
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
hoelzl@51523
  1626
  by (simp add: of_rat_less_eq)
hoelzl@51523
  1627
hoelzl@51523
  1628
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
hoelzl@51523
  1629
  by (simp add: of_rat_less)
hoelzl@51523
  1630
hoelzl@51523
  1631
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
hoelzl@51523
  1632
  by (simp add: of_rat_add)
hoelzl@51523
  1633
hoelzl@51523
  1634
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
hoelzl@51523
  1635
  by (simp add: of_rat_mult)
hoelzl@51523
  1636
hoelzl@51523
  1637
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
hoelzl@51523
  1638
  by (simp add: of_rat_minus)
hoelzl@51523
  1639
hoelzl@51523
  1640
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
hoelzl@51523
  1641
  by (simp add: of_rat_diff)
hoelzl@51523
  1642
hoelzl@51523
  1643
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
hoelzl@51523
  1644
  by (simp add: of_rat_inverse)
lp15@61284
  1645
hoelzl@51523
  1646
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
hoelzl@51523
  1647
  by (simp add: of_rat_divide)
hoelzl@51523
  1648
wenzelm@61942
  1649
lemma real_floor_code [code]: "\<lfloor>Ratreal x\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@63353
  1650
  by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff
wenzelm@63353
  1651
      of_int_floor_le of_rat_of_int_eq real_less_eq_code)
hoelzl@51523
  1652
hoelzl@51523
  1653
wenzelm@60758
  1654
text \<open>Quickcheck\<close>
hoelzl@51523
  1655
hoelzl@51523
  1656
definition (in term_syntax)
wenzelm@63353
  1657
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)"
wenzelm@63353
  1658
  where [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
hoelzl@51523
  1659
hoelzl@51523
  1660
notation fcomp (infixl "\<circ>>" 60)
hoelzl@51523
  1661
notation scomp (infixl "\<circ>\<rightarrow>" 60)
hoelzl@51523
  1662
hoelzl@51523
  1663
instantiation real :: random
hoelzl@51523
  1664
begin
hoelzl@51523
  1665
hoelzl@51523
  1666
definition
hoelzl@51523
  1667
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
hoelzl@51523
  1668
hoelzl@51523
  1669
instance ..
hoelzl@51523
  1670
hoelzl@51523
  1671
end
hoelzl@51523
  1672
hoelzl@51523
  1673
no_notation fcomp (infixl "\<circ>>" 60)
hoelzl@51523
  1674
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
hoelzl@51523
  1675
hoelzl@51523
  1676
instantiation real :: exhaustive
hoelzl@51523
  1677
begin
hoelzl@51523
  1678
hoelzl@51523
  1679
definition
wenzelm@63353
  1680
  "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (\<lambda>r. f (Ratreal r)) d"
hoelzl@51523
  1681
hoelzl@51523
  1682
instance ..
hoelzl@51523
  1683
hoelzl@51523
  1684
end
hoelzl@51523
  1685
hoelzl@51523
  1686
instantiation real :: full_exhaustive
hoelzl@51523
  1687
begin
hoelzl@51523
  1688
hoelzl@51523
  1689
definition
wenzelm@63353
  1690
  "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (\<lambda>r. f (valterm_ratreal r)) d"
hoelzl@51523
  1691
hoelzl@51523
  1692
instance ..
hoelzl@51523
  1693
hoelzl@51523
  1694
end
hoelzl@51523
  1695
hoelzl@51523
  1696
instantiation real :: narrowing
hoelzl@51523
  1697
begin
hoelzl@51523
  1698
hoelzl@51523
  1699
definition
wenzelm@63353
  1700
  "narrowing_real = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
hoelzl@51523
  1701
hoelzl@51523
  1702
instance ..
hoelzl@51523
  1703
hoelzl@51523
  1704
end
hoelzl@51523
  1705
hoelzl@51523
  1706
wenzelm@60758
  1707
subsection \<open>Setup for Nitpick\<close>
hoelzl@51523
  1708
wenzelm@60758
  1709
declaration \<open>
hoelzl@51523
  1710
  Nitpick_HOL.register_frac_type @{type_name real}
blanchet@62079
  1711
    [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
blanchet@62079
  1712
     (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
blanchet@62079
  1713
     (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
blanchet@62079
  1714
     (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
blanchet@62079
  1715
     (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
blanchet@62079
  1716
     (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
blanchet@62079
  1717
     (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
blanchet@62079
  1718
     (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
wenzelm@60758
  1719
\<close>
hoelzl@51523
  1720
hoelzl@51523
  1721
lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
wenzelm@63353
  1722
  ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
wenzelm@63353
  1723
  times_real_inst.times_real uminus_real_inst.uminus_real
wenzelm@63353
  1724
  zero_real_inst.zero_real
hoelzl@51523
  1725
blanchet@56078
  1726
wenzelm@60758
  1727
subsection \<open>Setup for SMT\<close>
blanchet@56078
  1728
blanchet@58061
  1729
ML_file "Tools/SMT/smt_real.ML"
blanchet@58061
  1730
ML_file "Tools/SMT/z3_real.ML"
blanchet@56078
  1731
blanchet@58061
  1732
lemma [z3_rule]:
wenzelm@63353
  1733
  "0 + x = x"
blanchet@56078
  1734
  "x + 0 = x"
blanchet@56078
  1735
  "0 * x = 0"
blanchet@56078
  1736
  "1 * x = x"
blanchet@65885
  1737
  "-x = -1 * x"
blanchet@56078
  1738
  "x + y = y + x"
wenzelm@63353
  1739
  for x y :: real
blanchet@56078
  1740
  by auto
hoelzl@51523
  1741
boehmes@63960
  1742
boehmes@63960
  1743
subsection \<open>Setup for Argo\<close>
boehmes@63960
  1744
boehmes@63960
  1745
ML_file "Tools/Argo/argo_real.ML"
boehmes@63960
  1746
hoelzl@51523
  1747
end