src/HOL/Real/RComplete.thy
author wenzelm
Wed Sep 17 21:27:14 2008 +0200 (2008-09-17)
changeset 28263 69eaa97e7e96
parent 28091 50f2d6ba024c
child 28562 4e74209f113e
permissions -rw-r--r--
moved global ML bindings to global place;
wenzelm@16893
     1
(*  Title       : HOL/Real/RComplete.thy
paulson@7219
     2
    ID          : $Id$
wenzelm@16893
     3
    Author      : Jacques D. Fleuriot, University of Edinburgh
wenzelm@16893
     4
    Author      : Larry Paulson, University of Cambridge
wenzelm@16893
     5
    Author      : Jeremy Avigad, Carnegie Mellon University
wenzelm@16893
     6
    Author      : Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
wenzelm@16893
     7
*)
paulson@5078
     8
wenzelm@16893
     9
header {* Completeness of the Reals; Floor and Ceiling Functions *}
paulson@14365
    10
nipkow@15131
    11
theory RComplete
nipkow@15140
    12
imports Lubs RealDef
nipkow@15131
    13
begin
paulson@14365
    14
paulson@14365
    15
lemma real_sum_of_halves: "x/2 + x/2 = (x::real)"
wenzelm@16893
    16
  by simp
paulson@14365
    17
paulson@14365
    18
wenzelm@16893
    19
subsection {* Completeness of Positive Reals *}
wenzelm@16893
    20
wenzelm@16893
    21
text {*
wenzelm@16893
    22
  Supremum property for the set of positive reals
wenzelm@16893
    23
wenzelm@16893
    24
  Let @{text "P"} be a non-empty set of positive reals, with an upper
wenzelm@16893
    25
  bound @{text "y"}.  Then @{text "P"} has a least upper bound
wenzelm@16893
    26
  (written @{text "S"}).
paulson@14365
    27
wenzelm@16893
    28
  FIXME: Can the premise be weakened to @{text "\<forall>x \<in> P. x\<le> y"}?
wenzelm@16893
    29
*}
wenzelm@16893
    30
wenzelm@16893
    31
lemma posreal_complete:
wenzelm@16893
    32
  assumes positive_P: "\<forall>x \<in> P. (0::real) < x"
wenzelm@16893
    33
    and not_empty_P: "\<exists>x. x \<in> P"
wenzelm@16893
    34
    and upper_bound_Ex: "\<exists>y. \<forall>x \<in> P. x<y"
wenzelm@16893
    35
  shows "\<exists>S. \<forall>y. (\<exists>x \<in> P. y < x) = (y < S)"
wenzelm@16893
    36
proof (rule exI, rule allI)
wenzelm@16893
    37
  fix y
wenzelm@16893
    38
  let ?pP = "{w. real_of_preal w \<in> P}"
paulson@14365
    39
wenzelm@16893
    40
  show "(\<exists>x\<in>P. y < x) = (y < real_of_preal (psup ?pP))"
wenzelm@16893
    41
  proof (cases "0 < y")
wenzelm@16893
    42
    assume neg_y: "\<not> 0 < y"
wenzelm@16893
    43
    show ?thesis
wenzelm@16893
    44
    proof
wenzelm@16893
    45
      assume "\<exists>x\<in>P. y < x"
wenzelm@16893
    46
      have "\<forall>x. y < real_of_preal x"
wenzelm@16893
    47
        using neg_y by (rule real_less_all_real2)
wenzelm@16893
    48
      thus "y < real_of_preal (psup ?pP)" ..
wenzelm@16893
    49
    next
wenzelm@16893
    50
      assume "y < real_of_preal (psup ?pP)"
wenzelm@16893
    51
      obtain "x" where x_in_P: "x \<in> P" using not_empty_P ..
wenzelm@16893
    52
      hence "0 < x" using positive_P by simp
wenzelm@16893
    53
      hence "y < x" using neg_y by simp
wenzelm@16893
    54
      thus "\<exists>x \<in> P. y < x" using x_in_P ..
wenzelm@16893
    55
    qed
wenzelm@16893
    56
  next
wenzelm@16893
    57
    assume pos_y: "0 < y"
paulson@14365
    58
wenzelm@16893
    59
    then obtain py where y_is_py: "y = real_of_preal py"
wenzelm@16893
    60
      by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@16893
    61
wenzelm@23389
    62
    obtain a where "a \<in> P" using not_empty_P ..
wenzelm@23389
    63
    with positive_P have a_pos: "0 < a" ..
wenzelm@16893
    64
    then obtain pa where "a = real_of_preal pa"
wenzelm@16893
    65
      by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@23389
    66
    hence "pa \<in> ?pP" using `a \<in> P` by auto
wenzelm@16893
    67
    hence pP_not_empty: "?pP \<noteq> {}" by auto
paulson@14365
    68
wenzelm@16893
    69
    obtain sup where sup: "\<forall>x \<in> P. x < sup"
wenzelm@16893
    70
      using upper_bound_Ex ..
wenzelm@23389
    71
    from this and `a \<in> P` have "a < sup" ..
wenzelm@16893
    72
    hence "0 < sup" using a_pos by arith
wenzelm@16893
    73
    then obtain possup where "sup = real_of_preal possup"
wenzelm@16893
    74
      by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@16893
    75
    hence "\<forall>X \<in> ?pP. X \<le> possup"
wenzelm@16893
    76
      using sup by (auto simp add: real_of_preal_lessI)
wenzelm@16893
    77
    with pP_not_empty have psup: "\<And>Z. (\<exists>X \<in> ?pP. Z < X) = (Z < psup ?pP)"
wenzelm@16893
    78
      by (rule preal_complete)
wenzelm@16893
    79
wenzelm@16893
    80
    show ?thesis
wenzelm@16893
    81
    proof
wenzelm@16893
    82
      assume "\<exists>x \<in> P. y < x"
wenzelm@16893
    83
      then obtain x where x_in_P: "x \<in> P" and y_less_x: "y < x" ..
wenzelm@16893
    84
      hence "0 < x" using pos_y by arith
wenzelm@16893
    85
      then obtain px where x_is_px: "x = real_of_preal px"
wenzelm@16893
    86
        by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@16893
    87
wenzelm@16893
    88
      have py_less_X: "\<exists>X \<in> ?pP. py < X"
wenzelm@16893
    89
      proof
wenzelm@16893
    90
        show "py < px" using y_is_py and x_is_px and y_less_x
wenzelm@16893
    91
          by (simp add: real_of_preal_lessI)
wenzelm@16893
    92
        show "px \<in> ?pP" using x_in_P and x_is_px by simp
wenzelm@16893
    93
      qed
paulson@14365
    94
wenzelm@16893
    95
      have "(\<exists>X \<in> ?pP. py < X) ==> (py < psup ?pP)"
wenzelm@16893
    96
        using psup by simp
wenzelm@16893
    97
      hence "py < psup ?pP" using py_less_X by simp
wenzelm@16893
    98
      thus "y < real_of_preal (psup {w. real_of_preal w \<in> P})"
wenzelm@16893
    99
        using y_is_py and pos_y by (simp add: real_of_preal_lessI)
wenzelm@16893
   100
    next
wenzelm@16893
   101
      assume y_less_psup: "y < real_of_preal (psup ?pP)"
paulson@14365
   102
wenzelm@16893
   103
      hence "py < psup ?pP" using y_is_py
wenzelm@16893
   104
        by (simp add: real_of_preal_lessI)
wenzelm@16893
   105
      then obtain "X" where py_less_X: "py < X" and X_in_pP: "X \<in> ?pP"
wenzelm@16893
   106
        using psup by auto
wenzelm@16893
   107
      then obtain x where x_is_X: "x = real_of_preal X"
wenzelm@16893
   108
        by (simp add: real_gt_zero_preal_Ex)
wenzelm@16893
   109
      hence "y < x" using py_less_X and y_is_py
wenzelm@16893
   110
        by (simp add: real_of_preal_lessI)
wenzelm@16893
   111
wenzelm@16893
   112
      moreover have "x \<in> P" using x_is_X and X_in_pP by simp
wenzelm@16893
   113
wenzelm@16893
   114
      ultimately show "\<exists> x \<in> P. y < x" ..
wenzelm@16893
   115
    qed
wenzelm@16893
   116
  qed
wenzelm@16893
   117
qed
wenzelm@16893
   118
wenzelm@16893
   119
text {*
wenzelm@16893
   120
  \medskip Completeness properties using @{text "isUb"}, @{text "isLub"} etc.
wenzelm@16893
   121
*}
paulson@14365
   122
paulson@14365
   123
lemma real_isLub_unique: "[| isLub R S x; isLub R S y |] ==> x = (y::real)"
wenzelm@16893
   124
  apply (frule isLub_isUb)
wenzelm@16893
   125
  apply (frule_tac x = y in isLub_isUb)
wenzelm@16893
   126
  apply (blast intro!: order_antisym dest!: isLub_le_isUb)
wenzelm@16893
   127
  done
paulson@14365
   128
paulson@5078
   129
wenzelm@16893
   130
text {*
wenzelm@16893
   131
  \medskip Completeness theorem for the positive reals (again).
wenzelm@16893
   132
*}
wenzelm@16893
   133
wenzelm@16893
   134
lemma posreals_complete:
wenzelm@16893
   135
  assumes positive_S: "\<forall>x \<in> S. 0 < x"
wenzelm@16893
   136
    and not_empty_S: "\<exists>x. x \<in> S"
wenzelm@16893
   137
    and upper_bound_Ex: "\<exists>u. isUb (UNIV::real set) S u"
wenzelm@16893
   138
  shows "\<exists>t. isLub (UNIV::real set) S t"
wenzelm@16893
   139
proof
wenzelm@16893
   140
  let ?pS = "{w. real_of_preal w \<in> S}"
wenzelm@16893
   141
wenzelm@16893
   142
  obtain u where "isUb UNIV S u" using upper_bound_Ex ..
wenzelm@16893
   143
  hence sup: "\<forall>x \<in> S. x \<le> u" by (simp add: isUb_def setle_def)
