src/HOL/RComplete.thy
author huffman
Fri Sep 02 15:19:59 2011 -0700 (2011-09-02)
changeset 44668 31d41a0f6b5d
parent 44667 ee5772ca7d16
child 44669 8e6cdb9c00a7
permissions -rw-r--r--
simplify proof of Rats_dense_in_real;
remove lemma Rats_dense_in_nn_real;
wenzelm@30122
     1
(*  Title:      HOL/RComplete.thy
wenzelm@30122
     2
    Author:     Jacques D. Fleuriot, University of Edinburgh
wenzelm@30122
     3
    Author:     Larry Paulson, University of Cambridge
wenzelm@30122
     4
    Author:     Jeremy Avigad, Carnegie Mellon University
wenzelm@30122
     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@32707
    17
lemma abs_diff_less_iff:
haftmann@35028
    18
  "(\<bar>x - a\<bar> < (r::'a::linordered_idom)) = (a - r < x \<and> x < a + r)"
paulson@32707
    19
  by auto
paulson@14365
    20
wenzelm@16893
    21
subsection {* Completeness of Positive Reals *}
wenzelm@16893
    22
wenzelm@16893
    23
text {*
wenzelm@16893
    24
  Supremum property for the set of positive reals
wenzelm@16893
    25
wenzelm@16893
    26
  Let @{text "P"} be a non-empty set of positive reals, with an upper
wenzelm@16893
    27
  bound @{text "y"}.  Then @{text "P"} has a least upper bound
wenzelm@16893
    28
  (written @{text "S"}).
paulson@14365
    29
wenzelm@16893
    30
  FIXME: Can the premise be weakened to @{text "\<forall>x \<in> P. x\<le> y"}?
wenzelm@16893
    31
*}
wenzelm@16893
    32
huffman@36795
    33
text {* Only used in HOL/Import/HOL4Compat.thy; delete? *}
huffman@36795
    34
wenzelm@16893
    35
lemma posreal_complete:
wenzelm@16893
    36
  assumes positive_P: "\<forall>x \<in> P. (0::real) < x"
wenzelm@16893
    37
    and not_empty_P: "\<exists>x. x \<in> P"
wenzelm@16893
    38
    and upper_bound_Ex: "\<exists>y. \<forall>x \<in> P. x<y"
wenzelm@16893
    39
  shows "\<exists>S. \<forall>y. (\<exists>x \<in> P. y < x) = (y < S)"
huffman@36795
    40
proof -
huffman@36795
    41
  from upper_bound_Ex have "\<exists>z. \<forall>x\<in>P. x \<le> z"
huffman@36795
    42
    by (auto intro: less_imp_le)
huffman@36795
    43
  from complete_real [OF not_empty_P this] obtain S
huffman@36795
    44
  where S1: "\<And>x. x \<in> P \<Longrightarrow> x \<le> S" and S2: "\<And>z. \<forall>x\<in>P. x \<le> z \<Longrightarrow> S \<le> z" by fast
huffman@36795
    45
  have "\<forall>y. (\<exists>x \<in> P. y < x) = (y < S)"
huffman@36795
    46
  proof
huffman@36795
    47
    fix y show "(\<exists>x\<in>P. y < x) = (y < S)"
huffman@36795
    48
      apply (cases "\<exists>x\<in>P. y < x", simp_all)
huffman@36795
    49
      apply (clarify, drule S1, simp)
huffman@36795
    50
      apply (simp add: not_less S2)
huffman@36795
    51
      done
wenzelm@16893
    52
  qed
huffman@36795
    53
  thus ?thesis ..
wenzelm@16893
    54
qed
wenzelm@16893
    55
wenzelm@16893
    56
text {*
wenzelm@16893
    57
  \medskip Completeness properties using @{text "isUb"}, @{text "isLub"} etc.
wenzelm@16893
    58
*}
paulson@14365
    59
paulson@14365
    60
lemma real_isLub_unique: "[| isLub R S x; isLub R S y |] ==> x = (y::real)"
wenzelm@16893
    61
  apply (frule isLub_isUb)
wenzelm@16893
    62
  apply (frule_tac x = y in isLub_isUb)
wenzelm@16893
    63
  apply (blast intro!: order_antisym dest!: isLub_le_isUb)
wenzelm@16893
    64
  done
paulson@14365
    65
paulson@5078
    66
wenzelm@16893
    67
text {*
wenzelm@16893
    68
  \medskip reals Completeness (again!)
wenzelm@16893
    69
*}
paulson@14365
    70
wenzelm@16893
    71
lemma reals_complete:
wenzelm@16893
    72
  assumes notempty_S: "\<exists>X. X \<in> S"
wenzelm@16893
    73
    and exists_Ub: "\<exists>Y. isUb (UNIV::real set) S Y"
wenzelm@16893
    74
  shows "\<exists>t. isLub (UNIV :: real set) S t"
wenzelm@16893
    75
proof -
huffman@36795
    76
  from assms have "\<exists>X. X \<in> S" and "\<exists>Y. \<forall>x\<in>S. x \<le> Y"
huffman@36795
    77
    unfolding isUb_def setle_def by simp_all
huffman@36795
    78
  from complete_real [OF this] show ?thesis
huffman@36795
    79
    unfolding isLub_def leastP_def setle_def setge_def Ball_def
huffman@36795
    80
      Collect_def mem_def isUb_def UNIV_def by simp
wenzelm@16893
    81
qed
paulson@14365
    82
paulson@32707
    83
text{*A version of the same theorem without all those predicates!*}
paulson@32707
    84
lemma reals_complete2:
paulson@32707
    85
  fixes S :: "(real set)"
paulson@32707
    86
  assumes "\<exists>y. y\<in>S" and "\<exists>(x::real). \<forall>y\<in>S. y \<le> x"
paulson@32707
    87
  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) & 
