src/HOL/Real/RComplete.thy
author nipkow
Sun Oct 21 22:33:35 2007 +0200 (2007-10-21)
changeset 25140 273772abbea2
parent 24355 93d78fdeb55a
child 25162 ad4d5365d9d8
permissions -rw-r--r--
More changes from >0 to ~=0::nat
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
paulson@14641
   433
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
paulson@14641
   434
wenzelm@19765
   435
definition
wenzelm@21404
   436
  floor :: "real => int" where
wenzelm@19765
   437
  "floor r = (LEAST n::int. r < real (n+1))"
paulson@14641
   438
wenzelm@21404
   439
definition
wenzelm@21404
   440
  ceiling :: "real => int" where
wenzelm@19765
   441
  "ceiling r = - floor (- r)"
paulson@14641
   442
wenzelm@21210
   443
notation (xsymbols)
wenzelm@21404
   444
  floor  ("\<lfloor>_\<rfloor>") and
wenzelm@19765
   445
  ceiling  ("\<lceil>_\<rceil>")
paulson@14641
   446
wenzelm@21210
   447
notation (HTML output)
wenzelm@21404
   448
  floor  ("\<lfloor>_\<rfloor>") and
wenzelm@19765
   449
  ceiling  ("\<lceil>_\<rceil>")
paulson@14641
   450
paulson@14641
   451
paulson@14641
   452
lemma number_of_less_real_of_int_iff [simp]:
paulson@14641
   453
     "((number_of n) < real (m::int)) = (number_of n < m)"
paulson@14641
   454
apply auto
paulson@14641
   455
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   456
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   457
done
paulson@14641
   458
paulson@14641
   459
lemma number_of_less_real_of_int_iff2 [simp]:
paulson@14641
   460
     "(real (m::int) < (number_of n)) = (m < number_of n)"
paulson@14641
   461
apply auto
paulson@14641
   462
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   463
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   464
done
paulson@14641
   465
paulson@14641
   466
lemma number_of_le_real_of_int_iff [simp]:
paulson@14641
   467
     "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
paulson@14641
   468
by (simp add: linorder_not_less [symmetric])
paulson@14641
   469
paulson@14641
   470
lemma number_of_le_real_of_int_iff2 [simp]:
paulson@14641
   471
     "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"
paulson@14641
   472
by (simp add: linorder_not_less [symmetric])
paulson@14641
   473
paulson@14641
   474
lemma floor_zero [simp]: "floor 0 = 0"
avigad@16819
   475
apply (simp add: floor_def del: real_of_int_add)
avigad@16819
   476
apply (rule Least_equality)
avigad@16819
   477
apply simp_all
paulson@14641
   478
done
paulson@14641
   479
paulson@14641
   480
lemma floor_real_of_nat_zero [simp]: "floor (real (0::nat)) = 0"
paulson@14641
   481
by auto
paulson@14641
   482
huffman@24355
   483
lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
paulson@14641
   484
apply (simp only: floor_def)
paulson@14641
   485
apply (rule Least_equality)
huffman@23309
   486
apply (drule_tac [2] real_of_int_of_nat_eq [THEN ssubst])
paulson@14641
   487
apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
huffman@23309
   488
apply simp_all
paulson@14641
   489
done
paulson@14641
   490
huffman@24355
   491
lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
paulson@14641
   492
apply (simp only: floor_def)
paulson@14641
   493
apply (rule Least_equality)
huffman@23309
   494
apply (drule_tac [2] real_of_int_of_nat_eq [THEN ssubst])
avigad@16819
   495
apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
paulson@14641
   496
apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
huffman@23309
   497
apply simp_all
paulson@14641
   498
done
paulson@14641
   499
paulson@14641
   500
lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
paulson@14641
   501
apply (simp only: floor_def)
paulson@14641
   502
apply (rule Least_equality)
huffman@23309
   503
apply auto
paulson@14641
   504
done
paulson@14641
   505
paulson@14641
   506
lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
paulson@14641
   507
apply (simp only: floor_def)
paulson@14641
   508
apply (rule Least_equality)
avigad@16819
   509
apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
huffman@23309
   510
apply auto
paulson@14641
   511
done
paulson@14641
   512
paulson@14641
   513
lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
paulson@14641
   514
apply (case_tac "r < 0")
paulson@14641
   515
apply (blast intro: reals_Archimedean_6c_int)
paulson@14641
   516
apply (simp only: linorder_not_less)
paulson@14641
   517
apply (blast intro: reals_Archimedean_6b_int reals_Archimedean_6c_int)
paulson@14641
   518
done
paulson@14641
   519
paulson@14641
   520
lemma lemma_floor:
paulson@14641
   521
  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
paulson@14641
   522
  shows "m \<le> (n::int)"
paulson@14641
   523
proof -
wenzelm@23389
   524
  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
wenzelm@23389
   525
  also have "... = real (n + 1)" by simp
wenzelm@23389
   526
  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
paulson@14641
   527
  thus ?thesis by arith
paulson@14641
   528
qed
paulson@14641
   529
paulson@14641
   530
lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
paulson@14641
   531
apply (simp add: floor_def Least_def)
paulson@14641
   532
apply (insert real_lb_ub_int [of r], safe)
avigad@16819
   533
apply (rule theI2)
avigad@16819
   534
apply auto
paulson@14641
   535
done
paulson@14641
   536
avigad@16819
   537
lemma floor_mono: "x < y ==> floor x \<le> floor y"
paulson@14641
   538
apply (simp add: floor_def Least_def)
paulson@14641
   539
apply (insert real_lb_ub_int [of x])
paulson@14641
   540
apply (insert real_lb_ub_int [of y], safe)
paulson@14641
   541