wenzelm@16893
   144
wenzelm@16893
   145
  obtain x where x_in_S: "x \<in> S" using not_empty_S ..
wenzelm@16893
   146
  hence x_gt_zero: "0 < x" using positive_S by simp
wenzelm@16893
   147
  have  "x \<le> u" using sup and x_in_S ..
wenzelm@16893
   148
  hence "0 < u" using x_gt_zero by arith
wenzelm@16893
   149
wenzelm@16893
   150
  then obtain pu where u_is_pu: "u = real_of_preal pu"
wenzelm@16893
   151
    by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@16893
   152
wenzelm@16893
   153
  have pS_less_pu: "\<forall>pa \<in> ?pS. pa \<le> pu"
wenzelm@16893
   154
  proof
wenzelm@16893
   155
    fix pa
wenzelm@16893
   156
    assume "pa \<in> ?pS"
wenzelm@16893
   157
    then obtain a where "a \<in> S" and "a = real_of_preal pa"
wenzelm@16893
   158
      by simp
wenzelm@16893
   159
    moreover hence "a \<le> u" using sup by simp
wenzelm@16893
   160
    ultimately show "pa \<le> pu"
wenzelm@16893
   161
      using sup and u_is_pu by (simp add: real_of_preal_le_iff)
wenzelm@16893
   162
  qed
paulson@14365
   163
wenzelm@16893
   164
  have "\<forall>y \<in> S. y \<le> real_of_preal (psup ?pS)"
wenzelm@16893
   165
  proof
wenzelm@16893
   166
    fix y
wenzelm@16893
   167
    assume y_in_S: "y \<in> S"
wenzelm@16893
   168
    hence "0 < y" using positive_S by simp
wenzelm@16893
   169
    then obtain py where y_is_py: "y = real_of_preal py"
wenzelm@16893
   170
      by (auto simp add: real_gt_zero_preal_Ex)
wenzelm@16893
   171
    hence py_in_pS: "py \<in> ?pS" using y_in_S by simp
wenzelm@16893
   172
    with pS_less_pu have "py \<le> psup ?pS"
wenzelm@16893
   173
      by (rule preal_psup_le)
wenzelm@16893
   174
    thus "y \<le> real_of_preal (psup ?pS)"
wenzelm@16893
   175
      using y_is_py by (simp add: real_of_preal_le_iff)
wenzelm@16893
   176
  qed
wenzelm@16893
   177
wenzelm@16893
   178
  moreover {
wenzelm@16893
   179
    fix x
wenzelm@16893
   180
    assume x_ub_S: "\<forall>y\<in>S. y \<le> x"
wenzelm@16893
   181
    have "real_of_preal (psup ?pS) \<le> x"
wenzelm@16893
   182
    proof -
wenzelm@16893
   183
      obtain "s" where s_in_S: "s \<in> S" using not_empty_S ..
wenzelm@16893
   184
      hence s_pos: "0 < s" using positive_S by simp
wenzelm@16893
   185
wenzelm@16893
   186
      hence "\<exists> ps. s = real_of_preal ps" by (simp add: real_gt_zero_preal_Ex)
wenzelm@16893
   187
      then obtain "ps" where s_is_ps: "s = real_of_preal ps" ..
wenzelm@16893
   188
      hence ps_in_pS: "ps \<in> {w. real_of_preal w \<in> S}" using s_in_S by simp
wenzelm@16893
   189
wenzelm@16893
   190
      from x_ub_S have "s \<le> x" using s_in_S ..
wenzelm@16893
   191
      hence "0 < x" using s_pos by simp
wenzelm@16893
   192
      hence "\<exists> px. x = real_of_preal px" by (simp add: real_gt_zero_preal_Ex)
wenzelm@16893
   193
      then obtain "px" where x_is_px: "x = real_of_preal px" ..
wenzelm@16893
   194
wenzelm@16893
   195
      have "\<forall>pe \<in> ?pS. pe \<le> px"
wenzelm@16893
   196
      proof
wenzelm@16893
   197
	fix pe
wenzelm@16893
   198
	assume "pe \<in> ?pS"
wenzelm@16893
   199
	hence "real_of_preal pe \<in> S" by simp
wenzelm@16893
   200
	hence "real_of_preal pe \<le> x" using x_ub_S by simp
wenzelm@16893
   201
	thus "pe \<le> px" using x_is_px by (simp add: real_of_preal_le_iff)
wenzelm@16893
   202
      qed
wenzelm@16893
   203
wenzelm@16893
   204
      moreover have "?pS \<noteq> {}" using ps_in_pS by auto
wenzelm@16893
   205
      ultimately have "(psup ?pS) \<le> px" by (simp add: psup_le_ub)
wenzelm@16893
   206
      thus "real_of_preal (psup ?pS) \<le> x" using x_is_px by (simp add: real_of_preal_le_iff)
wenzelm@16893
   207
    qed
wenzelm@16893
   208
  }
wenzelm@16893
   209
  ultimately show "isLub UNIV S (real_of_preal (psup ?pS))"
wenzelm@16893
   210
    by (simp add: isLub_def leastP_def isUb_def setle_def setge_def)
wenzelm@16893
   211
qed
wenzelm@16893
   212
wenzelm@16893
   213
text {*
wenzelm@16893
   214
  \medskip reals Completeness (again!)
wenzelm@16893
   215
*}
paulson@14365
   216
wenzelm@16893
   217
lemma reals_complete:
wenzelm@16893
   218
  assumes notempty_S: "\<exists>X. X \<in> S"
wenzelm@16893
   219
    and exists_Ub: "\<exists>Y. isUb (UNIV::real set) S Y"
wenzelm@16893
   220
  shows "\<exists>t. isLub (UNIV :: real set) S t"
wenzelm@16893
   221
proof -
wenzelm@16893
   222
  obtain X where X_in_S: "X \<in> S" using notempty_S ..
wenzelm@16893
   223
  obtain Y where Y_isUb: "isUb (UNIV::real set) S Y"
wenzelm@16893
   224
    using exists_Ub ..
wenzelm@16893
   225
  let ?SHIFT = "{z. \<exists>x \<in>S. z = x + (-X) + 1} \<inter> {x. 0 < x}"
wenzelm@16893
   226
wenzelm@16893
   227
  {
wenzelm@16893
   228
    fix x
wenzelm@16893
   229
    assume "isUb (UNIV::real set) S x"
wenzelm@16893
   230
    hence S_le_x: "\<forall> y \<in> S. y <= x"
wenzelm@16893
   231
      by (simp add: isUb_def setle_def)
wenzelm@16893
   232
    {
wenzelm@16893
   233
      fix s
wenzelm@16893
   234
      assume "s \<in> {z. \<exists>x\<in>S. z = x + - X + 1}"
wenzelm@16893
   235
      hence "\<exists> x \<in> S. s = x + -X + 1" ..
wenzelm@16893
   236
      then obtain x1 where "x1 \<in> S" and "s = x1 + (-X) + 1" ..
wenzelm@16893
   237
      moreover hence "x1 \<le> x" using S_le_x by simp
wenzelm@16893
   238
      ultimately have "s \<le> x + - X + 1" by arith
wenzelm@16893
   239
    }
wenzelm@16893
   240
    then have "isUb (UNIV::real set) ?SHIFT (x + (-X) + 1)"
wenzelm@16893
   241
      by (auto simp add: isUb_def setle_def)
wenzelm@16893
   242
  } note S_Ub_is_SHIFT_Ub = this
wenzelm@16893
   243
wenzelm@16893
   244
  hence "isUb UNIV ?SHIFT (Y + (-X) + 1)" using Y_isUb by simp
wenzelm@16893
   245
  hence "\<exists>Z. isUb UNIV ?SHIFT Z" ..
wenzelm@16893
   246
  moreover have "\<forall>y \<in> ?SHIFT. 0 < y" by auto
wenzelm@16893
   247
  moreover have shifted_not_empty: "\<exists>u. u \<in> ?SHIFT"
wenzelm@16893
   248
    using X_in_S and Y_isUb by auto
wenzelm@16893
   249
  ultimately obtain t where t_is_Lub: "isLub UNIV ?SHIFT t"
wenzelm@16893
   250
    using posreals_complete [of ?SHIFT] by blast
wenzelm@16893
   251
wenzelm@16893
   252
  show ?thesis
wenzelm@16893
   253
  proof
wenzelm@16893
   254
    show "isLub UNIV S (t + X + (-1))"
wenzelm@16893
   255
    proof (rule isLubI2)
wenzelm@16893
   256
      {
wenzelm@16893
   257
        fix x
wenzelm@16893
   258
        assume "isUb (UNIV::real set) S x"
wenzelm@16893
   259
        hence "isUb (UNIV::real set) (?SHIFT) (x + (-X) + 1)"
wenzelm@16893
   260
	  using S_Ub_is_SHIFT_Ub by simp
wenzelm@16893
   261
        hence "t \<le> (x + (-X) + 1)"
wenzelm@16893
   262
	  using t_is_Lub by (simp add: isLub_le_isUb)
wenzelm@16893
   263
        hence "t + X + -1 \<le> x" by arith
wenzelm@16893
   264
      }
wenzelm@16893
   265
      then show "(t + X + -1) <=* Collect (isUb UNIV S)"
wenzelm@16893
   266
	by (simp add: setgeI)
wenzelm@16893
   267
    next
wenzelm@16893
   268
      show "isUb UNIV S (t + X + -1)"
wenzelm@16893
   269
      proof -