paulson@32707
    88
               (\<forall>z. ((\<forall>y\<in>S. y \<le> z) --> x \<le> z))"
huffman@36795
    89
using assms by (rule complete_real)
paulson@32707
    90
paulson@14365
    91
wenzelm@16893
    92
subsection {* The Archimedean Property of the Reals *}
wenzelm@16893
    93
wenzelm@16893
    94
theorem reals_Archimedean:
wenzelm@16893
    95
  assumes x_pos: "0 < x"
wenzelm@16893
    96
  shows "\<exists>n. inverse (real (Suc n)) < x"
huffman@36795
    97
  unfolding real_of_nat_def using x_pos
huffman@36795
    98
  by (rule ex_inverse_of_nat_Suc_less)
paulson@14365
    99
paulson@14365
   100
lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
huffman@36795
   101
  unfolding real_of_nat_def by (rule ex_less_of_nat)
huffman@30097
   102
wenzelm@16893
   103
lemma reals_Archimedean3:
wenzelm@16893
   104
  assumes x_greater_zero: "0 < x"
wenzelm@16893
   105
  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
huffman@30097
   106
  unfolding real_of_nat_def using `0 < x`
huffman@30097
   107
  by (auto intro: ex_less_of_nat_mult)
paulson@14365
   108
avigad@16819
   109
nipkow@28091
   110
subsection{*Density of the Rational Reals in the Reals*}
nipkow@28091
   111
nipkow@28091
   112
text{* This density proof is due to Stefan Richter and was ported by TN.  The
nipkow@28091
   113
original source is \emph{Real Analysis} by H.L. Royden.
nipkow@28091
   114
It employs the Archimedean property of the reals. *}
nipkow@28091
   115
huffman@44668
   116
lemma Rats_dense_in_real:
huffman@44668
   117
  fixes x :: real
huffman@44668
   118
  assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
nipkow@28091
   119
proof -
nipkow@28091
   120
  from `x<y` have "0 < y-x" by simp
nipkow@28091
   121
  with reals_Archimedean obtain q::nat 
huffman@44668
   122
    where q: "inverse (real q) < y-x" and "0 < q" by auto
huffman@44668
   123
  def p \<equiv> "ceiling (y * real q) - 1"
huffman@44668
   124
  def r \<equiv> "of_int p / real q"
huffman@44668
   125
  from q have "x < y - inverse (real q)" by simp
huffman@44668
   126
  also have "y - inverse (real q) \<le> r"
huffman@44668
   127
    unfolding r_def p_def
huffman@44668
   128
    by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
huffman@44668
   129
  finally have "x < r" .
huffman@44668
   130
  moreover have "r < y"
huffman@44668
   131
    unfolding r_def p_def
huffman@44668
   132
    by (simp add: divide_less_eq diff_less_eq `0 < q`
huffman@44668
   133
      less_ceiling_iff [symmetric])
huffman@44668
   134
  moreover from r_def have "r \<in> \<rat>" by simp
nipkow@28091
   135
  ultimately show ?thesis by fast
nipkow@28091
   136
qed
nipkow@28091
   137
nipkow@28091
   138
paulson@14641
   139
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
paulson@14641
   140
paulson@14641
   141
lemma number_of_less_real_of_int_iff [simp]:
paulson@14641
   142
     "((number_of n) < real (m::int)) = (number_of n < m)"
paulson@14641
   143
apply auto
paulson@14641
   144
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   145
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   146
done
paulson@14641
   147
paulson@14641
   148
lemma number_of_less_real_of_int_iff2 [simp]:
paulson@14641
   149
     "(real (m::int) < (number_of n)) = (m < number_of n)"
