src/HOL/RealDef.thy
author haftmann
Thu Aug 18 13:55:26 2011 +0200 (2011-08-18)
changeset 44278 1220ecb81e8f
parent 43887 442aceb54969
child 44344 49be3e7d4762
permissions -rw-r--r--
observe distinction between sets and predicates more properly
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@36795
   304
subsection {* Equivalence relation on Cauchy sequences *}
huffman@36795
   305
huffman@36795
   306
definition
huffman@36795
   307
  realrel :: "((nat \<Rightarrow> rat) \<times> (nat \<Rightarrow> rat)) set"
huffman@36795
   308
where
huffman@36795
   309
  "realrel = {(X, Y). cauchy X \<and> cauchy Y \<and> vanishes (\<lambda>n. X n - Y n)}"
huffman@36795
   310
huffman@36795
   311
lemma refl_realrel: "refl_on {X. cauchy X} realrel"
huffman@36795
   312
  unfolding realrel_def by (rule refl_onI, clarsimp, simp)
huffman@36795
   313
huffman@36795
   314
lemma sym_realrel: "sym realrel"
huffman@36795
   315
  unfolding realrel_def
huffman@36795
   316
  by (rule symI, clarify, drule vanishes_minus, simp)
paulson@14484
   317
huffman@36795
   318
lemma trans_realrel: "trans realrel"
huffman@36795
   319
  unfolding realrel_def
huffman@36795
   320
  apply (rule transI, clarify)
huffman@36795
   321
  apply (drule (1) vanishes_add)
huffman@36795
   322
  apply (simp add: algebra_simps)
huffman@36795
   323
  done
huffman@36795
   324
huffman@36795
   325
lemma equiv_realrel: "equiv {X. cauchy X} realrel"
huffman@36795
   326
  using refl_realrel sym_realrel trans_realrel
haftmann@40815
   327
  by (rule equivI)
huffman@36795
   328
huffman@36795
   329
subsection {* The field of real numbers *}
huffman@36795
   330
huffman@36795
   331
typedef (open) real = "{X. cauchy X} // realrel"
huffman@36795
   332
  by (fast intro: quotientI cauchy_const)
huffman@36795
   333
huffman@36795
   334
definition
huffman@36795
   335
  Real :: "(nat \<Rightarrow> rat) \<Rightarrow> real"
huffman@36795
   336
where
huffman@36795
   337
  "Real X = Abs_real (realrel `` {X})"
huffman@36795
   338
huffman@36795
   339
definition
huffman@36795
   340
  real_case :: "((nat \<Rightarrow> rat) \<Rightarrow> 'a) \<Rightarrow> real \<Rightarrow> 'a"
huffman@36795
   341
where
huffman@36795
   342
  "real_case f x = (THE y. \<forall>X\<in>Rep_real x. y = f X)"
huffman@36795
   343
huffman@36795
   344
lemma Real_induct [induct type: real]:
huffman@36795
   345
  "(\<And>X. cauchy X \<Longrightarrow> P (Real X)) \<Longrightarrow> P x"
huffman@36795
   346
  unfolding Real_def
huffman@36795
   347
  apply (induct x)
huffman@36795
   348
  apply (erule quotientE)
huffman@36795
   349
  apply (simp)
huffman@36795
   350
  done
huffman@36795
   351
huffman@36795
   352
lemma real_case_1:
huffman@36795
   353
  assumes f: "congruent realrel f"
huffman@36795
   354
  assumes X: "cauchy X"
huffman@36795
   355
  shows "real_case f (Real X) = f X"
huffman@36795
   356
  unfolding real_case_def Real_def
huffman@36795
   357
  apply (subst Abs_real_inverse)
huffman@36795
   358
  apply (simp add: quotientI X)
huffman@36795
   359
  apply (rule the_equality)
huffman@36795
   360
  apply clarsimp
haftmann@40816
   361
  apply (erule congruentD [OF f])
huffman@36795
   362
  apply (erule bspec)
huffman@36795
   363
  apply simp
huffman@36795
   364
  apply (rule refl_onD [OF refl_realrel])
huffman@36795
   365
  apply (simp add: X)
huffman@36795
   366
  done
huffman@36795
   367
huffman@36795
   368
lemma real_case_2:
huffman@36795
   369
  assumes f: "congruent2 realrel realrel f"
huffman@36795
   370
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   371
  shows "real_case (\<lambda>X. real_case (\<lambda>Y. f X Y) (Real Y)) (Real X) = f X Y"
huffman@36795
   372
 apply (subst real_case_1 [OF _ X])
haftmann@40816
   373
  apply (rule congruentI)
huffman@36795
   374
  apply (subst real_case_1 [OF _ Y])
huffman@36795
   375
   apply (rule congruent2_implies_congruent [OF equiv_realrel f])
huffman@36795
   376
   apply (simp add: realrel_def)
huffman@36795
   377
  apply (subst real_case_1 [OF _ Y])
huffman@36795
   378
   apply (rule congruent2_implies_congruent [OF equiv_realrel f])
huffman@36795
   379
   apply (simp add: realrel_def)
haftmann@40817
   380
  apply (erule congruent2D [OF f])
huffman@36795
   381
  apply (rule refl_onD [OF refl_realrel])
huffman@36795
   382
  apply (simp add: Y)
huffman@36795
   383
  apply (rule real_case_1 [OF _ Y])
huffman@36795
   384
  apply (rule congruent2_implies_congruent [OF equiv_realrel f])
huffman@36795
   385
  apply (simp add: X)
huffman@36795
   386
  done
huffman@36795
   387
huffman@36795
   388
lemma eq_Real:
huffman@36795
   389
  "cauchy X \<Longrightarrow> cauchy Y \<Longrightarrow> Real X = Real Y \<longleftrightarrow> vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   390
  unfolding Real_def
huffman@36795
   391
  apply (subst Abs_real_inject)
huffman@36795
   392
  apply (simp add: quotientI)
huffman@36795
   393
  apply (simp add: quotientI)
huffman@36795
   394
  apply (simp add: eq_equiv_class_iff [OF equiv_realrel])
huffman@36795
   395
  apply (simp add: realrel_def)
huffman@36795
   396
  done
huffman@36795
   397
huffman@36795
   398
lemma add_respects2_realrel:
huffman@36795
   399
  "(\<lambda>X Y. Real (\<lambda>n. X n + Y n)) respects2 realrel"
huffman@36795
   400
proof (rule congruent2_commuteI [OF equiv_realrel, unfolded mem_Collect_eq])
huffman@36795
   401
  fix X Y show "Real (\<lambda>n. X n + Y n) = Real (\<lambda>n. Y n + X n)"
huffman@36795
   402
    by (simp add: add_commute)
huffman@36795
   403
next
huffman@36795
   404
  fix X assume X: "cauchy X"
huffman@36795
   405
  fix Y Z assume "(Y, Z) \<in> realrel"
huffman@36795
   406
  hence Y: "cauchy Y" and Z: "cauchy Z" and YZ: "vanishes (\<lambda>n. Y n - Z n)"
huffman@36795
   407
    unfolding realrel_def by simp_all
huffman@36795
   408
  show "Real (\<lambda>n. X n + Y n) = Real (\<lambda>n. X n + Z n)"
huffman@36795
   409
  proof (rule eq_Real [THEN iffD2])
huffman@36795
   410
    show "cauchy (\<lambda>n. X n + Y n)" using X Y by (rule cauchy_add)
huffman@36795
   411
    show "cauchy (\<lambda>n. X n + Z n)" using X Z by (rule cauchy_add)
huffman@36795
   412
    show "vanishes (\<lambda>n. (X n + Y n) - (X n + Z n))"
huffman@36795
   413
      unfolding add_diff_add using YZ by simp
huffman@36795
   414
  qed
huffman@36795
   415
qed
huffman@36795
   416
huffman@36795
   417
lemma minus_respects_realrel:
huffman@36795
   418
  "(\<lambda>X. Real (\<lambda>n. - X n)) respects realrel"
haftmann@40816
   419
proof (rule congruentI)
huffman@36795
   420
  fix X Y assume "(X, Y) \<in> realrel"
huffman@36795
   421
  hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   422
    unfolding realrel_def by simp_all
huffman@36795
   423
  show "Real (\<lambda>n. - X n) = Real (\<lambda>n. - Y n)"
huffman@36795
   424
  proof (rule eq_Real [THEN iffD2])
huffman@36795
   425
    show "cauchy (\<lambda>n. - X n)" using X by (rule cauchy_minus)
huffman@36795
   426
    show "cauchy (\<lambda>n. - Y n)" using Y by (rule cauchy_minus)
huffman@36795
   427
    show "vanishes (\<lambda>n. (- X n) - (- Y n))"
huffman@36795
   428
      unfolding minus_diff_minus using XY by (rule vanishes_minus)
huffman@36795
   429
  qed
huffman@36795
   430
qed
huffman@36795
   431
huffman@36795
   432
lemma mult_respects2_realrel:
huffman@36795
   433
  "(\<lambda>X Y. Real (\<lambda>n. X n * Y n)) respects2 realrel"
huffman@36795
   434
proof (rule congruent2_commuteI [OF equiv_realrel, unfolded mem_Collect_eq])
huffman@36795
   435
  fix X Y
huffman@36795
   436
  show "Real (\<lambda>n. X n * Y n) = Real (\<lambda>n. Y n * X n)"
huffman@36795
   437
    by (simp add: mult_commute)
huffman@36795
   438
next
huffman@36795
   439
  fix X assume X: "cauchy X"
huffman@36795
   440
  fix Y Z assume "(Y, Z) \<in> realrel"
huffman@36795
   441
  hence Y: "cauchy Y" and Z: "cauchy Z" and YZ: "vanishes (\<lambda>n. Y n - Z n)"
huffman@36795
   442
    unfolding realrel_def by simp_all
huffman@36795
   443
  show "Real (\<lambda>n. X n * Y n) = Real (\<lambda>n. X n * Z n)"
huffman@36795
   444
  proof (rule eq_Real [THEN iffD2])
huffman@36795
   445
    show "cauchy (\<lambda>n. X n * Y n)" using X Y by (rule cauchy_mult)
huffman@36795
   446
    show "cauchy (\<lambda>n. X n * Z n)" using X Z by (rule cauchy_mult)
huffman@36795
   447
    have "vanishes (\<lambda>n. X n * (Y n - Z n))"
huffman@36795
   448
      by (intro vanishes_mult_bounded cauchy_imp_bounded X YZ)
huffman@36795
   449
    thus "vanishes (\<lambda>n. X n * Y n - X n * Z n)"
