src/HOL/RComplete.thy
author huffman
Fri Sep 02 16:48:30 2011 -0700 (2011-09-02)
changeset 44669 8e6cdb9c00a7
parent 44668 31d41a0f6b5d
child 44678 21eb31192850
permissions -rw-r--r--
remove redundant lemma reals_complete2 in favor of complete_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@14365
    83
wenzelm@16893
    84
subsection {* The Archimedean Property of the Reals *}
wenzelm@16893
    85
wenzelm@16893
    86
theorem reals_Archimedean:
wenzelm@16893
    87
  assumes x_pos: "0 < x"
wenzelm@16893
    88
  shows "\<exists>n. inverse (real (Suc n)) < x"
huffman@36795
    89
  unfolding real_of_nat_def using x_pos
huffman@36795
    90
  by (rule ex_inverse_of_nat_Suc_less)
paulson@14365
    91
paulson@14365
    92
lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
huffman@36795
    93
  unfolding real_of_nat_def by (rule ex_less_of_nat)
huffman@30097
    94
wenzelm@16893
    95
lemma reals_Archimedean3:
wenzelm@16893
    96
  assumes x_greater_zero: "0 < x"
wenzelm@16893
    97
  shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
huffman@30097
    98
  unfolding real_of_nat_def using `0 < x`
huffman@30097
    99
  by (auto intro: ex_less_of_nat_mult)
paulson@14365
   100
avigad@16819
   101
nipkow@28091
   102
subsection{*Density of the Rational Reals in the Reals*}
nipkow@28091
   103
nipkow@28091
   104
text{* This density proof is due to Stefan Richter and was ported by TN.  The
nipkow@28091
   105
original source is \emph{Real Analysis} by H.L. Royden.
nipkow@28091
   106
It employs the Archimedean property of the reals. *}
nipkow@28091
   107
huffman@44668
   108
lemma Rats_dense_in_real:
huffman@44668
   109
  fixes x :: real
huffman@44668
   110
  assumes "x < y" shows "\<exists>r\<in>\<rat>. x < r \<and> r < y"
nipkow@28091
   111
proof -
nipkow@28091
   112
  from `x<y` have "0 < y-x" by simp
nipkow@28091
   113
  with reals_Archimedean obtain q::nat 
huffman@44668
   114
    where q: "inverse (real q) < y-x" and "0 < q" by auto
huffman@44668
   115
  def p \<equiv> "ceiling (y * real q) - 1"
huffman@44668
   116
  def r \<equiv> "of_int p / real q"
huffman@44668
   117
  from q have "x < y - inverse (real q)" by simp
huffman@44668
   118
  also have "y - inverse (real q) \<le> r"
huffman@44668
   119
    unfolding r_def p_def
huffman@44668
   120
    by (simp add: le_divide_eq left_diff_distrib le_of_int_ceiling `0 < q`)
huffman@44668
   121
  finally have "x < r" .
huffman@44668
   122
  moreover have "r < y"
huffman@44668
   123
    unfolding r_def p_def
huffman@44668
   124
    by (simp add: divide_less_eq diff_less_eq `0 < q`
huffman@44668
   125
      less_ceiling_iff [symmetric])
huffman@44668
   126
  moreover from r_def have "r \<in> \<rat>" by simp
nipkow@28091
   127
  ultimately show ?thesis by fast
nipkow@28091
   128
qed
nipkow@28091
   129
nipkow@28091
   130
paulson@14641
   131
subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
paulson@14641
   132
paulson@14641
   133
lemma number_of_less_real_of_int_iff [simp]:
paulson@14641
   134
     "((number_of n) < real (m::int)) = (number_of 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
paulson@14641
   140
lemma number_of_less_real_of_int_iff2 [simp]:
paulson@14641
   141
     "(real (m::int) < (number_of n)) = (m < number_of 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
paulson@14641
   147
lemma number_of_le_real_of_int_iff [simp]:
paulson@14641
   148
     "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
paulson@14641
   149
by (simp add: linorder_not_less [symmetric])
paulson@14641
   150
paulson@14641
   151