paulson@14641
   150
apply auto
paulson@14641
   151
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   152
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   153
done
paulson@14641
   154
paulson@14641
   155
lemma number_of_le_real_of_int_iff [simp]:
paulson@14641
   156
     "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
paulson@14641
   157
by (simp add: linorder_not_less [symmetric])
paulson@14641
   158
paulson@14641
   159
lemma number_of_le_real_of_int_iff2 [simp]:
paulson@14641
   160
     "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"
paulson@14641
   161
by (simp add: linorder_not_less [symmetric])
paulson@14641
   162
huffman@24355
   163
lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
huffman@30097
   164
unfolding real_of_nat_def by simp
paulson@14641
   165
huffman@24355
   166
lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
huffman@30102
   167
unfolding real_of_nat_def by (simp add: floor_minus)
paulson@14641
   168
paulson@14641
   169
lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
huffman@30097
   170
unfolding real_of_int_def by simp
paulson@14641
   171
paulson@14641
   172
lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
huffman@30102
   173
unfolding real_of_int_def by (simp add: floor_minus)
paulson@14641
   174
paulson@14641
   175
lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
huffman@30097
   176
unfolding real_of_int_def by (rule floor_exists)
paulson@14641
   177
paulson@14641
   178
lemma lemma_floor:
paulson@14641
   179
  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
paulson@14641
   180
  shows "m \<le> (n::int)"
paulson@14641
   181
proof -
wenzelm@23389
   182
  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
wenzelm@23389
   183
  also have "... = real (n + 1)" by simp
wenzelm@23389
   184
  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
paulson@14641
   185
  thus ?thesis by arith
paulson@14641
   186
qed
paulson@14641
   187
paulson@14641
   188
lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
huffman@30097
   189
unfolding real_of_int_def by (rule of_int_floor_le)
paulson@14641
   190
paulson@14641
   191
lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
paulson@14641
   192
by (auto intro: lemma_floor)
paulson@14641
   193
paulson@14641
   194
lemma real_of_int_floor_cancel [simp]:
paulson@14641
   195
    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
huffman@30097
   196
  using floor_real_of_int by metis
paulson@14641
   197
paulson@14641
   198
lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
huffman@30097
   199
  unfolding real_of_int_def using floor_unique [of n x] by simp
paulson@14641
   200
paulson@14641
   201
lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
huffman@30097
   202
  unfolding real_of_int_def by (rule floor_unique)
paulson@14641
   203
paulson@14641
   204
lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   205
apply (rule inj_int [THEN injD])
paulson@14641
   206
apply (simp add: real_of_nat_Suc)
nipkow@15539
   207
apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
paulson@14641
   208
done
paulson@14641
   209
paulson@14641
   210
lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   211
apply (drule order_le_imp_less_or_eq)
paulson@14641
   212
apply (auto intro: floor_eq3)
paulson@14641
   213
done
paulson@14641
   214
paulson@14641
   215
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
huffman@30097
   216
  unfolding real_of_int_def using floor_correct [of r] by simp
avigad@16819
   217
avigad@16819
   218
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
huffman@30097
   219
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   220
paulson@14641
   221
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
huffman@30097
   222
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   223
avigad@16819
   224
lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
huffman@30097
   225
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   226
avigad@16819
   227
lemma le_floor: "real a <= x ==> a <= floor x"
huffman@30097
   228
  unfolding real_of_int_def by (simp add: le_floor_iff)
avigad@16819
   229
avigad@16819
   230
lemma real_le_floor: "a <= floor x ==> real a <= x"
huffman@30097
   231
  unfolding real_of_int_def by (simp add: le_floor_iff)
avigad@16819
   232
avigad@16819
   233
lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
huffman@30097
   234
  unfolding real_of_int_def by (rule le_floor_iff)
avigad@16819
   235
avigad@16819
   236
lemma floor_less_eq: "(floor x < a) = (x < real a)"
huffman@30097
   237
  unfolding real_of_int_def by (rule floor_less_iff)
avigad@16819
   238
avigad@16819
   239
lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
huffman@30097
   240
  unfolding real_of_int_def by (rule less_floor_iff)
avigad@16819
   241
avigad@16819
   242
lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
huffman@30097
   243
  unfolding real_of_int_def by (rule floor_le_iff)
avigad@16819
   244
avigad@16819
   245
lemma floor_add [simp]: "floor (x + real a) = floor x + a"
huffman@30097
   246
  unfolding real_of_int_def by (rule floor_add_of_int)
avigad@16819
   247
avigad@16819
   248
lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
huffman@30097
   249
  unfolding real_of_int_def by (rule floor_diff_of_int)
avigad@16819
   250
hoelzl@35578
   251
