src/HOL/RComplete.thy
author webertj
Fri Oct 19 15:12:52 2012 +0200 (2012-10-19)
changeset 49962 a8cc904a6820
parent 47596 c031e65c8ddc
child 51518 6a56b7088a6a
permissions -rw-r--r--
Renamed {left,right}_distrib to distrib_{right,left}.
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:
huffman@44690
    36
  fixes P :: "real set"
huffman@44690
    37
  assumes 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
haftmann@45966
    79
    by (simp add: isLub_def leastP_def isUb_def setle_def setge_def)
wenzelm@16893
    80
qed
paulson@14365
    81
paulson@14365
    82
wenzelm@16893
    83
subsection {* The Archimedean Property of the Reals *}
wenzelm@16893
    84
wenzelm@16893
    85
theorem reals_Archimedean:
wenzelm@16893
    86
  assumes x_pos: "0 < x"
wenzelm@16893
    87
  shows "\<exists>n. inverse (real (Suc n)) < x"
huffman@36795
    88
  unfolding real_of_nat_def using x_pos
huffman@36795
    89
  by (rule ex_inverse_of_nat_Suc_less)
paulson@14365
    90
paulson@14365
    91
lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
huffman@36795
    92
  unfolding real_of_nat_def by (rule ex_less_of_nat)
huffman@30097
    93
wenzelm@16893
    94
lemma reals_Archimedean3:
wenzelm@16893
    95
  assumes x_greater_zero: "0 < x"
wenzelm@16893
    96
  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
huffman@30097
    97
  unfolding real_of_nat_def using `0 < x`
huffman@30097
    98
  by (auto intro: ex_less_of_nat_mult)
paulson@14365
    99
avigad@16819
   100
nipkow@28091
   101
subsection{*Density of the Rational Reals in the Reals*}
nipkow@28091
   102
nipkow@28091
   103
text{* This density proof is due to Stefan Richter and was ported by TN.  The
nipkow@28091
   104
original source is \emph{Real Analysis} by H.L. Royden.
nipkow@28091
   105
It employs the Archimedean property of the reals. *}
nipkow@28091
   106
huffman@44668
   107
lemma Rats_dense_in_real:
huffman@44668
   108
  fixes x :: real
huffman@44668
   109
  assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
nipkow@28091
   110
proof -
nipkow@28091
   111
  from `x<y` have "0 < y-x" by simp
nipkow@28091
   112
  with reals_Archimedean obtain q::nat 
huffman@44668
   113
    where q: "inverse (real q) < y-x" and "0 < q" by auto
huffman@44668
   114
  def p \<equiv> "ceiling (y * real q) - 1"
huffman@44668
   115
  def r \<equiv> "of_int p / real q"
huffman@44668
   116
  from q have "x < y - inverse (real q)" by simp
huffman@44668
   117
  also have "y - inverse (real q) \<le> r"
huffman@44668
   118
    unfolding r_def p_def
huffman@44668
   119
    by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
huffman@44668
   120
  finally have "x < r" .
huffman@44668
   121
  moreover have "r < y"
huffman@44668
   122
    unfolding r_def p_def
huffman@44668
   123
    by (simp add: divide_less_eq diff_less_eq `0 < q`
huffman@44668
   124
      less_ceiling_iff [symmetric])
huffman@44668
   125
  moreover from r_def have "r \<in> \<rat>" by simp
nipkow@28091
   126
  ultimately show ?thesis by fast
nipkow@28091
   127
qed
nipkow@28091
   128
nipkow@28091
   129
paulson@14641
   130
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
paulson@14641
   131
huffman@47108
   132
(* FIXME: theorems for negative numerals *)
huffman@47108
   133
lemma numeral_less_real_of_int_iff [simp]:
huffman@47108
   134
     "((numeral n) < real (m::int)) = (numeral n < m)"
paulson@14641
   135
apply auto
paulson@14641
   136
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   137
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   138
done
paulson@14641
   139
huffman@47108
   140
lemma numeral_less_real_of_int_iff2 [simp]:
huffman@47108
   141
     "(real (m::int) < (numeral n)) = (m < numeral n)"
paulson@14641
   142
apply auto
paulson@14641
   143
apply (rule real_of_int_less_iff [THEN iffD1])
paulson@14641
   144
apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
paulson@14641
   145
done
paulson@14641
   146
huffman@47108
   147
