src/HOL/Real/RComplete.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 21210 c17fd2df4e9e
child 22998 97e1f9c2cc46
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
     1 (*  Title       : HOL/Real/RComplete.thy
     2     ID          : $Id$
     3     Author      : Jacques D. Fleuriot, University of Edinburgh
     4     Author      : Larry Paulson, University of Cambridge
     5     Author      : Jeremy Avigad, Carnegie Mellon University
     6     Author      : Florian Zuleger, Johannes Hoelzl, and Simon Funke, TU Muenchen
     7 *)
     8 
     9 header {* Completeness of the Reals; Floor and Ceiling Functions *}
    10 
    11 theory RComplete
    12 imports Lubs RealDef
    13 begin
    14 
    15 lemma real_sum_of_halves: "x/2 + x/2 = (x::real)"
    16   by simp
    17 
    18 
    19 subsection {* Completeness of Positive Reals *}
    20 
    21 text {*
    22   Supremum property for the set of positive reals
    23 
    24   Let @{text "P"} be a non-empty set of positive reals, with an upper
    25   bound @{text "y"}.  Then @{text "P"} has a least upper bound
    26   (written @{text "S"}).
    27 
    28   FIXME: Can the premise be weakened to @{text "\<forall>x \<in> P. x\<le> y"}?
    29 *}
    30 
    31 lemma posreal_complete:
    32   assumes positive_P: "\<forall>x \<in> P. (0::real) < x"
    33     and not_empty_P: "\<exists>x. x \<in> P"
    34     and upper_bound_Ex: "\<exists>y. \<forall>x \<in> P. x<y"
    35   shows "\<exists>S. \<forall>y. (\<exists>x \<in> P. y < x) = (y < S)"
    36 proof (rule exI, rule allI)
    37   fix y
    38   let ?pP = "{w. real_of_preal w \<in> P}"
    39 
    40   show "(\<exists>x\<in>P. y < x) = (y < real_of_preal (psup ?pP))"
    41   proof (cases "0 < y")
    42     assume neg_y: "\<not> 0 < y"
    43     show ?thesis
    44     proof
    45       assume "\<exists>x\<in>P. y < x"
    46       have "\<forall>x. y < real_of_preal x"
    47         using neg_y by (rule real_less_all_real2)
    48       thus "y < real_of_preal (psup ?pP)" ..
    49     next
    50       assume "y < real_of_preal (psup ?pP)"
    51       obtain "x" where x_in_P: "x \<in> P" using not_empty_P ..
    52       hence "0 < x" using positive_P by simp
    53       hence "y < x" using neg_y by simp
    54       thus "\<exists>x \<in> P. y < x" using x_in_P ..
    55     qed
    56   next
    57     assume pos_y: "0 < y"
    58 
    59     then obtain py where y_is_py: "y = real_of_preal py"
    60       by (auto simp add: real_gt_zero_preal_Ex)
    61 
    62     obtain a where a_in_P: "a \<in> P" using not_empty_P ..
    63     have a_pos: "0 < a" using positive_P ..
    64     then obtain pa where "a = real_of_preal pa"
    65       by (auto simp add: real_gt_zero_preal_Ex)
    66     hence "pa \<in> ?pP" using a_in_P by auto
    67     hence pP_not_empty: "?pP \<noteq> {}" by auto
    68 
    69     obtain sup where sup: "\<forall>x \<in> P. x < sup"
    70       using upper_bound_Ex ..
    71     hence  "a < sup" ..
    72     hence "0 < sup" using a_pos by arith
    73     then obtain possup where "sup = real_of_preal possup"
    74       by (auto simp add: real_gt_zero_preal_Ex)
    75     hence "\<forall>X \<in> ?pP. X \<le> possup"
    76       using sup by (auto simp add: real_of_preal_lessI)
    77     with pP_not_empty have psup: "\<And>Z. (\<exists>X \<in> ?pP. Z < X) = (Z < psup ?pP)"
    78       by (rule preal_complete)
    79 
    80     show ?thesis
    81     proof
    82       assume "\<exists>x \<in> P. y < x"
    83       then obtain x where x_in_P: "x \<in> P" and y_less_x: "y < x" ..
    84       hence "0 < x" using pos_y by arith
    85       then obtain px where x_is_px: "x = real_of_preal px"
    86         by (auto simp add: real_gt_zero_preal_Ex)
    87 
    88       have py_less_X: "\<exists>X \<in> ?pP. py < X"
    89       proof
    90         show "py < px" using y_is_py and x_is_px and y_less_x
    91           by (simp add: real_of_preal_lessI)
    92         show "px \<in> ?pP" using x_in_P and x_is_px by simp
    93       qed
    94 
    95       have "(\<exists>X \<in> ?pP. py < X) ==> (py < psup ?pP)"
    96         using psup by simp
    97       hence "py < psup ?pP" using py_less_X by simp
    98       thus "y < real_of_preal (psup {w. real_of_preal w \<in> P})"
    99         using y_is_py and pos_y by (simp add: real_of_preal_lessI)
   100     next
   101       assume y_less_psup: "y < real_of_preal (psup ?pP)"
   102 
   103       hence "py < psup ?pP" using y_is_py
   104         by (simp add: real_of_preal_lessI)
   105       then obtain "X" where py_less_X: "py < X" and X_in_pP: "X \<in> ?pP"
   106         using psup by auto
   107       then obtain x where x_is_X: "x = real_of_preal X"
   108         by (simp add: real_gt_zero_preal_Ex)
   109       hence "y < x" using py_less_X and y_is_py
   110         by (simp add: real_of_preal_lessI)
   111 
   112       moreover have "x \<in> P" using x_is_X and X_in_pP by simp
   113 
   114       ultimately show "\<exists> x \<in> P. y < x" ..
   115     qed
   116   qed
   117 qed
   118 
   119 text {*
   120   \medskip Completeness properties using @{text "isUb"}, @{text "isLub"} etc.
   121 *}
   122 
   123 lemma real_isLub_unique: "[| isLub R S x; isLub R S y |] ==> x = (y::real)"
   124   apply (frule isLub_isUb)
   125   apply (frule_tac x = y in isLub_isUb)
   126   apply (blast intro!: order_antisym dest!: isLub_le_isUb)
   127   done
   128 
   129 
   130 text {*
   131   \medskip Completeness theorem for the positive reals (again).
   132 *}
   133 
   134 lemma posreals_complete:
   135   assumes positive_S: "\<forall>x \<in> S. 0 < x"
   136     and not_empty_S: "\<exists>x. x \<in> S"
   137     and upper_bound_Ex: "\<exists>u. isUb (UNIV::real set) S u"
   138   shows "\<exists>t. isLub (UNIV::real set) S t"
   139 proof
   140   let ?pS = "{w. real_of_preal w \<in> S}"
   141 
   142   obtain u where "isUb UNIV S u" using upper_bound_Ex ..
   143   hence sup: "\<forall>x \<in> S. x \<le> u" by (simp add: isUb_def setle_def)
   144 
   145   obtain x where x_in_S: "x \<in> S" using not_empty_S ..
   146   hence x_gt_zero: "0 < x" using positive_S by simp
   147   have  "x \<le> u" using sup and x_in_S ..
   148   hence "0 < u" using x_gt_zero by arith
   149 
   150   then obtain pu where u_is_pu: "u = real_of_preal pu"
   151     by (auto simp add: real_gt_zero_preal_Ex)
   152 
   153   have pS_less_pu: "\<forall>pa \<in> ?pS. pa \<le> pu"
   154   proof
   155     fix pa
   156     assume "pa \<in> ?pS"
   157     then obtain a where "a \<in> S" and "a = real_of_preal pa"
   158       by simp
   159     moreover hence "a \<le> u" using sup by simp
   160     ultimately show "pa \<le> pu"
   161       using sup and u_is_pu by (simp add: real_of_preal_le_iff)
   162   qed
   163 
   164   have "\<forall>y \<in> S. y \<le> real_of_preal (psup ?pS)"
   165   proof
   166     fix y
   167     assume y_in_S: "y \<in> S"
   168     hence "0 < y" using positive_S by simp
   169     then obtain py where y_is_py: "y = real_of_preal py"
   170       by (auto simp add: real_gt_zero_preal_Ex)
   171     hence py_in_pS: "py \<in> ?pS" using y_in_S by simp
   172     with pS_less_pu have "py \<le> psup ?pS"
   173       by (rule preal_psup_le)
   174     thus "y \<le> real_of_preal (psup ?pS)"
   175       using y_is_py by (simp add: real_of_preal_le_iff)
   176   qed
   177 
   178   moreover {
   179     fix x
   180     assume x_ub_S: "\<forall>y\<in>S. y \<le> x"
   181     have "real_of_preal (psup ?pS) \<le> x"
   182     proof -
   183       obtain "s" where s_in_S: "s \<in> S" using not_empty_S ..
   184       hence s_pos: "0 < s" using positive_S by simp
   185 
   186       hence "\<exists> ps. s = real_of_preal ps" by (simp add: real_gt_zero_preal_Ex)
   187       then obtain "ps" where s_is_ps: "s = real_of_preal ps" ..
   188       hence ps_in_pS: "ps \<in> {w. real_of_preal w \<in> S}" using s_in_S by simp
   189 
   190       from x_ub_S have "s \<le> x" using s_in_S ..
   191       hence "0 < x" using s_pos by simp
   192       hence "\<exists> px. x = real_of_preal px" by (simp add: real_gt_zero_preal_Ex)
   193       then obtain "px" where x_is_px: "x = real_of_preal px" ..
   194 
   195       have "\<forall>pe \<in> ?pS. pe \<le> px"
   196       proof
   197 	fix pe
   198 	assume "pe \<in> ?pS"
   199 	hence "real_of_preal pe \<in> S" by simp
   200 	hence "real_of_preal pe \<le> x" using x_ub_S by simp
   201 	thus "pe \<le> px" using x_is_px by (simp add: real_of_preal_le_iff)
   202       qed
   203 
   204       moreover have "?pS \<noteq> {}" using ps_in_pS by auto
   205       ultimately have "(psup ?pS) \<le> px" by (simp add: psup_le_ub)
   206       thus "real_of_preal (psup ?pS) \<le> x" using x_is_px by (simp add: real_of_preal_le_iff)
   207     qed
   208   }
   209   ultimately show "isLub UNIV S (real_of_preal (psup ?pS))"
   210     by (simp add: isLub_def leastP_def isUb_def setle_def setge_def)
   211 qed
   212 
   213 text {*
   214   \medskip reals Completeness (again!)
   215 *}
   216 
   217 lemma reals_complete:
   218   assumes notempty_S: "\<exists>X. X \<in> S"
   219     and exists_Ub: "\<exists>Y. isUb (UNIV::real set) S Y"
   220   shows "\<exists>t. isLub (UNIV :: real set) S t"
   221 proof -
   222   obtain X where X_in_S: "X \<in> S" using notempty_S ..
   223   obtain Y where Y_isUb: "isUb (UNIV::real set) S Y"
   224     using exists_Ub ..
   225   let ?SHIFT = "{z. \<exists>x \<in>S. z = x + (-X) + 1} \<inter> {x. 0 < x}"
   226 
   227   {
   228     fix x
   229     assume "isUb (UNIV::real set) S x"
   230     hence S_le_x: "\<forall> y \<in> S. y <= x"
   231       by (simp add: isUb_def setle_def)
   232     {
   233       fix s
   234       assume "s \<in> {z. \<exists>x\<in>S. z = x + - X + 1}"
   235       hence "\<exists> x \<in> S. s = x + -X + 1" ..
   236       then obtain x1 where "x1 \<in> S" and "s = x1 + (-X) + 1" ..
   237       moreover hence "x1 \<le> x" using S_le_x by simp
   238       ultimately have "s \<le> x + - X + 1" by arith
   239     }
   240     then have "isUb (UNIV::real set) ?SHIFT (x + (-X) + 1)"
   241       by (auto simp add: isUb_def setle_def)
   242   } note S_Ub_is_SHIFT_Ub = this
   243 
   244   hence "isUb UNIV ?SHIFT (Y + (-X) + 1)" using Y_isUb by simp
   245   hence "\<exists>Z. isUb UNIV ?SHIFT Z" ..
   246   moreover have "\<forall>y \<in> ?SHIFT. 0 < y" by auto
   247   moreover have shifted_not_empty: "\<exists>u. u \<in> ?SHIFT"
   248     using X_in_S and Y_isUb by auto
   249   ultimately obtain t where t_is_Lub: "isLub UNIV ?SHIFT t"
   250     using posreals_complete [of ?SHIFT] by blast
   251 
   252   show ?thesis
   253   proof
   254     show "isLub UNIV S (t + X + (-1))"
   255     proof (rule isLubI2)
   256       {
   257         fix x
   258         assume "isUb (UNIV::real set) S x"
   259         hence "isUb (UNIV::real set) (?SHIFT) (x + (-X) + 1)"
   260 	  using S_Ub_is_SHIFT_Ub by simp
   261         hence "t \<le> (x + (-X) + 1)"
   262 	  using t_is_Lub by (simp add: isLub_le_isUb)
   263         hence "t + X + -1 \<le> x" by arith
   264       }
   265       then show "(t + X + -1) <=* Collect (isUb UNIV S)"
   266 	by (simp add: setgeI)
   267     next
   268       show "isUb UNIV S (t + X + -1)"
   269       proof -
   270         {
   271           fix y
   272           assume y_in_S: "y \<in> S"
   273           have "y \<le> t + X + -1"
   274           proof -
   275             obtain "u" where u_in_shift: "u \<in> ?SHIFT" using shifted_not_empty ..
   276             hence "\<exists> x \<in> S. u = x + - X + 1" by simp
   277             then obtain "x" where x_and_u: "u = x + - X + 1" ..
   278             have u_le_t: "u \<le> t" using u_in_shift and t_is_Lub by (simp add: isLubD2)
   279 
   280             show ?thesis
   281             proof cases
   282               assume "y \<le> x"
   283               moreover have "x = u + X + - 1" using x_and_u by arith
   284               moreover have "u + X + - 1  \<le> t + X + -1" using u_le_t by arith
   285               ultimately show "y  \<le> t + X + -1" by arith
   286             next
   287               assume "~(y \<le> x)"
   288               hence x_less_y: "x < y" by arith
   289 
   290               have "x + (-X) + 1 \<in> ?SHIFT" using x_and_u and u_in_shift by simp
   291               hence "0 < x + (-X) + 1" by simp
   292               hence "0 < y + (-X) + 1" using x_less_y by arith
   293               hence "y + (-X) + 1 \<in> ?SHIFT" using y_in_S by simp
   294               hence "y + (-X) + 1 \<le> t" using t_is_Lub  by (simp add: isLubD2)
   295               thus ?thesis by simp
   296             qed
   297           qed
   298         }
   299         then show ?thesis by (simp add: isUb_def setle_def)
   300       qed
   301     qed
   302   qed
   303 qed
   304 
   305 
   306 subsection {* The Archimedean Property of the Reals *}
   307 
   308 theorem reals_Archimedean:
   309   assumes x_pos: "0 < x"
   310   shows "\<exists>n. inverse (real (Suc n)) < x"
   311 proof (rule ccontr)
   312   assume contr: "\<not> ?thesis"
   313   have "\<forall>n. x * real (Suc n) <= 1"
   314   proof
   315     fix n
   316     from contr have "x \<le> inverse (real (Suc n))"
   317       by (simp add: linorder_not_less)
   318     hence "x \<le> (1 / (real (Suc n)))"
   319       by (simp add: inverse_eq_divide)
   320     moreover have "0 \<le> real (Suc n)"
   321       by (rule real_of_nat_ge_zero)
   322     ultimately have "x * real (Suc n) \<le> (1 / real (Suc n)) * real (Suc n)"
   323       by (rule mult_right_mono)
   324     thus "x * real (Suc n) \<le> 1" by simp
   325   qed
   326   hence "{z. \<exists>n. z = x * (real (Suc n))} *<= 1"
   327     by (simp add: setle_def, safe, rule spec)
   328   hence "isUb (UNIV::real set) {z. \<exists>n. z = x * (real (Suc n))} 1"
   329     by (simp add: isUbI)
   330   hence "\<exists>Y. isUb (UNIV::real set) {z. \<exists>n. z = x* (real (Suc n))} Y" ..
   331   moreover have "\<exists>X. X \<in> {z. \<exists>n. z = x* (real (Suc n))}" by auto
   332   ultimately have "\<exists>t. isLub UNIV {z. \<exists>n. z = x * real (Suc n)} t"
   333     by (simp add: reals_complete)
   334   then obtain "t" where
   335     t_is_Lub: "isLub UNIV {z. \<exists>n. z = x * real (Suc n)} t" ..
   336 
   337   have "\<forall>n::nat. x * real n \<le> t + - x"
   338   proof
   339     fix n
   340     from t_is_Lub have "x * real (Suc n) \<le> t"
   341       by (simp add: isLubD2)
   342     hence  "x * (real n) + x \<le> t"
   343       by (simp add: right_distrib real_of_nat_Suc)
   344     thus  "x * (real n) \<le> t + - x" by arith
   345   qed
   346 
   347   hence "\<forall>m. x * real (Suc m) \<le> t + - x" by simp
   348   hence "{z. \<exists>n. z = x * (real (Suc n))}  *<= (t + - x)"
   349     by (auto simp add: setle_def)
   350   hence "isUb (UNIV::real set) {z. \<exists>n. z = x * (real (Suc n))} (t + (-x))"
   351     by (simp add: isUbI)
   352   hence "t \<le> t + - x"
   353     using t_is_Lub by (simp add: isLub_le_isUb)
   354   thus False using x_pos by arith
   355 qed
   356 
   357 text {*
   358   There must be other proofs, e.g. @{text "Suc"} of the largest
   359   integer in the cut representing @{text "x"}.
   360 *}
   361 
   362 lemma reals_Archimedean2: "\<exists>n. (x::real) < real (n::nat)"
   363 proof cases
   364   assume "x \<le> 0"
   365   hence "x < real (1::nat)" by simp
   366   thus ?thesis ..
   367 next
   368   assume "\<not> x \<le> 0"
   369   hence x_greater_zero: "0 < x" by simp
   370   hence "0 < inverse x" by simp
   371   then obtain n where "inverse (real (Suc n)) < inverse x"
   372     using reals_Archimedean by blast
   373   hence "inverse (real (Suc n)) * x < inverse x * x"
   374     using x_greater_zero by (rule mult_strict_right_mono)
   375   hence "inverse (real (Suc n)) * x < 1"
   376     using x_greater_zero by (simp add: real_mult_inverse_left mult_commute)
   377   hence "real (Suc n) * (inverse (real (Suc n)) * x) < real (Suc n) * 1"
   378     by (rule mult_strict_left_mono) simp
   379   hence "x < real (Suc n)"
   380     by (simp add: mult_commute ring_eq_simps real_mult_inverse_left)
   381   thus "\<exists>(n::nat). x < real n" ..
   382 qed
   383 
   384 lemma reals_Archimedean3:
   385   assumes x_greater_zero: "0 < x"
   386   shows "\<forall>(y::real). \<exists>(n::nat). y < real n * x"
   387 proof
   388   fix y
   389   have x_not_zero: "x \<noteq> 0" using x_greater_zero by simp
   390   obtain n where "y * inverse x < real (n::nat)"
   391     using reals_Archimedean2 ..
   392   hence "y * inverse x * x < real n * x"
   393     using x_greater_zero by (simp add: mult_strict_right_mono)
   394   hence "x * inverse x * y < x * real n"
   395     by (simp add: mult_commute ring_eq_simps)
   396   hence "y < real (n::nat) * x"
   397     using x_not_zero by (simp add: real_mult_inverse_left ring_eq_simps)
   398   thus "\<exists>(n::nat). y < real n * x" ..
   399 qed
   400 
   401 lemma reals_Archimedean6:
   402      "0 \<le> r ==> \<exists>(n::nat). real (n - 1) \<le> r & r < real (n)"
   403 apply (insert reals_Archimedean2 [of r], safe)
   404 apply (frule_tac P = "%k. r < real k" and k = n and m = "%x. x"
   405        in ex_has_least_nat, auto)
   406 apply (rule_tac x = x in exI)
   407 apply (case_tac x, simp)
   408 apply (rename_tac x')
   409 apply (drule_tac x = x' in spec, simp)
   410 done
   411 
   412 lemma reals_Archimedean6a: "0 \<le> r ==> \<exists>n. real (n) \<le> r & r < real (Suc n)"
   413   by (drule reals_Archimedean6) auto
   414 
   415 lemma reals_Archimedean_6b_int:
   416      "0 \<le> r ==> \<exists>n::int. real n \<le> r & r < real (n+1)"
   417 apply (drule reals_Archimedean6a, auto)
   418 apply (rule_tac x = "int n" in exI)
   419 apply (simp add: real_of_int_real_of_nat real_of_nat_Suc)
   420 done
   421 
   422 lemma reals_Archimedean_6c_int:
   423      "r < 0 ==> \<exists>n::int. real n \<le> r & r < real (n+1)"
   424 apply (rule reals_Archimedean_6b_int [of "-r", THEN exE], simp, auto)
   425 apply (rename_tac n)
   426 apply (drule real_le_imp_less_or_eq, auto)
   427 apply (rule_tac x = "- n - 1" in exI)
   428 apply (rule_tac [2] x = "- n" in exI, auto)
   429 done
   430 
   431 
   432 subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
   433 
   434 definition
   435   floor :: "real => int" where
   436   "floor r = (LEAST n::int. r < real (n+1))"
   437 
   438 definition
   439   ceiling :: "real => int" where
   440   "ceiling r = - floor (- r)"
   441 
   442 notation (xsymbols)
   443   floor  ("\<lfloor>_\<rfloor>") and
   444   ceiling  ("\<lceil>_\<rceil>")
   445 
   446 notation (HTML output)
   447   floor  ("\<lfloor>_\<rfloor>") and
   448   ceiling  ("\<lceil>_\<rceil>")
   449 
   450 
   451 lemma number_of_less_real_of_int_iff [simp]:
   452      "((number_of n) < real (m::int)) = (number_of n < m)"
   453 apply auto
   454 apply (rule real_of_int_less_iff [THEN iffD1])
   455 apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
   456 done
   457 
   458 lemma number_of_less_real_of_int_iff2 [simp]:
   459      "(real (m::int) < (number_of n)) = (m < number_of n)"
   460 apply auto
   461 apply (rule real_of_int_less_iff [THEN iffD1])
   462 apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
   463 done
   464 
   465 lemma number_of_le_real_of_int_iff [simp]:
   466      "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
   467 by (simp add: linorder_not_less [symmetric])
   468 
   469 lemma number_of_le_real_of_int_iff2 [simp]:
   470      "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"
   471 by (simp add: linorder_not_less [symmetric])
   472 
   473 lemma floor_zero [simp]: "floor 0 = 0"
   474 apply (simp add: floor_def del: real_of_int_add)
   475 apply (rule Least_equality)
   476 apply simp_all
   477 done
   478 
   479 lemma floor_real_of_nat_zero [simp]: "floor (real (0::nat)) = 0"
   480 by auto
   481 
   482 lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
   483 apply (simp only: floor_def)
   484 apply (rule Least_equality)
   485 apply (drule_tac [2] real_of_int_real_of_nat [THEN ssubst])
   486 apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
   487 apply (simp_all add: real_of_int_real_of_nat)
   488 done
   489 
   490 lemma floor_minus_real_of_nat [simp]: "floor (- real (n::nat)) = - int n"
   491 apply (simp only: floor_def)
   492 apply (rule Least_equality)
   493 apply (drule_tac [2] real_of_int_real_of_nat [THEN ssubst])
   494 apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
   495 apply (drule_tac [2] real_of_int_less_iff [THEN iffD1])
   496 apply (simp_all add: real_of_int_real_of_nat)
   497 done
   498 
   499 lemma floor_real_of_int [simp]: "floor (real (n::int)) = n"
   500 apply (simp only: floor_def)
   501 apply (rule Least_equality)
   502 apply (drule_tac [2] real_of_int_real_of_nat [THEN ssubst])
   503 apply (drule_tac [2] real_of_int_less_iff [THEN iffD1], auto)
   504 done
   505 
   506 lemma floor_minus_real_of_int [simp]: "floor (- real (n::int)) = - n"
   507 apply (simp only: floor_def)
   508 apply (rule Least_equality)
   509 apply (drule_tac [2] real_of_int_minus [THEN sym, THEN subst])
   510 apply (drule_tac [2] real_of_int_real_of_nat [THEN ssubst])
   511 apply (drule_tac [2] real_of_int_less_iff [THEN iffD1], auto)
   512 done
   513 
   514 lemma real_lb_ub_int: " \<exists>n::int. real n \<le> r & r < real (n+1)"
   515 apply (case_tac "r < 0")
   516 apply (blast intro: reals_Archimedean_6c_int)
   517 apply (simp only: linorder_not_less)
   518 apply (blast intro: reals_Archimedean_6b_int reals_Archimedean_6c_int)
   519 done
   520 
   521 lemma lemma_floor:
   522   assumes a1: "real m \<le> r" and a2: "r < real n + 1"
   523   shows "m \<le> (n::int)"
   524 proof -
   525   have "real m < real n + 1" by (rule order_le_less_trans)
   526   also have "... = real(n+1)" by simp
   527   finally have "m < n+1" by (simp only: real_of_int_less_iff)
   528   thus ?thesis by arith
   529 qed
   530 
   531 lemma real_of_int_floor_le [simp]: "real (floor r) \<le> r"
   532 apply (simp add: floor_def Least_def)
   533 apply (insert real_lb_ub_int [of r], safe)
   534 apply (rule theI2)
   535 apply auto
   536 done
   537 
   538 lemma floor_mono: "x < y ==> floor x \<le> floor y"
   539 apply (simp add: floor_def Least_def)
   540 apply (insert real_lb_ub_int [of x])
   541 apply (insert real_lb_ub_int [of y], safe)
   542 apply (rule theI2)
   543 apply (rule_tac [3] theI2)
   544 apply simp
   545 apply (erule conjI)
   546 apply (auto simp add: order_eq_iff int_le_real_less)
   547 done
   548 
   549 lemma floor_mono2: "x \<le> y ==> floor x \<le> floor y"
   550 by (auto dest: real_le_imp_less_or_eq simp add: floor_mono)
   551 
   552 lemma lemma_floor2: "real n < real (x::int) + 1 ==> n \<le> x"
   553 by (auto intro: lemma_floor)
   554 
   555 lemma real_of_int_floor_cancel [simp]:
   556     "(real (floor x) = x) = (\<exists>n::int. x = real n)"
   557 apply (simp add: floor_def Least_def)
   558 apply (insert real_lb_ub_int [of x], erule exE)
   559 apply (rule theI2)
   560 apply (auto intro: lemma_floor)
   561 done
   562 
   563 lemma floor_eq: "[| real n < x; x < real n + 1 |] ==> floor x = n"
   564 apply (simp add: floor_def)
   565 apply (rule Least_equality)
   566 apply (auto intro: lemma_floor)
   567 done
   568 
   569 lemma floor_eq2: "[| real n \<le> x; x < real n + 1 |] ==> floor x = n"
   570 apply (simp add: floor_def)
   571 apply (rule Least_equality)
   572 apply (auto intro: lemma_floor)
   573 done
   574 
   575 lemma floor_eq3: "[| real n < x; x < real (Suc n) |] ==> nat(floor x) = n"
   576 apply (rule inj_int [THEN injD])
   577 apply (simp add: real_of_nat_Suc)
   578 apply (simp add: real_of_nat_Suc floor_eq floor_eq [where n = "int n"])
   579 done
   580 
   581 lemma floor_eq4: "[| real n \<le> x; x < real (Suc n) |] ==> nat(floor x) = n"
   582 apply (drule order_le_imp_less_or_eq)
   583 apply (auto intro: floor_eq3)
   584 done
   585 
   586 lemma floor_number_of_eq [simp]:
   587      "floor(number_of n :: real) = (number_of n :: int)"
   588 apply (subst real_number_of [symmetric])
   589 apply (rule floor_real_of_int)
   590 done
   591 
   592 lemma floor_one [simp]: "floor 1 = 1"
   593   apply (rule trans)
   594   prefer 2
   595   apply (rule floor_real_of_int)
   596   apply simp
   597 done
   598 
   599 lemma real_of_int_floor_ge_diff_one [simp]: "r - 1 \<le> real(floor r)"
   600 apply (simp add: floor_def Least_def)
   601 apply (insert real_lb_ub_int [of r], safe)
   602 apply (rule theI2)
   603 apply (auto intro: lemma_floor)
   604 done
   605 
   606 lemma real_of_int_floor_gt_diff_one [simp]: "r - 1 < real(floor r)"
   607 apply (simp add: floor_def Least_def)
   608 apply (insert real_lb_ub_int [of r], safe)
   609 apply (rule theI2)
   610 apply (auto intro: lemma_floor)
   611 done
   612 
   613 lemma real_of_int_floor_add_one_ge [simp]: "r \<le> real(floor r) + 1"
   614 apply (insert real_of_int_floor_ge_diff_one [of r])
   615 apply (auto simp del: real_of_int_floor_ge_diff_one)
   616 done
   617 
   618 lemma real_of_int_floor_add_one_gt [simp]: "r < real(floor r) + 1"
   619 apply (insert real_of_int_floor_gt_diff_one [of r])
   620 apply (auto simp del: real_of_int_floor_gt_diff_one)
   621 done
   622 
   623 lemma le_floor: "real a <= x ==> a <= floor x"
   624   apply (subgoal_tac "a < floor x + 1")
   625   apply arith
   626   apply (subst real_of_int_less_iff [THEN sym])
   627   apply simp
   628   apply (insert real_of_int_floor_add_one_gt [of x])
   629   apply arith
   630 done
   631 
   632 lemma real_le_floor: "a <= floor x ==> real a <= x"
   633   apply (rule order_trans)
   634   prefer 2
   635   apply (rule real_of_int_floor_le)
   636   apply (subst real_of_int_le_iff)
   637   apply assumption
   638 done
   639 
   640 lemma le_floor_eq: "(a <= floor x) = (real a <= x)"
   641   apply (rule iffI)
   642   apply (erule real_le_floor)
   643   apply (erule le_floor)
   644 done
   645 
   646 lemma le_floor_eq_number_of [simp]:
   647     "(number_of n <= floor x) = (number_of n <= x)"
   648 by (simp add: le_floor_eq)
   649 
   650 lemma le_floor_eq_zero [simp]: "(0 <= floor x) = (0 <= x)"
   651 by (simp add: le_floor_eq)
   652 
   653 lemma le_floor_eq_one [simp]: "(1 <= floor x) = (1 <= x)"
   654 by (simp add: le_floor_eq)
   655 
   656 lemma floor_less_eq: "(floor x < a) = (x < real a)"
   657   apply (subst linorder_not_le [THEN sym])+
   658   apply simp
   659   apply (rule le_floor_eq)
   660 done
   661 
   662 lemma floor_less_eq_number_of [simp]:
   663     "(floor x < number_of n) = (x < number_of n)"
   664 by (simp add: floor_less_eq)
   665 
   666 lemma floor_less_eq_zero [simp]: "(floor x < 0) = (x < 0)"
   667 by (simp add: floor_less_eq)
   668 
   669 lemma floor_less_eq_one [simp]: "(floor x < 1) = (x < 1)"
   670 by (simp add: floor_less_eq)
   671 
   672 lemma less_floor_eq: "(a < floor x) = (real a + 1 <= x)"
   673   apply (insert le_floor_eq [of "a + 1" x])
   674   apply auto
   675 done
   676 
   677 lemma less_floor_eq_number_of [simp]:
   678     "(number_of n < floor x) = (number_of n + 1 <= x)"
   679 by (simp add: less_floor_eq)
   680 
   681 lemma less_floor_eq_zero [simp]: "(0 < floor x) = (1 <= x)"
   682 by (simp add: less_floor_eq)
   683 
   684 lemma less_floor_eq_one [simp]: "(1 < floor x) = (2 <= x)"
   685 by (simp add: less_floor_eq)
   686 
   687 lemma floor_le_eq: "(floor x <= a) = (x < real a + 1)"
   688   apply (insert floor_less_eq [of x "a + 1"])
   689   apply auto
   690 done
   691 
   692 lemma floor_le_eq_number_of [simp]:
   693     "(floor x <= number_of n) = (x < number_of n + 1)"
   694 by (simp add: floor_le_eq)
   695 
   696 lemma floor_le_eq_zero [simp]: "(floor x <= 0) = (x < 1)"
   697 by (simp add: floor_le_eq)
   698 
   699 lemma floor_le_eq_one [simp]: "(floor x <= 1) = (x < 2)"
   700 by (simp add: floor_le_eq)
   701 
   702 lemma floor_add [simp]: "floor (x + real a) = floor x + a"
   703   apply (subst order_eq_iff)
   704   apply (rule conjI)
   705   prefer 2
   706   apply (subgoal_tac "floor x + a < floor (x + real a) + 1")
   707   apply arith
   708   apply (subst real_of_int_less_iff [THEN sym])
   709   apply simp
   710   apply (subgoal_tac "x + real a < real(floor(x + real a)) + 1")
   711   apply (subgoal_tac "real (floor x) <= x")
   712   apply arith
   713   apply (rule real_of_int_floor_le)
   714   apply (rule real_of_int_floor_add_one_gt)
   715   apply (subgoal_tac "floor (x + real a) < floor x + a + 1")
   716   apply arith
   717   apply (subst real_of_int_less_iff [THEN sym])
   718   apply simp
   719   apply (subgoal_tac "real(floor(x + real a)) <= x + real a")
   720   apply (subgoal_tac "x < real(floor x) + 1")
   721   apply arith
   722   apply (rule real_of_int_floor_add_one_gt)
   723   apply (rule real_of_int_floor_le)
   724 done
   725 
   726 lemma floor_add_number_of [simp]:
   727     "floor (x + number_of n) = floor x + number_of n"
   728   apply (subst floor_add [THEN sym])
   729   apply simp
   730 done
   731 
   732 lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
   733   apply (subst floor_add [THEN sym])
   734   apply simp
   735 done
   736 
   737 lemma floor_subtract [simp]: "floor (x - real a) = floor x - a"
   738   apply (subst diff_minus)+
   739   apply (subst real_of_int_minus [THEN sym])
   740   apply (rule floor_add)
   741 done
   742 
   743 lemma floor_subtract_number_of [simp]: "floor (x - number_of n) =
   744     floor x - number_of n"
   745   apply (subst floor_subtract [THEN sym])
   746   apply simp
   747 done
   748 
   749 lemma floor_subtract_one [simp]: "floor (x - 1) = floor x - 1"
   750   apply (subst floor_subtract [THEN sym])
   751   apply simp
   752 done
   753 
   754 lemma ceiling_zero [simp]: "ceiling 0 = 0"
   755 by (simp add: ceiling_def)
   756 
   757 lemma ceiling_real_of_nat [simp]: "ceiling (real (n::nat)) = int n"
   758 by (simp add: ceiling_def)
   759 
   760 lemma ceiling_real_of_nat_zero [simp]: "ceiling (real (0::nat)) = 0"
   761 by auto
   762 
   763 lemma ceiling_floor [simp]: "ceiling (real (floor r)) = floor r"
   764 by (simp add: ceiling_def)
   765 
   766 lemma floor_ceiling [simp]: "floor (real (ceiling r)) = ceiling r"
   767 by (simp add: ceiling_def)
   768 
   769 lemma real_of_int_ceiling_ge [simp]: "r \<le> real (ceiling r)"
   770 apply (simp add: ceiling_def)
   771 apply (subst le_minus_iff, simp)
   772 done
   773 
   774 lemma ceiling_mono: "x < y ==> ceiling x \<le> ceiling y"
   775 by (simp add: floor_mono ceiling_def)
   776 
   777 lemma ceiling_mono2: "x \<le> y ==> ceiling x \<le> ceiling y"
   778 by (simp add: floor_mono2 ceiling_def)
   779 
   780 lemma real_of_int_ceiling_cancel [simp]:
   781      "(real (ceiling x) = x) = (\<exists>n::int. x = real n)"
   782 apply (auto simp add: ceiling_def)
   783 apply (drule arg_cong [where f = uminus], auto)
   784 apply (rule_tac x = "-n" in exI, auto)
   785 done
   786 
   787 lemma ceiling_eq: "[| real n < x; x < real n + 1 |] ==> ceiling x = n + 1"
   788 apply (simp add: ceiling_def)
   789 apply (rule minus_equation_iff [THEN iffD1])
   790 apply (simp add: floor_eq [where n = "-(n+1)"])
   791 done
   792 
   793 lemma ceiling_eq2: "[| real n < x; x \<le> real n + 1 |] ==> ceiling x = n + 1"
   794 by (simp add: ceiling_def floor_eq2 [where n = "-(n+1)"])
   795 
   796 lemma ceiling_eq3: "[| real n - 1 < x; x \<le> real n  |] ==> ceiling x = n"
   797 by (simp add: ceiling_def floor_eq2 [where n = "-n"])
   798 
   799 lemma ceiling_real_of_int [simp]: "ceiling (real (n::int)) = n"
   800 by (simp add: ceiling_def)
   801 
   802 lemma ceiling_number_of_eq [simp]:
   803      "ceiling (number_of n :: real) = (number_of n)"
   804 apply (subst real_number_of [symmetric])
   805 apply (rule ceiling_real_of_int)
   806 done
   807 
   808 lemma ceiling_one [simp]: "ceiling 1 = 1"
   809   by (unfold ceiling_def, simp)
   810 
   811 lemma real_of_int_ceiling_diff_one_le [simp]: "real (ceiling r) - 1 \<le> r"
   812 apply (rule neg_le_iff_le [THEN iffD1])
   813 apply (simp add: ceiling_def diff_minus)
   814 done
   815 
   816 lemma real_of_int_ceiling_le_add_one [simp]: "real (ceiling r) \<le> r + 1"
   817 apply (insert real_of_int_ceiling_diff_one_le [of r])
   818 apply (simp del: real_of_int_ceiling_diff_one_le)
   819 done
   820 
   821 lemma ceiling_le: "x <= real a ==> ceiling x <= a"
   822   apply (unfold ceiling_def)
   823   apply (subgoal_tac "-a <= floor(- x)")
   824   apply simp
   825   apply (rule le_floor)
   826   apply simp
   827 done
   828 
   829 lemma ceiling_le_real: "ceiling x <= a ==> x <= real a"
   830   apply (unfold ceiling_def)
   831   apply (subgoal_tac "real(- a) <= - x")
   832   apply simp
   833   apply (rule real_le_floor)
   834   apply simp
   835 done
   836 
   837 lemma ceiling_le_eq: "(ceiling x <= a) = (x <= real a)"
   838   apply (rule iffI)
   839   apply (erule ceiling_le_real)
   840   apply (erule ceiling_le)
   841 done
   842 
   843 lemma ceiling_le_eq_number_of [simp]:
   844     "(ceiling x <= number_of n) = (x <= number_of n)"
   845 by (simp add: ceiling_le_eq)
   846 
   847 lemma ceiling_le_zero_eq [simp]: "(ceiling x <= 0) = (x <= 0)"
   848 by (simp add: ceiling_le_eq)
   849 
   850 lemma ceiling_le_eq_one [simp]: "(ceiling x <= 1) = (x <= 1)"
   851 by (simp add: ceiling_le_eq)
   852 
   853 lemma less_ceiling_eq: "(a < ceiling x) = (real a < x)"
   854   apply (subst linorder_not_le [THEN sym])+
   855   apply simp
   856   apply (rule ceiling_le_eq)
   857 done
   858 
   859 lemma less_ceiling_eq_number_of [simp]:
   860     "(number_of n < ceiling x) = (number_of n < x)"
   861 by (simp add: less_ceiling_eq)
   862 
   863 lemma less_ceiling_eq_zero [simp]: "(0 < ceiling x) = (0 < x)"
   864 by (simp add: less_ceiling_eq)
   865 
   866 lemma less_ceiling_eq_one [simp]: "(1 < ceiling x) = (1 < x)"
   867 by (simp add: less_ceiling_eq)
   868 
   869 lemma ceiling_less_eq: "(ceiling x < a) = (x <= real a - 1)"
   870   apply (insert ceiling_le_eq [of x "a - 1"])
   871   apply auto
   872 done
   873 
   874 lemma ceiling_less_eq_number_of [simp]:
   875     "(ceiling x < number_of n) = (x <= number_of n - 1)"
   876 by (simp add: ceiling_less_eq)
   877 
   878 lemma ceiling_less_eq_zero [simp]: "(ceiling x < 0) = (x <= -1)"
   879 by (simp add: ceiling_less_eq)
   880 
   881 lemma ceiling_less_eq_one [simp]: "(ceiling x < 1) = (x <= 0)"
   882 by (simp add: ceiling_less_eq)
   883 
   884 lemma le_ceiling_eq: "(a <= ceiling x) = (real a - 1 < x)"
   885   apply (insert less_ceiling_eq [of "a - 1" x])
   886   apply auto
   887 done
   888 
   889 lemma le_ceiling_eq_number_of [simp]:
   890     "(number_of n <= ceiling x) = (number_of n - 1 < x)"
   891 by (simp add: le_ceiling_eq)
   892 
   893 lemma le_ceiling_eq_zero [simp]: "(0 <= ceiling x) = (-1 < x)"
   894 by (simp add: le_ceiling_eq)
   895 
   896 lemma le_ceiling_eq_one [simp]: "(1 <= ceiling x) = (0 < x)"
   897 by (simp add: le_ceiling_eq)
   898 
   899 lemma ceiling_add [simp]: "ceiling (x + real a) = ceiling x + a"
   900   apply (unfold ceiling_def, simp)
   901   apply (subst real_of_int_minus [THEN sym])
   902   apply (subst floor_add)
   903   apply simp
   904 done
   905 
   906 lemma ceiling_add_number_of [simp]: "ceiling (x + number_of n) =
   907     ceiling x + number_of n"
   908   apply (subst ceiling_add [THEN sym])
   909   apply simp
   910 done
   911 
   912 lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
   913   apply (subst ceiling_add [THEN sym])
   914   apply simp
   915 done
   916 
   917 lemma ceiling_subtract [simp]: "ceiling (x - real a) = ceiling x - a"
   918   apply (subst diff_minus)+
   919   apply (subst real_of_int_minus [THEN sym])
   920   apply (rule ceiling_add)
   921 done
   922 
   923 lemma ceiling_subtract_number_of [simp]: "ceiling (x - number_of n) =
   924     ceiling x - number_of n"
   925   apply (subst ceiling_subtract [THEN sym])
   926   apply simp
   927 done
   928 
   929 lemma ceiling_subtract_one [simp]: "ceiling (x - 1) = ceiling x - 1"
   930   apply (subst ceiling_subtract [THEN sym])
   931   apply simp
   932 done
   933 
   934 subsection {* Versions for the natural numbers *}
   935 
   936 definition
   937   natfloor :: "real => nat" where
   938   "natfloor x = nat(floor x)"
   939 
   940 definition
   941   natceiling :: "real => nat" where
   942   "natceiling x = nat(ceiling x)"
   943 
   944 lemma natfloor_zero [simp]: "natfloor 0 = 0"
   945   by (unfold natfloor_def, simp)
   946 
   947 lemma natfloor_one [simp]: "natfloor 1 = 1"
   948   by (unfold natfloor_def, simp)
   949 
   950 lemma zero_le_natfloor [simp]: "0 <= natfloor x"
   951   by (unfold natfloor_def, simp)
   952 
   953 lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
   954   by (unfold natfloor_def, simp)
   955 
   956 lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
   957   by (unfold natfloor_def, simp)
   958 
   959 lemma real_natfloor_le: "0 <= x ==> real(natfloor x) <= x"
   960   by (unfold natfloor_def, simp)
   961 
   962 lemma natfloor_neg: "x <= 0 ==> natfloor x = 0"
   963   apply (unfold natfloor_def)
   964   apply (subgoal_tac "floor x <= floor 0")
   965   apply simp
   966   apply (erule floor_mono2)
   967 done
   968 
   969 lemma natfloor_mono: "x <= y ==> natfloor x <= natfloor y"
   970   apply (case_tac "0 <= x")
   971   apply (subst natfloor_def)+
   972   apply (subst nat_le_eq_zle)
   973   apply force
   974   apply (erule floor_mono2)
   975   apply (subst natfloor_neg)
   976   apply simp
   977   apply simp
   978 done
   979 
   980 lemma le_natfloor: "real x <= a ==> x <= natfloor a"
   981   apply (unfold natfloor_def)
   982   apply (subst nat_int [THEN sym])
   983   apply (subst nat_le_eq_zle)
   984   apply simp
   985   apply (rule le_floor)
   986   apply simp
   987 done
   988 
   989 lemma le_natfloor_eq: "0 <= x ==> (a <= natfloor x) = (real a <= x)"
   990   apply (rule iffI)
   991   apply (rule order_trans)
   992   prefer 2
   993   apply (erule real_natfloor_le)
   994   apply (subst real_of_nat_le_iff)
   995   apply assumption
   996   apply (erule le_natfloor)
   997 done
   998 
   999 lemma le_natfloor_eq_number_of [simp]:
  1000     "~ neg((number_of n)::int) ==> 0 <= x ==>
  1001       (number_of n <= natfloor x) = (number_of n <= x)"
  1002   apply (subst le_natfloor_eq, assumption)
  1003   apply simp
  1004 done
  1005 
  1006 lemma le_natfloor_eq_one [simp]: "(1 <= natfloor x) = (1 <= x)"
  1007   apply (case_tac "0 <= x")
  1008   apply (subst le_natfloor_eq, assumption, simp)
  1009   apply (rule iffI)
  1010   apply (subgoal_tac "natfloor x <= natfloor 0")
  1011   apply simp
  1012   apply (rule natfloor_mono)
  1013   apply simp
  1014   apply simp
  1015 done
  1016 
  1017 lemma natfloor_eq: "real n <= x ==> x < real n + 1 ==> natfloor x = n"
  1018   apply (unfold natfloor_def)
  1019   apply (subst nat_int [THEN sym]);back;
  1020   apply (subst eq_nat_nat_iff)
  1021   apply simp
  1022   apply simp
  1023   apply (rule floor_eq2)
  1024   apply auto
  1025 done
  1026 
  1027 lemma real_natfloor_add_one_gt: "x < real(natfloor x) + 1"
  1028   apply (case_tac "0 <= x")
  1029   apply (unfold natfloor_def)
  1030   apply simp
  1031   apply simp_all
  1032 done
  1033 
  1034 lemma real_natfloor_gt_diff_one: "x - 1 < real(natfloor x)"
  1035   apply (simp add: compare_rls)
  1036   apply (rule real_natfloor_add_one_gt)
  1037 done
  1038 
  1039 lemma ge_natfloor_plus_one_imp_gt: "natfloor z + 1 <= n ==> z < real n"
  1040   apply (subgoal_tac "z < real(natfloor z) + 1")
  1041   apply arith
  1042   apply (rule real_natfloor_add_one_gt)
  1043 done
  1044 
  1045 lemma natfloor_add [simp]: "0 <= x ==> natfloor (x + real a) = natfloor x + a"
  1046   apply (unfold natfloor_def)
  1047   apply (subgoal_tac "real a = real (int a)")
  1048   apply (erule ssubst)
  1049   apply (simp add: nat_add_distrib)
  1050   apply simp
  1051 done
  1052 
  1053 lemma natfloor_add_number_of [simp]:
  1054     "~neg ((number_of n)::int) ==> 0 <= x ==>
  1055       natfloor (x + number_of n) = natfloor x + number_of n"
  1056   apply (subst natfloor_add [THEN sym])
  1057   apply simp_all
  1058 done
  1059 
  1060 lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
  1061   apply (subst natfloor_add [THEN sym])
  1062   apply assumption
  1063   apply simp
  1064 done
  1065 
  1066 lemma natfloor_subtract [simp]: "real a <= x ==>
  1067     natfloor(x - real a) = natfloor x - a"
  1068   apply (unfold natfloor_def)
  1069   apply (subgoal_tac "real a = real (int a)")
  1070   apply (erule ssubst)
  1071   apply simp
  1072   apply simp
  1073 done
  1074 
  1075 lemma natceiling_zero [simp]: "natceiling 0 = 0"
  1076   by (unfold natceiling_def, simp)
  1077 
  1078 lemma natceiling_one [simp]: "natceiling 1 = 1"
  1079   by (unfold natceiling_def, simp)
  1080 
  1081 lemma zero_le_natceiling [simp]: "0 <= natceiling x"
  1082   by (unfold natceiling_def, simp)
  1083 
  1084 lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
  1085   by (unfold natceiling_def, simp)
  1086 
  1087 lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
  1088   by (unfold natceiling_def, simp)
  1089 
  1090 lemma real_natceiling_ge: "x <= real(natceiling x)"
  1091   apply (unfold natceiling_def)
  1092   apply (case_tac "x < 0")
  1093   apply simp
  1094   apply (subst real_nat_eq_real)
  1095   apply (subgoal_tac "ceiling 0 <= ceiling x")
  1096   apply simp
  1097   apply (rule ceiling_mono2)
  1098   apply simp
  1099   apply simp
  1100 done
  1101 
  1102 lemma natceiling_neg: "x <= 0 ==> natceiling x = 0"
  1103   apply (unfold natceiling_def)
  1104   apply simp
  1105 done
  1106 
  1107 lemma natceiling_mono: "x <= y ==> natceiling x <= natceiling y"
  1108   apply (case_tac "0 <= x")
  1109   apply (subst natceiling_def)+
  1110   apply (subst nat_le_eq_zle)
  1111   apply (rule disjI2)
  1112   apply (subgoal_tac "real (0::int) <= real(ceiling y)")
  1113   apply simp
  1114   apply (rule order_trans)
  1115   apply simp
  1116   apply (erule order_trans)
  1117   apply simp
  1118   apply (erule ceiling_mono2)
  1119   apply (subst natceiling_neg)
  1120   apply simp_all
  1121 done
  1122 
  1123 lemma natceiling_le: "x <= real a ==> natceiling x <= a"
  1124   apply (unfold natceiling_def)
  1125   apply (case_tac "x < 0")
  1126   apply simp
  1127   apply (subst nat_int [THEN sym]);back;
  1128   apply (subst nat_le_eq_zle)
  1129   apply simp
  1130   apply (rule ceiling_le)
  1131   apply simp
  1132 done
  1133 
  1134 lemma natceiling_le_eq: "0 <= x ==> (natceiling x <= a) = (x <= real a)"
  1135   apply (rule iffI)
  1136   apply (rule order_trans)
  1137   apply (rule real_natceiling_ge)
  1138   apply (subst real_of_nat_le_iff)
  1139   apply assumption
  1140   apply (erule natceiling_le)
  1141 done
  1142 
  1143 lemma natceiling_le_eq_number_of [simp]:
  1144     "~ neg((number_of n)::int) ==> 0 <= x ==>
  1145       (natceiling x <= number_of n) = (x <= number_of n)"
  1146   apply (subst natceiling_le_eq, assumption)
  1147   apply simp
  1148 done
  1149 
  1150 lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
  1151   apply (case_tac "0 <= x")
  1152   apply (subst natceiling_le_eq)
  1153   apply assumption
  1154   apply simp
  1155   apply (subst natceiling_neg)
  1156   apply simp
  1157   apply simp
  1158 done
  1159 
  1160 lemma natceiling_eq: "real n < x ==> x <= real n + 1 ==> natceiling x = n + 1"
  1161   apply (unfold natceiling_def)
  1162   apply (simplesubst nat_int [THEN sym]) back back
  1163   apply (subgoal_tac "nat(int n) + 1 = nat(int n + 1)")
  1164   apply (erule ssubst)
  1165   apply (subst eq_nat_nat_iff)
  1166   apply (subgoal_tac "ceiling 0 <= ceiling x")
  1167   apply simp
  1168   apply (rule ceiling_mono2)
  1169   apply force
  1170   apply force
  1171   apply (rule ceiling_eq2)
  1172   apply (simp, simp)
  1173   apply (subst nat_add_distrib)
  1174   apply auto
  1175 done
  1176 
  1177 lemma natceiling_add [simp]: "0 <= x ==>
  1178     natceiling (x + real a) = natceiling x + a"
  1179   apply (unfold natceiling_def)
  1180   apply (subgoal_tac "real a = real (int a)")
  1181   apply (erule ssubst)
  1182   apply simp
  1183   apply (subst nat_add_distrib)
  1184   apply (subgoal_tac "0 = ceiling 0")
  1185   apply (erule ssubst)
  1186   apply (erule ceiling_mono2)
  1187   apply simp_all
  1188 done
  1189 
  1190 lemma natceiling_add_number_of [simp]:
  1191     "~ neg ((number_of n)::int) ==> 0 <= x ==>
  1192       natceiling (x + number_of n) = natceiling x + number_of n"
  1193   apply (subst natceiling_add [THEN sym])
  1194   apply simp_all
  1195 done
  1196 
  1197 lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
  1198   apply (subst natceiling_add [THEN sym])
  1199   apply assumption
  1200   apply simp
  1201 done
  1202 
  1203 lemma natceiling_subtract [simp]: "real a <= x ==>
  1204     natceiling(x - real a) = natceiling x - a"
  1205   apply (unfold natceiling_def)
  1206   apply (subgoal_tac "real a = real (int a)")
  1207   apply (erule ssubst)
  1208   apply simp
  1209   apply simp
  1210 done
  1211 
  1212 lemma natfloor_div_nat: "1 <= x ==> 0 < y ==>
  1213   natfloor (x / real y) = natfloor x div y"
  1214 proof -
  1215   assume "1 <= (x::real)" and "0 < (y::nat)"
  1216   have "natfloor x = (natfloor x) div y * y + (natfloor x) mod y"
  1217     by simp
  1218   then have a: "real(natfloor x) = real ((natfloor x) div y) * real y +
  1219     real((natfloor x) mod y)"
  1220     by (simp only: real_of_nat_add [THEN sym] real_of_nat_mult [THEN sym])
  1221   have "x = real(natfloor x) + (x - real(natfloor x))"
  1222     by simp
  1223   then have "x = real ((natfloor x) div y) * real y +
  1224       real((natfloor x) mod y) + (x - real(natfloor x))"
  1225     by (simp add: a)
  1226   then have "x / real y = ... / real y"
  1227     by simp
  1228   also have "... = real((natfloor x) div y) + real((natfloor x) mod y) /
  1229     real y + (x - real(natfloor x)) / real y"
  1230     by (auto simp add: ring_distrib ring_eq_simps add_divide_distrib
  1231       diff_divide_distrib prems)
  1232   finally have "natfloor (x / real y) = natfloor(...)" by simp
  1233   also have "... = natfloor(real((natfloor x) mod y) /
  1234     real y + (x - real(natfloor x)) / real y + real((natfloor x) div y))"
  1235     by (simp add: add_ac)
  1236   also have "... = natfloor(real((natfloor x) mod y) /
  1237     real y + (x - real(natfloor x)) / real y) + (natfloor x) div y"
  1238     apply (rule natfloor_add)
  1239     apply (rule add_nonneg_nonneg)
  1240     apply (rule divide_nonneg_pos)
  1241     apply simp
  1242     apply (simp add: prems)
  1243     apply (rule divide_nonneg_pos)
  1244     apply (simp add: compare_rls)
  1245     apply (rule real_natfloor_le)
  1246     apply (insert prems, auto)
  1247     done
  1248   also have "natfloor(real((natfloor x) mod y) /
  1249     real y + (x - real(natfloor x)) / real y) = 0"
  1250     apply (rule natfloor_eq)
  1251     apply simp
  1252     apply (rule add_nonneg_nonneg)
  1253     apply (rule divide_nonneg_pos)
  1254     apply force
  1255     apply (force simp add: prems)
  1256     apply (rule divide_nonneg_pos)
  1257     apply (simp add: compare_rls)
  1258     apply (rule real_natfloor_le)
  1259     apply (auto simp add: prems)
  1260     apply (insert prems, arith)
  1261     apply (simp add: add_divide_distrib [THEN sym])
  1262     apply (subgoal_tac "real y = real y - 1 + 1")
  1263     apply (erule ssubst)
  1264     apply (rule add_le_less_mono)
  1265     apply (simp add: compare_rls)
  1266     apply (subgoal_tac "real(natfloor x mod y) + 1 =
  1267       real(natfloor x mod y + 1)")
  1268     apply (erule ssubst)
  1269     apply (subst real_of_nat_le_iff)
  1270     apply (subgoal_tac "natfloor x mod y < y")
  1271     apply arith
  1272     apply (rule mod_less_divisor)
  1273     apply assumption
  1274     apply auto
  1275     apply (simp add: compare_rls)
  1276     apply (subst add_commute)
  1277     apply (rule real_natfloor_add_one_gt)
  1278     done
  1279   finally show ?thesis
  1280     by simp
  1281 qed
  1282 
  1283 end