lemma le_mult_floor:
hoelzl@35578
   252
  assumes "0 \<le> (a :: real)" and "0 \<le> b"
hoelzl@35578
   253
  shows "floor a * floor b \<le> floor (a * b)"
hoelzl@35578
   254
proof -
hoelzl@35578
   255
  have "real (floor a) \<le> a"
hoelzl@35578
   256
    and "real (floor b) \<le> b" by auto
hoelzl@35578
   257
  hence "real (floor a * floor b) \<le> a * b"
hoelzl@35578
   258
    using assms by (auto intro!: mult_mono)
hoelzl@35578
   259
  also have "a * b < real (floor (a * b) + 1)" by auto
hoelzl@35578
   260
  finally show ?thesis unfolding real_of_int_less_iff by simp
hoelzl@35578
   261
qed
hoelzl@35578
   262
huffman@24355
   263
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
huffman@30097
   264
  unfolding real_of_nat_def by simp
paulson@14641
   265
paulson@14641
   266
lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"
huffman@30097
   267
  unfolding real_of_int_def by simp
paulson@14641
   268
paulson@14641
   269
lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"
huffman@30097
   270
  unfolding real_of_int_def by simp
paulson@14641
   271
paulson@14641
   272
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
huffman@30097
   273
  unfolding real_of_int_def by (rule le_of_int_ceiling)
paulson@14641
   274
huffman@30097
   275
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
huffman@30097
   276
  unfolding real_of_int_def by simp
paulson@14641
   277
paulson@14641
   278
lemma real_of_int_ceiling_cancel [simp]:
paulson@14641
   279
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
huffman@30097
   280
  using ceiling_real_of_int by metis
paulson@14641
   281
paulson@14641
   282
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   283
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   284
paulson@14641
   285
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   286
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   287
paulson@14641
   288
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
huffman@30097
   289
  unfolding real_of_int_def using ceiling_unique [of n x] by simp
paulson@14641
   290
paulson@14641
   291
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
huffman@30097
   292
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   293
paulson@14641
   294
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
huffman@30097
   295
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   296
avigad@16819
   297
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
huffman@30097
   298
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   299
avigad@16819
   300
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
huffman@30097
   301
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   302
avigad@16819
   303
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
huffman@30097
   304
  unfolding real_of_int_def by (rule ceiling_le_iff)
avigad@16819
   305
avigad@16819
   306
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
huffman@30097
   307
  unfolding real_of_int_def by (rule less_ceiling_iff)
avigad@16819
   308
avigad@16819
   309
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
huffman@30097
   310
  unfolding real_of_int_def by (rule ceiling_less_iff)
avigad@16819
   311
avigad@16819
   312
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
huffman@30097
   313
  unfolding real_of_int_def by (rule le_ceiling_iff)
avigad@16819
   314
avigad@16819
   315
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
huffman@30097
   316
  unfolding real_of_int_def by (rule ceiling_add_of_int)
avigad@16819
   317
avigad@16819
   318
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
huffman@30097
   319
  unfolding real_of_int_def by (rule ceiling_diff_of_int)
avigad@16819
   320
avigad@16819
   321
avigad@16819
   322
subsection {* Versions for the natural numbers *}
avigad@16819
   323
wenzelm@19765
   324
definition
wenzelm@21404
   325
  natfloor :: "real => nat" where
wenzelm@19765
   326
  "natfloor x = nat(floor x)"
wenzelm@21404
   327
wenzelm@21404
   328
definition
wenzelm@21404
   329
  natceiling :: "real => nat" where
wenzelm@19765
   330
  "natceiling x = nat(ceiling x)"
avigad@16819
   331
avigad@16819
   332
lemma natfloor_zero [simp]: "natfloor 0 = 0"
avigad@16819
   333
  by (unfold natfloor_def, simp)
avigad@16819
   334
avigad@16819
   335
lemma natfloor_one [simp]: "natfloor 1 = 1"
avigad@16819
   336
  by (unfold natfloor_def, simp)
avigad@16819
   337
avigad@16819
   338
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
avigad@16819
   339
  by (unfold natfloor_def, simp)
avigad@16819
   340
avigad@16819
   341
lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
avigad@16819
   342
  by (unfold natfloor_def, simp)
avigad@16819
   343
avigad@16819
   344
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
avigad@16819
   345
  by (unfold natfloor_def, simp)
avigad@16819
   346
avigad@16819
   347
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
avigad@16819
   348
  by (unfold natfloor_def, simp)
avigad@16819
   349
avigad@16819
   350
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
avigad@16819
   351
  apply (unfold natfloor_def)
avigad@16819
   352
  apply (subgoal_tac "floor x <= floor 0")
avigad@16819
   353
  apply simp
huffman@30097
   354
  apply (erule floor_mono)