lemma number_of_le_real_of_int_iff2 [simp]:
paulson@14641
   152
     "(real (m::int) \<le> (number_of n)) = (m \<le> number_of 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
huffman@24355
   255
lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
huffman@30097
   256
  unfolding real_of_nat_def by simp
paulson@14641
   257
paulson@14641
   258
lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"
huffman@30097
   259
  unfolding real_of_int_def by simp
paulson@14641
   260
paulson@14641
   261
lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"
huffman@30097
   262
  unfolding real_of_int_def by simp
paulson@14641
   263
paulson@14641
   264
lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
huffman@30097
   265
  unfolding real_of_int_def by (rule le_of_int_ceiling)
paulson@14641
   266
huffman@30097
   267
lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
huffman@30097
   268
  unfolding real_of_int_def by simp
paulson@14641
   269
paulson@14641
   270
lemma real_of_int_ceiling_cancel [simp]:
paulson@14641
   271
     "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
huffman@30097
   272
  using ceiling_real_of_int by metis
paulson@14641
   273
paulson@14641
   274
lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   275
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   276
paulson@14641
   277
lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
huffman@30097
   278
  unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp
paulson@14641
   279
paulson@14641
   280
lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
huffman@30097
   281
  unfolding real_of_int_def using ceiling_unique [of n x] by simp
paulson@14641
   282
paulson@14641
   283
lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
huffman@30097
   284
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   285
paulson@14641
   286
lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
huffman@30097
   287
  unfolding real_of_int_def using ceiling_correct [of r] by simp
paulson@14641
   288
avigad@16819
   289
lemma ceiling_le: "x <= real a ==> ceiling x <= a"
huffman@30097
   290
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   291
avigad@16819
   292
lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
huffman@30097
   293
  unfolding real_of_int_def by (simp add: ceiling_le_iff)
avigad@16819
   294
avigad@16819
   295
lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
huffman@30097
   296
  unfolding real_of_int_def by (rule ceiling_le_iff)
avigad@16819
   297
avigad@16819
   298
lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
huffman@30097
   299
  unfolding real_of_int_def by (rule less_ceiling_iff)
avigad@16819
   300
avigad@16819
   301
lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
huffman@30097
   302
  unfolding real_of_int_def by (rule ceiling_less_iff)
avigad@16819
   303
avigad@16819
   304
lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
huffman@30097
   305
  unfolding real_of_int_def by (rule le_ceiling_iff)
avigad@16819
   306
avigad@16819
   307
lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
huffman@30097
   308
  unfolding real_of_int_def by (rule ceiling_add_of_int)
avigad@16819
   309
avigad@16819
   310
lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
huffman@30097
   311
  unfolding real_of_int_def by (rule ceiling_diff_of_int)
avigad@16819
   312
avigad@16819
   313
avigad@16819
   314
subsection {* Versions for the natural numbers *}
avigad@16819
   315
wenzelm@19765
   316
definition
wenzelm@21404
   317
  natfloor :: "real => nat" where
wenzelm@19765
   318
  "natfloor x = nat(floor x)"
wenzelm@21404
   319
wenzelm@21404
   320
definition
wenzelm@21404
   321
  natceiling :: "real => nat" where
wenzelm@19765
   322
  "natceiling x = nat(ceiling x)"
avigad@16819
   323
avigad@16819
   324
lemma natfloor_zero [simp]: "natfloor 0 = 0"
avigad@16819
   325
  by (unfold natfloor_def, simp)
avigad@16819
   326
avigad@16819
   327
lemma natfloor_one [simp]: "natfloor 1 = 1"
avigad@16819
   328
  by (unfold natfloor_def, simp)
avigad@16819
   329
avigad@16819
   330
lemma zero_le_natfloor [simp]: "0 <= natfloor x"
avigad@16819
   331
  by (unfold natfloor_def, simp)
avigad@16819
   332
avigad@16819
   333
lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
avigad@16819
   334
  by (unfold natfloor_def, simp)
avigad@16819
   335
avigad@16819
   336
lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
avigad@16819
   337
  by (unfold natfloor_def, simp)