apply (rule theI2)
avigad@16819
   542
apply (rule_tac [3] theI2)
avigad@16819
   543
apply simp
avigad@16819
   544
apply (erule conjI)
avigad@16819
   545
apply (auto simp add: order_eq_iff int_le_real_less)
paulson@14641
   546
done
paulson@14641
   547
avigad@16819
   548
lemma floor_mono2: "x \<le> y ==> floor x \<le> floor y"
huffman@22998
   549
by (auto dest: order_le_imp_less_or_eq simp add: floor_mono)
paulson@14641
   550
paulson@14641
   551
lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
paulson@14641
   552
by (auto intro: lemma_floor)
paulson@14641
   553
paulson@14641
   554
lemma real_of_int_floor_cancel [simp]:
paulson@14641
   555
    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
paulson@14641
   556
apply (simp add: floor_def Least_def)
paulson@14641
   557
apply (insert real_lb_ub_int [of x], erule exE)
paulson@14641
   558
apply (rule theI2)
wenzelm@16893
   559
apply (auto intro: lemma_floor)
paulson@14641
   560
done
paulson@14641
   561
paulson@14641
   562
lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
paulson@14641
   563
apply (simp add: floor_def)
paulson@14641
   564
apply (rule Least_equality)
paulson@14641
   565
apply (auto intro: lemma_floor)
paulson@14641
   566
done
paulson@14641
   567
paulson@14641
   568
lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
paulson@14641
   569
apply (simp add: floor_def)
paulson@14641
   570
apply (rule Least_equality)
paulson@14641
   571
apply (auto intro: lemma_floor)
paulson@14641
   572
done
paulson@14641
   573
paulson@14641
   574
lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   575
apply (rule inj_int [THEN injD])
paulson@14641
   576
apply (simp add: real_of_nat_Suc)
nipkow@15539
   577
apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
paulson@14641
   578
done
paulson@14641
   579
paulson@14641
   580
lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   581
apply (drule order_le_imp_less_or_eq)
paulson@14641
   582
apply (auto intro: floor_eq3)
paulson@14641
   583
done
paulson@14641
   584
paulson@14641
   585
lemma floor_number_of_eq [simp]:
paulson@14641
   586
     "floor(number_of n :: real) = (number_of n :: int)"
paulson@14641
   587
apply (subst real_number_of [symmetric])
paulson@14641
   588
apply (rule floor_real_of_int)
paulson@14641
   589
done
paulson@14641
   590
avigad@16819
   591
lemma floor_one [simp]: "floor 1 = 1"
avigad@16819
   592
  apply (rule trans)
avigad@16819
   593
  prefer 2
avigad@16819
   594
  apply (rule floor_real_of_int)
avigad@16819
   595
  apply simp
avigad@16819
   596
done
avigad@16819
   597
paulson@14641
   598
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
paulson@14641
   599
apply (simp add: floor_def Least_def)
paulson@14641
   600
apply (insert real_lb_ub_int [of r], safe)
paulson@14641
   601
apply (rule theI2)
paulson@14641
   602
apply (auto intro: lemma_floor)
avigad@16819
   603
done
avigad@16819
   604
avigad@16819
   605
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
avigad@16819
   606
apply (simp add: floor_def Least_def)
avigad@16819
   607
apply (insert real_lb_ub_int [of r], safe)
avigad@16819
   608
apply (rule theI2)
avigad@16819
   609
apply (auto intro: lemma_floor)
paulson@14641
   610
done
paulson@14641
   611
paulson@14641
   612
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
paulson@14641
   613
apply (insert real_of_int_floor_ge_diff_one [of r])
paulson@14641
   614
apply (auto simp del: real_of_int_floor_ge_diff_one)
paulson@14641
   615
done
paulson@14641
   616
avigad@16819
   617
lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
avigad@16819
   618
apply (insert real_of_int_floor_gt_diff_one [of r])
avigad@16819
   619
apply (auto simp del: real_of_int_floor_gt_diff_one)
avigad@16819
   620
done
paulson@14641
   621
avigad@16819
   622
lemma le_floor: "real a <= x ==> a <= floor x"
avigad@16819
   623
  apply (subgoal_tac "a < floor x + 1")
avigad@16819
   624
  apply arith
avigad@16819
   625
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   626
  apply simp
wenzelm@16893
   627
  apply (insert real_of_int_floor_add_one_gt [of x])
avigad@16819
   628
  apply arith
avigad@16819
   629
done
avigad@16819
   630
avigad@16819
   631
lemma real_le_floor: "a <= floor x ==> real a <= x"
avigad@16819
   632
  apply (rule order_trans)
avigad@16819
   633
  prefer 2
avigad@16819
   634
  apply (rule real_of_int_floor_le)
avigad@16819
   635
  apply (subst real_of_int_le_iff)
avigad@16819
   636
  apply assumption
avigad@16819
   637
done
avigad@16819
   638
avigad@16819
   639
lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
avigad@16819
   640
  apply (rule iffI)
avigad@16819
   641
  apply (erule real_le_floor)
avigad@16819
   642
  apply (erule le_floor)
avigad@16819
   643
done
avigad@16819
   644
wenzelm@16893
   645
lemma le_floor_eq_number_of [simp]:
avigad@16819
   646
    "(number_of n <= floor x) = (number_of n <= x)"
avigad@16819
   647
by (simp add: le_floor_eq)
avigad@16819
   648
avigad@16819
   649
lemma le_floor_eq_zero [simp]: "(0 <= floor x) = (0 <= x)"
avigad@16819
   650
by (simp add: le_floor_eq)
avigad@16819
   651
avigad@16819
   652