lemma numeral_le_real_of_int_iff [simp]:
huffman@47108
   148
     "((numeral n) \<le> real (m::int)) = (numeral n \<le> m)"
paulson@14641
   149
by (simp add: linorder_not_less [symmetric])
paulson@14641
   150
huffman@47108
   151
lemma numeral_le_real_of_int_iff2 [simp]:
huffman@47108
   152
     "(real (m::int) \<le> (numeral n)) = (m \<le> numeral n)"
paulson@14641
   153
by (simp add: linorder_not_less [symmetric])
paulson@14641
   154
huffman@24355
   155
lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
huffman@30097
   156
unfolding real_of_nat_def by simp
paulson@14641
   157
huffman@24355
   158
lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
huffman@30102
   159
unfolding real_of_nat_def by (simp add: floor_minus)
paulson@14641
   160
paulson@14641
   161
lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
huffman@30097
   162
unfolding real_of_int_def by simp
paulson@14641
   163
paulson@14641
   164
lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
huffman@30102
   165
unfolding real_of_int_def by (simp add: floor_minus)
paulson@14641
   166
paulson@14641
   167
lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
huffman@30097
   168
unfolding real_of_int_def by (rule floor_exists)
paulson@14641
   169
paulson@14641
   170
lemma lemma_floor:
paulson@14641
   171
  assumes a1: "real m \<le> r" and a2: "r < real n + 1"
paulson@14641
   172
  shows "m \<le> (n::int)"
paulson@14641
   173
proof -
wenzelm@23389
   174
  have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)
wenzelm@23389
   175
  also have "... = real (n + 1)" by simp
wenzelm@23389
   176
  finally have "m < n + 1" by (simp only: real_of_int_less_iff)
paulson@14641
   177
  thus ?thesis by arith
paulson@14641
   178
qed
paulson@14641
   179
paulson@14641
   180
lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
huffman@30097
   181
unfolding real_of_int_def by (rule of_int_floor_le)
paulson@14641
   182
paulson@14641
   183
lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
paulson@14641
   184
by (auto intro: lemma_floor)
paulson@14641
   185
paulson@14641
   186
lemma real_of_int_floor_cancel [simp]:
paulson@14641
   187
    "(real (floor x) = x) = (\<exists>n::int. x = real n)"
huffman@30097
   188
  using floor_real_of_int by metis
paulson@14641
   189
paulson@14641
   190
lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
huffman@30097
   191
  unfolding real_of_int_def using floor_unique [of n x] by simp
paulson@14641
   192
paulson@14641
   193
lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
huffman@30097
   194
  unfolding real_of_int_def by (rule floor_unique)
paulson@14641
   195
paulson@14641
   196
lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   197
apply (rule inj_int [THEN injD])
paulson@14641
   198
apply (simp add: real_of_nat_Suc)
nipkow@15539
   199
apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
paulson@14641
   200
done
paulson@14641
   201
paulson@14641
   202
lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
paulson@14641
   203
apply (drule order_le_imp_less_or_eq)
paulson@14641
   204
apply (auto intro: floor_eq3)
paulson@14641
   205
done
paulson@14641
   206
paulson@14641
   207
lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
huffman@30097
   208
  unfolding real_of_int_def using floor_correct [of r] by simp
avigad@16819
   209
avigad@16819
   210
lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
huffman@30097
   211
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   212
paulson@14641
   213
lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
huffman@30097
   214
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   215
avigad@16819
   216
lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
huffman@30097
   217
  unfolding real_of_int_def using floor_correct [of r] by simp
paulson@14641
   218
avigad@16819
   219
lemma le_floor: "real a <= x ==> a <= floor x"
huffman@30097
   220
  unfolding real_of_int_def by (simp add: le_floor_iff)
avigad@16819
   221
avigad@16819
   222
lemma real_le_floor: "a <= floor x ==> real a <= x"
huffman@30097
   223
  unfolding real_of_int_def by (simp add: le_floor_iff)
avigad@16819
   224
avigad@16819
   225
lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
huffman@30097
   226
  unfolding real_of_int_def by (rule le_floor_iff)
avigad@16819
   227
avigad@16819
   228
lemma floor_less_eq: "(floor x < a) = (x < real a)"
huffman@30097
   229
  unfolding real_of_int_def by (rule floor_less_iff)
avigad@16819
   230
avigad@16819
   231
lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
huffman@30097
   232
  unfolding real_of_int_def by (rule less_floor_iff)
avigad@16819
   233
avigad@16819
   234
lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
huffman@30097
   235
  unfolding real_of_int_def by (rule floor_le_iff)
avigad@16819
   236
avigad@16819
   237
lemma floor_add [simp]: "floor (x + real a) = floor x + a"
huffman@30097
   238
  unfolding real_of_int_def by (rule floor_add_of_int)
avigad@16819
   239
avigad@16819
   240
lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
huffman@30097
   241
  unfolding real_of_int_def by (rule floor_diff_of_int)
avigad@16819
   242
hoelzl@35578
   243
lemma le_mult_floor:
hoelzl@35578
   244
  assumes "0 \<le> (a :: real)" and "0 \<le> b"
hoelzl@35578
   245
  shows "floor a * floor b \<le> floor (a * b)"
hoelzl@35578
   246
proof -
hoelzl@35578
   247
  have "real (floor a) \<le> a"
hoelzl@35578
   248
    and "real (floor b) \<le> b" by auto
hoelzl@35578
   249
  hence "real (floor a * floor b) \<le> a * b"
hoelzl@35578
   250
    using assms by (auto intro!: mult_mono)
hoelzl@35578
   251
  also have "a * b < real (floor (a * b) + 1)" by auto
hoelzl@35578
   252
  finally show ?thesis unfolding real_of_int_less_iff by simp
hoelzl@35578
   253
qed
hoelzl@35578
   254
hoelzl@47596
   255
lemma floor_divide_eq_div:
hoelzl@47596
   256
  "floor (real a / real b) = a div b"
hoelzl@47596
   257
proof cases
hoelzl@47596
   258
  assume "b \<noteq> 0 \<or> b dvd a"
hoelzl@47596
   259
  with real_of_int_div3[of a b] show ?thesis
hoelzl@47596
   260
    by (auto simp: real_of_int_div[symmetric] intro!: floor_eq2 real_of_int_div4 neq_le_trans)
hoelzl@47596
   261
       (metis add_left_cancel zero_neq_one real_of_int_div_aux real_of_int_inject
hoelzl@47596
   262
              real_of_int_zero_cancel right_inverse_eq div_self mod_div_trivial)
hoelzl@47596
   263
qed (auto simp: real_of_int_div)
hoelzl@47596
   264
huffman@24355
   265
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
huffman@30097
   266
  unfolding real_of_nat_def by simp
paulson@14641
   267
paulson@14641
   268
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
huffman@30097
   269
  unfolding real_of_int_def by (rule le_of_int_ceiling)
paulson@14641
   270
huffman@30097
   271
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
huffman@30097
   272
  unfolding real_of_int_def by simp
paulson@14641
   273
paulson@14641
   274
lemma real_of_int_ceiling_cancel [simp]:
paulson@14641
   275
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
huffman@30097
   276
  using ceiling_real_of_int by metis
paulson@14641
   277
paulson@14641
   278
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   279
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   280
paulson@14641
   281
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   282
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   283
paulson@14641
   284
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
huffman@30097
   285
  unfolding real_of_int_def using ceiling_unique [of n x] by simp
paulson@14641
   286
paulson@14641
   287
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
huffman@30097
   288
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   289
paulson@14641
   290
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
huffman@30097
   291
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   292
avigad@16819
   293
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
huffman@30097
   294
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   295
avigad@16819
   296
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
huffman@30097
   297
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   298
avigad@16819
   299
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
huffman@30097
   300
  unfolding real_of_int_def by (rule ceiling_le_iff)
avigad@16819
   301
avigad@16819
   302
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
huffman@30097
   303
  unfolding real_of_int_def by (rule less_ceiling_iff)
avigad@16819
   304
avigad@16819
   305
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
huffman@30097
   306
  unfolding real_of_int_def by (rule ceiling_less_iff)
avigad@16819
   307
avigad@16819
   308
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
huffman@30097
   309
  unfolding real_of_int_def by (rule le_ceiling_iff)
avigad@16819
   310
avigad@16819
   311
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
huffman@30097
   312
  unfolding real_of_int_def by (rule ceiling_add_of_int)
avigad@16819
   313
avigad@16819
   314
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
huffman@30097
   315
  unfolding real_of_int_def by (rule ceiling_diff_of_int)
avigad@16819
   316
avigad@16819
   317
avigad@16819
   318