avigad@16819
   338
avigad@16819
   339
lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
avigad@16819
   340
  by (unfold natfloor_def, simp)
avigad@16819
   341
avigad@16819
   342
lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
avigad@16819
   343
  apply (unfold natfloor_def)
avigad@16819
   344
  apply (subgoal_tac "floor x <= floor 0")
avigad@16819
   345
  apply simp
huffman@30097
   346
  apply (erule floor_mono)
avigad@16819
   347
done
avigad@16819
   348
avigad@16819
   349
lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
avigad@16819
   350
  apply (case_tac "0 <= x")
avigad@16819
   351
  apply (subst natfloor_def)+
avigad@16819
   352
  apply (subst nat_le_eq_zle)
avigad@16819
   353
  apply force
huffman@30097
   354
  apply (erule floor_mono)
avigad@16819
   355
  apply (subst natfloor_neg)
avigad@16819
   356
  apply simp
avigad@16819
   357
  apply simp
avigad@16819
   358
done
avigad@16819
   359
avigad@16819
   360
lemma le_natfloor: "real x <= a ==> x <= natfloor a"
avigad@16819
   361
  apply (unfold natfloor_def)
avigad@16819
   362
  apply (subst nat_int [THEN sym])
avigad@16819
   363
  apply (subst nat_le_eq_zle)
avigad@16819
   364
  apply simp
avigad@16819
   365
  apply (rule le_floor)
avigad@16819
   366
  apply simp
avigad@16819
   367
done
avigad@16819
   368
hoelzl@35578
   369
lemma less_natfloor:
hoelzl@35578
   370
  assumes "0 \<le> x" and "x < real (n :: nat)"
hoelzl@35578
   371
  shows "natfloor x < n"
hoelzl@35578
   372
proof (rule ccontr)
hoelzl@35578
   373
  assume "\<not> ?thesis" hence *: "n \<le> natfloor x" by simp
hoelzl@35578
   374
  note assms(2)
hoelzl@35578
   375
  also have "real n \<le> real (natfloor x)"
hoelzl@35578
   376
    using * unfolding real_of_nat_le_iff .
hoelzl@35578
   377
  finally have "x < real (natfloor x)" .
hoelzl@35578
   378
  with real_natfloor_le[OF assms(1)]
hoelzl@35578
   379
  show False by auto
hoelzl@35578
   380
qed
hoelzl@35578
   381
avigad@16819
   382
lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
avigad@16819
   383
  apply (rule iffI)
avigad@16819
   384
  apply (rule order_trans)
avigad@16819
   385
  prefer 2
avigad@16819
   386
  apply (erule real_natfloor_le)
avigad@16819
   387
  apply (subst real_of_nat_le_iff)
avigad@16819
   388
  apply assumption
avigad@16819
   389
  apply (erule le_natfloor)
avigad@16819
   390
done
avigad@16819
   391
wenzelm@16893
   392
lemma le_natfloor_eq_number_of [simp]:
avigad@16819
   393
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16819
   394
      (number_of n <= natfloor x) = (number_of n <= x)"
avigad@16819
   395
  apply (subst le_natfloor_eq, assumption)
avigad@16819
   396
  apply simp
avigad@16819
   397
done
avigad@16819
   398
avigad@16820
   399
lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
avigad@16819
   400
  apply (case_tac "0 <= x")
avigad@16819
   401
  apply (subst le_natfloor_eq, assumption, simp)
avigad@16819
   402
  apply (rule iffI)
wenzelm@16893
   403
  apply (subgoal_tac "natfloor x <= natfloor 0")
avigad@16819
   404
  apply simp
avigad@16819
   405
  apply (rule natfloor_mono)
avigad@16819
   406
  apply simp
avigad@16819
   407
  apply simp
avigad@16819
   408
done
avigad@16819
   409
avigad@16819
   410
lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
avigad@16819
   411
  apply (unfold natfloor_def)
hoelzl@35578
   412
  apply (subst (2) nat_int [THEN sym])
avigad@16819
   413
  apply (subst eq_nat_nat_iff)