lemma le_floor_eq_one [simp]: "(1 <= floor x) = (1 <= x)"
avigad@16819
   653
by (simp add: le_floor_eq)
avigad@16819
   654
avigad@16819
   655
lemma floor_less_eq: "(floor x < a) = (x < real a)"
avigad@16819
   656
  apply (subst linorder_not_le [THEN sym])+
avigad@16819
   657
  apply simp
avigad@16819
   658
  apply (rule le_floor_eq)
avigad@16819
   659
done
avigad@16819
   660
wenzelm@16893
   661
lemma floor_less_eq_number_of [simp]:
avigad@16819
   662
    "(floor x < number_of n) = (x < number_of n)"
avigad@16819
   663
by (simp add: floor_less_eq)
avigad@16819
   664
avigad@16819
   665
lemma floor_less_eq_zero [simp]: "(floor x < 0) = (x < 0)"
avigad@16819
   666
by (simp add: floor_less_eq)
avigad@16819
   667
avigad@16819
   668
lemma floor_less_eq_one [simp]: "(floor x < 1) = (x < 1)"
avigad@16819
   669
by (simp add: floor_less_eq)
avigad@16819
   670
avigad@16819
   671
lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
avigad@16819
   672
  apply (insert le_floor_eq [of "a + 1" x])
avigad@16819
   673
  apply auto
avigad@16819
   674
done
avigad@16819
   675
wenzelm@16893
   676
lemma less_floor_eq_number_of [simp]:
avigad@16819
   677
    "(number_of n < floor x) = (number_of n + 1 <= x)"
avigad@16819
   678
by (simp add: less_floor_eq)
avigad@16819
   679
avigad@16819
   680
lemma less_floor_eq_zero [simp]: "(0 < floor x) = (1 <= x)"
avigad@16819
   681
by (simp add: less_floor_eq)
avigad@16819
   682
avigad@16819
   683
lemma less_floor_eq_one [simp]: "(1 < floor x) = (2 <= x)"
avigad@16819
   684
by (simp add: less_floor_eq)
avigad@16819
   685
avigad@16819
   686
lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
avigad@16819
   687
  apply (insert floor_less_eq [of x "a + 1"])
avigad@16819
   688
  apply auto
avigad@16819
   689
done
avigad@16819
   690
wenzelm@16893
   691
lemma floor_le_eq_number_of [simp]:
avigad@16819
   692
    "(floor x <= number_of n) = (x < number_of n + 1)"
avigad@16819
   693
by (simp add: floor_le_eq)
avigad@16819
   694
avigad@16819
   695
lemma floor_le_eq_zero [simp]: "(floor x <= 0) = (x < 1)"
avigad@16819
   696
by (simp add: floor_le_eq)
avigad@16819
   697
avigad@16819
   698
lemma floor_le_eq_one [simp]: "(floor x <= 1) = (x < 2)"
avigad@16819
   699
by (simp add: floor_le_eq)
avigad@16819
   700
avigad@16819
   701
lemma floor_add [simp]: "floor (x + real a) = floor x + a"
avigad@16819
   702
  apply (subst order_eq_iff)
avigad@16819
   703
  apply (rule conjI)
avigad@16819
   704
  prefer 2
avigad@16819
   705
  apply (subgoal_tac "floor x + a < floor (x + real a) + 1")
avigad@16819
   706
  apply arith
avigad@16819
   707
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   708
  apply simp
avigad@16819
   709
  apply (subgoal_tac "x + real a < real(floor(x + real a)) + 1")
avigad@16819
   710
  apply (subgoal_tac "real (floor x) <= x")
avigad@16819
   711
  apply arith
avigad@16819
   712
  apply (rule real_of_int_floor_le)
avigad@16819
   713
  apply (rule real_of_int_floor_add_one_gt)
avigad@16819
   714
  apply (subgoal_tac "floor (x + real a) < floor x + a + 1")
avigad@16819
   715
  apply arith
wenzelm@16893
   716
  apply (subst real_of_int_less_iff [THEN sym])
avigad@16819
   717
  apply simp
wenzelm@16893
   718
  apply (subgoal_tac "real(floor(x + real a)) <= x + real a")
avigad@16819
   719
  apply (subgoal_tac "x < real(floor x) + 1")
avigad@16819
   720
  apply arith
avigad@16819
   721
  apply (rule real_of_int_floor_add_one_gt)
avigad@16819
   722
  apply (rule real_of_int_floor_le)
avigad@16819
   723
done
avigad@16819
   724
wenzelm@16893
   725
lemma floor_add_number_of [simp]:
avigad@16819
   726
    "floor (x + number_of n) = floor x + number_of n"
avigad@16819
   727
  apply (subst floor_add [THEN sym])
avigad@16819
   728
  apply simp
avigad@16819
   729
done
avigad@16819
   730
avigad@16819
   731
lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
avigad@16819
   732
  apply (subst floor_add [THEN sym])
avigad@16819
   733
  apply simp
avigad@16819
   734
done
avigad@16819
   735
avigad@16819
   736
lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
avigad@16819
   737
  apply (subst diff_minus)+
avigad@16819
   738
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   739
  apply (rule floor_add)
avigad@16819
   740
done
avigad@16819
   741
wenzelm@16893
   742
lemma floor_subtract_number_of [simp]: "floor (x - number_of n) =
avigad@16819
   743
    floor x - number_of n"
avigad@16819
   744
  apply (subst floor_subtract [THEN sym])
avigad@16819
   745
  apply simp
avigad@16819
   746
done
avigad@16819
   747
avigad@16819
   748
lemma floor_subtract_one [simp]: "floor (x - 1) = floor x - 1"
avigad@16819
   749
  apply (subst floor_subtract [THEN sym])
