src/HOL/RealDef.thy
author wenzelm
Fri Jan 14 15:44:47 2011 +0100 (2011-01-14)
changeset 41550 efa734d9b221
parent 41231 2e901158675e
child 41792 ff3cb0c418b7
permissions -rw-r--r--
eliminated global prems;
tuned proofs;
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
huffman@36795
   124
  cauchy :: "(nat \<Rightarrow> rat) set"
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
huffman@36795
   796
subsection {* Completeness *}
paulson@14365
   797
huffman@36795
   798
lemma not_positive_Real:
huffman@36795
   799
  assumes X: "cauchy X"
huffman@36795
   800
  shows "\<not> positive (Real X) \<longleftrightarrow> (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> r)"
huffman@36795
   801
unfolding positive_Real [OF X]
huffman@36795
   802
apply (auto, unfold not_less)
huffman@36795
   803
apply (erule obtain_pos_sum)
huffman@36795
   804
apply (drule_tac x=s in spec, simp)
huffman@36795
   805
apply (drule_tac r=t in cauchyD [OF X], clarify)
huffman@36795
   806
apply (drule_tac x=k in spec, clarsimp)
huffman@36795
   807
apply (rule_tac x=n in exI, clarify, rename_tac m)
huffman@36795
   808
apply (drule_tac x=m in spec, simp)
huffman@36795
   809
apply (drule_tac x=n in spec, simp)
huffman@36795
   810
apply (drule spec, drule (1) mp, clarify, rename_tac i)
huffman@36795
   811
apply (rule_tac x="max i k" in exI, simp)
huffman@36795
   812
done
huffman@36795
   813
huffman@36795
   814
lemma le_Real:
huffman@36795
   815
  assumes X: "cauchy X" and Y: "cauchy Y"
huffman@36795
   816
  shows "Real X \<le> Real Y = (\<forall>r>0. \<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r)"
huffman@36795
   817
