src/HOL/RComplete.thy
 author huffman Fri Sep 02 14:27:55 2011 -0700 (2011-09-02) changeset 44667 ee5772ca7d16 parent 41550 efa734d9b221 child 44668 31d41a0f6b5d permissions -rw-r--r--
remove unused, unnecessary lemmas
     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_nn_real: fixes x::real

   117 assumes "0\<le>x" and "x<y" shows "\<exists>r \<in> \<rat>.  x<r \<and> r<y"

   118 proof -

   119   from x<y have "0 < y-x" by simp

   120   with reals_Archimedean obtain q::nat

   121     where q: "inverse (real q) < y-x" and "0 < real q" by auto

   122   def p \<equiv> "LEAST n.  y \<le> real (Suc n)/real q"

   123   from reals_Archimedean2 obtain n::nat where "y * real q < real n" by auto

   124   with 0 < real q have ex: "y \<le> real n/real q" (is "?P n")

   125     by (simp add: pos_less_divide_eq[THEN sym])

   126   also from assms have "\<not> y \<le> real (0::nat) / real q" by simp

   127   ultimately have main: "(LEAST n. y \<le> real n/real q) = Suc p"

   128     by (unfold p_def) (rule Least_Suc)

   129   also from ex have "?P (LEAST x. ?P x)" by (rule LeastI)

   130   ultimately have suc: "y \<le> real (Suc p) / real q" by simp

   131   def r \<equiv> "real p/real q"

   132   have "x = y-(y-x)" by simp

   133   also from suc q have "\<dots> < real (Suc p)/real q - inverse (real q)" by arith

   134   also have "\<dots> = real p / real q"

   135     by (simp only: inverse_eq_divide diff_minus real_of_nat_Suc

   136     minus_divide_left add_divide_distrib[THEN sym]) simp

   137   finally have "x<r" by (unfold r_def)

   138   have "p<Suc p" .. also note main[THEN sym]

   139   finally have "\<not> ?P p"  by (rule not_less_Least)

   140   hence "r<y" by (simp add: r_def)

   141   from r_def have "r \<in> \<rat>" by simp

   142   with x<r r<y show ?thesis by fast

   143 qed

   144

   145 theorem Rats_dense_in_real: fixes x y :: real

   146 assumes "x<y" shows "\<exists>r \<in> \<rat>.  x<r \<and> r<y"

   147 proof -

   148   from reals_Archimedean2 obtain n::nat where "-x < real n" by auto

   149   hence "0 \<le> x + real n" by arith

   150   also from x<y have "x + real n < y + real n" by arith

   151   ultimately have "\<exists>r \<in> \<rat>. x + real n < r \<and> r < y + real n"

   152     by(rule Rats_dense_in_nn_real)

   153   then obtain r where "r \<in> \<rat>" and r2: "x + real n < r"

   154     and r3: "r < y + real n"

   155     by blast

   156   have "r - real n = r + real (int n)/real (-1::int)" by simp

   157   also from r\<in>\<rat> have "r + real (int n)/real (-1::int) \<in> \<rat>" by simp

   158   also from r2 have "x < r - real n" by arith

   159   moreover from r3 have "r - real n < y" by arith

   160   ultimately show ?thesis by fast

   161 qed

   162

   163

   164 subsection{*Floor and Ceiling Functions from the Reals to the Integers*}

   165

   166 lemma number_of_less_real_of_int_iff [simp]:

   167      "((number_of n) < real (m::int)) = (number_of n < m)"

   168 apply auto

   169 apply (rule real_of_int_less_iff [THEN iffD1])

   170 apply (drule_tac  real_of_int_less_iff [THEN iffD2], auto)

   171 done

   172

   173 lemma number_of_less_real_of_int_iff2 [simp]:

   174      "(real (m::int) < (number_of n)) = (m < number_of n)"

   175 apply auto

   176 apply (rule real_of_int_less_iff [THEN iffD1])

   177 apply (drule_tac  real_of_int_less_iff [THEN iffD2], auto)

   178 done

   179

   180 lemma number_of_le_real_of_int_iff [simp]:

   181      "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"

   182 by (simp add: linorder_not_less [symmetric])

   183

   184 lemma number_of_le_real_of_int_iff2 [simp]:

   185      "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"

   186 by (simp add: linorder_not_less [symmetric])

   187

   188 lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"

   189 unfolding real_of_nat_def by simp

   190

   191 lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"

   192 unfolding real_of_nat_def by (simp add: floor_minus)

   193

   194 lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"

   195 unfolding real_of_int_def by simp

   196

   197 lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"

   198 unfolding real_of_int_def by (simp add: floor_minus)

   199

   200 lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"

   201 unfolding real_of_int_def by (rule floor_exists)

   202

   203 lemma lemma_floor:

   204   assumes a1: "real m \<le> r" and a2: "r < real n + 1"

   205   shows "m \<le> (n::int)"

   206 proof -

   207   have "real m < real n + 1" using a1 a2 by (rule order_le_less_trans)

   208   also have "... = real (n + 1)" by simp

   209   finally have "m < n + 1" by (simp only: real_of_int_less_iff)

   210   thus ?thesis by arith

   211 qed

   212

   213 lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"

   214 unfolding real_of_int_def by (rule of_int_floor_le)

   215

   216 lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"

   217 by (auto intro: lemma_floor)

   218

   219 lemma real_of_int_floor_cancel [simp]:

   220     "(real (floor x) = x) = (\<exists>n::int. x = real n)"

   221   using floor_real_of_int by metis

   222

   223 lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"

   224   unfolding real_of_int_def using floor_unique [of n x] by simp

   225

   226 lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"

   227   unfolding real_of_int_def by (rule floor_unique)

   228

   229 lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"

   230 apply (rule inj_int [THEN injD])

   231 apply (simp add: real_of_nat_Suc)

   232 apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])

   233 done

   234

   235 lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"

   236 apply (drule order_le_imp_less_or_eq)

   237 apply (auto intro: floor_eq3)

   238 done

   239

   240 lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"

   241   unfolding real_of_int_def using floor_correct [of r] by simp

   242

   243 lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"

   244   unfolding real_of_int_def using floor_correct [of r] by simp

   245

   246 lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"

   247   unfolding real_of_int_def using floor_correct [of r] by simp

   248

   249 lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"

   250   unfolding real_of_int_def using floor_correct [of r] by simp

   251

   252 lemma le_floor: "real a <= x ==> a <= floor x"

   253   unfolding real_of_int_def by (simp add: le_floor_iff)

   254

   255 lemma real_le_floor: "a <= floor x ==> real a <= x"

   256   unfolding real_of_int_def by (simp add: le_floor_iff)

   257

   258 lemma le_floor_eq: "(a <= floor x) = (real a <= x)"

   259   unfolding real_of_int_def by (rule le_floor_iff)

   260

   261 lemma floor_less_eq: "(floor x < a) = (x < real a)"

   262   unfolding real_of_int_def by (rule floor_less_iff)

   263

   264 lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"

   265   unfolding real_of_int_def by (rule less_floor_iff)

   266

   267 lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"

   268   unfolding real_of_int_def by (rule floor_le_iff)

   269

   270 lemma floor_add [simp]: "floor (x + real a) = floor x + a"

   271   unfolding real_of_int_def by (rule floor_add_of_int)

   272

   273 lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"

   274   unfolding real_of_int_def by (rule floor_diff_of_int)

   275

   276 lemma le_mult_floor:

   277   assumes "0 \<le> (a :: real)" and "0 \<le> b"

   278   shows "floor a * floor b \<le> floor (a * b)"

   279 proof -

   280   have "real (floor a) \<le> a"

   281     and "real (floor b) \<le> b" by auto

   282   hence "real (floor a * floor b) \<le> a * b"

   283     using assms by (auto intro!: mult_mono)

   284   also have "a * b < real (floor (a * b) + 1)" by auto

   285   finally show ?thesis unfolding real_of_int_less_iff by simp

   286 qed

   287

   288 lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"

   289   unfolding real_of_nat_def by simp

   290

   291 lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"

   292   unfolding real_of_int_def by simp

   293

   294 lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"

   295   unfolding real_of_int_def by simp

   296

   297 lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"

   298   unfolding real_of_int_def by (rule le_of_int_ceiling)

   299

   300 lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"

   301   unfolding real_of_int_def by simp

   302

   303 lemma real_of_int_ceiling_cancel [simp]:

   304      "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"

   305   using ceiling_real_of_int by metis

   306

   307 lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"

   308   unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp

   309

   310 lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"

   311   unfolding real_of_int_def using ceiling_unique [of "n + 1" x] by simp

   312

   313 lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"

   314   unfolding real_of_int_def using ceiling_unique [of n x] by simp

   315

   316 lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"

   317   unfolding real_of_int_def using ceiling_correct [of r] by simp

   318

   319 lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"

   320   unfolding real_of_int_def using ceiling_correct [of r] by simp

   321

   322 lemma ceiling_le: "x <= real a ==> ceiling x <= a"

   323   unfolding real_of_int_def by (simp add: ceiling_le_iff)

   324

   325 lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"

   326   unfolding real_of_int_def by (simp add: ceiling_le_iff)

   327

   328 lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"

   329   unfolding real_of_int_def by (rule ceiling_le_iff)

   330

   331 lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"

   332   unfolding real_of_int_def by (rule less_ceiling_iff)

   333

   334 lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"

   335   unfolding real_of_int_def by (rule ceiling_less_iff)

   336

   337 lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"

   338   unfolding real_of_int_def by (rule le_ceiling_iff)

   339

   340 lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"

   341   unfolding real_of_int_def by (rule ceiling_add_of_int)

   342

   343 lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"

   344   unfolding real_of_int_def by (rule ceiling_diff_of_int)

   345

   346

   347 subsection {* Versions for the natural numbers *}

   348

   349 definition

   350   natfloor :: "real => nat" where

   351   "natfloor x = nat(floor x)"

   352

   353 definition

   354   natceiling :: "real => nat" where

   355   "natceiling x = nat(ceiling x)"

   356

   357 lemma natfloor_zero [simp]: "natfloor 0 = 0"

   358   by (unfold natfloor_def, simp)

   359

   360 lemma natfloor_one [simp]: "natfloor 1 = 1"

   361   by (unfold natfloor_def, simp)

   362

   363 lemma zero_le_natfloor [simp]: "0 <= natfloor x"

   364   by (unfold natfloor_def, simp)

   365

   366 lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"

   367   by (unfold natfloor_def, simp)

   368

   369 lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"

   370   by (unfold natfloor_def, simp)

   371

   372 lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"

   373   by (unfold natfloor_def, simp)

   374

   375 lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"

   376   apply (unfold natfloor_def)

   377   apply (subgoal_tac "floor x <= floor 0")

   378   apply simp

   379   apply (erule floor_mono)

   380 done

   381

   382 lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"

   383   apply (case_tac "0 <= x")

   384   apply (subst natfloor_def)+

   385   apply (subst nat_le_eq_zle)

   386   apply force

   387   apply (erule floor_mono)

   388   apply (subst natfloor_neg)

   389   apply simp

   390   apply simp

   391 done

   392

   393 lemma le_natfloor: "real x <= a ==> x <= natfloor a"

   394   apply (unfold natfloor_def)

   395   apply (subst nat_int [THEN sym])

   396   apply (subst nat_le_eq_zle)

   397   apply simp

   398   apply (rule le_floor)

   399   apply simp

   400 done

   401

   402 lemma less_natfloor:

   403   assumes "0 \<le> x" and "x < real (n :: nat)"

   404   shows "natfloor x < n"

   405 proof (rule ccontr)

   406   assume "\<not> ?thesis" hence *: "n \<le> natfloor x" by simp

   407   note assms(2)

   408   also have "real n \<le> real (natfloor x)"

   409     using * unfolding real_of_nat_le_iff .

   410   finally have "x < real (natfloor x)" .

   411   with real_natfloor_le[OF assms(1)]

   412   show False by auto

   413 qed

   414

   415 lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"

   416   apply (rule iffI)

   417   apply (rule order_trans)

   418   prefer 2

   419   apply (erule real_natfloor_le)

   420   apply (subst real_of_nat_le_iff)

   421   apply assumption

   422   apply (erule le_natfloor)

   423 done

   424

   425 lemma le_natfloor_eq_number_of [simp]:

   426     "~ neg((number_of n)::int) ==> 0 <= x ==>

   427       (number_of n <= natfloor x) = (number_of n <= x)"

   428   apply (subst le_natfloor_eq, assumption)

   429   apply simp

   430 done

   431

   432 lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"

   433   apply (case_tac "0 <= x")

   434   apply (subst le_natfloor_eq, assumption, simp)

   435   apply (rule iffI)

   436   apply (subgoal_tac "natfloor x <= natfloor 0")

   437   apply simp

   438   apply (rule natfloor_mono)

   439   apply simp

   440   apply simp

   441 done

   442

   443 lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"

   444   apply (unfold natfloor_def)

   445   apply (subst (2) nat_int [THEN sym])

   446   apply (subst eq_nat_nat_iff)

   447   apply simp

   448   apply simp

   449   apply (rule floor_eq2)

   450   apply auto

   451 done

   452

   453 lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"

   454   apply (case_tac "0 <= x")

   455   apply (unfold natfloor_def)

   456   apply simp

   457   apply simp_all

   458 done

   459

   460 lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"

   461 using real_natfloor_add_one_gt by (simp add: algebra_simps)

   462

   463 lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"

   464   apply (subgoal_tac "z < real(natfloor z) + 1")

   465   apply arith

   466   apply (rule real_natfloor_add_one_gt)

   467 done

   468

   469 lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"

   470   apply (unfold natfloor_def)

   471   apply (subgoal_tac "real a = real (int a)")

   472   apply (erule ssubst)

   473   apply (simp add: nat_add_distrib del: real_of_int_of_nat_eq)

   474   apply simp

   475 done

   476

   477 lemma natfloor_add_number_of [simp]:

   478     "~neg ((number_of n)::int) ==> 0 <= x ==>

   479       natfloor (x + number_of n) = natfloor x + number_of n"

   480   apply (subst natfloor_add [THEN sym])

   481   apply simp_all

   482 done

   483

   484 lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"

   485   apply (subst natfloor_add [THEN sym])

   486   apply assumption

   487   apply simp

   488 done

   489

   490 lemma natfloor_subtract [simp]: "real a <= x ==>

   491     natfloor(x - real a) = natfloor x - a"

   492   apply (unfold natfloor_def)

   493   apply (subgoal_tac "real a = real (int a)")

   494   apply (erule ssubst)

   495   apply (simp del: real_of_int_of_nat_eq)

   496   apply simp

   497 done

   498

   499 lemma natfloor_div_nat:

   500   assumes "1 <= x" and "y > 0"

   501   shows "natfloor (x / real y) = natfloor x div y"

   502 proof -

   503   have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"

   504     by simp

   505   then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +

   506     real((natfloor x) mod y)"

   507     by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])

   508   have "x = real(natfloor x) + (x - real(natfloor x))"

   509     by simp

   510   then have "x = real ((natfloor x) div y) * real y +

   511       real((natfloor x) mod y) + (x - real(natfloor x))"

   512     by (simp add: a)

   513   then have "x / real y = ... / real y"

   514     by simp

   515   also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /

   516     real y + (x - real(natfloor x)) / real y"

   517     by (auto simp add: algebra_simps add_divide_distrib diff_divide_distrib)

   518   finally have "natfloor (x / real y) = natfloor(...)" by simp

   519   also have "... = natfloor(real((natfloor x) mod y) /

   520     real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"

   521     by (simp add: add_ac)

   522   also have "... = natfloor(real((natfloor x) mod y) /

   523     real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"

   524     apply (rule natfloor_add)

   525     apply (rule add_nonneg_nonneg)

   526     apply (rule divide_nonneg_pos)

   527     apply simp

   528     apply (simp add: assms)

   529     apply (rule divide_nonneg_pos)

   530     apply (simp add: algebra_simps)

   531     apply (rule real_natfloor_le)

   532     using assms apply auto

   533     done

   534   also have "natfloor(real((natfloor x) mod y) /

   535     real y + (x - real(natfloor x)) / real y) = 0"

   536     apply (rule natfloor_eq)

   537     apply simp

   538     apply (rule add_nonneg_nonneg)

   539     apply (rule divide_nonneg_pos)

   540     apply force

   541     apply (force simp add: assms)

   542     apply (rule divide_nonneg_pos)

   543     apply (simp add: algebra_simps)

   544     apply (rule real_natfloor_le)

   545     apply (auto simp add: assms)

   546     using assms apply arith

   547     using assms apply (simp add: add_divide_distrib [THEN sym])

   548     apply (subgoal_tac "real y = real y - 1 + 1")

   549     apply (erule ssubst)

   550     apply (rule add_le_less_mono)

   551     apply (simp add: algebra_simps)

   552     apply (subgoal_tac "1 + real(natfloor x mod y) =

   553       real(natfloor x mod y + 1)")

   554     apply (erule ssubst)

   555     apply (subst real_of_nat_le_iff)

   556     apply (subgoal_tac "natfloor x mod y < y")

   557     apply arith

   558     apply (rule mod_less_divisor)

   559     apply auto

   560     using real_natfloor_add_one_gt

   561     apply (simp add: algebra_simps)

   562     done

   563   finally show ?thesis by simp

   564 qed

   565

   566 lemma le_mult_natfloor:

   567   assumes "0 \<le> (a :: real)" and "0 \<le> b"

   568   shows "natfloor a * natfloor b \<le> natfloor (a * b)"

   569   unfolding natfloor_def

   570   apply (subst nat_mult_distrib[symmetric])

   571   using assms apply simp

   572   apply (subst nat_le_eq_zle)

   573   using assms le_mult_floor by (auto intro!: mult_nonneg_nonneg)

   574

   575 lemma natceiling_zero [simp]: "natceiling 0 = 0"

   576   by (unfold natceiling_def, simp)

   577

   578 lemma natceiling_one [simp]: "natceiling 1 = 1"

   579   by (unfold natceiling_def, simp)

   580

   581 lemma zero_le_natceiling [simp]: "0 <= natceiling x"

   582   by (unfold natceiling_def, simp)

   583

   584 lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"

   585   by (unfold natceiling_def, simp)

   586

   587 lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"

   588   by (unfold natceiling_def, simp)

   589

   590 lemma real_natceiling_ge: "x <= real(natceiling x)"

   591   apply (unfold natceiling_def)

   592   apply (case_tac "x < 0")

   593   apply simp

   594   apply (subst real_nat_eq_real)

   595   apply (subgoal_tac "ceiling 0 <= ceiling x")

   596   apply simp

   597   apply (rule ceiling_mono)

   598   apply simp

   599   apply simp

   600 done

   601

   602 lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"

   603   apply (unfold natceiling_def)

   604   apply simp

   605 done

   606

   607 lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"

   608   apply (case_tac "0 <= x")

   609   apply (subst natceiling_def)+

   610   apply (subst nat_le_eq_zle)

   611   apply (rule disjI2)

   612   apply (subgoal_tac "real (0::int) <= real(ceiling y)")

   613   apply simp

   614   apply (rule order_trans)

   615   apply simp

   616   apply (erule order_trans)

   617   apply simp

   618   apply (erule ceiling_mono)

   619   apply (subst natceiling_neg)

   620   apply simp_all

   621 done

   622

   623 lemma natceiling_le: "x <= real a ==> natceiling x <= a"

   624   apply (unfold natceiling_def)

   625   apply (case_tac "x < 0")

   626   apply simp

   627   apply (subst (2) nat_int [THEN sym])

   628   apply (subst nat_le_eq_zle)

   629   apply simp

   630   apply (rule ceiling_le)

   631   apply simp

   632 done

   633

   634 lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"

   635   apply (rule iffI)

   636   apply (rule order_trans)

   637   apply (rule real_natceiling_ge)

   638   apply (subst real_of_nat_le_iff)

   639   apply assumption

   640   apply (erule natceiling_le)

   641 done

   642

   643 lemma natceiling_le_eq_number_of [simp]:

   644     "~ neg((number_of n)::int) ==> 0 <= x ==>

   645       (natceiling x <= number_of n) = (x <= number_of n)"

   646   apply (subst natceiling_le_eq, assumption)

   647   apply simp

   648 done

   649

   650 lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"

   651   apply (case_tac "0 <= x")

   652   apply (subst natceiling_le_eq)

   653   apply assumption

   654   apply simp

   655   apply (subst natceiling_neg)

   656   apply simp

   657   apply simp

   658 done

   659

   660 lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"

   661   apply (unfold natceiling_def)

   662   apply (simplesubst nat_int [THEN sym]) back back

   663   apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")

   664   apply (erule ssubst)

   665   apply (subst eq_nat_nat_iff)

   666   apply (subgoal_tac "ceiling 0 <= ceiling x")

   667   apply simp

   668   apply (rule ceiling_mono)

   669   apply force

   670   apply force

   671   apply (rule ceiling_eq2)

   672   apply (simp, simp)

   673   apply (subst nat_add_distrib)

   674   apply auto

   675 done

   676

   677 lemma natceiling_add [simp]: "0 <= x ==>

   678     natceiling (x + real a) = natceiling x + a"

   679   apply (unfold natceiling_def)

   680   apply (subgoal_tac "real a = real (int a)")

   681   apply (erule ssubst)

   682   apply (simp del: real_of_int_of_nat_eq)

   683   apply (subst nat_add_distrib)

   684   apply (subgoal_tac "0 = ceiling 0")

   685   apply (erule ssubst)

   686   apply (erule ceiling_mono)

   687   apply simp_all

   688 done

   689

   690 lemma natceiling_add_number_of [simp]:

   691     "~ neg ((number_of n)::int) ==> 0 <= x ==>

   692       natceiling (x + number_of n) = natceiling x + number_of n"

   693   apply (subst natceiling_add [THEN sym])

   694   apply simp_all

   695 done

   696

   697 lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"

   698   apply (subst natceiling_add [THEN sym])

   699   apply assumption

   700   apply simp

   701 done

   702

   703 lemma natceiling_subtract [simp]: "real a <= x ==>

   704     natceiling(x - real a) = natceiling x - a"

   705   apply (unfold natceiling_def)

   706   apply (subgoal_tac "real a = real (int a)")

   707   apply (erule ssubst)

   708   apply (simp del: real_of_int_of_nat_eq)

   709   apply simp

   710 done

   711

   712 subsection {* Exponentiation with floor *}

   713

   714 lemma floor_power:

   715   assumes "x = real (floor x)"

   716   shows "floor (x ^ n) = floor x ^ n"

   717 proof -

   718   have *: "x ^ n = real (floor x ^ n)"

   719     using assms by (induct n arbitrary: x) simp_all

   720   show ?thesis unfolding real_of_int_inject[symmetric]

   721     unfolding * floor_real_of_int ..

   722 qed

   723

   724 lemma natfloor_power:

   725   assumes "x = real (natfloor x)"

   726   shows "natfloor (x ^ n) = natfloor x ^ n"

   727 proof -

   728   from assms have "0 \<le> floor x" by auto

   729   note assms[unfolded natfloor_def real_nat_eq_real[OF 0 \<le> floor x]]

   730   from floor_power[OF this]

   731   show ?thesis unfolding natfloor_def nat_power_eq[OF 0 \<le> floor x, symmetric]

   732     by simp

   733 qed

   734

   735 end