avigad@16819
   750
  apply simp
avigad@16819
   751
done
paulson@14641
   752
paulson@14641
   753
lemma ceiling_zero [simp]: "ceiling 0 = 0"
paulson@14641
   754
by (simp add: ceiling_def)
paulson@14641
   755
huffman@24355
   756
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
paulson@14641
   757
by (simp add: ceiling_def)
paulson@14641
   758
paulson@14641
   759
lemma ceiling_real_of_nat_zero [simp]: "ceiling (real (0::nat)) = 0"
paulson@14641
   760
by auto
paulson@14641
   761
paulson@14641
   762
lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"
paulson@14641
   763
by (simp add: ceiling_def)
paulson@14641
   764
paulson@14641
   765
lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"
paulson@14641
   766
by (simp add: ceiling_def)
paulson@14641
   767
paulson@14641
   768
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
paulson@14641
   769
apply (simp add: ceiling_def)
paulson@14641
   770
apply (subst le_minus_iff, simp)
paulson@14641
   771
done
paulson@14641
   772
avigad@16819
   773
lemma ceiling_mono: "x < y ==> ceiling x \<le> ceiling y"
avigad@16819
   774
by (simp add: floor_mono ceiling_def)
paulson@14641
   775
avigad@16819
   776
lemma ceiling_mono2: "x \<le> y ==> ceiling x \<le> ceiling y"
avigad@16819
   777
by (simp add: floor_mono2 ceiling_def)
paulson@14641
   778
paulson@14641
   779
lemma real_of_int_ceiling_cancel [simp]:
paulson@14641
   780
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
paulson@14641
   781
apply (auto simp add: ceiling_def)
paulson@14641
   782
apply (drule arg_cong [where f = uminus], auto)
paulson@14641
   783
apply (rule_tac x = "-n" in exI, auto)
paulson@14641
   784
done
paulson@14641
   785
paulson@14641
   786
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
paulson@14641
   787
apply (simp add: ceiling_def)
paulson@14641
   788
apply (rule minus_equation_iff [THEN iffD1])
paulson@14641
   789
apply (simp add: floor_eq [where n = "-(n+1)"])
paulson@14641
   790
done
paulson@14641
   791
paulson@14641
   792
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
paulson@14641
   793
by (simp add: ceiling_def floor_eq2 [where n = "-(n+1)"])
paulson@14641
   794
paulson@14641
   795
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
paulson@14641
   796
by (simp add: ceiling_def floor_eq2 [where n = "-n"])
paulson@14641
   797
paulson@14641
   798
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
paulson@14641
   799
by (simp add: ceiling_def)
paulson@14641
   800
paulson@14641
   801
lemma ceiling_number_of_eq [simp]:
paulson@14641
   802
     "ceiling (number_of n :: real) = (number_of n)"
paulson@14641
   803
apply (subst real_number_of [symmetric])
paulson@14641
   804
apply (rule ceiling_real_of_int)
paulson@14641
   805
done
paulson@14641
   806
avigad@16819
   807
lemma ceiling_one [simp]: "ceiling 1 = 1"
avigad@16819
   808
  by (unfold ceiling_def, simp)
avigad@16819
   809
paulson@14641
   810
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
paulson@14641
   811
apply (rule neg_le_iff_le [THEN iffD1])
paulson@14641
   812
apply (simp add: ceiling_def diff_minus)
paulson@14641
   813
done
paulson@14641
   814
paulson@14641
   815
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
paulson@14641
   816
apply (insert real_of_int_ceiling_diff_one_le [of r])
paulson@14641
   817
apply (simp del: real_of_int_ceiling_diff_one_le)
paulson@14641
   818
done
paulson@14641
   819
avigad@16819
   820
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
avigad@16819
   821
  apply (unfold ceiling_def)
avigad@16819
   822
  apply (subgoal_tac "-a <= floor(- x)")
avigad@16819
   823
  apply simp
avigad@16819
   824
  apply (rule le_floor)
avigad@16819
   825
  apply simp
avigad@16819
   826
done
avigad@16819
   827
avigad@16819
   828
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
avigad@16819
   829
  apply (unfold ceiling_def)
avigad@16819
   830
  apply (subgoal_tac "real(- a) <= - x")
avigad@16819
   831
  apply simp
avigad@16819
   832
  apply (rule real_le_floor)
avigad@16819
   833
  apply simp
avigad@16819
   834
done
avigad@16819
   835
avigad@16819
   836
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
avigad@16819
   837
  apply (rule iffI)
avigad@16819
   838
  apply (erule ceiling_le_real)
avigad@16819
   839
  apply (erule ceiling_le)
avigad@16819
   840
done
avigad@16819
   841
wenzelm@16893
   842
lemma ceiling_le_eq_number_of [simp]:
avigad@16819
   843
    "(ceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
   844
by (simp add: ceiling_le_eq)
avigad@16819
   845
wenzelm@16893
   846
lemma ceiling_le_zero_eq [simp]: "(ceiling x <= 0) = (x <= 0)"
avigad@16819
   847
by (simp add: ceiling_le_eq)
avigad@16819
   848
wenzelm@16893
   849
lemma ceiling_le_eq_one [simp]: "(ceiling x <= 1) = (x <= 1)"
avigad@16819
   850
by (simp add: ceiling_le_eq)
avigad@16819
   851
avigad@16819
   852
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
avigad@16819
   853
  apply (subst linorder_not_le [THEN sym])+
avigad@16819
   854
  apply simp
avigad@16819
   855
  apply (rule ceiling_le_eq)
avigad@16819
   856
done
avigad@16819
   857
wenzelm@16893
   858