wenzelm@16893
   270
        {
wenzelm@16893
   271
          fix y
wenzelm@16893
   272
          assume y_in_S: "y \<in> S"
wenzelm@16893
   273
          have "y \<le> t + X + -1"
wenzelm@16893
   274
          proof -
wenzelm@16893
   275
            obtain "u" where u_in_shift: "u \<in> ?SHIFT" using shifted_not_empty ..
wenzelm@16893
   276
            hence "\<exists> x \<in> S. u = x + - X + 1" by simp
wenzelm@16893
   277
            then obtain "x" where x_and_u: "u = x + - X + 1" ..
wenzelm@16893
   278
            have u_le_t: "u \<le> t" using u_in_shift and t_is_Lub by (simp add: isLubD2)
wenzelm@16893
   279
wenzelm@16893
   280
            show ?thesis
wenzelm@16893
   281
            proof cases
wenzelm@16893
   282
              assume "y \<le> x"
wenzelm@16893
   283
              moreover have "x = u + X + - 1" using x_and_u by arith
wenzelm@16893
   284
              moreover have "u + X + - 1  \<le> t + X + -1" using u_le_t by arith
wenzelm@16893
   285
              ultimately show "y  \<le> t + X + -1" by arith
wenzelm@16893
   286
            next
wenzelm@16893
   287
              assume "~(y \<le> x)"
wenzelm@16893
   288
              hence x_less_y: "x < y" by arith
wenzelm@16893
   289
wenzelm@16893
   290
              have "x + (-X) + 1 \<in> ?SHIFT" using x_and_u and u_in_shift by simp
wenzelm@16893
   291
              hence "0 < x + (-X) + 1" by simp
wenzelm@16893
   292
              hence "0 < y + (-X) + 1" using x_less_y by arith
wenzelm@16893
   293
              hence "y + (-X) + 1 \<in> ?SHIFT" using y_in_S by simp
wenzelm@16893
   294
              hence "y + (-X) + 1 \<le> t" using t_is_Lub  by (simp add: isLubD2)
wenzelm@16893
   295
              thus ?thesis by simp
wenzelm@16893
   296
            qed
wenzelm@16893
   297
          qed
wenzelm@16893
   298
        }
wenzelm@16893
   299
        then show ?thesis by (simp add: isUb_def setle_def)
wenzelm@16893
   300
      qed
wenzelm@16893
   301
    qed
wenzelm@16893
   302
  qed
wenzelm@16893
   303
qed
paulson@14365
   304
paulson@14365
   305
wenzelm@16893
   306
subsection {* The Archimedean Property of the Reals *}
wenzelm@16893
   307
wenzelm@16893
   308
theorem reals_Archimedean:
wenzelm@16893
   309
  assumes x_pos: "0 < x"
wenzelm@16893
   310
  shows "\<exists>n. inverse (real (Suc n)) < x"
wenzelm@16893
   311
proof (rule ccontr)
wenzelm@16893
   312
  assume contr: "\<not> ?thesis"
wenzelm@16893
   313
  have "\<forall>n. x * real (Suc n) <= 1"
wenzelm@16893
   314
  proof
wenzelm@16893
   315
    fix n
wenzelm@16893
   316
    from contr have "x \<le> inverse (real (Suc n))"
wenzelm@16893
   317
      by (simp add: linorder_not_less)
wenzelm@16893
   318
    hence "x \<le> (1 / (real (Suc n)))"
wenzelm@16893
   319
      by (simp add: inverse_eq_divide)
wenzelm@16893
   320
    moreover have "0 \<le> real (Suc n)"
wenzelm@16893
   321
      by (rule real_of_nat_ge_zero)
wenzelm@16893
   322
    ultimately have "x * real (Suc n) \<le> (1 / real (Suc n)) * real (Suc n)"
wenzelm@16893
   323
      by (rule mult_right_mono)
wenzelm@16893
   324
    thus "x * real (Suc n) \<le> 1" by simp
wenzelm@16893
   325
  qed
wenzelm@16893
   326
  hence "{z. \<exists>n. z = x * (real (Suc n))} *<= 1"
wenzelm@16893
   327
    by (simp add: setle_def, safe, rule spec)
wenzelm@16893
   328
  hence "isUb (UNIV::real set) {z. \<exists>n. z = x * (real (Suc n))} 1"
wenzelm@16893
   329
    by (simp add: isUbI)
wenzelm@16893
   330
  hence "\<exists>Y. isUb (UNIV::real set) {z. \<exists>n. z = x* (real (Suc n))} Y" ..
wenzelm@16893
   331
  moreover have "\<exists>X. X \<in> {z. \<exists>n. z = x* (real (Suc n))}" by auto
wenzelm@16893
   332
  ultimately have "\<exists>t. isLub UNIV {z. \<exists>n. z = x * real (Suc n)} t"
wenzelm@16893
   333
    by (simp add: reals_complete)
wenzelm@16893
   334
  then obtain "t" where
wenzelm@16893
   335
    t_is_Lub: "isLub UNIV {z. \<exists>n. z = x * real (Suc n)} t" ..
wenzelm@16893
   336
wenzelm@16893
   337
  have "\<forall>n::nat. x * real n \<le> t + - x"
wenzelm@16893
   338
  proof
wenzelm@16893
   339
    fix n
wenzelm@16893
   340
    from t_is_Lub have "x * real (Suc n) \<le> t"
wenzelm@16893
   341
      by (simp add: isLubD2)
wenzelm@16893
   342
    hence  "x * (real n) + x \<le> t"
wenzelm@16893
   343
      by (simp add: right_distrib real_of_nat_Suc)
wenzelm@16893
   344
    thus  "x * (real n) \<le> t + - x" by arith
wenzelm@16893
   345
  qed
paulson@14365
   346
wenzelm@16893
   347
  hence "\<forall>m. x * real (Suc m) \<le> t + - x" by simp
wenzelm@16893
   348
  hence "{z. \<exists>n. z = x * (real (Suc n))}  *<= (t + - x)"
wenzelm@16893
   349
    by (auto simp add: setle_def)
wenzelm@16893
   350
  hence "isUb (UNIV::real set) {z. \<exists>n. z = x * (real (Suc n))} (t + (-x))"
wenzelm@16893
   351
    by (simp add: isUbI)
wenzelm@16893
   352
  hence "t \<le> t + - x"
wenzelm@16893
   353
    using t_is_Lub by (simp add: isLub_le_isUb)
wenzelm@16893
   354
  thus False using x_pos by arith
wenzelm@16893
   355
qed
wenzelm@16893
   356
wenzelm@16893
   357
text {*
wenzelm@16893
   358
  There must be other proofs, e.g. @{text "Suc"} of the largest
wenzelm@16893
   359
  integer in the cut representing @{text "x"}.
wenzelm@16893
   360
*}
paulson@14365
   361
paulson@14365
   362
lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
wenzelm@16893
   363
proof cases
wenzelm@16893
   364
  assume "x \<le> 0"
wenzelm@16893
   365
  hence "x < real (1::nat)" by simp
wenzelm@16893
   366
  thus ?thesis ..
wenzelm@16893
   367
next
wenzelm@16893
   368
  assume "\<not> x \<le> 0"
wenzelm@16893
   369
  hence x_greater_zero: "0 < x" by simp
wenzelm@16893
   370
  hence "0 < inverse x" by simp
wenzelm@16893
   371
  then obtain n where "inverse (real (Suc n)) < inverse x"
wenzelm@16893
   372
    using reals_Archimedean by blast
wenzelm@16893
   373
  hence "inverse (real (Suc n)) * x < inverse x * x"
wenzelm@16893
   374
    using x_greater_zero by (rule mult_strict_right_mono)
wenzelm@16893
   375
  hence "inverse (real (Suc n)) * x < 1"
huffman@23008
   376
    using x_greater_zero by simp
wenzelm@16893
   377
  hence "real (Suc n) * (inverse (real (Suc n)) * x) < real (Suc n) * 1"
wenzelm@16893
   378
    by (rule mult_strict_left_mono) simp
wenzelm@16893
   379
  hence "x < real (Suc n)"
nipkow@23477
   380
    by (simp add: ring_simps)
wenzelm@16893
   381
  thus "\<exists>(n::nat). x < real n" ..
wenzelm@16893
   382
qed
paulson@14365
   383
wenzelm@16893
   384
lemma reals_Archimedean3:
wenzelm@16893
   385
  assumes x_greater_zero: "0 < x"
wenzelm@16893
   386
  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
wenzelm@16893
   387
proof
wenzelm@16893
   388
  fix y
wenzelm@16893
   389
  have x_not_zero: "x \<noteq> 0" using x_greater_zero by simp
wenzelm@16893
   390
  obtain n where "y * inverse x < real (n::nat)"
wenzelm@16893
   391
    using reals_Archimedean2 ..
wenzelm@16893
   392
  hence "y * inverse x * x < real n * x"
wenzelm@16893
   393
    using x_greater_zero by (simp add: mult_strict_right_mono)
wenzelm@16893
   394
  hence "x * inverse x * y < x * real n"
nipkow@23477
   395
    by (simp add: ring_simps)
wenzelm@16893
   396
  hence "y < real (n::nat) * x"
nipkow@23477
   397
    using x_not_zero by (simp add: ring_simps)
wenzelm@16893
   398
  thus "\<exists>(n::nat). y < real n * x" ..
wenzelm@16893
   399
qed
paulson@14365
   400
avigad@16819
   401
lemma reals_Archimedean6:
avigad@16819
   402
     "0 \<le> r ==> \<exists>(n::nat). real (n - 1) \<le> r & r < real (n)"
avigad@16819
   403
apply (insert reals_Archimedean2 [of r], safe)
huffman@23012
   404
apply (subgoal_tac "\<exists>x::nat. r < real x \<and> (\<forall>y. r < real y \<longrightarrow> x \<le> y)", auto)
avigad@16819
   405
apply (rule_tac x = x in exI)
avigad@16819
   406
apply (case_tac x, simp)
avigad@16819
   407