subsection {* Versions for the natural numbers *}
avigad@16819
   319
wenzelm@19765
   320
definition
wenzelm@21404
   321
  natfloor :: "real => nat" where
wenzelm@19765
   322
  "natfloor x = nat(floor x)"
wenzelm@21404
   323
wenzelm@21404
   324
definition
wenzelm@21404
   325
  natceiling :: "real => nat" where
wenzelm@19765
   326
  "natceiling x = nat(ceiling x)"
avigad@16819
   327
avigad@16819
   328
lemma natfloor_zero [simp]: "natfloor 0 = 0"
avigad@16819
   329
  by (unfold natfloor_def, simp)
avigad@16819
   330
avigad@16819
   331
lemma natfloor_one [simp]: "natfloor 1 = 1"
avigad@16819
   332
  by (unfold natfloor_def, simp)
avigad@16819
   333
avigad@16819
   334
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
avigad@16819
   335
  by (unfold natfloor_def, simp)
avigad@16819
   336
huffman@47108
   337
lemma natfloor_numeral_eq [simp]: "natfloor (numeral n) = numeral n"
avigad@16819
   338
  by (unfold natfloor_def, simp)
avigad@16819
   339
avigad@16819
   340
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
avigad@16819
   341
  by (unfold natfloor_def, simp)
avigad@16819
   342
avigad@16819
   343
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
avigad@16819
   344
  by (unfold natfloor_def, simp)
avigad@16819
   345
avigad@16819
   346
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
huffman@44679
   347
  unfolding natfloor_def by simp
huffman@44679
   348
avigad@16819
   349
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
huffman@44679
   350
  unfolding natfloor_def by (intro nat_mono floor_mono)
avigad@16819
   351
avigad@16819
   352
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
avigad@16819
   353
  apply (unfold natfloor_def)
avigad@16819
   354
  apply (subst nat_int [THEN sym])
huffman@44679
   355
  apply (rule nat_mono)
avigad@16819
   356
  apply (rule le_floor)
avigad@16819
   357
  apply simp
avigad@16819
   358
done
avigad@16819
   359
huffman@44679
   360
lemma natfloor_less_iff: "0 \<le> x \<Longrightarrow> natfloor x < n \<longleftrightarrow> x < real n"
huffman@44679
   361
  unfolding natfloor_def real_of_nat_def
huffman@44679
   362
  by (simp add: nat_less_iff floor_less_iff)
huffman@44679
   363
hoelzl@35578
   364
lemma less_natfloor:
hoelzl@35578
   365
  assumes "0 \<le> x" and "x < real (n :: nat)"
hoelzl@35578
   366
  shows "natfloor x < n"
huffman@44679
   367
  using assms by (simp add: natfloor_less_iff)
hoelzl@35578
   368
avigad@16819
   369
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
avigad@16819
   370
  apply (rule iffI)
avigad@16819
   371
  apply (rule order_trans)
avigad@16819
   372
  prefer 2
avigad@16819
   373
  apply (erule real_natfloor_le)
avigad@16819
   374
  apply (subst real_of_nat_le_iff)
avigad@16819
   375
  apply assumption
avigad@16819
   376
  apply (erule le_natfloor)
avigad@16819
   377
done
avigad@16819
   378
huffman@47108
   379
lemma le_natfloor_eq_numeral [simp]:
huffman@47108
   380
    "~ neg((numeral n)::int) ==> 0 <= x ==>
huffman@47108
   381
      (numeral n <= natfloor x) = (numeral n <= x)"
avigad@16819
   382
  apply (subst le_natfloor_eq, assumption)
avigad@16819
   383
  apply simp
avigad@16819
   384
done
avigad@16819
   385
avigad@16820
   386
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
avigad@16819
   387
  apply (case_tac "0 <= x")
avigad@16819
   388
  apply (subst le_natfloor_eq, assumption, simp)
avigad@16819
   389
  apply (rule iffI)
wenzelm@16893
   390
  apply (subgoal_tac "natfloor x <= natfloor 0")
avigad@16819
   391
  apply simp
avigad@16819
   392
  apply (rule natfloor_mono)
avigad@16819
   393
  apply simp
avigad@16819
   394
  apply simp
avigad@16819
   395
done
avigad@16819
   396
avigad@16819
   397
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
huffman@44679
   398
  unfolding natfloor_def by (simp add: floor_eq2 [where n="int n"])
