src/HOL/Real.thy
author hoelzl
Tue, 05 Nov 2013 09:45:02 +0100
changeset 55715 c4159fe6fa46
parent 55714 326fd7103cb4
child 55733 b01057e72233
permissions -rw-r--r--
move Lubs from HOL to HOL-Library (replaced by conditionally complete lattices)
hoelzl@52660
     1
(*  Title:      HOL/Real.thy
hoelzl@52660
     2
    Author:     Jacques D. Fleuriot, University of Edinburgh, 1998
hoelzl@52660
     3
    Author:     Larry Paulson, University of Cambridge
hoelzl@52660
     4
    Author:     Jeremy Avigad, Carnegie Mellon University
hoelzl@52660
     5
    Author:     Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
hoelzl@52660
     6
    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
hoelzl@52660
     7
    Construction of Cauchy Reals by Brian Huffman, 2010
hoelzl@52660
     8
*)
hoelzl@52660
     9
hoelzl@52660
    10
header {* Development of the Reals using Cauchy Sequences *}
hoelzl@52660
    11
hoelzl@52660
    12
theory Real
hoelzl@52910
    13
imports Rat Conditionally_Complete_Lattices
hoelzl@52660
    14
begin
hoelzl@52660
    15
hoelzl@52660
    16
text {*
hoelzl@52660
    17
  This theory contains a formalization of the real numbers as
hoelzl@52660
    18
  equivalence classes of Cauchy sequences of rationals.  See
hoelzl@52660
    19
  @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative
hoelzl@52660
    20
  construction using Dedekind cuts.
hoelzl@52660
    21
*}
hoelzl@52660
    22
hoelzl@52660
    23
subsection {* Preliminary lemmas *}
hoelzl@52660
    24
hoelzl@52660
    25
lemma add_diff_add:
hoelzl@52660
    26
  fixes a b c d :: "'a::ab_group_add"
hoelzl@52660
    27
  shows "(a + c) - (b + d) = (a - b) + (c - d)"
hoelzl@52660
    28
  by simp
hoelzl@52660
    29
hoelzl@52660
    30
lemma minus_diff_minus:
hoelzl@52660
    31
  fixes a b :: "'a::ab_group_add"
hoelzl@52660
    32
  shows "- a - - b = - (a - b)"
hoelzl@52660
    33
  by simp
hoelzl@52660
    34
hoelzl@52660
    35
lemma mult_diff_mult:
hoelzl@52660
    36
  fixes x y a b :: "'a::ring"
hoelzl@52660
    37
  shows "(x * y - a * b) = x * (y - b) + (x - a) * b"
hoelzl@52660
    38
  by (simp add: algebra_simps)
hoelzl@52660
    39
hoelzl@52660
    40
lemma inverse_diff_inverse:
hoelzl@52660
    41
  fixes a b :: "'a::division_ring"
hoelzl@52660
    42
  assumes "a \<noteq> 0" and "b \<noteq> 0"
hoelzl@52660
    43
  shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
hoelzl@52660
    44
  using assms by (simp add: algebra_simps)
hoelzl@52660
    45
hoelzl@52660
    46
lemma obtain_pos_sum:
hoelzl@52660
    47
  fixes r :: rat assumes r: "0 < r"
hoelzl@52660
    48
  obtains s t where "0 < s" and "0 < t" and "r = s + t"
hoelzl@52660
    49
proof
hoelzl@52660
    50
    from r show "0 < r/2" by simp
hoelzl@52660
    51
    from r show "0 < r/2" by simp
hoelzl@52660
    52
    show "r = r/2 + r/2" by simp
hoelzl@52660
    53
qed
hoelzl@52660
    54
hoelzl@52660
    55
subsection {* Sequences that converge to zero *}
hoelzl@52660
    56
hoelzl@52660
    57
definition
hoelzl@52660
    58
  vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
hoelzl@52660
    59
where
hoelzl@52660
    60
  "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
hoelzl@52660
    61
hoelzl@52660
    62
lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
hoelzl@52660
    63
  unfolding vanishes_def by simp
hoelzl@52660
    64
hoelzl@52660
    65
lemma vanishesD: "\<lbrakk>vanishes X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
hoelzl@52660
    66
  unfolding vanishes_def by simp
hoelzl@52660
    67
hoelzl@52660
    68
lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
hoelzl@52660
    69
  unfolding vanishes_def
hoelzl@52660
    70
  apply (cases "c = 0", auto)
hoelzl@52660
    71
  apply (rule exI [where x="\<bar>c\<bar>"], auto)
hoelzl@52660
    72
  done
hoelzl@52660
    73
hoelzl@52660
    74
lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
hoelzl@52660
    75
  unfolding vanishes_def by simp
hoelzl@52660
    76
hoelzl@52660
    77
lemma vanishes_add:
hoelzl@52660
    78
  assumes X: "vanishes X" and Y: "vanishes Y"
hoelzl@52660
    79
  shows "vanishes (\<lambda>n. X n + Y n)"
hoelzl@52660
    80
proof (rule vanishesI)
hoelzl@52660
    81
  fix r :: rat assume "0 < r"
hoelzl@52660
    82
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@52660
    83
    by (rule obtain_pos_sum)
hoelzl@52660
    84
  obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
hoelzl@52660
    85
    using vanishesD [OF X s] ..
hoelzl@52660
    86
  obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
hoelzl@52660
    87
    using vanishesD [OF Y t] ..
hoelzl@52660
    88
  have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
hoelzl@52660
    89
  proof (clarsimp)
hoelzl@52660
    90
    fix n assume n: "i \<le> n" "j \<le> n"
hoelzl@52660
    91
    have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>" by (rule abs_triangle_ineq)
hoelzl@52660
    92
    also have "\<dots> < s + t" by (simp add: add_strict_mono i j n)
hoelzl@52660
    93
    finally show "\<bar>X n + Y n\<bar> < r" unfolding r .
hoelzl@52660
    94
  qed
hoelzl@52660
    95
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
hoelzl@52660
    96
qed
hoelzl@52660
    97
hoelzl@52660
    98
lemma vanishes_diff:
hoelzl@52660
    99
  assumes X: "vanishes X" and Y: "vanishes Y"
hoelzl@52660
   100
  shows "vanishes (\<lambda>n. X n - Y n)"
haftmann@55682
   101
  unfolding diff_conv_add_uminus by (intro vanishes_add vanishes_minus X Y)
hoelzl@52660
   102
hoelzl@52660
   103
lemma vanishes_mult_bounded:
hoelzl@52660
   104
  assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
hoelzl@52660
   105
  assumes Y: "vanishes (\<lambda>n. Y n)"
hoelzl@52660
   106
  shows "vanishes (\<lambda>n. X n * Y n)"
hoelzl@52660
   107
proof (rule vanishesI)
hoelzl@52660
   108
  fix r :: rat assume r: "0 < r"
hoelzl@52660
   109
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
hoelzl@52660
   110
    using X by fast
hoelzl@52660
   111
  obtain b where b: "0 < b" "r = a * b"
hoelzl@52660
   112
  proof
hoelzl@52660
   113
    show "0 < r / a" using r a by (simp add: divide_pos_pos)
hoelzl@52660
   114
    show "r = a * (r / a)" using a by simp
hoelzl@52660
   115
  qed
hoelzl@52660
   116
  obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
hoelzl@52660
   117
    using vanishesD [OF Y b(1)] ..
hoelzl@52660
   118
  have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