apply (rename_tac x')
avigad@16819
   408
apply (drule_tac x = x' in spec, simp)
huffman@23012
   409
apply (rule_tac x="LEAST n. r < real n" in exI, safe)
huffman@23012
   410
apply (erule LeastI, erule Least_le)
avigad@16819
   411
done
avigad@16819
   412
avigad@16819
   413
lemma reals_Archimedean6a: "0 \<le> r ==> \<exists>n. real (n) \<le> r & r < real (Suc n)"
wenzelm@16893
   414
  by (drule reals_Archimedean6) auto
avigad@16819
   415
avigad@16819
   416
lemma reals_Archimedean_6b_int:
avigad@16819
   417
     "0 \<le> r ==> \<exists>n::int. real n \<le> r & r < real (n+1)"
avigad@16819
   418
apply (drule reals_Archimedean6a, auto)
avigad@16819
   419
apply (rule_tac x = "int n" in exI)
avigad@16819
   420
apply (simp add: real_of_int_real_of_nat real_of_nat_Suc)
avigad@16819
   421
done
avigad@16819
   422
avigad@16819
   423
lemma reals_Archimedean_6c_int:
avigad@16819
   424
     "r < 0 ==> \<exists>n::int. real n \<le> r & r < real (n+1)"
avigad@16819
   425
apply (rule reals_Archimedean_6b_int [of "-r", THEN exE], simp, auto)
avigad@16819
   426
apply (rename_tac n)
huffman@22998
   427
apply (drule order_le_imp_less_or_eq, auto)
avigad@16819
   428
apply (rule_tac x = "- n - 1" in exI)
avigad@16819
   429
apply (rule_tac [2] x = "- n" in exI, auto)
avigad@16819
   430
done
avigad@16819
   431
avigad@16819
   432
nipkow@28091
   433
subsection{*Density of the Rational Reals in the Reals*}
nipkow@28091
   434
nipkow@28091
   435
text{* This density proof is due to Stefan Richter and was ported by TN.  The
nipkow@28091
   436
original source is \emph{Real Analysis} by H.L. Royden.
nipkow@28091
   437
It employs the Archimedean property of the reals. *}
nipkow@28091
   438
nipkow@28091
   439
lemma Rats_dense_in_nn_real: fixes x::real
nipkow@28091
   440
assumes "0\<le>x" and "x<y" shows "\<exists>r \<in> \<rat>.  x<r \<and> r<y"
nipkow@28091
   441
proof -
nipkow@28091
   442
  from `x<y` have "0 < y-x" by simp
nipkow@28091
   443
  with reals_Archimedean obtain q::nat 
nipkow@28091
   444
    where q: "inverse (real q) < y-x" and "0 < real q" by auto  
nipkow@28091
   445
  def p \<equiv> "LEAST n.  y \<le> real (Suc n)/real q"  
nipkow@28091
   446
  from reals_Archimedean2 obtain n::nat where "y * real q < real n" by auto
nipkow@28091
   447
  with `0 < real q` have ex: "y \<le> real n/real q" (is "?P n")
nipkow@28091
   448
    by (simp add: pos_less_divide_eq[THEN sym])
nipkow@28091
   449
  also from assms have "\<not> y \<le> real (0::nat) / real q" by simp
nipkow@28091
   450
  ultimately have main: "(LEAST n. y \<le> real n/real q) = Suc p"
nipkow@28091
   451
    by (unfold p_def) (rule Least_Suc)
nipkow@28091
   452
  also from ex have "?P (LEAST x. ?P x)" by (rule LeastI)
nipkow@28091
   453
  ultimately have suc: "y \<le> real (Suc p) / real q" by simp
nipkow@28091
   454
  def r \<equiv> "real p/real q"
nipkow@28091
   455
  have "x = y-(y-x)" by simp
nipkow@28091
   456
  also from suc q have "\<dots> < real (Suc p)/real q - inverse (real q)" by arith
nipkow@28091
   457
  also have "\<dots> = real p / real q"
nipkow@28091
   458
    by (simp only: inverse_eq_divide real_diff_def real_of_nat_Suc 
nipkow@28091
   459
    minus_divide_left add_divide_distrib[THEN sym]) simp
nipkow@28091
   460
  finally have "x<r" by (unfold r_def)
nipkow@28091
   461
  have "p<Suc p" .. also note main[THEN sym]
nipkow@28091
   462
  finally have "\<not> ?P p"  by (rule not_less_Least)
nipkow@28091
   463
  hence "r<y" by (simp add: r_def)
nipkow@28091
   464
  from r_def have "r \<in> \<rat>" by simp
nipkow@28091
   465
  with `x<r` `r<y` show ?thesis by fast
nipkow@28091
   466
qed
nipkow@28091
   467
nipkow@28091
   468
theorem Rats_dense_in_real: fixes x y :: real
nipkow@28091
   469
assumes "x<y" shows "\<exists>r \<in> \<rat>.  x<r \<and> r<y"
nipkow@28091
   470
proof -
nipkow@28091
   471
  from reals_Archimedean2 obtain n::nat where "-x < real n" by auto
nipkow@28091
   472
  hence "0 \<le> x + real n" by arith
nipkow@28091
   473
  also from `x<y` have "x + real n < y + real n" by arith
nipkow@28091
   474
  ultimately have "\<exists>r \<in> \<rat>. x + real n < r \<and> r < y + real n"
nipkow@28091
   475
    by(rule Rats_dense_in_nn_real)
nipkow@28091
   476
  then obtain r where "r \<in> \<rat>" and r2: "x + real n < r" 
nipkow@28091
   477
    and r3: "r < y + real n"
nipkow@28091
   478
    by blast
nipkow@28091
   479
  have "r - real n = r + real (int n)/real (-1::int)" by simp
nipkow@28091
   480
  also from `r\<in>\<rat>` have "r + real (int n)/real (-1::int) \<in> \<rat>" by simp
nipkow@28091
   481
  also from r2 have "x < r - real n" by arith
nipkow@28091
   482
  moreover from r3 have "r - real n < y" by arith
nipkow@28091
   483
  ultimately show ?thesis by fast
nipkow@28091
   484
qed
nipkow@28091
   485
nipkow@28091
   486
paulson@14641
   487
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
paulson@14641
   488
wenzelm@19765
   489
definition
wenzelm@21404
   490
  floor :: "real => int" where
haftmann@27435
   491
  [code func del]: "floor r = (LEAST n::int. r < real (n+1))"
paulson@14641
   492
wenzelm@21404
   493
definition
wenzelm@21404
   494
  ceiling :: "real => int" where
wenzelm@19765
   495
  "ceiling r = - floor (- r)"
paulson@14641
   496
wenzelm@21210
   497
notation (xsymbols)
wenzelm@21404
   498
  floor  ("\<lfloor>_\<rfloor>") and
wenzelm@19765
   499
  ceiling  ("\<lceil>_\<rceil>")
paulson@14641
   500
wenzelm@21210
   501
notation (HTML output)
wenzelm@21404
   502
  floor  ("\<lfloor>_\<rfloor>") and
wenzelm@19765
   503
  ceiling  ("\<lceil>_\<rceil>")
paulson@14641
   504
paulson@14641
   505
paulson@14641
   506
lemma number_of_less_real_of_int_iff [simp]:
paulson@14641
   507
     "((number_of n) < real (m::int)) = (number_of n < m)"
paulson@14641
   508
apply auto
paulson@14641
   509
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   510
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   511
done
paulson@14641
   512
paulson@14641
   513
lemma number_of_less_real_of_int_iff2 [simp]:
paulson@14641
   514
     "(real (m::int) < (number_of n)) = (m < number_of n)"
paulson@14641
   515
apply auto
paulson@14641
   516
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   517
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   518
done
paulson@14641
   519
paulson@14641
   520
lemma number_of_le_real_of_int_iff [simp]:
paulson@14641
   521
     "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
paulson@14641
   522
by (simp add: linorder_not_less [symmetric])
paulson@14641
   523
paulson@14641
   524
lemma number_of_le_real_of_int_iff2 [simp]:
paulson@14641
   525
     "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"
paulson@14641
   526
by (simp add: linorder_not_less [symmetric])
paulson@14641
   527
paulson@14641
   528
lemma floor_zero [simp]: "floor 0 = 0"
avigad@16819
   529
apply (simp add: floor_def del: real_of_int_add)
avigad@16819
   530
apply (rule Least_equality)
avigad@16819
   531
apply simp_all
paulson@14641
   532
done
paulson@14641
   533
paulson@14641
   534
lemma floor_real_of_nat_zero [simp]: "floor (real (0::nat)) = 0"
paulson@14641
   535
by auto
paulson@14641
   536
huffman@24355
   537
lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
paulson@14641
   538
apply (simp only: floor_def)
paulson@14641
   539
apply (rule Least_equality)
huffman@23309
   540
apply (drule_tac [2] real_of_int_of_nat_eq [THEN ssubst])
paulson@14641
   541
apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
huffman@23309
   542
apply simp_all
paulson@14641
   543
done
paulson@14641
   544
huffman@24355
   545
lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
paulson@14641
   546
apply (simp only: floor_def)
paulson@14641
   547
apply (rule Least_equality)
huffman@23309
   548
apply (drule_tac [2] real_of_int_of_nat_eq [THEN ssubst])
avigad@16819
   549
apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
paulson@14641
   550
apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
huffman@23309
   551
apply simp_all
paulson@14641
   552
done
paulson@14641
   553
paulson@14641
   554
lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
paulson@14641
   555
apply (simp only: floor_def)
paulson@14641
   556
apply (rule Least_equality)
huffman@23309
   557
apply auto
paulson@14641
   558
done
paulson@14641
   559
paulson@14641
   560
lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
paulson@14641
   561
apply (simp only: floor_def)
paulson@14641
   562
apply (rule Least_equality)
avigad@16819
   563
apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
huffman@23309
   564
apply auto
paulson@14641
   565
done
paulson@14641
   566
paulson@14641
   567
lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
paulson@14641
   568
apply (case_tac "r < 0")
paulson@14641
   569
apply (blast intro: reals_Archimedean_6c_int)
paulson@14641
   570
apply (simp only: linorder_not_less)
paulson@14641
   571
apply (blast intro: reals_Archimedean_6b_int reals_Archimedean_6c_int)
paulson@14641
   572
done
paulson@14641
   573
paulson@14641
   574
lemma lemma_floor:
paulson@14641
   575
  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
paulson@14641
   576
  shows "m \<le> (n::int)"
paulson@14641
   577
proof -
wenzelm@23389
   578
  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
wenzelm@23389
   579
  also have "... = real (n + 1)" by simp
wenzelm@23389
   580
  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
paulson@14641
   581
  thus ?thesis by arith
paulson@14641
   582
qed
paulson@14641
   583
paulson@14641
   584
lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
paulson@14641
   585
apply (simp add: floor_def Least_def)
paulson@14641
   586
apply (insert real_lb_ub_int [of r], safe)
avigad@16819
   587
apply (rule theI2)
avigad@16819
   588
apply auto
paulson@14641
   589
done
paulson@14641
   590
avigad@16819
   591
lemma floor_mono: "x < y ==> floor x \<le> floor y"
paulson@14641
   592
apply (simp add: floor_def Least_def)
paulson@14641
   593
apply (insert real_lb_ub_int [of x])
paulson@14641
   594
apply (insert real_lb_ub_int [of y], safe)
paulson@14641
   595
apply (rule theI2)
avigad@16819
   596
apply (rule_tac [3] theI2)
avigad@16819
   597
apply simp
avigad@16819
   598
apply (erule conjI)
avigad@16819
   599
apply (auto simp add: order_eq_iff int_le_real_less)
paulson@14641
   600
done
paulson@14641
   601
avigad@16819
   602
lemma floor_mono2: "x \<le> y ==> floor x \<le> floor y"
huffman@22998
   603
by (auto dest: order_le_imp_less_or_eq simp add: floor_mono)
paulson@14641
   604
paulson@14641
   605
lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
paulson@14641
   606
by (auto intro: lemma_floor)
paulson@14641
   607
paulson@14641
   608
lemma real_of_int_floor_cancel [simp]:
paulson@14641
   609
    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
paulson@14641
   610
apply (simp add: floor_def Least_def)
paulson@14641
   611
apply (insert real_lb_ub_int [of x], erule exE)
paulson@14641
   612
apply (rule theI2)
wenzelm@16893
   613
apply (auto intro: lemma_floor)
paulson@14641
   614
done
paulson@14641
   615
paulson@14641
   616
lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
paulson@14641
   617
apply (simp add: floor_def)
paulson@14641
   618
apply (rule Least_equality)
paulson@14641
   619
apply (auto intro: lemma_floor)
paulson@14641
   620
done
paulson@14641
   621
paulson@14641
   622
lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
paulson@14641
   623
apply (simp add: floor_def)
paulson@14641
   624
apply (rule Least_equality)
paulson@14641
   625
apply (auto intro: lemma_floor)
paulson@14641
   626
done
paulson@14641
   627
paulson@14641
   628
lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   629
apply (rule inj_int [THEN injD])
paulson@14641
   630
apply (simp add: real_of_nat_Suc)
nipkow@15539
   631
apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
paulson@14641
   632
done
paulson@14641
   633
paulson@14641
   634
lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   635
apply (drule order_le_imp_less_or_eq)
paulson@14641
   636
apply (auto intro: floor_eq3)
paulson@14641
   637
done
paulson@14641
   638
paulson@14641
   639
lemma floor_number_of_eq [simp]:
paulson@14641
   640
     "floor(number_of n :: real) = (number_of n :: int)"
paulson@14641
   641
apply (subst real_number_of [symmetric])
paulson@14641
   642
apply (rule floor_real_of_int)
paulson@14641
   643
done
paulson@14641
   644
avigad@16819
   645
lemma floor_one [simp]: "floor 1 = 1"
avigad@16819
   646
  apply (rule trans)
avigad@16819
   647
  prefer 2
avigad@16819
   648
  apply (rule floor_real_of_int)
avigad@16819
   649
  apply simp
avigad@16819
   650
done
avigad@16819
   651
paulson@14641
   652
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
paulson@14641
   653
apply (simp add: floor_def Least_def)
paulson@14641
   654
apply (insert real_lb_ub_int [of r], safe)
paulson@14641
   655
apply (rule theI2)
paulson@14641
   656
apply (auto intro: lemma_floor)
avigad@16819
   657
done
avigad@16819
   658
avigad@16819
   659
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
avigad@16819
   660
apply (simp add: floor_def Least_def)
avigad@16819
   661
apply (insert real_lb_ub_int [of r], safe)
avigad@16819
   662
apply (rule theI2)
avigad@16819
   663
apply (auto intro: lemma_floor)
paulson@14641
   664
done
paulson@14641
   665
paulson@14641
   666
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
paulson@14641
   667
apply (insert real_of_int_floor_ge_diff_one [of r])
paulson@14641
   668
apply (auto simp del: real_of_int_floor_ge_diff_one)
paulson@14641
   669
done
paulson@14641
   670
avigad@16819
   671
lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
avigad@16819
   672
apply (insert real_of_int_floor_gt_diff_one [of r])
avigad@16819
   673
apply (auto simp del: real_of_int_floor_gt_diff_one)
avigad@16819
   674
done
paulson@14641
   675
avigad@16819
   676
lemma le_floor: "real a <= x ==> a <= floor x"
avigad@16819
   677
  apply (subgoal_tac "a < floor x + 1")
avigad@16819
   678
  apply arith
avigad@16819
   679
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   680
  apply simp
wenzelm@16893
   681
  apply (insert real_of_int_floor_add_one_gt [of x])
avigad@16819
   682
  apply arith
avigad@16819
   683
done
avigad@16819
   684
avigad@16819
   685
lemma real_le_floor: "a <= floor x ==> real a <= x"
avigad@16819
   686
  apply (rule order_trans)
avigad@16819
   687
  prefer 2
avigad@16819
   688
  apply (rule real_of_int_floor_le)
avigad@16819
   689
  apply (subst real_of_int_le_iff)
avigad@16819
   690
  apply assumption
avigad@16819
   691
done
avigad@16819
   692
avigad@16819
   693
lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
avigad@16819
   694
  apply (rule iffI)
avigad@16819
   695
  apply (erule real_le_floor)
avigad@16819
   696
  apply (erule le_floor)
avigad@16819
   697
done
avigad@16819
   698
wenzelm@16893
   699
lemma le_floor_eq_number_of [simp]:
avigad@16819
   700
    "(number_of n <= floor x) = (number_of n <= x)"
avigad@16819
   701
by (simp add: le_floor_eq)
avigad@16819
   702
avigad@16819
   703
lemma le_floor_eq_zero [simp]: "(0 <= floor x) = (0 <= x)"
avigad@16819
   704
by (simp add: le_floor_eq)
avigad@16819
   705
avigad@16819
   706
lemma le_floor_eq_one [simp]: "(1 <= floor x) = (1 <= x)"
avigad@16819
   707
by (simp add: le_floor_eq)
avigad@16819
   708
avigad@16819
   709
lemma floor_less_eq: "(floor x < a) = (x < real a)"
avigad@16819
   710
  apply (subst linorder_not_le [THEN sym])+
avigad@16819
   711
  apply simp
avigad@16819
   712
  apply (rule le_floor_eq)
avigad@16819
   713
done
avigad@16819
   714
wenzelm@16893
   715
lemma floor_less_eq_number_of [simp]:
avigad@16819
   716
    "(floor x < number_of n) = (x < number_of n)"
avigad@16819
   717
by (simp add: floor_less_eq)
avigad@16819
   718
avigad@16819
   719
lemma floor_less_eq_zero [simp]: "(floor x < 0) = (x < 0)"
avigad@16819
   720
by (simp add: floor_less_eq)
avigad@16819
   721
avigad@16819
   722
lemma floor_less_eq_one [simp]: "(floor x < 1) = (x < 1)"
avigad@16819
   723
by (simp add: floor_less_eq)
avigad@16819
   724
avigad@16819
   725
lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
avigad@16819
   726
  apply (insert le_floor_eq [of "a + 1" x])
avigad@16819
   727
  apply auto
avigad@16819
   728
done
avigad@16819
   729
wenzelm@16893
   730
lemma less_floor_eq_number_of [simp]:
avigad@16819
   731
    "(number_of n < floor x) = (number_of n + 1 <= x)"
avigad@16819
   732
by (simp add: less_floor_eq)
avigad@16819
   733
avigad@16819
   734
lemma less_floor_eq_zero [simp]: "(0 < floor x) = (1 <= x)"
avigad@16819
   735
by (simp add: less_floor_eq)
avigad@16819
   736
avigad@16819
   737
lemma less_floor_eq_one [simp]: "(1 < floor x) = (2 <= x)"
avigad@16819
   738
by (simp add: less_floor_eq)
avigad@16819
   739
avigad@16819
   740
lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
avigad@16819
   741
  apply (insert floor_less_eq [of x "a + 1"])
avigad@16819
   742
  apply auto
avigad@16819
   743
done
avigad@16819
   744
wenzelm@16893
   745
lemma floor_le_eq_number_of [simp]:
avigad@16819
   746
    "(floor x <= number_of n) = (x < number_of n + 1)"
avigad@16819
   747
by (simp add: floor_le_eq)
avigad@16819
   748
avigad@16819
   749
lemma floor_le_eq_zero [simp]: "(floor x <= 0) = (x < 1)"
avigad@16819
   750
by (simp add: floor_le_eq)
avigad@16819
   751
avigad@16819
   752
lemma floor_le_eq_one [simp]: "(floor x <= 1) = (x < 2)"
avigad@16819
   753
by (simp add: floor_le_eq)
avigad@16819
   754
avigad@16819
   755
lemma floor_add [simp]: "floor (x + real a) = floor x + a"
avigad@16819
   756
  apply (subst order_eq_iff)
avigad@16819
   757
  apply (rule conjI)
avigad@16819
   758
  prefer 2
avigad@16819
   759
  apply (subgoal_tac "floor x + a < floor (x + real a) + 1")
avigad@16819
   760
  apply arith
avigad@16819
   761
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   762
  apply simp
avigad@16819
   763
  apply (subgoal_tac "x + real a < real(floor(x + real a)) + 1")
avigad@16819
   764
  apply (subgoal_tac "real (floor x) <= x")
avigad@16819
   765
  apply arith
avigad@16819
   766
  apply (rule real_of_int_floor_le)
avigad@16819
   767
  apply (rule real_of_int_floor_add_one_gt)
avigad@16819
   768
  apply (subgoal_tac "floor (x + real a) < floor x + a + 1")
avigad@16819
   769
  apply arith
wenzelm@16893
   770
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   771
  apply simp
wenzelm@16893
   772
  apply (subgoal_tac "real(floor(x + real a)) <= x + real a")
avigad@16819
   773
  apply (subgoal_tac "x < real(floor x) + 1")
avigad@16819
   774
  apply arith
avigad@16819
   775
  apply (rule real_of_int_floor_add_one_gt)
avigad@16819
   776
  apply (rule real_of_int_floor_le)
avigad@16819
   777
done
avigad@16819
   778
wenzelm@16893
   779
lemma floor_add_number_of [simp]:
avigad@16819
   780
    "floor (x + number_of n) = floor x + number_of n"
avigad@16819
   781
  apply (subst floor_add [THEN sym])
avigad@16819
   782
  apply simp
avigad@16819
   783
done
avigad@16819
   784
avigad@16819
   785
lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
avigad@16819
   786
  apply (subst floor_add [THEN sym])
avigad@16819
   787
  apply simp
avigad@16819
   788
done
avigad@16819
   789
avigad@16819
   790
lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
avigad@16819
   791
  apply (subst diff_minus)+
avigad@16819
   792
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   793
  apply (rule floor_add)
avigad@16819
   794
done
avigad@16819
   795
wenzelm@16893
   796
lemma floor_subtract_number_of [simp]: "floor (x - number_of n) =
avigad@16819
   797
    floor x - number_of n"
avigad@16819
   798
  apply (subst floor_subtract [THEN sym])
avigad@16819
   799
  apply simp
avigad@16819
   800
done
avigad@16819
   801
avigad@16819
   802
lemma floor_subtract_one [simp]: "floor (x - 1) = floor x - 1"
avigad@16819
   803
  apply (subst floor_subtract [THEN sym])
avigad@16819
   804
  apply simp
avigad@16819
   805
done
paulson@14641
   806
paulson@14641
   807
lemma ceiling_zero [simp]: "ceiling 0 = 0"
paulson@14641
   808
by (simp add: ceiling_def)
paulson@14641
   809
huffman@24355
   810
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
paulson@14641
   811
by (simp add: ceiling_def)
paulson@14641
   812
paulson@14641
   813
lemma ceiling_real_of_nat_zero [simp]: "ceiling (real (0::nat)) = 0"
paulson@14641
   814
by auto
paulson@14641
   815
paulson@14641
   816
lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"
paulson@14641
   817
by (simp add: ceiling_def)
paulson@14641
   818
paulson@14641
   819
lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"
paulson@14641
   820
by (simp add: ceiling_def)
paulson@14641
   821
paulson@14641
   822
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
paulson@14641
   823
apply (simp add: ceiling_def)
paulson@14641
   824
apply (subst le_minus_iff, simp)
paulson@14641
   825
done
paulson@14641
   826
avigad@16819
   827
lemma ceiling_mono: "x < y ==> ceiling x \<le> ceiling y"
avigad@16819
   828
by (simp add: floor_mono ceiling_def)
paulson@14641
   829
avigad@16819
   830
lemma ceiling_mono2: "x \<le> y ==> ceiling x \<le> ceiling y"
avigad@16819
   831
by (simp add: floor_mono2 ceiling_def)
paulson@14641
   832
paulson@14641
   833
lemma real_of_int_ceiling_cancel [simp]:
paulson@14641
   834
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
paulson@14641
   835
apply (auto simp add: ceiling_def)
paulson@14641
   836
apply (drule arg_cong [where f = uminus], auto)
paulson@14641
   837
apply (rule_tac x = "-n" in exI, auto)
paulson@14641
   838
done
paulson@14641
   839
paulson@14641
   840
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
paulson@14641
   841
apply (simp add: ceiling_def)
paulson@14641
   842
apply (rule minus_equation_iff [THEN iffD1])
paulson@14641
   843
apply (simp add: floor_eq [where n = "-(n+1)"])
paulson@14641
   844
done
paulson@14641
   845
paulson@14641
   846
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
paulson@14641
   847
by (simp add: ceiling_def floor_eq2 [where n = "-(n+1)"])
paulson@14641
   848
paulson@14641
   849
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
paulson@14641
   850
by (simp add: ceiling_def floor_eq2 [where n = "-n"])
paulson@14641
   851
paulson@14641
   852
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
paulson@14641
   853
by (simp add: ceiling_def)
paulson@14641
   854
paulson@14641
   855
lemma ceiling_number_of_eq [simp]:
paulson@14641
   856
     "ceiling (number_of n :: real) = (number_of n)"
paulson@14641
   857
apply (subst real_number_of [symmetric])
paulson@14641
   858
apply (rule ceiling_real_of_int)
paulson@14641
   859
done
paulson@14641
   860
avigad@16819
   861
lemma ceiling_one [simp]: "ceiling 1 = 1"
avigad@16819
   862
  by (unfold ceiling_def, simp)
avigad@16819
   863
paulson@14641
   864
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
paulson@14641
   865
apply (rule neg_le_iff_le [THEN iffD1])
paulson@14641
   866
apply (simp add: ceiling_def diff_minus)
paulson@14641
   867
done
paulson@14641
   868
paulson@14641
   869
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
paulson@14641
   870
apply (insert real_of_int_ceiling_diff_one_le [of r])
paulson@14641
   871
apply (simp del: real_of_int_ceiling_diff_one_le)
paulson@14641
   872
done
paulson@14641
   873
avigad@16819
   874
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
avigad@16819
   875
  apply (unfold ceiling_def)
avigad@16819
   876
  apply (subgoal_tac "-a <= floor(- x)")
avigad@16819
   877
  apply simp
avigad@16819
   878
  apply (rule le_floor)
avigad@16819
   879
  apply simp
avigad@16819
   880
done
avigad@16819
   881
avigad@16819
   882
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
avigad@16819
   883
  apply (unfold ceiling_def)
avigad@16819
   884
  apply (subgoal_tac "real(- a) <= - x")
avigad@16819
   885
  apply simp
avigad@16819
   886
  apply (rule real_le_floor)
avigad@16819
   887
  apply simp
avigad@16819
   888
done
avigad@16819
   889
avigad@16819
   890
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
avigad@16819
   891
  apply (rule iffI)
avigad@16819
   892
  apply (erule ceiling_le_real)
avigad@16819
   893
  apply (erule ceiling_le)
avigad@16819
   894
done
avigad@16819
   895
wenzelm@16893
   896
lemma ceiling_le_eq_number_of [simp]:
avigad@16819
   897
    "(ceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
   898
by (simp add: ceiling_le_eq)
avigad@16819
   899
wenzelm@16893
   900
lemma ceiling_le_zero_eq [simp]: "(ceiling x <= 0) = (x <= 0)"
avigad@16819
   901
by (simp add: ceiling_le_eq)
avigad@16819
   902
wenzelm@16893
   903
lemma ceiling_le_eq_one [simp]: "(ceiling x <= 1) = (x <= 1)"
avigad@16819
   904
by (simp add: ceiling_le_eq)
avigad@16819
   905
avigad@16819
   906
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
avigad@16819
   907
  apply (subst linorder_not_le [THEN sym])+
avigad@16819
   908
  apply simp
avigad@16819
   909
  apply (rule ceiling_le_eq)
avigad@16819
   910
done
avigad@16819
   911
wenzelm@16893
   912
lemma less_ceiling_eq_number_of [simp]:
avigad@16819
   913
    "(number_of n < ceiling x) = (number_of n < x)"
avigad@16819
   914
by (simp add: less_ceiling_eq)
avigad@16819
   915
avigad@16819
   916
lemma less_ceiling_eq_zero [simp]: "(0 < ceiling x) = (0 < x)"
avigad@16819
   917
by (simp add: less_ceiling_eq)
avigad@16819
   918
avigad@16819
   919
lemma less_ceiling_eq_one [simp]: "(1 < ceiling x) = (1 < x)"
avigad@16819
   920
by (simp add: less_ceiling_eq)
avigad@16819
   921
avigad@16819
   922
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
avigad@16819
   923
  apply (insert ceiling_le_eq [of x "a - 1"])
avigad@16819
   924
  apply auto
avigad@16819
   925
done
avigad@16819
   926
wenzelm@16893
   927
lemma ceiling_less_eq_number_of [simp]:
avigad@16819
   928
    "(ceiling x < number_of n) = (x <= number_of n - 1)"
avigad@16819
   929
by (simp add: ceiling_less_eq)
avigad@16819
   930
avigad@16819
   931
lemma ceiling_less_eq_zero [simp]: "(ceiling x < 0) = (x <= -1)"
avigad@16819
   932
by (simp add: ceiling_less_eq)
avigad@16819
   933
avigad@16819
   934
lemma ceiling_less_eq_one [simp]: "(ceiling x < 1) = (x <= 0)"
avigad@16819
   935
by (simp add: ceiling_less_eq)
avigad@16819
   936
avigad@16819
   937
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
avigad@16819
   938
  apply (insert less_ceiling_eq [of "a - 1" x])
avigad@16819
   939
  apply auto
avigad@16819
   940
done
avigad@16819
   941
wenzelm@16893
   942
lemma le_ceiling_eq_number_of [simp]:
avigad@16819
   943
    "(number_of n <= ceiling x) = (number_of n - 1 < x)"
avigad@16819
   944
by (simp add: le_ceiling_eq)
avigad@16819
   945
avigad@16819
   946
lemma le_ceiling_eq_zero [simp]: "(0 <= ceiling x) = (-1 < x)"
avigad@16819
   947
by (simp add: le_ceiling_eq)
avigad@16819
   948
avigad@16819
   949
lemma le_ceiling_eq_one [simp]: "(1 <= ceiling x) = (0 < x)"
avigad@16819
   950
by (simp add: le_ceiling_eq)
avigad@16819
   951
avigad@16819
   952
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
avigad@16819
   953
  apply (unfold ceiling_def, simp)
avigad@16819
   954
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   955
  apply (subst floor_add)
avigad@16819
   956
  apply simp
avigad@16819
   957
done
avigad@16819
   958
wenzelm@16893
   959
lemma ceiling_add_number_of [simp]: "ceiling (x + number_of n) =
avigad@16819
   960
    ceiling x + number_of n"
avigad@16819
   961
  apply (subst ceiling_add [THEN sym])
avigad@16819
   962
  apply simp
avigad@16819
   963
done
avigad@16819
   964
avigad@16819
   965
lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
avigad@16819
   966
  apply (subst ceiling_add [THEN sym])
avigad@16819
   967
  apply simp
avigad@16819
   968
done
avigad@16819
   969
avigad@16819
   970
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
avigad@16819
   971
  apply (subst diff_minus)+
avigad@16819
   972
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   973
  apply (rule ceiling_add)
avigad@16819
   974
done
avigad@16819
   975
wenzelm@16893
   976
lemma ceiling_subtract_number_of [simp]: "ceiling (x - number_of n) =
avigad@16819
   977
    ceiling x - number_of n"
avigad@16819
   978
  apply (subst ceiling_subtract [THEN sym])
avigad@16819
   979
  apply simp
avigad@16819
   980
done
avigad@16819
   981
avigad@16819
   982
lemma ceiling_subtract_one [simp]: "ceiling (x - 1) = ceiling x - 1"
avigad@16819
   983
  apply (subst ceiling_subtract [THEN sym])
avigad@16819
   984
  apply simp
avigad@16819
   985
done
avigad@16819
   986
avigad@16819
   987
subsection {* Versions for the natural numbers *}
avigad@16819
   988
wenzelm@19765
   989
definition
wenzelm@21404
   990
  natfloor :: "real => nat" where
wenzelm@19765
   991
  "natfloor x = nat(floor x)"
wenzelm@21404
   992
wenzelm@21404
   993
definition
wenzelm@21404
   994
  natceiling :: "real => nat" where
wenzelm@19765
   995
  "natceiling x = nat(ceiling x)"
avigad@16819
   996
avigad@16819
   997
lemma natfloor_zero [simp]: "natfloor 0 = 0"
avigad@16819
   998
  by (unfold natfloor_def, simp)
avigad@16819
   999
avigad@16819
  1000
lemma natfloor_one [simp]: "natfloor 1 = 1"
avigad@16819
  1001
  by (unfold natfloor_def, simp)
avigad@16819
  1002
avigad@16819
  1003
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
avigad@16819
  1004
  by (unfold natfloor_def, simp)
avigad@16819
  1005
avigad@16819
  1006
lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
avigad@16819
  1007
  by (unfold natfloor_def, simp)
avigad@16819
  1008
avigad@16819
  1009
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
avigad@16819
  1010
  by (unfold natfloor_def, simp)
avigad@16819
  1011
avigad@16819
  1012
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
avigad@16819
  1013
  by (unfold natfloor_def, simp)
avigad@16819
  1014
avigad@16819
  1015
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
avigad@16819
  1016
  apply (unfold natfloor_def)
avigad@16819
  1017
  apply (subgoal_tac "floor x <= floor 0")
avigad@16819
  1018
  apply simp
avigad@16819
  1019
  apply (erule floor_mono2)
avigad@16819
  1020
done
avigad@16819
  1021
avigad@16819
  1022
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
avigad@16819
  1023
  apply (case_tac "0 <= x")
avigad@16819
  1024
  apply (subst natfloor_def)+
avigad@16819
  1025
  apply (subst nat_le_eq_zle)
avigad@16819
  1026
  apply force
wenzelm@16893
  1027
  apply (erule floor_mono2)
avigad@16819
  1028
  apply (subst natfloor_neg)
avigad@16819
  1029
  apply simp
avigad@16819
  1030
  apply simp
avigad@16819
  1031
done
avigad@16819
  1032
avigad@16819
  1033
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
avigad@16819
  1034
  apply (unfold natfloor_def)
avigad@16819
  1035
  apply (subst nat_int [THEN sym])
avigad@16819
  1036
  apply (subst nat_le_eq_zle)
avigad@16819
  1037
  apply simp
avigad@16819
  1038
  apply (rule le_floor)
avigad@16819
  1039
  apply simp
avigad@16819
  1040
done
avigad@16819
  1041
avigad@16819
  1042
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
avigad@16819
  1043
  apply (rule iffI)
avigad@16819
  1044
  apply (rule order_trans)
avigad@16819
  1045
  prefer 2
avigad@16819
  1046
  apply (erule real_natfloor_le)
avigad@16819
  1047
  apply (subst real_of_nat_le_iff)
avigad@16819
  1048
  apply assumption
avigad@16819
  1049
  apply (erule le_natfloor)
avigad@16819
  1050
done
avigad@16819
  1051
wenzelm@16893
  1052
lemma le_natfloor_eq_number_of [simp]:
avigad@16819
  1053
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16819
  1054
      (number_of n <= natfloor x) = (number_of n <= x)"
avigad@16819
  1055
  apply (subst le_natfloor_eq, assumption)
avigad@16819
  1056
  apply simp
avigad@16819
  1057
done
avigad@16819
  1058
avigad@16820
  1059
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
avigad@16819
  1060
  apply (case_tac "0 <= x")
avigad@16819
  1061
  apply (subst le_natfloor_eq, assumption, simp)
avigad@16819
  1062
  apply (rule iffI)
wenzelm@16893
  1063
  apply (subgoal_tac "natfloor x <= natfloor 0")
avigad@16819
  1064
  apply simp
avigad@16819
  1065
  apply (rule natfloor_mono)
avigad@16819
  1066
  apply simp
avigad@16819
  1067
  apply simp
avigad@16819
  1068
done
avigad@16819
  1069
avigad@16819
  1070
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
avigad@16819
  1071
  apply (unfold natfloor_def)
avigad@16819
  1072
  apply (subst nat_int [THEN sym]);back;
avigad@16819
  1073
  apply (subst eq_nat_nat_iff)
avigad@16819
  1074
  apply simp
avigad@16819
  1075
  apply simp
avigad@16819
  1076
  apply (rule floor_eq2)
avigad@16819
  1077
  apply auto
avigad@16819
  1078
done
avigad@16819
  1079
avigad@16819
  1080
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
avigad@16819
  1081
  apply (case_tac "0 <= x")
avigad@16819
  1082
  apply (unfold natfloor_def)
avigad@16819
  1083
  apply simp
avigad@16819
  1084
  apply simp_all
avigad@16819
  1085
done
avigad@16819
  1086
avigad@16819
  1087
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
avigad@16819
  1088
  apply (simp add: compare_rls)
avigad@16819
  1089
  apply (rule real_natfloor_add_one_gt)
avigad@16819
  1090
done
avigad@16819
  1091
avigad@16819
  1092
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
avigad@16819
  1093
  apply (subgoal_tac "z < real(natfloor z) + 1")
avigad@16819
  1094
  apply arith
avigad@16819
  1095
  apply (rule real_natfloor_add_one_gt)
avigad@16819
  1096
done
avigad@16819
  1097
avigad@16819
  1098
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
avigad@16819
  1099
  apply (unfold natfloor_def)
huffman@24355
  1100
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1101
  apply (erule ssubst)
huffman@23309
  1102
  apply (simp add: nat_add_distrib del: real_of_int_of_nat_eq)
avigad@16819
  1103
  apply simp
avigad@16819
  1104
done
avigad@16819
  1105
wenzelm@16893
  1106
lemma natfloor_add_number_of [simp]:
wenzelm@16893
  1107
    "~neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16819
  1108
      natfloor (x + number_of n) = natfloor x + number_of n"
avigad@16819
  1109
  apply (subst natfloor_add [THEN sym])
avigad@16819
  1110
  apply simp_all
avigad@16819
  1111
done
avigad@16819
  1112
avigad@16819
  1113
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
avigad@16819
  1114
  apply (subst natfloor_add [THEN sym])
avigad@16819
  1115
  apply assumption
avigad@16819
  1116
  apply simp
avigad@16819
  1117
done
avigad@16819
  1118
wenzelm@16893
  1119
lemma natfloor_subtract [simp]: "real a <= x ==>
avigad@16819
  1120
    natfloor(x - real a) = natfloor x - a"
avigad@16819
  1121
  apply (unfold natfloor_def)
huffman@24355
  1122
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1123
  apply (erule ssubst)
huffman@23309
  1124
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1125
  apply simp
avigad@16819
  1126
done
avigad@16819
  1127
avigad@16819
  1128
lemma natceiling_zero [simp]: "natceiling 0 = 0"
avigad@16819
  1129
  by (unfold natceiling_def, simp)
avigad@16819
  1130
avigad@16819
  1131
lemma natceiling_one [simp]: "natceiling 1 = 1"
avigad@16819
  1132
  by (unfold natceiling_def, simp)
avigad@16819
  1133
avigad@16819
  1134
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
avigad@16819
  1135
  by (unfold natceiling_def, simp)
avigad@16819
  1136
avigad@16819
  1137
lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
avigad@16819
  1138
  by (unfold natceiling_def, simp)
avigad@16819
  1139
avigad@16819
  1140
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
avigad@16819
  1141
  by (unfold natceiling_def, simp)
avigad@16819
  1142
avigad@16819
  1143
lemma real_natceiling_ge: "x <= real(natceiling x)"
avigad@16819
  1144
  apply (unfold natceiling_def)
avigad@16819
  1145
  apply (case_tac "x < 0")
avigad@16819
  1146
  apply simp
avigad@16819
  1147
  apply (subst real_nat_eq_real)
avigad@16819
  1148
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
  1149
  apply simp
avigad@16819
  1150
  apply (rule ceiling_mono2)
avigad@16819
  1151
  apply simp
avigad@16819
  1152
  apply simp
avigad@16819
  1153
done
avigad@16819
  1154
avigad@16819
  1155
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
avigad@16819
  1156
  apply (unfold natceiling_def)
avigad@16819
  1157
  apply simp
avigad@16819
  1158
done
avigad@16819
  1159
avigad@16819
  1160
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
avigad@16819
  1161
  apply (case_tac "0 <= x")
avigad@16819
  1162
  apply (subst natceiling_def)+
avigad@16819
  1163
  apply (subst nat_le_eq_zle)
avigad@16819
  1164
  apply (rule disjI2)
avigad@16819
  1165
  apply (subgoal_tac "real (0::int) <= real(ceiling y)")
avigad@16819
  1166
  apply simp
avigad@16819
  1167
  apply (rule order_trans)
avigad@16819
  1168
  apply simp
avigad@16819
  1169
  apply (erule order_trans)
avigad@16819
  1170
  apply simp
avigad@16819
  1171
  apply (erule ceiling_mono2)
avigad@16819
  1172
  apply (subst natceiling_neg)
avigad@16819
  1173
  apply simp_all
avigad@16819
  1174
done
avigad@16819
  1175
avigad@16819
  1176
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
avigad@16819
  1177
  apply (unfold natceiling_def)
avigad@16819
  1178
  apply (case_tac "x < 0")
avigad@16819
  1179
  apply simp
avigad@16819
  1180
  apply (subst nat_int [THEN sym]);back;
avigad@16819
  1181
  apply (subst nat_le_eq_zle)
avigad@16819
  1182
  apply simp
avigad@16819
  1183
  apply (rule ceiling_le)
avigad@16819
  1184
  apply simp
avigad@16819
  1185
done
avigad@16819
  1186
avigad@16819
  1187
lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"
avigad@16819
  1188
  apply (rule iffI)
avigad@16819
  1189
  apply (rule order_trans)
avigad@16819
  1190
  apply (rule real_natceiling_ge)
avigad@16819
  1191
  apply (subst real_of_nat_le_iff)
avigad@16819
  1192
  apply assumption
avigad@16819
  1193
  apply (erule natceiling_le)
avigad@16819
  1194
done
avigad@16819
  1195
wenzelm@16893
  1196
lemma natceiling_le_eq_number_of [simp]:
avigad@16820
  1197
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16820
  1198
      (natceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
  1199
  apply (subst natceiling_le_eq, assumption)
avigad@16819
  1200
  apply simp
avigad@16819
  1201
done
avigad@16819
  1202
avigad@16820
  1203
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
avigad@16819
  1204
  apply (case_tac "0 <= x")
avigad@16819
  1205
  apply (subst natceiling_le_eq)
avigad@16819
  1206
  apply assumption
avigad@16819
  1207
  apply simp
avigad@16819
  1208
  apply (subst natceiling_neg)
avigad@16819
  1209
  apply simp
avigad@16819
  1210
  apply simp
avigad@16819
  1211
done
avigad@16819
  1212
avigad@16819
  1213
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
avigad@16819
  1214
  apply (unfold natceiling_def)
wenzelm@19850
  1215
  apply (simplesubst nat_int [THEN sym]) back back
avigad@16819
  1216
  apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")
avigad@16819
  1217
  apply (erule ssubst)
avigad@16819
  1218
  apply (subst eq_nat_nat_iff)
avigad@16819
  1219
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
  1220
  apply simp
avigad@16819
  1221
  apply (rule ceiling_mono2)
avigad@16819
  1222
  apply force
avigad@16819
  1223
  apply force
avigad@16819
  1224
  apply (rule ceiling_eq2)
avigad@16819
  1225
  apply (simp, simp)
avigad@16819
  1226
  apply (subst nat_add_distrib)
avigad@16819
  1227
  apply auto
avigad@16819
  1228
done
avigad@16819
  1229
wenzelm@16893
  1230
lemma natceiling_add [simp]: "0 <= x ==>
avigad@16819
  1231
    natceiling (x + real a) = natceiling x + a"
avigad@16819
  1232
  apply (unfold natceiling_def)
huffman@24355
  1233
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1234
  apply (erule ssubst)
huffman@23309
  1235
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1236
  apply (subst nat_add_distrib)
avigad@16819
  1237
  apply (subgoal_tac "0 = ceiling 0")
avigad@16819
  1238
  apply (erule ssubst)
avigad@16819
  1239
  apply (erule ceiling_mono2)
avigad@16819
  1240
  apply simp_all
avigad@16819
  1241
done
avigad@16819
  1242
wenzelm@16893
  1243
lemma natceiling_add_number_of [simp]:
wenzelm@16893
  1244
    "~ neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16820
  1245
      natceiling (x + number_of n) = natceiling x + number_of n"
avigad@16819
  1246
  apply (subst natceiling_add [THEN sym])
avigad@16819
  1247
  apply simp_all
avigad@16819
  1248
done
avigad@16819
  1249
avigad@16819
  1250
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
avigad@16819
  1251
  apply (subst natceiling_add [THEN sym])
avigad@16819
  1252
  apply assumption
avigad@16819
  1253
  apply simp
avigad@16819
  1254
done
avigad@16819
  1255
wenzelm@16893
  1256
lemma natceiling_subtract [simp]: "real a <= x ==>
avigad@16819
  1257
    natceiling(x - real a) = natceiling x - a"
avigad@16819
  1258
  apply (unfold natceiling_def)
huffman@24355
  1259
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1260
  apply (erule ssubst)
huffman@23309
  1261
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1262
  apply simp
avigad@16819
  1263
done
avigad@16819
  1264
nipkow@25162
  1265
lemma natfloor_div_nat: "1 <= x ==> y > 0 ==>
avigad@16819
  1266
  natfloor (x / real y) = natfloor x div y"
avigad@16819
  1267
proof -
nipkow@25162
  1268
  assume "1 <= (x::real)" and "(y::nat) > 0"
avigad@16819
  1269
  have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"
avigad@16819
  1270
    by simp
wenzelm@16893
  1271
  then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +
avigad@16819
  1272
    real((natfloor x) mod y)"
avigad@16819
  1273
    by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])
avigad@16819
  1274
  have "x = real(natfloor x) + (x - real(natfloor x))"
avigad@16819
  1275
    by simp
wenzelm@16893
  1276
  then have "x = real ((natfloor x) div y) * real y +
avigad@16819
  1277
      real((natfloor x) mod y) + (x - real(natfloor x))"
avigad@16819
  1278
    by (simp add: a)
avigad@16819
  1279
  then have "x / real y = ... / real y"
avigad@16819
  1280
    by simp
wenzelm@16893
  1281
  also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /
avigad@16819
  1282
    real y + (x - real(natfloor x)) / real y"
nipkow@23477
  1283
    by (auto simp add: ring_simps add_divide_distrib
avigad@16819
  1284
      diff_divide_distrib prems)
avigad@16819
  1285
  finally have "natfloor (x / real y) = natfloor(...)" by simp
wenzelm@16893
  1286
  also have "... = natfloor(real((natfloor x) mod y) /
avigad@16819
  1287
    real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"
avigad@16819
  1288
    by (simp add: add_ac)
wenzelm@16893
  1289
  also have "... = natfloor(real((natfloor x) mod y) /
avigad@16819
  1290
    real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"
avigad@16819
  1291
    apply (rule natfloor_add)
avigad@16819
  1292
    apply (rule add_nonneg_nonneg)
avigad@16819
  1293
    apply (rule divide_nonneg_pos)
avigad@16819
  1294
    apply simp
avigad@16819
  1295
    apply (simp add: prems)
avigad@16819
  1296
    apply (rule divide_nonneg_pos)
avigad@16819
  1297
    apply (simp add: compare_rls)
avigad@16819
  1298
    apply (rule real_natfloor_le)
avigad@16819
  1299
    apply (insert prems, auto)
avigad@16819
  1300
    done
wenzelm@16893
  1301
  also have "natfloor(real((natfloor x) mod y) /
avigad@16819
  1302
    real y + (x - real(natfloor x)) / real y) = 0"