huffman@36795
   450
      by (simp add: right_diff_distrib)
huffman@36795
   451
  qed
huffman@36795
   452
qed
huffman@36795
   453
huffman@36795
   454
lemma vanishes_diff_inverse:
huffman@36795
   455
  assumes X: "cauchy X" "\<not> vanishes X"
huffman@36795
   456
  assumes Y: "cauchy Y" "\<not> vanishes Y"
huffman@36795
   457
  assumes XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   458
  shows "vanishes (\<lambda>n. inverse (X n) - inverse (Y n))"
huffman@36795
   459
proof (rule vanishesI)
huffman@36795
   460
  fix r :: rat assume r: "0 < r"
huffman@36795
   461
  obtain a i where a: "0 < a" and i: "\<forall>n\<ge>i. a < \<bar>X n\<bar>"
huffman@36795
   462
    using cauchy_not_vanishes [OF X] by fast
huffman@36795
   463
  obtain b j where b: "0 < b" and j: "\<forall>n\<ge>j. b < \<bar>Y n\<bar>"
huffman@36795
   464
    using cauchy_not_vanishes [OF Y] by fast
huffman@36795
   465
  obtain s where s: "0 < s" and "inverse a * s * inverse b = r"
huffman@36795
   466
  proof
huffman@36795
   467
    show "0 < a * r * b"
huffman@36795
   468
      using a r b by (simp add: mult_pos_pos)
huffman@36795
   469
    show "inverse a * (a * r * b) * inverse b = r"
huffman@36795
   470
      using a r b by simp
huffman@36795
   471
  qed
huffman@36795
   472
  obtain k where k: "\<forall>n\<ge>k. \<bar>X n - Y n\<bar> < s"
huffman@36795
   473
    using vanishesD [OF XY s] ..
huffman@36795
   474
  have "\<forall>n\<ge>max (max i j) k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r"
huffman@36795
   475
  proof (clarsimp)
huffman@36795
   476
    fix n assume n: "i \<le> n" "j \<le> n" "k \<le> n"
huffman@36795
   477
    have "X n \<noteq> 0" and "Y n \<noteq> 0"
huffman@36795
   478
      using i j a b n by auto
huffman@36795
   479
    hence "\<bar>inverse (X n) - inverse (Y n)\<bar> =
huffman@36795
   480
        inverse \<bar>X n\<bar> * \<bar>X n - Y n\<bar> * inverse \<bar>Y n\<bar>"
huffman@36795
   481
      by (simp add: inverse_diff_inverse abs_mult)
huffman@36795
   482
    also have "\<dots> < inverse a * s * inverse b"