lemma less_ceiling_eq_number_of [simp]:
avigad@16819
   859
    "(number_of n < ceiling x) = (number_of n < x)"
avigad@16819
   860
by (simp add: less_ceiling_eq)
avigad@16819
   861
avigad@16819
   862
lemma less_ceiling_eq_zero [simp]: "(0 < ceiling x) = (0 < x)"
avigad@16819
   863
by (simp add: less_ceiling_eq)
avigad@16819
   864
avigad@16819
   865
lemma less_ceiling_eq_one [simp]: "(1 < ceiling x) = (1 < x)"
avigad@16819
   866
by (simp add: less_ceiling_eq)
avigad@16819
   867
avigad@16819
   868
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
avigad@16819
   869
  apply (insert ceiling_le_eq [of x "a - 1"])
avigad@16819
   870
  apply auto
avigad@16819
   871
done
avigad@16819
   872
wenzelm@16893
   873
lemma ceiling_less_eq_number_of [simp]:
avigad@16819
   874
    "(ceiling x < number_of n) = (x <= number_of n - 1)"
avigad@16819
   875
by (simp add: ceiling_less_eq)
avigad@16819
   876
avigad@16819
   877
lemma ceiling_less_eq_zero [simp]: "(ceiling x < 0) = (x <= -1)"
avigad@16819
   878
by (simp add: ceiling_less_eq)
avigad@16819
   879
avigad@16819
   880
lemma ceiling_less_eq_one [simp]: "(ceiling x < 1) = (x <= 0)"
avigad@16819
   881
by (simp add: ceiling_less_eq)
avigad@16819
   882
avigad@16819
   883
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
avigad@16819
   884
  apply (insert less_ceiling_eq [of "a - 1" x])
avigad@16819
   885
  apply auto
avigad@16819
   886
done
avigad@16819
   887
wenzelm@16893
   888
lemma le_ceiling_eq_number_of [simp]:
avigad@16819
   889
    "(number_of n <= ceiling x) = (number_of n - 1 < x)"
avigad@16819
   890
by (simp add: le_ceiling_eq)
avigad@16819
   891
avigad@16819
   892
lemma le_ceiling_eq_zero [simp]: "(0 <= ceiling x) = (-1 < x)"
avigad@16819
   893
by (simp add: le_ceiling_eq)
avigad@16819
   894
avigad@16819
   895
lemma le_ceiling_eq_one [simp]: "(1 <= ceiling x) = (0 < x)"
avigad@16819
   896
by (simp add: le_ceiling_eq)
avigad@16819
   897
avigad@16819
   898
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
avigad@16819
   899
  apply (unfold ceiling_def, simp)
avigad@16819
   900
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   901
  apply (subst floor_add)
avigad@16819
   902
  apply simp
avigad@16819
   903
done
avigad@16819
   904
wenzelm@16893
   905
lemma ceiling_add_number_of [simp]: "ceiling (x + number_of n) =
avigad@16819
   906
    ceiling x + number_of n"
avigad@16819
   907
  apply (subst ceiling_add [THEN sym])
avigad@16819
   908
  apply simp
avigad@16819
   909
done
avigad@16819
   910
avigad@16819
   911
lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
avigad@16819
   912
  apply (subst ceiling_add [THEN sym])
avigad@16819
   913
  apply simp
avigad@16819
   914
done
avigad@16819
   915
avigad@16819
   916
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
avigad@16819
   917
  apply (subst diff_minus)+
avigad@16819
   918
  apply (subst real_of_int_minus [THEN sym])
avigad@16819
   919
  apply (rule ceiling_add)
avigad@16819
   920
done
avigad@16819
   921
wenzelm@16893
   922
lemma ceiling_subtract_number_of [simp]: "ceiling (x - number_of n) =
avigad@16819
   923
    ceiling x - number_of n"
avigad@16819
   924
  apply (subst ceiling_subtract [THEN sym])
avigad@16819
   925
  apply simp
avigad@16819
   926
done
avigad@16819
   927
avigad@16819
   928
lemma ceiling_subtract_one [simp]: "ceiling (x - 1) = ceiling x - 1"
avigad@16819
   929
  apply (subst ceiling_subtract [THEN sym])
avigad@16819
   930
  apply simp
avigad@16819
   931
done
avigad@16819
   932
avigad@16819
   933
subsection {* Versions for the natural numbers *}
avigad@16819
   934
wenzelm@19765
   935
definition
wenzelm@21404
   936
  natfloor :: "real => nat" where
wenzelm@19765
   937
  "natfloor x = nat(floor x)"
wenzelm@21404
   938
wenzelm@21404
   939
definition
wenzelm@21404
   940
  natceiling :: "real => nat" where
wenzelm@19765
   941
  "natceiling x = nat(ceiling x)"
avigad@16819
   942
avigad@16819
   943
lemma natfloor_zero [simp]: "natfloor 0 = 0"
avigad@16819
   944
  by (unfold natfloor_def, simp)
avigad@16819
   945
avigad@16819
   946
lemma natfloor_one [simp]: "natfloor 1 = 1"
avigad@16819
   947
  by (unfold natfloor_def, simp)
avigad@16819
   948
avigad@16819
   949
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
avigad@16819
   950
  by (unfold natfloor_def, simp)
avigad@16819
   951
avigad@16819
   952
lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
avigad@16819
   953
  by (unfold natfloor_def, simp)
avigad@16819
   954
avigad@16819
   955
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
avigad@16819
   956
  by (unfold natfloor_def, simp)
avigad@16819
   957
avigad@16819
   958
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
avigad@16819
   959
  by (unfold natfloor_def, simp)
avigad@16819
   960