avigad@16819
   414
  apply simp
avigad@16819
   415
  apply simp
avigad@16819
   416
  apply (rule floor_eq2)
avigad@16819
   417
  apply auto
avigad@16819
   418
done
avigad@16819
   419
avigad@16819
   420
lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
avigad@16819
   421
  apply (case_tac "0 <= x")
avigad@16819
   422
  apply (unfold natfloor_def)
avigad@16819
   423
  apply simp
avigad@16819
   424
  apply simp_all
avigad@16819
   425
done
avigad@16819
   426
avigad@16819
   427
lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
nipkow@29667
   428
using real_natfloor_add_one_gt by (simp add: algebra_simps)
avigad@16819
   429
avigad@16819
   430
lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
avigad@16819
   431
  apply (subgoal_tac "z < real(natfloor z) + 1")
avigad@16819
   432
  apply arith
avigad@16819
   433
  apply (rule real_natfloor_add_one_gt)
avigad@16819
   434
done
avigad@16819
   435
avigad@16819
   436
lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
avigad@16819
   437
  apply (unfold natfloor_def)
huffman@24355
   438
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   439
  apply (erule ssubst)
huffman@23309
   440
  apply (simp add: nat_add_distrib del: real_of_int_of_nat_eq)
avigad@16819
   441
  apply simp
avigad@16819
   442
done
avigad@16819
   443
wenzelm@16893
   444
lemma natfloor_add_number_of [simp]:
wenzelm@16893
   445
    "~neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16819
   446
      natfloor (x + number_of n) = natfloor x + number_of n"
avigad@16819
   447
  apply (subst natfloor_add [THEN sym])
avigad@16819
   448
  apply simp_all
avigad@16819
   449
done
avigad@16819
   450
avigad@16819
   451
lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
avigad@16819
   452
  apply (subst natfloor_add [THEN sym])
avigad@16819
   453
  apply assumption
avigad@16819
   454
  apply simp
avigad@16819
   455
done
avigad@16819
   456
wenzelm@16893
   457
lemma natfloor_subtract [simp]: "real a <= x ==>
avigad@16819
   458
    natfloor(x - real a) = natfloor x - a"
avigad@16819
   459
  apply (unfold natfloor_def)
huffman@24355
   460
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   461
  apply (erule ssubst)
huffman@23309
   462
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   463
  apply simp
avigad@16819
   464
done
avigad@16819
   465
wenzelm@41550
   466
lemma natfloor_div_nat:
wenzelm@41550
   467
  assumes "1 <= x" and "y > 0"
wenzelm@41550
   468
  shows "natfloor (x / real y) = natfloor x div y"
hoelzl@35578
   469
proof -
hoelzl@35578
   470
  have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"
hoelzl@35578
   471
    by simp
hoelzl@35578
   472
  then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +
hoelzl@35578
   473
    real((natfloor x) mod y)"
hoelzl@35578
   474
    by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])
hoelzl@35578
   475
  have "x = real(natfloor x) + (x - real(natfloor x))"
hoelzl@35578
   476
    by simp
hoelzl@35578
   477
  then have "x = real ((natfloor x) div y) * real y +
hoelzl@35578
   478
      real((natfloor x) mod y) + (x - real(natfloor x))"
hoelzl@35578
   479
    by (simp add: a)
hoelzl@35578
   480
  then have "x / real y = ... / real y"
hoelzl@35578
   481
    by simp
hoelzl@35578
   482
  also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /
hoelzl@35578
   483
    real y + (x - real(natfloor x)) / real y"
wenzelm@41550
   484
    by (auto simp add: algebra_simps add_divide_distrib diff_divide_distrib)
hoelzl@35578
   485
  finally have "natfloor (x / real y) = natfloor(...)" by simp
hoelzl@35578
   486
  also have "... = natfloor(real((natfloor x) mod y) /
hoelzl@35578
   487
    real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"
hoelzl@35578
   488
    by (simp add: add_ac)
hoelzl@35578
   489
  also have "... = natfloor(real((natfloor x) mod y) /