avigad@16819
  1303
    apply (rule natfloor_eq)
avigad@16819
  1304
    apply simp
avigad@16819
  1305
    apply (rule add_nonneg_nonneg)
avigad@16819
  1306
    apply (rule divide_nonneg_pos)
avigad@16819
  1307
    apply force
avigad@16819
  1308
    apply (force simp add: prems)
avigad@16819
  1309
    apply (rule divide_nonneg_pos)
avigad@16819
  1310
    apply (simp add: compare_rls)
avigad@16819
  1311
    apply (rule real_natfloor_le)
avigad@16819
  1312
    apply (auto simp add: prems)
avigad@16819
  1313
    apply (insert prems, arith)
avigad@16819
  1314
    apply (simp add: add_divide_distrib [THEN sym])
avigad@16819
  1315
    apply (subgoal_tac "real y = real y - 1 + 1")
avigad@16819
  1316
    apply (erule ssubst)
avigad@16819
  1317
    apply (rule add_le_less_mono)
avigad@16819
  1318
    apply (simp add: compare_rls)
wenzelm@16893
  1319
    apply (subgoal_tac "real(natfloor x mod y) + 1 =
avigad@16819
  1320
      real(natfloor x mod y + 1)")
avigad@16819
  1321
    apply (erule ssubst)
avigad@16819
  1322
    apply (subst real_of_nat_le_iff)
avigad@16819
  1323
    apply (subgoal_tac "natfloor x mod y < y")
avigad@16819
  1324
    apply arith
avigad@16819
  1325
    apply (rule mod_less_divisor)
avigad@16819
  1326
    apply auto
avigad@16819
  1327
    apply (simp add: compare_rls)
avigad@16819
  1328
    apply (subst add_commute)
avigad@16819
  1329
    apply (rule real_natfloor_add_one_gt)
avigad@16819
  1330
    done
nipkow@25140
  1331
  finally show ?thesis by simp
avigad@16819
  1332
qed
avigad@16819
  1333
paulson@14365
  1334
end