avigad@16819
   399
avigad@16819
   400
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
avigad@16819
   401
  apply (case_tac "0 <= x")
avigad@16819
   402
  apply (unfold natfloor_def)
avigad@16819
   403
  apply simp
avigad@16819
   404
  apply simp_all
avigad@16819
   405
done
avigad@16819
   406
avigad@16819
   407
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
nipkow@29667
   408
using real_natfloor_add_one_gt by (simp add: algebra_simps)
avigad@16819
   409
avigad@16819
   410
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
avigad@16819
   411
  apply (subgoal_tac "z < real(natfloor z) + 1")
avigad@16819
   412
  apply arith
avigad@16819
   413
  apply (rule real_natfloor_add_one_gt)
avigad@16819
   414
done
avigad@16819
   415
avigad@16819
   416
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
huffman@44679
   417
  unfolding natfloor_def
huffman@44679
   418
  unfolding real_of_int_of_nat_eq [symmetric] floor_add
huffman@44679
   419
  by (simp add: nat_add_distrib)
avigad@16819
   420
huffman@47108
   421
lemma natfloor_add_numeral [simp]:
huffman@47108
   422
    "~neg ((numeral n)::int) ==> 0 <= x ==>
huffman@47108
   423
      natfloor (x + numeral n) = natfloor x + numeral n"
huffman@44679
   424
  by (simp add: natfloor_add [symmetric])
avigad@16819
   425
avigad@16819
   426
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
huffman@44679
   427
  by (simp add: natfloor_add [symmetric] del: One_nat_def)
avigad@16819
   428
bulwahn@46671
   429
lemma natfloor_subtract [simp]:
bulwahn@46671
   430
    "natfloor(x - real a) = natfloor x - a"
bulwahn@46671
   431
  unfolding natfloor_def real_of_int_of_nat_eq [symmetric] floor_subtract
huffman@44679
   432
  by simp
avigad@16819
   433
wenzelm@41550
   434
lemma natfloor_div_nat:
wenzelm@41550
   435
  assumes "1 <= x" and "y > 0"
wenzelm@41550
   436
  shows "natfloor (x / real y) = natfloor x div y"
huffman@44679
   437
proof (rule natfloor_eq)
huffman@44679
   438
  have "(natfloor x) div y * y \<le> natfloor x"
huffman@44679
   439
    by (rule add_leD1 [where k="natfloor x mod y"], simp)
huffman@44679
   440
  thus "real (natfloor x div y) \<le> x / real y"
huffman@44679
   441
    using assms by (simp add: le_divide_eq le_natfloor_eq)
huffman@44679
   442
  have "natfloor x < (natfloor x) div y * y + y"
huffman@44679
   443
    apply (subst mod_div_equality [symmetric])
huffman@44679
   444
    apply (rule add_strict_left_mono)
huffman@44679
   445
    apply (rule mod_less_divisor)
huffman@44679
   446
    apply fact
hoelzl@35578
   447
    done
huffman@44679
   448
  thus "x / real y < real (natfloor x div y) + 1"
huffman@44679
   449
    using assms
webertj@49962
   450
    by (simp add: divide_less_eq natfloor_less_iff distrib_right)
hoelzl@35578
   451
qed
hoelzl@35578
   452
hoelzl@35578
   453
lemma le_mult_natfloor:
hoelzl@35578
   454
  shows "natfloor a * natfloor b \<le> natfloor (a * b)"
bulwahn@46671
   455
  by (cases "0 <= a & 0 <= b")
