src/HOL/RealDef.thy
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51474 1e9e68247ad1
parent 51375 d9e62d9c98de
child 51518 6a56b7088a6a
permissions -rw-r--r--
generalize Bfun and Bseq to metric spaces; Bseq is an abbreviation for Bfun
haftmann@28952
     1
(*  Title       : HOL/RealDef.thy
huffman@36795
     2
    Author      : Jacques D. Fleuriot, 1998
paulson@14387
     3
    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
avigad@16819
     4
    Additional contributions by Jeremy Avigad
huffman@36795
     5
    Construction of Cauchy Reals by Brian Huffman, 2010
paulson@14269
     6
*)
paulson@14269
     7
huffman@36795
     8
header {* Development of the Reals using Cauchy Sequences *}
paulson@14387
     9
nipkow@15131
    10
theory RealDef
huffman@36795
    11
imports Rat
nipkow@15131
    12
begin
paulson@5588
    13
huffman@36795
    14
text {*
huffman@36795
    15
  This theory contains a formalization of the real numbers as
huffman@36795
    16
  equivalence classes of Cauchy sequences of rationals.  See
huffman@40810
    17
  @{file "~~/src/HOL/ex/Dedekind_Real.thy"} for an alternative
huffman@40810
    18
  construction using Dedekind cuts.
huffman@36795
    19
*}
huffman@36795
    20
huffman@36795
    21
subsection {* Preliminary lemmas *}
huffman@36795
    22
huffman@36795
    23
lemma add_diff_add:
huffman@36795
    24
  fixes a b c d :: "'a::ab_group_add"
huffman@36795
    25
  shows "(a + c) - (b + d) = (a - b) + (c - d)"
huffman@36795
    26
  by simp
huffman@36795
    27
huffman@36795
    28
lemma minus_diff_minus:
huffman@36795
    29
  fixes a b :: "'a::ab_group_add"
huffman@36795
    30
  shows "- a - - b = - (a - b)"
huffman@36795
    31
  by simp
huffman@36795
    32
huffman@36795
    33
lemma mult_diff_mult:
huffman@36795
    34
  fixes x y a b :: "'a::ring"
huffman@36795
    35
  shows "(x * y - a * b) = x * (y - b) + (x - a) * b"
huffman@36795
    36
  by (simp add: algebra_simps)
huffman@36795
    37
huffman@36795
    38
lemma inverse_diff_inverse:
huffman@36795
    39
  fixes a b :: "'a::division_ring"
huffman@36795
    40
  assumes "a \<noteq> 0" and "b \<noteq> 0"
huffman@36795
    41
  shows "inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
huffman@36795
    42
  using assms by (simp add: algebra_simps)
huffman@36795
    43
huffman@36795
    44
lemma obtain_pos_sum:
huffman@36795
    45
  fixes r :: rat assumes r: "0 < r"
huffman@36795
    46
  obtains s t where "0 < s" and "0 < t" and "r = s + t"
huffman@36795
    47
proof
huffman@36795
    48
    from r show "0 < r/2" by simp
huffman@36795
    49
    from r show "0 < r/2" by simp
huffman@36795
    50
    show "r = r/2 + r/2" by simp
huffman@36795
    51
qed
huffman@36795
    52
huffman@36795
    53
subsection {* Sequences that converge to zero *}
huffman@36795
    54
wenzelm@19765
    55
definition
huffman@36795
    56
  vanishes :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
huffman@36795
    57
where
huffman@36795
    58
  "vanishes X = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r)"
huffman@36795
    59
huffman@36795
    60
lemma vanishesI: "(\<And>r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r) \<Longrightarrow> vanishes X"
huffman@36795
    61
  unfolding vanishes_def by simp
huffman@36795
    62
huffman@36795
    63
lemma vanishesD: "\<lbrakk>vanishes X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. \<bar>X n\<bar> < r"
huffman@36795
    64
  unfolding vanishes_def by simp
huffman@36795
    65
huffman@36795
    66
lemma vanishes_const [simp]: "vanishes (\<lambda>n. c) \<longleftrightarrow> c = 0"
huffman@36795
    67
  unfolding vanishes_def
huffman@36795
    68
  apply (cases "c = 0", auto)
huffman@36795
    69
  apply (rule exI [where x="\<bar>c\<bar>"], auto)
huffman@36795
    70
  done
huffman@36795
    71
huffman@36795
    72
lemma vanishes_minus: "vanishes X \<Longrightarrow> vanishes (\<lambda>n. - X n)"
huffman@36795
    73
  unfolding vanishes_def by simp
paulson@14269
    74
huffman@36795
    75
lemma vanishes_add:
huffman@36795
    76
  assumes X: "vanishes X" and Y: "vanishes Y"
huffman@36795
    77
  shows "vanishes (\<lambda>n. X n + Y n)"
huffman@36795
    78
proof (rule vanishesI)
huffman@36795
    79
  fix r :: rat assume "0 < r"
huffman@36795
    80
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
    81
    by (rule obtain_pos_sum)
huffman@36795
    82
  obtain i where i: "\<forall>n\<ge>i. \<bar>X n\<bar> < s"
huffman@36795
    83
    using vanishesD [OF X s] ..
huffman@36795
    84
  obtain j where j: "\<forall>n\<ge>j. \<bar>Y n\<bar> < t"
huffman@36795
    85
    using vanishesD [OF Y t] ..
huffman@36795
    86
  have "\<forall>n\<ge>max i j. \<bar>X n + Y n\<bar> < r"
huffman@36795
    87
  proof (clarsimp)
huffman@36795
    88
    fix n assume n: "i \<le> n" "j \<le> n"
huffman@36795
    89
    have "\<bar>X n + Y n\<bar> \<le> \<bar>X n\<bar> + \<bar>Y n\<bar>" by (rule abs_triangle_ineq)
huffman@36795
    90
    also have "\<dots> < s + t" by (simp add: add_strict_mono i j n)
huffman@36795
    91
    finally show "\<bar>X n + Y n\<bar> < r" unfolding r .
huffman@36795
    92
  qed
huffman@36795
    93
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n + Y n\<bar> < r" ..
huffman@36795
    94
qed
huffman@36795
    95
huffman@36795
    96
lemma vanishes_diff:
huffman@36795
    97
  assumes X: "vanishes X" and Y: "vanishes Y"
huffman@36795
    98
  shows "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
    99
unfolding diff_minus by (intro vanishes_add vanishes_minus X Y)
huffman@36795
   100
huffman@36795
   101
lemma vanishes_mult_bounded:
huffman@36795
   102
  assumes X: "\<exists>a>0. \<forall>n. \<bar>X n\<bar> < a"
huffman@36795
   103
  assumes Y: "vanishes (\<lambda>n. Y n)"
huffman@36795
   104
  shows "vanishes (\<lambda>n. X n * Y n)"
huffman@36795
   105
proof (rule vanishesI)
huffman@36795
   106
  fix r :: rat assume r: "0 < r"
huffman@36795
   107
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
huffman@36795
   108
    using X by fast
huffman@36795
   109
  obtain b where b: "0 < b" "r = a * b"
huffman@36795
   110
  proof
huffman@36795
   111
    show "0 < r / a" using r a by (simp add: divide_pos_pos)
huffman@36795
   112
    show "r = a * (r / a)" using a by simp
huffman@36795
   113
  qed
huffman@36795
   114
  obtain k where k: "\<forall>n\<ge>k. \<bar>Y n\<bar> < b"
huffman@36795
   115
    using vanishesD [OF Y b(1)] ..
huffman@36795
   116
  have "\<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r"
