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