hoelzl@35578
   490
    real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"
hoelzl@35578
   491
    apply (rule natfloor_add)
hoelzl@35578
   492
    apply (rule add_nonneg_nonneg)
hoelzl@35578
   493
    apply (rule divide_nonneg_pos)
hoelzl@35578
   494
    apply simp
wenzelm@41550
   495
    apply (simp add: assms)
hoelzl@35578
   496
    apply (rule divide_nonneg_pos)
hoelzl@35578
   497
    apply (simp add: algebra_simps)
hoelzl@35578
   498
    apply (rule real_natfloor_le)
wenzelm@41550
   499
    using assms apply auto
hoelzl@35578
   500
    done
hoelzl@35578
   501
  also have "natfloor(real((natfloor x) mod y) /
hoelzl@35578
   502
    real y + (x - real(natfloor x)) / real y) = 0"
hoelzl@35578
   503
    apply (rule natfloor_eq)
hoelzl@35578
   504
    apply simp
hoelzl@35578
   505
    apply (rule add_nonneg_nonneg)
hoelzl@35578
   506
    apply (rule divide_nonneg_pos)
hoelzl@35578
   507
    apply force
wenzelm@41550
   508
    apply (force simp add: assms)
hoelzl@35578
   509
    apply (rule divide_nonneg_pos)
hoelzl@35578
   510
    apply (simp add: algebra_simps)
hoelzl@35578
   511
    apply (rule real_natfloor_le)
wenzelm@41550
   512
    apply (auto simp add: assms)
wenzelm@41550
   513
    using assms apply arith
wenzelm@41550
   514
    using assms apply (simp add: add_divide_distrib [THEN sym])
hoelzl@35578
   515
    apply (subgoal_tac "real y = real y - 1 + 1")
hoelzl@35578
   516
    apply (erule ssubst)
hoelzl@35578
   517
    apply (rule add_le_less_mono)
hoelzl@35578
   518
    apply (simp add: algebra_simps)