bulwahn@46671
   456
    (auto simp add: le_natfloor_eq mult_nonneg_nonneg mult_mono' real_natfloor_le natfloor_neg)
hoelzl@35578
   457
avigad@16819
   458
lemma natceiling_zero [simp]: "natceiling 0 = 0"
avigad@16819
   459
  by (unfold natceiling_def, simp)
avigad@16819
   460
avigad@16819
   461
lemma natceiling_one [simp]: "natceiling 1 = 1"
avigad@16819
   462
  by (unfold natceiling_def, simp)
avigad@16819
   463
avigad@16819
   464
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
avigad@16819
   465
  by (unfold natceiling_def, simp)
avigad@16819
   466
huffman@47108
   467
lemma natceiling_numeral_eq [simp]: "natceiling (numeral n) = numeral n"
avigad@16819
   468
  by (unfold natceiling_def, simp)
avigad@16819
   469
avigad@16819
   470
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
avigad@16819
   471
  by (unfold natceiling_def, simp)
avigad@16819
   472
avigad@16819
   473
lemma real_natceiling_ge: "x <= real(natceiling x)"
huffman@44679
   474
  unfolding natceiling_def by (cases "x < 0", simp_all)
avigad@16819
   475
avigad@16819
   476
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
huffman@44679
   477
  unfolding natceiling_def by simp
avigad@16819
   478
avigad@16819
   479
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
huffman@44679
   480
  unfolding natceiling_def by (intro nat_mono ceiling_mono)
huffman@44679
   481
avigad@16819
   482
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
huffman@44679
   483
  unfolding natceiling_def real_of_nat_def
huffman@44679
   484
  by (simp add: nat_le_iff ceiling_le_iff)
avigad@16819
   485
huffman@44708
   486
lemma natceiling_le_eq: "(natceiling x <= a) = (x <= real a)"
huffman@44708
   487
  unfolding natceiling_def real_of_nat_def
huffman@44679
   488
  by (simp add: nat_le_iff ceiling_le_iff)
avigad@16819
   489
huffman@47108
   490
lemma natceiling_le_eq_numeral [simp]:
huffman@47108
   491
    "~ neg((numeral n)::int) ==>
huffman@47108
   492
      (natceiling x <= numeral n) = (x <= numeral n)"
huffman@44679
   493
  by (simp add: natceiling_le_eq)
avigad@16819
   494
avigad@16820
   495
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
huffman@44679
   496
  unfolding natceiling_def
huffman@44679
   497
  by (simp add: nat_le_iff ceiling_le_iff)
avigad@16819
   498
avigad@16819
   499
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
huffman@44679
   500
  unfolding natceiling_def
huffman@44679
   501
  by (simp add: ceiling_eq2 [where n="int n"])
avigad@16819
   502
wenzelm@16893
   503
lemma natceiling_add [simp]: "0 <= x ==>
avigad@16819
   504
    natceiling (x + real a) = natceiling x + a"
huffman@44679
   505
  unfolding natceiling_def
huffman@44679
   506
  unfolding real_of_int_of_nat_eq [symmetric] ceiling_add
huffman@44679
   507
  by (simp add: nat_add_distrib)
avigad@16819
   508
huffman@47108
   509
lemma natceiling_add_numeral [simp]:
huffman@47108
   510
    "~ neg ((numeral n)::int) ==> 0 <= x ==>
huffman@47108
   511
      natceiling (x + numeral n) = natceiling x + numeral n"
huffman@44679
   512
  by (simp add: natceiling_add [symmetric])
avigad@16819
   513
avigad@16819
   514
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
huffman@44679
   515
  by (simp add: natceiling_add [symmetric] del: One_nat_def)
avigad@16819
   516
bulwahn@46671
   517
lemma natceiling_subtract [simp]: "natceiling(x - real a) = natceiling x - a"
bulwahn@46671
   518
  unfolding natceiling_def real_of_int_of_nat_eq [symmetric] ceiling_subtract
huffman@44679
   519
  by simp
avigad@16819
   520
huffman@36826
   521
subsection {* Exponentiation with floor *}
huffman@36826
   522
huffman@36826
   523
lemma floor_power:
huffman@36826
   524
  assumes "x = real (floor x)"
huffman@36826
   525
  shows "floor (x ^ n) = floor x ^ n"
huffman@36826
   526
proof -
huffman@36826
   527
  have *: "x ^ n = real (floor x ^ n)"
huffman@36826
   528
    using assms by (induct n arbitrary: x) simp_all
huffman@36826
   529
  show ?thesis unfolding real_of_int_inject[symmetric]
huffman@36826
   530
    unfolding * floor_real_of_int ..
huffman@36826
   531
qed
huffman@36826
   532
huffman@36826
   533
lemma natfloor_power:
huffman@36826
   534
  assumes "x = real (natfloor x)"
huffman@36826
   535
  shows "natfloor (x ^ n) = natfloor x ^ n"
huffman@36826
   536
proof -
huffman@36826
   537
  from assms have "0 \<le> floor x" by auto
huffman@36826
   538
  note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
huffman@36826
   539
  from floor_power[OF this]
huffman@36826
   540
  show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
huffman@36826
   541
    by simp
huffman@36826
   542
qed
avigad@16819
   543
paulson@14365
   544
end