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