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