unfolding not_less [symmetric, where 'a=real] less_real_def
huffman@36795
   818
apply (simp add: diff_Real not_positive_Real X Y)
huffman@36795
   819
apply (simp add: diff_le_eq add_ac)
huffman@36795
   820
done
paulson@14365
   821
huffman@36795
   822
lemma le_RealI:
huffman@36795
   823
  assumes Y: "cauchy Y"
huffman@36795
   824
  shows "\<forall>n. x \<le> of_rat (Y n) \<Longrightarrow> x \<le> Real Y"
huffman@36795
   825
proof (induct x)
huffman@36795
   826
  fix X assume X: "cauchy X" and "\<forall>n. Real X \<le> of_rat (Y n)"
huffman@36795
   827
  hence le: "\<And>m r. 0 < r \<Longrightarrow> \<exists>k. \<forall>n\<ge>k. X n \<le> Y m + r"
huffman@36795
   828
    by (simp add: of_rat_Real le_Real)
huffman@36795
   829
  {
huffman@36795
   830
    fix r :: rat assume "0 < r"
huffman@36795
   831
    then obtain s t where s: "0 < s" and t: "0 < t" and r: "r = s + t"
huffman@36795
   832
      by (rule obtain_pos_sum)
huffman@36795
   833
    obtain i where i: "\<forall>m\<ge>i. \<forall>n\<ge>i. \<bar>Y m - Y n\<bar> < s"
huffman@36795
   834
      using cauchyD [OF Y s] ..
huffman@36795
   835
    obtain j where j: "\<forall>n\<ge>j. X n \<le> Y i + t"
huffman@36795
   836
      using le [OF t] ..
huffman@36795
   837
    have "\<forall>n\<ge>max i j. X n \<le> Y n + r"
huffman@36795
   838
    proof (clarsimp)
huffman@36795
   839
      fix n assume n: "i \<le> n" "j \<le> n"
huffman@36795
   840
      have "X n \<le> Y i + t" using n j by simp
huffman@36795
   841
      moreover have "\<bar>Y i - Y n\<bar> < s" using n i by simp
huffman@36795
   842
      ultimately show "X n \<le> Y n + r" unfolding r by simp
huffman@36795
   843
    qed
huffman@36795
   844
    hence "\<exists>k. \<forall>n\<ge>k. X n \<le> Y n + r" ..
huffman@36795
   845
  }
huffman@36795
   846
  thus "Real X \<le> Real Y"
huffman@36795
   847
    by (simp add: of_rat_Real le_Real X Y)
huffman@36795
   848
qed
paulson@14365
   849
huffman@36795
   850
lemma Real_leI:
huffman@36795
   851
  assumes X: "cauchy X"
huffman@36795
   852
  assumes le: "\<forall>n. of_rat (X n) \<le> y"
huffman@36795
   853
  shows "Real X \<le> y"
huffman@36795
   854
proof -
huffman@36795
   855
  have "- y \<le> - Real X"
huffman@36795
   856
    by (simp add: minus_Real X le_RealI of_rat_minus le)
huffman@36795
   857
  thus ?thesis by simp
huffman@36795
   858
qed
huffman@36795
   859
huffman@36795
   860
lemma less_RealD:
huffman@36795
   861
  assumes Y: "cauchy Y"
huffman@36795
   862
  shows "x < Real Y \<Longrightarrow> \<exists>n. x < of_rat (Y n)"
huffman@36795
   863
by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
huffman@36795
   864
huffman@36795
   865
lemma of_nat_less_two_power:
huffman@36795
   866
  "of_nat n < (2::'a::{linordered_idom,number_ring}) ^ n"
huffman@36795
   867
apply (induct n)
huffman@36795
   868
apply simp
huffman@36795
   869
apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
huffman@36795
   870
apply (drule (1) add_le_less_mono, simp)
huffman@36795
   871
apply simp
paulson@14365
   872
done
paulson@14365
   873
huffman@36795
   874
lemma complete_real:
huffman@36795
   875
  fixes S :: "real set"
huffman@36795
   876
  assumes "\<exists>x. x \<in> S" and "\<exists>z. \<forall>x\<in>S. x \<le> z"
huffman@36795
   877
  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
   878
proof -
huffman@36795
   879
  obtain x where x: "x \<in> S" using assms(1) ..
huffman@36795
   880
  obtain z where z: "\<forall>x\<in>S. x \<le> z" using assms(2) ..
paulson@14365
   881
huffman@36795
   882
  def P \<equiv> "\<lambda>x. \<forall>y\<in>S. y \<le> of_rat x"
huffman@36795
   883
  obtain a where a: "\<not> P a"
huffman@36795
   884
  proof
huffman@36795
   885
    have "of_int (floor (x - 1)) \<le> x - 1" by (rule of_int_floor_le)
huffman@36795
   886
    also have "x - 1 < x" by simp
huffman@36795
   887
    finally have "of_int (floor (x - 1)) < x" .
huffman@36795
   888
    hence "\<not> x \<le> of_int (floor (x - 1))" by (simp only: not_le)
huffman@36795
   889
    then show "\<not> P (of_int (floor (x - 1)))"
huffman@36795
   890
      unfolding P_def of_rat_of_int_eq using x by fast
huffman@36795
   891
  qed
huffman@36795
   892
  obtain b where b: "P b"
huffman@36795
   893
  proof
huffman@36795
   894
    show "P (of_int (ceiling z))"
huffman@36795
   895
    unfolding P_def of_rat_of_int_eq
huffman@36795
   896
    proof
huffman@36795
   897
      fix y assume "y \<in> S"
huffman@36795
   898
      hence "y \<le> z" using z by simp
huffman@36795
   899
      also have "z \<le> of_int (ceiling z)" by (rule le_of_int_ceiling)
huffman@36795
   900
      finally show "y \<le> of_int (ceiling z)" .
huffman@36795
   901
    qed
huffman@36795
   902
  qed
paulson@14365
   903
huffman@36795
   904
  def avg \<equiv> "\<lambda>x y :: rat. x/2 + y/2"
huffman@36795
   905
  def bisect \<equiv> "\<lambda>(x, y). if P (avg x y) then (x, avg x y) else (avg x y, y)"
huffman@36795
   906
  def A \<equiv> "\<lambda>n. fst ((bisect ^^ n) (a, b))"
huffman@36795
   907
  def B \<equiv> "\<lambda>n. snd ((bisect ^^ n) (a, b))"
huffman@36795
   908
  def C \<equiv> "\<lambda>n. avg (A n) (B n)"
huffman@36795
   909
  have A_0 [simp]: "A 0 = a" unfolding A_def by simp
huffman@36795
   910
  have B_0 [simp]: "B 0 = b" unfolding B_def by simp
huffman@36795
   911
  have A_Suc [simp]: "\<And>n. A (Suc n) = (if P (C n) then A n else C n)"
huffman@36795
   912
    unfolding A_def B_def C_def bisect_def split_def by simp
huffman@36795
   913
  have B_Suc [simp]: "\<And>n. B (Suc n) = (if P (C n) then C n else B n)"
huffman@36795
   914
    unfolding A_def B_def C_def bisect_def split_def by simp
paulson@14365
   915
huffman@36795
   916
  have width: "\<And>n. B n - A n = (b - a) / 2^n"
huffman@36795
   917
    apply (simp add: eq_divide_eq)
huffman@36795
   918
    apply (induct_tac n, simp)
huffman@36795
   919
    apply (simp add: C_def avg_def algebra_simps)
huffman@36795
   920
    done
huffman@36795
   921
huffman@36795
   922
  have twos: "\<And>y r :: rat. 0 < r \<Longrightarrow> \<exists>n. y / 2 ^ n < r"
huffman@36795
   923
    apply (simp add: divide_less_eq)
huffman@36795
   924
    apply (subst mult_commute)
huffman@36795
   925
    apply (frule_tac y=y in ex_less_of_nat_mult)
huffman@36795
   926
    apply clarify
huffman@36795
   927
    apply (rule_tac x=n in exI)
huffman@36795
   928
    apply (erule less_trans)
huffman@36795
   929
    apply (rule mult_strict_right_mono)
huffman@36795
   930
    apply (rule le_less_trans [OF _ of_nat_less_two_power])
huffman@36795
   931
    apply simp
huffman@36795
   932
    apply assumption
huffman@36795
   933
    done
paulson@14365
   934
huffman@36795
   935
  have PA: "\<And>n. \<not> P (A n)"
huffman@36795
   936
    by (induct_tac n, simp_all add: a)
huffman@36795
   937
  have PB: "\<And>n. P (B n)"
huffman@36795
   938
    by (induct_tac n, simp_all add: b)
huffman@36795
   939
  have ab: "a < b"
huffman@36795
   940
    using a b unfolding P_def
huffman@36795
   941
    apply (clarsimp simp add: not_le)
huffman@36795
   942
    apply (drule (1) bspec)
huffman@36795
   943
    apply (drule (1) less_le_trans)
huffman@36795
   944
    apply (simp add: of_rat_less)
huffman@36795
   945
    done
huffman@36795
   946
  have AB: "\<And>n. A n < B n"
huffman@36795
   947
    by (induct_tac n, simp add: ab, simp add: C_def avg_def)
huffman@36795
   948
  have A_mono: "\<And>i j. i \<le> j \<Longrightarrow> A i \<le> A j"
huffman@36795
   949
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   950
    apply (erule less_Suc_induct)
huffman@36795
   951
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   952
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   953
    apply (rule AB [THEN less_imp_le])
huffman@36795
   954
    apply simp
huffman@36795
   955
    done
huffman@36795
   956
  have B_mono: "\<And>i j. i \<le> j \<Longrightarrow> B j \<le> B i"
huffman@36795
   957
    apply (auto simp add: le_less [where 'a=nat])
huffman@36795
   958
    apply (erule less_Suc_induct)
huffman@36795
   959
    apply (clarsimp simp add: C_def avg_def)
huffman@36795
   960
    apply (simp add: add_divide_distrib [symmetric])
huffman@36795
   961
    apply (rule AB [THEN less_imp_le])
huffman@36795
   962
    apply simp
huffman@36795
   963
    done
huffman@36795
   964
  have cauchy_lemma:
huffman@36795
   965
    "\<And>X. \<forall>n. \<forall>i\<ge>n. A n \<le> X i \<and> X i \<le> B n \<Longrightarrow> cauchy X"
huffman@36795
   966
    apply (rule cauchyI)
huffman@36795
   967
    apply (drule twos [where y="b - a"])
huffman@36795
   968
    apply (erule exE)
huffman@36795
   969
    apply (rule_tac x=n in exI, clarify, rename_tac i j)
huffman@36795
   970
    apply (rule_tac y="B n - A n" in le_less_trans) defer
huffman@36795
   971
    apply (simp add: width)
huffman@36795
   972
    apply (drule_tac x=n in spec)
huffman@36795
   973
    apply (frule_tac x=i in spec, drule (1) mp)
huffman@36795
   974
    apply (frule_tac x=j in spec, drule (1) mp)
huffman@36795
   975
    apply (frule A_mono, drule B_mono)
huffman@36795
   976
    apply (frule A_mono, drule B_mono)
huffman@36795
   977
    apply arith
huffman@36795
   978
    done
huffman@36795
   979
  have "cauchy A"
huffman@36795
   980
    apply (rule cauchy_lemma [rule_format])
huffman@36795
   981
    apply (simp add: A_mono)
huffman@36795
   982
    apply (erule order_trans [OF less_imp_le [OF AB] B_mono])
huffman@36795
   983
    done
huffman@36795
   984
  have "cauchy B"
huffman@36795
   985
    apply (rule cauchy_lemma [rule_format])
huffman@36795
   986
    apply (simp add: B_mono)
huffman@36795
   987
    apply (erule order_trans [OF A_mono less_imp_le [OF AB]])
huffman@36795
   988
    done
huffman@36795
   989
  have 1: "\<forall>x\<in>S. x \<le> Real B"
huffman@36795
   990
  proof
huffman@36795
   991
    fix x assume "x \<in> S"
huffman@36795
   992
    then show "x \<le> Real B"
huffman@36795
   993
      using PB [unfolded P_def] `cauchy B`
huffman@36795
   994
      by (simp add: le_RealI)
huffman@36795
   995
  qed
huffman@36795
   996
  have 2: "\<forall>z. (\<forall>x\<in>S. x \<le> z) \<longrightarrow> Real A \<le> z"
huffman@36795
   997
    apply clarify
huffman@36795
   998
    apply (erule contrapos_pp)
huffman@36795
   999
    apply (simp add: not_le)
huffman@36795
  1000
    apply (drule less_RealD [OF `cauchy A`], clarify)
huffman@36795
  1001
    apply (subgoal_tac "\<not> P (A n)")
huffman@36795
  1002
    apply (simp add: P_def not_le, clarify)
huffman@36795
  1003
    apply (erule rev_bexI)
huffman@36795
  1004
    apply (erule (1) less_trans)
huffman@36795
  1005
    apply (simp add: PA)
huffman@36795
  1006
    done
huffman@36795
  1007
  have "vanishes (\<lambda>n. (b - a) / 2 ^ n)"
huffman@36795
  1008
  proof (rule vanishesI)
huffman@36795
  1009
    fix r :: rat assume "0 < r"
huffman@36795
  1010
    then obtain k where k: "\<bar>b - a\<bar> / 2 ^ k < r"
huffman@36795
  1011
      using twos by fast
huffman@36795
  1012
    have "\<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r"
huffman@36795
  1013
    proof (clarify)
huffman@36795
  1014
      fix n assume n: "k \<le> n"
huffman@36795
  1015
      have "\<bar>(b - a) / 2 ^ n\<bar> = \<bar>b - a\<bar> / 2 ^ n"
huffman@36795
  1016
        by simp
huffman@36795
  1017
      also have "\<dots> \<le> \<bar>b - a\<bar> / 2 ^ k"
huffman@36795
  1018
        using n by (simp add: divide_left_mono mult_pos_pos)
huffman@36795
  1019
      also note k
huffman@36795
  1020
      finally show "\<bar>(b - a) / 2 ^ n\<bar> < r" .
huffman@36795
  1021
    qed
huffman@36795
  1022
    thus "\<exists>k. \<forall>n\<ge>k. \<bar>(b - a) / 2 ^ n\<bar> < r" ..
huffman@36795
  1023
  qed
huffman@36795
  1024
  hence 3: "Real B = Real A"
huffman@36795
  1025
    by (simp add: eq_Real `cauchy A` `cauchy B` width)
huffman@36795
  1026
  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
  1027
    using 1 2 3 by (rule_tac x="Real B" in exI, simp)
paulson@14484
  1028
qed
paulson@14365
  1029
huffman@36795
  1030
subsection {* Hiding implementation details *}
paulson@14365
  1031
huffman@36795
  1032
hide_const (open) vanishes cauchy positive Real real_case
paulson@14365
  1033
huffman@36795
  1034
declare Real_induct [induct del]
huffman@36795
  1035
declare Abs_real_induct [induct del]
huffman@36795
  1036
declare Abs_real_cases [cases del]
huffman@36795
  1037
huffman@36795
  1038
subsection {* Legacy theorem names *}
huffman@36795
  1039
huffman@36795
  1040
text {* TODO: Could we have a way to mark theorem names as deprecated,
huffman@36795
  1041
and have Isabelle issue a warning and display the preferred name? *}
paulson@14365
  1042
huffman@36795
  1043
lemmas real_diff_def = minus_real_def [of "r" "s", standard]
huffman@36795
  1044
lemmas real_divide_def = divide_real_def [of "R" "S", standard]
huffman@36795
  1045
lemmas real_less_def = less_le [of "x::real" "y", standard]
huffman@36795
  1046
lemmas real_abs_def = abs_real_def [of "r", standard]
huffman@36795
  1047
lemmas real_sgn_def = sgn_real_def [of "x", standard]
huffman@36796
  1048
lemmas real_mult_commute = mult_commute [of "z::real" "w", standard]
huffman@36795
  1049
lemmas real_mult_assoc = mult_assoc [of "z1::real" "z2" "z3", standard]
huffman@36795
  1050
lemmas real_mult_1 = mult_1_left [of "z::real", standard]
huffman@36795
  1051
lemmas real_add_mult_distrib = left_distrib [of "z1::real" "z2" "w", standard]
huffman@36795
  1052
lemmas real_zero_not_eq_one = zero_neq_one [where 'a=real]
huffman@36795
  1053
lemmas real_mult_inverse_left = left_inverse [of "x::real", standard]
huffman@36795
  1054
lemmas INVERSE_ZERO = inverse_zero [where 'a=real]
huffman@36795
  1055
lemmas real_le_refl = order_refl [of "w::real", standard]
huffman@36795
  1056
lemmas real_le_antisym = order_antisym [of "z::real" "w", standard]
huffman@36795
  1057
lemmas real_le_trans = order_trans [of "i::real" "j" "k", standard]
huffman@36941
  1058
lemmas real_le_linear = linear [of "z::real" "w", standard]
huffman@36795
  1059
lemmas real_le_eq_diff = le_iff_diff_le_0 [of "x::real" "y", standard]
huffman@36795
  1060
lemmas real_add_left_mono = add_left_mono [of "x::real" "y" "z", standard]
huffman@36795
  1061
lemmas real_mult_order = mult_pos_pos [of "x::real" "y", standard]
huffman@36795
  1062
lemmas real_mult_less_mono2 =
huffman@36795
  1063
  mult_strict_left_mono [of "x::real" "y" "z", COMP swap_prems_rl, standard]
paulson@14334
  1064
paulson@14334
  1065
subsection{*More Lemmas*}
paulson@14334
  1066
huffman@36776
  1067
text {* BH: These lemmas should not be necessary; they should be
huffman@36776
  1068
covered by existing simp rules and simplification procedures. *}
huffman@36776
  1069
paulson@14334
  1070
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
huffman@36776
  1071
by simp (* redundant with mult_cancel_left *)
paulson@14334
  1072
paulson@14334
  1073
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
huffman@36776
  1074
by simp (* redundant with mult_cancel_right *)
paulson@14334
  1075
paulson@14334
  1076
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
huffman@36776
  1077
by simp (* solved by linordered_ring_less_cancel_factor simproc *)
paulson@14334
  1078
paulson@14334
  1079
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
huffman@36776
  1080
by simp (* solved by linordered_ring_le_cancel_factor simproc *)
paulson@14334
  1081
paulson@14334
  1082
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
huffman@36776
  1083
by (rule mult_le_cancel_left_pos)
huffman@36776
  1084
(* BH: Why doesn't "simp" prove this one, like it does the last one? *)
paulson@14334
  1085
paulson@14334
  1086
haftmann@24198
  1087
subsection {* Embedding numbers into the Reals *}
haftmann@24198
  1088
haftmann@24198
  1089
abbreviation
haftmann@24198
  1090
  real_of_nat :: "nat \<Rightarrow> real"
haftmann@24198
  1091
where
haftmann@24198
  1092
  "real_of_nat \<equiv> of_nat"
haftmann@24198
  1093
haftmann@24198
  1094
abbreviation
haftmann@24198
  1095
  real_of_int :: "int \<Rightarrow> real"
haftmann@24198
  1096
where
haftmann@24198
  1097
  "real_of_int \<equiv> of_int"
haftmann@24198
  1098
haftmann@24198
  1099
abbreviation
haftmann@24198
  1100
  real_of_rat :: "rat \<Rightarrow> real"
haftmann@24198
  1101
where
haftmann@24198
  1102
  "real_of_rat \<equiv> of_rat"
haftmann@24198
  1103
haftmann@24198
  1104
consts
haftmann@24198
  1105
  (*overloaded constant for injecting other types into "real"*)
haftmann@24198
  1106
  real :: "'a => real"
paulson@14365
  1107
paulson@14378
  1108
defs (overloaded)
haftmann@31998
  1109
  real_of_nat_def [code_unfold]: "real == real_of_nat"
haftmann@31998
  1110
  real_of_int_def [code_unfold]: "real == real_of_int"
paulson@14365
  1111
wenzelm@40939
  1112
declare [[coercion_enabled]]
nipkow@40864
  1113
declare [[coercion "real::nat\<Rightarrow>real"]]
nipkow@40864
  1114
declare [[coercion "real::int\<Rightarrow>real"]]
nipkow@41022
  1115
declare [[coercion "int"]]
nipkow@40864
  1116
hoelzl@41024
  1117
declare [[coercion_map map]]
hoelzl@41024
  1118
declare [[coercion_map "% f g h . g o h o f"]]
hoelzl@41024
  1119
declare [[coercion_map "% f g (x,y) . (f x, g y)"]]
hoelzl@41024
  1120
avigad@16819
  1121
lemma real_eq_of_nat: "real = of_nat"
haftmann@24198
  1122
  unfolding real_of_nat_def ..
avigad@16819
  1123
avigad@16819
  1124
lemma real_eq_of_int: "real = of_int"
haftmann@24198
  1125
  unfolding real_of_int_def ..
avigad@16819
  1126
paulson@14365
  1127
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
  1128
by (simp add: real_of_int_def) 
paulson@14365
  1129
paulson@14365
  1130
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
  1131
by (simp add: real_of_int_def) 
paulson@14334
  1132
avigad@16819
  1133
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
  1134
by (simp add: real_of_int_def) 
paulson@14365
  1135
avigad@16819
  1136
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
  1137
by (simp add: real_of_int_def) 
avigad@16819
  1138
avigad@16819
  1139
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
  1140
by (simp add: real_of_int_def) 
paulson@14365
  1141
avigad@16819
  1142
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
  1143
by (simp add: real_of_int_def) 
paulson@14334
  1144
huffman@35344
  1145
lemma real_of_int_power [simp]: "real (x ^ n) = real (x::int) ^ n"
huffman@35344
  1146
by (simp add: real_of_int_def of_int_power)
huffman@35344
  1147
huffman@35344
  1148
lemmas power_real_of_int = real_of_int_power [symmetric]
huffman@35344
  1149
avigad@16819
  1150
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
  1151
  apply (subst real_eq_of_int)+
avigad@16819
  1152
  apply (rule of_int_setsum)
avigad@16819
  1153
done
avigad@16819
  1154
avigad@16819
  1155
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
  1156
    (PROD x:A. real(f x))"
avigad@16819
  1157
  apply (subst real_eq_of_int)+
avigad@16819
  1158
  apply (rule of_int_setprod)
avigad@16819
  1159
done
paulson@14365
  1160
chaieb@27668
  1161
lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
paulson@14378
  1162
by (simp add: real_of_int_def) 
paulson@14365
  1163
chaieb@27668
  1164
lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
paulson@14378
  1165
by (simp add: real_of_int_def) 
paulson@14365
  1166
chaieb@27668
  1167
lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
paulson@14378
  1168
by (simp add: real_of_int_def) 
paulson@14365
  1169
chaieb@27668
  1170
lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
  1171
by (simp add: real_of_int_def) 
paulson@14365
  1172
chaieb@27668
  1173
lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
  1174
by (simp add: real_of_int_def) 
avigad@16819
  1175
chaieb@27668
  1176
lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
  1177
by (simp add: real_of_int_def) 
avigad@16819
  1178
chaieb@27668
  1179
lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
avigad@16819
  1180
by (simp add: real_of_int_def)
avigad@16819
  1181
chaieb@27668
  1182
lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
  1183
by (simp add: real_of_int_def)
avigad@16819
  1184
avigad@16888
  1185
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
  1186
by (auto simp add: abs_if)
avigad@16888
  1187
avigad@16819
  1188
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
  1189
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
  1190
  apply (simp del: real_of_int_add)
avigad@16819
  1191
  apply auto
avigad@16819
  1192
done
avigad@16819
  1193
avigad@16819
  1194
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
  1195
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
  1196
  apply (simp del: real_of_int_add)
avigad@16819
  1197
  apply simp
avigad@16819
  1198
done
avigad@16819
  1199
avigad@16819
  1200
lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
avigad@16819
  1201
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1202
proof -
wenzelm@41550
  1203
  assume d: "d ~= 0"
avigad@16819
  1204
  have "x = (x div d) * d + x mod d"
avigad@16819
  1205
    by auto
avigad@16819
  1206
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1207
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
  1208
  then have "real x / real d = ... / real d"
avigad@16819
  1209
    by simp
avigad@16819
  1210
  then show ?thesis
wenzelm@41550
  1211
    by (auto simp add: add_divide_distrib algebra_simps d)
avigad@16819
  1212
qed
avigad@16819
  1213
avigad@16819
  1214
lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
avigad@16819
  1215
    real(n div d) = real n / real d"
avigad@16819
  1216
  apply (frule real_of_int_div_aux [of d n])
avigad@16819
  1217
  apply simp
nipkow@30042
  1218
  apply (simp add: dvd_eq_mod_eq_0)
avigad@16819
  1219
done
avigad@16819
  1220
avigad@16819
  1221
lemma real_of_int_div2:
avigad@16819
  1222
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
  1223
  apply (case_tac "x = 0")
avigad@16819
  1224
  apply simp
avigad@16819
  1225
  apply (case_tac "0 < x")
nipkow@29667
  1226
  apply (simp add: algebra_simps)
avigad@16819
  1227
  apply (subst real_of_int_div_aux)
avigad@16819
  1228
  apply simp
avigad@16819
  1229
  apply simp
avigad@16819
  1230
  apply (subst zero_le_divide_iff)
avigad@16819
  1231
  apply auto
nipkow@29667
  1232
  apply (simp add: algebra_simps)
avigad@16819
  1233
  apply (subst real_of_int_div_aux)
avigad@16819
  1234
  apply simp
avigad@16819
  1235
  apply simp
avigad@16819
  1236
  apply (subst zero_le_divide_iff)
avigad@16819
  1237
  apply auto
avigad@16819
  1238
done
avigad@16819
  1239
avigad@16819
  1240
lemma real_of_int_div3:
avigad@16819
  1241
  "real (n::int) / real (x) - real (n div x) <= 1"
avigad@16819
  1242
  apply(case_tac "x = 0")
avigad@16819
  1243
  apply simp
nipkow@29667
  1244
  apply (simp add: algebra_simps)
avigad@16819
  1245
  apply (subst real_of_int_div_aux)
avigad@16819
  1246
  apply assumption
avigad@16819
  1247
  apply simp
avigad@16819
  1248
  apply (subst divide_le_eq)
avigad@16819
  1249
  apply clarsimp
avigad@16819
  1250
  apply (rule conjI)
avigad@16819
  1251
  apply (rule impI)
avigad@16819
  1252
  apply (rule order_less_imp_le)
avigad@16819
  1253
  apply simp
avigad@16819
  1254
  apply (rule impI)
avigad@16819
  1255
  apply (rule order_less_imp_le)
avigad@16819
  1256
  apply simp
avigad@16819
  1257
done
avigad@16819
  1258
avigad@16819
  1259
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
nipkow@27964
  1260
by (insert real_of_int_div2 [of n x], simp)
nipkow@27964
  1261
huffman@35635
  1262
lemma Ints_real_of_int [simp]: "real (x::int) \<in> Ints"
huffman@35635
  1263
unfolding real_of_int_def by (rule Ints_of_int)
huffman@35635
  1264
nipkow@27964
  1265
paulson@14365
  1266
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
  1267
paulson@14334
  1268
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
  1269
by (simp add: real_of_nat_def)
paulson@14334
  1270
huffman@30082
  1271
lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
huffman@30082
  1272
by (simp add: real_of_nat_def)
huffman@30082
  1273
paulson@14334
  1274
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
  1275
by (simp add: real_of_nat_def)
paulson@14334
  1276
paulson@14365
  1277
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
  1278
by (simp add: real_of_nat_def)
paulson@14334
  1279
paulson@14334
  1280
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
  1281
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
  1282
by (simp add: real_of_nat_def)
paulson@14334
  1283
paulson@14334
  1284
lemma real_of_nat_less_iff [iff]: 
paulson@14334
  1285
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
  1286
by (simp add: real_of_nat_def)
paulson@14334
  1287
paulson@14334
  1288
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
  1289
by (simp add: real_of_nat_def)
paulson@14334
  1290
paulson@14334
  1291
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
paulson@14378
  1292
by (simp add: real_of_nat_def zero_le_imp_of_nat)
paulson@14334
  1293
paulson@14365
  1294
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
  1295
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
  1296
paulson@14334
  1297
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
huffman@23431
  1298
by (simp add: real_of_nat_def of_nat_mult)
paulson@14334
  1299
huffman@35344
  1300
lemma real_of_nat_power [simp]: "real (m ^ n) = real (m::nat) ^ n"
huffman@35344
  1301
by (simp add: real_of_nat_def of_nat_power)
huffman@35344
  1302
huffman@35344
  1303
lemmas power_real_of_nat = real_of_nat_power [symmetric]
huffman@35344
  1304
avigad@16819
  1305
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
  1306
    (SUM x:A. real(f x))"
avigad@16819
  1307
  apply (subst real_eq_of_nat)+
avigad@16819
  1308
  apply (rule of_nat_setsum)
avigad@16819
  1309
done
avigad@16819
  1310
avigad@16819
  1311
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
  1312
    (PROD x:A. real(f x))"
avigad@16819
  1313
  apply (subst real_eq_of_nat)+
avigad@16819
  1314
  apply (rule of_nat_setprod)
avigad@16819
  1315
done
avigad@16819
  1316
avigad@16819
  1317
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
  1318
  apply (subst card_eq_setsum)
avigad@16819
  1319
  apply (subst real_of_nat_setsum)
avigad@16819
  1320
  apply simp
avigad@16819
  1321
done
avigad@16819
  1322
paulson@14334
  1323
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
  1324
by (simp add: real_of_nat_def)
paulson@14334
  1325
paulson@14387
  1326
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
  1327
by (simp add: real_of_nat_def)
paulson@14334
  1328
paulson@14365
  1329
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
huffman@23438
  1330
by (simp add: add: real_of_nat_def of_nat_diff)
paulson@14334
  1331
nipkow@25162
  1332
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
nipkow@25140
  1333
by (auto simp: real_of_nat_def)
paulson@14365
  1334
paulson@14365
  1335
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
  1336
by (simp add: add: real_of_nat_def)
paulson@14334
  1337
paulson@14365
  1338
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
  1339
by (simp add: add: real_of_nat_def)
paulson@14334
  1340
avigad@16819
  1341
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
  1342
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
  1343
  apply simp
avigad@16819
  1344
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
  1345
done
avigad@16819
  1346
avigad@16819
  1347
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
  1348
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
  1349
  apply (simp add: less_Suc_eq_le)
avigad@16819
  1350
  apply (simp add: real_of_nat_Suc)
avigad@16819
  1351
done
avigad@16819
  1352
avigad@16819
  1353
lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
avigad@16819
  1354
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
  1355
proof -
wenzelm@41550
  1356
  assume d: "0 < d"
avigad@16819
  1357
  have "x = (x div d) * d + x mod d"
avigad@16819
  1358
    by auto
avigad@16819
  1359
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
  1360
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
  1361
  then have "real x / real d = \<dots> / real d"
avigad@16819
  1362
    by simp
avigad@16819
  1363
  then show ?thesis
wenzelm@41550
  1364
    by (auto simp add: add_divide_distrib algebra_simps d)
avigad@16819
  1365
qed
avigad@16819
  1366
avigad@16819
  1367
lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
avigad@16819
  1368
    real(n div d) = real n / real d"
avigad@16819
  1369
  apply (frule real_of_nat_div_aux [of d n])
avigad@16819
  1370
  apply simp
avigad@16819
  1371
  apply (subst dvd_eq_mod_eq_0 [THEN sym])
avigad@16819
  1372
  apply assumption
avigad@16819
  1373
done
avigad@16819
  1374
avigad@16819
  1375
lemma real_of_nat_div2:
avigad@16819
  1376
  "0 <= real (n::nat) / real (x) - real (n div x)"
nipkow@25134
  1377
apply(case_tac "x = 0")
nipkow@25134
  1378
 apply (simp)
nipkow@29667
  1379
apply (simp add: algebra_simps)
nipkow@25134
  1380
apply (subst real_of_nat_div_aux)
nipkow@25134
  1381
 apply simp
nipkow@25134
  1382
apply simp
nipkow@25134
  1383
apply (subst zero_le_divide_iff)
nipkow@25134
  1384
apply simp
avigad@16819
  1385
done
avigad@16819
  1386
avigad@16819
  1387
lemma real_of_nat_div3:
avigad@16819
  1388
  "real (n::nat) / real (x) - real (n div x) <= 1"
nipkow@25134
  1389
apply(case_tac "x = 0")
nipkow@25134
  1390
apply (simp)
nipkow@29667
  1391
apply (simp add: algebra_simps)
nipkow@25134
  1392
apply (subst real_of_nat_div_aux)
nipkow@25134
  1393
 apply simp
nipkow@25134
  1394
apply simp
avigad@16819
  1395
done
avigad@16819
  1396
avigad@16819
  1397
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
nipkow@29667
  1398
by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
  1399
paulson@14365
  1400
lemma real_of_int_real_of_nat: "real (int n) = real n"
paulson@14378
  1401
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
paulson@14378
  1402
paulson@14426
  1403
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
  1404
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
  1405
avigad@16819
  1406
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
  1407
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
  1408
  apply force
avigad@16819
  1409
  apply (simp only: real_of_int_real_of_nat)
avigad@16819
  1410
done
paulson@14387
  1411
huffman@35635
  1412
lemma Nats_real_of_nat [simp]: "real (n::nat) \<in> Nats"
huffman@35635
  1413
unfolding real_of_nat_def by (rule of_nat_in_Nats)
huffman@35635
  1414
huffman@35635
  1415
lemma Ints_real_of_nat [simp]: "real (n::nat) \<in> Ints"
huffman@35635
  1416
unfolding real_of_nat_def by (rule Ints_of_nat)
huffman@35635
  1417
nipkow@28001
  1418
nipkow@28001
  1419
subsection{* Rationals *}
nipkow@28001
  1420
nipkow@28091
  1421
lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
nipkow@28091
  1422
by (simp add: real_eq_of_nat)
nipkow@28091
  1423
nipkow@28091
  1424
nipkow@28001
  1425
lemma Rats_eq_int_div_int:
nipkow@28091
  1426
  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
nipkow@28001
  1427
proof
nipkow@28091
  1428
  show "\<rat> \<subseteq> ?S"
nipkow@28001
  1429
  proof
nipkow@28091
  1430
    fix x::real assume "x : \<rat>"
nipkow@28001
  1431
    then obtain r where "x = of_rat r" unfolding Rats_def ..
nipkow@28001
  1432
    have "of_rat r : ?S"
nipkow@28001
  1433
      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1434
    thus "x : ?S" using `x = of_rat r` by simp
nipkow@28001
  1435
  qed
nipkow@28001
  1436
next
nipkow@28091
  1437
  show "?S \<subseteq> \<rat>"
nipkow@28001
  1438
  proof(auto simp:Rats_def)
nipkow@28001
  1439
    fix i j :: int assume "j \<noteq> 0"
nipkow@28001
  1440
    hence "real i / real j = of_rat(Fract i j)"
nipkow@28001
  1441
      by (simp add:of_rat_rat real_eq_of_int)
nipkow@28001
  1442
    thus "real i / real j \<in> range of_rat" by blast
nipkow@28001
  1443
  qed
nipkow@28001
  1444
qed
nipkow@28001
  1445
nipkow@28001
  1446
lemma Rats_eq_int_div_nat:
nipkow@28091
  1447
  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
nipkow@28001
  1448
proof(auto simp:Rats_eq_int_div_int)
nipkow@28001
  1449
  fix i j::int assume "j \<noteq> 0"
nipkow@28001
  1450
  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
nipkow@28001
  1451
  proof cases
nipkow@28001
  1452
    assume "j>0"
nipkow@28001
  1453
    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
nipkow@28001
  1454
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1455
    thus ?thesis by blast
nipkow@28001
  1456
  next
nipkow@28001
  1457
    assume "~ j>0"
nipkow@28001
  1458
    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
nipkow@28001
  1459
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
  1460
    thus ?thesis by blast
nipkow@28001
  1461
  qed
nipkow@28001
  1462
next
nipkow@28001
  1463
  fix i::int and n::nat assume "0 < n"
nipkow@28001
  1464
  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
nipkow@28001
  1465
  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
nipkow@28001
  1466
qed
nipkow@28001
  1467
nipkow@28001
  1468
lemma Rats_abs_nat_div_natE:
nipkow@28001
  1469
  assumes "x \<in> \<rat>"
huffman@31706
  1470
  obtains m n :: nat
huffman@31706
  1471
  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
nipkow@28001
  1472
proof -
nipkow@28001
  1473
  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
nipkow@28001
  1474
    by(auto simp add: Rats_eq_int_div_nat)
nipkow@28001
  1475
  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
nipkow@28001
  1476
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
nipkow@28001
  1477
  let ?gcd = "gcd m n"
huffman@31706
  1478
  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
nipkow@28001
  1479
  let ?k = "m div ?gcd"
nipkow@28001
  1480
  let ?l = "n div ?gcd"
nipkow@28001
  1481
  let ?gcd' = "gcd ?k ?l"
nipkow@28001
  1482
  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
nipkow@28001
  1483
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1484
  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
nipkow@28001
  1485
    by (rule dvd_mult_div_cancel)
nipkow@28001
  1486
  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
nipkow@28001
  1487
  moreover
nipkow@28001
  1488
  have "\<bar>x\<bar> = real ?k / real ?l"
nipkow@28001
  1489
  proof -
nipkow@28001
  1490
    from gcd have "real ?k / real ?l =
nipkow@28001
  1491
        real (?gcd * ?k) / real (?gcd * ?l)" by simp
nipkow@28001
  1492
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
nipkow@28001
  1493
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
nipkow@28001
  1494
    finally show ?thesis ..
nipkow@28001
  1495
  qed
nipkow@28001
  1496
  moreover
nipkow@28001
  1497
  have "?gcd' = 1"
nipkow@28001
  1498
  proof -
nipkow@28001
  1499
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
nipkow@31952
  1500
      by (rule gcd_mult_distrib_nat)
nipkow@28001
  1501
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
huffman@31706
  1502
    with gcd show ?thesis by auto
nipkow@28001
  1503
  qed
nipkow@28001
  1504
  ultimately show ?thesis ..
nipkow@28001
  1505
qed
nipkow@28001
  1506
nipkow@28001
  1507
paulson@14387
  1508
subsection{*Numerals and Arithmetic*}
paulson@14387
  1509
haftmann@32069
  1510
lemma [code_unfold_post]:
haftmann@24198
  1511
  "number_of k = real_of_int (number_of k)"
huffman@36795
  1512
  unfolding number_of_is_id number_of_real_def ..
paulson@14387
  1513
paulson@14387
  1514
paulson@14387
  1515
text{*Collapse applications of @{term real} to @{term number_of}*}
paulson@14387
  1516
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
huffman@35216
  1517
by (simp add: real_of_int_def)
paulson@14387
  1518
paulson@14387
  1519
lemma real_of_nat_number_of [simp]:
paulson@14387
  1520
     "real (number_of v :: nat) =  
paulson@14387
  1521
        (if neg (number_of v :: int) then 0  
paulson@14387
  1522
         else (number_of v :: real))"
huffman@35216
  1523
by (simp add: real_of_int_real_of_nat [symmetric])
paulson@14387
  1524
haftmann@31100
  1525
declaration {*
haftmann@31100
  1526
  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
haftmann@31100
  1527
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
haftmann@31100
  1528
  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
haftmann@31100
  1529
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
haftmann@31100
  1530
  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
haftmann@31100
  1531
      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
haftmann@31100
  1532
      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
haftmann@31100
  1533
      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
haftmann@31100
  1534
      @{thm real_of_nat_number_of}, @{thm real_number_of}]
huffman@36795
  1535
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
huffman@36795
  1536
  #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
haftmann@31100
  1537
*}
paulson@14387
  1538
kleing@19023
  1539
paulson@14387
  1540
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
  1541
paulson@14387
  1542
text{*Needed in this non-standard form by Hyperreal/Transcendental*}
paulson@14387
  1543
lemma real_0_le_divide_iff:
paulson@14387
  1544
     "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
huffman@36795
  1545
by (auto simp add: zero_le_divide_iff)
paulson@14387
  1546
paulson@14387
  1547
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
  1548
by arith
paulson@14387
  1549
huffman@36839
  1550
text {* FIXME: redundant with @{text add_eq_0_iff} below *}
paulson@15085
  1551
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
  1552
by auto
paulson@14387
  1553
paulson@15085
  1554
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
  1555
by auto
paulson@14387
  1556
paulson@15085
  1557
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
  1558
by auto
paulson@14387
  1559
paulson@15085
  1560
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
  1561
by auto
paulson@14387
  1562
paulson@15085
  1563
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
  1564
by auto
paulson@14387
  1565
huffman@36839
  1566
subsection {* Lemmas about powers *}
paulson@14387
  1567
huffman@36839
  1568
text {* FIXME: declare this in Rings.thy or not at all *}
huffman@36839
  1569
declare abs_mult_self [simp]
huffman@36839
  1570
huffman@36839
  1571
(* used by Import/HOL/real.imp *)
huffman@36839
  1572
lemma two_realpow_ge_one: "(1::real) \<le> 2 ^ n"
huffman@36839
  1573
by simp
huffman@36839
  1574
huffman@36839
  1575
lemma two_realpow_gt [simp]: "real (n::nat) < 2 ^ n"
huffman@36839
  1576
apply (induct "n")
huffman@36839
  1577
apply (auto simp add: real_of_nat_Suc)
huffman@36839
  1578
apply (subst mult_2)
huffman@36839
  1579
apply (erule add_less_le_mono)
huffman@36839
  1580
apply (rule two_realpow_ge_one)
huffman@36839
  1581
done
huffman@36839
  1582
huffman@36839
  1583
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1584
lemma realpow_Suc_le_self:
huffman@36839
  1585
  fixes r :: "'a::linordered_semidom"
huffman@36839
  1586
  shows "[| 0 \<le> r; r \<le> 1 |] ==> r ^ Suc n \<le> r"
huffman@36839
  1587
by (insert power_decreasing [of 1 "Suc n" r], simp)
huffman@36839
  1588
huffman@36839
  1589
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1590
lemma realpow_minus_mult:
huffman@36839
  1591
  fixes x :: "'a::monoid_mult"
huffman@36839
  1592
  shows "0 < n \<Longrightarrow> x ^ (n - 1) * x = x ^ n"
huffman@36839
  1593
by (simp add: power_commutes split add: nat_diff_split)
huffman@36839
  1594
huffman@36839
  1595
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1596
lemma realpow_two_diff:
huffman@36839
  1597
  fixes x :: "'a::comm_ring_1"
huffman@36839
  1598
  shows "x^Suc (Suc 0) - y^Suc (Suc 0) = (x - y) * (x + y)"
huffman@36839
  1599
by (simp add: algebra_simps)
huffman@36839
  1600
huffman@36839
  1601
text {* TODO: move elsewhere *}
huffman@36839
  1602
lemma add_eq_0_iff:
huffman@36839
  1603
  fixes x y :: "'a::group_add"
huffman@36839
  1604
  shows "x + y = 0 \<longleftrightarrow> y = - x"
huffman@36839
  1605
by (auto dest: minus_unique)
huffman@36839
  1606
huffman@36839
  1607
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1608
lemma realpow_two_disj:
huffman@36839
  1609
  fixes x :: "'a::idom"
huffman@36839
  1610
  shows "(x^Suc (Suc 0) = y^Suc (Suc 0)) = (x = y | x = -y)"
huffman@36839
  1611
using realpow_two_diff [of x y]
huffman@36839
  1612
by (auto simp add: add_eq_0_iff)
huffman@36839
  1613
huffman@36839
  1614
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1615
lemma real_two_squares_add_zero_iff [simp]:
huffman@36839
  1616
  "(x * x + y * y = 0) = ((x::real) = 0 \<and> y = 0)"
huffman@36839
  1617
by (rule sum_squares_eq_zero_iff)
huffman@36839
  1618
huffman@36839
  1619
text {* TODO: no longer real-specific; rename and move elsewhere *}
huffman@36839
  1620
lemma real_squared_diff_one_factored:
huffman@36839
  1621
  fixes x :: "'a::ring_1"
huffman@36839
  1622
  shows "x * x - 1 = (x + 1) * (x - 1)"
huffman@36839
  1623
by (simp add: algebra_simps)
huffman@36839
  1624
huffman@36839
  1625
text {* FIXME: declare this [simp] for all types, or not at all *}
huffman@36839
  1626
lemma realpow_two_sum_zero_iff [simp]:
huffman@36839
  1627
     "(x ^ 2 + y ^ 2 = (0::real)) = (x = 0 & y = 0)"
huffman@36839
  1628
by (rule sum_power2_eq_zero_iff)
huffman@36839
  1629
huffman@36839
  1630
lemma real_minus_mult_self_le [simp]: "-(u * u) \<le> (x * (x::real))"
huffman@36839
  1631
by (rule_tac y = 0 in order_trans, auto)
huffman@36839
  1632
huffman@36839
  1633
lemma realpow_square_minus_le [simp]: "-(u ^ 2) \<le> (x::real) ^ 2"
huffman@36839
  1634
by (auto simp add: power2_eq_square)
huffman@36839
  1635
huffman@36839
  1636
huffman@36839
  1637
subsection{*Density of the Reals*}
paulson@14387
  1638
paulson@14387
  1639
lemma real_lbound_gt_zero:
paulson@14387
  1640
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
  1641
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
  1642
apply (simp add: min_def)
paulson@14387
  1643
done
paulson@14387
  1644
paulson@14387
  1645
haftmann@35050
  1646
text{*Similar results are proved in @{text Fields}*}
paulson@14387
  1647
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
  1648
  by auto
paulson@14387
  1649
paulson@14387
  1650
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
  1651
  by auto
paulson@14387
  1652
paulson@14387
  1653
paulson@14387
  1654
subsection{*Absolute Value Function for the Reals*}
paulson@14387
  1655
paulson@14387
  1656
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
  1657
by (simp add: abs_if)
paulson@14387
  1658
huffman@23289
  1659
(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
paulson@14387
  1660
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
haftmann@35050
  1661
by (force simp add: abs_le_iff)
paulson@14387
  1662
paulson@14387
  1663
lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
paulson@15003
  1664
by (simp add: abs_if)
paulson@14387
  1665
paulson@14387
  1666
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
  1667
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
  1668
paulson@14387
  1669
lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
webertj@20217
  1670
by simp
paulson@14387
  1671
 
paulson@14387
  1672
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
  1673
by simp
paulson@14387
  1674
berghofe@24534
  1675
haftmann@27544
  1676
subsection {* Implementation of rational real numbers *}
berghofe@24534
  1677
haftmann@27544
  1678
definition Ratreal :: "rat \<Rightarrow> real" where
haftmann@27544
  1679
  [simp]: "Ratreal = of_rat"
berghofe@24534
  1680
haftmann@24623
  1681
code_datatype Ratreal
berghofe@24534
  1682
haftmann@31998
  1683
lemma Ratreal_number_collapse [code_post]:
haftmann@27544
  1684
  "Ratreal 0 = 0"
haftmann@27544
  1685
  "Ratreal 1 = 1"
haftmann@27544
  1686
  "Ratreal (number_of k) = number_of k"
haftmann@27544
  1687
by simp_all
berghofe@24534
  1688
haftmann@31998
  1689
lemma zero_real_code [code, code_unfold]:
haftmann@27544
  1690
  "0 = Ratreal 0"
haftmann@27544
  1691
by simp
berghofe@24534
  1692
haftmann@31998
  1693
lemma one_real_code [code, code_unfold]:
haftmann@27544
  1694
  "1 = Ratreal 1"
haftmann@27544
  1695
by simp
haftmann@27544
  1696
haftmann@31998
  1697
lemma number_of_real_code [code_unfold]:
haftmann@27544
  1698
  "number_of k = Ratreal (number_of k)"
haftmann@27544
  1699
by simp
haftmann@27544
  1700
haftmann@31998
  1701
lemma Ratreal_number_of_quotient [code_post]:
haftmann@27544
  1702
  "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
haftmann@27544
  1703
by simp
haftmann@27544
  1704
haftmann@31998
  1705
lemma Ratreal_number_of_quotient2 [code_post]:
haftmann@27544
  1706
  "Ratreal (number_of r / number_of s) = number_of r / number_of s"
haftmann@27544
  1707
unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
berghofe@24534
  1708
haftmann@38857
  1709
instantiation real :: equal
haftmann@26513
  1710
begin
haftmann@26513
  1711
haftmann@38857
  1712
definition "HOL.equal (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
haftmann@26513
  1713
haftmann@38857
  1714
instance proof
haftmann@38857
  1715
qed (simp add: equal_real_def)
berghofe@24534
  1716
haftmann@38857
  1717
lemma real_equal_code [code]:
haftmann@38857
  1718
  "HOL.equal (Ratreal x) (Ratreal y) \<longleftrightarrow> HOL.equal x y"
haftmann@38857
  1719
  by (simp add: equal_real_def equal)
haftmann@26513
  1720
haftmann@38857
  1721
lemma [code nbe]:
haftmann@38857
  1722
  "HOL.equal (x::real) x \<longleftrightarrow> True"
haftmann@38857
  1723
  by (rule equal_refl)
haftmann@28351
  1724
haftmann@26513
  1725
end
berghofe@24534
  1726
haftmann@27544
  1727
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
haftmann@27652
  1728
  by (simp add: of_rat_less_eq)
berghofe@24534
  1729
haftmann@27544
  1730
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
haftmann@27652
  1731
  by (simp add: of_rat_less)
berghofe@24534
  1732
haftmann@27544
  1733
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
haftmann@27544
  1734
  by (simp add: of_rat_add)
berghofe@24534
  1735
haftmann@27544
  1736
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
haftmann@27544
  1737
  by (simp add: of_rat_mult)
haftmann@27544
  1738
haftmann@27544
  1739
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
haftmann@27544
  1740
  by (simp add: of_rat_minus)
berghofe@24534
  1741
haftmann@27544
  1742
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
haftmann@27544
  1743
  by (simp add: of_rat_diff)
berghofe@24534
  1744
haftmann@27544
  1745
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
haftmann@27544
  1746
  by (simp add: of_rat_inverse)
haftmann@27544
  1747
 
haftmann@27544
  1748
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
haftmann@27544
  1749
  by (simp add: of_rat_divide)
berghofe@24534
  1750
haftmann@31203
  1751
definition (in term_syntax)
haftmann@32657
  1752
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@32657
  1753
  [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
haftmann@31203
  1754
haftmann@37751
  1755
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1756
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1757
haftmann@31203
  1758
instantiation real :: random
haftmann@31203
  1759
begin
haftmann@31203
  1760
haftmann@31203
  1761
definition
haftmann@37751
  1762
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
haftmann@31203
  1763
haftmann@31203
  1764
instance ..
haftmann@31203
  1765
haftmann@31203
  1766
end
haftmann@31203
  1767
haftmann@37751
  1768
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1769
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1770
bulwahn@41231
  1771
instantiation real :: full_small
bulwahn@41231
  1772
begin
bulwahn@41231
  1773
bulwahn@41231
  1774
definition
bulwahn@41231
  1775
  "full_small f d = full_small (%r. f (valterm_ratreal r)) d"
bulwahn@41231
  1776
bulwahn@41231
  1777
instance ..
bulwahn@41231
  1778
bulwahn@41231
  1779
end
bulwahn@41231
  1780
haftmann@24623
  1781
text {* Setup for SML code generator *}
nipkow@23031
  1782
nipkow@23031
  1783
types_code
berghofe@24534
  1784
  real ("(int */ int)")
nipkow@23031
  1785
attach (term_of) {*
berghofe@24534
  1786
fun term_of_real (p, q) =
haftmann@24623
  1787
  let
haftmann@24623
  1788
    val rT = HOLogic.realT
berghofe@24534
  1789
  in
berghofe@24534
  1790
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
haftmann@24623
  1791
    else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
berghofe@24534
  1792
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
berghofe@24534
  1793
  end;
nipkow@23031
  1794
*}
nipkow@23031
  1795
attach (test) {*
nipkow@23031
  1796
fun gen_real i =
berghofe@24534
  1797
  let
berghofe@24534
  1798
    val p = random_range 0 i;
berghofe@24534
  1799
    val q = random_range 1 (i + 1);
berghofe@24534
  1800
    val g = Integer.gcd p q;
wenzelm@24630
  1801
    val p' = p div g;
wenzelm@24630
  1802
    val q' = q div g;
berghofe@25885
  1803
    val r = (if one_of [true, false] then p' else ~ p',
haftmann@31666
  1804
      if p' = 0 then 1 else q')
berghofe@24534
  1805
  in
berghofe@25885
  1806
    (r, fn () => term_of_real r)
berghofe@24534
  1807
  end;
nipkow@23031
  1808
*}
nipkow@23031
  1809
nipkow@23031
  1810
consts_code
haftmann@24623
  1811
  Ratreal ("(_)")
berghofe@24534
  1812
berghofe@24534
  1813
consts_code
berghofe@24534
  1814
  "of_int :: int \<Rightarrow> real" ("\<module>real'_of'_int")
berghofe@24534
  1815
attach {*
haftmann@31666
  1816
fun real_of_int i = (i, 1);
berghofe@24534
  1817
*}
berghofe@24534
  1818
blanchet@38287
  1819
declaration {*
blanchet@38287
  1820
  Nitpick_HOL.register_frac_type @{type_name real}
wenzelm@33209
  1821
   [(@{const_name zero_real_inst.zero_real}, @{const_name Nitpick.zero_frac}),
wenzelm@33209
  1822
    (@{const_name one_real_inst.one_real}, @{const_name Nitpick.one_frac}),
wenzelm@33209
  1823
    (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
wenzelm@33209
  1824
    (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
wenzelm@33209
  1825
    (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
wenzelm@33209
  1826
    (@{const_name number_real_inst.number_of_real}, @{const_name Nitpick.number_of_frac}),
wenzelm@33209
  1827
    (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
blanchet@37397
  1828
    (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_eq_frac}),
wenzelm@33209
  1829
    (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
blanchet@33197
  1830
*}
blanchet@33197
  1831
blanchet@33197
  1832
lemmas [nitpick_def] = inverse_real_inst.inverse_real
blanchet@33197
  1833
    number_real_inst.number_of_real one_real_inst.one_real
blanchet@37397
  1834
    ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
blanchet@33197
  1835
    times_real_inst.times_real uminus_real_inst.uminus_real
blanchet@33197
  1836
    zero_real_inst.zero_real
blanchet@33197
  1837
paulson@5588
  1838
end