avigad@16819
   355
done
avigad@16819
   356
avigad@16819
   357
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
avigad@16819
   358
  apply (case_tac "0 <= x")
avigad@16819
   359
  apply (subst natfloor_def)+
avigad@16819
   360
  apply (subst nat_le_eq_zle)
avigad@16819
   361
  apply force
huffman@30097
   362
  apply (erule floor_mono)
avigad@16819
   363
  apply (subst natfloor_neg)
avigad@16819
   364
  apply simp
avigad@16819
   365
  apply simp
avigad@16819
   366
done
avigad@16819
   367
avigad@16819
   368
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
avigad@16819
   369
  apply (unfold natfloor_def)
avigad@16819
   370
  apply (subst nat_int [THEN sym])
avigad@16819
   371
  apply (subst nat_le_eq_zle)
avigad@16819
   372
  apply simp
avigad@16819
   373
  apply (rule le_floor)
avigad@16819
   374
  apply simp
avigad@16819
   375
done
avigad@16819
   376
hoelzl@35578
   377
lemma less_natfloor:
hoelzl@35578
   378
  assumes "0 \<le> x" and "x < real (n :: nat)"
hoelzl@35578
   379
  shows "natfloor x < n"
hoelzl@35578
   380
proof (rule ccontr)
hoelzl@35578
   381
  assume "\<not> ?thesis" hence *: "n \<le> natfloor x" by simp
hoelzl@35578
   382
  note assms(2)
hoelzl@35578
   383
  also have "real n \<le> real (natfloor x)"
hoelzl@35578
   384
    using * unfolding real_of_nat_le_iff .
hoelzl@35578
   385
  finally have "x < real (natfloor x)" .
hoelzl@35578
   386
  with real_natfloor_le[OF assms(1)]
hoelzl@35578
   387
  show False by auto
hoelzl@35578
   388
qed
hoelzl@35578
   389
avigad@16819
   390
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
avigad@16819
   391
  apply (rule iffI)
avigad@16819
   392
  apply (rule order_trans)
avigad@16819
   393
  prefer 2
avigad@16819
   394
  apply (erule real_natfloor_le)
avigad@16819
   395
  apply (subst real_of_nat_le_iff)
avigad@16819
   396
  apply assumption
avigad@16819
   397
  apply (erule le_natfloor)
avigad@16819
   398
done
avigad@16819
   399
wenzelm@16893
   400
lemma le_natfloor_eq_number_of [simp]:
avigad@16819
   401
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16819
   402
      (number_of n <= natfloor x) = (number_of n <= x)"
avigad@16819
   403
  apply (subst le_natfloor_eq, assumption)
avigad@16819
   404
  apply simp
avigad@16819
   405
done
avigad@16819
   406
avigad@16820
   407
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
avigad@16819
   408
  apply (case_tac "0 <= x")
avigad@16819
   409
  apply (subst le_natfloor_eq, assumption, simp)
avigad@16819
   410
  apply (rule iffI)
wenzelm@16893
   411
  apply (subgoal_tac "natfloor x <= natfloor 0")
avigad@16819
   412
  apply simp
avigad@16819
   413
  apply (rule natfloor_mono)
avigad@16819
   414
  apply simp
avigad@16819
   415
  apply simp
avigad@16819
   416
done
avigad@16819
   417
avigad@16819
   418
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
avigad@16819
   419
  apply (unfold natfloor_def)
hoelzl@35578
   420
  apply (subst (2) nat_int [THEN sym])
avigad@16819
   421
  apply (subst eq_nat_nat_iff)
avigad@16819
   422
  apply simp
avigad@16819
   423
  apply simp
avigad@16819
   424
  apply (rule floor_eq2)
avigad@16819
   425
  apply auto
avigad@16819
   426
done
avigad@16819
   427
avigad@16819
   428
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
avigad@16819
   429
  apply (case_tac "0 <= x")
avigad@16819
   430
  apply (unfold natfloor_def)
avigad@16819
   431
  apply simp
avigad@16819
   432
  apply simp_all
avigad@16819
   433
done
avigad@16819
   434
avigad@16819
   435
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
nipkow@29667
   436
using real_natfloor_add_one_gt by (simp add: algebra_simps)
avigad@16819
   437
avigad@16819
   438
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
avigad@16819
   439
  apply (subgoal_tac "z < real(natfloor z) + 1")
avigad@16819
   440
  apply arith
avigad@16819
   441
  apply (rule real_natfloor_add_one_gt)
avigad@16819
   442
done
avigad@16819
   443
avigad@16819
   444
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
avigad@16819
   445
  apply (unfold natfloor_def)
huffman@24355
   446
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   447
  apply (erule ssubst)
huffman@23309
   448
  apply (simp add: nat_add_distrib del: real_of_int_of_nat_eq)