huffman@36795
   117
    by (simp add: b(2) abs_mult mult_strict_mono' a k)
huffman@36795
   118
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>X n * Y n\<bar> < r" ..
huffman@36795
   119
qed
huffman@36795
   120
huffman@36795
   121
subsection {* Cauchy sequences *}
paulson@5588
   122
wenzelm@19765
   123
definition
haftmann@44278
   124
  cauchy :: "(nat \<Rightarrow> rat) \<Rightarrow> bool"
huffman@36795
   125
where
huffman@36795
   126
  "cauchy X \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r)"
huffman@36795
   127
huffman@36795
   128
lemma cauchyI:
huffman@36795
   129
  "(\<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"
huffman@36795
   130
  unfolding cauchy_def by simp
huffman@36795
   131
huffman@36795
   132
lemma cauchyD:
huffman@36795
   133
  "\<lbrakk>cauchy X; 0 < r\<rbrakk> \<Longrightarrow> \<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < r"
huffman@36795
   134
  unfolding cauchy_def by simp
huffman@36795
   135
huffman@36795
   136
lemma cauchy_const [simp]: "cauchy (\<lambda>n. x)"
huffman@36795
   137
  unfolding cauchy_def by simp
huffman@36795
   138
huffman@36795
   139
lemma cauchy_add [simp]:
huffman@36795
   140
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   141
  shows "cauchy (\<lambda>n. X n + Y n)"
huffman@36795
   142
proof (rule cauchyI)
huffman@36795
   143
  fix r :: rat assume "0 < r"
huffman@36795
   144
  then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
   145
    by (rule obtain_pos_sum)
huffman@36795
   146
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
huffman@36795
   147
    using cauchyD [OF X s] ..
huffman@36795
   148
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
huffman@36795
   149
    using cauchyD [OF Y t] ..
huffman@36795
   150
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r"
huffman@36795
   151
  proof (clarsimp)
huffman@36795
   152
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
huffman@36795
   153
    have "\<bar>(X m + Y m) - (X n + Y n)\<bar> \<le> \<bar>X m - X n\<bar> + \<bar>Y m - Y n\<bar>"
huffman@36795
   154
      unfolding add_diff_add by (rule abs_triangle_ineq)
huffman@36795
   155
    also have "\<dots> < s + t"
huffman@36795
   156
      by (rule add_strict_mono, simp_all add: i j *)
huffman@36795
   157
    finally show "\<bar>(X m + Y m) - (X n + Y n)\<bar> < r" unfolding r .
huffman@36795
   158
  qed
huffman@36795
   159
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>(X m + Y m) - (X n + Y n)\<bar> < r" ..
huffman@36795
   160
qed
huffman@36795
   161
huffman@36795
   162
lemma cauchy_minus [simp]:
huffman@36795
   163
  assumes X: "cauchy X"
huffman@36795
   164
  shows "cauchy (\<lambda>n. - X n)"
huffman@36795
   165
using assms unfolding cauchy_def
huffman@36795
   166
unfolding minus_diff_minus abs_minus_cancel .
huffman@36795
   167
huffman@36795
   168
lemma cauchy_diff [simp]:
huffman@36795
   169
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   170
  shows "cauchy (\<lambda>n. X n - Y n)"
huffman@36795
   171
using assms unfolding diff_minus by simp
huffman@36795
   172
huffman@36795
   173
lemma cauchy_imp_bounded:
huffman@36795
   174
  assumes "cauchy X" shows "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
huffman@36795
   175
proof -
huffman@36795
   176
  obtain k where k: "\<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m - X n\<bar> < 1"
huffman@36795
   177
    using cauchyD [OF assms zero_less_one] ..
huffman@36795
   178
  show "\<exists>b>0. \<forall>n. \<bar>X n\<bar> < b"
huffman@36795
   179
  proof (intro exI conjI allI)
huffman@36795
   180
    have "0 \<le> \<bar>X 0\<bar>" by simp
huffman@36795
   181
    also have "\<bar>X 0\<bar> \<le> Max (abs ` X ` {..k})" by simp
huffman@36795
   182
    finally have "0 \<le> Max (abs ` X ` {..k})" .
huffman@36795
   183
    thus "0 < Max (abs ` X ` {..k}) + 1" by simp
huffman@36795
   184
  next
huffman@36795
   185
    fix n :: nat
huffman@36795
   186
    show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1"
huffman@36795
   187
    proof (rule linorder_le_cases)
huffman@36795
   188
      assume "n \<le> k"
huffman@36795
   189
      hence "\<bar>X n\<bar> \<le> Max (abs ` X ` {..k})" by simp
huffman@36795
   190
      thus "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" by simp
huffman@36795
   191
    next
huffman@36795
   192
      assume "k \<le> n"
huffman@36795
   193
      have "\<bar>X n\<bar> = \<bar>X k + (X n - X k)\<bar>" by simp
huffman@36795
   194
      also have "\<bar>X k + (X n - X k)\<bar> \<le> \<bar>X k\<bar> + \<bar>X n - X k\<bar>"
huffman@36795
   195
        by (rule abs_triangle_ineq)
huffman@36795
   196
      also have "\<dots> < Max (abs ` X ` {..k}) + 1"
huffman@36795
   197
        by (rule add_le_less_mono, simp, simp add: k `k \<le> n`)
huffman@36795
   198
      finally show "\<bar>X n\<bar> < Max (abs ` X ` {..k}) + 1" .
huffman@36795
   199
    qed
huffman@36795
   200
  qed
huffman@36795
   201
qed
huffman@36795
   202
huffman@36795
   203
lemma cauchy_mult [simp]:
huffman@36795
   204
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   205
  shows "cauchy (\<lambda>n. X n * Y n)"
huffman@36795
   206
proof (rule cauchyI)
huffman@36795
   207
  fix r :: rat assume "0 < r"
huffman@36795
   208
  then obtain u v where u: "0 < u" and v: "0 < v" and "r = u + v"
huffman@36795
   209
    by (rule obtain_pos_sum)
huffman@36795
   210
  obtain a where a: "0 < a" "\<forall>n. \<bar>X n\<bar> < a"
huffman@36795
   211
    using cauchy_imp_bounded [OF X] by fast
huffman@36795
   212
  obtain b where b: "0 < b" "\<forall>n. \<bar>Y n\<bar> < b"
huffman@36795
   213
    using cauchy_imp_bounded [OF Y] by fast
huffman@36795
   214
  obtain s t where s: "0 < s" and t: "0 < t" and r: "r = a * t + s * b"
huffman@36795
   215
  proof
huffman@36795
   216
    show "0 < v/b" using v b(1) by (rule divide_pos_pos)
huffman@36795
   217
    show "0 < u/a" using u a(1) by (rule divide_pos_pos)
huffman@36795
   218
    show "r = a * (u/a) + (v/b) * b"
huffman@36795
   219
      using a(1) b(1) `r = u + v` by simp
huffman@36795
   220
  qed
huffman@36795
   221
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
huffman@36795
   222
    using cauchyD [OF X s] ..
huffman@36795
   223
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>Y m - Y n\<bar> < t"
huffman@36795
   224
    using cauchyD [OF Y t] ..
huffman@36795
   225
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>X m * Y m - X n * Y n\<bar> < r"
huffman@36795
   226
  proof (clarsimp)
huffman@36795
   227
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
huffman@36795
   228
    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>"
huffman@36795
   229
      unfolding mult_diff_mult ..
huffman@36795
   230
    also have "\<dots> \<le> \<bar>X m * (Y m - Y n)\<bar> + \<bar>(X m - X n) * Y n\<bar>"
huffman@36795
   231
      by (rule abs_triangle_ineq)
huffman@36795
   232
    also have "\<dots> = \<bar>X m\<bar> * \<bar>Y m - Y n\<bar> + \<bar>X m - X n\<bar> * \<bar>Y n\<bar>"
huffman@36795
   233
      unfolding abs_mult ..
huffman@36795
   234
    also have "\<dots> < a * t + s * b"
huffman@36795
   235
      by (simp_all add: add_strict_mono mult_strict_mono' a b i j *)
huffman@36795
   236
    finally show "\<bar>X m * Y m - X n * Y n\<bar> < r" unfolding r .
huffman@36795
   237
  qed
huffman@36795
   238
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>X m * Y m - X n * Y n\<bar> < r" ..
huffman@36795
   239
qed
huffman@36795
   240
huffman@36795
   241
lemma cauchy_not_vanishes_cases:
huffman@36795
   242
  assumes X: "cauchy X"
huffman@36795
   243
  assumes nz: "\<not> vanishes X"
huffman@36795
   244
  shows "\<exists>b>0. \<exists>k. (\<forall>n\<ge>k. b < - X n) \<or> (\<forall>n\<ge>k. b < X n)"
huffman@36795
   245
proof -
huffman@36795
   246
  obtain r where "0 < r" and r: "\<forall>k. \<exists>n\<ge>k. r \<le> \<bar>X n\<bar>"
huffman@36795
   247
    using nz unfolding vanishes_def by (auto simp add: not_less)
huffman@36795
   248
  obtain s t where s: "0 < s" and t: "0 < t" and "r = s + t"
huffman@36795
   249
    using `0 < r` by (rule obtain_pos_sum)
huffman@36795
   250
  obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>X m - X n\<bar> < s"
huffman@36795
   251
    using cauchyD [OF X s] ..
huffman@36795
   252
  obtain k where "i \<le> k" and "r \<le> \<bar>X k\<bar>"
huffman@36795
   253
    using r by fast
huffman@36795
   254
  have k: "\<forall>n\<ge>k. \<bar>X n - X k\<bar> < s"
huffman@36795
   255
    using i `i \<le> k` by auto
huffman@36795
   256
  have "X k \<le> - r \<or> r \<le> X k"
huffman@36795
   257
    using `r \<le> \<bar>X k\<bar>` by auto
huffman@36795
   258
  hence "(\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
huffman@36795
   259
    unfolding `r = s + t` using k by auto
huffman@36795
   260
  hence "\<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)" ..
huffman@36795
   261
  thus "\<exists>t>0. \<exists>k. (\<forall>n\<ge>k. t < - X n) \<or> (\<forall>n\<ge>k. t < X n)"
huffman@36795
   262
    using t by auto
huffman@36795
   263
qed
huffman@36795
   264
huffman@36795
   265
lemma cauchy_not_vanishes:
huffman@36795
   266
  assumes X: "cauchy X"
huffman@36795
   267
  assumes nz: "\<not> vanishes X"
huffman@36795
   268
  shows "\<exists>b>0. \<exists>k. \<forall>n\<ge>k. b < \<bar>X n\<bar>"
huffman@36795
   269
using cauchy_not_vanishes_cases [OF assms]
huffman@36795
   270
by clarify (rule exI, erule conjI, rule_tac x=k in exI, auto)
huffman@36795
   271
huffman@36795
   272
lemma cauchy_inverse [simp]:
huffman@36795
   273
  assumes X: "cauchy X"
huffman@36795
   274
  assumes nz: "\<not> vanishes X"
huffman@36795
   275
  shows "cauchy (\<lambda>n. inverse (X n))"
huffman@36795
   276
proof (rule cauchyI)
huffman@36795
   277
  fix r :: rat assume "0 < r"
huffman@36795
   278
  obtain b i where b: "0 < b" and i: "\<forall>n\<ge>i. b < \<bar>X n\<bar>"
huffman@36795
   279
    using cauchy_not_vanishes [OF X nz] by fast
huffman@36795
   280
  from b i have nz: "\<forall>n\<ge>i. X n \<noteq> 0" by auto
huffman@36795
   281
  obtain s where s: "0 < s" and r: "r = inverse b * s * inverse b"
huffman@36795
   282
  proof
huffman@36795
   283
    show "0 < b * r * b"
huffman@36795
   284
      by (simp add: `0 < r` b mult_pos_pos)
huffman@36795
   285
    show "r = inverse b * (b * r * b) * inverse b"
huffman@36795
   286
      using b by simp
huffman@36795
   287
  qed
huffman@36795
   288
  obtain j where j: "\<forall>m\<ge>j. \<forall>n\<ge>j. \<bar>X m - X n\<bar> < s"
huffman@36795
   289
    using cauchyD [OF X s] ..
huffman@36795
   290
  have "\<forall>m\<ge>max i j. \<forall>n\<ge>max i j. \<bar>inverse (X m) - inverse (X n)\<bar> < r"
huffman@36795
   291
  proof (clarsimp)
huffman@36795
   292
    fix m n assume *: "i \<le> m" "j \<le> m" "i \<le> n" "j \<le> n"
huffman@36795
   293
    have "\<bar>inverse (X m) - inverse (X n)\<bar> =
huffman@36795
   294
          inverse \<bar>X m\<bar> * \<bar>X m - X n\<bar> * inverse \<bar>X n\<bar>"
huffman@36795
   295
      by (simp add: inverse_diff_inverse nz * abs_mult)
huffman@36795
   296
    also have "\<dots> < inverse b * s * inverse b"
huffman@36795
   297
      by (simp add: mult_strict_mono less_imp_inverse_less
huffman@36795
   298
                    mult_pos_pos i j b * s)
huffman@36795
   299
    finally show "\<bar>inverse (X m) - inverse (X n)\<bar> < r" unfolding r .
huffman@36795
   300
  qed
huffman@36795
   301
  thus "\<exists>k. \<forall>m\<ge>k. \<forall>n\<ge>k. \<bar>inverse (X m) - inverse (X n)\<bar> < r" ..
huffman@36795
   302
qed
huffman@36795
   303
huffman@47901
   304
lemma vanishes_diff_inverse:
huffman@47901
   305
  assumes X: "cauchy X" "\<not> vanishes X"
huffman@47901
   306
  assumes Y: "cauchy Y" "\<not> vanishes Y"
huffman@47901
   307
  assumes XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@47901
   308
  shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
huffman@47901
   309
proof (rule vanishesI)
huffman@47901
   310
  fix r :: rat assume r: "0 < r"
huffman@47901
   311
  obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
huffman@47901
   312
    using cauchy_not_vanishes [OF X] by fast
huffman@47901
   313
  obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
huffman@47901
   314
    using cauchy_not_vanishes [OF Y] by fast
huffman@47901
   315
  obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
huffman@47901
   316
  proof
huffman@47901
   317
    show "0 < a * r * b"
huffman@47901
   318
      using a r b by (simp add: mult_pos_pos)
huffman@47901
   319
    show "inverse a * (a * r * b) * inverse b = r"
huffman@47901
   320
      using a r b by simp
huffman@47901
   321
  qed
huffman@47901
   322
  obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
huffman@47901
   323
    using vanishesD [OF XY s] ..
huffman@47901
   324
  have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
huffman@47901
   325
  proof (clarsimp)
huffman@47901
   326
    fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
huffman@47901
   327
    have "X n \<noteq> 0" and "Y n \<noteq> 0"
huffman@47901
   328
      using i j a b n by auto
huffman@47901
   329
    hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
huffman@47901
   330
        inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
huffman@47901
   331
      by (simp add: inverse_diff_inverse abs_mult)
huffman@47901
   332
    also have "\<dots> < inverse a * s * inverse b"
huffman@47901
   333
      apply (intro mult_strict_mono' less_imp_inverse_less)
huffman@47901
   334
      apply (simp_all add: a b i j k n mult_nonneg_nonneg)
huffman@47901
   335
      done
huffman@47901
   336
    also note `inverse a * s * inverse b = r`
huffman@47901
   337
    finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
huffman@47901
   338
  qed
huffman@47901
   339
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
huffman@47901
   340
qed
huffman@47901
   341
huffman@36795
   342
subsection {* Equivalence relation on Cauchy sequences *}
huffman@36795
   343
huffman@47902
   344
definition realrel :: "(nat \<Rightarrow> rat) \<Rightarrow> (nat \<Rightarrow> rat) \<Rightarrow> bool"
huffman@47902
   345
  where "realrel = (\<lambda>X Y. cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n))"
huffman@36795
   346
huffman@47902
   347
lemma realrelI [intro?]:
huffman@47902
   348
  assumes "cauchy X" and "cauchy Y" and "vanishes (\<lambda>n. X n - Y n)"
huffman@47902
   349
  shows "realrel X Y"
huffman@47902
   350
  using assms unfolding realrel_def by simp
huffman@36795
   351
huffman@47902
   352
lemma realrel_refl: "cauchy X \<Longrightarrow> realrel X X"
huffman@47902
   353
  unfolding realrel_def by simp
paulson@14484
   354
huffman@47902
   355
lemma symp_realrel: "symp realrel"
huffman@36795
   356
  unfolding realrel_def
huffman@47902
   357
  by (rule sympI, clarify, drule vanishes_minus, simp)
huffman@47902
   358
huffman@47902
   359
lemma transp_realrel: "transp realrel"
huffman@47902
   360
  unfolding realrel_def
huffman@47902
   361
  apply (rule transpI, clarify)
huffman@36795
   362
  apply (drule (1) vanishes_add)
huffman@36795
   363
  apply (simp add: algebra_simps)
huffman@36795
   364
  done
huffman@36795
   365
huffman@47902
   366
lemma part_equivp_realrel: "part_equivp realrel"
huffman@47902
   367
  by (fast intro: part_equivpI symp_realrel transp_realrel
huffman@47902
   368
    realrel_refl cauchy_const)
huffman@36795
   369
huffman@36795
   370
subsection {* The field of real numbers *}
huffman@36795
   371
huffman@47902
   372
quotient_type real = "nat \<Rightarrow> rat" / partial: realrel
huffman@47902
   373
  morphisms rep_real Real
huffman@47902
   374
  by (rule part_equivp_realrel)
huffman@36795
   375
kuncar@51375
   376
lemma cr_real_eq: "pcr_real = (\<lambda>x y. cauchy x \<and> Real x = y)"
kuncar@51375
   377
  unfolding real.pcr_cr_eq cr_real_def realrel_def by auto
huffman@36795
   378
huffman@47902
   379
lemma Real_induct [induct type: real]: (* TODO: generate automatically *)
huffman@47902
   380
  assumes "\<And>X. cauchy X \<Longrightarrow> P (Real X)" shows "P x"
huffman@47902
   381
proof (induct x)
huffman@47902
   382
  case (1 X)
huffman@47902
   383
  hence "cauchy X" by (simp add: realrel_def)
huffman@47902
   384
  thus "P (Real X)" by (rule assms)
huffman@47902
   385
qed
huffman@36795
   386
huffman@36795
   387
lemma eq_Real:
huffman@36795
   388
  "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
huffman@47902
   389
  using real.rel_eq_transfer
kuncar@51375
   390
  unfolding real.pcr_cr_eq cr_real_def fun_rel_def realrel_def by simp
huffman@47902
   391
huffman@47902
   392
declare real.forall_transfer [transfer_rule del]
huffman@36795
   393
huffman@47902
   394
lemma forall_real_transfer [transfer_rule]: (* TODO: generate automatically *)
kuncar@51375
   395
  "(fun_rel (fun_rel pcr_real op =) op =)
huffman@47902
   396
    (transfer_bforall cauchy) transfer_forall"
kuncar@51375
   397
  using real.forall_transfer
huffman@47902
   398
  by (simp add: realrel_def)
huffman@47902
   399
huffman@47902
   400
instantiation real :: field_inverse_zero
huffman@47902
   401
begin
huffman@47902
   402
huffman@47902
   403
lift_definition zero_real :: "real" is "\<lambda>n. 0"
huffman@47902
   404
  by (simp add: realrel_refl)
huffman@36795
   405
huffman@47902
   406
lift_definition one_real :: "real" is "\<lambda>n. 1"
huffman@47902
   407
  by (simp add: realrel_refl)
huffman@47902
   408
huffman@47902
   409
lift_definition plus_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n + Y n"
huffman@47902
   410
  unfolding realrel_def add_diff_add
huffman@47902
   411
  by (simp only: cauchy_add vanishes_add simp_thms)
huffman@36795
   412
huffman@47902
   413
lift_definition uminus_real :: "real \<Rightarrow> real" is "\<lambda>X n. - X n"
huffman@47902
   414
  unfolding realrel_def minus_diff_minus
huffman@47902
   415
  by (simp only: cauchy_minus vanishes_minus simp_thms)
huffman@36795
   416
huffman@47902
   417
lift_definition times_real :: "real \<Rightarrow> real \<Rightarrow> real" is "\<lambda>X Y n. X n * Y n"
huffman@47902
   418
  unfolding realrel_def mult_diff_mult
huffman@47902
   419
  by (subst (4) mult_commute, simp only: cauchy_mult vanishes_add
huffman@47902
   420
    vanishes_mult_bounded cauchy_imp_bounded simp_thms)
huffman@47902
   421
huffman@47902
   422
lift_definition inverse_real :: "real \<Rightarrow> real"
huffman@47902
   423
  is "\<lambda>X. if vanishes X then (\<lambda>n. 0) else (\<lambda>n. inverse (X n))"
huffman@47902
   424
proof -
huffman@47902
   425
  fix X Y assume "realrel X Y"
huffman@36795
   426
  hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   427
    unfolding realrel_def by simp_all
huffman@36795
   428
  have "vanishes X \<longleftrightarrow> vanishes Y"
huffman@36795
   429
  proof
huffman@36795
   430
    assume "vanishes X"
huffman@36795
   431
    from vanishes_diff [OF this XY] show "vanishes Y" by simp
huffman@36795
   432
  next
huffman@36795
   433
    assume "vanishes Y"
huffman@36795
   434
    from vanishes_add [OF this XY] show "vanishes X" by simp
huffman@36795
   435
  qed
huffman@47902
   436
  thus "?thesis X Y"
huffman@47902
   437
    unfolding realrel_def
huffman@47902
   438
    by (simp add: vanishes_diff_inverse X Y XY)
huffman@36795
   439
qed
huffman@36795
   440
huffman@36795
   441
definition
huffman@36795
   442
  "x - y = (x::real) + - y"
bauerg@10606
   443
haftmann@25571
   444
definition
huffman@36795
   445
  "x / y = (x::real) * inverse y"
huffman@36795
   446
huffman@36795
   447
lemma add_Real:
huffman@36795
   448
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   449
  shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
huffman@47902
   450
  using assms plus_real.transfer
huffman@47902
   451
  unfolding cr_real_eq fun_rel_def by simp
huffman@36795
   452
huffman@36795
   453
lemma minus_Real:
huffman@36795
   454
  assumes X: "cauchy X"
huffman@36795
   455
  shows "- Real X = Real (\<lambda>n. - X n)"
huffman@47902
   456
  using assms uminus_real.transfer
huffman@47902
   457
  unfolding cr_real_eq fun_rel_def by simp
paulson@5588
   458
huffman@36795
   459
lemma diff_Real:
huffman@36795
   460
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   461
  shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
huffman@36795
   462
  unfolding minus_real_def diff_minus
huffman@36795
   463
  by (simp add: minus_Real add_Real X Y)
haftmann@25571
   464
huffman@36795
   465
lemma mult_Real:
huffman@36795
   466
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   467
  shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
huffman@47902
   468
  using assms times_real.transfer
huffman@47902
   469
  unfolding cr_real_eq fun_rel_def by simp
huffman@36795
   470
huffman@36795
   471
lemma inverse_Real:
huffman@36795
   472
  assumes X: "cauchy X"
huffman@36795
   473
  shows "inverse (Real X) =
huffman@36795
   474
    (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
huffman@47902
   475
  using assms inverse_real.transfer zero_real.transfer
huffman@47902
   476
  unfolding cr_real_eq fun_rel_def by (simp split: split_if_asm, metis)
paulson@14269
   477
huffman@36795
   478
instance proof
huffman@36795
   479
  fix a b c :: real
huffman@36795
   480
  show "a + b = b + a"
huffman@47902
   481
    by transfer (simp add: add_ac realrel_def)
huffman@36795
   482
  show "(a + b) + c = a + (b + c)"
huffman@47902
   483
    by transfer (simp add: add_ac realrel_def)
huffman@36795
   484
  show "0 + a = a"
huffman@47902
   485
    by transfer (simp add: realrel_def)
huffman@36795
   486
  show "- a + a = 0"
huffman@47902
   487
    by transfer (simp add: realrel_def)
huffman@36795
   488
  show "a - b = a + - b"
huffman@36795
   489
    by (rule minus_real_def)
huffman@36795
   490
  show "(a * b) * c = a * (b * c)"
huffman@47902
   491
    by transfer (simp add: mult_ac realrel_def)
huffman@36795
   492
  show "a * b = b * a"
huffman@47902
   493
    by transfer (simp add: mult_ac realrel_def)
huffman@36795
   494
  show "1 * a = a"
huffman@47902
   495
    by transfer (simp add: mult_ac realrel_def)
huffman@36795
   496
  show "(a + b) * c = a * c + b * c"
webertj@49962
   497
    by transfer (simp add: distrib_right realrel_def)
huffman@36795
   498
  show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
huffman@47902
   499
    by transfer (simp add: realrel_def)
huffman@36795
   500
  show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
huffman@47902
   501
    apply transfer
huffman@47902
   502
    apply (simp add: realrel_def)
huffman@36795
   503
    apply (rule vanishesI)
huffman@36795
   504
    apply (frule (1) cauchy_not_vanishes, clarify)
huffman@36795
   505
    apply (rule_tac x=k in exI, clarify)
huffman@36795
   506
    apply (drule_tac x=n in spec, simp)
huffman@36795
   507
    done
huffman@36795
   508
  show "a / b = a * inverse b"
huffman@36795
   509
    by (rule divide_real_def)
huffman@36795
   510
  show "inverse (0::real) = 0"
huffman@47902
   511
    by transfer (simp add: realrel_def)
huffman@36795
   512
qed
haftmann@25571
   513
haftmann@25571
   514
end
paulson@14334
   515
huffman@36795
   516
subsection {* Positive reals *}
paulson@14269
   517
huffman@47902
   518
lift_definition positive :: "real \<Rightarrow> bool"
huffman@47902
   519
  is "\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
huffman@47902
   520
proof -
huffman@47902
   521
  { fix X Y
huffman@47902
   522
    assume "realrel X Y"
huffman@47902
   523
    hence XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@47902
   524
      unfolding realrel_def by simp_all
huffman@47902
   525
    assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
huffman@47902
   526
    then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
huffman@47902
   527
      by fast
huffman@47902
   528
    obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@47902
   529
      using `0 < r` by (rule obtain_pos_sum)
huffman@47902
   530
    obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
huffman@47902
   531
      using vanishesD [OF XY s] ..
huffman@47902
   532
    have "\<forall>n\<ge>max i j. t < Y n"
huffman@47902
   533
    proof (clarsimp)
huffman@47902
   534
      fix n assume n: "i \<le> n" "j \<le> n"
huffman@47902
   535
      have "\<bar>X n - Y n\<bar> < s" and "r < X n"
huffman@47902
   536
        using i j n by simp_all
huffman@47902
   537
      thus "t < Y n" unfolding r by simp
huffman@47902
   538
    qed
huffman@47902
   539
    hence "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
huffman@47902
   540
  } note 1 = this
huffman@47902
   541
  fix X Y assume "realrel X Y"
huffman@47902
   542
  hence "realrel X Y" and "realrel Y X"
huffman@47902
   543
    using symp_realrel unfolding symp_def by auto
huffman@47902
   544
  thus "?thesis X Y"
huffman@47902
   545
    by (safe elim!: 1)
paulson@14484
   546
qed
paulson@14269
   547
huffman@36795
   548
lemma positive_Real:
huffman@36795
   549
  assumes X: "cauchy X"
huffman@36795
   550
  shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
huffman@47902
   551
  using assms positive.transfer
huffman@47902
   552
  unfolding cr_real_eq fun_rel_def by simp
huffman@23287
   553
huffman@36795
   554
lemma positive_zero: "\<not> positive 0"
huffman@47902
   555
  by transfer auto
paulson@14269
   556
huffman@36795
   557
lemma positive_add:
huffman@36795
   558
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
huffman@47902
   559
apply transfer
huffman@36795
   560
apply (clarify, rename_tac a b i j)
huffman@36795
   561
apply (rule_tac x="a + b" in exI, simp)
huffman@36795
   562
apply (rule_tac x="max i j" in exI, clarsimp)
huffman@36795
   563
apply (simp add: add_strict_mono)
paulson@14269
   564
done
paulson@14269
   565
huffman@36795
   566
lemma positive_mult:
huffman@36795
   567
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
huffman@47902
   568
apply transfer
huffman@36795
   569
apply (clarify, rename_tac a b i j)
huffman@36795
   570
apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
huffman@36795
   571
apply (rule_tac x="max i j" in exI, clarsimp)
huffman@36795
   572
apply (rule mult_strict_mono, auto)
huffman@36795
   573
done
huffman@36795
   574
huffman@36795
   575
lemma positive_minus:
huffman@36795
   576
  "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
huffman@47902
   577
apply transfer
huffman@47902
   578
apply (simp add: realrel_def)
huffman@36795
   579
apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
paulson@14269
   580
done
paulson@14334
   581
huffman@36795
   582
instantiation real :: linordered_field_inverse_zero
huffman@36795
   583
begin
paulson@14341
   584
huffman@36795
   585
definition
huffman@36795
   586
  "x < y \<longleftrightarrow> positive (y - x)"
paulson@14341
   587
huffman@36795
   588
definition
huffman@36795
   589
  "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
paulson@14334
   590
huffman@36795
   591
definition
huffman@36795
   592
  "abs (a::real) = (if a < 0 then - a else a)"
paulson@14269
   593
huffman@36795
   594
definition
huffman@36795
   595
  "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
paulson@14269
   596
huffman@36795
   597
instance proof
huffman@36795
   598
  fix a b c :: real
huffman@36795
   599
  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
huffman@36795
   600
    by (rule abs_real_def)
huffman@36795
   601
  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
huffman@36795
   602
    unfolding less_eq_real_def less_real_def
huffman@36795
   603
    by (auto, drule (1) positive_add, simp_all add: positive_zero)
huffman@36795
   604
  show "a \<le> a"
huffman@36795
   605
    unfolding less_eq_real_def by simp
huffman@36795
   606
  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
huffman@36795
   607
    unfolding less_eq_real_def less_real_def
huffman@36795
   608
    by (auto, drule (1) positive_add, simp add: algebra_simps)
huffman@36795
   609
  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
huffman@36795
   610
    unfolding less_eq_real_def less_real_def
huffman@36795
   611
    by (auto, drule (1) positive_add, simp add: positive_zero)
huffman@36795
   612
  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
huffman@47108
   613
    unfolding less_eq_real_def less_real_def by (auto simp: diff_minus) (* by auto *)
huffman@47108
   614
    (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
huffman@47108
   615
    (* Should produce c + b - (c + a) \<equiv> b - a *)
huffman@36795
   616
  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
huffman@36795
   617
    by (rule sgn_real_def)
huffman@36795
   618
  show "a \<le> b \<or> b \<le> a"
huffman@36795
   619
    unfolding less_eq_real_def less_real_def
huffman@36795
   620
    by (auto dest!: positive_minus)
huffman@36795
   621
  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
huffman@36795
   622
    unfolding less_real_def
huffman@36795
   623
    by (drule (1) positive_mult, simp add: algebra_simps)
huffman@23288
   624
qed
paulson@14378
   625
huffman@36795
   626
end
paulson@14334
   627
haftmann@25571
   628
instantiation real :: distrib_lattice
haftmann@25571
   629
begin
haftmann@25571
   630
haftmann@25571
   631
definition
huffman@36795
   632
  "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
haftmann@25571
   633
haftmann@25571
   634
definition
huffman@36795
   635
  "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
huffman@36795
   636
huffman@36795
   637
instance proof
huffman@36795
   638
qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
huffman@36795
   639
huffman@36795
   640
end
haftmann@25571
   641
huffman@36795
   642
lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
huffman@36795
   643
apply (induct x)
huffman@36795
   644
apply (simp add: zero_real_def)
huffman@36795
   645
apply (simp add: one_real_def add_Real)
huffman@36795
   646
done
paulson@14378
   647
huffman@36795
   648
lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
huffman@36795
   649
apply (cases x rule: int_diff_cases)
huffman@36795
   650
apply (simp add: of_nat_Real diff_Real)
huffman@36795
   651
done
paulson@14334
   652
huffman@36795
   653
lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
huffman@36795
   654
apply (induct x)
huffman@36795
   655
apply (simp add: Fract_of_int_quotient of_rat_divide)
huffman@36795
   656
apply (simp add: of_int_Real divide_inverse)
huffman@36795
   657
apply (simp add: inverse_Real mult_Real)
huffman@36795
   658
done
huffman@36795
   659
huffman@36795
   660
instance real :: archimedean_field
paulson@14334
   661
proof
huffman@36795
   662
  fix x :: real
huffman@36795
   663
  show "\<exists>z. x \<le> of_int z"
huffman@36795
   664
    apply (induct x)
huffman@36795
   665
    apply (frule cauchy_imp_bounded, clarify)
huffman@36795
   666
    apply (rule_tac x="ceiling b + 1" in exI)
huffman@36795
   667
    apply (rule less_imp_le)
huffman@36795
   668
    apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
huffman@36795
   669
    apply (rule_tac x=1 in exI, simp add: algebra_simps)
huffman@36795
   670
    apply (rule_tac x=0 in exI, clarsimp)
huffman@36795
   671
    apply (rule le_less_trans [OF abs_ge_self])
huffman@36795
   672
    apply (rule less_le_trans [OF _ le_of_int_ceiling])
huffman@36795
   673
    apply simp
huffman@36795
   674
    done
paulson@14334
   675
qed
paulson@14334
   676
bulwahn@43732
   677
instantiation real :: floor_ceiling
bulwahn@43732
   678
begin
bulwahn@43732
   679
bulwahn@43732
   680
definition [code del]:
bulwahn@43732
   681
  "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
bulwahn@43732
   682
bulwahn@43732
   683
instance proof
bulwahn@43732
   684
  fix x :: real
bulwahn@43732
   685
  show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
bulwahn@43732
   686
    unfolding floor_real_def using floor_exists1 by (rule theI')
bulwahn@43732
   687
qed
bulwahn@43732
   688
bulwahn@43732
   689
end
bulwahn@43732
   690
huffman@36795
   691
subsection {* Completeness *}
paulson@14365
   692
huffman@36795
   693
lemma not_positive_Real:
huffman@36795
   694
  assumes X: "cauchy X"
huffman@36795
   695
  shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
huffman@36795
   696
unfolding positive_Real [OF X]
huffman@36795
   697
apply (auto, unfold not_less)
huffman@36795
   698
apply (erule obtain_pos_sum)
huffman@36795
   699
apply (drule_tac x=s in spec, simp)
huffman@36795
   700
apply (drule_tac r=t in cauchyD [OF X], clarify)
huffman@36795
   701
apply (drule_tac x=k in spec, clarsimp)
huffman@36795
   702
apply (rule_tac x=n in exI, clarify, rename_tac m)
huffman@36795
   703
apply (drule_tac x=m in spec, simp)
huffman@36795
   704
apply (drule_tac x=n in spec, simp)
huffman@36795
   705
apply (drule spec, drule (1) mp, clarify, rename_tac i)
huffman@36795
   706
apply (rule_tac x="max i k" in exI, simp)
huffman@36795
   707
done
huffman@36795
   708
huffman@36795
   709
lemma le_Real:
huffman@36795
   710
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   711
  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
huffman@36795
   712
unfolding not_less [symmetric, where 'a=real] less_real_def
huffman@36795
   713
apply (simp add: diff_Real not_positive_Real X Y)
huffman@36795
   714
apply (simp add: diff_le_eq add_ac)
huffman@36795
   715
done
paulson@14365
   716
huffman@36795
   717
lemma le_RealI:
huffman@36795
   718
  assumes Y: "cauchy Y"
huffman@36795
   719
  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
huffman@36795
   720
proof (induct x)
huffman@36795
   721
  fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
huffman@36795
   722
  hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
huffman@36795
   723
    by (simp add: of_rat_Real le_Real)
huffman@36795
   724
  {
huffman@36795
   725
    fix r :: rat assume "0 < r"
huffman@36795
   726
    then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
   727
      by (rule obtain_pos_sum)
huffman@36795
   728
    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
huffman@36795
   729
      using cauchyD [OF Y s] ..
huffman@36795
   730
    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
huffman@36795
   731
      using le [OF t] ..
huffman@36795
   732
    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
huffman@36795
   733
    proof (clarsimp)
huffman@36795
   734
      fix n assume n: "i \<le> n" "j \<le> n"
huffman@36795
   735
      have "X n \<le> Y i + t" using n j by simp
huffman@36795
   736
      moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
huffman@36795
   737
      ultimately show "X n \<le> Y n + r" unfolding r by simp
huffman@36795
   738
    qed
huffman@36795
   739
    hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
huffman@36795
   740
  }
huffman@36795
   741
  thus "Real X \<le> Real Y"
huffman@36795
   742
    by (simp add: of_rat_Real le_Real X Y)
huffman@36795
   743
qed
paulson@14365
   744
huffman@36795
   745
lemma Real_leI:
huffman@36795
   746
  assumes X: "cauchy X"
huffman@36795
   747
  assumes le: "\<forall>n. of_rat (X n) \<le> y"
huffman@36795
   748
  shows "Real X \<le> y"
huffman@36795
   749
proof -
huffman@36795
   750
  have "- y \<le> - Real X"
huffman@36795
   751
    by (simp add: minus_Real X le_RealI of_rat_minus le)
huffman@36795
   752
  thus ?thesis by simp
huffman@36795
   753
qed
huffman@36795
   754
huffman@36795
   755
lemma less_RealD:
huffman@36795
   756
  assumes Y: "cauchy Y"
huffman@36795
   757
  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
huffman@36795
   758
by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
huffman@36795
   759
huffman@36795
   760
lemma of_nat_less_two_power:
huffman@47108
   761
  "of_nat n < (2::'a::linordered_idom) ^ n"
huffman@36795
   762
apply (induct n)
huffman@36795
   763
apply simp
huffman@36795
   764
apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
huffman@36795
   765
apply (drule (1) add_le_less_mono, simp)
huffman@36795
   766
apply simp
paulson@14365
   767
done
paulson@14365
   768
huffman@36795
   769
lemma complete_real:
huffman@36795
   770
  fixes S :: "real set"
huffman@36795
   771
  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
huffman@36795
   772
  shows "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
huffman@36795
   773
proof -
huffman@36795
   774
  obtain x where x: "x \<in> S" using assms(1) ..
huffman@36795
   775
  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
paulson@14365
   776
huffman@36795
   777
  def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
huffman@36795
   778
  obtain a where a: "\<not> P a"
huffman@36795
   779
  proof
huffman@36795
   780
    have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
huffman@36795
   781
    also have "x - 1 < x" by simp
huffman@36795
   782
    finally have "of_int (floor (x - 1)) < x" .
huffman@36795
   783
    hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
huffman@36795
   784
    then show "\<not> P (of_int (floor (x - 1)))"
huffman@36795
   785
      unfolding P_def of_rat_of_int_eq using x by fast
huffman@36795
   786
  qed
huffman@36795
   787
  obtain b where b: "P b"
huffman@36795
   788
  proof
huffman@36795
   789
    show "P (of_int (ceiling z))"
huffman@36795
   790
    unfolding P_def of_rat_of_int_eq
huffman@36795
   791
    proof
huffman@36795
   792
      fix y assume "y \<in> S"
huffman@36795
   793
      hence "y \<le> z" using z by simp
huffman@36795
   794
      also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
huffman@36795
   795
      finally show "y \<le> of_int (ceiling z)" .
huffman@36795
   796
    qed
huffman@36795
   797
  qed
paulson@14365
   798
huffman@36795
   799
  def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
huffman@36795
   800
  def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
huffman@36795
   801
  def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
huffman@36795
   802
  def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
huffman@36795
   803
  def C \<equiv> "\<lambda>n. avg (A n) (B n)"
huffman@36795
   804
  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
huffman@36795
   805
  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
huffman@36795
   806
  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
huffman@36795
   807
    unfolding A_def B_def C_def bisect_def split_def by simp
huffman@36795
   808
  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
huffman@36795
   809
    unfolding A_def B_def C_def bisect_def split_def by simp
paulson@14365
   810
huffman@36795
   811
  have width: "\<And>n. B n - A n = (b - a) / 2^n"
huffman@36795
   812
    apply (simp add: eq_divide_eq)
huffman@36795
   813
    apply (induct_tac n, simp)
huffman@36795
   814
    apply (simp add: C_def avg_def algebra_simps)
huffman@36795
   815
    done
huffman@36795
   816
huffman@36795
   817
  have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
huffman@36795
   818
    apply (simp add: divide_less_eq)
huffman@36795
   819
    apply (subst mult_commute)
huffman@36795
   820
    apply (frule_tac y=y in ex_less_of_nat_mult)
huffman@36795
   821
    apply clarify
huffman@36795
   822
    apply (rule_tac x=n in exI)
huffman@36795
   823
    apply (erule less_trans)
huffman@36795
   824
    apply (rule mult_strict_right_mono)
huffman@36795
   825
    apply (rule le_less_trans [OF _ of_nat_less_two_power])
huffman@36795
   826
    apply simp
huffman@36795
   827
    apply assumption
huffman@36795
   828
    done
paulson@14365
   829
huffman@36795
   830
  have PA: "\<And>n. \<not> P (A n)"
huffman@36795
   831
    by (induct_tac n, simp_all add: a)
huffman@36795
   832
  have PB: "\<And>n. P (B n)"
huffman@36795
   833
    by (induct_tac n, simp_all add: b)
huffman@36795
   834
  have ab: "a < b"
huffman@36795
   835
    using a b unfolding P_def
huffman@36795
   836
    apply (clarsimp simp add: not_le)
huffman@36795
   837
    apply (drule (1) bspec)
huffman@36795
   838
    apply (drule (1) less_le_trans)
huffman@36795
   839
    apply (simp add: of_rat_less)
huffman@36795
   840
    done
huffman@36795
   841
  have AB: "\<And>n. A n < B n"
huffman@36795
   842
    by (induct_tac n, simp add: ab, simp add: C_def avg_def)
huffman@36795
   843
  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
huffman@36795
   844
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   845
    apply (erule less_Suc_induct)
huffman@36795
   846
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   847
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   848
    apply (rule AB [THEN less_imp_le])
huffman@36795
   849
    apply simp
huffman@36795
   850
    done
huffman@36795
   851
  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
huffman@36795
   852
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   853
    apply (erule less_Suc_induct)
huffman@36795
   854
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   855
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   856
    apply (rule AB [THEN less_imp_le])
huffman@36795
   857
    apply simp
huffman@36795
   858
    done
huffman@36795
   859
  have cauchy_lemma:
huffman@36795
   860
    "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
huffman@36795
   861
    apply (rule cauchyI)
huffman@36795
   862
    apply (drule twos [where y="b - a"])
huffman@36795
   863
    apply (erule exE)
huffman@36795
   864
    apply (rule_tac x=n in exI, clarify, rename_tac i j)
huffman@36795
   865
    apply (rule_tac y="B n - A n" in le_less_trans) defer
huffman@36795
   866
    apply (simp add: width)
huffman@36795
   867
    apply (drule_tac x=n in spec)
huffman@36795
   868
    apply (frule_tac x=i in spec, drule (1) mp)
huffman@36795
   869
    apply (frule_tac x=j in spec, drule (1) mp)
huffman@36795
   870
    apply (frule A_mono, drule B_mono)
huffman@36795
   871
    apply (frule A_mono, drule B_mono)
huffman@36795
   872
    apply arith
huffman@36795
   873
    done
huffman@36795
   874
  have "cauchy A"
huffman@36795
   875
    apply (rule cauchy_lemma [rule_format])
huffman@36795
   876
    apply (simp add: A_mono)
huffman@36795
   877
    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
huffman@36795
   878
    done
huffman@36795
   879
  have "cauchy B"
huffman@36795
   880
    apply (rule cauchy_lemma [rule_format])
huffman@36795
   881
    apply (simp add: B_mono)
huffman@36795
   882
    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
huffman@36795
   883
    done
huffman@36795
   884
  have 1: "\<forall>x\<in>S. x \<le> Real B"
huffman@36795
   885
  proof
huffman@36795
   886
    fix x assume "x \<in> S"
huffman@36795
   887
    then show "x \<le> Real B"
huffman@36795
   888
      using PB [unfolded P_def] `cauchy B`
huffman@36795
   889
      by (simp add: le_RealI)
huffman@36795
   890
  qed
huffman@36795
   891
  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
huffman@36795
   892
    apply clarify
huffman@36795
   893
    apply (erule contrapos_pp)
huffman@36795
   894
    apply (simp add: not_le)
huffman@36795
   895
    apply (drule less_RealD [OF `cauchy A`], clarify)
huffman@36795
   896
    apply (subgoal_tac "\<not> P (A n)")
huffman@36795
   897
    apply (simp add: P_def not_le, clarify)
huffman@36795
   898
    apply (erule rev_bexI)
huffman@36795
   899
    apply (erule (1) less_trans)
huffman@36795
   900
    apply (simp add: PA)
huffman@36795
   901
    done
huffman@36795
   902
  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
huffman@36795
   903
  proof (rule vanishesI)
huffman@36795
   904
    fix r :: rat assume "0 < r"
huffman@36795
   905
    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
huffman@36795
   906
      using twos by fast
huffman@36795
   907
    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
huffman@36795
   908
    proof (clarify)
huffman@36795
   909
      fix n assume n: "k \<le> n"
huffman@36795
   910
      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
huffman@36795
   911
        by simp
huffman@36795
   912
      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
huffman@36795
   913
        using n by (simp add: divide_left_mono mult_pos_pos)
huffman@36795
   914
      also note k
huffman@36795
   915
      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
huffman@36795
   916
    qed
huffman@36795
   917
    thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
huffman@36795
   918
  qed
huffman@36795
   919
  hence 3: "Real B = Real A"
huffman@36795
   920
    by (simp add: eq_Real `cauchy A` `cauchy B` width)
huffman@36795
   921
  show "\<exists>y. (\<forall>x\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> y \<le> z)"
huffman@36795
   922
    using 1 2 3 by (rule_tac x="Real B" in exI, simp)
paulson@14484
   923
qed
paulson@14365
   924
huffman@36795
   925
subsection {* Hiding implementation details *}
paulson@14365
   926
huffman@47902
   927
hide_const (open) vanishes cauchy positive Real
paulson@14365
   928
huffman@36795
   929
declare Real_induct [induct del]
huffman@36795
   930
declare Abs_real_induct [induct del]
huffman@36795
   931
declare Abs_real_cases [cases del]
huffman@36795
   932
huffman@47902
   933
lemmas [transfer_rule del] =
huffman@47902
   934
  real.All_transfer real.Ex_transfer real.rel_eq_transfer forall_real_transfer
huffman@47902
   935
  zero_real.transfer one_real.transfer plus_real.transfer uminus_real.transfer
kuncar@51185
   936
  times_real.transfer inverse_real.transfer positive.transfer real.right_unique
kuncar@51185
   937
  real.right_total
huffman@47902
   938
paulson@14334
   939
subsection{*More Lemmas*}
paulson@14334
   940
huffman@36776
   941
text {* BH: These lemmas should not be necessary; they should be
huffman@36776
   942
covered by existing simp rules and simplification procedures. *}
huffman@36776
   943
paulson@14334
   944
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
huffman@36776
   945
by simp (* redundant with mult_cancel_left *)
paulson@14334
   946
paulson@14334
   947
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
huffman@36776
   948
by simp (* redundant with mult_cancel_right *)
paulson@14334
   949
paulson@14334
   950
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
huffman@36776
   951
by simp (* solved by linordered_ring_less_cancel_factor simproc *)
paulson@14334
   952
paulson@14334
   953
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
huffman@36776
   954
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
paulson@14334
   955
paulson@14334
   956
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
huffman@47428
   957
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
paulson@14334
   958
paulson@14334
   959
haftmann@24198
   960
subsection {* Embedding numbers into the Reals *}
haftmann@24198
   961
haftmann@24198
   962
abbreviation
haftmann@24198
   963
  real_of_nat :: "nat \<Rightarrow> real"
haftmann@24198
   964
where
haftmann@24198
   965
  "real_of_nat \<equiv> of_nat"
haftmann@24198
   966
haftmann@24198
   967
abbreviation
haftmann@24198
   968
  real_of_int :: "int \<Rightarrow> real"
haftmann@24198
   969
where
haftmann@24198
   970
  "real_of_int \<equiv> of_int"
haftmann@24198
   971
haftmann@24198
   972
abbreviation
haftmann@24198
   973
  real_of_rat :: "rat \<Rightarrow> real"
haftmann@24198
   974
where
haftmann@24198
   975
  "real_of_rat \<equiv> of_rat"
haftmann@24198
   976
haftmann@24198
   977
consts
haftmann@24198
   978
  (*overloaded constant for injecting other types into "real"*)
haftmann@24198
   979
  real :: "'a => real"
paulson@14365
   980
paulson@14378
   981
defs (overloaded)
haftmann@31998
   982
  real_of_nat_def [code_unfold]: "real == real_of_nat"
haftmann@31998
   983
  real_of_int_def [code_unfold]: "real == real_of_int"
paulson@14365
   984
wenzelm@40939
   985
declare [[coercion_enabled]]
nipkow@40864
   986
declare [[coercion "real::nat\<Rightarrow>real"]]
nipkow@40864
   987
declare [[coercion "real::int\<Rightarrow>real"]]
nipkow@41022
   988
declare [[coercion "int"]]
nipkow@40864
   989
hoelzl@41024
   990
declare [[coercion_map map]]
noschinl@42112
   991
declare [[coercion_map "% f g h x. g (h (f x))"]]
hoelzl@41024
   992
declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
hoelzl@41024
   993
avigad@16819
   994
lemma real_eq_of_nat: "real = of_nat"
haftmann@24198
   995
  unfolding real_of_nat_def ..
avigad@16819
   996
avigad@16819
   997
lemma real_eq_of_int: "real = of_int"
haftmann@24198
   998
  unfolding real_of_int_def ..
avigad@16819
   999
paulson@14365
  1000
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
  1001
by (simp add: real_of_int_def) 
paulson@14365
  1002
paulson@14365
  1003
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
  1004
by (simp add: real_of_int_def) 
paulson@14334
  1005
avigad@16819
  1006
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
  1007
by (simp add: real_of_int_def) 
paulson@14365
  1008
avigad@16819
  1009
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
  1010
by (simp add: real_of_int_def) 
avigad@16819
  1011
avigad@16819
  1012
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
  1013
by (simp add: real_of_int_def) 
paulson@14365
  1014
avigad@16819
  1015
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
  1016
by (simp add: real_of_int_def) 
paulson@14334
  1017
huffman@35344
  1018
lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
huffman@35344
  1019
by (simp add: real_of_int_def of_int_power)
huffman@35344
  1020
huffman@35344
  1021
lemmas power_real_of_int = real_of_int_power [symmetric]
huffman@35344
  1022
avigad@16819
  1023
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
  1024
  apply (subst real_eq_of_int)+
avigad@16819
  1025
  apply (rule of_int_setsum)
avigad@16819
  1026
done
avigad@16819
  1027
avigad@16819
  1028
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
  1029
    (PROD x:A. real(f x))"
avigad@16819
  1030
  apply (subst real_eq_of_int)+
avigad@16819
  1031
  apply (rule of_int_setprod)
avigad@16819
  1032
done
paulson@14365
  1033
chaieb@27668
  1034
lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
paulson@14378
  1035
by (simp add: real_of_int_def) 
paulson@14365
  1036
chaieb@27668
  1037
lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
paulson@14378
  1038
by (simp add: real_of_int_def) 
paulson@14365
  1039
chaieb@27668
  1040
lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
paulson@14378
  1041
by (simp add: real_of_int_def) 
paulson@14365
  1042
chaieb@27668
  1043
lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
  1044
by (simp add: real_of_int_def) 
paulson@14365
  1045
chaieb@27668
  1046
lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
  1047
by (simp add: real_of_int_def) 
avigad@16819
  1048
chaieb@27668
  1049
lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
  1050
by (simp add: real_of_int_def) 
avigad@16819
  1051
chaieb@27668
  1052
lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
avigad@16819
  1053
by (simp add: real_of_int_def)
avigad@16819
  1054
chaieb@27668
  1055
lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
  1056
by (simp add: real_of_int_def)
avigad@16819
  1057
hoelzl@47597
  1058
lemma one_less_real_of_int_cancel_iff: "1 < real (i :: int) \<longleftrightarrow> 1 < i"
hoelzl@47597
  1059
  unfolding real_of_one[symmetric] real_of_int_less_iff ..
hoelzl@47597
  1060
hoelzl@47597
  1061
lemma one_le_real_of_int_cancel_iff: "1 \<le> real (i :: int) \<longleftrightarrow> 1 \<le> i"
hoelzl@47597
  1062
  unfolding real_of_one[symmetric] real_of_int_le_iff ..
hoelzl@47597
  1063
hoelzl@47597
  1064
lemma real_of_int_less_one_cancel_iff: "real (i :: int) < 1 \<longleftrightarrow> i < 1"
hoelzl@47597
  1065
  unfolding real_of_one[symmetric] real_of_int_less_iff ..
hoelzl@47597
  1066
hoelzl@47597
  1067
lemma real_of_int_le_one_cancel_iff: "real (i :: int) \<le> 1 \<longleftrightarrow> i \<le> 1"
hoelzl@47597
  1068
  unfolding real_of_one[symmetric] real_of_int_le_iff ..
hoelzl@47597
  1069
avigad@16888
  1070
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
  1071
by (auto simp add: abs_if)
avigad@16888
  1072
avigad@16819
  1073
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
  1074
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
  1075
  apply (simp del: real_of_int_add)
avigad@16819
  1076
  apply auto
avigad@16819
  1077
done
avigad@16819
  1078
avigad@16819
  1079
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
  1080
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
  1081
  apply (simp del: real_of_int_add)
avigad@16819
  1082
  apply simp
avigad@16819
  1083
done
avigad@16819
  1084
bulwahn@46670
  1085
lemma real_of_int_div_aux: "(real (x::int)) / (real d) = 
avigad@16819
  1086
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1087
proof -
avigad@16819
  1088
  have "x = (x div d) * d + x mod d"
avigad@16819
  1089
    by auto
avigad@16819
  1090
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1091
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
  1092
  then have "real x / real d = ... / real d"
avigad@16819
  1093
    by simp
avigad@16819
  1094
  then show ?thesis
bulwahn@46670
  1095
    by (auto simp add: add_divide_distrib algebra_simps)
avigad@16819
  1096
qed
avigad@16819
  1097
bulwahn@46670
  1098
lemma real_of_int_div: "(d :: int) dvd n ==>
avigad@16819
  1099
    real(n div d) = real n / real d"
bulwahn@46670
  1100
  apply (subst real_of_int_div_aux)
avigad@16819
  1101
  apply simp
nipkow@30042
  1102
  apply (simp add: dvd_eq_mod_eq_0)
avigad@16819
  1103
done
avigad@16819
  1104
avigad@16819
  1105
lemma real_of_int_div2:
avigad@16819
  1106
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
  1107
  apply (case_tac "x = 0")
avigad@16819
  1108
  apply simp
avigad@16819
  1109
  apply (case_tac "0 < x")
nipkow@29667
  1110
  apply (simp add: algebra_simps)
avigad@16819
  1111
  apply (subst real_of_int_div_aux)
avigad@16819
  1112
  apply simp
avigad@16819
  1113
  apply (subst zero_le_divide_iff)
avigad@16819
  1114
  apply auto
nipkow@29667
  1115
  apply (simp add: algebra_simps)
avigad@16819
  1116
  apply (subst real_of_int_div_aux)
avigad@16819
  1117
  apply simp
avigad@16819
  1118
  apply (subst zero_le_divide_iff)
avigad@16819
  1119
  apply auto
avigad@16819
  1120
done
avigad@16819
  1121
avigad@16819
  1122
lemma real_of_int_div3:
avigad@16819
  1123
  "real (n::int) / real (x) - real (n div x) <= 1"
nipkow@29667
  1124
  apply (simp add: algebra_simps)
avigad@16819
  1125
  apply (subst real_of_int_div_aux)
bulwahn@46670
  1126
  apply (auto simp add: divide_le_eq intro: order_less_imp_le)
avigad@16819
  1127
done
avigad@16819
  1128
avigad@16819
  1129
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
nipkow@27964
  1130
by (insert real_of_int_div2 [of n x], simp)
nipkow@27964
  1131
huffman@35635
  1132
lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
huffman@35635
  1133
unfolding real_of_int_def by (rule Ints_of_int)
huffman@35635
  1134
nipkow@27964
  1135
paulson@14365
  1136
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
  1137
paulson@14334
  1138
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
  1139
by (simp add: real_of_nat_def)
paulson@14334
  1140
huffman@30082
  1141
lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
huffman@30082
  1142
by (simp add: real_of_nat_def)
huffman@30082
  1143
paulson@14334
  1144
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
  1145
by (simp add: real_of_nat_def)
paulson@14334
  1146
paulson@14365
  1147
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
  1148
by (simp add: real_of_nat_def)
paulson@14334
  1149
paulson@14334
  1150
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
  1151
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
  1152
by (simp add: real_of_nat_def)
paulson@14334
  1153
paulson@14334
  1154
lemma real_of_nat_less_iff [iff]: 
paulson@14334
  1155
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
  1156
by (simp add: real_of_nat_def)
paulson@14334
  1157
paulson@14334
  1158
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
  1159
by (simp add: real_of_nat_def)
paulson@14334
  1160
paulson@14334
  1161
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
huffman@47489
  1162
by (simp add: real_of_nat_def)
paulson@14334
  1163
paulson@14365
  1164
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
  1165
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
  1166
paulson@14334
  1167
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
huffman@23431
  1168
by (simp add: real_of_nat_def of_nat_mult)
paulson@14334
  1169
huffman@35344
  1170
lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
huffman@35344
  1171
by (simp add: real_of_nat_def of_nat_power)
huffman@35344
  1172
huffman@35344
  1173
lemmas power_real_of_nat = real_of_nat_power [symmetric]
huffman@35344
  1174
avigad@16819
  1175
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
  1176
    (SUM x:A. real(f x))"
avigad@16819
  1177
  apply (subst real_eq_of_nat)+
avigad@16819
  1178
  apply (rule of_nat_setsum)
avigad@16819
  1179
done
avigad@16819
  1180
avigad@16819
  1181
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
  1182
    (PROD x:A. real(f x))"
avigad@16819
  1183
  apply (subst real_eq_of_nat)+
avigad@16819
  1184
  apply (rule of_nat_setprod)
avigad@16819
  1185
done
avigad@16819
  1186
avigad@16819
  1187
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
  1188
  apply (subst card_eq_setsum)
avigad@16819
  1189
  apply (subst real_of_nat_setsum)
avigad@16819
  1190
  apply simp
avigad@16819
  1191
done
avigad@16819
  1192
paulson@14334
  1193
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
  1194
by (simp add: real_of_nat_def)
paulson@14334
  1195
paulson@14387
  1196
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
  1197
by (simp add: real_of_nat_def)
paulson@14334
  1198
paulson@14365
  1199
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
huffman@23438
  1200
by (simp add: add: real_of_nat_def of_nat_diff)
paulson@14334
  1201
nipkow@25162
  1202
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
nipkow@25140
  1203
by (auto simp: real_of_nat_def)
paulson@14365
  1204
paulson@14365
  1205
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
  1206
by (simp add: add: real_of_nat_def)
paulson@14334
  1207
paulson@14365
  1208
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
  1209
by (simp add: add: real_of_nat_def)
paulson@14334
  1210
avigad@16819
  1211
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
  1212
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
  1213
  apply simp
avigad@16819
  1214
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
  1215
done
avigad@16819
  1216
avigad@16819
  1217
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
  1218
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
  1219
  apply (simp add: less_Suc_eq_le)
avigad@16819
  1220
  apply (simp add: real_of_nat_Suc)
avigad@16819
  1221
done
avigad@16819
  1222
bulwahn@46670
  1223
lemma real_of_nat_div_aux: "(real (x::nat)) / (real d) = 
avigad@16819
  1224
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1225
proof -
avigad@16819
  1226
  have "x = (x div d) * d + x mod d"
avigad@16819
  1227
    by auto
avigad@16819
  1228
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1229
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
  1230
  then have "real x / real d = \<dots> / real d"
avigad@16819
  1231
    by simp
avigad@16819
  1232
  then show ?thesis
bulwahn@46670
  1233
    by (auto simp add: add_divide_distrib algebra_simps)
avigad@16819
  1234
qed
avigad@16819
  1235
bulwahn@46670
  1236
lemma real_of_nat_div: "(d :: nat) dvd n ==>
avigad@16819
  1237
    real(n div d) = real n / real d"
bulwahn@46670
  1238
  by (subst real_of_nat_div_aux)
bulwahn@46670
  1239
    (auto simp add: dvd_eq_mod_eq_0 [symmetric])
avigad@16819
  1240
avigad@16819
  1241
lemma real_of_nat_div2:
avigad@16819
  1242
  "0 <= real (n::nat) / real (x) - real (n div x)"
nipkow@29667
  1243
apply (simp add: algebra_simps)
nipkow@25134
  1244
apply (subst real_of_nat_div_aux)
nipkow@25134
  1245
apply simp
nipkow@25134
  1246
apply (subst zero_le_divide_iff)
nipkow@25134
  1247
apply simp
avigad@16819
  1248
done
avigad@16819
  1249
avigad@16819
  1250
lemma real_of_nat_div3:
avigad@16819
  1251
  "real (n::nat) / real (x) - real (n div x) <= 1"
nipkow@25134
  1252
apply(case_tac "x = 0")
nipkow@25134
  1253
apply (simp)
nipkow@29667
  1254
apply (simp add: algebra_simps)
nipkow@25134
  1255
apply (subst real_of_nat_div_aux)
nipkow@25134
  1256
apply simp
avigad@16819
  1257
done
avigad@16819
  1258
avigad@16819
  1259
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
nipkow@29667
  1260
by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
  1261
paulson@14426
  1262
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
  1263
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
  1264
avigad@16819
  1265
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
  1266
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
  1267
  apply force
huffman@44822
  1268
  apply (simp only: real_of_int_of_nat_eq)
avigad@16819
  1269
done
paulson@14387
  1270
huffman@35635
  1271
lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
huffman@35635
  1272
unfolding real_of_nat_def by (rule of_nat_in_Nats)
huffman@35635
  1273
huffman@35635
  1274
lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
huffman@35635
  1275
unfolding real_of_nat_def by (rule Ints_of_nat)
huffman@35635
  1276
nipkow@28001
  1277
nipkow@28001
  1278
subsection{* Rationals *}
nipkow@28001
  1279
nipkow@28091
  1280
lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
nipkow@28091
  1281
by (simp add: real_eq_of_nat)
nipkow@28091
  1282
nipkow@28091
  1283
nipkow@28001
  1284
lemma Rats_eq_int_div_int:
nipkow@28091
  1285
  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
nipkow@28001
  1286
proof
nipkow@28091
  1287
  show "\<rat> \<subseteq> ?S"
nipkow@28001
  1288
  proof
nipkow@28091
  1289
    fix x::real assume "x : \<rat>"
nipkow@28001
  1290
    then obtain r where "x = of_rat r" unfolding Rats_def ..
nipkow@28001
  1291
    have "of_rat r : ?S"
nipkow@28001
  1292
      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1293
    thus "x : ?S" using `x = of_rat r` by simp
nipkow@28001
  1294
  qed
nipkow@28001
  1295
next
nipkow@28091
  1296
  show "?S \<subseteq> \<rat>"
nipkow@28001
  1297
  proof(auto simp:Rats_def)
nipkow@28001
  1298
    fix i j :: int assume "j \<noteq> 0"
nipkow@28001
  1299
    hence "real i / real j = of_rat(Fract i j)"
nipkow@28001
  1300
      by (simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1301
    thus "real i / real j \<in> range of_rat" by blast
nipkow@28001
  1302
  qed
nipkow@28001
  1303
qed
nipkow@28001
  1304
nipkow@28001
  1305
lemma Rats_eq_int_div_nat:
nipkow@28091
  1306
  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
nipkow@28001
  1307
proof(auto simp:Rats_eq_int_div_int)
nipkow@28001
  1308
  fix i j::int assume "j \<noteq> 0"
nipkow@28001
  1309
  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
nipkow@28001
  1310
  proof cases
nipkow@28001
  1311
    assume "j>0"
nipkow@28001
  1312
    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
nipkow@28001
  1313
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1314
    thus ?thesis by blast
nipkow@28001
  1315
  next
nipkow@28001
  1316
    assume "~ j>0"
nipkow@28001
  1317
    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
nipkow@28001
  1318
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1319
    thus ?thesis by blast
nipkow@28001
  1320
  qed
nipkow@28001
  1321
next
nipkow@28001
  1322
  fix i::int and n::nat assume "0 < n"
nipkow@28001
  1323
  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
nipkow@28001
  1324
  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
nipkow@28001
  1325
qed
nipkow@28001
  1326
nipkow@28001
  1327
lemma Rats_abs_nat_div_natE:
nipkow@28001
  1328
  assumes "x \<in> \<rat>"
huffman@31706
  1329
  obtains m n :: nat
huffman@31706
  1330
  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
nipkow@28001
  1331
proof -
nipkow@28001
  1332
  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
nipkow@28001
  1333
    by(auto simp add: Rats_eq_int_div_nat)
nipkow@28001
  1334
  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
nipkow@28001
  1335
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
nipkow@28001
  1336
  let ?gcd = "gcd m n"
huffman@31706
  1337
  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
nipkow@28001
  1338
  let ?k = "m div ?gcd"
nipkow@28001
  1339
  let ?l = "n div ?gcd"
nipkow@28001
  1340
  let ?gcd' = "gcd ?k ?l"
nipkow@28001
  1341
  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
nipkow@28001
  1342
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1343
  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
nipkow@28001
  1344
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1345
  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
nipkow@28001
  1346
  moreover
nipkow@28001
  1347
  have "\<bar>x\<bar> = real ?k / real ?l"
nipkow@28001
  1348
  proof -
nipkow@28001
  1349
    from gcd have "real ?k / real ?l =
nipkow@28001
  1350
        real (?gcd * ?k) / real (?gcd * ?l)" by simp
nipkow@28001
  1351
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
nipkow@28001
  1352
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
nipkow@28001
  1353
    finally show ?thesis ..
nipkow@28001
  1354
  qed
nipkow@28001
  1355
  moreover
nipkow@28001
  1356
  have "?gcd' = 1"
nipkow@28001
  1357
  proof -
nipkow@28001
  1358
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
nipkow@31952
  1359
      by (rule gcd_mult_distrib_nat)
nipkow@28001
  1360
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
huffman@31706
  1361
    with gcd show ?thesis by auto
nipkow@28001
  1362
  qed
nipkow@28001
  1363
  ultimately show ?thesis ..
nipkow@28001
  1364
qed
nipkow@28001
  1365
nipkow@28001
  1366
paulson@14387
  1367
subsection{*Numerals and Arithmetic*}
paulson@14387
  1368
haftmann@46028
  1369
lemma [code_abbrev]:
huffman@47108
  1370
  "real_of_int (numeral k) = numeral k"
huffman@47108
  1371
  "real_of_int (neg_numeral k) = neg_numeral k"
huffman@47108
  1372
  by simp_all
paulson@14387
  1373
paulson@14387
  1374
text{*Collapse applications of @{term real} to @{term number_of}*}
huffman@47108
  1375
lemma real_numeral [simp]:
huffman@47108
  1376
  "real (numeral v :: int) = numeral v"
huffman@47108
  1377
  "real (neg_numeral v :: int) = neg_numeral v"
huffman@47108
  1378
by (simp_all add: real_of_int_def)
paulson@14387
  1379
huffman@47108
  1380
lemma real_of_nat_numeral [simp]:
huffman@47108
  1381
  "real (numeral v :: nat) = numeral v"
huffman@47108
  1382
by (simp add: real_of_nat_def)
paulson@14387
  1383
haftmann@31100
  1384
declaration {*
haftmann@31100
  1385
  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
haftmann@31100
  1386
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
haftmann@31100
  1387
  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
haftmann@31100
  1388
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
haftmann@31100
  1389
  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
haftmann@31100
  1390
      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
haftmann@31100
  1391
      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
haftmann@31100
  1392
      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
huffman@47108
  1393
      @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
huffman@36795
  1394
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
huffman@36795
  1395
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
haftmann@31100
  1396
*}
paulson@14387
  1397
kleing@19023
  1398
paulson@14387
  1399
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
  1400
paulson@14387
  1401
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
  1402
by arith
paulson@14387
  1403
huffman@36839
  1404
text {* FIXME: redundant with @{text add_eq_0_iff} below *}
paulson@15085
  1405
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
  1406
by auto
paulson@14387
  1407
paulson@15085
  1408
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
  1409
by auto
paulson@14387
  1410
paulson@15085
  1411
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
  1412
by auto
paulson@14387
  1413
paulson@15085
  1414
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
  1415
by auto
paulson@14387
  1416
paulson@15085
  1417
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
  1418
by auto
paulson@14387
  1419
huffman@36839
  1420
subsection {* Lemmas about powers *}
paulson@14387
  1421
huffman@36839
  1422
text {* FIXME: declare this in Rings.thy or not at all *}
huffman@36839
  1423
declare abs_mult_self [simp]
huffman@36839
  1424
huffman@36839
  1425
(* used by Import/HOL/real.imp *)
huffman@36839
  1426
lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
huffman@36839
  1427
by simp
huffman@36839
  1428
huffman@36839
  1429
lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
huffman@36839
  1430
apply (induct "n")
huffman@36839
  1431
apply (auto simp add: real_of_nat_Suc)
huffman@36839
  1432
apply (subst mult_2)
huffman@36839
  1433
apply (erule add_less_le_mono)
huffman@36839
  1434
apply (rule two_realpow_ge_one)
huffman@36839
  1435
done
huffman@36839
  1436
huffman@36839
  1437
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1438
lemma realpow_Suc_le_self:
huffman@36839
  1439
  fixes r :: "'a::linordered_semidom"
huffman@36839
  1440
  shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
huffman@36839
  1441
by (insert power_decreasing [of 1 "Suc n" r], simp)
huffman@36839
  1442
huffman@36839
  1443
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1444
lemma realpow_minus_mult:
huffman@36839
  1445
  fixes x :: "'a::monoid_mult"
huffman@36839
  1446
  shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
huffman@36839
  1447
by (simp add: power_commutes split add: nat_diff_split)
huffman@36839
  1448
huffman@36839
  1449
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1450
lemma real_two_squares_add_zero_iff [simp]:
huffman@36839
  1451
  "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
huffman@36839
  1452
by (rule sum_squares_eq_zero_iff)
huffman@36839
  1453
huffman@36839
  1454
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1455
lemma realpow_two_sum_zero_iff [simp]:
huffman@36839
  1456
     "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
huffman@36839
  1457
by (rule sum_power2_eq_zero_iff)
huffman@36839
  1458
huffman@36839
  1459
lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
huffman@36839
  1460
by (rule_tac y = 0 in order_trans, auto)
huffman@36839
  1461
huffman@36839
  1462
lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
huffman@36839
  1463
by (auto simp add: power2_eq_square)
huffman@36839
  1464
huffman@36839
  1465
hoelzl@47598
  1466
lemma numeral_power_le_real_of_nat_cancel_iff[simp]:
hoelzl@47598
  1467
  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::nat) ^ n \<le> a"
hoelzl@47598
  1468
  unfolding real_of_nat_le_iff[symmetric] by simp
hoelzl@47598
  1469
hoelzl@47598
  1470
lemma real_of_nat_le_numeral_power_cancel_iff[simp]:
hoelzl@47598
  1471
  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::nat) ^ n"
hoelzl@47598
  1472
  unfolding real_of_nat_le_iff[symmetric] by simp
hoelzl@47598
  1473
hoelzl@47598
  1474
lemma numeral_power_le_real_of_int_cancel_iff[simp]:
hoelzl@47598
  1475
  "(numeral x::real) ^ n \<le> real a \<longleftrightarrow> (numeral x::int) ^ n \<le> a"
hoelzl@47598
  1476
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@47598
  1477
hoelzl@47598
  1478
lemma real_of_int_le_numeral_power_cancel_iff[simp]:
hoelzl@47598
  1479
  "real a \<le> (numeral x::real) ^ n \<longleftrightarrow> a \<le> (numeral x::int) ^ n"
hoelzl@47598
  1480
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@47598
  1481
hoelzl@47598
  1482
lemma neg_numeral_power_le_real_of_int_cancel_iff[simp]:
hoelzl@47598
  1483
  "(neg_numeral x::real) ^ n \<le> real a \<longleftrightarrow> (neg_numeral x::int) ^ n \<le> a"
hoelzl@47598
  1484
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@47598
  1485
hoelzl@47598
  1486
lemma real_of_int_le_neg_numeral_power_cancel_iff[simp]:
hoelzl@47598
  1487
  "real a \<le> (neg_numeral x::real) ^ n \<longleftrightarrow> a \<le> (neg_numeral x::int) ^ n"
hoelzl@47598
  1488
  unfolding real_of_int_le_iff[symmetric] by simp
hoelzl@47598
  1489
huffman@36839
  1490
subsection{*Density of the Reals*}
paulson@14387
  1491
paulson@14387
  1492
lemma real_lbound_gt_zero:
paulson@14387
  1493
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
  1494
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
  1495
apply (simp add: min_def)
paulson@14387
  1496
done
paulson@14387
  1497
paulson@14387
  1498
haftmann@35050
  1499
text{*Similar results are proved in @{text Fields}*}
paulson@14387
  1500
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
  1501
  by auto
paulson@14387
  1502
paulson@14387
  1503
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
  1504
  by auto
paulson@14387
  1505
paulson@14387
  1506
paulson@14387
  1507
subsection{*Absolute Value Function for the Reals*}
paulson@14387
  1508
paulson@14387
  1509
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
  1510
by (simp add: abs_if)
paulson@14387
  1511
huffman@23289
  1512
(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
paulson@14387
  1513
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
haftmann@35050
  1514
by (force simp add: abs_le_iff)
paulson@14387
  1515
huffman@44344
  1516
lemma abs_add_one_gt_zero: "(0::real) < 1 + abs(x)"
paulson@15003
  1517
by (simp add: abs_if)
paulson@14387
  1518
paulson@14387
  1519
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
  1520
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
  1521
huffman@44344
  1522
lemma abs_add_one_not_less_self: "~ abs(x) + (1::real) < x"
webertj@20217
  1523
by simp
paulson@14387
  1524
 
paulson@14387
  1525
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
  1526
by simp
paulson@14387
  1527
berghofe@24534
  1528
haftmann@27544
  1529
subsection {* Implementation of rational real numbers *}
berghofe@24534
  1530
huffman@47108
  1531
text {* Formal constructor *}
huffman@47108
  1532
haftmann@27544
  1533
definition Ratreal :: "rat \<Rightarrow> real" where
huffman@47108
  1534
  [code_abbrev, simp]: "Ratreal = of_rat"
berghofe@24534
  1535
haftmann@24623
  1536
code_datatype Ratreal
berghofe@24534
  1537
huffman@47108
  1538
huffman@47108
  1539
text {* Numerals *}
huffman@47108
  1540
huffman@47108
  1541
lemma [code_abbrev]:
huffman@47108
  1542
  "(of_rat (of_int a) :: real) = of_int a"
huffman@47108
  1543
  by simp
huffman@47108
  1544
huffman@47108
  1545
lemma [code_abbrev]:
huffman@47108
  1546
  "(of_rat 0 :: real) = 0"
huffman@47108
  1547
  by simp
huffman@47108
  1548
huffman@47108
  1549
lemma [code_abbrev]:
huffman@47108
  1550
  "(of_rat 1 :: real) = 1"
huffman@47108
  1551
  by simp
huffman@47108
  1552
huffman@47108
  1553
lemma [code_abbrev]:
huffman@47108
  1554
  "(of_rat (numeral k) :: real) = numeral k"
huffman@47108
  1555
  by simp
berghofe@24534
  1556
huffman@47108
  1557
lemma [code_abbrev]:
huffman@47108
  1558
  "(of_rat (neg_numeral k) :: real) = neg_numeral k"
huffman@47108
  1559
  by simp
huffman@47108
  1560
huffman@47108
  1561
lemma [code_post]:
huffman@47108
  1562
  "(of_rat (0 / r)  :: real) = 0"
huffman@47108
  1563
  "(of_rat (r / 0)  :: real) = 0"
huffman@47108
  1564
  "(of_rat (1 / 1)  :: real) = 1"
huffman@47108
  1565
  "(of_rat (numeral k / 1) :: real) = numeral k"
huffman@47108
  1566
  "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
huffman@47108
  1567
  "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
huffman@47108
  1568
  "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
huffman@47108
  1569
  "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
huffman@47108
  1570
  "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
huffman@47108
  1571
  "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
huffman@47108
  1572
  "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
huffman@47108
  1573
  by (simp_all add: of_rat_divide)
huffman@47108
  1574
huffman@47108
  1575
huffman@47108
  1576
text {* Operations *}
huffman@47108
  1577
huffman@47108
  1578
lemma zero_real_code [code]:
haftmann@27544
  1579
  "0 = Ratreal 0"
haftmann@27544
  1580
by simp
berghofe@24534
  1581
huffman@47108
  1582
lemma one_real_code [code]:
haftmann@27544
  1583
  "1 = Ratreal 1"
haftmann@27544
  1584
by simp
haftmann@27544
  1585
haftmann@38857
  1586
instantiation real :: equal
haftmann@26513
  1587
begin
haftmann@26513
  1588
haftmann@38857
  1589
definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
haftmann@26513
  1590
haftmann@38857
  1591
instance proof
haftmann@38857
  1592
qed (simp add: equal_real_def)
berghofe@24534
  1593
haftmann@38857
  1594
lemma real_equal_code [code]:
haftmann@38857
  1595
  "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
haftmann@38857
  1596
  by (simp add: equal_real_def equal)
haftmann@26513
  1597
haftmann@38857
  1598
lemma [code nbe]:
haftmann@38857
  1599
  "HOL.equal (x::real) x \<longleftrightarrow> True"
haftmann@38857
  1600
  by (rule equal_refl)
haftmann@28351
  1601
haftmann@26513
  1602
end
berghofe@24534
  1603
haftmann@27544
  1604
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
haftmann@27652
  1605
  by (simp add: of_rat_less_eq)
berghofe@24534
  1606
haftmann@27544
  1607
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
haftmann@27652
  1608
  by (simp add: of_rat_less)
berghofe@24534
  1609
haftmann@27544
  1610
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
haftmann@27544
  1611
  by (simp add: of_rat_add)
berghofe@24534
  1612
haftmann@27544
  1613
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
haftmann@27544
  1614
  by (simp add: of_rat_mult)
haftmann@27544
  1615
haftmann@27544
  1616
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
haftmann@27544
  1617
  by (simp add: of_rat_minus)
berghofe@24534
  1618
haftmann@27544
  1619
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
haftmann@27544
  1620
  by (simp add: of_rat_diff)
berghofe@24534
  1621
haftmann@27544
  1622
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
haftmann@27544
  1623
  by (simp add: of_rat_inverse)
haftmann@27544
  1624
 
haftmann@27544
  1625
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
haftmann@27544
  1626
  by (simp add: of_rat_divide)
berghofe@24534
  1627
bulwahn@43733
  1628
lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
bulwahn@43733
  1629
  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)
bulwahn@43733
  1630
huffman@47108
  1631
huffman@47108
  1632
text {* Quickcheck *}
huffman@47108
  1633
haftmann@31203
  1634
definition (in term_syntax)
haftmann@32657
  1635
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@32657
  1636
  [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
haftmann@31203
  1637
haftmann@37751
  1638
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1639
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1640
haftmann@31203
  1641
instantiation real :: random
haftmann@31203
  1642
begin
haftmann@31203
  1643
haftmann@31203
  1644
definition
haftmann@51126
  1645
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
haftmann@31203
  1646
haftmann@31203
  1647
instance ..
haftmann@31203
  1648
haftmann@31203
  1649
end
haftmann@31203
  1650
haftmann@37751
  1651
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1652
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1653
bulwahn@41920
  1654
instantiation real :: exhaustive
bulwahn@41231
  1655
begin
bulwahn@41231
  1656
bulwahn@41231
  1657
definition
bulwahn@45818
  1658
  "exhaustive_real f d = Quickcheck_Exhaustive.exhaustive (%r. f (Ratreal r)) d"
bulwahn@42311
  1659
bulwahn@42311
  1660
instance ..
bulwahn@42311
  1661
bulwahn@42311
  1662
end
bulwahn@42311
  1663
bulwahn@42311
  1664
instantiation real :: full_exhaustive
bulwahn@42311
  1665
begin
bulwahn@42311
  1666
bulwahn@42311
  1667
definition
bulwahn@45818
  1668
  "full_exhaustive_real f d = Quickcheck_Exhaustive.full_exhaustive (%r. f (valterm_ratreal r)) d"
bulwahn@41231
  1669
bulwahn@41231
  1670
instance ..
bulwahn@41231
  1671
bulwahn@41231
  1672
end
bulwahn@41231
  1673
bulwahn@43887
  1674
instantiation real :: narrowing
bulwahn@43887
  1675
begin
bulwahn@43887
  1676
bulwahn@43887
  1677
definition
bulwahn@43887
  1678
  "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
bulwahn@43887
  1679
bulwahn@43887
  1680
instance ..
bulwahn@43887
  1681
bulwahn@43887
  1682
end
bulwahn@43887
  1683
bulwahn@43887
  1684
bulwahn@45184
  1685
subsection {* Setup for Nitpick *}
berghofe@24534
  1686
blanchet@38287
  1687
declaration {*
blanchet@38287
  1688
  Nitpick_HOL.register_frac_type @{type_name real}
wenzelm@33209
  1689
   [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
wenzelm@33209
  1690
    (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
wenzelm@33209
  1691
    (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
wenzelm@33209
  1692
    (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
wenzelm@33209
  1693
    (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
wenzelm@33209
  1694
    (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
blanchet@45859
  1695
    (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
wenzelm@33209
  1696
    (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
blanchet@33197
  1697
*}
blanchet@33197
  1698
huffman@47108
  1699
lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
blanchet@37397
  1700
    ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
blanchet@33197
  1701
    times_real_inst.times_real uminus_real_inst.uminus_real
blanchet@33197
  1702
    zero_real_inst.zero_real
blanchet@33197
  1703
paulson@5588
  1704
end