hoelzl@35578
   519
    apply (subgoal_tac "1 + real(natfloor x mod y) =
hoelzl@35578
   520
      real(natfloor x mod y + 1)")
hoelzl@35578
   521
    apply (erule ssubst)
hoelzl@35578
   522
    apply (subst real_of_nat_le_iff)
hoelzl@35578
   523
    apply (subgoal_tac "natfloor x mod y < y")
hoelzl@35578
   524
    apply arith
hoelzl@35578
   525
    apply (rule mod_less_divisor)
hoelzl@35578
   526
    apply auto
hoelzl@35578
   527
    using real_natfloor_add_one_gt
hoelzl@35578
   528
    apply (simp add: algebra_simps)
hoelzl@35578
   529
    done
hoelzl@35578
   530
  finally show ?thesis by simp
hoelzl@35578
   531
qed
hoelzl@35578
   532
hoelzl@35578
   533
lemma le_mult_natfloor:
hoelzl@35578
   534
  assumes "0 \<le> (a :: real)" and "0 \<le> b"
hoelzl@35578
   535
  shows "natfloor a * natfloor b \<le> natfloor (a * b)"
hoelzl@35578
   536
  unfolding natfloor_def
hoelzl@35578
   537
  apply (subst nat_mult_distrib[symmetric])
hoelzl@35578
   538
  using assms apply simp
hoelzl@35578
   539
  apply (subst nat_le_eq_zle)
hoelzl@35578
   540
  using assms le_mult_floor by (auto intro!: mult_nonneg_nonneg)
hoelzl@35578
   541
avigad@16819
   542
lemma natceiling_zero [simp]: "natceiling 0 = 0"
avigad@16819
   543
  by (unfold natceiling_def, simp)
avigad@16819
   544
avigad@16819
   545
lemma natceiling_one [simp]: "natceiling 1 = 1"
avigad@16819
   546
  by (unfold natceiling_def, simp)
avigad@16819
   547
avigad@16819
   548
lemma zero_le_natceiling [simp]: "0 <= natceiling x"
avigad@16819
   549
  by (unfold natceiling_def, simp)
avigad@16819
   550
avigad@16819
   551
lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
avigad@16819
   552
  by (unfold natceiling_def, simp)
avigad@16819
   553
avigad@16819
   554
lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
avigad@16819
   555
  by (unfold natceiling_def, simp)
avigad@16819
   556
avigad@16819
   557
lemma real_natceiling_ge: "x <= real(natceiling x)"
avigad@16819
   558
  apply (unfold natceiling_def)
avigad@16819
   559
  apply (case_tac "x < 0")
avigad@16819
   560
  apply simp
avigad@16819
   561
  apply (subst real_nat_eq_real)
avigad@16819
   562
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
   563
  apply simp
huffman@30097
   564
  apply (rule ceiling_mono)
avigad@16819
   565
  apply simp
avigad@16819
   566
  apply simp
avigad@16819
   567
done
avigad@16819
   568
avigad@16819
   569
lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
avigad@16819
   570
  apply (unfold natceiling_def)
avigad@16819
   571
  apply simp
avigad@16819
   572
done
avigad@16819
   573
avigad@16819
   574
lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
avigad@16819
   575
  apply (case_tac "0 <= x")
avigad@16819
   576
  apply (subst natceiling_def)+
avigad@16819
   577
  apply (subst nat_le_eq_zle)
avigad@16819
   578
  apply (rule disjI2)
avigad@16819
   579
  apply (subgoal_tac "real (0::int) <= real(ceiling y)")
avigad@16819
   580
  apply simp
avigad@16819
   581
  apply (rule order_trans)
avigad@16819
   582
  apply simp
avigad@16819
   583
  apply (erule order_trans)
avigad@16819
   584
  apply simp
huffman@30097
   585
  apply (erule ceiling_mono)
avigad@16819
   586
  apply (subst natceiling_neg)
avigad@16819
   587
  apply simp_all
avigad@16819
   588
done
avigad@16819
   589
avigad@16819
   590
lemma natceiling_le: "x <= real a ==> natceiling x <= a"
avigad@16819
   591
  apply (unfold natceiling_def)
avigad@16819
   592
  apply (case_tac "x < 0")
avigad@16819
   593
  apply simp
hoelzl@35578
   594
  apply (subst (2) nat_int [THEN sym])
avigad@16819
   595
  apply (subst nat_le_eq_zle)
avigad@16819
   596
  apply simp
avigad@16819
   597
  apply (rule ceiling_le)
avigad@16819
   598
  apply simp
avigad@16819
   599
done
avigad@16819
   600
avigad@16819
   601
lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"
avigad@16819
   602
  apply (rule iffI)
avigad@16819
   603
  apply (rule order_trans)
avigad@16819
   604
  apply (rule real_natceiling_ge)
avigad@16819
   605
  apply (subst real_of_nat_le_iff)
avigad@16819
   606
  apply assumption
avigad@16819
   607
  apply (erule natceiling_le)
avigad@16819
   608
done
avigad@16819
   609
wenzelm@16893
   610
lemma natceiling_le_eq_number_of [simp]:
avigad@16820
   611
    "~ neg((number_of n)::int) ==> 0 <= x ==>
avigad@16820
   612
      (natceiling x <= number_of n) = (x <= number_of n)"
avigad@16819
   613
  apply (subst natceiling_le_eq, assumption)
avigad@16819
   614
  apply simp
avigad@16819
   615
done
avigad@16819
   616
avigad@16820
   617
lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
avigad@16819
   618
  apply (case_tac "0 <= x")
avigad@16819
   619
  apply (subst natceiling_le_eq)
avigad@16819
   620
  apply assumption
avigad@16819
   621
  apply simp
avigad@16819
   622
  apply (subst natceiling_neg)
avigad@16819
   623
  apply simp
avigad@16819
   624
  apply simp
avigad@16819
   625
done
avigad@16819
   626
avigad@16819
   627
lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
avigad@16819
   628
  apply (unfold natceiling_def)
wenzelm@19850
   629
  apply (simplesubst nat_int [THEN sym]) back back
avigad@16819
   630
  apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")
avigad@16819
   631
  apply (erule ssubst)
avigad@16819
   632
  apply (subst eq_nat_nat_iff)
avigad@16819
   633
  apply (subgoal_tac "ceiling 0 <= ceiling x")
avigad@16819
   634
  apply simp
huffman@30097
   635
  apply (rule ceiling_mono)
avigad@16819
   636
  apply force
avigad@16819
   637
  apply force
avigad@16819
   638
  apply (rule ceiling_eq2)
avigad@16819
   639
  apply (simp, simp)
avigad@16819
   640
  apply (subst nat_add_distrib)
avigad@16819
   641
  apply auto
avigad@16819
   642
done
avigad@16819
   643
wenzelm@16893
   644
lemma natceiling_add [simp]: "0 <= x ==>
avigad@16819
   645
    natceiling (x + real a) = natceiling x + a"
avigad@16819
   646
  apply (unfold natceiling_def)
huffman@24355
   647
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   648
  apply (erule ssubst)
huffman@23309
   649
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   650
  apply (subst nat_add_distrib)
avigad@16819
   651
  apply (subgoal_tac "0 = ceiling 0")
avigad@16819
   652
  apply (erule ssubst)
huffman@30097
   653
  apply (erule ceiling_mono)
avigad@16819
   654
  apply simp_all
avigad@16819
   655
done
avigad@16819
   656
wenzelm@16893
   657
lemma natceiling_add_number_of [simp]:
wenzelm@16893
   658
    "~ neg ((number_of n)::int) ==> 0 <= x ==>
avigad@16820
   659
      natceiling (x + number_of n) = natceiling x + number_of n"
avigad@16819
   660
  apply (subst natceiling_add [THEN sym])
avigad@16819
   661
  apply simp_all
avigad@16819
   662
done
avigad@16819
   663
avigad@16819
   664
lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
avigad@16819
   665
  apply (subst natceiling_add [THEN sym])
avigad@16819
   666
  apply assumption
avigad@16819
   667
  apply simp
avigad@16819
   668
done
avigad@16819
   669
wenzelm@16893
   670
lemma natceiling_subtract [simp]: "real a <= x ==>
avigad@16819
   671
    natceiling(x - real a) = natceiling x - a"
avigad@16819
   672
  apply (unfold natceiling_def)
huffman@24355
   673
  apply (subgoal_tac "real a = real (int a)")
avigad@16819
   674
  apply (erule ssubst)
huffman@23309
   675
  apply (simp del: real_of_int_of_nat_eq)
avigad@16819
   676
  apply simp
avigad@16819
   677
done
avigad@16819
   678
huffman@36826
   679
subsection {* Exponentiation with floor *}
huffman@36826
   680
huffman@36826
   681
lemma floor_power:
huffman@36826
   682
  assumes "x = real (floor x)"
huffman@36826
   683
  shows "floor (x ^ n) = floor x ^ n"
huffman@36826
   684
proof -
huffman@36826
   685
  have *: "x ^ n = real (floor x ^ n)"
huffman@36826
   686
    using assms by (induct n arbitrary: x) simp_all
huffman@36826
   687
  show ?thesis unfolding real_of_int_inject[symmetric]
huffman@36826
   688
    unfolding * floor_real_of_int ..
huffman@36826
   689
qed
huffman@36826
   690
huffman@36826
   691
lemma natfloor_power:
huffman@36826
   692
  assumes "x = real (natfloor x)"
huffman@36826
   693
  shows "natfloor (x ^ n) = natfloor x ^ n"
huffman@36826
   694
proof -
huffman@36826
   695
  from assms have "0 \<le> floor x" by auto
huffman@36826
   696
  note assms[unfolded natfloor_def real_nat_eq_real[OF `0 \<le> floor x`]]
huffman@36826
   697
  from floor_power[OF this]
huffman@36826
   698
  show ?thesis unfolding natfloor_def nat_power_eq[OF `0 \<le> floor x`, symmetric]
huffman@36826
   699
    by simp
huffman@36826
   700
qed
avigad@16819
   701
paulson@14365
   702
end