avigad@16819
   449
  apply simp
avigad@16819
   450
done
avigad@16819
   451
wenzelm@16893
   452
lemma natfloor_add_number_of [simp]:
wenzelm@16893
   453
    "~neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16819
   454
      natfloor (x + number_of n) = natfloor x + number_of n"
avigad@16819
   455
  apply (subst natfloor_add [THEN sym])
avigad@16819
   456
  apply simp_all
avigad@16819
   457
done
avigad@16819
   458
avigad@16819
   459
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
avigad@16819
   460
  apply (subst natfloor_add [THEN sym])
avigad@16819
   461
  apply assumption
avigad@16819
   462
  apply simp
avigad@16819
   463
done
avigad@16819
   464
wenzelm@16893
   465
lemma natfloor_subtract [simp]: "real a <= x ==>
avigad@16819
   466
    natfloor(x - real a) = natfloor x - a"
avigad@16819
   467
  apply (unfold natfloor_def)
huffman@24355
   468
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   469
  apply (erule ssubst)
huffman@23309
   470
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   471
  apply simp
avigad@16819
   472
done
avigad@16819
   473
wenzelm@41550
   474
lemma natfloor_div_nat:
wenzelm@41550
   475
  assumes "1 <= x" and "y > 0"
wenzelm@41550
   476
  shows "natfloor (x / real y) = natfloor x div y"
hoelzl@35578
   477
proof -
hoelzl@35578
   478
  have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"
hoelzl@35578
   479
    by simp
hoelzl@35578
   480
  then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +
hoelzl@35578
   481
    real((natfloor x) mod y)"
hoelzl@35578
   482
    by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])
hoelzl@35578
   483
  have "x = real(natfloor x) + (x - real(natfloor x))"
hoelzl@35578
   484
    by simp
hoelzl@35578
   485
  then have "x = real ((natfloor x) div y) * real y +
hoelzl@35578
   486
      real((natfloor x) mod y) + (x - real(natfloor x))"
hoelzl@35578
   487
    by (simp add: a)
hoelzl@35578
   488
  then have "x / real y = ... / real y"
hoelzl@35578
   489
    by simp
hoelzl@35578
   490
  also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /
hoelzl@35578
   491
    real y + (x - real(natfloor x)) / real y"
wenzelm@41550
   492
    by (auto simp add: algebra_simps add_divide_distrib diff_divide_distrib)
hoelzl@35578
   493
  finally have "natfloor (x / real y) = natfloor(...)" by simp
hoelzl@35578
   494
  also have "... = natfloor(real((natfloor x) mod y) /
hoelzl@35578
   495
    real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"
hoelzl@35578
   496
    by (simp add: add_ac)
hoelzl@35578
   497
  also have "... = natfloor(real((natfloor x) mod y) /
hoelzl@35578
   498
    real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"
hoelzl@35578
   499
    apply (rule natfloor_add)
hoelzl@35578
   500
    apply (rule add_nonneg_nonneg)
hoelzl@35578
   501
    apply (rule divide_nonneg_pos)
hoelzl@35578
   502
    apply simp
wenzelm@41550
   503
    apply (simp add: assms)
hoelzl@35578
   504
    apply (rule divide_nonneg_pos)
hoelzl@35578
   505
    apply (simp add: algebra_simps)
hoelzl@35578
   506
    apply (rule real_natfloor_le)
wenzelm@41550
   507
    using assms apply auto
hoelzl@35578
   508
    done
hoelzl@35578
   509
  also have "natfloor(real((natfloor x) mod y) /
hoelzl@35578
   510
    real y + (x - real(natfloor x)) / real y) = 0"
hoelzl@35578
   511
    apply (rule natfloor_eq)
hoelzl@35578
   512
    apply simp
hoelzl@35578
   513
    apply (rule add_nonneg_nonneg)
hoelzl@35578
   514
    apply (rule divide_nonneg_pos)
hoelzl@35578
   515
    apply force
wenzelm@41550
   516
    apply (force simp add: assms)
hoelzl@35578
   517
    apply (rule divide_nonneg_pos)
hoelzl@35578
   518
    apply (simp add: algebra_simps)
hoelzl@35578
   519
    apply (rule real_natfloor_le)
wenzelm@41550
   520
    apply (auto simp add: assms)
wenzelm@41550
   521
    using assms apply arith
wenzelm@41550
   522
    using assms apply (simp add: add_divide_distrib [THEN sym])
hoelzl@35578
   523
    apply (subgoal_tac "real y = real y - 1 + 1")
hoelzl@35578
   524
    apply (erule ssubst)
hoelzl@35578
   525
    apply (rule add_le_less_mono)
hoelzl@35578
   526
    apply (simp add: algebra_simps)