hoelzl@52660
   119
    by (simp add: b(2) abs_mult mult_strict_mono' a k)
hoelzl@52660
   120
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
hoelzl@52660
   121
qed
hoelzl@52660
   122
hoelzl@52660
   123
subsection {* Cauchy sequences *}
hoelzl@52660
   124
hoelzl@52660
   125
definition
hoelzl@52660
   126
  cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
hoelzl@52660
   127
where
hoelzl@52660
   128
  "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
hoelzl@52660
   129
hoelzl@52660
   130
lemma cauchyI:
hoelzl@52660
   131
  "(\<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@52660
   132
  unfolding cauchy_def by simp
hoelzl@52660
   133
hoelzl@52660
   134
lemma cauchyD:
hoelzl@52660
   135
  "\<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@52660
   136
  unfolding cauchy_def by simp
hoelzl@52660
   137
hoelzl@52660
   138
lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
hoelzl@52660
   139
  unfolding cauchy_def by simp
hoelzl@52660
   140
hoelzl@52660
   141
lemma cauchy_add [simp]:
hoelzl@52660
   142
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   143
  shows "cauchy (\<lambda>n. X n + Y n)"
hoelzl@52660
   144
proof (rule cauchyI)
hoelzl@52660
   145
  fix r :: rat assume "0 < r"
hoelzl@52660
   146
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@52660
   147
    by (rule obtain_pos_sum)
hoelzl@52660
   148
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@52660
   149
    using cauchyD [OF X s] ..
hoelzl@52660
   150
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
hoelzl@52660
   151
    using cauchyD [OF Y t] ..
hoelzl@52660
   152
  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@52660
   153
  proof (clarsimp)
hoelzl@52660
   154
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
hoelzl@52660
   155
    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@52660
   156
      unfolding add_diff_add by (rule abs_triangle_ineq)
hoelzl@52660
   157
    also have "\<dots> < s + t"
hoelzl@52660
   158
      by (rule add_strict_mono, simp_all add: i j *)
hoelzl@52660
   159
    finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" unfolding r .
hoelzl@52660
   160
  qed
hoelzl@52660
   161
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
hoelzl@52660
   162
qed
hoelzl@52660
   163
hoelzl@52660
   164
lemma cauchy_minus [simp]:
hoelzl@52660
   165
  assumes X: "cauchy X"
hoelzl@52660
   166
  shows "cauchy (\<lambda>n. - X n)"
hoelzl@52660
   167
using assms unfolding cauchy_def
hoelzl@52660
   168
unfolding minus_diff_minus abs_minus_cancel .
hoelzl@52660
   169
hoelzl@52660
   170
lemma cauchy_diff [simp]:
hoelzl@52660
   171
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   172
  shows "cauchy (\<lambda>n. X n - Y n)"
haftmann@55682
   173
  using assms unfolding diff_conv_add_uminus by (simp del: add_uminus_conv_diff)
hoelzl@52660
   174
hoelzl@52660
   175
lemma cauchy_imp_bounded:
hoelzl@52660
   176
  assumes "cauchy X" shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
hoelzl@52660
   177
proof -
hoelzl@52660
   178
  obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
hoelzl@52660
   179
    using cauchyD [OF assms zero_less_one] ..
hoelzl@52660
   180
  show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
hoelzl@52660
   181
  proof (intro exI conjI allI)
hoelzl@52660
   182
    have "0 \<le> \<bar>X 0\<bar>" by simp
hoelzl@52660
   183
    also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
hoelzl@52660
   184
    finally have "0 \<le> Max (abs ` X ` {..k})" .
hoelzl@52660
   185
    thus "0 < Max (abs ` X ` {..k}) + 1" by simp
hoelzl@52660
   186
  next
hoelzl@52660
   187
    fix n :: nat
hoelzl@52660
   188
    show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
hoelzl@52660
   189
    proof (rule linorder_le_cases)
hoelzl@52660
   190
      assume "n \<le> k"
hoelzl@52660
   191
      hence "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
hoelzl@52660
   192
      thus "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
hoelzl@52660
   193
    next
hoelzl@52660
   194
      assume "k \<le> n"
hoelzl@52660
   195
      have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
hoelzl@52660
   196
      also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
hoelzl@52660
   197
        by (rule abs_triangle_ineq)
hoelzl@52660
   198
      also have "\<dots> < Max (abs ` X ` {..k}) + 1"
hoelzl@52660
   199
        by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
hoelzl@52660
   200
      finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
hoelzl@52660
   201
    qed
hoelzl@52660
   202
  qed
hoelzl@52660
   203
qed
hoelzl@52660
   204
hoelzl@52660
   205
lemma cauchy_mult [simp]:
hoelzl@52660
   206
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   207
  shows "cauchy (\<lambda>n. X n * Y n)"
hoelzl@52660
   208
proof (rule cauchyI)
hoelzl@52660
   209
  fix r :: rat assume "0 < r"
hoelzl@52660
   210
  then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
hoelzl@52660
   211
    by (rule obtain_pos_sum)
hoelzl@52660
   212
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
hoelzl@52660
   213
    using cauchy_imp_bounded [OF X] by fast
hoelzl@52660
   214
  obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
hoelzl@52660
   215
    using cauchy_imp_bounded [OF Y] by fast
hoelzl@52660
   216
  obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
hoelzl@52660
   217
  proof
hoelzl@52660
   218
    show "0 < v/b" using v b(1) by (rule divide_pos_pos)
hoelzl@52660
   219
    show "0 < u/a" using u a(1) by (rule divide_pos_pos)
hoelzl@52660
   220
    show "r = a * (u/a) + (v/b) * b"
hoelzl@52660
   221
      using a(1) b(1) `r = u + v` by simp
hoelzl@52660
   222
  qed
hoelzl@52660
   223
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@52660
   224
    using cauchyD [OF X s] ..
hoelzl@52660
   225
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
hoelzl@52660
   226
    using cauchyD [OF Y t] ..
hoelzl@52660
   227
  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@52660
   228
  proof (clarsimp)
hoelzl@52660
   229
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
hoelzl@52660
   230
    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@52660
   231
      unfolding mult_diff_mult ..
hoelzl@52660
   232
    also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
hoelzl@52660
   233
      by (rule abs_triangle_ineq)
hoelzl@52660
   234
    also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
hoelzl@52660
   235
      unfolding abs_mult ..
hoelzl@52660
   236
    also have "\<dots> < a * t + s * b"
hoelzl@52660
   237
      by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
hoelzl@52660
   238
    finally show "\<bar>X m * Y m - X n * Y n\<bar> < r" unfolding r .
hoelzl@52660
   239
  qed
hoelzl@52660
   240
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
hoelzl@52660
   241
qed
hoelzl@52660
   242
hoelzl@52660
   243
lemma cauchy_not_vanishes_cases:
hoelzl@52660
   244
  assumes X: "cauchy X"
hoelzl@52660
   245
  assumes nz: "\<not> vanishes X"
hoelzl@52660
   246
  shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
hoelzl@52660
   247
proof -
hoelzl@52660
   248
  obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
hoelzl@52660
   249
    using nz unfolding vanishes_def by (auto simp add: not_less)
hoelzl@52660
   250
  obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
hoelzl@52660
   251
    using `0 < r` by (rule obtain_pos_sum)
hoelzl@52660
   252
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
hoelzl@52660
   253
    using cauchyD [OF X s] ..
hoelzl@52660
   254
  obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
hoelzl@52660
   255
    using r by fast
hoelzl@52660
   256
  have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
hoelzl@52660
   257
    using i `i \<le> k` by auto
hoelzl@52660
   258
  have "X k \<le> - r \<or> r \<le> X k"
hoelzl@52660
   259
    using `r \<le> \<bar>X k\<bar>` by auto
hoelzl@52660
   260
  hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
hoelzl@52660
   261
    unfolding `r = s + t` using k by auto
hoelzl@52660
   262
  hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
hoelzl@52660
   263
  thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
hoelzl@52660
   264
    using t by auto
hoelzl@52660
   265
qed
hoelzl@52660
   266
hoelzl@52660
   267
lemma cauchy_not_vanishes:
hoelzl@52660
   268
  assumes X: "cauchy X"
hoelzl@52660
   269
  assumes nz: "\<not> vanishes X"
hoelzl@52660
   270
  shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
hoelzl@52660
   271
using cauchy_not_vanishes_cases [OF assms]
hoelzl@52660
   272
by clarify (rule exI, erule conjI, rule_tac x=k in exI, auto)
hoelzl@52660
   273
hoelzl@52660
   274
lemma cauchy_inverse [simp]:
hoelzl@52660
   275
  assumes X: "cauchy X"
hoelzl@52660
   276
  assumes nz: "\<not> vanishes X"
hoelzl@52660
   277
  shows "cauchy (\<lambda>n. inverse (X n))"
hoelzl@52660
   278
proof (rule cauchyI)
hoelzl@52660
   279
  fix r :: rat assume "0 < r"
hoelzl@52660
   280
  obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
hoelzl@52660
   281
    using cauchy_not_vanishes [OF X nz] by fast
hoelzl@52660
   282
  from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
hoelzl@52660
   283
  obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
hoelzl@52660
   284
  proof
hoelzl@52660
   285
    show "0 < b * r * b"
hoelzl@52660
   286
      by (simp add: `0 < r` b mult_pos_pos)
hoelzl@52660
   287
    show "r = inverse b * (b * r * b) * inverse b"
hoelzl@52660
   288
      using b by simp
hoelzl@52660
   289
  qed
hoelzl@52660
   290
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
hoelzl@52660
   291
    using cauchyD [OF X s] ..
hoelzl@52660
   292
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
hoelzl@52660
   293
  proof (clarsimp)
hoelzl@52660
   294
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
hoelzl@52660
   295
    have "\<bar>inverse (X m) - inverse (X n)\<bar> =
hoelzl@52660
   296
          inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
hoelzl@52660
   297
      by (simp add: inverse_diff_inverse nz * abs_mult)
hoelzl@52660
   298
    also have "\<dots> < inverse b * s * inverse b"
hoelzl@52660
   299
      by (simp add: mult_strict_mono less_imp_inverse_less
hoelzl@52660
   300
                    mult_pos_pos i j b * s)
hoelzl@52660
   301
    finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" unfolding r .
hoelzl@52660
   302
  qed
hoelzl@52660
   303
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
hoelzl@52660
   304
qed
hoelzl@52660
   305
hoelzl@52660
   306
lemma vanishes_diff_inverse:
hoelzl@52660
   307
  assumes X: "cauchy X" "\<not> vanishes X"
hoelzl@52660
   308
  assumes Y: "cauchy Y" "\<not> vanishes Y"
hoelzl@52660
   309
  assumes XY: "vanishes (\<lambda>n. X n - Y n)"
hoelzl@52660
   310
  shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
hoelzl@52660
   311
proof (rule vanishesI)
hoelzl@52660
   312
  fix r :: rat assume r: "0 < r"
hoelzl@52660
   313
  obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
hoelzl@52660
   314
    using cauchy_not_vanishes [OF X] by fast
hoelzl@52660
   315
  obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
hoelzl@52660
   316
    using cauchy_not_vanishes [OF Y] by fast
hoelzl@52660
   317
  obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
hoelzl@52660
   318
  proof
hoelzl@52660
   319
    show "0 < a * r * b"
hoelzl@52660
   320
      using a r b by (simp add: mult_pos_pos)
hoelzl@52660
   321
    show "inverse a * (a * r * b) * inverse b = r"
hoelzl@52660
   322
      using a r b by simp
hoelzl@52660
   323
  qed
hoelzl@52660
   324
  obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
hoelzl@52660
   325
    using vanishesD [OF XY s] ..
hoelzl@52660
   326
  have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
hoelzl@52660
   327
  proof (clarsimp)
hoelzl@52660
   328
    fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
hoelzl@52660
   329
    have "X n \<noteq> 0" and "Y n \<noteq> 0"
hoelzl@52660
   330
      using i j a b n by auto
hoelzl@52660
   331
    hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
hoelzl@52660
   332
        inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
hoelzl@52660
   333
      by (simp add: inverse_diff_inverse abs_mult)
hoelzl@52660
   334
    also have "\<dots> < inverse a * s * inverse b"
hoelzl@52660
   335
      apply (intro mult_strict_mono' less_imp_inverse_less)
hoelzl@52660
   336
      apply (simp_all add: a b i j k n mult_nonneg_nonneg)
hoelzl@52660
   337
      done
hoelzl@52660
   338
    also note `inverse a * s * inverse b = r`
hoelzl@52660
   339
    finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
hoelzl@52660
   340
  qed
hoelzl@52660
   341
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
hoelzl@52660
   342
qed
hoelzl@52660
   343
hoelzl@52660
   344
subsection {* Equivalence relation on Cauchy sequences *}
hoelzl@52660
   345
hoelzl@52660
   346
definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
hoelzl@52660
   347
  where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
hoelzl@52660
   348
hoelzl@52660
   349
lemma realrelI [intro?]:
hoelzl@52660
   350
  assumes "cauchy X" and "cauchy Y" and "vanishes (\<lambda>n. X n - Y n)"
hoelzl@52660
   351
  shows "realrel X Y"
hoelzl@52660
   352
  using assms unfolding realrel_def by simp
hoelzl@52660
   353
hoelzl@52660
   354
lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
hoelzl@52660
   355
  unfolding realrel_def by simp
hoelzl@52660
   356
hoelzl@52660
   357
lemma symp_realrel: "symp realrel"
hoelzl@52660
   358
  unfolding realrel_def
hoelzl@52660
   359
  by (rule sympI, clarify, drule vanishes_minus, simp)
hoelzl@52660
   360
hoelzl@52660
   361
lemma transp_realrel: "transp realrel"
hoelzl@52660
   362
  unfolding realrel_def
hoelzl@52660
   363
  apply (rule transpI, clarify)
hoelzl@52660
   364
  apply (drule (1) vanishes_add)
hoelzl@52660
   365
  apply (simp add: algebra_simps)
hoelzl@52660
   366
  done
hoelzl@52660
   367
hoelzl@52660
   368
lemma part_equivp_realrel: "part_equivp realrel"
hoelzl@52660
   369
  by (fast intro: part_equivpI symp_realrel transp_realrel
hoelzl@52660
   370
    realrel_refl cauchy_const)
hoelzl@52660
   371
hoelzl@52660
   372
subsection {* The field of real numbers *}
hoelzl@52660
   373
hoelzl@52660
   374
quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
hoelzl@52660
   375
  morphisms rep_real Real
hoelzl@52660
   376
  by (rule part_equivp_realrel)
hoelzl@52660
   377
hoelzl@52660
   378
lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
hoelzl@52660
   379
  unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
hoelzl@52660
   380
hoelzl@52660
   381
lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
hoelzl@52660
   382
  assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)" shows "P x"
hoelzl@52660
   383
proof (induct x)
hoelzl@52660
   384
  case (1 X)
hoelzl@52660
   385
  hence "cauchy X" by (simp add: realrel_def)
hoelzl@52660
   386
  thus "P (Real X)" by (rule assms)
hoelzl@52660
   387
qed
hoelzl@52660
   388
hoelzl@52660
   389
lemma eq_Real:
hoelzl@52660
   390
  "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
hoelzl@52660
   391
  using real.rel_eq_transfer
hoelzl@52660
   392
  unfolding real.pcr_cr_eq cr_real_def fun_rel_def realrel_def by simp
hoelzl@52660
   393
kuncar@53093
   394
lemma Domainp_pcr_real [transfer_domain_rule]: "Domainp pcr_real = cauchy"
kuncar@53093
   395
by (simp add: real.domain_eq realrel_def)
hoelzl@52660
   396
hoelzl@52660
   397
instantiation real :: field_inverse_zero
hoelzl@52660
   398
begin
hoelzl@52660
   399
hoelzl@52660
   400
lift_definition zero_real :: "real" is "\<lambda>n. 0"
hoelzl@52660
   401
  by (simp add: realrel_refl)
hoelzl@52660
   402
hoelzl@52660
   403
lift_definition one_real :: "real" is "\<lambda>n. 1"
hoelzl@52660
   404
  by (simp add: realrel_refl)
hoelzl@52660
   405
hoelzl@52660
   406
lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
hoelzl@52660
   407
  unfolding realrel_def add_diff_add
hoelzl@52660
   408
  by (simp only: cauchy_add vanishes_add simp_thms)
hoelzl@52660
   409
hoelzl@52660
   410
lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
hoelzl@52660
   411
  unfolding realrel_def minus_diff_minus
hoelzl@52660
   412
  by (simp only: cauchy_minus vanishes_minus simp_thms)
hoelzl@52660
   413
hoelzl@52660
   414
lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
hoelzl@52660
   415
  unfolding realrel_def mult_diff_mult
hoelzl@52660
   416
  by (subst (4) mult_commute, simp only: cauchy_mult vanishes_add
hoelzl@52660
   417
    vanishes_mult_bounded cauchy_imp_bounded simp_thms)
hoelzl@52660
   418
hoelzl@52660
   419
lift_definition inverse_real :: "real \<Rightarrow> real"
hoelzl@52660
   420
  is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
hoelzl@52660
   421
proof -
hoelzl@52660
   422
  fix X Y assume "realrel X Y"
hoelzl@52660
   423
  hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
hoelzl@52660
   424
    unfolding realrel_def by simp_all
hoelzl@52660
   425
  have "vanishes X \<longleftrightarrow> vanishes Y"
hoelzl@52660
   426
  proof
hoelzl@52660
   427
    assume "vanishes X"
hoelzl@52660
   428
    from vanishes_diff [OF this XY] show "vanishes Y" by simp
hoelzl@52660
   429
  next
hoelzl@52660
   430
    assume "vanishes Y"
hoelzl@52660
   431
    from vanishes_add [OF this XY] show "vanishes X" by simp
hoelzl@52660
   432
  qed
hoelzl@52660
   433
  thus "?thesis X Y"
hoelzl@52660
   434
    unfolding realrel_def
hoelzl@52660
   435
    by (simp add: vanishes_diff_inverse X Y XY)
hoelzl@52660
   436
qed
hoelzl@52660
   437
hoelzl@52660
   438
definition
hoelzl@52660
   439
  "x - y = (x::real) + - y"
hoelzl@52660
   440
hoelzl@52660
   441
definition
hoelzl@52660
   442
  "x / y = (x::real) * inverse y"
hoelzl@52660
   443
hoelzl@52660
   444
lemma add_Real:
hoelzl@52660
   445
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   446
  shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
hoelzl@52660
   447
  using assms plus_real.transfer
hoelzl@52660
   448
  unfolding cr_real_eq fun_rel_def by simp
hoelzl@52660
   449
hoelzl@52660
   450
lemma minus_Real:
hoelzl@52660
   451
  assumes X: "cauchy X"
hoelzl@52660
   452
  shows "- Real X = Real (\<lambda>n. - X n)"
hoelzl@52660
   453
  using assms uminus_real.transfer
hoelzl@52660
   454
  unfolding cr_real_eq fun_rel_def by simp
hoelzl@52660
   455
hoelzl@52660
   456
lemma diff_Real:
hoelzl@52660
   457
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   458
  shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
haftmann@55682
   459
  unfolding minus_real_def
hoelzl@52660
   460
  by (simp add: minus_Real add_Real X Y)
hoelzl@52660
   461
hoelzl@52660
   462
lemma mult_Real:
hoelzl@52660
   463
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   464
  shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
hoelzl@52660
   465
  using assms times_real.transfer
hoelzl@52660
   466
  unfolding cr_real_eq fun_rel_def by simp
hoelzl@52660
   467
hoelzl@52660
   468
lemma inverse_Real:
hoelzl@52660
   469
  assumes X: "cauchy X"
hoelzl@52660
   470
  shows "inverse (Real X) =
hoelzl@52660
   471
    (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
hoelzl@52660
   472
  using assms inverse_real.transfer zero_real.transfer
hoelzl@52660
   473
  unfolding cr_real_eq fun_rel_def by (simp split: split_if_asm, metis)
hoelzl@52660
   474
hoelzl@52660
   475
instance proof
hoelzl@52660
   476
  fix a b c :: real
hoelzl@52660
   477
  show "a + b = b + a"
hoelzl@52660
   478
    by transfer (simp add: add_ac realrel_def)
hoelzl@52660
   479
  show "(a + b) + c = a + (b + c)"
hoelzl@52660
   480
    by transfer (simp add: add_ac realrel_def)
hoelzl@52660
   481
  show "0 + a = a"
hoelzl@52660
   482
    by transfer (simp add: realrel_def)
hoelzl@52660
   483
  show "- a + a = 0"
hoelzl@52660
   484
    by transfer (simp add: realrel_def)
hoelzl@52660
   485
  show "a - b = a + - b"
hoelzl@52660
   486
    by (rule minus_real_def)
hoelzl@52660
   487
  show "(a * b) * c = a * (b * c)"
hoelzl@52660
   488
    by transfer (simp add: mult_ac realrel_def)
hoelzl@52660
   489
  show "a * b = b * a"
hoelzl@52660
   490
    by transfer (simp add: mult_ac realrel_def)
hoelzl@52660
   491
  show "1 * a = a"
hoelzl@52660
   492
    by transfer (simp add: mult_ac realrel_def)
hoelzl@52660
   493
  show "(a + b) * c = a * c + b * c"
hoelzl@52660
   494
    by transfer (simp add: distrib_right realrel_def)
hoelzl@52660
   495
  show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
hoelzl@52660
   496
    by transfer (simp add: realrel_def)
hoelzl@52660
   497
  show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
hoelzl@52660
   498
    apply transfer
hoelzl@52660
   499
    apply (simp add: realrel_def)
hoelzl@52660
   500
    apply (rule vanishesI)
hoelzl@52660
   501
    apply (frule (1) cauchy_not_vanishes, clarify)
hoelzl@52660
   502
    apply (rule_tac x=k in exI, clarify)
hoelzl@52660
   503
    apply (drule_tac x=n in spec, simp)
hoelzl@52660
   504
    done
hoelzl@52660
   505
  show "a / b = a * inverse b"
hoelzl@52660
   506
    by (rule divide_real_def)
hoelzl@52660
   507
  show "inverse (0::real) = 0"
hoelzl@52660
   508
    by transfer (simp add: realrel_def)
hoelzl@52660
   509
qed
hoelzl@52660
   510
hoelzl@52660
   511
end
hoelzl@52660
   512
hoelzl@52660
   513
subsection {* Positive reals *}
hoelzl@52660
   514
hoelzl@52660
   515
lift_definition positive :: "real \<Rightarrow> bool"
hoelzl@52660
   516
  is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
hoelzl@52660
   517
proof -
hoelzl@52660
   518
  { fix X Y
hoelzl@52660
   519
    assume "realrel X Y"
hoelzl@52660
   520
    hence XY: "vanishes (\<lambda>n. X n - Y n)"
hoelzl@52660
   521
      unfolding realrel_def by simp_all
hoelzl@52660
   522
    assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
hoelzl@52660
   523
    then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
hoelzl@52660
   524
      by fast
hoelzl@52660
   525
    obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@52660
   526
      using `0 < r` by (rule obtain_pos_sum)
hoelzl@52660
   527
    obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
hoelzl@52660
   528
      using vanishesD [OF XY s] ..
hoelzl@52660
   529
    have "\<forall>n\<ge>max i j. t < Y n"
hoelzl@52660
   530
    proof (clarsimp)
hoelzl@52660
   531
      fix n assume n: "i \<le> n" "j \<le> n"
hoelzl@52660
   532
      have "\<bar>X n - Y n\<bar> < s" and "r < X n"
hoelzl@52660
   533
        using i j n by simp_all
hoelzl@52660
   534
      thus "t < Y n" unfolding r by simp
hoelzl@52660
   535
    qed
hoelzl@52660
   536
    hence "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
hoelzl@52660
   537
  } note 1 = this
hoelzl@52660
   538
  fix X Y assume "realrel X Y"
hoelzl@52660
   539
  hence "realrel X Y" and "realrel Y X"
hoelzl@52660
   540
    using symp_realrel unfolding symp_def by auto
hoelzl@52660
   541
  thus "?thesis X Y"
hoelzl@52660
   542
    by (safe elim!: 1)
hoelzl@52660
   543
qed
hoelzl@52660
   544
hoelzl@52660
   545
lemma positive_Real:
hoelzl@52660
   546
  assumes X: "cauchy X"
hoelzl@52660
   547
  shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
hoelzl@52660
   548
  using assms positive.transfer
hoelzl@52660
   549
  unfolding cr_real_eq fun_rel_def by simp
hoelzl@52660
   550
hoelzl@52660
   551
lemma positive_zero: "\<not> positive 0"
hoelzl@52660
   552
  by transfer auto
hoelzl@52660
   553
hoelzl@52660
   554
lemma positive_add:
hoelzl@52660
   555
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
hoelzl@52660
   556
apply transfer
hoelzl@52660
   557
apply (clarify, rename_tac a b i j)
hoelzl@52660
   558
apply (rule_tac x="a + b" in exI, simp)
hoelzl@52660
   559
apply (rule_tac x="max i j" in exI, clarsimp)
hoelzl@52660
   560
apply (simp add: add_strict_mono)
hoelzl@52660
   561
done
hoelzl@52660
   562
hoelzl@52660
   563
lemma positive_mult:
hoelzl@52660
   564
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
hoelzl@52660
   565
apply transfer
hoelzl@52660
   566
apply (clarify, rename_tac a b i j)
hoelzl@52660
   567
apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
hoelzl@52660
   568
apply (rule_tac x="max i j" in exI, clarsimp)
hoelzl@52660
   569
apply (rule mult_strict_mono, auto)
hoelzl@52660
   570
done
hoelzl@52660
   571
hoelzl@52660
   572
lemma positive_minus:
hoelzl@52660
   573
  "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
hoelzl@52660
   574
apply transfer
hoelzl@52660
   575
apply (simp add: realrel_def)
hoelzl@52660
   576
apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
hoelzl@52660
   577
done
hoelzl@52660
   578
hoelzl@52660
   579
instantiation real :: linordered_field_inverse_zero
hoelzl@52660
   580
begin
hoelzl@52660
   581
hoelzl@52660
   582
definition
hoelzl@52660
   583
  "x < y \<longleftrightarrow> positive (y - x)"
hoelzl@52660
   584
hoelzl@52660
   585
definition
hoelzl@52660
   586
  "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
hoelzl@52660
   587
hoelzl@52660
   588
definition
hoelzl@52660
   589
  "abs (a::real) = (if a < 0 then - a else a)"
hoelzl@52660
   590
hoelzl@52660
   591
definition
hoelzl@52660
   592
  "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
hoelzl@52660
   593
hoelzl@52660
   594
instance proof
hoelzl@52660
   595
  fix a b c :: real
hoelzl@52660
   596
  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
hoelzl@52660
   597
    by (rule abs_real_def)
hoelzl@52660
   598
  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
hoelzl@52660
   599
    unfolding less_eq_real_def less_real_def
hoelzl@52660
   600
    by (auto, drule (1) positive_add, simp_all add: positive_zero)
hoelzl@52660
   601
  show "a \<le> a"
hoelzl@52660
   602
    unfolding less_eq_real_def by simp
hoelzl@52660
   603
  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
hoelzl@52660
   604
    unfolding less_eq_real_def less_real_def
hoelzl@52660
   605
    by (auto, drule (1) positive_add, simp add: algebra_simps)
hoelzl@52660
   606
  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
hoelzl@52660
   607
    unfolding less_eq_real_def less_real_def
hoelzl@52660
   608
    by (auto, drule (1) positive_add, simp add: positive_zero)
hoelzl@52660
   609
  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
haftmann@55682
   610
    unfolding less_eq_real_def less_real_def by auto
hoelzl@52660
   611
    (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
hoelzl@52660
   612
    (* Should produce c + b - (c + a) \<equiv> b - a *)
hoelzl@52660
   613
  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
hoelzl@52660
   614
    by (rule sgn_real_def)
hoelzl@52660
   615
  show "a \<le> b \<or> b \<le> a"
hoelzl@52660
   616
    unfolding less_eq_real_def less_real_def
hoelzl@52660
   617
    by (auto dest!: positive_minus)
hoelzl@52660
   618
  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
hoelzl@52660
   619
    unfolding less_real_def
hoelzl@52660
   620
    by (drule (1) positive_mult, simp add: algebra_simps)
hoelzl@52660
   621
qed
hoelzl@52660
   622
hoelzl@52660
   623
end
hoelzl@52660
   624
hoelzl@52660
   625
instantiation real :: distrib_lattice
hoelzl@52660
   626
begin
hoelzl@52660
   627
hoelzl@52660
   628
definition
hoelzl@52660
   629
  "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
hoelzl@52660
   630
hoelzl@52660
   631
definition
hoelzl@52660
   632
  "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
hoelzl@52660
   633
hoelzl@52660
   634
instance proof
hoelzl@52660
   635
qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
hoelzl@52660
   636
hoelzl@52660
   637
end
hoelzl@52660
   638
hoelzl@52660
   639
lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
hoelzl@52660
   640
apply (induct x)
hoelzl@52660
   641
apply (simp add: zero_real_def)
hoelzl@52660
   642
apply (simp add: one_real_def add_Real)
hoelzl@52660
   643
done
hoelzl@52660
   644
hoelzl@52660
   645
lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
hoelzl@52660
   646
apply (cases x rule: int_diff_cases)
hoelzl@52660
   647
apply (simp add: of_nat_Real diff_Real)
hoelzl@52660
   648
done
hoelzl@52660
   649
hoelzl@52660
   650
lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
hoelzl@52660
   651
apply (induct x)
hoelzl@52660
   652
apply (simp add: Fract_of_int_quotient of_rat_divide)
hoelzl@52660
   653
apply (simp add: of_int_Real divide_inverse)
hoelzl@52660
   654
apply (simp add: inverse_Real mult_Real)
hoelzl@52660
   655
done
hoelzl@52660
   656
hoelzl@52660
   657
instance real :: archimedean_field
hoelzl@52660
   658
proof
hoelzl@52660
   659
  fix x :: real
hoelzl@52660
   660
  show "\<exists>z. x \<le> of_int z"
hoelzl@52660
   661
    apply (induct x)
hoelzl@52660
   662
    apply (frule cauchy_imp_bounded, clarify)
hoelzl@52660
   663
    apply (rule_tac x="ceiling b + 1" in exI)
hoelzl@52660
   664
    apply (rule less_imp_le)
hoelzl@52660
   665
    apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
hoelzl@52660
   666
    apply (rule_tac x=1 in exI, simp add: algebra_simps)
hoelzl@52660
   667
    apply (rule_tac x=0 in exI, clarsimp)
hoelzl@52660
   668
    apply (rule le_less_trans [OF abs_ge_self])
hoelzl@52660
   669
    apply (rule less_le_trans [OF _ le_of_int_ceiling])
hoelzl@52660
   670
    apply simp
hoelzl@52660
   671
    done
hoelzl@52660
   672
qed
hoelzl@52660
   673
hoelzl@52660
   674
instantiation real :: floor_ceiling
hoelzl@52660
   675
begin
hoelzl@52660
   676
hoelzl@52660
   677
definition [code del]:
hoelzl@52660
   678
  "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
hoelzl@52660
   679
hoelzl@52660
   680
instance proof
hoelzl@52660
   681
  fix x :: real
hoelzl@52660
   682
  show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
hoelzl@52660
   683
    unfolding floor_real_def using floor_exists1 by (rule theI')
hoelzl@52660
   684
qed
hoelzl@52660
   685
hoelzl@52660
   686
end
hoelzl@52660
   687
hoelzl@52660
   688
subsection {* Completeness *}
hoelzl@52660
   689
hoelzl@52660
   690
lemma not_positive_Real:
hoelzl@52660
   691
  assumes X: "cauchy X"
hoelzl@52660
   692
  shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
hoelzl@52660
   693
unfolding positive_Real [OF X]
hoelzl@52660
   694
apply (auto, unfold not_less)
hoelzl@52660
   695
apply (erule obtain_pos_sum)
hoelzl@52660
   696
apply (drule_tac x=s in spec, simp)
hoelzl@52660
   697
apply (drule_tac r=t in cauchyD [OF X], clarify)
hoelzl@52660
   698
apply (drule_tac x=k in spec, clarsimp)
hoelzl@52660
   699
apply (rule_tac x=n in exI, clarify, rename_tac m)
hoelzl@52660
   700
apply (drule_tac x=m in spec, simp)
hoelzl@52660
   701
apply (drule_tac x=n in spec, simp)
hoelzl@52660
   702
apply (drule spec, drule (1) mp, clarify, rename_tac i)
hoelzl@52660
   703
apply (rule_tac x="max i k" in exI, simp)
hoelzl@52660
   704
done
hoelzl@52660
   705
hoelzl@52660
   706
lemma le_Real:
hoelzl@52660
   707
  assumes X: "cauchy X" and Y: "cauchy Y"
hoelzl@52660
   708
  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
hoelzl@52660
   709
unfolding not_less [symmetric, where 'a=real] less_real_def
hoelzl@52660
   710
apply (simp add: diff_Real not_positive_Real X Y)
hoelzl@52660
   711
apply (simp add: diff_le_eq add_ac)
hoelzl@52660
   712
done
hoelzl@52660
   713
hoelzl@52660
   714
lemma le_RealI:
hoelzl@52660
   715
  assumes Y: "cauchy Y"
hoelzl@52660
   716
  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
hoelzl@52660
   717
proof (induct x)
hoelzl@52660
   718
  fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
hoelzl@52660
   719
  hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
hoelzl@52660
   720
    by (simp add: of_rat_Real le_Real)
hoelzl@52660
   721
  {
hoelzl@52660
   722
    fix r :: rat assume "0 < r"
hoelzl@52660
   723
    then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
hoelzl@52660
   724
      by (rule obtain_pos_sum)
hoelzl@52660
   725
    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
hoelzl@52660
   726
      using cauchyD [OF Y s] ..
hoelzl@52660
   727
    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
hoelzl@52660
   728
      using le [OF t] ..
hoelzl@52660
   729
    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
hoelzl@52660
   730
    proof (clarsimp)
hoelzl@52660
   731
      fix n assume n: "i \<le> n" "j \<le> n"
hoelzl@52660
   732
      have "X n \<le> Y i + t" using n j by simp
hoelzl@52660
   733
      moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
hoelzl@52660
   734
      ultimately show "X n \<le> Y n + r" unfolding r by simp
hoelzl@52660
   735
    qed
hoelzl@52660
   736
    hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
hoelzl@52660
   737
  }
hoelzl@52660
   738
  thus "Real X \<le> Real Y"
hoelzl@52660
   739
    by (simp add: of_rat_Real le_Real X Y)
hoelzl@52660
   740
qed
hoelzl@52660
   741
hoelzl@52660
   742
lemma Real_leI:
hoelzl@52660
   743
  assumes X: "cauchy X"
hoelzl@52660
   744
  assumes le: "\<forall>n. of_rat (X n) \<le> y"
hoelzl@52660
   745
  shows "Real X \<le> y"
hoelzl@52660
   746
proof -
hoelzl@52660
   747
  have "- y \<le> - Real X"
hoelzl@52660
   748
    by (simp add: minus_Real X le_RealI of_rat_minus le)
hoelzl@52660
   749
  thus ?thesis by simp
hoelzl@52660
   750
qed
hoelzl@52660
   751
hoelzl@52660
   752
lemma less_RealD:
hoelzl@52660
   753
  assumes Y: "cauchy Y"
hoelzl@52660
   754
  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
hoelzl@52660
   755
by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
hoelzl@52660
   756
hoelzl@52660
   757
lemma of_nat_less_two_power:
hoelzl@52660
   758
  "of_nat n < (2::'a::linordered_idom) ^ n"
hoelzl@52660
   759
apply (induct n)
hoelzl@52660
   760
apply simp
hoelzl@52660
   761
apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
hoelzl@52660
   762
apply (drule (1) add_le_less_mono, simp)
hoelzl@52660
   763
apply simp
hoelzl@52660
   764
done
hoelzl@52660
   765
hoelzl@52660
   766
lemma complete_real:
hoelzl@52660
   767
  fixes S :: "real set"
hoelzl@52660
   768
  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
hoelzl@52660
   769
  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@52660
   770
proof -
hoelzl@52660
   771
  obtain x where x: "x \<in> S" using assms(1) ..
hoelzl@52660
   772
  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
hoelzl@52660
   773
hoelzl@52660
   774
  def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
hoelzl@52660
   775
  obtain a where a: "\<not> P a"
hoelzl@52660
   776
  proof
hoelzl@52660
   777
    have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
hoelzl@52660
   778
    also have "x - 1 < x" by simp
hoelzl@52660
   779
    finally have "of_int (floor (x - 1)) < x" .
hoelzl@52660
   780
    hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
hoelzl@52660
   781
    then show "\<not> P (of_int (floor (x - 1)))"
hoelzl@52660
   782
      unfolding P_def of_rat_of_int_eq using x by fast
hoelzl@52660
   783
  qed
hoelzl@52660
   784
  obtain b where b: "P b"
hoelzl@52660
   785
  proof
hoelzl@52660
   786
    show "P (of_int (ceiling z))"
hoelzl@52660
   787
    unfolding P_def of_rat_of_int_eq
hoelzl@52660
   788
    proof
hoelzl@52660
   789
      fix y assume "y \<in> S"
hoelzl@52660
   790
      hence "y \<le> z" using z by simp
hoelzl@52660
   791
      also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
hoelzl@52660
   792
      finally show "y \<le> of_int (ceiling z)" .
hoelzl@52660
   793
    qed
hoelzl@52660
   794
  qed
hoelzl@52660
   795
hoelzl@52660
   796
  def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
hoelzl@52660
   797
  def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
hoelzl@52660
   798
  def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
hoelzl@52660
   799
  def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
hoelzl@52660
   800
  def C \<equiv> "\<lambda>n. avg (A n) (B n)"
hoelzl@52660
   801
  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
hoelzl@52660
   802
  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
hoelzl@52660
   803
  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
hoelzl@52660
   804
    unfolding A_def B_def C_def bisect_def split_def by simp
hoelzl@52660
   805
  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
hoelzl@52660
   806
    unfolding A_def B_def C_def bisect_def split_def by simp
hoelzl@52660
   807
hoelzl@52660
   808
  have width: "\<And>n. B n - A n = (b - a) / 2^n"
hoelzl@52660
   809
    apply (simp add: eq_divide_eq)
hoelzl@52660
   810
    apply (induct_tac n, simp)
hoelzl@52660
   811
    apply (simp add: C_def avg_def algebra_simps)
hoelzl@52660
   812
    done
hoelzl@52660
   813
hoelzl@52660
   814
  have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
hoelzl@52660
   815
    apply (simp add: divide_less_eq)
hoelzl@52660
   816
    apply (subst mult_commute)
hoelzl@52660
   817
    apply (frule_tac y=y in ex_less_of_nat_mult)
hoelzl@52660
   818
    apply clarify
hoelzl@52660
   819
    apply (rule_tac x=n in exI)
hoelzl@52660
   820
    apply (erule less_trans)
hoelzl@52660
   821
    apply (rule mult_strict_right_mono)
hoelzl@52660
   822
    apply (rule le_less_trans [OF _ of_nat_less_two_power])
hoelzl@52660
   823
    apply simp
hoelzl@52660
   824
    apply assumption
hoelzl@52660
   825
    done
hoelzl@52660
   826
hoelzl@52660
   827
  have PA: "\<And>n. \<not> P (A n)"
hoelzl@52660
   828
    by (induct_tac n, simp_all add: a)
hoelzl@52660
   829
  have PB: "\<And>n. P (B n)"
hoelzl@52660
   830
    by (induct_tac n, simp_all add: b)
hoelzl@52660
   831
  have ab: "a < b"
hoelzl@52660
   832
    using a b unfolding P_def
hoelzl@52660
   833
    apply (clarsimp simp add: not_le)
hoelzl@52660
   834
    apply (drule (1) bspec)
hoelzl@52660
   835
    apply (drule (1) less_le_trans)
hoelzl@52660
   836
    apply (simp add: of_rat_less)
hoelzl@52660
   837
    done
hoelzl@52660
   838
  have AB: "\<And>n. A n < B n"
hoelzl@52660
   839
    by (induct_tac n, simp add: ab, simp add: C_def avg_def)
hoelzl@52660
   840
  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
hoelzl@52660
   841
    apply (auto simp add: le_less [where 'a=nat])
hoelzl@52660
   842
    apply (erule less_Suc_induct)
hoelzl@52660
   843
    apply (clarsimp simp add: C_def avg_def)
hoelzl@52660
   844
    apply (simp add: add_divide_distrib [symmetric])
hoelzl@52660
   845
    apply (rule AB [THEN less_imp_le])
hoelzl@52660
   846
    apply simp
hoelzl@52660
   847
    done
hoelzl@52660
   848
  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
hoelzl@52660
   849
    apply (auto simp add: le_less [where 'a=nat])
hoelzl@52660
   850
    apply (erule less_Suc_induct)
hoelzl@52660
   851
    apply (clarsimp simp add: C_def avg_def)
hoelzl@52660
   852
    apply (simp add: add_divide_distrib [symmetric])
hoelzl@52660
   853
    apply (rule AB [THEN less_imp_le])
hoelzl@52660
   854
    apply simp
hoelzl@52660
   855
    done
hoelzl@52660
   856
  have cauchy_lemma:
hoelzl@52660
   857
    "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
hoelzl@52660
   858
    apply (rule cauchyI)
hoelzl@52660
   859
    apply (drule twos [where y="b - a"])
hoelzl@52660
   860
    apply (erule exE)
hoelzl@52660
   861
    apply (rule_tac x=n in exI, clarify, rename_tac i j)
hoelzl@52660
   862
    apply (rule_tac y="B n - A n" in le_less_trans) defer
hoelzl@52660
   863
    apply (simp add: width)
hoelzl@52660
   864
    apply (drule_tac x=n in spec)
hoelzl@52660
   865
    apply (frule_tac x=i in spec, drule (1) mp)
hoelzl@52660
   866
    apply (frule_tac x=j in spec, drule (1) mp)
hoelzl@52660
   867
    apply (frule A_mono, drule B_mono)
hoelzl@52660
   868
    apply (frule A_mono, drule B_mono)
hoelzl@52660
   869
    apply arith
hoelzl@52660
   870
    done
hoelzl@52660
   871
  have "cauchy A"
hoelzl@52660
   872
    apply (rule cauchy_lemma [rule_format])
hoelzl@52660
   873
    apply (simp add: A_mono)
hoelzl@52660
   874
    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
hoelzl@52660
   875
    done
hoelzl@52660
   876
  have "cauchy B"
hoelzl@52660
   877
    apply (rule cauchy_lemma [rule_format])
hoelzl@52660
   878
    apply (simp add: B_mono)
hoelzl@52660
   879
    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
hoelzl@52660
   880
    done
hoelzl@52660
   881
  have 1: "\<forall>x\<in>S. x \<le> Real B"
hoelzl@52660
   882
  proof
hoelzl@52660
   883
    fix x assume "x \<in> S"
hoelzl@52660
   884
    then show "x \<le> Real B"
hoelzl@52660
   885
      using PB [unfolded P_def] `cauchy B`
hoelzl@52660
   886
      by (simp add: le_RealI)
hoelzl@52660
   887
  qed
hoelzl@52660
   888
  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
hoelzl@52660
   889
    apply clarify
hoelzl@52660
   890
    apply (erule contrapos_pp)
hoelzl@52660
   891
    apply (simp add: not_le)
hoelzl@52660
   892
    apply (drule less_RealD [OF `cauchy A`], clarify)
hoelzl@52660
   893
    apply (subgoal_tac "\<not> P (A n)")
hoelzl@52660
   894
    apply (simp add: P_def not_le, clarify)
hoelzl@52660
   895
    apply (erule rev_bexI)
hoelzl@52660
   896
    apply (erule (1) less_trans)
hoelzl@52660
   897
    apply (simp add: PA)
hoelzl@52660
   898
    done
hoelzl@52660
   899
  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
hoelzl@52660
   900
  proof (rule vanishesI)
hoelzl@52660
   901
    fix r :: rat assume "0 < r"
hoelzl@52660
   902
    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
hoelzl@52660
   903
      using twos by fast
hoelzl@52660
   904
    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
hoelzl@52660
   905
    proof (clarify)
hoelzl@52660
   906
      fix n assume n: "k \<le> n"
hoelzl@52660
   907
      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
hoelzl@52660
   908
        by simp
hoelzl@52660
   909
      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
hoelzl@52660
   910
        using n by (simp add: divide_left_mono mult_pos_pos)
hoelzl@52660
   911
      also note k
hoelzl@52660
   912
      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
hoelzl@52660
   913
    qed
hoelzl@52660
   914
    thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
hoelzl@52660
   915
  qed
hoelzl@52660
   916
  hence 3: "Real B = Real A"
hoelzl@52660
   917
    by (simp add: eq_Real `cauchy A` `cauchy B` width)
hoelzl@52660
   918
  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@52660
   919
    using 1 2 3 by (rule_tac x="Real B" in exI, simp)
hoelzl@52660
   920
qed
hoelzl@52660
   921
hoelzl@52912
   922
instantiation real :: linear_continuum
hoelzl@52660
   923
begin
hoelzl@52660
   924
hoelzl@52660
   925
subsection{*Supremum of a set of reals*}
hoelzl@52660
   926
hoelzl@52660
   927
definition
hoelzl@52660
   928
  Sup_real_def: "Sup X \<equiv> LEAST z::real. \<forall>x\<in>X. x\<le>z"
hoelzl@52660
   929
hoelzl@52660
   930
definition
hoelzl@52660
   931
  Inf_real_def: "Inf (X::real set) \<equiv> - Sup (uminus ` X)"
hoelzl@52660
   932
hoelzl@52660
   933
instance
hoelzl@52660
   934
proof
hoelzl@55710
   935
  { fix x :: real and X :: "real set"
hoelzl@55710
   936
    assume x: "x \<in> X" "bdd_above X"
hoelzl@52660
   937
    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@55710
   938
      using complete_real[of X] unfolding bdd_above_def by blast
hoelzl@52660
   939
    then show "x \<le> Sup X"
hoelzl@52660
   940
      unfolding Sup_real_def by (rule LeastI2_order) (auto simp: x) }
hoelzl@52660
   941
  note Sup_upper = this
hoelzl@52660
   942
hoelzl@52660
   943
  { fix z :: real and X :: "real set"
hoelzl@52660
   944
    assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> x \<le> z"
hoelzl@52660
   945
    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@52660
   946
      using complete_real[of X] by blast
hoelzl@52660
   947
    then have "Sup X = s"
hoelzl@52660
   948
      unfolding Sup_real_def by (best intro: Least_equality)  
wenzelm@54511
   949
    also from s z have "... \<le> z"
hoelzl@52660
   950
      by blast
hoelzl@52660
   951
    finally show "Sup X \<le> z" . }
hoelzl@52660
   952
  note Sup_least = this
hoelzl@52660
   953
hoelzl@52660
   954
  { fix x z :: real and X :: "real set"
hoelzl@55710
   955
    assume x: "x \<in> X" and [simp]: "bdd_below X"
hoelzl@52660
   956
    have "-x \<le> Sup (uminus ` X)"
hoelzl@55710
   957
      by (rule Sup_upper) (auto simp add: image_iff x)
hoelzl@52660
   958
    then show "Inf X \<le> x" 
hoelzl@52660
   959
      by (auto simp add: Inf_real_def) }
hoelzl@52660
   960
hoelzl@52660
   961
  { fix z :: real and X :: "real set"
hoelzl@52660
   962
    assume x: "X \<noteq> {}" and z: "\<And>x. x \<in> X \<Longrightarrow> z \<le> x"
hoelzl@52660
   963
    have "Sup (uminus ` X) \<le> -z"
hoelzl@52660
   964
      using x z by (force intro: Sup_least)
hoelzl@52660
   965
    then show "z \<le> Inf X" 
hoelzl@52660
   966
        by (auto simp add: Inf_real_def) }
hoelzl@52912
   967
hoelzl@52912
   968
  show "\<exists>a b::real. a \<noteq> b"
hoelzl@52912
   969
    using zero_neq_one by blast
hoelzl@52660
   970
qed
hoelzl@52660
   971
end
hoelzl@52660
   972
hoelzl@52660
   973
hoelzl@52660
   974
subsection {* Hiding implementation details *}
hoelzl@52660
   975
hoelzl@52660
   976
hide_const (open) vanishes cauchy positive Real
hoelzl@52660
   977
hoelzl@52660
   978
declare Real_induct [induct del]
hoelzl@52660
   979
declare Abs_real_induct [induct del]
hoelzl@52660
   980
declare Abs_real_cases [cases del]
hoelzl@52660
   981
kuncar@54789
   982
lifting_update real.lifting
kuncar@54789
   983
lifting_forget real.lifting
kuncar@53093
   984
  
hoelzl@52660
   985
subsection{*More Lemmas*}
hoelzl@52660
   986
hoelzl@52660
   987
text {* BH: These lemmas should not be necessary; they should be
hoelzl@52660
   988
covered by existing simp rules and simplification procedures. *}
hoelzl@52660
   989
hoelzl@52660
   990
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
hoelzl@52660
   991
by simp (* redundant with mult_cancel_left *)
hoelzl@52660
   992
hoelzl@52660
   993
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
hoelzl@52660
   994
by simp (* redundant with mult_cancel_right *)
hoelzl@52660
   995
hoelzl@52660
   996
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
hoelzl@52660
   997
by simp (* solved by linordered_ring_less_cancel_factor simproc *)
hoelzl@52660
   998
hoelzl@52660
   999
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
hoelzl@52660
  1000
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
hoelzl@52660
  1001
hoelzl@52660
  1002
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
hoelzl@52660
  1003
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
hoelzl@52660
  1004
hoelzl@52660
  1005
hoelzl@52660
  1006
subsection {* Embedding numbers into the Reals *}
hoelzl@52660
  1007
hoelzl@52660
  1008
abbreviation
hoelzl@52660
  1009
  real_of_nat :: "nat \<Rightarrow> real"
hoelzl@52660
  1010
where
hoelzl@52660
  1011
  "real_of_nat \<equiv> of_nat"
hoelzl@52660
  1012
hoelzl@52660
  1013
abbreviation
hoelzl@52660
  1014
  real_of_int :: "int \<Rightarrow> real"
hoelzl@52660
  1015
where
hoelzl@52660
  1016
  "real_of_int \<equiv> of_int"
hoelzl@52660
  1017
hoelzl@52660
  1018
abbreviation
hoelzl@52660
  1019
  real_of_rat :: "rat \<Rightarrow> real"
hoelzl@52660
  1020
where
hoelzl@52660
  1021
  "real_of_rat \<equiv> of_rat"
hoelzl@52660
  1022
hoelzl@52660
  1023
consts
hoelzl@52660
  1024
  (*overloaded constant for injecting other types into "real"*)
hoelzl@52660
  1025
  real :: "'a => real"
hoelzl@52660
  1026
hoelzl@52660
  1027
defs (overloaded)
hoelzl@52660
  1028
  real_of_nat_def [code_unfold]: "real == real_of_nat"
hoelzl@52660
  1029
  real_of_int_def [code_unfold]: "real == real_of_int"
hoelzl@52660
  1030
hoelzl@52660
  1031
declare [[coercion_enabled]]
hoelzl@52660
  1032
declare [[coercion "real::nat\<Rightarrow>real"]]
hoelzl@52660
  1033
declare [[coercion "real::int\<Rightarrow>real"]]
hoelzl@52660
  1034
declare [[coercion "int"]]
hoelzl@52660
  1035
hoelzl@52660
  1036
declare [[coercion_map map]]
hoelzl@52660
  1037
declare [[coercion_map "% f g h x. g (h (f x))"]]
hoelzl@52660
  1038
declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
hoelzl@52660
  1039
hoelzl@52660
  1040
lemma real_eq_of_nat: "real = of_nat"
hoelzl@52660
  1041
  unfolding real_of_nat_def ..
hoelzl@52660
  1042
hoelzl@52660
  1043
lemma real_eq_of_int: "real = of_int"
hoelzl@52660
  1044
  unfolding real_of_int_def ..
hoelzl@52660
  1045
hoelzl@52660
  1046
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
hoelzl@52660
  1047
by (simp add: real_of_int_def) 
hoelzl@52660
  1048
hoelzl@52660
  1049
lemma real_of_one [simp]: "real (1::int) = (1::real)"
hoelzl@52660
  1050
by (simp add: real_of_int_def) 
hoelzl@52660
  1051
hoelzl@52660
  1052
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
hoelzl@52660
  1053
by (simp add: real_of_int_def) 
hoelzl@52660
  1054
hoelzl@52660
  1055
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
hoelzl@52660
  1056
by (simp add: real_of_int_def) 
hoelzl@52660
  1057
hoelzl@52660
  1058
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
hoelzl@52660
  1059
by (simp add: real_of_int_def) 
hoelzl@52660
  1060
hoelzl@52660
  1061
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
hoelzl@52660
  1062
by (simp add: real_of_int_def) 
hoelzl@52660
  1063
hoelzl@52660
  1064
lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
hoelzl@52660
  1065
by (simp add: real_of_int_def of_int_power)
hoelzl@52660
  1066
hoelzl@52660
  1067
lemmas power_real_of_int = real_of_int_power [symmetric]
hoelzl@52660
  1068
hoelzl@52660
  1069
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
hoelzl@52660
  1070
  apply (subst real_eq_of_int)+
hoelzl@52660
  1071
  apply (rule of_int_setsum)
hoelzl@52660
  1072
done
hoelzl@52660
  1073
hoelzl@52660
  1074
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
hoelzl@52660
  1075
    (PROD x:A. real(f x))"
hoelzl@52660
  1076
  apply (subst real_eq_of_int)+
hoelzl@52660
  1077
  apply (rule of_int_setprod)
hoelzl@52660
  1078
done
hoelzl@52660
  1079
hoelzl@52660
  1080
lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
hoelzl@52660
  1081
by (simp add: real_of_int_def) 
hoelzl@52660
  1082
hoelzl@52660
  1083
lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
hoelzl@52660
  1084
by (simp add: real_of_int_def) 
hoelzl@52660
  1085
hoelzl@52660
  1086
lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
hoelzl@52660
  1087
by (simp add: real_of_int_def) 
hoelzl@52660
  1088
hoelzl@52660
  1089
lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
hoelzl@52660
  1090
by (simp add: real_of_int_def) 
hoelzl@52660
  1091
hoelzl@52660
  1092
lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
hoelzl@52660
  1093
by (simp add: real_of_int_def) 
hoelzl@52660
  1094
hoelzl@52660
  1095
lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
hoelzl@52660
  1096
by (simp add: real_of_int_def) 
hoelzl@52660
  1097
hoelzl@52660
  1098
lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
hoelzl@52660
  1099
by (simp add: real_of_int_def)
hoelzl@52660
  1100
hoelzl@52660
  1101
lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
hoelzl@52660
  1102
by (simp add: real_of_int_def)
hoelzl@52660
  1103
hoelzl@52660
  1104
lemma one_less_real_of_int_cancel_iff: "1 < real (i :: int) \<longleftrightarrow> 1 < i"
hoelzl@52660
  1105
  unfolding real_of_one[symmetric] real_of_int_less_iff ..
hoelzl@52660
  1106
hoelzl@52660
  1107
lemma one_le_real_of_int_cancel_iff: "1 \<le> real (i :: int) \<longleftrightarrow> 1 \<le> i"
hoelzl@52660
  1108
  unfolding real_of_one[symmetric] real_of_int_le_iff ..
hoelzl@52660
  1109
hoelzl@52660
  1110
lemma real_of_int_less_one_cancel_iff: "real (i :: int) < 1 \<longleftrightarrow> i < 1"
hoelzl@52660
  1111
  unfolding real_of_one[symmetric] real_of_int_less_iff ..
hoelzl@52660
  1112
hoelzl@52660
  1113
lemma real_of_int_le_one_cancel_iff: "real (i :: int) \<le> 1 \<longleftrightarrow> i \<le> 1"
hoelzl@52660
  1114
  unfolding real_of_one[symmetric] real_of_int_le_iff ..
hoelzl@52660
  1115
hoelzl@52660
  1116
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
hoelzl@52660
  1117
by (auto simp add: abs_if)
hoelzl@52660
  1118
hoelzl@52660
  1119
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
hoelzl@52660
  1120
  apply (subgoal_tac "real n + 1 = real (n + 1)")
hoelzl@52660
  1121
  apply (simp del: real_of_int_add)
hoelzl@52660
  1122
  apply auto
hoelzl@52660
  1123
done
hoelzl@52660
  1124
hoelzl@52660
  1125
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
hoelzl@52660
  1126
  apply (subgoal_tac "real m + 1 = real (m + 1)")
hoelzl@52660
  1127
  apply (simp del: real_of_int_add)
hoelzl@52660
  1128
  apply simp
hoelzl@52660
  1129
done
hoelzl@52660
  1130
hoelzl@52660
  1131
lemma real_of_int_div_aux: "(real (x::int)) / (real d) = 
hoelzl@52660
  1132
    real (x div d) + (real (x mod d)) / (real d)"
hoelzl@52660
  1133
proof -
hoelzl@52660
  1134
  have "x = (x div d) * d + x mod d"
hoelzl@52660
  1135
    by auto
hoelzl@52660
  1136
  then have "real x = real (x div d) * real d + real(x mod d)"
hoelzl@52660
  1137
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
hoelzl@52660
  1138
  then have "real x / real d = ... / real d"
hoelzl@52660
  1139
    by simp
hoelzl@52660
  1140
  then show ?thesis
hoelzl@52660
  1141
    by (auto simp add: add_divide_distrib algebra_simps)
hoelzl@52660
  1142
qed
hoelzl@52660
  1143
hoelzl@52660
  1144
lemma real_of_int_div: "(d :: int) dvd n ==>
hoelzl@52660
  1145
    real(n div d) = real n / real d"
hoelzl@52660
  1146
  apply (subst real_of_int_div_aux)
hoelzl@52660
  1147
  apply simp
hoelzl@52660
  1148
  apply (simp add: dvd_eq_mod_eq_0)
hoelzl@52660
  1149
done
hoelzl@52660
  1150
hoelzl@52660
  1151
lemma real_of_int_div2:
hoelzl@52660
  1152
  "0 <= real (n::int) / real (x) - real (n div x)"
hoelzl@52660
  1153
  apply (case_tac "x = 0")
hoelzl@52660
  1154
  apply simp
hoelzl@52660
  1155
  apply (case_tac "0 < x")
hoelzl@52660
  1156
  apply (simp add: algebra_simps)
hoelzl@52660
  1157
  apply (subst real_of_int_div_aux)
hoelzl@52660
  1158
  apply simp
hoelzl@52660
  1159
  apply (subst zero_le_divide_iff)
hoelzl@52660
  1160
  apply auto
hoelzl@52660
  1161
  apply (simp add: algebra_simps)
hoelzl@52660
  1162
  apply (subst real_of_int_div_aux)
hoelzl@52660
  1163
  apply simp
hoelzl@52660
  1164
  apply (subst zero_le_divide_iff)
hoelzl@52660
  1165
  apply auto
hoelzl@52660
  1166
done
hoelzl@52660
  1167
hoelzl@52660
  1168
lemma real_of_int_div3:
hoelzl@52660
  1169
  "real (n::int) / real (x) - real (n div x) <= 1"
hoelzl@52660
  1170
  apply (simp add: algebra_simps)
hoelzl@52660
  1171
  apply (subst real_of_int_div_aux)
hoelzl@52660
  1172
  apply (auto simp add: divide_le_eq intro: order_less_imp_le)
hoelzl@52660
  1173
done
hoelzl@52660
  1174
hoelzl@52660
  1175
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
hoelzl@52660
  1176
by (insert real_of_int_div2 [of n x], simp)
hoelzl@52660
  1177
hoelzl@52660
  1178
lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
hoelzl@52660
  1179
unfolding real_of_int_def by (rule Ints_of_int)
hoelzl@52660
  1180
hoelzl@52660
  1181
hoelzl@52660
  1182
subsection{*Embedding the Naturals into the Reals*}
hoelzl@52660
  1183
hoelzl@52660
  1184
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
hoelzl@52660
  1185
by (simp add: real_of_nat_def)
hoelzl@52660
  1186
hoelzl@52660
  1187
lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
hoelzl@52660
  1188
by (simp add: real_of_nat_def)
hoelzl@52660
  1189
hoelzl@52660
  1190
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
hoelzl@52660
  1191
by (simp add: real_of_nat_def)
hoelzl@52660
  1192
hoelzl@52660
  1193
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
hoelzl@52660
  1194
by (simp add: real_of_nat_def)
hoelzl@52660
  1195
hoelzl@52660
  1196
(*Not for addsimps: often the LHS is used to represent a positive natural*)
hoelzl@52660
  1197
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
hoelzl@52660
  1198
by (simp add: real_of_nat_def)
hoelzl@52660
  1199
hoelzl@52660
  1200
lemma real_of_nat_less_iff [iff]: 
hoelzl@52660
  1201
     "(real (n::nat) < real m) = (n < m)"
hoelzl@52660
  1202
by (simp add: real_of_nat_def)
hoelzl@52660
  1203
hoelzl@52660
  1204
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
hoelzl@52660
  1205
by (simp add: real_of_nat_def)
hoelzl@52660
  1206
hoelzl@52660
  1207
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
hoelzl@52660
  1208
by (simp add: real_of_nat_def)
hoelzl@52660
  1209
hoelzl@52660
  1210
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
hoelzl@52660
  1211
by (simp add: real_of_nat_def del: of_nat_Suc)
hoelzl@52660
  1212
hoelzl@52660
  1213
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
hoelzl@52660
  1214
by (simp add: real_of_nat_def of_nat_mult)
hoelzl@52660
  1215
hoelzl@52660
  1216
lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
hoelzl@52660
  1217
by (simp add: real_of_nat_def of_nat_power)
hoelzl@52660
  1218
hoelzl@52660
  1219
lemmas power_real_of_nat = real_of_nat_power [symmetric]
hoelzl@52660
  1220
hoelzl@52660
  1221
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
hoelzl@52660
  1222
    (SUM x:A. real(f x))"
hoelzl@52660
  1223
  apply (subst real_eq_of_nat)+
hoelzl@52660
  1224
  apply (rule of_nat_setsum)
hoelzl@52660
  1225
done
hoelzl@52660
  1226
hoelzl@52660
  1227
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
hoelzl@52660
  1228
    (PROD x:A. real(f x))"
hoelzl@52660
  1229
  apply (subst real_eq_of_nat)+
hoelzl@52660
  1230
  apply (rule of_nat_setprod)
hoelzl@52660
  1231
done
hoelzl@52660
  1232
hoelzl@52660
  1233
lemma real_of_card: "real (card A) = setsum (%x.1) A"
hoelzl@52660
  1234
  apply (subst card_eq_setsum)
hoelzl@52660
  1235
  apply (subst real_of_nat_setsum)
hoelzl@52660
  1236
  apply simp
hoelzl@52660
  1237
done
hoelzl@52660
  1238
hoelzl@52660
  1239
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
hoelzl@52660
  1240
by (simp add: real_of_nat_def)
hoelzl@52660
  1241
hoelzl@52660
  1242
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
hoelzl@52660
  1243
by (simp add: real_of_nat_def)
hoelzl@52660
  1244
hoelzl@52660
  1245
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
hoelzl@52660
  1246
by (simp add: add: real_of_nat_def of_nat_diff)
hoelzl@52660
  1247
hoelzl@52660
  1248
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
hoelzl@52660
  1249
by (auto simp: real_of_nat_def)
hoelzl@52660
  1250
hoelzl@52660
  1251
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
hoelzl@52660
  1252
by (simp add: add: real_of_nat_def)
hoelzl@52660
  1253
hoelzl@52660
  1254
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
hoelzl@52660
  1255
by (simp add: add: real_of_nat_def)
hoelzl@52660
  1256
hoelzl@52660
  1257
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
hoelzl@52660
  1258
  apply (subgoal_tac "real n + 1 = real (Suc n)")
hoelzl@52660
  1259
  apply simp
hoelzl@52660
  1260
  apply (auto simp add: real_of_nat_Suc)
hoelzl@52660
  1261
done
hoelzl@52660
  1262
hoelzl@52660
  1263
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
hoelzl@52660
  1264
  apply (subgoal_tac "real m + 1 = real (Suc m)")
hoelzl@52660
  1265
  apply (simp add: less_Suc_eq_le)
hoelzl@52660
  1266
  apply (simp add: real_of_nat_Suc)
hoelzl@52660
  1267
done
hoelzl@52660
  1268
hoelzl@52660
  1269
lemma real_of_nat_div_aux: "(real (x::nat)) / (real d) = 
hoelzl@52660
  1270
    real (x div d) + (real (x mod d)) / (real d)"
hoelzl@52660
  1271
proof -
hoelzl@52660
  1272
  have "x = (x div d) * d + x mod d"
hoelzl@52660
  1273
    by auto
hoelzl@52660
  1274
  then have "real x = real (x div d) * real d + real(x mod d)"
hoelzl@52660
  1275
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
hoelzl@52660
  1276
  then have "real x / real d = \<dots> / real d"
hoelzl@52660
  1277
    by simp
hoelzl@52660
  1278
  then show ?thesis
hoelzl@52660
  1279
    by (auto simp add: add_divide_distrib algebra_simps)
hoelzl@52660
  1280
qed
hoelzl@52660
  1281
hoelzl@52660
  1282
lemma real_of_nat_div: "(d :: nat) dvd n ==>
hoelzl@52660
  1283
    real(n div d) = real n / real d"
hoelzl@52660
  1284
  by (subst real_of_nat_div_aux)
hoelzl@52660
  1285
    (auto simp add: dvd_eq_mod_eq_0 [symmetric])
hoelzl@52660
  1286
hoelzl@52660
  1287
lemma real_of_nat_div2:
hoelzl@52660
  1288
  "0 <= real (n::nat) / real (x) - real (n div x)"
hoelzl@52660
  1289
apply (simp add: algebra_simps)
hoelzl@52660
  1290
apply (subst real_of_nat_div_aux)
hoelzl@52660
  1291
apply simp
hoelzl@52660
  1292
apply (subst zero_le_divide_iff)
hoelzl@52660
  1293
apply simp
hoelzl@52660
  1294
done
hoelzl@52660
  1295
hoelzl@52660
  1296
lemma real_of_nat_div3:
hoelzl@52660
  1297
  "real (n::nat) / real (x) - real (n div x) <= 1"
hoelzl@52660
  1298
apply(case_tac "x = 0")
hoelzl@52660
  1299
apply (simp)
hoelzl@52660
  1300
apply (simp add: algebra_simps)
hoelzl@52660
  1301
apply (subst real_of_nat_div_aux)
hoelzl@52660
  1302
apply simp
hoelzl@52660
  1303
done
hoelzl@52660
  1304
hoelzl@52660
  1305
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
hoelzl@52660
  1306
by (insert real_of_nat_div2 [of n x], simp)
hoelzl@52660
  1307
hoelzl@52660
  1308
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
hoelzl@52660
  1309
by (simp add: real_of_int_def real_of_nat_def)
hoelzl@52660
  1310
hoelzl@52660
  1311
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
hoelzl@52660
  1312
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
hoelzl@52660
  1313
  apply force
hoelzl@52660
  1314
  apply (simp only: real_of_int_of_nat_eq)
hoelzl@52660
  1315
done
hoelzl@52660
  1316
hoelzl@52660
  1317
lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
hoelzl@52660
  1318
unfolding real_of_nat_def by (rule of_nat_in_Nats)
hoelzl@52660
  1319
hoelzl@52660
  1320
lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
hoelzl@52660
  1321
unfolding real_of_nat_def by (rule Ints_of_nat)
hoelzl@52660
  1322
hoelzl@52660
  1323
subsection {* The Archimedean Property of the Reals *}
hoelzl@52660
  1324
hoelzl@52660
  1325
theorem reals_Archimedean:
hoelzl@52660
  1326
  assumes x_pos: "0 < x"
hoelzl@52660
  1327
  shows "\<exists>n. inverse (real (Suc n)) < x"
hoelzl@52660
  1328
  unfolding real_of_nat_def using x_pos
hoelzl@52660
  1329
  by (rule ex_inverse_of_nat_Suc_less)
hoelzl@52660
  1330
hoelzl@52660
  1331
lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
hoelzl@52660
  1332
  unfolding real_of_nat_def by (rule ex_less_of_nat)
hoelzl@52660
  1333
hoelzl@52660
  1334
lemma reals_Archimedean3:
hoelzl@52660
  1335
  assumes x_greater_zero: "0 < x"
hoelzl@52660
  1336
  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
hoelzl@52660
  1337
  unfolding real_of_nat_def using `0 < x`
hoelzl@52660
  1338
  by (auto intro: ex_less_of_nat_mult)
hoelzl@52660
  1339
hoelzl@52660
  1340
hoelzl@52660
  1341
subsection{* Rationals *}
hoelzl@52660
  1342
hoelzl@52660
  1343
lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
hoelzl@52660
  1344
by (simp add: real_eq_of_nat)
hoelzl@52660
  1345
hoelzl@52660
  1346
hoelzl@52660
  1347
lemma Rats_eq_int_div_int:
hoelzl@52660
  1348
  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
hoelzl@52660
  1349
proof
hoelzl@52660
  1350
  show "\<rat> \<subseteq> ?S"
hoelzl@52660
  1351
  proof
hoelzl@52660
  1352
    fix x::real assume "x : \<rat>"
hoelzl@52660
  1353
    then obtain r where "x = of_rat r" unfolding Rats_def ..
hoelzl@52660
  1354
    have "of_rat r : ?S"
hoelzl@52660
  1355
      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
hoelzl@52660
  1356
    thus "x : ?S" using `x = of_rat r` by simp
hoelzl@52660
  1357
  qed
hoelzl@52660
  1358
next
hoelzl@52660
  1359
  show "?S \<subseteq> \<rat>"
hoelzl@52660
  1360
  proof(auto simp:Rats_def)
hoelzl@52660
  1361
    fix i j :: int assume "j \<noteq> 0"
hoelzl@52660
  1362
    hence "real i / real j = of_rat(Fract i j)"
hoelzl@52660
  1363
      by (simp add:of_rat_rat real_eq_of_int)
hoelzl@52660
  1364
    thus "real i / real j \<in> range of_rat" by blast
hoelzl@52660
  1365
  qed
hoelzl@52660
  1366
qed
hoelzl@52660
  1367
hoelzl@52660
  1368
lemma Rats_eq_int_div_nat:
hoelzl@52660
  1369
  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
hoelzl@52660
  1370
proof(auto simp:Rats_eq_int_div_int)
hoelzl@52660
  1371
  fix i j::int assume "j \<noteq> 0"
hoelzl@52660
  1372
  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
hoelzl@52660
  1373
  proof cases
hoelzl@52660
  1374
    assume "j>0"
hoelzl@52660
  1375
    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
hoelzl@52660
  1376
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
hoelzl@52660
  1377
    thus ?thesis by blast
hoelzl@52660
  1378
  next
hoelzl@52660
  1379
    assume "~ j>0"
hoelzl@52660
  1380
    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
hoelzl@52660
  1381
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
hoelzl@52660
  1382
    thus ?thesis by blast
hoelzl@52660
  1383
  qed
hoelzl@52660
  1384
next
hoelzl@52660
  1385
  fix i::int and n::nat assume "0 < n"
hoelzl@52660
  1386
  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
hoelzl@52660
  1387
  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
hoelzl@52660
  1388
qed
hoelzl@52660
  1389
hoelzl@52660
  1390
lemma Rats_abs_nat_div_natE:
hoelzl@52660
  1391
  assumes "x \<in> \<rat>"
hoelzl@52660
  1392
  obtains m n :: nat
hoelzl@52660
  1393
  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
hoelzl@52660
  1394
proof -
hoelzl@52660
  1395
  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
hoelzl@52660
  1396
    by(auto simp add: Rats_eq_int_div_nat)
hoelzl@52660
  1397
  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
hoelzl@52660
  1398
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
hoelzl@52660
  1399
  let ?gcd = "gcd m n"
hoelzl@52660
  1400
  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
hoelzl@52660
  1401
  let ?k = "m div ?gcd"
hoelzl@52660
  1402
  let ?l = "n div ?gcd"
hoelzl@52660
  1403
  let ?gcd' = "gcd ?k ?l"
hoelzl@52660
  1404
  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
hoelzl@52660
  1405
    by (rule dvd_mult_div_cancel)
hoelzl@52660
  1406
  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
hoelzl@52660
  1407
    by (rule dvd_mult_div_cancel)
hoelzl@52660
  1408
  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
hoelzl@52660
  1409
  moreover
hoelzl@52660
  1410
  have "\<bar>x\<bar> = real ?k / real ?l"
hoelzl@52660
  1411
  proof -
hoelzl@52660
  1412
    from gcd have "real ?k / real ?l =
hoelzl@52660
  1413
        real (?gcd * ?k) / real (?gcd * ?l)" by simp
hoelzl@52660
  1414
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
hoelzl@52660
  1415
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
hoelzl@52660
  1416
    finally show ?thesis ..
hoelzl@52660
  1417
  qed
hoelzl@52660
  1418
  moreover
hoelzl@52660
  1419
  have "?gcd' = 1"
hoelzl@52660
  1420
  proof -
hoelzl@52660
  1421
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
hoelzl@52660
  1422
      by (rule gcd_mult_distrib_nat)
hoelzl@52660
  1423
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
hoelzl@52660
  1424
    with gcd show ?thesis by auto
hoelzl@52660
  1425
  qed
hoelzl@52660
  1426
  ultimately show ?thesis ..
hoelzl@52660
  1427
qed
hoelzl@52660
  1428
hoelzl@52660
  1429
subsection{*Density of the Rational Reals in the Reals*}
hoelzl@52660
  1430
hoelzl@52660
  1431
text{* This density proof is due to Stefan Richter and was ported by TN.  The
hoelzl@52660
  1432
original source is \emph{Real Analysis} by H.L. Royden.
hoelzl@52660
  1433
It employs the Archimedean property of the reals. *}
hoelzl@52660
  1434
hoelzl@52660
  1435
lemma Rats_dense_in_real:
hoelzl@52660
  1436
  fixes x :: real
hoelzl@52660
  1437
  assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
hoelzl@52660
  1438
proof -
hoelzl@52660
  1439
  from `x<y` have "0 < y-x" by simp
hoelzl@52660
  1440
  with reals_Archimedean obtain q::nat 
hoelzl@52660
  1441
    where q: "inverse (real q) < y-x" and "0 < q" by auto
hoelzl@52660
  1442
  def p \<equiv> "ceiling (y * real q) - 1"
hoelzl@52660
  1443
  def r \<equiv> "of_int p / real q"
hoelzl@52660
  1444
  from q have "x < y - inverse (real q)" by simp
hoelzl@52660
  1445
  also have "y - inverse (real q) \<le> r"
hoelzl@52660
  1446
    unfolding r_def p_def
hoelzl@52660
  1447
    by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
hoelzl@52660
  1448
  finally have "x < r" .
hoelzl@52660
  1449
  moreover have "r < y"
hoelzl@52660
  1450
    unfolding r_def p_def
hoelzl@52660
  1451
    by (simp add: divide_less_eq diff_less_eq `0 < q`
hoelzl@52660
  1452
      less_ceiling_iff [symmetric])
hoelzl@52660
  1453
  moreover from r_def have "r \<in> \<rat>" by simp
hoelzl@52660
  1454
  ultimately show ?thesis by fast
hoelzl@52660
  1455
qed
hoelzl@52660
  1456
hoelzl@52660
  1457
hoelzl@52660
  1458
hoelzl@52660
  1459
subsection{*Numerals and Arithmetic*}
hoelzl@52660
  1460
hoelzl@52660
  1461
lemma [code_abbrev]:
hoelzl@52660
  1462
  "real_of_int (numeral k) = numeral k"
hoelzl@52660
  1463
  "real_of_int (neg_numeral k) = neg_numeral k"
hoelzl@52660
  1464
  by simp_all
hoelzl@52660
  1465
hoelzl@52660
  1466
text{*Collapse applications of @{term real} to @{term number_of}*}
hoelzl@52660
  1467
lemma real_numeral [simp]:
hoelzl@52660
  1468
  "real (numeral v :: int) = numeral v"
hoelzl@52660
  1469
  "real (neg_numeral v :: int) = neg_numeral v"
hoelzl@52660
  1470
by (simp_all add: real_of_int_def)
hoelzl@52660
  1471
hoelzl@52660
  1472
lemma real_of_nat_numeral [simp]:
hoelzl@52660
  1473
  "real (numeral v :: nat) = numeral v"
hoelzl@52660
  1474
by (simp add: real_of_nat_def)
hoelzl@52660
  1475
hoelzl@52660
  1476
declaration {*
hoelzl@52660
  1477
  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
hoelzl@52660
  1478
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
hoelzl@52660
  1479
  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
hoelzl@52660
  1480
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
hoelzl@52660
  1481
  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
hoelzl@52660
  1482
      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
hoelzl@52660
  1483
      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
hoelzl@52660
  1484
      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
hoelzl@52660
  1485
      @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
hoelzl@52660
  1486
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
hoelzl@52660
  1487
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
hoelzl@52660
  1488
*}
hoelzl@52660
  1489
hoelzl@52660
  1490
hoelzl@52660
  1491
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
hoelzl@52660
  1492
hoelzl@52660
  1493
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
hoelzl@52660
  1494
by arith
hoelzl@52660
  1495
hoelzl@52660
  1496
text {* FIXME: redundant with @{text add_eq_0_iff} below *}
hoelzl@52660
  1497
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
hoelzl@52660
  1498
by auto
hoelzl@52660
  1499
hoelzl@52660
  1500
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
hoelzl@52660
  1501
by auto
hoelzl@52660
  1502
hoelzl@52660
  1503
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
hoelzl@52660
  1504
by auto
hoelzl@52660
  1505
hoelzl@52660
  1506
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
hoelzl@52660
  1507
by auto
hoelzl@52660
  1508
hoelzl@52660
  1509
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
hoelzl@52660
  1510
by auto
hoelzl@52660
  1511
hoelzl@52660
  1512
subsection {* Lemmas about powers *}
hoelzl@52660
  1513
hoelzl@52660
  1514
text {* FIXME: declare this in Rings.thy or not at all *}
hoelzl@52660
  1515
declare abs_mult_self [simp]
hoelzl@52660
  1516
hoelzl@52660
  1517
(* used by Import/HOL/real.imp *)
hoelzl@52660
  1518
lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
hoelzl@52660
  1519
by simp
hoelzl@52660
  1520
hoelzl@52660
  1521
lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
hoelzl@52660
  1522
apply (induct "n")
hoelzl@52660
  1523
apply (auto simp add: real_of_nat_Suc)
hoelzl@52660
  1524
apply (subst mult_2)
hoelzl@52660
  1525
apply (erule add_less_le_mono)
hoelzl@52660
  1526
apply (rule two_realpow_ge_one)
hoelzl@52660
  1527
done
hoelzl@52660
  1528
hoelzl@52660
  1529
text {* TODO: no longer real-specific; rename and move elsewhere *}
hoelzl@52660
  1530
lemma realpow_Suc_le_self:
hoelzl@52660
  1531
  fixes r :: "'a::linordered_semidom"
hoelzl@52660
  1532
  shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
hoelzl@52660
  1533
by (insert power_decreasing [of 1 "Suc n" r], simp)
hoelzl@52660
  1534
hoelzl@52660
  1535
text {* TODO: no longer real-specific; rename and move elsewhere *}
hoelzl@52660
  1536
lemma realpow_minus_mult:
hoelzl@52660
  1537
  fixes x :: "'a::monoid_mult"
hoelzl@52660
  1538
  shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
hoelzl@52660
  1539
by (simp add: power_commutes split add: nat_diff_split)
hoelzl@52660
  1540
hoelzl@52660
  1541
text {* FIXME: declare this [simp] for all types, or not at all *}
hoelzl@52660
  1542
lemma real_two_squares_add_zero_iff [simp]:
hoelzl@52660
  1543
  "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
hoelzl@52660
  1544
by (rule sum_squares_eq_zero_iff)
hoelzl@52660
  1545
hoelzl@52660
  1546
text {* FIXME: declare this [simp] for all types, or not at all *}
hoelzl@52660
  1547
lemma realpow_two_sum_zero_iff [simp]:
wenzelm@54213
  1548
     "(x\<^sup>2 + y\<^sup>2 = (0::real)) = (x = 0 & y = 0)"
hoelzl@52660
  1549
by (rule sum_power2_eq_zero_iff)
hoelzl@52660
  1550
hoelzl@52660
  1551
lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
hoelzl@52660
  1552
by (rule_tac y = 0 in order_trans, auto)
hoelzl@52660
  1553
wenzelm@54213
  1554
lemma realpow_square_minus_le [simp]: "- u\<^sup>2 \<le> (x::real)\<^sup>2"
hoelzl@52660
  1555
by (auto simp add: power2_eq_square)
hoelzl@52660
  1556
hoelzl@52660
  1557
hoelzl@52660
  1558
lemma numeral_power_le_real_of_nat_cancel_iff[simp]:
hoelzl@52660
  1559
  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::nat) ^ n \<le> a"
hoelzl@52660
  1560
  unfolding real_of_nat_le_iff[symmetric] by simp
hoelzl@52660
  1561
hoelzl@52660
  1562
lemma real_of_nat_le_numeral_power_cancel_iff[simp]:
hoelzl@52660
  1563
  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::nat) ^ n"
hoelzl@52660
  1564
  unfolding real_of_nat_le_iff[symmetric] by simp
hoelzl@52660
  1565
hoelzl@52660
  1566
lemma numeral_power_le_real_of_int_cancel_iff[simp]:
hoelzl@52660
  1567
  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::int) ^ n \<le> a"
hoelzl@52660
  1568
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@52660
  1569
hoelzl@52660
  1570
lemma real_of_int_le_numeral_power_cancel_iff[simp]:
hoelzl@52660
  1571
  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::int) ^ n"
hoelzl@52660
  1572
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@52660
  1573
hoelzl@52660
  1574
lemma neg_numeral_power_le_real_of_int_cancel_iff[simp]:
hoelzl@52660
  1575
  "(neg_numeral x::real) ^ n \<le> real a \<longleftrightarrow> (neg_numeral x::int) ^ n \<le> a"
hoelzl@52660
  1576
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@52660
  1577
hoelzl@52660
  1578
lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]:
hoelzl@52660
  1579
  "real a \<le> (neg_numeral x::real) ^ n \<longleftrightarrow> a \<le> (neg_numeral x::int) ^ n"
hoelzl@52660
  1580
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@52660
  1581
hoelzl@52660
  1582
subsection{*Density of the Reals*}
hoelzl@52660
  1583
hoelzl@52660
  1584
lemma real_lbound_gt_zero:
hoelzl@52660
  1585
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
hoelzl@52660
  1586
apply (rule_tac x = " (min d1 d2) /2" in exI)
hoelzl@52660
  1587
apply (simp add: min_def)
hoelzl@52660
  1588
done
hoelzl@52660
  1589
hoelzl@52660
  1590
hoelzl@52660
  1591
text{*Similar results are proved in @{text Fields}*}
hoelzl@52660
  1592
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
hoelzl@52660
  1593
  by auto
hoelzl@52660
  1594
hoelzl@52660
  1595
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
hoelzl@52660
  1596
  by auto
hoelzl@52660
  1597
hoelzl@52660
  1598
lemma real_sum_of_halves: "x/2 + x/2 = (x::real)"
hoelzl@52660
  1599
  by simp
hoelzl@52660
  1600
hoelzl@52660
  1601
subsection{*Absolute Value Function for the Reals*}
hoelzl@52660
  1602
hoelzl@52660
  1603
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
hoelzl@52660
  1604
by (simp add: abs_if)
hoelzl@52660
  1605
hoelzl@52660
  1606
(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
hoelzl@52660
  1607
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
hoelzl@52660
  1608
by (force simp add: abs_le_iff)
hoelzl@52660
  1609
hoelzl@52660
  1610
lemma abs_add_one_gt_zero: "(0::real) < 1 + abs(x)"
hoelzl@52660
  1611
by (simp add: abs_if)
hoelzl@52660
  1612
hoelzl@52660
  1613
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
hoelzl@52660
  1614
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
hoelzl@52660
  1615
hoelzl@52660
  1616
lemma abs_add_one_not_less_self: "~ abs(x) + (1::real) < x"
hoelzl@52660
  1617
by simp
hoelzl@52660
  1618
 
hoelzl@52660
  1619
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
hoelzl@52660
  1620
by simp
hoelzl@52660
  1621
hoelzl@52660
  1622
hoelzl@52660
  1623
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
hoelzl@52660
  1624
hoelzl@52660
  1625
(* FIXME: theorems for negative numerals *)
hoelzl@52660
  1626
lemma numeral_less_real_of_int_iff [simp]:
hoelzl@52660
  1627
     "((numeral n) < real (m::int)) = (numeral n < m)"
hoelzl@52660
  1628
apply auto
hoelzl@52660
  1629
apply (rule real_of_int_less_iff [THEN iffD1])
hoelzl@52660
  1630
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
hoelzl@52660
  1631
done
hoelzl@52660
  1632
hoelzl@52660
  1633
lemma numeral_less_real_of_int_iff2 [simp]:
hoelzl@52660
  1634
     "(real (m::int) < (numeral n)) = (m < numeral n)"
hoelzl@52660
  1635
apply auto
hoelzl@52660
  1636
apply (rule real_of_int_less_iff [THEN iffD1])
hoelzl@52660
  1637
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
hoelzl@52660
  1638
done
hoelzl@52660
  1639
hoelzl@52660
  1640
lemma numeral_le_real_of_int_iff [simp]:
hoelzl@52660
  1641
     "((numeral n) \<le> real (m::int)) = (numeral n \<le> m)"
hoelzl@52660
  1642
by (simp add: linorder_not_less [symmetric])
hoelzl@52660
  1643
hoelzl@52660
  1644
lemma numeral_le_real_of_int_iff2 [simp]:
hoelzl@52660
  1645
     "(real (m::int) \<le> (numeral n)) = (m \<le> numeral n)"
hoelzl@52660
  1646
by (simp add: linorder_not_less [symmetric])
hoelzl@52660
  1647
hoelzl@52660
  1648
lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
hoelzl@52660
  1649
unfolding real_of_nat_def by simp
hoelzl@52660
  1650
hoelzl@52660
  1651
lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
hoelzl@52660
  1652
unfolding real_of_nat_def by (simp add: floor_minus)
hoelzl@52660
  1653
hoelzl@52660
  1654
lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
hoelzl@52660
  1655
unfolding real_of_int_def by simp
hoelzl@52660
  1656
hoelzl@52660
  1657
lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
hoelzl@52660
  1658
unfolding real_of_int_def by (simp add: floor_minus)
hoelzl@52660
  1659
hoelzl@52660
  1660
lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
hoelzl@52660
  1661
unfolding real_of_int_def by (rule floor_exists)
hoelzl@52660
  1662
hoelzl@52660
  1663
lemma lemma_floor:
hoelzl@52660
  1664
  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
hoelzl@52660
  1665
  shows "m \<le> (n::int)"
hoelzl@52660
  1666
proof -
hoelzl@52660
  1667
  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
hoelzl@52660
  1668
  also have "... = real (n + 1)" by simp
hoelzl@52660
  1669
  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
hoelzl@52660
  1670
  thus ?thesis by arith
hoelzl@52660
  1671
qed
hoelzl@52660
  1672
hoelzl@52660
  1673
lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
hoelzl@52660
  1674
unfolding real_of_int_def by (rule of_int_floor_le)
hoelzl@52660
  1675
hoelzl@52660
  1676
lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
hoelzl@52660
  1677
by (auto intro: lemma_floor)
hoelzl@52660
  1678
hoelzl@52660
  1679
lemma real_of_int_floor_cancel [simp]:
hoelzl@52660
  1680
    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
hoelzl@52660
  1681
  using floor_real_of_int by metis
hoelzl@52660
  1682
hoelzl@52660
  1683
lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
hoelzl@52660
  1684
  unfolding real_of_int_def using floor_unique [of n x] by simp
hoelzl@52660
  1685
hoelzl@52660
  1686
lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
hoelzl@52660
  1687
  unfolding real_of_int_def by (rule floor_unique)
hoelzl@52660
  1688
hoelzl@52660
  1689
lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
hoelzl@52660
  1690
apply (rule inj_int [THEN injD])
hoelzl@52660
  1691
apply (simp add: real_of_nat_Suc)
hoelzl@52660
  1692
apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
hoelzl@52660
  1693
done
hoelzl@52660
  1694
hoelzl@52660
  1695
lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
hoelzl@52660
  1696
apply (drule order_le_imp_less_or_eq)
hoelzl@52660
  1697
apply (auto intro: floor_eq3)
hoelzl@52660
  1698
done
hoelzl@52660
  1699
hoelzl@52660
  1700
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
hoelzl@52660
  1701
  unfolding real_of_int_def using floor_correct [of r] by simp
hoelzl@52660
  1702
hoelzl@52660
  1703
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
hoelzl@52660
  1704
  unfolding real_of_int_def using floor_correct [of r] by simp
hoelzl@52660
  1705
hoelzl@52660
  1706
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
hoelzl@52660
  1707
  unfolding real_of_int_def using floor_correct [of r] by simp
hoelzl@52660
  1708
hoelzl@52660
  1709
lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
hoelzl@52660
  1710
  unfolding real_of_int_def using floor_correct [of r] by simp
hoelzl@52660
  1711
hoelzl@52660
  1712
lemma le_floor: "real a <= x ==> a <= floor x"
hoelzl@52660
  1713
  unfolding real_of_int_def by (simp add: le_floor_iff)
hoelzl@52660
  1714
hoelzl@52660
  1715
lemma real_le_floor: "a <= floor x ==> real a <= x"
hoelzl@52660
  1716
  unfolding real_of_int_def by (simp add: le_floor_iff)
hoelzl@52660
  1717
hoelzl@52660
  1718
lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
hoelzl@52660
  1719
  unfolding real_of_int_def by (rule le_floor_iff)
hoelzl@52660
  1720
hoelzl@52660
  1721
lemma floor_less_eq: "(floor x < a) = (x < real a)"
hoelzl@52660
  1722
  unfolding real_of_int_def by (rule floor_less_iff)
hoelzl@52660
  1723
hoelzl@52660
  1724
lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
hoelzl@52660
  1725
  unfolding real_of_int_def by (rule less_floor_iff)
hoelzl@52660
  1726
hoelzl@52660
  1727
lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
hoelzl@52660
  1728
  unfolding real_of_int_def by (rule floor_le_iff)
hoelzl@52660
  1729
hoelzl@52660
  1730
lemma floor_add [simp]: "floor (x + real a) = floor x + a"
hoelzl@52660
  1731
  unfolding real_of_int_def by (rule floor_add_of_int)
hoelzl@52660
  1732
hoelzl@52660
  1733
lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
hoelzl@52660
  1734
  unfolding real_of_int_def by (rule floor_diff_of_int)
hoelzl@52660
  1735
hoelzl@52660
  1736
lemma le_mult_floor:
hoelzl@52660
  1737
  assumes "0 \<le> (a :: real)" and "0 \<le> b"
hoelzl@52660
  1738
  shows "floor a * floor b \<le> floor (a * b)"
hoelzl@52660
  1739
proof -
hoelzl@52660
  1740
  have "real (floor a) \<le> a"
hoelzl@52660
  1741
    and "real (floor b) \<le> b" by auto
hoelzl@52660
  1742
  hence "real (floor a * floor b) \<le> a * b"
hoelzl@52660
  1743
    using assms by (auto intro!: mult_mono)
hoelzl@52660
  1744
  also have "a * b < real (floor (a * b) + 1)" by auto
hoelzl@52660
  1745
  finally show ?thesis unfolding real_of_int_less_iff by simp
hoelzl@52660
  1746
qed
hoelzl@52660
  1747
hoelzl@52660
  1748
lemma floor_divide_eq_div:
hoelzl@52660
  1749
  "floor (real a / real b) = a div b"
hoelzl@52660
  1750
proof cases
hoelzl@52660
  1751
  assume "b \<noteq> 0 \<or> b dvd a"
hoelzl@52660
  1752
  with real_of_int_div3[of a b] show ?thesis
hoelzl@52660
  1753
    by (auto simp: real_of_int_div[symmetric] intro!: floor_eq2 real_of_int_div4 neq_le_trans)
hoelzl@52660
  1754
       (metis add_left_cancel zero_neq_one real_of_int_div_aux real_of_int_inject
hoelzl@52660
  1755
              real_of_int_zero_cancel right_inverse_eq div_self mod_div_trivial)
hoelzl@52660
  1756
qed (auto simp: real_of_int_div)
hoelzl@52660
  1757
hoelzl@52660
  1758
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
hoelzl@52660
  1759
  unfolding real_of_nat_def by simp
hoelzl@52660
  1760
hoelzl@52660
  1761
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
hoelzl@52660
  1762
  unfolding real_of_int_def by (rule le_of_int_ceiling)
hoelzl@52660
  1763
hoelzl@52660
  1764
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
hoelzl@52660
  1765
  unfolding real_of_int_def by simp
hoelzl@52660
  1766
hoelzl@52660
  1767
lemma real_of_int_ceiling_cancel [simp]:
hoelzl@52660
  1768
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
hoelzl@52660
  1769
  using ceiling_real_of_int by metis
hoelzl@52660
  1770
hoelzl@52660
  1771
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
hoelzl@52660
  1772
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
hoelzl@52660
  1773
hoelzl@52660
  1774
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
hoelzl@52660
  1775
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
hoelzl@52660
  1776
hoelzl@52660
  1777
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
hoelzl@52660
  1778
  unfolding real_of_int_def using ceiling_unique [of n x] by simp
hoelzl@52660
  1779
hoelzl@52660
  1780
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
hoelzl@52660
  1781
  unfolding real_of_int_def using ceiling_correct [of r] by simp
hoelzl@52660
  1782
hoelzl@52660
  1783
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
hoelzl@52660
  1784
  unfolding real_of_int_def using ceiling_correct [of r] by simp
hoelzl@52660
  1785
hoelzl@52660
  1786
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
hoelzl@52660
  1787
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
hoelzl@52660
  1788
hoelzl@52660
  1789
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
hoelzl@52660
  1790
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
hoelzl@52660
  1791
hoelzl@52660
  1792
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
hoelzl@52660
  1793
  unfolding real_of_int_def by (rule ceiling_le_iff)
hoelzl@52660
  1794
hoelzl@52660
  1795
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
hoelzl@52660
  1796
  unfolding real_of_int_def by (rule less_ceiling_iff)
hoelzl@52660
  1797
hoelzl@52660
  1798
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
hoelzl@52660
  1799
  unfolding real_of_int_def by (rule ceiling_less_iff)
hoelzl@52660
  1800
hoelzl@52660
  1801
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
hoelzl@52660
  1802
  unfolding real_of_int_def by (rule le_ceiling_iff)
hoelzl@52660
  1803
hoelzl@52660
  1804
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
hoelzl@52660
  1805
  unfolding real_of_int_def by (rule ceiling_add_of_int)
hoelzl@52660
  1806
hoelzl@52660
  1807
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
hoelzl@52660
  1808
  unfolding real_of_int_def by (rule ceiling_diff_of_int)
hoelzl@52660
  1809
hoelzl@52660
  1810
hoelzl@52660
  1811
subsubsection {* Versions for the natural numbers *}
hoelzl@52660
  1812
hoelzl@52660
  1813
definition
hoelzl@52660
  1814
  natfloor :: "real => nat" where
hoelzl@52660
  1815
  "natfloor x = nat(floor x)"
hoelzl@52660
  1816
hoelzl@52660
  1817
definition
hoelzl@52660
  1818
  natceiling :: "real => nat" where
hoelzl@52660
  1819
  "natceiling x = nat(ceiling x)"
hoelzl@52660
  1820
hoelzl@52660
  1821
lemma natfloor_zero [simp]: "natfloor 0 = 0"
hoelzl@52660
  1822
  by (unfold natfloor_def, simp)
hoelzl@52660
  1823
hoelzl@52660
  1824
lemma natfloor_one [simp]: "natfloor 1 = 1"
hoelzl@52660
  1825
  by (unfold natfloor_def, simp)
hoelzl@52660
  1826
hoelzl@52660
  1827
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
hoelzl@52660
  1828
  by (unfold natfloor_def, simp)
hoelzl@52660
  1829
hoelzl@52660
  1830
lemma natfloor_numeral_eq [simp]: "natfloor (numeral n) = numeral n"
hoelzl@52660
  1831
  by (unfold natfloor_def, simp)
hoelzl@52660
  1832
hoelzl@52660
  1833
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
hoelzl@52660
  1834
  by (unfold natfloor_def, simp)
hoelzl@52660
  1835
hoelzl@52660
  1836
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
hoelzl@52660
  1837
  by (unfold natfloor_def, simp)
hoelzl@52660
  1838
hoelzl@52660
  1839
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
hoelzl@52660
  1840
  unfolding natfloor_def by simp
hoelzl@52660
  1841
hoelzl@52660
  1842
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
hoelzl@52660
  1843
  unfolding natfloor_def by (intro nat_mono floor_mono)
hoelzl@52660
  1844
hoelzl@52660
  1845
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
hoelzl@52660
  1846
  apply (unfold natfloor_def)
hoelzl@52660
  1847
  apply (subst nat_int [THEN sym])
hoelzl@52660
  1848
  apply (rule nat_mono)
hoelzl@52660
  1849
  apply (rule le_floor)
hoelzl@52660
  1850
  apply simp
hoelzl@52660
  1851
done
hoelzl@52660
  1852
hoelzl@52660
  1853
lemma natfloor_less_iff: "0 \<le> x \<Longrightarrow> natfloor x < n \<longleftrightarrow> x < real n"
hoelzl@52660
  1854
  unfolding natfloor_def real_of_nat_def
hoelzl@52660
  1855
  by (simp add: nat_less_iff floor_less_iff)
hoelzl@52660
  1856
hoelzl@52660
  1857
lemma less_natfloor:
hoelzl@52660
  1858
  assumes "0 \<le> x" and "x < real (n :: nat)"
hoelzl@52660
  1859
  shows "natfloor x < n"
hoelzl@52660
  1860
  using assms by (simp add: natfloor_less_iff)
hoelzl@52660
  1861
hoelzl@52660
  1862
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
hoelzl@52660
  1863
  apply (rule iffI)
hoelzl@52660
  1864
  apply (rule order_trans)
hoelzl@52660
  1865
  prefer 2
hoelzl@52660
  1866
  apply (erule real_natfloor_le)
hoelzl@52660
  1867
  apply (subst real_of_nat_le_iff)
hoelzl@52660
  1868
  apply assumption
hoelzl@52660
  1869
  apply (erule le_natfloor)
hoelzl@52660
  1870
done
hoelzl@52660
  1871
hoelzl@52660
  1872
lemma le_natfloor_eq_numeral [simp]:
hoelzl@52660
  1873
    "~ neg((numeral n)::int) ==> 0 <= x ==>
hoelzl@52660
  1874
      (numeral n <= natfloor x) = (numeral n <= x)"
hoelzl@52660
  1875
  apply (subst le_natfloor_eq, assumption)
hoelzl@52660
  1876
  apply simp
hoelzl@52660
  1877
done
hoelzl@52660
  1878
hoelzl@52660
  1879
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
hoelzl@52660
  1880
  apply (case_tac "0 <= x")
hoelzl@52660
  1881
  apply (subst le_natfloor_eq, assumption, simp)
hoelzl@52660
  1882
  apply (rule iffI)
hoelzl@52660
  1883
  apply (subgoal_tac "natfloor x <= natfloor 0")
hoelzl@52660
  1884
  apply simp
hoelzl@52660
  1885
  apply (rule natfloor_mono)
hoelzl@52660
  1886
  apply simp
hoelzl@52660
  1887
  apply simp
hoelzl@52660
  1888
done
hoelzl@52660
  1889
hoelzl@52660
  1890
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
hoelzl@52660
  1891
  unfolding natfloor_def by (simp add: floor_eq2 [where n="int n"])
hoelzl@52660
  1892
hoelzl@52660
  1893
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
hoelzl@52660
  1894
  apply (case_tac "0 <= x")
hoelzl@52660
  1895
  apply (unfold natfloor_def)
hoelzl@52660
  1896
  apply simp
hoelzl@52660
  1897
  apply simp_all
hoelzl@52660
  1898
done
hoelzl@52660
  1899
hoelzl@52660
  1900
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
hoelzl@52660
  1901
using real_natfloor_add_one_gt by (simp add: algebra_simps)
hoelzl@52660
  1902
hoelzl@52660
  1903
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
hoelzl@52660
  1904
  apply (subgoal_tac "z < real(natfloor z) + 1")
hoelzl@52660
  1905
  apply arith
hoelzl@52660
  1906
  apply (rule real_natfloor_add_one_gt)
hoelzl@52660
  1907
done
hoelzl@52660
  1908
hoelzl@52660
  1909
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
hoelzl@52660
  1910
  unfolding natfloor_def
hoelzl@52660
  1911
  unfolding real_of_int_of_nat_eq [symmetric] floor_add
hoelzl@52660
  1912
  by (simp add: nat_add_distrib)
hoelzl@52660
  1913
hoelzl@52660
  1914
lemma natfloor_add_numeral [simp]:
hoelzl@52660
  1915
    "~neg ((numeral n)::int) ==> 0 <= x ==>
hoelzl@52660
  1916
      natfloor (x + numeral n) = natfloor x + numeral n"
hoelzl@52660
  1917
  by (simp add: natfloor_add [symmetric])
hoelzl@52660
  1918
hoelzl@52660
  1919
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
hoelzl@52660
  1920
  by (simp add: natfloor_add [symmetric] del: One_nat_def)
hoelzl@52660
  1921
hoelzl@52660
  1922
lemma natfloor_subtract [simp]:
hoelzl@52660
  1923
    "natfloor(x - real a) = natfloor x - a"
hoelzl@52660
  1924
  unfolding natfloor_def real_of_int_of_nat_eq [symmetric] floor_subtract
hoelzl@52660
  1925
  by simp
hoelzl@52660
  1926
hoelzl@52660
  1927
lemma natfloor_div_nat:
hoelzl@52660
  1928
  assumes "1 <= x" and "y > 0"
hoelzl@52660
  1929
  shows "natfloor (x / real y) = natfloor x div y"
hoelzl@52660
  1930
proof (rule natfloor_eq)
hoelzl@52660
  1931
  have "(natfloor x) div y * y \<le> natfloor x"
hoelzl@52660
  1932
    by (rule add_leD1 [where k="natfloor x mod y"], simp)
hoelzl@52660
  1933
  thus "real (natfloor x div y) \<le> x / real y"
hoelzl@52660
  1934
    using assms by (simp add: le_divide_eq le_natfloor_eq)
hoelzl@52660
  1935
  have "natfloor x < (natfloor x) div y * y + y"
hoelzl@52660
  1936
    apply (subst mod_div_equality [symmetric])
hoelzl@52660
  1937
    apply (rule add_strict_left_mono)
hoelzl@52660
  1938
    apply (rule mod_less_divisor)
hoelzl@52660
  1939
    apply fact
hoelzl@52660
  1940
    done
hoelzl@52660
  1941
  thus "x / real y < real (natfloor x div y) + 1"
hoelzl@52660
  1942
    using assms
hoelzl@52660
  1943
    by (simp add: divide_less_eq natfloor_less_iff distrib_right)
hoelzl@52660
  1944
qed
hoelzl@52660
  1945
hoelzl@52660
  1946
lemma le_mult_natfloor:
hoelzl@52660
  1947
  shows "natfloor a * natfloor b \<le> natfloor (a * b)"
hoelzl@52660
  1948
  by (cases "0 <= a & 0 <= b")
hoelzl@52660
  1949
    (auto simp add: le_natfloor_eq mult_nonneg_nonneg mult_mono' real_natfloor_le natfloor_neg)
hoelzl@52660
  1950
hoelzl@52660
  1951
lemma natceiling_zero [simp]: "natceiling 0 = 0"
hoelzl@52660
  1952
  by (unfold natceiling_def, simp)
hoelzl@52660
  1953
hoelzl@52660
  1954
lemma natceiling_one [simp]: "natceiling 1 = 1"
hoelzl@52660
  1955
  by (unfold natceiling_def, simp)
hoelzl@52660
  1956
hoelzl@52660
  1957
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
hoelzl@52660
  1958
  by (unfold natceiling_def, simp)
hoelzl@52660
  1959
hoelzl@52660
  1960
lemma natceiling_numeral_eq [simp]: "natceiling (numeral n) = numeral n"
hoelzl@52660
  1961
  by (unfold natceiling_def, simp)
hoelzl@52660
  1962
hoelzl@52660
  1963
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
hoelzl@52660
  1964
  by (unfold natceiling_def, simp)
hoelzl@52660
  1965
hoelzl@52660
  1966
lemma real_natceiling_ge: "x <= real(natceiling x)"
hoelzl@52660
  1967
  unfolding natceiling_def by (cases "x < 0", simp_all)
hoelzl@52660
  1968
hoelzl@52660
  1969
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
hoelzl@52660
  1970
  unfolding natceiling_def by simp
hoelzl@52660
  1971
hoelzl@52660
  1972
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
hoelzl@52660
  1973
  unfolding natceiling_def by (intro nat_mono ceiling_mono)
hoelzl@52660
  1974
hoelzl@52660
  1975
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
hoelzl@52660
  1976
  unfolding natceiling_def real_of_nat_def
hoelzl@52660
  1977
  by (simp add: nat_le_iff ceiling_le_iff)
hoelzl@52660
  1978
hoelzl@52660
  1979
lemma natceiling_le_eq: "(natceiling x <= a) = (x <= real a)"
hoelzl@52660
  1980
  unfolding natceiling_def real_of_nat_def
hoelzl@52660
  1981
  by (simp add: nat_le_iff ceiling_le_iff)
hoelzl@52660
  1982
hoelzl@52660
  1983
lemma natceiling_le_eq_numeral [simp]:
hoelzl@52660
  1984
    "~ neg((numeral n)::int) ==>
hoelzl@52660
  1985
      (natceiling x <= numeral n) = (x <= numeral n)"
hoelzl@52660
  1986
  by (simp add: natceiling_le_eq)
hoelzl@52660
  1987
hoelzl@52660
  1988
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
hoelzl@52660
  1989
  unfolding natceiling_def
hoelzl@52660
  1990
  by (simp add: nat_le_iff ceiling_le_iff)
hoelzl@52660
  1991
hoelzl@52660
  1992
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
hoelzl@52660
  1993
  unfolding natceiling_def
hoelzl@52660
  1994
  by (simp add: ceiling_eq2 [where n="int n"])
hoelzl@52660
  1995
hoelzl@52660
  1996
lemma natceiling_add [simp]: "0 <= x ==>
hoelzl@52660
  1997
    natceiling (x + real a) = natceiling x + a"
hoelzl@52660
  1998
  unfolding natceiling_def
hoelzl@52660
  1999
  unfolding real_of_int_of_nat_eq [symmetric] ceiling_add
hoelzl@52660
  2000
  by (simp add: nat_add_distrib)
hoelzl@52660
  2001
hoelzl@52660
  2002
lemma natceiling_add_numeral [simp]:
hoelzl@52660
  2003
    "~ neg ((numeral n)::int) ==> 0 <= x ==>
hoelzl@52660
  2004
      natceiling (x + numeral n) = natceiling x + numeral n"
hoelzl@52660
  2005
  by (simp add: natceiling_add [symmetric])
hoelzl@52660
  2006
hoelzl@52660
  2007
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
hoelzl@52660
  2008
  by (simp add: natceiling_add [symmetric] del: One_nat_def)
hoelzl@52660
  2009
hoelzl@52660
  2010
lemma natceiling_subtract [simp]: "natceiling(x - real a) = natceiling x - a"
hoelzl@52660
  2011
  unfolding natceiling_def real_of_int_of_nat_eq [symmetric] ceiling_subtract
hoelzl@52660
  2012
  by simp
hoelzl@52660
  2013
hoelzl@52660
  2014
subsection {* Exponentiation with floor *}
hoelzl@52660
  2015
hoelzl@52660
  2016
lemma floor_power:
hoelzl@52660
  2017
  assumes "x = real (floor x)"
hoelzl@52660
  2018
  shows "floor (x ^ n) = floor x ^ n"
hoelzl@52660
  2019
proof -
hoelzl@52660
  2020
  have *: "x ^ n = real (floor x ^ n)"
hoelzl@52660
  2021
    using assms by (induct n arbitrary: x) simp_all
hoelzl@52660
  2022
  show ?thesis unfolding real_of_int_inject[symmetric]
hoelzl@52660
  2023
    unfolding * floor_real_of_int ..
hoelzl@52660
  2024
qed
hoelzl@52660
  2025
hoelzl@52660
  2026
lemma natfloor_power:
hoelzl@52660
  2027
  assumes "x = real (natfloor x)"
hoelzl@52660
  2028
  shows "natfloor (x ^ n) = natfloor x ^ n"
hoelzl@52660
  2029
proof -
hoelzl@52660
  2030
  from assms have "0 \<le> floor x" by auto
hoelzl@52660
  2031
  note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
hoelzl@52660
  2032
  from floor_power[OF this]
hoelzl@52660
  2033
  show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
hoelzl@52660
  2034
    by simp
hoelzl@52660
  2035
qed
hoelzl@52660
  2036
hoelzl@52660
  2037
hoelzl@52660
  2038
subsection {* Implementation of rational real numbers *}
hoelzl@52660
  2039
hoelzl@52660
  2040
text {* Formal constructor *}
hoelzl@52660
  2041
hoelzl@52660
  2042
definition Ratreal :: "rat \<Rightarrow> real" where
hoelzl@52660
  2043
  [code_abbrev, simp]: "Ratreal = of_rat"
hoelzl@52660
  2044
hoelzl@52660
  2045
code_datatype Ratreal
hoelzl@52660
  2046
hoelzl@52660
  2047
hoelzl@52660
  2048
text {* Numerals *}
hoelzl@52660
  2049
hoelzl@52660
  2050
lemma [code_abbrev]:
hoelzl@52660
  2051
  "(of_rat (of_int a) :: real) = of_int a"
hoelzl@52660
  2052
  by simp
hoelzl@52660
  2053
hoelzl@52660
  2054
lemma [code_abbrev]:
hoelzl@52660
  2055
  "(of_rat 0 :: real) = 0"
hoelzl@52660
  2056
  by simp
hoelzl@52660
  2057
hoelzl@52660
  2058
lemma [code_abbrev]:
hoelzl@52660
  2059
  "(of_rat 1 :: real) = 1"
hoelzl@52660
  2060
  by simp
hoelzl@52660
  2061
hoelzl@52660
  2062
lemma [code_abbrev]:
hoelzl@52660
  2063
  "(of_rat (numeral k) :: real) = numeral k"
hoelzl@52660
  2064
  by simp
hoelzl@52660
  2065
hoelzl@52660
  2066
lemma [code_abbrev]:
hoelzl@52660
  2067
  "(of_rat (neg_numeral k) :: real) = neg_numeral k"
hoelzl@52660
  2068
  by simp
hoelzl@52660
  2069
hoelzl@52660
  2070
lemma [code_post]:
hoelzl@52660
  2071
  "(of_rat (0 / r)  :: real) = 0"
hoelzl@52660
  2072
  "(of_rat (r / 0)  :: real) = 0"
hoelzl@52660
  2073
  "(of_rat (1 / 1)  :: real) = 1"
hoelzl@52660
  2074
  "(of_rat (numeral k / 1) :: real) = numeral k"
hoelzl@52660
  2075
  "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
hoelzl@52660
  2076
  "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
hoelzl@52660
  2077
  "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
hoelzl@52660
  2078
  "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
hoelzl@52660
  2079
  "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
hoelzl@52660
  2080
  "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
hoelzl@52660
  2081
  "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
hoelzl@52660
  2082
  by (simp_all add: of_rat_divide)
hoelzl@52660
  2083
hoelzl@52660
  2084
hoelzl@52660
  2085
text {* Operations *}
hoelzl@52660
  2086
hoelzl@52660
  2087
lemma zero_real_code [code]:
hoelzl@52660
  2088
  "0 = Ratreal 0"
hoelzl@52660
  2089
by simp
hoelzl@52660
  2090
hoelzl@52660
  2091
lemma one_real_code [code]:
hoelzl@52660
  2092
  "1 = Ratreal 1"
hoelzl@52660
  2093
by simp
hoelzl@52660
  2094
hoelzl@52660
  2095
instantiation real :: equal
hoelzl@52660
  2096
begin
hoelzl@52660
  2097
hoelzl@52660
  2098
definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
hoelzl@52660
  2099
hoelzl@52660
  2100
instance proof
hoelzl@52660
  2101
qed (simp add: equal_real_def)
hoelzl@52660
  2102
hoelzl@52660
  2103
lemma real_equal_code [code]:
hoelzl@52660
  2104
  "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
hoelzl@52660
  2105
  by (simp add: equal_real_def equal)
hoelzl@52660
  2106
hoelzl@52660
  2107
lemma [code nbe]:
hoelzl@52660
  2108
  "HOL.equal (x::real) x \<longleftrightarrow> True"
hoelzl@52660
  2109
  by (rule equal_refl)
hoelzl@52660
  2110
hoelzl@52660
  2111
end
hoelzl@52660
  2112
hoelzl@52660
  2113
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
hoelzl@52660
  2114
  by (simp add: of_rat_less_eq)
hoelzl@52660
  2115
hoelzl@52660
  2116
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
hoelzl@52660
  2117
  by (simp add: of_rat_less)
hoelzl@52660
  2118
hoelzl@52660
  2119
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
hoelzl@52660
  2120
  by (simp add: of_rat_add)
hoelzl@52660
  2121
hoelzl@52660
  2122
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
hoelzl@52660
  2123
  by (simp add: of_rat_mult)
hoelzl@52660
  2124
hoelzl@52660
  2125
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
hoelzl@52660
  2126
  by (simp add: of_rat_minus)
hoelzl@52660
  2127
hoelzl@52660
  2128
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
hoelzl@52660
  2129
  by (simp add: of_rat_diff)
hoelzl@52660
  2130
hoelzl@52660
  2131
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
hoelzl@52660
  2132
  by (simp add: of_rat_inverse)
hoelzl@52660
  2133
 
hoelzl@52660
  2134
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
hoelzl@52660
  2135
  by (simp add: of_rat_divide)
hoelzl@52660
  2136
hoelzl@52660
  2137
lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
hoelzl@52660
  2138
  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@52660
  2139
hoelzl@52660
  2140
hoelzl@52660
  2141
text {* Quickcheck *}
hoelzl@52660
  2142
hoelzl@52660
  2143
definition (in term_syntax)
hoelzl@52660
  2144
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
hoelzl@52660
  2145
  [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
hoelzl@52660
  2146
hoelzl@52660
  2147
notation fcomp (infixl "\<circ>>" 60)
hoelzl@52660
  2148
notation scomp (infixl "\<circ>\<rightarrow>" 60)
hoelzl@52660
  2149
hoelzl@52660
  2150
instantiation real :: random
hoelzl@52660
  2151
begin
hoelzl@52660
  2152
hoelzl@52660
  2153
definition
hoelzl@52660
  2154
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
hoelzl@52660
  2155
hoelzl@52660
  2156
instance ..
hoelzl@52660
  2157
hoelzl@52660
  2158
end
hoelzl@52660
  2159
hoelzl@52660
  2160
no_notation fcomp (infixl "\<circ>>" 60)
hoelzl@52660
  2161
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
hoelzl@52660
  2162
hoelzl@52660
  2163
instantiation real :: exhaustive
hoelzl@52660
  2164
begin
hoelzl@52660
  2165
hoelzl@52660
  2166
definition
hoelzl@52660
  2167
  "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
hoelzl@52660
  2168
hoelzl@52660
  2169
instance ..
hoelzl@52660
  2170
hoelzl@52660
  2171
end
hoelzl@52660
  2172
hoelzl@52660
  2173
instantiation real :: full_exhaustive
hoelzl@52660
  2174
begin
hoelzl@52660
  2175
hoelzl@52660
  2176
definition
hoelzl@52660
  2177
  "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
hoelzl@52660
  2178
hoelzl@52660
  2179
instance ..
hoelzl@52660
  2180
hoelzl@52660
  2181
end
hoelzl@52660
  2182
hoelzl@52660
  2183
instantiation real :: narrowing
hoelzl@52660
  2184
begin
hoelzl@52660
  2185
hoelzl@52660
  2186
definition
hoelzl@52660
  2187
  "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
hoelzl@52660
  2188
hoelzl@52660
  2189
instance ..
hoelzl@52660
  2190
hoelzl@52660
  2191
end
hoelzl@52660
  2192
hoelzl@52660
  2193
hoelzl@52660
  2194
subsection {* Setup for Nitpick *}
hoelzl@52660
  2195
hoelzl@52660
  2196
declaration {*
hoelzl@52660
  2197
  Nitpick_HOL.register_frac_type @{type_name real}
hoelzl@52660
  2198
   [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
hoelzl@52660
  2199
    (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
hoelzl@52660
  2200
    (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
hoelzl@52660
  2201
    (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
hoelzl@52660
  2202
    (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
hoelzl@52660
  2203
    (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
hoelzl@52660
  2204
    (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
hoelzl@52660
  2205
    (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
hoelzl@52660
  2206
*}
hoelzl@52660
  2207
hoelzl@52660
  2208
lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
hoelzl@52660
  2209
    ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
hoelzl@52660
  2210
    times_real_inst.times_real uminus_real_inst.uminus_real
hoelzl@52660
  2211
    zero_real_inst.zero_real
hoelzl@52660
  2212
hoelzl@52660
  2213
ML_file "Tools/SMT/smt_real.ML"
hoelzl@52660
  2214
setup SMT_Real.setup
hoelzl@52660
  2215
hoelzl@52660
  2216
end