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