hoelzl@35578
   527
    apply (subgoal_tac "1 + real(natfloor x mod y) =
hoelzl@35578
   528
      real(natfloor x mod y + 1)")
hoelzl@35578
   529
    apply (erule ssubst)
hoelzl@35578
   530
    apply (subst real_of_nat_le_iff)
hoelzl@35578
   531
    apply (subgoal_tac "natfloor x mod y < y")
hoelzl@35578
   532
    apply arith
hoelzl@35578
   533
    apply (rule mod_less_divisor)
hoelzl@35578
   534
    apply auto
hoelzl@35578
   535
    using real_natfloor_add_one_gt
hoelzl@35578
   536
    apply (simp add: algebra_simps)
hoelzl@35578
   537
    done
hoelzl@35578
   538
  finally show ?thesis by simp
hoelzl@35578
   539
qed
hoelzl@35578
   540
hoelzl@35578
   541
lemma le_mult_natfloor:
hoelzl@35578
   542
  assumes "0 \<le> (a :: real)" and "0 \<le> b"
hoelzl@35578
   543
  shows "natfloor a * natfloor b \<le> natfloor (a * b)"
hoelzl@35578
   544
  unfolding natfloor_def
hoelzl@35578
   545
  apply (subst nat_mult_distrib[symmetric])
hoelzl@35578
   546
  using assms apply simp
hoelzl@35578
   547
  apply (subst nat_le_eq_zle)
hoelzl@35578
   548
  using assms le_mult_floor by (auto intro!: mult_nonneg_nonneg)
hoelzl@35578
   549
avigad@16819
   550
lemma natceiling_zero [simp]: "natceiling 0 = 0"
avigad@16819
   551
  by (unfold natceiling_def, simp)
avigad@16819
   552
avigad@16819
   553
lemma natceiling_one [simp]: "natceiling 1 = 1"
avigad@16819
   554
  by (unfold natceiling_def, simp)
avigad@16819
   555
avigad@16819
   556
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
avigad@16819
   557
  by (unfold natceiling_def, simp)
avigad@16819
   558
avigad@16819
   559
lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
avigad@16819
   560
  by (unfold natceiling_def, simp)
avigad@16819
   561
avigad@16819
   562
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
avigad@16819
   563
  by (unfold natceiling_def, simp)
avigad@16819
   564
avigad@16819
   565
lemma real_natceiling_ge: "x <= real(natceiling x)"
avigad@16819
   566
  apply (unfold natceiling_def)
avigad@16819
   567
  apply (case_tac "x < 0")
avigad@16819
   568
  apply simp
avigad@16819
   569
  apply (subst real_nat_eq_real)
avigad@16819
   570
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
   571
  apply simp
huffman@30097
   572
  apply (rule ceiling_mono)
avigad@16819
   573
  apply simp
avigad@16819
   574
  apply simp
avigad@16819
   575
done
avigad@16819
   576
avigad@16819
   577
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
avigad@16819
   578
  apply (unfold natceiling_def)
avigad@16819
   579
  apply simp
avigad@16819
   580
done
avigad@16819
   581
avigad@16819
   582
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
avigad@16819
   583
  apply (case_tac "0 <= x")
avigad@16819
   584
  apply (subst natceiling_def)+
avigad@16819
   585
  apply (subst nat_le_eq_zle)
avigad@16819
   586
  apply (rule disjI2)
avigad@16819
   587
  apply (subgoal_tac "real (0::int) <= real(ceiling y)")
avigad@16819
   588
  apply simp
avigad@16819
   589
  apply (rule order_trans)
avigad@16819
   590
  apply simp
avigad@16819
   591
  apply (erule order_trans)
avigad@16819
   592
  apply simp
huffman@30097
   593
  apply (erule ceiling_mono)
avigad@16819
   594
  apply (subst natceiling_neg)
avigad@16819
   595
  apply simp_all
avigad@16819
   596
done
avigad@16819
   597
avigad@16819
   598
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
avigad@16819
   599
  apply (unfold natceiling_def)
avigad@16819
   600
  apply (case_tac "x < 0")
avigad@16819
   601
  apply simp
hoelzl@35578
   602
  apply (subst (2) nat_int [THEN sym])
avigad@16819
   603
  apply (subst nat_le_eq_zle)
avigad@16819
   604
  apply simp
avigad@16819
   605
  apply (rule ceiling_le)
avigad@16819
   606
  apply simp
avigad@16819
   607
done
avigad@16819
   608
avigad@16819
   609
lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"
avigad@16819
   610
  apply (rule iffI)
avigad@16819
   611
  apply (rule order_trans)
avigad@16819
   612
  apply (rule real_natceiling_ge)
avigad@16819
   613
  apply (subst real_of_nat_le_iff)
avigad@16819
   614
  apply assumption
avigad@16819
   615
  apply (erule natceiling_le)
avigad@16819
   616
done
avigad@16819
   617
wenzelm@16893
   618
lemma natceiling_le_eq_number_of [simp]:
avigad@16820
   619
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16820
   620
      (natceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
   621
  apply (subst natceiling_le_eq, assumption)
avigad@16819
   622
  apply simp
avigad@16819
   623
done
avigad@16819
   624
avigad@16820
   625
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
avigad@16819
   626
  apply (case_tac "0 <= x")
avigad@16819
   627
  apply (subst natceiling_le_eq)
avigad@16819
   628
  apply assumption
avigad@16819
   629
  apply simp
avigad@16819
   630
  apply (subst natceiling_neg)
avigad@16819
   631
  apply simp
avigad@16819
   632
  apply simp
avigad@16819
   633
done
avigad@16819
   634
avigad@16819
   635
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
avigad@16819
   636
  apply (unfold natceiling_def)
wenzelm@19850
   637
  apply (simplesubst nat_int [THEN sym]) back back
avigad@16819
   638
  apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")
avigad@16819
   639
  apply (erule ssubst)
avigad@16819
   640
  apply (subst eq_nat_nat_iff)
avigad@16819
   641
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
   642
  apply simp
huffman@30097
   643
  apply (rule ceiling_mono)
avigad@16819
   644
  apply force
avigad@16819
   645
  apply force
avigad@16819
   646
  apply (rule ceiling_eq2)
avigad@16819
   647
  apply (simp, simp)
avigad@16819
   648
  apply (subst nat_add_distrib)
avigad@16819
   649
  apply auto
avigad@16819
   650
done
avigad@16819
   651
wenzelm@16893
   652
lemma natceiling_add [simp]: "0 <= x ==>
avigad@16819
   653
    natceiling (x + real a) = natceiling x + a"
avigad@16819
   654
  apply (unfold natceiling_def)
huffman@24355
   655
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   656
  apply (erule ssubst)
huffman@23309
   657
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   658
  apply (subst nat_add_distrib)
avigad@16819
   659
  apply (subgoal_tac "0 = ceiling 0")
avigad@16819
   660
  apply (erule ssubst)
huffman@30097
   661
  apply (erule ceiling_mono)
avigad@16819
   662
  apply simp_all
avigad@16819
   663
done
avigad@16819
   664
wenzelm@16893
   665
lemma natceiling_add_number_of [simp]:
wenzelm@16893
   666
    "~ neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16820
   667
      natceiling (x + number_of n) = natceiling x + number_of n"
avigad@16819
   668
  apply (subst natceiling_add [THEN sym])
avigad@16819
   669
  apply simp_all
avigad@16819
   670
done
avigad@16819
   671
avigad@16819
   672
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
avigad@16819
   673
  apply (subst natceiling_add [THEN sym])
avigad@16819
   674
  apply assumption
avigad@16819
   675
  apply simp
avigad@16819
   676
done
avigad@16819
   677
wenzelm@16893
   678
lemma natceiling_subtract [simp]: "real a <= x ==>
avigad@16819
   679
    natceiling(x - real a) = natceiling x - a"
avigad@16819
   680
  apply (unfold natceiling_def)
huffman@24355
   681
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   682
  apply (erule ssubst)
huffman@23309
   683
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   684
  apply simp
avigad@16819
   685
done
avigad@16819
   686
huffman@36826
   687
subsection {* Exponentiation with floor *}
huffman@36826
   688
huffman@36826
   689
lemma floor_power:
huffman@36826
   690
  assumes "x = real (floor x)"
huffman@36826
   691
  shows "floor (x ^ n) = floor x ^ n"
huffman@36826
   692
proof -
huffman@36826
   693
  have *: "x ^ n = real (floor x ^ n)"
huffman@36826
   694
    using assms by (induct n arbitrary: x) simp_all
huffman@36826
   695
  show ?thesis unfolding real_of_int_inject[symmetric]
huffman@36826
   696
    unfolding * floor_real_of_int ..
huffman@36826
   697
qed
huffman@36826
   698
huffman@36826
   699
lemma natfloor_power:
huffman@36826
   700
  assumes "x = real (natfloor x)"
huffman@36826
   701
  shows "natfloor (x ^ n) = natfloor x ^ n"
huffman@36826
   702
proof -
huffman@36826
   703
  from assms have "0 \<le> floor x" by auto
huffman@36826
   704
  note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
huffman@36826
   705
  from floor_power[OF this]
huffman@36826
   706
  show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
huffman@36826
   707
    by simp
huffman@36826
   708
qed
avigad@16819
   709
paulson@14365
   710
end