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