avigad@16819
   961
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
avigad@16819
   962
  apply (unfold natfloor_def)
avigad@16819
   963
  apply (subgoal_tac "floor x <= floor 0")
avigad@16819
   964
  apply simp
avigad@16819
   965
  apply (erule floor_mono2)
avigad@16819
   966
done
avigad@16819
   967
avigad@16819
   968
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
avigad@16819
   969
  apply (case_tac "0 <= x")
avigad@16819
   970
  apply (subst natfloor_def)+
avigad@16819
   971
  apply (subst nat_le_eq_zle)
avigad@16819
   972
  apply force
wenzelm@16893
   973
  apply (erule floor_mono2)
avigad@16819
   974
  apply (subst natfloor_neg)
avigad@16819
   975
  apply simp
avigad@16819
   976
  apply simp
avigad@16819
   977
done
avigad@16819
   978
avigad@16819
   979
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
avigad@16819
   980
  apply (unfold natfloor_def)
avigad@16819
   981
  apply (subst nat_int [THEN sym])
avigad@16819
   982
  apply (subst nat_le_eq_zle)
avigad@16819
   983
  apply simp
avigad@16819
   984
  apply (rule le_floor)
avigad@16819
   985
  apply simp
avigad@16819
   986
done
avigad@16819
   987
avigad@16819
   988
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
avigad@16819
   989
  apply (rule iffI)
avigad@16819
   990
  apply (rule order_trans)
avigad@16819
   991
  prefer 2
avigad@16819
   992
  apply (erule real_natfloor_le)
avigad@16819
   993
  apply (subst real_of_nat_le_iff)
avigad@16819
   994
  apply assumption
avigad@16819
   995
  apply (erule le_natfloor)
avigad@16819
   996
done
avigad@16819
   997
wenzelm@16893
   998
lemma le_natfloor_eq_number_of [simp]:
avigad@16819
   999
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16819
  1000
      (number_of n <= natfloor x) = (number_of n <= x)"
avigad@16819
  1001
  apply (subst le_natfloor_eq, assumption)
avigad@16819
  1002
  apply simp
avigad@16819
  1003
done
avigad@16819
  1004
avigad@16820
  1005
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
avigad@16819
  1006
  apply (case_tac "0 <= x")
avigad@16819
  1007
  apply (subst le_natfloor_eq, assumption, simp)
avigad@16819
  1008
  apply (rule iffI)
wenzelm@16893
  1009
  apply (subgoal_tac "natfloor x <= natfloor 0")
avigad@16819
  1010
  apply simp
avigad@16819
  1011
  apply (rule natfloor_mono)
avigad@16819
  1012
  apply simp
avigad@16819
  1013
  apply simp
avigad@16819
  1014
done
avigad@16819
  1015
avigad@16819
  1016
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
avigad@16819
  1017
  apply (unfold natfloor_def)
avigad@16819
  1018
  apply (subst nat_int [THEN sym]);back;
avigad@16819
  1019
  apply (subst eq_nat_nat_iff)
avigad@16819
  1020
  apply simp
avigad@16819
  1021
  apply simp
avigad@16819
  1022
  apply (rule floor_eq2)
avigad@16819
  1023
  apply auto
avigad@16819
  1024
done
avigad@16819
  1025
avigad@16819
  1026
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
avigad@16819
  1027
  apply (case_tac "0 <= x")
avigad@16819
  1028
  apply (unfold natfloor_def)
avigad@16819
  1029
  apply simp
avigad@16819
  1030
  apply simp_all
avigad@16819
  1031
done
avigad@16819
  1032
avigad@16819
  1033
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
avigad@16819
  1034
  apply (simp add: compare_rls)
avigad@16819
  1035
  apply (rule real_natfloor_add_one_gt)
avigad@16819
  1036
done
avigad@16819
  1037
avigad@16819
  1038
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
avigad@16819
  1039
  apply (subgoal_tac "z < real(natfloor z) + 1")
avigad@16819
  1040
  apply arith
avigad@16819
  1041
  apply (rule real_natfloor_add_one_gt)
avigad@16819
  1042
done
avigad@16819
  1043
avigad@16819
  1044
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
avigad@16819
  1045
  apply (unfold natfloor_def)
huffman@24355
  1046
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1047
  apply (erule ssubst)
huffman@23309
  1048
  apply (simp add: nat_add_distrib del: real_of_int_of_nat_eq)
avigad@16819
  1049
  apply simp
avigad@16819
  1050
done
avigad@16819
  1051
wenzelm@16893
  1052
lemma natfloor_add_number_of [simp]:
wenzelm@16893
  1053
    "~neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16819
  1054
      natfloor (x + number_of n) = natfloor x + number_of n"
avigad@16819
  1055
  apply (subst natfloor_add [THEN sym])
avigad@16819
  1056
  apply simp_all
avigad@16819
  1057
done
avigad@16819
  1058
avigad@16819
  1059
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
avigad@16819
  1060
  apply (subst natfloor_add [THEN sym])
avigad@16819
  1061
  apply assumption
avigad@16819
  1062
  apply simp
avigad@16819
  1063
done
avigad@16819
  1064
wenzelm@16893
  1065
lemma natfloor_subtract [simp]: "real a <= x ==>
avigad@16819
  1066
    natfloor(x - real a) = natfloor x - a"
avigad@16819
  1067
  apply (unfold natfloor_def)
huffman@24355
  1068
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1069
  apply (erule ssubst)
huffman@23309
  1070
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1071
  apply simp
avigad@16819
  1072
done
avigad@16819
  1073
avigad@16819
  1074
lemma natceiling_zero [simp]: "natceiling 0 = 0"
avigad@16819
  1075
  by (unfold natceiling_def, simp)