huffman@36795
   483
      apply (intro mult_strict_mono' less_imp_inverse_less)
huffman@36795
   484
      apply (simp_all add: a b i j k n mult_nonneg_nonneg)
huffman@36795
   485
      done
huffman@36795
   486
    also note `inverse a * s * inverse b = r`
huffman@36795
   487
    finally show "\<bar>inverse (X n) - inverse (Y n)\<bar> < r" .
huffman@36795
   488
  qed
huffman@36795
   489
  thus "\<exists>k. \<forall>n\<ge>k. \<bar>inverse (X n) - inverse (Y n)\<bar> < r" ..
huffman@36795
   490
qed
huffman@36795
   491
huffman@36795
   492
lemma inverse_respects_realrel:
huffman@36795
   493
  "(\<lambda>X. if vanishes X then c else Real (\<lambda>n. inverse (X n))) respects realrel"
huffman@36795
   494
    (is "?inv respects realrel")
haftmann@40816
   495
proof (rule congruentI)
huffman@36795
   496
  fix X Y assume "(X, Y) \<in> realrel"
huffman@36795
   497
  hence X: "cauchy X" and Y: "cauchy Y" and XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   498
    unfolding realrel_def by simp_all
huffman@36795
   499
  have "vanishes X \<longleftrightarrow> vanishes Y"
huffman@36795
   500
  proof
huffman@36795
   501
    assume "vanishes X"
huffman@36795
   502
    from vanishes_diff [OF this XY] show "vanishes Y" by simp
huffman@36795
   503
  next
huffman@36795
   504
    assume "vanishes Y"
huffman@36795
   505
    from vanishes_add [OF this XY] show "vanishes X" by simp
huffman@36795
   506
  qed
huffman@36795
   507
  thus "?inv X = ?inv Y"
huffman@36795
   508
    by (simp add: vanishes_diff_inverse eq_Real X Y XY)
huffman@36795
   509
qed
huffman@36795
   510
huffman@36795
   511
instantiation real :: field_inverse_zero
haftmann@25571
   512
begin
paulson@5588
   513
haftmann@25571
   514
definition
huffman@36795
   515
  "0 = Real (\<lambda>n. 0)"
haftmann@25571
   516
haftmann@25571
   517
definition
huffman@36795
   518
  "1 = Real (\<lambda>n. 1)"
huffman@36795
   519
huffman@36795
   520
definition
huffman@36795
   521
  "x + y = real_case (\<lambda>X. real_case (\<lambda>Y. Real (\<lambda>n. X n + Y n)) y) x"
paulson@5588
   522
haftmann@25571
   523
definition
huffman@36795
   524
  "- x = real_case (\<lambda>X. Real (\<lambda>n. - X n)) x"
huffman@36795
   525
huffman@36795
   526
definition
huffman@36795
   527
  "x - y = (x::real) + - y"
bauerg@10606
   528
haftmann@25571
   529
definition
huffman@36795
   530
  "x * y = real_case (\<lambda>X. real_case (\<lambda>Y. Real (\<lambda>n. X n * Y n)) y) x"
haftmann@25571
   531
haftmann@25571
   532
definition
huffman@36795
   533
  "inverse =
huffman@36795
   534
    real_case (\<lambda>X. if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
paulson@14484
   535
haftmann@25571
   536
definition
huffman@36795
   537
  "x / y = (x::real) * inverse y"
huffman@36795
   538
huffman@36795
   539
lemma add_Real:
huffman@36795
   540
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   541
  shows "Real X + Real Y = Real (\<lambda>n. X n + Y n)"
huffman@36795
   542
  unfolding plus_real_def
huffman@36795
   543
  by (rule real_case_2 [OF add_respects2_realrel X Y])
huffman@36795
   544
huffman@36795
   545
lemma minus_Real:
huffman@36795
   546
  assumes X: "cauchy X"
huffman@36795
   547
  shows "- Real X = Real (\<lambda>n. - X n)"
huffman@36795
   548
  unfolding uminus_real_def
huffman@36795
   549
  by (rule real_case_1 [OF minus_respects_realrel X])
paulson@5588
   550
huffman@36795
   551
lemma diff_Real:
huffman@36795
   552
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   553
  shows "Real X - Real Y = Real (\<lambda>n. X n - Y n)"
huffman@36795
   554
  unfolding minus_real_def diff_minus
huffman@36795
   555
  by (simp add: minus_Real add_Real X Y)
haftmann@25571
   556
huffman@36795
   557
lemma mult_Real:
huffman@36795
   558
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   559
  shows "Real X * Real Y = Real (\<lambda>n. X n * Y n)"
huffman@36795
   560
  unfolding times_real_def
huffman@36795
   561
  by (rule real_case_2 [OF mult_respects2_realrel X Y])
huffman@36795
   562
huffman@36795
   563
lemma inverse_Real:
huffman@36795
   564
  assumes X: "cauchy X"
huffman@36795
   565
  shows "inverse (Real X) =
huffman@36795
   566
    (if vanishes X then 0 else Real (\<lambda>n. inverse (X n)))"
huffman@36795
   567
  unfolding inverse_real_def
huffman@36795
   568
  by (rule real_case_1 [OF inverse_respects_realrel X])
paulson@14269
   569
huffman@36795
   570
instance proof
huffman@36795
   571
  fix a b c :: real
huffman@36795
   572
  show "a + b = b + a"
huffman@36795
   573
    by (induct a, induct b) (simp add: add_Real add_ac)
huffman@36795
   574
  show "(a + b) + c = a + (b + c)"
huffman@36795
   575
    by (induct a, induct b, induct c) (simp add: add_Real add_ac)
huffman@36795
   576
  show "0 + a = a"
huffman@36795
   577
    unfolding zero_real_def
huffman@36795
   578
    by (induct a) (simp add: add_Real)
huffman@36795
   579
  show "- a + a = 0"
huffman@36795
   580
    unfolding zero_real_def
huffman@36795
   581
    by (induct a) (simp add: minus_Real add_Real)
huffman@36795
   582
  show "a - b = a + - b"
huffman@36795
   583
    by (rule minus_real_def)
huffman@36795
   584
  show "(a * b) * c = a * (b * c)"
huffman@36795
   585
    by (induct a, induct b, induct c) (simp add: mult_Real mult_ac)
huffman@36795
   586
  show "a * b = b * a"
huffman@36795
   587
    by (induct a, induct b) (simp add: mult_Real mult_ac)
huffman@36795
   588
  show "1 * a = a"
huffman@36795
   589
    unfolding one_real_def
huffman@36795
   590
    by (induct a) (simp add: mult_Real)
huffman@36795
   591
  show "(a + b) * c = a * c + b * c"
huffman@36795
   592
    by (induct a, induct b, induct c)
huffman@36795
   593
       (simp add: mult_Real add_Real algebra_simps)
huffman@36795
   594
  show "(0\<Colon>real) \<noteq> (1\<Colon>real)"
huffman@36795
   595
    unfolding zero_real_def one_real_def
huffman@36795
   596
    by (simp add: eq_Real)
huffman@36795
   597
  show "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
huffman@36795
   598
    unfolding zero_real_def one_real_def
huffman@36795
   599
    apply (induct a)
huffman@36795
   600
    apply (simp add: eq_Real inverse_Real mult_Real)
huffman@36795
   601
    apply (rule vanishesI)
huffman@36795
   602
    apply (frule (1) cauchy_not_vanishes, clarify)
huffman@36795
   603
    apply (rule_tac x=k in exI, clarify)
huffman@36795
   604
    apply (drule_tac x=n in spec, simp)
huffman@36795
   605
    done
huffman@36795
   606
  show "a / b = a * inverse b"
huffman@36795
   607
    by (rule divide_real_def)
huffman@36795
   608
  show "inverse (0::real) = 0"
huffman@36795
   609
    by (simp add: zero_real_def inverse_Real)
huffman@36795
   610
qed
haftmann@25571
   611
haftmann@25571
   612
end
paulson@14334
   613
huffman@36795
   614
subsection {* Positive reals *}
paulson@14269
   615
huffman@36795
   616
definition
huffman@36795
   617
  positive :: "real \<Rightarrow> bool"
huffman@36795
   618
where
huffman@36795
   619
  "positive = real_case (\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
paulson@14269
   620
huffman@36795
   621
lemma bool_congruentI:
huffman@36795
   622
  assumes sym: "sym r"
huffman@36795
   623
  assumes P: "\<And>x y. (x, y) \<in> r \<Longrightarrow> P x \<Longrightarrow> P y"
huffman@36795
   624
  shows "P respects r"
haftmann@40816
   625
apply (rule congruentI)
huffman@36795
   626
apply (rule iffI)
huffman@36795
   627
apply (erule (1) P)
huffman@36795
   628
apply (erule (1) P [OF symD [OF sym]])
paulson@14269
   629
done
paulson@14269
   630
huffman@36795
   631
lemma positive_respects_realrel:
huffman@36795
   632
  "(\<lambda>X. \<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n) respects realrel"
huffman@36795
   633
proof (rule bool_congruentI)
huffman@36795
   634
  show "sym realrel" by (rule sym_realrel)
huffman@36795
   635
next
huffman@36795
   636
  fix X Y assume "(X, Y) \<in> realrel"
huffman@36795
   637
  hence XY: "vanishes (\<lambda>n. X n - Y n)"
huffman@36795
   638
    unfolding realrel_def by simp_all
huffman@36795
   639
  assume "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n"
huffman@36795
   640
  then obtain r i where "0 < r" and i: "\<forall>n\<ge>i. r < X n"
huffman@36795
   641
    by fast
huffman@36795
   642
  obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
   643
    using `0 < r` by (rule obtain_pos_sum)
huffman@36795
   644
  obtain j where j: "\<forall>n\<ge>j. \<bar>X n - Y n\<bar> < s"
huffman@36795
   645
    using vanishesD [OF XY s] ..
huffman@36795
   646
  have "\<forall>n\<ge>max i j. t < Y n"
huffman@36795
   647
  proof (clarsimp)
huffman@36795
   648
    fix n assume n: "i \<le> n" "j \<le> n"
huffman@36795
   649
    have "\<bar>X n - Y n\<bar> < s" and "r < X n"
huffman@36795
   650
      using i j n by simp_all
huffman@36795
   651
    thus "t < Y n" unfolding r by simp
huffman@36795
   652
  qed
huffman@36795
   653
  thus "\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < Y n" using t by fast
paulson@14484
   654
qed
paulson@14269
   655
huffman@36795
   656
lemma positive_Real:
huffman@36795
   657
  assumes X: "cauchy X"
huffman@36795
   658
  shows "positive (Real X) \<longleftrightarrow> (\<exists>r>0. \<exists>k. \<forall>n\<ge>k. r < X n)"
huffman@36795
   659
unfolding positive_def
huffman@36795
   660
by (rule real_case_1 [OF positive_respects_realrel X])
huffman@23287
   661
huffman@36795
   662
lemma positive_zero: "\<not> positive 0"
huffman@36795
   663
unfolding zero_real_def by (auto simp add: positive_Real)
paulson@14269
   664
huffman@36795
   665
lemma positive_add:
huffman@36795
   666
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
huffman@36795
   667
apply (induct x, induct y, rename_tac Y X)
huffman@36795
   668
apply (simp add: add_Real positive_Real)
huffman@36795
   669
apply (clarify, rename_tac a b i j)
huffman@36795
   670
apply (rule_tac x="a + b" in exI, simp)
huffman@36795
   671
apply (rule_tac x="max i j" in exI, clarsimp)
huffman@36795
   672
apply (simp add: add_strict_mono)
paulson@14269
   673
done
paulson@14269
   674
huffman@36795
   675
lemma positive_mult:
huffman@36795
   676
  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
huffman@36795
   677
apply (induct x, induct y, rename_tac Y X)
huffman@36795
   678
apply (simp add: mult_Real positive_Real)
huffman@36795
   679
apply (clarify, rename_tac a b i j)
huffman@36795
   680
apply (rule_tac x="a * b" in exI, simp add: mult_pos_pos)
huffman@36795
   681
apply (rule_tac x="max i j" in exI, clarsimp)
huffman@36795
   682
apply (rule mult_strict_mono, auto)
huffman@36795
   683
done
huffman@36795
   684
huffman@36795
   685
lemma positive_minus:
huffman@36795
   686
  "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
huffman@36795
   687
apply (induct x, rename_tac X)
huffman@36795
   688
apply (simp add: zero_real_def eq_Real minus_Real positive_Real)
huffman@36795
   689
apply (drule (1) cauchy_not_vanishes_cases, safe, fast, fast)
paulson@14269
   690
done
paulson@14334
   691
huffman@36795
   692
instantiation real :: linordered_field_inverse_zero
huffman@36795
   693
begin
paulson@14341
   694
huffman@36795
   695
definition
huffman@36795
   696
  "x < y \<longleftrightarrow> positive (y - x)"
paulson@14341
   697
huffman@36795
   698
definition
huffman@36795
   699
  "x \<le> (y::real) \<longleftrightarrow> x < y \<or> x = y"
paulson@14334
   700
huffman@36795
   701
definition
huffman@36795
   702
  "abs (a::real) = (if a < 0 then - a else a)"
paulson@14269
   703
huffman@36795
   704
definition
huffman@36795
   705
  "sgn (a::real) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
paulson@14269
   706
huffman@36795
   707
instance proof
huffman@36795
   708
  fix a b c :: real
huffman@36795
   709
  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
huffman@36795
   710
    by (rule abs_real_def)
huffman@36795
   711
  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
huffman@36795
   712
    unfolding less_eq_real_def less_real_def
huffman@36795
   713
    by (auto, drule (1) positive_add, simp_all add: positive_zero)
huffman@36795
   714
  show "a \<le> a"
huffman@36795
   715
    unfolding less_eq_real_def by simp
huffman@36795
   716
  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
huffman@36795
   717
    unfolding less_eq_real_def less_real_def
huffman@36795
   718
    by (auto, drule (1) positive_add, simp add: algebra_simps)
huffman@36795
   719
  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
huffman@36795
   720
    unfolding less_eq_real_def less_real_def
huffman@36795
   721
    by (auto, drule (1) positive_add, simp add: positive_zero)
huffman@36795
   722
  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
huffman@36795
   723
    unfolding less_eq_real_def less_real_def by auto
huffman@36795
   724
  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
huffman@36795
   725
    by (rule sgn_real_def)
huffman@36795
   726
  show "a \<le> b \<or> b \<le> a"
huffman@36795
   727
    unfolding less_eq_real_def less_real_def
huffman@36795
   728
    by (auto dest!: positive_minus)
huffman@36795
   729
  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
huffman@36795
   730
    unfolding less_real_def
huffman@36795
   731
    by (drule (1) positive_mult, simp add: algebra_simps)
huffman@23288
   732
qed
paulson@14378
   733
huffman@36795
   734
end
paulson@14334
   735
haftmann@25571
   736
instantiation real :: distrib_lattice
haftmann@25571
   737
begin
haftmann@25571
   738
haftmann@25571
   739
definition
huffman@36795
   740
  "(inf :: real \<Rightarrow> real \<Rightarrow> real) = min"
haftmann@25571
   741
haftmann@25571
   742
definition
huffman@36795
   743
  "(sup :: real \<Rightarrow> real \<Rightarrow> real) = max"
huffman@36795
   744
huffman@36795
   745
instance proof
huffman@36795
   746
qed (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
huffman@36795
   747
huffman@36795
   748
end
haftmann@25571
   749
huffman@36795
   750
instantiation real :: number_ring
huffman@36795
   751
begin
huffman@36795
   752
huffman@36795
   753
definition
haftmann@37767
   754
  "(number_of x :: real) = of_int x"
huffman@36795
   755
huffman@36795
   756
instance proof
huffman@36795
   757
qed (rule number_of_real_def)
haftmann@22456
   758
haftmann@25571
   759
end
haftmann@25571
   760
huffman@36795
   761
lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
huffman@36795
   762
apply (induct x)
huffman@36795
   763
apply (simp add: zero_real_def)
huffman@36795
   764
apply (simp add: one_real_def add_Real)
huffman@36795
   765
done
paulson@14378
   766
huffman@36795
   767
lemma of_int_Real: "of_int x = Real (\<lambda>n. of_int x)"
huffman@36795
   768
apply (cases x rule: int_diff_cases)
huffman@36795
   769
apply (simp add: of_nat_Real diff_Real)
huffman@36795
   770
done
paulson@14334
   771
huffman@36795
   772
lemma of_rat_Real: "of_rat x = Real (\<lambda>n. x)"
huffman@36795
   773
apply (induct x)
huffman@36795
   774
apply (simp add: Fract_of_int_quotient of_rat_divide)
huffman@36795
   775
apply (simp add: of_int_Real divide_inverse)
huffman@36795
   776
apply (simp add: inverse_Real mult_Real)
huffman@36795
   777
done
huffman@36795
   778
huffman@36795
   779
instance real :: archimedean_field
paulson@14334
   780
proof
huffman@36795
   781
  fix x :: real
huffman@36795
   782
  show "\<exists>z. x \<le> of_int z"
huffman@36795
   783
    apply (induct x)
huffman@36795
   784
    apply (frule cauchy_imp_bounded, clarify)
huffman@36795
   785
    apply (rule_tac x="ceiling b + 1" in exI)
huffman@36795
   786
    apply (rule less_imp_le)
huffman@36795
   787
    apply (simp add: of_int_Real less_real_def diff_Real positive_Real)
huffman@36795
   788
    apply (rule_tac x=1 in exI, simp add: algebra_simps)
huffman@36795
   789
    apply (rule_tac x=0 in exI, clarsimp)
huffman@36795
   790
    apply (rule le_less_trans [OF abs_ge_self])
huffman@36795
   791
    apply (rule less_le_trans [OF _ le_of_int_ceiling])
huffman@36795
   792
    apply simp
huffman@36795
   793
    done
paulson@14334
   794
qed
paulson@14334
   795
bulwahn@43732
   796
instantiation real :: floor_ceiling
bulwahn@43732
   797
begin
bulwahn@43732
   798
bulwahn@43732
   799
definition [code del]:
bulwahn@43732
   800
  "floor (x::real) = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
bulwahn@43732
   801
bulwahn@43732
   802
instance proof
bulwahn@43732
   803
  fix x :: real
bulwahn@43732
   804
  show "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
bulwahn@43732
   805
    unfolding floor_real_def using floor_exists1 by (rule theI')
bulwahn@43732
   806
qed
bulwahn@43732
   807
bulwahn@43732
   808
end
bulwahn@43732
   809
huffman@36795
   810
subsection {* Completeness *}
paulson@14365
   811
huffman@36795
   812
lemma not_positive_Real:
huffman@36795
   813
  assumes X: "cauchy X"
huffman@36795
   814
  shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
huffman@36795
   815
unfolding positive_Real [OF X]
huffman@36795
   816
apply (auto, unfold not_less)
huffman@36795
   817
apply (erule obtain_pos_sum)
huffman@36795
   818
apply (drule_tac x=s in spec, simp)
huffman@36795
   819
apply (drule_tac r=t in cauchyD [OF X], clarify)
huffman@36795
   820
apply (drule_tac x=k in spec, clarsimp)
huffman@36795
   821
apply (rule_tac x=n in exI, clarify, rename_tac m)
huffman@36795
   822
apply (drule_tac x=m in spec, simp)
huffman@36795
   823
apply (drule_tac x=n in spec, simp)
huffman@36795
   824
apply (drule spec, drule (1) mp, clarify, rename_tac i)
huffman@36795
   825
apply (rule_tac x="max i k" in exI, simp)
huffman@36795
   826
done
huffman@36795
   827
huffman@36795
   828
lemma le_Real:
huffman@36795
   829
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   830
  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
huffman@36795
   831
unfolding not_less [symmetric, where 'a=real] less_real_def
huffman@36795
   832
apply (simp add: diff_Real not_positive_Real X Y)
huffman@36795
   833
apply (simp add: diff_le_eq add_ac)
huffman@36795
   834
done
paulson@14365
   835
huffman@36795
   836
lemma le_RealI:
huffman@36795
   837
  assumes Y: "cauchy Y"
huffman@36795
   838
  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
huffman@36795
   839
proof (induct x)
huffman@36795
   840
  fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
huffman@36795
   841
  hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
huffman@36795
   842
    by (simp add: of_rat_Real le_Real)
huffman@36795
   843
  {
huffman@36795
   844
    fix r :: rat assume "0 < r"
huffman@36795
   845
    then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
   846
      by (rule obtain_pos_sum)
huffman@36795
   847
    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
huffman@36795
   848
      using cauchyD [OF Y s] ..
huffman@36795
   849
    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
huffman@36795
   850
      using le [OF t] ..
huffman@36795
   851
    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
huffman@36795
   852
    proof (clarsimp)
huffman@36795
   853
      fix n assume n: "i \<le> n" "j \<le> n"
huffman@36795
   854
      have "X n \<le> Y i + t" using n j by simp
huffman@36795
   855
      moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
huffman@36795
   856
      ultimately show "X n \<le> Y n + r" unfolding r by simp
huffman@36795
   857
    qed
huffman@36795
   858
    hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
huffman@36795
   859
  }
huffman@36795
   860
  thus "Real X \<le> Real Y"
huffman@36795
   861
    by (simp add: of_rat_Real le_Real X Y)
huffman@36795
   862
qed
paulson@14365
   863
huffman@36795
   864
lemma Real_leI:
huffman@36795
   865
  assumes X: "cauchy X"
huffman@36795
   866
  assumes le: "\<forall>n. of_rat (X n) \<le> y"
huffman@36795
   867
  shows "Real X \<le> y"
huffman@36795
   868
proof -
huffman@36795
   869
  have "- y \<le> - Real X"
huffman@36795
   870
    by (simp add: minus_Real X le_RealI of_rat_minus le)
huffman@36795
   871
  thus ?thesis by simp
huffman@36795
   872
qed
huffman@36795
   873
huffman@36795
   874
lemma less_RealD:
huffman@36795
   875
  assumes Y: "cauchy Y"
huffman@36795
   876
  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
huffman@36795
   877
by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
huffman@36795
   878
huffman@36795
   879
lemma of_nat_less_two_power:
huffman@36795
   880
  "of_nat n < (2::'a::{linordered_idom,number_ring}) ^ n"
huffman@36795
   881
apply (induct n)
huffman@36795
   882
apply simp
huffman@36795
   883
apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
huffman@36795
   884
apply (drule (1) add_le_less_mono, simp)
huffman@36795
   885
apply simp
paulson@14365
   886
done
paulson@14365
   887
huffman@36795
   888
lemma complete_real:
huffman@36795
   889
  fixes S :: "real set"
huffman@36795
   890
  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
huffman@36795
   891
  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
   892
proof -
huffman@36795
   893
  obtain x where x: "x \<in> S" using assms(1) ..
huffman@36795
   894
  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
paulson@14365
   895
huffman@36795
   896
  def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
huffman@36795
   897
  obtain a where a: "\<not> P a"
huffman@36795
   898
  proof
huffman@36795
   899
    have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
huffman@36795
   900
    also have "x - 1 < x" by simp
huffman@36795
   901
    finally have "of_int (floor (x - 1)) < x" .
huffman@36795
   902
    hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
huffman@36795
   903
    then show "\<not> P (of_int (floor (x - 1)))"
huffman@36795
   904
      unfolding P_def of_rat_of_int_eq using x by fast
huffman@36795
   905
  qed
huffman@36795
   906
  obtain b where b: "P b"
huffman@36795
   907
  proof
huffman@36795
   908
    show "P (of_int (ceiling z))"
huffman@36795
   909
    unfolding P_def of_rat_of_int_eq
huffman@36795
   910
    proof
huffman@36795
   911
      fix y assume "y \<in> S"
huffman@36795
   912
      hence "y \<le> z" using z by simp
huffman@36795
   913
      also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
huffman@36795
   914
      finally show "y \<le> of_int (ceiling z)" .
huffman@36795
   915
    qed
huffman@36795
   916
  qed
paulson@14365
   917
huffman@36795
   918
  def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
huffman@36795
   919
  def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
huffman@36795
   920
  def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
huffman@36795
   921
  def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
huffman@36795
   922
  def C \<equiv> "\<lambda>n. avg (A n) (B n)"
huffman@36795
   923
  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
huffman@36795
   924
  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
huffman@36795
   925
  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
huffman@36795
   926
    unfolding A_def B_def C_def bisect_def split_def by simp
huffman@36795
   927
  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
huffman@36795
   928
    unfolding A_def B_def C_def bisect_def split_def by simp
paulson@14365
   929
huffman@36795
   930
  have width: "\<And>n. B n - A n = (b - a) / 2^n"
huffman@36795
   931
    apply (simp add: eq_divide_eq)
huffman@36795
   932
    apply (induct_tac n, simp)
huffman@36795
   933
    apply (simp add: C_def avg_def algebra_simps)
huffman@36795
   934
    done
huffman@36795
   935
huffman@36795
   936
  have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
huffman@36795
   937
    apply (simp add: divide_less_eq)
huffman@36795
   938
    apply (subst mult_commute)
huffman@36795
   939
    apply (frule_tac y=y in ex_less_of_nat_mult)
huffman@36795
   940
    apply clarify
huffman@36795
   941
    apply (rule_tac x=n in exI)
huffman@36795
   942
    apply (erule less_trans)
huffman@36795
   943
    apply (rule mult_strict_right_mono)
huffman@36795
   944
    apply (rule le_less_trans [OF _ of_nat_less_two_power])
huffman@36795
   945
    apply simp
huffman@36795
   946
    apply assumption
huffman@36795
   947
    done
paulson@14365
   948
huffman@36795
   949
  have PA: "\<And>n. \<not> P (A n)"
huffman@36795
   950
    by (induct_tac n, simp_all add: a)
huffman@36795
   951
  have PB: "\<And>n. P (B n)"
huffman@36795
   952
    by (induct_tac n, simp_all add: b)
huffman@36795
   953
  have ab: "a < b"
huffman@36795
   954
    using a b unfolding P_def
huffman@36795
   955
    apply (clarsimp simp add: not_le)
huffman@36795
   956
    apply (drule (1) bspec)
huffman@36795
   957
    apply (drule (1) less_le_trans)
huffman@36795
   958
    apply (simp add: of_rat_less)
huffman@36795
   959
    done
huffman@36795
   960
  have AB: "\<And>n. A n < B n"
huffman@36795
   961
    by (induct_tac n, simp add: ab, simp add: C_def avg_def)
huffman@36795
   962
  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
huffman@36795
   963
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   964
    apply (erule less_Suc_induct)
huffman@36795
   965
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   966
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   967
    apply (rule AB [THEN less_imp_le])
huffman@36795
   968
    apply simp
huffman@36795
   969
    done
huffman@36795
   970
  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
huffman@36795
   971
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   972
    apply (erule less_Suc_induct)
huffman@36795
   973
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   974
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   975
    apply (rule AB [THEN less_imp_le])
huffman@36795
   976
    apply simp
huffman@36795
   977
    done
huffman@36795
   978
  have cauchy_lemma:
huffman@36795
   979
    "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
huffman@36795
   980
    apply (rule cauchyI)
huffman@36795
   981
    apply (drule twos [where y="b - a"])
huffman@36795
   982
    apply (erule exE)
huffman@36795
   983
    apply (rule_tac x=n in exI, clarify, rename_tac i j)
huffman@36795
   984
    apply (rule_tac y="B n - A n" in le_less_trans) defer
huffman@36795
   985
    apply (simp add: width)
huffman@36795
   986
    apply (drule_tac x=n in spec)
huffman@36795
   987
    apply (frule_tac x=i in spec, drule (1) mp)
huffman@36795
   988
    apply (frule_tac x=j in spec, drule (1) mp)
huffman@36795
   989
    apply (frule A_mono, drule B_mono)
huffman@36795
   990
    apply (frule A_mono, drule B_mono)
huffman@36795
   991
    apply arith
huffman@36795
   992
    done
huffman@36795
   993
  have "cauchy A"
huffman@36795
   994
    apply (rule cauchy_lemma [rule_format])
huffman@36795
   995
    apply (simp add: A_mono)
huffman@36795
   996
    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
huffman@36795
   997
    done
huffman@36795
   998
  have "cauchy B"
huffman@36795
   999
    apply (rule cauchy_lemma [rule_format])
huffman@36795
  1000
    apply (simp add: B_mono)
huffman@36795
  1001
    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
huffman@36795
  1002
    done
huffman@36795
  1003
  have 1: "\<forall>x\<in>S. x \<le> Real B"
huffman@36795
  1004
  proof
huffman@36795
  1005
    fix x assume "x \<in> S"
huffman@36795
  1006
    then show "x \<le> Real B"
huffman@36795
  1007
      using PB [unfolded P_def] `cauchy B`
huffman@36795
  1008
      by (simp add: le_RealI)
huffman@36795
  1009
  qed
huffman@36795
  1010
  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
huffman@36795
  1011
    apply clarify
huffman@36795
  1012
    apply (erule contrapos_pp)
huffman@36795
  1013
    apply (simp add: not_le)
huffman@36795
  1014
    apply (drule less_RealD [OF `cauchy A`], clarify)
huffman@36795
  1015
    apply (subgoal_tac "\<not> P (A n)")
huffman@36795
  1016
    apply (simp add: P_def not_le, clarify)
huffman@36795
  1017
    apply (erule rev_bexI)
huffman@36795
  1018
    apply (erule (1) less_trans)
huffman@36795
  1019
    apply (simp add: PA)
huffman@36795
  1020
    done
huffman@36795
  1021
  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
huffman@36795
  1022
  proof (rule vanishesI)
huffman@36795
  1023
    fix r :: rat assume "0 < r"
huffman@36795
  1024
    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
huffman@36795
  1025
      using twos by fast
huffman@36795
  1026
    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
huffman@36795
  1027
    proof (clarify)
huffman@36795
  1028
      fix n assume n: "k \<le> n"
huffman@36795
  1029
      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
huffman@36795
  1030
        by simp
huffman@36795
  1031
      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
huffman@36795
  1032
        using n by (simp add: divide_left_mono mult_pos_pos)
huffman@36795
  1033
      also note k
huffman@36795
  1034
      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
huffman@36795
  1035
    qed
huffman@36795
  1036
    thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
huffman@36795
  1037
  qed
huffman@36795
  1038
  hence 3: "Real B = Real A"
huffman@36795
  1039
    by (simp add: eq_Real `cauchy A` `cauchy B` width)
huffman@36795
  1040
  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
  1041
    using 1 2 3 by (rule_tac x="Real B" in exI, simp)
paulson@14484
  1042
qed
paulson@14365
  1043
huffman@36795
  1044
subsection {* Hiding implementation details *}
paulson@14365
  1045
huffman@36795
  1046
hide_const (open) vanishes cauchy positive Real real_case
paulson@14365
  1047
huffman@36795
  1048
declare Real_induct [induct del]
huffman@36795
  1049
declare Abs_real_induct [induct del]
huffman@36795
  1050
declare Abs_real_cases [cases del]
huffman@36795
  1051
huffman@36795
  1052
subsection {* Legacy theorem names *}
huffman@36795
  1053
huffman@36795
  1054
text {* TODO: Could we have a way to mark theorem names as deprecated,
huffman@36795
  1055
and have Isabelle issue a warning and display the preferred name? *}
paulson@14365
  1056
huffman@36795
  1057
lemmas real_diff_def = minus_real_def [of "r" "s", standard]
huffman@36795
  1058
lemmas real_divide_def = divide_real_def [of "R" "S", standard]
huffman@36795
  1059
lemmas real_less_def = less_le [of "x::real" "y", standard]
huffman@36795
  1060
lemmas real_abs_def = abs_real_def [of "r", standard]
huffman@36795
  1061
lemmas real_sgn_def = sgn_real_def [of "x", standard]
huffman@36796
  1062
lemmas real_mult_commute = mult_commute [of "z::real" "w", standard]
huffman@36795
  1063
lemmas real_mult_assoc = mult_assoc [of "z1::real" "z2" "z3", standard]
huffman@36795
  1064
lemmas real_mult_1 = mult_1_left [of "z::real", standard]
huffman@36795
  1065
lemmas real_add_mult_distrib = left_distrib [of "z1::real" "z2" "w", standard]
huffman@36795
  1066
lemmas real_zero_not_eq_one = zero_neq_one [where 'a=real]
huffman@36795
  1067
lemmas real_mult_inverse_left = left_inverse [of "x::real", standard]
huffman@36795
  1068
lemmas INVERSE_ZERO = inverse_zero [where 'a=real]
huffman@36795
  1069
lemmas real_le_refl = order_refl [of "w::real", standard]
huffman@36795
  1070
lemmas real_le_antisym = order_antisym [of "z::real" "w", standard]
huffman@36795
  1071
lemmas real_le_trans = order_trans [of "i::real" "j" "k", standard]
huffman@36941
  1072
lemmas real_le_linear = linear [of "z::real" "w", standard]
huffman@36795
  1073
lemmas real_le_eq_diff = le_iff_diff_le_0 [of "x::real" "y", standard]
huffman@36795
  1074
lemmas real_add_left_mono = add_left_mono [of "x::real" "y" "z", standard]
huffman@36795
  1075
lemmas real_mult_order = mult_pos_pos [of "x::real" "y", standard]
huffman@36795
  1076
lemmas real_mult_less_mono2 =
huffman@36795
  1077
  mult_strict_left_mono [of "x::real" "y" "z", COMP swap_prems_rl, standard]
paulson@14334
  1078
paulson@14334
  1079
subsection{*More Lemmas*}
paulson@14334
  1080
huffman@36776
  1081
text {* BH: These lemmas should not be necessary; they should be
huffman@36776
  1082
covered by existing simp rules and simplification procedures. *}
huffman@36776
  1083
paulson@14334
  1084
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
huffman@36776
  1085
by simp (* redundant with mult_cancel_left *)
paulson@14334
  1086
paulson@14334
  1087
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
huffman@36776
  1088
by simp (* redundant with mult_cancel_right *)
paulson@14334
  1089
paulson@14334
  1090
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
huffman@36776
  1091
by simp (* solved by linordered_ring_less_cancel_factor simproc *)
paulson@14334
  1092
paulson@14334
  1093
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
huffman@36776
  1094
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
paulson@14334
  1095
paulson@14334
  1096
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
huffman@36776
  1097
by (rule mult_le_cancel_left_pos)
huffman@36776
  1098
(* BH: Why doesn't "simp" prove this one, like it does the last one? *)
paulson@14334
  1099
paulson@14334
  1100
haftmann@24198
  1101
subsection {* Embedding numbers into the Reals *}
haftmann@24198
  1102
haftmann@24198
  1103
abbreviation
haftmann@24198
  1104
  real_of_nat :: "nat \<Rightarrow> real"
haftmann@24198
  1105
where
haftmann@24198
  1106
  "real_of_nat \<equiv> of_nat"
haftmann@24198
  1107
haftmann@24198
  1108
abbreviation
haftmann@24198
  1109
  real_of_int :: "int \<Rightarrow> real"
haftmann@24198
  1110
where
haftmann@24198
  1111
  "real_of_int \<equiv> of_int"
haftmann@24198
  1112
haftmann@24198
  1113
abbreviation
haftmann@24198
  1114
  real_of_rat :: "rat \<Rightarrow> real"
haftmann@24198
  1115
where
haftmann@24198
  1116
  "real_of_rat \<equiv> of_rat"
haftmann@24198
  1117
haftmann@24198
  1118
consts
haftmann@24198
  1119
  (*overloaded constant for injecting other types into "real"*)
haftmann@24198
  1120
  real :: "'a => real"
paulson@14365
  1121
paulson@14378
  1122
defs (overloaded)
haftmann@31998
  1123
  real_of_nat_def [code_unfold]: "real == real_of_nat"
haftmann@31998
  1124
  real_of_int_def [code_unfold]: "real == real_of_int"
paulson@14365
  1125
wenzelm@40939
  1126
declare [[coercion_enabled]]
nipkow@40864
  1127
declare [[coercion "real::nat\<Rightarrow>real"]]
nipkow@40864
  1128
declare [[coercion "real::int\<Rightarrow>real"]]
nipkow@41022
  1129
declare [[coercion "int"]]
nipkow@40864
  1130
hoelzl@41024
  1131
declare [[coercion_map map]]
noschinl@42112
  1132
declare [[coercion_map "% f g h x. g (h (f x))"]]
hoelzl@41024
  1133
declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
hoelzl@41024
  1134
avigad@16819
  1135
lemma real_eq_of_nat: "real = of_nat"
haftmann@24198
  1136
  unfolding real_of_nat_def ..
avigad@16819
  1137
avigad@16819
  1138
lemma real_eq_of_int: "real = of_int"
haftmann@24198
  1139
  unfolding real_of_int_def ..
avigad@16819
  1140
paulson@14365
  1141
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
  1142
by (simp add: real_of_int_def) 
paulson@14365
  1143
paulson@14365
  1144
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
  1145
by (simp add: real_of_int_def) 
paulson@14334
  1146
avigad@16819
  1147
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
  1148
by (simp add: real_of_int_def) 
paulson@14365
  1149
avigad@16819
  1150
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
  1151
by (simp add: real_of_int_def) 
avigad@16819
  1152
avigad@16819
  1153
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
  1154
by (simp add: real_of_int_def) 
paulson@14365
  1155
avigad@16819
  1156
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
  1157
by (simp add: real_of_int_def) 
paulson@14334
  1158
huffman@35344
  1159
lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
huffman@35344
  1160
by (simp add: real_of_int_def of_int_power)
huffman@35344
  1161
huffman@35344
  1162
lemmas power_real_of_int = real_of_int_power [symmetric]
huffman@35344
  1163
avigad@16819
  1164
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
  1165
  apply (subst real_eq_of_int)+
avigad@16819
  1166
  apply (rule of_int_setsum)
avigad@16819
  1167
done
avigad@16819
  1168
avigad@16819
  1169
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
  1170
    (PROD x:A. real(f x))"
avigad@16819
  1171
  apply (subst real_eq_of_int)+
avigad@16819
  1172
  apply (rule of_int_setprod)
avigad@16819
  1173
done
paulson@14365
  1174
chaieb@27668
  1175
lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
paulson@14378
  1176
by (simp add: real_of_int_def) 
paulson@14365
  1177
chaieb@27668
  1178
lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
paulson@14378
  1179
by (simp add: real_of_int_def) 
paulson@14365
  1180
chaieb@27668
  1181
lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
paulson@14378
  1182
by (simp add: real_of_int_def) 
paulson@14365
  1183
chaieb@27668
  1184
lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
  1185
by (simp add: real_of_int_def) 
paulson@14365
  1186
chaieb@27668
  1187
lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
  1188
by (simp add: real_of_int_def) 
avigad@16819
  1189
chaieb@27668
  1190
lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
  1191
by (simp add: real_of_int_def) 
avigad@16819
  1192
chaieb@27668
  1193
lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
avigad@16819
  1194
by (simp add: real_of_int_def)
avigad@16819
  1195
chaieb@27668
  1196
lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
  1197
by (simp add: real_of_int_def)
avigad@16819
  1198
avigad@16888
  1199
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
  1200
by (auto simp add: abs_if)
avigad@16888
  1201
avigad@16819
  1202
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
  1203
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
  1204
  apply (simp del: real_of_int_add)
avigad@16819
  1205
  apply auto
avigad@16819
  1206
done
avigad@16819
  1207
avigad@16819
  1208
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
  1209
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
  1210
  apply (simp del: real_of_int_add)
avigad@16819
  1211
  apply simp
avigad@16819
  1212
done
avigad@16819
  1213
avigad@16819
  1214
lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
avigad@16819
  1215
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1216
proof -
wenzelm@41550
  1217
  assume d: "d ~= 0"
avigad@16819
  1218
  have "x = (x div d) * d + x mod d"
avigad@16819
  1219
    by auto
avigad@16819
  1220
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1221
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
  1222
  then have "real x / real d = ... / real d"
avigad@16819
  1223
    by simp
avigad@16819
  1224
  then show ?thesis
wenzelm@41550
  1225
    by (auto simp add: add_divide_distrib algebra_simps d)
avigad@16819
  1226
qed
avigad@16819
  1227
avigad@16819
  1228
lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
avigad@16819
  1229
    real(n div d) = real n / real d"
avigad@16819
  1230
  apply (frule real_of_int_div_aux [of d n])
avigad@16819
  1231
  apply simp
nipkow@30042
  1232
  apply (simp add: dvd_eq_mod_eq_0)
avigad@16819
  1233
done
avigad@16819
  1234
avigad@16819
  1235
lemma real_of_int_div2:
avigad@16819
  1236
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
  1237
  apply (case_tac "x = 0")
avigad@16819
  1238
  apply simp
avigad@16819
  1239
  apply (case_tac "0 < x")
nipkow@29667
  1240
  apply (simp add: algebra_simps)
avigad@16819
  1241
  apply (subst real_of_int_div_aux)
avigad@16819
  1242
  apply simp
avigad@16819
  1243
  apply simp
avigad@16819
  1244
  apply (subst zero_le_divide_iff)
avigad@16819
  1245
  apply auto
nipkow@29667
  1246
  apply (simp add: algebra_simps)
avigad@16819
  1247
  apply (subst real_of_int_div_aux)
avigad@16819
  1248
  apply simp
avigad@16819
  1249
  apply simp
avigad@16819
  1250
  apply (subst zero_le_divide_iff)
avigad@16819
  1251
  apply auto
avigad@16819
  1252
done
avigad@16819
  1253
avigad@16819
  1254
lemma real_of_int_div3:
avigad@16819
  1255
  "real (n::int) / real (x) - real (n div x) <= 1"
avigad@16819
  1256
  apply(case_tac "x = 0")
avigad@16819
  1257
  apply simp
nipkow@29667
  1258
  apply (simp add: algebra_simps)
avigad@16819
  1259
  apply (subst real_of_int_div_aux)
avigad@16819
  1260
  apply assumption
avigad@16819
  1261
  apply simp
avigad@16819
  1262
  apply (subst divide_le_eq)
avigad@16819
  1263
  apply clarsimp
avigad@16819
  1264
  apply (rule conjI)
avigad@16819
  1265
  apply (rule impI)
avigad@16819
  1266
  apply (rule order_less_imp_le)
avigad@16819
  1267
  apply simp
avigad@16819
  1268
  apply (rule impI)
avigad@16819
  1269
  apply (rule order_less_imp_le)
avigad@16819
  1270
  apply simp
avigad@16819
  1271
done
avigad@16819
  1272
avigad@16819
  1273
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
nipkow@27964
  1274
by (insert real_of_int_div2 [of n x], simp)
nipkow@27964
  1275
huffman@35635
  1276
lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
huffman@35635
  1277
unfolding real_of_int_def by (rule Ints_of_int)
huffman@35635
  1278
nipkow@27964
  1279
paulson@14365
  1280
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
  1281
paulson@14334
  1282
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
  1283
by (simp add: real_of_nat_def)
paulson@14334
  1284
huffman@30082
  1285
lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
huffman@30082
  1286
by (simp add: real_of_nat_def)
huffman@30082
  1287
paulson@14334
  1288
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
  1289
by (simp add: real_of_nat_def)
paulson@14334
  1290
paulson@14365
  1291
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
  1292
by (simp add: real_of_nat_def)
paulson@14334
  1293
paulson@14334
  1294
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
  1295
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
  1296
by (simp add: real_of_nat_def)
paulson@14334
  1297
paulson@14334
  1298
lemma real_of_nat_less_iff [iff]: 
paulson@14334
  1299
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
  1300
by (simp add: real_of_nat_def)
paulson@14334
  1301
paulson@14334
  1302
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
  1303
by (simp add: real_of_nat_def)
paulson@14334
  1304
paulson@14334
  1305
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
paulson@14378
  1306
by (simp add: real_of_nat_def zero_le_imp_of_nat)
paulson@14334
  1307
paulson@14365
  1308
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
  1309
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
  1310
paulson@14334
  1311
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
huffman@23431
  1312
by (simp add: real_of_nat_def of_nat_mult)
paulson@14334
  1313
huffman@35344
  1314
lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
huffman@35344
  1315
by (simp add: real_of_nat_def of_nat_power)
huffman@35344
  1316
huffman@35344
  1317
lemmas power_real_of_nat = real_of_nat_power [symmetric]
huffman@35344
  1318
avigad@16819
  1319
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
  1320
    (SUM x:A. real(f x))"
avigad@16819
  1321
  apply (subst real_eq_of_nat)+
avigad@16819
  1322
  apply (rule of_nat_setsum)
avigad@16819
  1323
done
avigad@16819
  1324
avigad@16819
  1325
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
  1326
    (PROD x:A. real(f x))"
avigad@16819
  1327
  apply (subst real_eq_of_nat)+
avigad@16819
  1328
  apply (rule of_nat_setprod)
avigad@16819
  1329
done
avigad@16819
  1330
avigad@16819
  1331
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
  1332
  apply (subst card_eq_setsum)
avigad@16819
  1333
  apply (subst real_of_nat_setsum)
avigad@16819
  1334
  apply simp
avigad@16819
  1335
done
avigad@16819
  1336
paulson@14334
  1337
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
  1338
by (simp add: real_of_nat_def)
paulson@14334
  1339
paulson@14387
  1340
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
  1341
by (simp add: real_of_nat_def)
paulson@14334
  1342
paulson@14365
  1343
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
huffman@23438
  1344
by (simp add: add: real_of_nat_def of_nat_diff)
paulson@14334
  1345
nipkow@25162
  1346
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
nipkow@25140
  1347
by (auto simp: real_of_nat_def)
paulson@14365
  1348
paulson@14365
  1349
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
  1350
by (simp add: add: real_of_nat_def)
paulson@14334
  1351
paulson@14365
  1352
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
  1353
by (simp add: add: real_of_nat_def)
paulson@14334
  1354
avigad@16819
  1355
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
  1356
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
  1357
  apply simp
avigad@16819
  1358
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
  1359
done
avigad@16819
  1360
avigad@16819
  1361
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
  1362
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
  1363
  apply (simp add: less_Suc_eq_le)
avigad@16819
  1364
  apply (simp add: real_of_nat_Suc)
avigad@16819
  1365
done
avigad@16819
  1366
avigad@16819
  1367
lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
avigad@16819
  1368
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1369
proof -
wenzelm@41550
  1370
  assume d: "0 < d"
avigad@16819
  1371
  have "x = (x div d) * d + x mod d"
avigad@16819
  1372
    by auto
avigad@16819
  1373
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1374
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
  1375
  then have "real x / real d = \<dots> / real d"
avigad@16819
  1376
    by simp
avigad@16819
  1377
  then show ?thesis
wenzelm@41550
  1378
    by (auto simp add: add_divide_distrib algebra_simps d)
avigad@16819
  1379
qed
avigad@16819
  1380
avigad@16819
  1381
lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
avigad@16819
  1382
    real(n div d) = real n / real d"
avigad@16819
  1383
  apply (frule real_of_nat_div_aux [of d n])
avigad@16819
  1384
  apply simp
avigad@16819
  1385
  apply (subst dvd_eq_mod_eq_0 [THEN sym])
avigad@16819
  1386
  apply assumption
avigad@16819
  1387
done
avigad@16819
  1388
avigad@16819
  1389
lemma real_of_nat_div2:
avigad@16819
  1390
  "0 <= real (n::nat) / real (x) - real (n div x)"
nipkow@25134
  1391
apply(case_tac "x = 0")
nipkow@25134
  1392
 apply (simp)
nipkow@29667
  1393
apply (simp add: algebra_simps)
nipkow@25134
  1394
apply (subst real_of_nat_div_aux)
nipkow@25134
  1395
 apply simp
nipkow@25134
  1396
apply simp
nipkow@25134
  1397
apply (subst zero_le_divide_iff)
nipkow@25134
  1398
apply simp
avigad@16819
  1399
done
avigad@16819
  1400
avigad@16819
  1401
lemma real_of_nat_div3:
avigad@16819
  1402
  "real (n::nat) / real (x) - real (n div x) <= 1"
nipkow@25134
  1403
apply(case_tac "x = 0")
nipkow@25134
  1404
apply (simp)
nipkow@29667
  1405
apply (simp add: algebra_simps)
nipkow@25134
  1406
apply (subst real_of_nat_div_aux)
nipkow@25134
  1407
 apply simp
nipkow@25134
  1408
apply simp
avigad@16819
  1409
done
avigad@16819
  1410
avigad@16819
  1411
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
nipkow@29667
  1412
by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
  1413
paulson@14365
  1414
lemma real_of_int_real_of_nat: "real (int n) = real n"
paulson@14378
  1415
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
paulson@14378
  1416
paulson@14426
  1417
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
  1418
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
  1419
avigad@16819
  1420
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
  1421
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
  1422
  apply force
avigad@16819
  1423
  apply (simp only: real_of_int_real_of_nat)
avigad@16819
  1424
done
paulson@14387
  1425
huffman@35635
  1426
lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
huffman@35635
  1427
unfolding real_of_nat_def by (rule of_nat_in_Nats)
huffman@35635
  1428
huffman@35635
  1429
lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
huffman@35635
  1430
unfolding real_of_nat_def by (rule Ints_of_nat)
huffman@35635
  1431
nipkow@28001
  1432
nipkow@28001
  1433
subsection{* Rationals *}
nipkow@28001
  1434
nipkow@28091
  1435
lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
nipkow@28091
  1436
by (simp add: real_eq_of_nat)
nipkow@28091
  1437
nipkow@28091
  1438
nipkow@28001
  1439
lemma Rats_eq_int_div_int:
nipkow@28091
  1440
  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
nipkow@28001
  1441
proof
nipkow@28091
  1442
  show "\<rat> \<subseteq> ?S"
nipkow@28001
  1443
  proof
nipkow@28091
  1444
    fix x::real assume "x : \<rat>"
nipkow@28001
  1445
    then obtain r where "x = of_rat r" unfolding Rats_def ..
nipkow@28001
  1446
    have "of_rat r : ?S"
nipkow@28001
  1447
      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1448
    thus "x : ?S" using `x = of_rat r` by simp
nipkow@28001
  1449
  qed
nipkow@28001
  1450
next
nipkow@28091
  1451
  show "?S \<subseteq> \<rat>"
nipkow@28001
  1452
  proof(auto simp:Rats_def)
nipkow@28001
  1453
    fix i j :: int assume "j \<noteq> 0"
nipkow@28001
  1454
    hence "real i / real j = of_rat(Fract i j)"
nipkow@28001
  1455
      by (simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1456
    thus "real i / real j \<in> range of_rat" by blast
nipkow@28001
  1457
  qed
nipkow@28001
  1458
qed
nipkow@28001
  1459
nipkow@28001
  1460
lemma Rats_eq_int_div_nat:
nipkow@28091
  1461
  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
nipkow@28001
  1462
proof(auto simp:Rats_eq_int_div_int)
nipkow@28001
  1463
  fix i j::int assume "j \<noteq> 0"
nipkow@28001
  1464
  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
nipkow@28001
  1465
  proof cases
nipkow@28001
  1466
    assume "j>0"
nipkow@28001
  1467
    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
nipkow@28001
  1468
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1469
    thus ?thesis by blast
nipkow@28001
  1470
  next
nipkow@28001
  1471
    assume "~ j>0"
nipkow@28001
  1472
    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
nipkow@28001
  1473
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1474
    thus ?thesis by blast
nipkow@28001
  1475
  qed
nipkow@28001
  1476
next
nipkow@28001
  1477
  fix i::int and n::nat assume "0 < n"
nipkow@28001
  1478
  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
nipkow@28001
  1479
  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
nipkow@28001
  1480
qed
nipkow@28001
  1481
nipkow@28001
  1482
lemma Rats_abs_nat_div_natE:
nipkow@28001
  1483
  assumes "x \<in> \<rat>"
huffman@31706
  1484
  obtains m n :: nat
huffman@31706
  1485
  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
nipkow@28001
  1486
proof -
nipkow@28001
  1487
  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
nipkow@28001
  1488
    by(auto simp add: Rats_eq_int_div_nat)
nipkow@28001
  1489
  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
nipkow@28001
  1490
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
nipkow@28001
  1491
  let ?gcd = "gcd m n"
huffman@31706
  1492
  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
nipkow@28001
  1493
  let ?k = "m div ?gcd"
nipkow@28001
  1494
  let ?l = "n div ?gcd"
nipkow@28001
  1495
  let ?gcd' = "gcd ?k ?l"
nipkow@28001
  1496
  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
nipkow@28001
  1497
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1498
  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
nipkow@28001
  1499
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1500
  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
nipkow@28001
  1501
  moreover
nipkow@28001
  1502
  have "\<bar>x\<bar> = real ?k / real ?l"
nipkow@28001
  1503
  proof -
nipkow@28001
  1504
    from gcd have "real ?k / real ?l =
nipkow@28001
  1505
        real (?gcd * ?k) / real (?gcd * ?l)" by simp
nipkow@28001
  1506
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
nipkow@28001
  1507
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
nipkow@28001
  1508
    finally show ?thesis ..
nipkow@28001
  1509
  qed
nipkow@28001
  1510
  moreover
nipkow@28001
  1511
  have "?gcd' = 1"
nipkow@28001
  1512
  proof -
nipkow@28001
  1513
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
nipkow@31952
  1514
      by (rule gcd_mult_distrib_nat)
nipkow@28001
  1515
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
huffman@31706
  1516
    with gcd show ?thesis by auto
nipkow@28001
  1517
  qed
nipkow@28001
  1518
  ultimately show ?thesis ..
nipkow@28001
  1519
qed
nipkow@28001
  1520
nipkow@28001
  1521
paulson@14387
  1522
subsection{*Numerals and Arithmetic*}
paulson@14387
  1523
haftmann@32069
  1524
lemma [code_unfold_post]:
haftmann@24198
  1525
  "number_of k = real_of_int (number_of k)"
huffman@36795
  1526
  unfolding number_of_is_id number_of_real_def ..
paulson@14387
  1527
paulson@14387
  1528
paulson@14387
  1529
text{*Collapse applications of @{term real} to @{term number_of}*}
paulson@14387
  1530
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
huffman@35216
  1531
by (simp add: real_of_int_def)
paulson@14387
  1532
paulson@14387
  1533
lemma real_of_nat_number_of [simp]:
paulson@14387
  1534
     "real (number_of v :: nat) =  
paulson@14387
  1535
        (if neg (number_of v :: int) then 0  
paulson@14387
  1536
         else (number_of v :: real))"
huffman@35216
  1537
by (simp add: real_of_int_real_of_nat [symmetric])
paulson@14387
  1538
haftmann@31100
  1539
declaration {*
haftmann@31100
  1540
  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
haftmann@31100
  1541
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
haftmann@31100
  1542
  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
haftmann@31100
  1543
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
haftmann@31100
  1544
  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
haftmann@31100
  1545
      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
haftmann@31100
  1546
      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
haftmann@31100
  1547
      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
haftmann@31100
  1548
      @{thm real_of_nat_number_of}, @{thm real_number_of}]
huffman@36795
  1549
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
huffman@36795
  1550
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
haftmann@31100
  1551
*}
paulson@14387
  1552
kleing@19023
  1553
paulson@14387
  1554
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
  1555
paulson@14387
  1556
text{*Needed in this non-standard form by Hyperreal/Transcendental*}
paulson@14387
  1557
lemma real_0_le_divide_iff:
paulson@14387
  1558
     "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
huffman@36795
  1559
by (auto simp add: zero_le_divide_iff)
paulson@14387
  1560
paulson@14387
  1561
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
  1562
by arith
paulson@14387
  1563
huffman@36839
  1564
text {* FIXME: redundant with @{text add_eq_0_iff} below *}
paulson@15085
  1565
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
  1566
by auto
paulson@14387
  1567
paulson@15085
  1568
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
  1569
by auto
paulson@14387
  1570
paulson@15085
  1571
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
  1572
by auto
paulson@14387
  1573
paulson@15085
  1574
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
  1575
by auto
paulson@14387
  1576
paulson@15085
  1577
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
  1578
by auto
paulson@14387
  1579
huffman@36839
  1580
subsection {* Lemmas about powers *}
paulson@14387
  1581
huffman@36839
  1582
text {* FIXME: declare this in Rings.thy or not at all *}
huffman@36839
  1583
declare abs_mult_self [simp]
huffman@36839
  1584
huffman@36839
  1585
(* used by Import/HOL/real.imp *)
huffman@36839
  1586
lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
huffman@36839
  1587
by simp
huffman@36839
  1588
huffman@36839
  1589
lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
huffman@36839
  1590
apply (induct "n")
huffman@36839
  1591
apply (auto simp add: real_of_nat_Suc)
huffman@36839
  1592
apply (subst mult_2)
huffman@36839
  1593
apply (erule add_less_le_mono)
huffman@36839
  1594
apply (rule two_realpow_ge_one)
huffman@36839
  1595
done
huffman@36839
  1596
huffman@36839
  1597
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1598
lemma realpow_Suc_le_self:
huffman@36839
  1599
  fixes r :: "'a::linordered_semidom"
huffman@36839
  1600
  shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
huffman@36839
  1601
by (insert power_decreasing [of 1 "Suc n" r], simp)
huffman@36839
  1602
huffman@36839
  1603
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1604
lemma realpow_minus_mult:
huffman@36839
  1605
  fixes x :: "'a::monoid_mult"
huffman@36839
  1606
  shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
huffman@36839
  1607
by (simp add: power_commutes split add: nat_diff_split)
huffman@36839
  1608
huffman@36839
  1609
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1610
lemma realpow_two_diff:
huffman@36839
  1611
  fixes x :: "'a::comm_ring_1"
huffman@36839
  1612
  shows "x^Suc (Suc 0) - y^Suc (Suc 0) = (x - y) * (x + y)"
huffman@36839
  1613
by (simp add: algebra_simps)
huffman@36839
  1614
huffman@36839
  1615
text {* TODO: move elsewhere *}
huffman@36839
  1616
lemma add_eq_0_iff:
huffman@36839
  1617
  fixes x y :: "'a::group_add"
huffman@36839
  1618
  shows "x + y = 0 \<longleftrightarrow> y = - x"
huffman@36839
  1619
by (auto dest: minus_unique)
huffman@36839
  1620
huffman@36839
  1621
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1622
lemma realpow_two_disj:
huffman@36839
  1623
  fixes x :: "'a::idom"
huffman@36839
  1624
  shows "(x^Suc (Suc 0) = y^Suc (Suc 0)) = (x = y | x = -y)"
huffman@36839
  1625
using realpow_two_diff [of x y]
huffman@36839
  1626
by (auto simp add: add_eq_0_iff)
huffman@36839
  1627
huffman@36839
  1628
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1629
lemma real_two_squares_add_zero_iff [simp]:
huffman@36839
  1630
  "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
huffman@36839
  1631
by (rule sum_squares_eq_zero_iff)
huffman@36839
  1632
huffman@36839
  1633
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1634
lemma real_squared_diff_one_factored:
huffman@36839
  1635
  fixes x :: "'a::ring_1"
huffman@36839
  1636
  shows "x * x - 1 = (x + 1) * (x - 1)"
huffman@36839
  1637
by (simp add: algebra_simps)
huffman@36839
  1638
huffman@36839
  1639
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1640
lemma realpow_two_sum_zero_iff [simp]:
huffman@36839
  1641
     "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
huffman@36839
  1642
by (rule sum_power2_eq_zero_iff)
huffman@36839
  1643
huffman@36839
  1644
lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
huffman@36839
  1645
by (rule_tac y = 0 in order_trans, auto)
huffman@36839
  1646
huffman@36839
  1647
lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
huffman@36839
  1648
by (auto simp add: power2_eq_square)
huffman@36839
  1649
huffman@36839
  1650
huffman@36839
  1651
subsection{*Density of the Reals*}
paulson@14387
  1652
paulson@14387
  1653
lemma real_lbound_gt_zero:
paulson@14387
  1654
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
  1655
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
  1656
apply (simp add: min_def)
paulson@14387
  1657
done
paulson@14387
  1658
paulson@14387
  1659
haftmann@35050
  1660
text{*Similar results are proved in @{text Fields}*}
paulson@14387
  1661
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
  1662
  by auto
paulson@14387
  1663
paulson@14387
  1664
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
  1665
  by auto
paulson@14387
  1666
paulson@14387
  1667
paulson@14387
  1668
subsection{*Absolute Value Function for the Reals*}
paulson@14387
  1669
paulson@14387
  1670
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
  1671
by (simp add: abs_if)
paulson@14387
  1672
huffman@23289
  1673
(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
paulson@14387
  1674
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
haftmann@35050
  1675
by (force simp add: abs_le_iff)
paulson@14387
  1676
paulson@14387
  1677
lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
paulson@15003
  1678
by (simp add: abs_if)
paulson@14387
  1679
paulson@14387
  1680
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
  1681
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
  1682
paulson@14387
  1683
lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
webertj@20217
  1684
by simp
paulson@14387
  1685
 
paulson@14387
  1686
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
  1687
by simp
paulson@14387
  1688
berghofe@24534
  1689
haftmann@27544
  1690
subsection {* Implementation of rational real numbers *}
berghofe@24534
  1691
haftmann@27544
  1692
definition Ratreal :: "rat \<Rightarrow> real" where
haftmann@27544
  1693
  [simp]: "Ratreal = of_rat"
berghofe@24534
  1694
haftmann@24623
  1695
code_datatype Ratreal
berghofe@24534
  1696
haftmann@31998
  1697
lemma Ratreal_number_collapse [code_post]:
haftmann@27544
  1698
  "Ratreal 0 = 0"
haftmann@27544
  1699
  "Ratreal 1 = 1"
haftmann@27544
  1700
  "Ratreal (number_of k) = number_of k"
haftmann@27544
  1701
by simp_all
berghofe@24534
  1702
haftmann@31998
  1703
lemma zero_real_code [code, code_unfold]:
haftmann@27544
  1704
  "0 = Ratreal 0"
haftmann@27544
  1705
by simp
berghofe@24534
  1706
haftmann@31998
  1707
lemma one_real_code [code, code_unfold]:
haftmann@27544
  1708
  "1 = Ratreal 1"
haftmann@27544
  1709
by simp
haftmann@27544
  1710
haftmann@31998
  1711
lemma number_of_real_code [code_unfold]:
haftmann@27544
  1712
  "number_of k = Ratreal (number_of k)"
haftmann@27544
  1713
by simp
haftmann@27544
  1714
haftmann@31998
  1715
lemma Ratreal_number_of_quotient [code_post]:
haftmann@27544
  1716
  "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
haftmann@27544
  1717
by simp
haftmann@27544
  1718
haftmann@31998
  1719
lemma Ratreal_number_of_quotient2 [code_post]:
haftmann@27544
  1720
  "Ratreal (number_of r / number_of s) = number_of r / number_of s"
haftmann@27544
  1721
unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
berghofe@24534
  1722
haftmann@38857
  1723
instantiation real :: equal
haftmann@26513
  1724
begin
haftmann@26513
  1725
haftmann@38857
  1726
definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
haftmann@26513
  1727
haftmann@38857
  1728
instance proof
haftmann@38857
  1729
qed (simp add: equal_real_def)
berghofe@24534
  1730
haftmann@38857
  1731
lemma real_equal_code [code]:
haftmann@38857
  1732
  "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
haftmann@38857
  1733
  by (simp add: equal_real_def equal)
haftmann@26513
  1734
haftmann@38857
  1735
lemma [code nbe]:
haftmann@38857
  1736
  "HOL.equal (x::real) x \<longleftrightarrow> True"
haftmann@38857
  1737
  by (rule equal_refl)
haftmann@28351
  1738
haftmann@26513
  1739
end
berghofe@24534
  1740
haftmann@27544
  1741
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
haftmann@27652
  1742
  by (simp add: of_rat_less_eq)
berghofe@24534
  1743
haftmann@27544
  1744
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
haftmann@27652
  1745
  by (simp add: of_rat_less)
berghofe@24534
  1746
haftmann@27544
  1747
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
haftmann@27544
  1748
  by (simp add: of_rat_add)
berghofe@24534
  1749
haftmann@27544
  1750
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
haftmann@27544
  1751
  by (simp add: of_rat_mult)
haftmann@27544
  1752
haftmann@27544
  1753
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
haftmann@27544
  1754
  by (simp add: of_rat_minus)
berghofe@24534
  1755
haftmann@27544
  1756
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
haftmann@27544
  1757
  by (simp add: of_rat_diff)
berghofe@24534
  1758
haftmann@27544
  1759
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
haftmann@27544
  1760
  by (simp add: of_rat_inverse)
haftmann@27544
  1761
 
haftmann@27544
  1762
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
haftmann@27544
  1763
  by (simp add: of_rat_divide)
berghofe@24534
  1764
bulwahn@43733
  1765
lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
bulwahn@43733
  1766
  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
  1767
haftmann@31203
  1768
definition (in term_syntax)
haftmann@32657
  1769
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@32657
  1770
  [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
haftmann@31203
  1771
haftmann@37751
  1772
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1773
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1774
haftmann@31203
  1775
instantiation real :: random
haftmann@31203
  1776
begin
haftmann@31203
  1777
haftmann@31203
  1778
definition
haftmann@37751
  1779
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
haftmann@31203
  1780
haftmann@31203
  1781
instance ..
haftmann@31203
  1782
haftmann@31203
  1783
end
haftmann@31203
  1784
haftmann@37751
  1785
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1786
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1787
bulwahn@41920
  1788
instantiation real :: exhaustive
bulwahn@41231
  1789
begin
bulwahn@41231
  1790
bulwahn@41231
  1791
definition
bulwahn@42311
  1792
  "exhaustive f d = exhaustive (%r. f (Ratreal r)) d"
bulwahn@42311
  1793
bulwahn@42311
  1794
instance ..
bulwahn@42311
  1795
bulwahn@42311
  1796
end
bulwahn@42311
  1797
bulwahn@42311
  1798
instantiation real :: full_exhaustive
bulwahn@42311
  1799
begin
bulwahn@42311
  1800
bulwahn@42311
  1801
definition
bulwahn@42311
  1802
  "full_exhaustive f d = full_exhaustive (%r. f (valterm_ratreal r)) d"
bulwahn@41231
  1803
bulwahn@41231
  1804
instance ..
bulwahn@41231
  1805
bulwahn@41231
  1806
end
bulwahn@41231
  1807
bulwahn@43887
  1808
instantiation real :: narrowing
bulwahn@43887
  1809
begin
bulwahn@43887
  1810
bulwahn@43887
  1811
definition
bulwahn@43887
  1812
  "narrowing = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons Ratreal) narrowing"
bulwahn@43887
  1813
bulwahn@43887
  1814
instance ..
bulwahn@43887
  1815
bulwahn@43887
  1816
end
bulwahn@43887
  1817
bulwahn@43887
  1818
haftmann@24623
  1819
text {* Setup for SML code generator *}
nipkow@23031
  1820
nipkow@23031
  1821
types_code
berghofe@24534
  1822
  real ("(int */ int)")
nipkow@23031
  1823
attach (term_of) {*
berghofe@24534
  1824
fun term_of_real (p, q) =
haftmann@24623
  1825
  let
haftmann@24623
  1826
    val rT = HOLogic.realT
berghofe@24534
  1827
  in
berghofe@24534
  1828
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
haftmann@24623
  1829
    else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
berghofe@24534
  1830
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
berghofe@24534
  1831
  end;
nipkow@23031
  1832
*}
nipkow@23031
  1833
attach (test) {*
nipkow@23031
  1834
fun gen_real i =
berghofe@24534
  1835
  let
berghofe@24534
  1836
    val p = random_range 0 i;
berghofe@24534
  1837
    val q = random_range 1 (i + 1);
berghofe@24534
  1838
    val g = Integer.gcd p q;
wenzelm@24630
  1839
    val p' = p div g;
wenzelm@24630
  1840
    val q' = q div g;
berghofe@25885
  1841
    val r = (if one_of [true, false] then p' else ~ p',
haftmann@31666
  1842
      if p' = 0 then 1 else q')
berghofe@24534
  1843
  in
berghofe@25885
  1844
    (r, fn () => term_of_real r)
berghofe@24534
  1845
  end;
nipkow@23031
  1846
*}
nipkow@23031
  1847
nipkow@23031
  1848
consts_code
haftmann@24623
  1849
  Ratreal ("(_)")
berghofe@24534
  1850
berghofe@24534
  1851
consts_code
berghofe@24534
  1852
  "of_int :: int \<Rightarrow> real" ("\<module>real'_of'_int")
berghofe@24534
  1853
attach {*
haftmann@31666
  1854
fun real_of_int i = (i, 1);
berghofe@24534
  1855
*}
berghofe@24534
  1856
blanchet@38287
  1857
declaration {*
blanchet@38287
  1858
  Nitpick_HOL.register_frac_type @{type_name real}
wenzelm@33209
  1859
   [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
wenzelm@33209
  1860
    (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
wenzelm@33209
  1861
    (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
wenzelm@33209
  1862
    (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
wenzelm@33209
  1863
    (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
wenzelm@33209
  1864
    (@{const_name number_real_inst.number_of_real}, @{const_name Nitpick.number_of_frac}),
wenzelm@33209
  1865
    (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
blanchet@37397
  1866
    (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_eq_frac}),
wenzelm@33209
  1867
    (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
blanchet@33197
  1868
*}
blanchet@33197
  1869
blanchet@41792
  1870
lemmas [nitpick_unfold] = inverse_real_inst.inverse_real
blanchet@33197
  1871
    number_real_inst.number_of_real one_real_inst.one_real
blanchet@37397
  1872
    ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
blanchet@33197
  1873
    times_real_inst.times_real uminus_real_inst.uminus_real
blanchet@33197
  1874
    zero_real_inst.zero_real
blanchet@33197
  1875
paulson@5588
  1876
end