avigad@16819
  1076
avigad@16819
  1077
lemma natceiling_one [simp]: "natceiling 1 = 1"
avigad@16819
  1078
  by (unfold natceiling_def, simp)
avigad@16819
  1079
avigad@16819
  1080
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
avigad@16819
  1081
  by (unfold natceiling_def, simp)
avigad@16819
  1082
avigad@16819
  1083
lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
avigad@16819
  1084
  by (unfold natceiling_def, simp)
avigad@16819
  1085
avigad@16819
  1086
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
avigad@16819
  1087
  by (unfold natceiling_def, simp)
avigad@16819
  1088
avigad@16819
  1089
lemma real_natceiling_ge: "x <= real(natceiling x)"
avigad@16819
  1090
  apply (unfold natceiling_def)
avigad@16819
  1091
  apply (case_tac "x < 0")
avigad@16819
  1092
  apply simp
avigad@16819
  1093
  apply (subst real_nat_eq_real)
avigad@16819
  1094
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
  1095
  apply simp
avigad@16819
  1096
  apply (rule ceiling_mono2)
avigad@16819
  1097
  apply simp
avigad@16819
  1098
  apply simp
avigad@16819
  1099
done
avigad@16819
  1100
avigad@16819
  1101
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
avigad@16819
  1102
  apply (unfold natceiling_def)
avigad@16819
  1103
  apply simp
avigad@16819
  1104
done
avigad@16819
  1105
avigad@16819
  1106
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
avigad@16819
  1107
  apply (case_tac "0 <= x")
avigad@16819
  1108
  apply (subst natceiling_def)+
avigad@16819
  1109
  apply (subst nat_le_eq_zle)
avigad@16819
  1110
  apply (rule disjI2)
avigad@16819
  1111
  apply (subgoal_tac "real (0::int) <= real(ceiling y)")
avigad@16819
  1112
  apply simp
avigad@16819
  1113
  apply (rule order_trans)
avigad@16819
  1114
  apply simp
avigad@16819
  1115
  apply (erule order_trans)
avigad@16819
  1116
  apply simp
avigad@16819
  1117
  apply (erule ceiling_mono2)
avigad@16819
  1118
  apply (subst natceiling_neg)
avigad@16819
  1119
  apply simp_all
avigad@16819
  1120
done
avigad@16819
  1121
avigad@16819
  1122
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
avigad@16819
  1123
  apply (unfold natceiling_def)
avigad@16819
  1124
  apply (case_tac "x < 0")
avigad@16819
  1125
  apply simp
avigad@16819
  1126
  apply (subst nat_int [THEN sym]);back;
avigad@16819
  1127
  apply (subst nat_le_eq_zle)
avigad@16819
  1128
  apply simp
avigad@16819
  1129
  apply (rule ceiling_le)
avigad@16819
  1130
  apply simp
avigad@16819
  1131
done
avigad@16819
  1132
avigad@16819
  1133
lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"
avigad@16819
  1134
  apply (rule iffI)
avigad@16819
  1135
  apply (rule order_trans)
avigad@16819
  1136
  apply (rule real_natceiling_ge)
avigad@16819
  1137
  apply (subst real_of_nat_le_iff)
avigad@16819
  1138
  apply assumption
avigad@16819
  1139
  apply (erule natceiling_le)
avigad@16819
  1140
done
avigad@16819
  1141
wenzelm@16893
  1142
lemma natceiling_le_eq_number_of [simp]:
avigad@16820
  1143
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16820
  1144
      (natceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
  1145
  apply (subst natceiling_le_eq, assumption)
avigad@16819
  1146
  apply simp
avigad@16819
  1147
done
avigad@16819
  1148
avigad@16820
  1149
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
avigad@16819
  1150
  apply (case_tac "0 <= x")
avigad@16819
  1151
  apply (subst natceiling_le_eq)
avigad@16819
  1152
  apply assumption
avigad@16819
  1153
  apply simp
avigad@16819
  1154
  apply (subst natceiling_neg)
avigad@16819
  1155
  apply simp
avigad@16819
  1156
  apply simp
avigad@16819
  1157
done
avigad@16819
  1158
avigad@16819
  1159
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
avigad@16819
  1160
  apply (unfold natceiling_def)
wenzelm@19850
  1161
  apply (simplesubst nat_int [THEN sym]) back back
avigad@16819
  1162
  apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")
avigad@16819
  1163
  apply (erule ssubst)
avigad@16819
  1164
  apply (subst eq_nat_nat_iff)
avigad@16819
  1165
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
  1166
  apply simp
avigad@16819
  1167
  apply (rule ceiling_mono2)
avigad@16819
  1168
  apply force
avigad@16819
  1169
  apply force
avigad@16819
  1170
  apply (rule ceiling_eq2)
avigad@16819
  1171
  apply (simp, simp)
avigad@16819
  1172
  apply (subst nat_add_distrib)
avigad@16819
  1173
  apply auto
avigad@16819
  1174
done
avigad@16819
  1175
wenzelm@16893
  1176
lemma natceiling_add [simp]: "0 <= x ==>
avigad@16819
  1177
    natceiling (x + real a) = natceiling x + a"
avigad@16819
  1178
  apply (unfold natceiling_def)
huffman@24355
  1179
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1180
  apply (erule ssubst)
huffman@23309
  1181
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1182
  apply (subst nat_add_distrib)
avigad@16819
  1183
  apply (subgoal_tac "0 = ceiling 0")
avigad@16819
  1184
  apply (erule ssubst)
avigad@16819
  1185
  apply (erule ceiling_mono2)
avigad@16819
  1186
  apply simp_all
avigad@16819
  1187
done
avigad@16819
  1188
wenzelm@16893
  1189
lemma natceiling_add_number_of [simp]:
wenzelm@16893
  1190
    "~ neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16820
  1191
      natceiling (x + number_of n) = natceiling x + number_of n"
avigad@16819
  1192
  apply (subst natceiling_add [THEN sym])
avigad@16819
  1193
  apply simp_all
avigad@16819
  1194
done
avigad@16819
  1195
avigad@16819
  1196
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
avigad@16819
  1197
  apply (subst natceiling_add [THEN sym])
avigad@16819
  1198
  apply assumption
avigad@16819
  1199
  apply simp
avigad@16819
  1200
done
avigad@16819
  1201
wenzelm@16893
  1202
lemma natceiling_subtract [simp]: "real a <= x ==>
avigad@16819
  1203
    natceiling(x - real a) = natceiling x - a"
avigad@16819
  1204
  apply (unfold natceiling_def)
huffman@24355
  1205
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
  1206
  apply (erule ssubst)
huffman@23309
  1207
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
  1208
  apply simp
avigad@16819
  1209
done
avigad@16819
  1210
nipkow@25140
  1211
lemma natfloor_div_nat: "1 <= x ==> y \<noteq> 0 ==>
avigad@16819
  1212
  natfloor (x / real y) = natfloor x div y"
avigad@16819
  1213
proof -
nipkow@25140
  1214
  assume "1 <= (x::real)" and "(y::nat) \<noteq> 0"
avigad@16819
  1215
  have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"
avigad@16819
  1216
    by simp
wenzelm@16893
  1217
  then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +
avigad@16819
  1218
    real((natfloor x) mod y)"
avigad@16819
  1219
    by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])
avigad@16819
  1220
  have "x = real(natfloor x) + (x - real(natfloor x))"
avigad@16819
  1221
    by simp
wenzelm@16893
  1222
  then have "x = real ((natfloor x) div y) * real y +
avigad@16819
  1223
      real((natfloor x) mod y) + (x - real(natfloor x))"
avigad@16819
  1224
    by (simp add: a)
avigad@16819
  1225
  then have "x / real y = ... / real y"
avigad@16819
  1226
    by simp
wenzelm@16893
  1227
  also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /
avigad@16819
  1228
    real y + (x - real(natfloor x)) / real y"
nipkow@23477
  1229
    by (auto simp add: ring_simps add_divide_distrib
avigad@16819
  1230
      diff_divide_distrib prems)
avigad@16819
  1231
  finally have "natfloor (x / real y) = natfloor(...)" by simp
wenzelm@16893
  1232
  also have "... = natfloor(real((natfloor x) mod y) /
avigad@16819
  1233
    real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"
avigad@16819
  1234
    by (simp add: add_ac)
wenzelm@16893
  1235
  also have "... = natfloor(real((natfloor x) mod y) /
avigad@16819
  1236
    real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"
avigad@16819
  1237
    apply (rule natfloor_add)
avigad@16819
  1238
    apply (rule add_nonneg_nonneg)
avigad@16819
  1239
    apply (rule divide_nonneg_pos)
avigad@16819
  1240
    apply simp
avigad@16819
  1241
    apply (simp add: prems)
avigad@16819
  1242
    apply (rule divide_nonneg_pos)
avigad@16819
  1243
    apply (simp add: compare_rls)
avigad@16819
  1244
    apply (rule real_natfloor_le)
avigad@16819
  1245
    apply (insert prems, auto)
avigad@16819
  1246
    done
wenzelm@16893
  1247
  also have "natfloor(real((natfloor x) mod y) /
avigad@16819
  1248
    real y + (x - real(natfloor x)) / real y) = 0"
avigad@16819
  1249
    apply (rule natfloor_eq)
avigad@16819
  1250
    apply simp
avigad@16819
  1251
    apply (rule add_nonneg_nonneg)
avigad@16819
  1252
    apply (rule divide_nonneg_pos)
avigad@16819
  1253
    apply force
avigad@16819
  1254
    apply (force simp add: prems)
avigad@16819
  1255
    apply (rule divide_nonneg_pos)
avigad@16819
  1256
    apply (simp add: compare_rls)
avigad@16819
  1257
    apply (rule real_natfloor_le)
avigad@16819
  1258
    apply (auto simp add: prems)
avigad@16819
  1259
    apply (insert prems, arith)
avigad@16819
  1260
    apply (simp add: add_divide_distrib [THEN sym])
avigad@16819
  1261
    apply (subgoal_tac "real y = real y - 1 + 1")
avigad@16819
  1262
    apply (erule ssubst)
avigad@16819
  1263
    apply (rule add_le_less_mono)
avigad@16819
  1264
    apply (simp add: compare_rls)
wenzelm@16893
  1265
    apply (subgoal_tac "real(natfloor x mod y) + 1 =
avigad@16819
  1266
      real(natfloor x mod y + 1)")
avigad@16819
  1267
    apply (erule ssubst)
avigad@16819
  1268
    apply (subst real_of_nat_le_iff)
avigad@16819
  1269
    apply (subgoal_tac "natfloor x mod y < y")
avigad@16819
  1270
    apply arith
avigad@16819
  1271
    apply (rule mod_less_divisor)
avigad@16819
  1272
    apply auto
avigad@16819
  1273
    apply (simp add: compare_rls)
avigad@16819
  1274
    apply (subst add_commute)
avigad@16819
  1275
    apply (rule real_natfloor_add_one_gt)
avigad@16819
  1276
    done
nipkow@25140
  1277
  finally show ?thesis by simp
avigad@16819
  1278
qed
avigad@16819
  1279
paulson@14365
  1280
end