src/HOL/Archimedean_Field.thy
author eberlm
Mon Nov 02 11:56:28 2015 +0100 (2015-11-02)
changeset 61531 ab2e862263e7
parent 61378 3e04c9ca001a
child 61649 268d88ec9087
permissions -rw-r--r--
Rounding function, uniform limits, cotangent, binomial identities
     1 (*  Title:      HOL/Archimedean_Field.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 section \<open>Archimedean Fields, Floor and Ceiling Functions\<close>
     6 
     7 theory Archimedean_Field
     8 imports Main
     9 begin
    10 
    11 subsection \<open>Class of Archimedean fields\<close>
    12 
    13 text \<open>Archimedean fields have no infinite elements.\<close>
    14 
    15 class archimedean_field = linordered_field +
    16   assumes ex_le_of_int: "\<exists>z. x \<le> of_int z"
    17 
    18 lemma ex_less_of_int:
    19   fixes x :: "'a::archimedean_field" shows "\<exists>z. x < of_int z"
    20 proof -
    21   from ex_le_of_int obtain z where "x \<le> of_int z" ..
    22   then have "x < of_int (z + 1)" by simp
    23   then show ?thesis ..
    24 qed
    25 
    26 lemma ex_of_int_less:
    27   fixes x :: "'a::archimedean_field" shows "\<exists>z. of_int z < x"
    28 proof -
    29   from ex_less_of_int obtain z where "- x < of_int z" ..
    30   then have "of_int (- z) < x" by simp
    31   then show ?thesis ..
    32 qed
    33 
    34 lemma ex_less_of_nat:
    35   fixes x :: "'a::archimedean_field" shows "\<exists>n. x < of_nat n"
    36 proof -
    37   obtain z where "x < of_int z" using ex_less_of_int ..
    38   also have "\<dots> \<le> of_int (int (nat z))" by simp
    39   also have "\<dots> = of_nat (nat z)" by (simp only: of_int_of_nat_eq)
    40   finally show ?thesis ..
    41 qed
    42 
    43 lemma ex_le_of_nat:
    44   fixes x :: "'a::archimedean_field" shows "\<exists>n. x \<le> of_nat n"
    45 proof -
    46   obtain n where "x < of_nat n" using ex_less_of_nat ..
    47   then have "x \<le> of_nat n" by simp
    48   then show ?thesis ..
    49 qed
    50 
    51 text \<open>Archimedean fields have no infinitesimal elements.\<close>
    52 
    53 lemma ex_inverse_of_nat_Suc_less:
    54   fixes x :: "'a::archimedean_field"
    55   assumes "0 < x" shows "\<exists>n. inverse (of_nat (Suc n)) < x"
    56 proof -
    57   from \<open>0 < x\<close> have "0 < inverse x"
    58     by (rule positive_imp_inverse_positive)
    59   obtain n where "inverse x < of_nat n"
    60     using ex_less_of_nat ..
    61   then obtain m where "inverse x < of_nat (Suc m)"
    62     using \<open>0 < inverse x\<close> by (cases n) (simp_all del: of_nat_Suc)
    63   then have "inverse (of_nat (Suc m)) < inverse (inverse x)"
    64     using \<open>0 < inverse x\<close> by (rule less_imp_inverse_less)
    65   then have "inverse (of_nat (Suc m)) < x"
    66     using \<open>0 < x\<close> by (simp add: nonzero_inverse_inverse_eq)
    67   then show ?thesis ..
    68 qed
    69 
    70 lemma ex_inverse_of_nat_less:
    71   fixes x :: "'a::archimedean_field"
    72   assumes "0 < x" shows "\<exists>n>0. inverse (of_nat n) < x"
    73   using ex_inverse_of_nat_Suc_less [OF \<open>0 < x\<close>] by auto
    74 
    75 lemma ex_less_of_nat_mult:
    76   fixes x :: "'a::archimedean_field"
    77   assumes "0 < x" shows "\<exists>n. y < of_nat n * x"
    78 proof -
    79   obtain n where "y / x < of_nat n" using ex_less_of_nat ..
    80   with \<open>0 < x\<close> have "y < of_nat n * x" by (simp add: pos_divide_less_eq)
    81   then show ?thesis ..
    82 qed
    83 
    84 
    85 subsection \<open>Existence and uniqueness of floor function\<close>
    86 
    87 lemma exists_least_lemma:
    88   assumes "\<not> P 0" and "\<exists>n. P n"
    89   shows "\<exists>n. \<not> P n \<and> P (Suc n)"
    90 proof -
    91   from \<open>\<exists>n. P n\<close> have "P (Least P)" by (rule LeastI_ex)
    92   with \<open>\<not> P 0\<close> obtain n where "Least P = Suc n"
    93     by (cases "Least P") auto
    94   then have "n < Least P" by simp
    95   then have "\<not> P n" by (rule not_less_Least)
    96   then have "\<not> P n \<and> P (Suc n)"
    97     using \<open>P (Least P)\<close> \<open>Least P = Suc n\<close> by simp
    98   then show ?thesis ..
    99 qed
   100 
   101 lemma floor_exists:
   102   fixes x :: "'a::archimedean_field"
   103   shows "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
   104 proof (cases)
   105   assume "0 \<le> x"
   106   then have "\<not> x < of_nat 0" by simp
   107   then have "\<exists>n. \<not> x < of_nat n \<and> x < of_nat (Suc n)"
   108     using ex_less_of_nat by (rule exists_least_lemma)
   109   then obtain n where "\<not> x < of_nat n \<and> x < of_nat (Suc n)" ..
   110   then have "of_int (int n) \<le> x \<and> x < of_int (int n + 1)" by simp
   111   then show ?thesis ..
   112 next
   113   assume "\<not> 0 \<le> x"
   114   then have "\<not> - x \<le> of_nat 0" by simp
   115   then have "\<exists>n. \<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)"
   116     using ex_le_of_nat by (rule exists_least_lemma)
   117   then obtain n where "\<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)" ..
   118   then have "of_int (- int n - 1) \<le> x \<and> x < of_int (- int n - 1 + 1)" by simp
   119   then show ?thesis ..
   120 qed
   121 
   122 lemma floor_exists1:
   123   fixes x :: "'a::archimedean_field"
   124   shows "\<exists>!z. of_int z \<le> x \<and> x < of_int (z + 1)"
   125 proof (rule ex_ex1I)
   126   show "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
   127     by (rule floor_exists)
   128 next
   129   fix y z assume
   130     "of_int y \<le> x \<and> x < of_int (y + 1)"
   131     "of_int z \<le> x \<and> x < of_int (z + 1)"
   132   with le_less_trans [of "of_int y" "x" "of_int (z + 1)"]
   133        le_less_trans [of "of_int z" "x" "of_int (y + 1)"]
   134   show "y = z" by (simp del: of_int_add)
   135 qed
   136 
   137 
   138 subsection \<open>Floor function\<close>
   139 
   140 class floor_ceiling = archimedean_field +
   141   fixes floor :: "'a \<Rightarrow> int"
   142   assumes floor_correct: "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   143 
   144 notation (xsymbols)
   145   floor  ("\<lfloor>_\<rfloor>")
   146 
   147 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> floor x = z"
   148   using floor_correct [of x] floor_exists1 [of x] by auto
   149 
   150 lemma floor_unique_iff:
   151   fixes x :: "'a::floor_ceiling"
   152   shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
   153 using floor_correct floor_unique by auto
   154 
   155 lemma of_int_floor_le: "of_int (floor x) \<le> x"
   156   using floor_correct ..
   157 
   158 lemma le_floor_iff: "z \<le> floor x \<longleftrightarrow> of_int z \<le> x"
   159 proof
   160   assume "z \<le> floor x"
   161   then have "(of_int z :: 'a) \<le> of_int (floor x)" by simp
   162   also have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
   163   finally show "of_int z \<le> x" .
   164 next
   165   assume "of_int z \<le> x"
   166   also have "x < of_int (floor x + 1)" using floor_correct ..
   167   finally show "z \<le> floor x" by (simp del: of_int_add)
   168 qed
   169 
   170 lemma floor_less_iff: "floor x < z \<longleftrightarrow> x < of_int z"
   171   by (simp add: not_le [symmetric] le_floor_iff)
   172 
   173 lemma less_floor_iff: "z < floor x \<longleftrightarrow> of_int z + 1 \<le> x"
   174   using le_floor_iff [of "z + 1" x] by auto
   175 
   176 lemma floor_le_iff: "floor x \<le> z \<longleftrightarrow> x < of_int z + 1"
   177   by (simp add: not_less [symmetric] less_floor_iff)
   178 
   179 lemma floor_split[arith_split]: "P (floor t) \<longleftrightarrow> (\<forall>i. of_int i \<le> t \<and> t < of_int i + 1 \<longrightarrow> P i)"
   180   by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
   181 
   182 lemma floor_mono: assumes "x \<le> y" shows "floor x \<le> floor y"
   183 proof -
   184   have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
   185   also note \<open>x \<le> y\<close>
   186   finally show ?thesis by (simp add: le_floor_iff)
   187 qed
   188 
   189 lemma floor_less_cancel: "floor x < floor y \<Longrightarrow> x < y"
   190   by (auto simp add: not_le [symmetric] floor_mono)
   191 
   192 lemma floor_of_int [simp]: "floor (of_int z) = z"
   193   by (rule floor_unique) simp_all
   194 
   195 lemma floor_of_nat [simp]: "floor (of_nat n) = int n"
   196   using floor_of_int [of "of_nat n"] by simp
   197 
   198 lemma le_floor_add: "floor x + floor y \<le> floor (x + y)"
   199   by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
   200 
   201 text \<open>Floor with numerals\<close>
   202 
   203 lemma floor_zero [simp]: "floor 0 = 0"
   204   using floor_of_int [of 0] by simp
   205 
   206 lemma floor_one [simp]: "floor 1 = 1"
   207   using floor_of_int [of 1] by simp
   208 
   209 lemma floor_numeral [simp]: "floor (numeral v) = numeral v"
   210   using floor_of_int [of "numeral v"] by simp
   211 
   212 lemma floor_neg_numeral [simp]: "floor (- numeral v) = - numeral v"
   213   using floor_of_int [of "- numeral v"] by simp
   214 
   215 lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
   216   by (simp add: le_floor_iff)
   217 
   218 lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
   219   by (simp add: le_floor_iff)
   220 
   221 lemma numeral_le_floor [simp]:
   222   "numeral v \<le> floor x \<longleftrightarrow> numeral v \<le> x"
   223   by (simp add: le_floor_iff)
   224 
   225 lemma neg_numeral_le_floor [simp]:
   226   "- numeral v \<le> floor x \<longleftrightarrow> - numeral v \<le> x"
   227   by (simp add: le_floor_iff)
   228 
   229 lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
   230   by (simp add: less_floor_iff)
   231 
   232 lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
   233   by (simp add: less_floor_iff)
   234 
   235 lemma numeral_less_floor [simp]:
   236   "numeral v < floor x \<longleftrightarrow> numeral v + 1 \<le> x"
   237   by (simp add: less_floor_iff)
   238 
   239 lemma neg_numeral_less_floor [simp]:
   240   "- numeral v < floor x \<longleftrightarrow> - numeral v + 1 \<le> x"
   241   by (simp add: less_floor_iff)
   242 
   243 lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
   244   by (simp add: floor_le_iff)
   245 
   246 lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
   247   by (simp add: floor_le_iff)
   248 
   249 lemma floor_le_numeral [simp]:
   250   "floor x \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
   251   by (simp add: floor_le_iff)
   252 
   253 lemma floor_le_neg_numeral [simp]:
   254   "floor x \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
   255   by (simp add: floor_le_iff)
   256 
   257 lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
   258   by (simp add: floor_less_iff)
   259 
   260 lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
   261   by (simp add: floor_less_iff)
   262 
   263 lemma floor_less_numeral [simp]:
   264   "floor x < numeral v \<longleftrightarrow> x < numeral v"
   265   by (simp add: floor_less_iff)
   266 
   267 lemma floor_less_neg_numeral [simp]:
   268   "floor x < - numeral v \<longleftrightarrow> x < - numeral v"
   269   by (simp add: floor_less_iff)
   270 
   271 text \<open>Addition and subtraction of integers\<close>
   272 
   273 lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
   274   using floor_correct [of x] by (simp add: floor_unique)
   275 
   276 lemma floor_add_numeral [simp]:
   277     "floor (x + numeral v) = floor x + numeral v"
   278   using floor_add_of_int [of x "numeral v"] by simp
   279 
   280 lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
   281   using floor_add_of_int [of x 1] by simp
   282 
   283 lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
   284   using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
   285 
   286 lemma floor_uminus_of_int [simp]: "floor (- (of_int z)) = - z"
   287   by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
   288 
   289 lemma floor_diff_numeral [simp]:
   290   "floor (x - numeral v) = floor x - numeral v"
   291   using floor_diff_of_int [of x "numeral v"] by simp
   292 
   293 lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
   294   using floor_diff_of_int [of x 1] by simp
   295 
   296 lemma le_mult_floor:
   297   assumes "0 \<le> a" and "0 \<le> b"
   298   shows "floor a * floor b \<le> floor (a * b)"
   299 proof -
   300   have "of_int (floor a) \<le> a"
   301     and "of_int (floor b) \<le> b" by (auto intro: of_int_floor_le)
   302   hence "of_int (floor a * floor b) \<le> a * b"
   303     using assms by (auto intro!: mult_mono)
   304   also have "a * b < of_int (floor (a * b) + 1)"  
   305     using floor_correct[of "a * b"] by auto
   306   finally show ?thesis unfolding of_int_less_iff by simp
   307 qed
   308 
   309 lemma floor_divide_of_int_eq:
   310   fixes k l :: int
   311   shows "\<lfloor>of_int k / of_int l\<rfloor> = k div l"
   312 proof (cases "l = 0")
   313   case True then show ?thesis by simp
   314 next
   315   case False
   316   have *: "\<lfloor>of_int (k mod l) / of_int l :: 'a\<rfloor> = 0"
   317   proof (cases "l > 0")
   318     case True then show ?thesis
   319       by (auto intro: floor_unique)
   320   next
   321     case False
   322     obtain r where "r = - l" by blast
   323     then have l: "l = - r" by simp
   324     moreover with \<open>l \<noteq> 0\<close> False have "r > 0" by simp
   325     ultimately show ?thesis using pos_mod_bound [of r]
   326       by (auto simp add: zmod_zminus2_eq_if less_le field_simps intro: floor_unique)
   327   qed
   328   have "(of_int k :: 'a) = of_int (k div l * l + k mod l)"
   329     by simp
   330   also have "\<dots> = (of_int (k div l) + of_int (k mod l) / of_int l) * of_int l"
   331     using False by (simp only: of_int_add) (simp add: field_simps)
   332   finally have "(of_int k / of_int l :: 'a) = \<dots> / of_int l"
   333     by simp 
   334   then have "(of_int k / of_int l :: 'a) = of_int (k div l) + of_int (k mod l) / of_int l"
   335     using False by (simp only:) (simp add: field_simps)
   336   then have "\<lfloor>of_int k / of_int l :: 'a\<rfloor> = \<lfloor>of_int (k div l) + of_int (k mod l) / of_int l :: 'a\<rfloor>" 
   337     by simp
   338   then have "\<lfloor>of_int k / of_int l :: 'a\<rfloor> = \<lfloor>of_int (k mod l) / of_int l + of_int (k div l) :: 'a\<rfloor>"
   339     by (simp add: ac_simps)
   340   then have "\<lfloor>of_int k / of_int l :: 'a\<rfloor> = \<lfloor>of_int (k mod l) / of_int l :: 'a\<rfloor> + k div l"
   341     by simp
   342   with * show ?thesis by simp
   343 qed
   344 
   345 lemma floor_divide_of_nat_eq:
   346   fixes m n :: nat
   347   shows "\<lfloor>of_nat m / of_nat n\<rfloor> = of_nat (m div n)"
   348 proof (cases "n = 0")
   349   case True then show ?thesis by simp
   350 next
   351   case False
   352   then have *: "\<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> = 0"
   353     by (auto intro: floor_unique)
   354   have "(of_nat m :: 'a) = of_nat (m div n * n + m mod n)"
   355     by simp
   356   also have "\<dots> = (of_nat (m div n) + of_nat (m mod n) / of_nat n) * of_nat n"
   357     using False by (simp only: of_nat_add) (simp add: field_simps of_nat_mult)
   358   finally have "(of_nat m / of_nat n :: 'a) = \<dots> / of_nat n"
   359     by simp 
   360   then have "(of_nat m / of_nat n :: 'a) = of_nat (m div n) + of_nat (m mod n) / of_nat n"
   361     using False by (simp only:) simp
   362   then have "\<lfloor>of_nat m / of_nat n :: 'a\<rfloor> = \<lfloor>of_nat (m div n) + of_nat (m mod n) / of_nat n :: 'a\<rfloor>" 
   363     by simp
   364   then have "\<lfloor>of_nat m / of_nat n :: 'a\<rfloor> = \<lfloor>of_nat (m mod n) / of_nat n + of_nat (m div n) :: 'a\<rfloor>"
   365     by (simp add: ac_simps)
   366   moreover have "(of_nat (m div n) :: 'a) = of_int (of_nat (m div n))"
   367     by simp
   368   ultimately have "\<lfloor>of_nat m / of_nat n :: 'a\<rfloor> = \<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> + of_nat (m div n)"
   369     by (simp only: floor_add_of_int)
   370   with * show ?thesis by simp
   371 qed
   372 
   373 
   374 subsection \<open>Ceiling function\<close>
   375 
   376 definition
   377   ceiling :: "'a::floor_ceiling \<Rightarrow> int" where
   378   "ceiling x = - floor (- x)"
   379 
   380 notation (xsymbols)
   381   ceiling  ("\<lceil>_\<rceil>")
   382 
   383 lemma ceiling_correct: "of_int (ceiling x) - 1 < x \<and> x \<le> of_int (ceiling x)"
   384   unfolding ceiling_def using floor_correct [of "- x"] by simp
   385 
   386 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> ceiling x = z"
   387   unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
   388 
   389 lemma le_of_int_ceiling: "x \<le> of_int (ceiling x)"
   390   using ceiling_correct ..
   391 
   392 lemma ceiling_le_iff: "ceiling x \<le> z \<longleftrightarrow> x \<le> of_int z"
   393   unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
   394 
   395 lemma less_ceiling_iff: "z < ceiling x \<longleftrightarrow> of_int z < x"
   396   by (simp add: not_le [symmetric] ceiling_le_iff)
   397 
   398 lemma ceiling_less_iff: "ceiling x < z \<longleftrightarrow> x \<le> of_int z - 1"
   399   using ceiling_le_iff [of x "z - 1"] by simp
   400 
   401 lemma le_ceiling_iff: "z \<le> ceiling x \<longleftrightarrow> of_int z - 1 < x"
   402   by (simp add: not_less [symmetric] ceiling_less_iff)
   403 
   404 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> ceiling x \<ge> ceiling y"
   405   unfolding ceiling_def by (simp add: floor_mono)
   406 
   407 lemma ceiling_less_cancel: "ceiling x < ceiling y \<Longrightarrow> x < y"
   408   by (auto simp add: not_le [symmetric] ceiling_mono)
   409 
   410 lemma ceiling_of_int [simp]: "ceiling (of_int z) = z"
   411   by (rule ceiling_unique) simp_all
   412 
   413 lemma ceiling_of_nat [simp]: "ceiling (of_nat n) = int n"
   414   using ceiling_of_int [of "of_nat n"] by simp
   415 
   416 lemma ceiling_add_le: "ceiling (x + y) \<le> ceiling x + ceiling y"
   417   by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
   418 
   419 text \<open>Ceiling with numerals\<close>
   420 
   421 lemma ceiling_zero [simp]: "ceiling 0 = 0"
   422   using ceiling_of_int [of 0] by simp
   423 
   424 lemma ceiling_one [simp]: "ceiling 1 = 1"
   425   using ceiling_of_int [of 1] by simp
   426 
   427 lemma ceiling_numeral [simp]: "ceiling (numeral v) = numeral v"
   428   using ceiling_of_int [of "numeral v"] by simp
   429 
   430 lemma ceiling_neg_numeral [simp]: "ceiling (- numeral v) = - numeral v"
   431   using ceiling_of_int [of "- numeral v"] by simp
   432 
   433 lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
   434   by (simp add: ceiling_le_iff)
   435 
   436 lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
   437   by (simp add: ceiling_le_iff)
   438 
   439 lemma ceiling_le_numeral [simp]:
   440   "ceiling x \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
   441   by (simp add: ceiling_le_iff)
   442 
   443 lemma ceiling_le_neg_numeral [simp]:
   444   "ceiling x \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
   445   by (simp add: ceiling_le_iff)
   446 
   447 lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
   448   by (simp add: ceiling_less_iff)
   449 
   450 lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
   451   by (simp add: ceiling_less_iff)
   452 
   453 lemma ceiling_less_numeral [simp]:
   454   "ceiling x < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
   455   by (simp add: ceiling_less_iff)
   456 
   457 lemma ceiling_less_neg_numeral [simp]:
   458   "ceiling x < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
   459   by (simp add: ceiling_less_iff)
   460 
   461 lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
   462   by (simp add: le_ceiling_iff)
   463 
   464 lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
   465   by (simp add: le_ceiling_iff)
   466 
   467 lemma numeral_le_ceiling [simp]:
   468   "numeral v \<le> ceiling x \<longleftrightarrow> numeral v - 1 < x"
   469   by (simp add: le_ceiling_iff)
   470 
   471 lemma neg_numeral_le_ceiling [simp]:
   472   "- numeral v \<le> ceiling x \<longleftrightarrow> - numeral v - 1 < x"
   473   by (simp add: le_ceiling_iff)
   474 
   475 lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
   476   by (simp add: less_ceiling_iff)
   477 
   478 lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
   479   by (simp add: less_ceiling_iff)
   480 
   481 lemma numeral_less_ceiling [simp]:
   482   "numeral v < ceiling x \<longleftrightarrow> numeral v < x"
   483   by (simp add: less_ceiling_iff)
   484 
   485 lemma neg_numeral_less_ceiling [simp]:
   486   "- numeral v < ceiling x \<longleftrightarrow> - numeral v < x"
   487   by (simp add: less_ceiling_iff)
   488 
   489 lemma ceiling_altdef: "ceiling x = (if x = of_int (floor x) then floor x else floor x + 1)"
   490   by (intro ceiling_unique, (simp, linarith?)+)
   491 
   492 lemma floor_le_ceiling [simp]: "floor x \<le> ceiling x" by (simp add: ceiling_altdef)
   493 
   494 text \<open>Addition and subtraction of integers\<close>
   495 
   496 lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
   497   using ceiling_correct [of x] by (simp add: ceiling_unique)
   498 
   499 lemma ceiling_add_numeral [simp]:
   500     "ceiling (x + numeral v) = ceiling x + numeral v"
   501   using ceiling_add_of_int [of x "numeral v"] by simp
   502 
   503 lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
   504   using ceiling_add_of_int [of x 1] by simp
   505 
   506 lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
   507   using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
   508 
   509 lemma ceiling_diff_numeral [simp]:
   510   "ceiling (x - numeral v) = ceiling x - numeral v"
   511   using ceiling_diff_of_int [of x "numeral v"] by simp
   512 
   513 lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
   514   using ceiling_diff_of_int [of x 1] by simp
   515 
   516 lemma ceiling_split[arith_split]: "P (ceiling t) \<longleftrightarrow> (\<forall>i. of_int i - 1 < t \<and> t \<le> of_int i \<longrightarrow> P i)"
   517   by (auto simp add: ceiling_unique ceiling_correct)
   518 
   519 lemma ceiling_diff_floor_le_1: "ceiling x - floor x \<le> 1"
   520 proof -
   521   have "of_int \<lceil>x\<rceil> - 1 < x" 
   522     using ceiling_correct[of x] by simp
   523   also have "x < of_int \<lfloor>x\<rfloor> + 1"
   524     using floor_correct[of x] by simp_all
   525   finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
   526     by simp
   527   then show ?thesis
   528     unfolding of_int_less_iff by simp
   529 qed
   530 
   531 subsection \<open>Negation\<close>
   532 
   533 lemma floor_minus: "floor (- x) = - ceiling x"
   534   unfolding ceiling_def by simp
   535 
   536 lemma ceiling_minus: "ceiling (- x) = - floor x"
   537   unfolding ceiling_def by simp
   538 
   539 subsection \<open>Frac Function\<close>
   540 
   541 
   542 definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
   543   "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
   544 
   545 lemma frac_lt_1: "frac x < 1"
   546   by  (simp add: frac_def) linarith
   547 
   548 lemma frac_eq_0_iff [simp]: "frac x = 0 \<longleftrightarrow> x \<in> \<int>"
   549   by (simp add: frac_def) (metis Ints_cases Ints_of_int floor_of_int )
   550 
   551 lemma frac_ge_0 [simp]: "frac x \<ge> 0"
   552   unfolding frac_def
   553   by linarith
   554 
   555 lemma frac_gt_0_iff [simp]: "frac x > 0 \<longleftrightarrow> x \<notin> \<int>"
   556   by (metis frac_eq_0_iff frac_ge_0 le_less less_irrefl)
   557 
   558 lemma frac_of_int [simp]: "frac (of_int z) = 0"
   559   by (simp add: frac_def)
   560 
   561 lemma floor_add: "\<lfloor>x + y\<rfloor> = (if frac x + frac y < 1 then \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor> else (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>) + 1)"  
   562 proof -
   563   {assume "x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
   564    then have "\<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>"
   565      by (metis add.commute floor_unique le_floor_add le_floor_iff of_int_add)
   566    }
   567   moreover
   568   { assume "\<not> x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
   569     then have "\<lfloor>x + y\<rfloor> = 1 + (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>)"
   570       apply (simp add: floor_unique_iff)
   571       apply (auto simp add: algebra_simps)
   572       by linarith    
   573   }
   574   ultimately show ?thesis
   575     by (auto simp add: frac_def algebra_simps)
   576 qed
   577 
   578 lemma frac_add: "frac (x + y) = (if frac x + frac y < 1 then frac x + frac y
   579                                  else (frac x + frac y) - 1)"  
   580   by (simp add: frac_def floor_add)
   581 
   582 lemma frac_unique_iff:
   583   fixes x :: "'a::floor_ceiling"
   584   shows  "(frac x) = a \<longleftrightarrow> x - a \<in> \<int> \<and> 0 \<le> a \<and> a < 1"
   585   apply (auto simp: Ints_def frac_def)
   586   apply linarith
   587   apply linarith
   588   by (metis (no_types) add.commute add.left_neutral eq_diff_eq floor_add_of_int floor_unique of_int_0)
   589 
   590 lemma frac_eq: "(frac x) = x \<longleftrightarrow> 0 \<le> x \<and> x < 1"
   591   by (simp add: frac_unique_iff)
   592   
   593 lemma frac_neg:
   594   fixes x :: "'a::floor_ceiling"
   595   shows  "frac (-x) = (if x \<in> \<int> then 0 else 1 - frac x)"
   596   apply (auto simp add: frac_unique_iff)
   597   apply (simp add: frac_def)
   598   by (meson frac_lt_1 less_iff_diff_less_0 not_le not_less_iff_gr_or_eq)
   599 
   600 
   601 subsection \<open>Rounding to the nearest integer\<close>
   602 
   603 definition round where "round x = \<lfloor>x + 1/2\<rfloor>"
   604 
   605 lemma of_int_round_ge:     "of_int (round x) \<ge> x - 1/2"
   606   and of_int_round_le:     "of_int (round x) \<le> x + 1/2"
   607   and of_int_round_abs_le: "\<bar>of_int (round x) - x\<bar> \<le> 1/2"
   608   and of_int_round_gt:     "of_int (round x) > x - 1/2"
   609 proof -
   610   from floor_correct[of "x + 1/2"] have "x + 1/2 < of_int (round x) + 1" by (simp add: round_def)
   611   from add_strict_right_mono[OF this, of "-1"] show A: "of_int (round x) > x - 1/2" by simp
   612   thus "of_int (round x) \<ge> x - 1/2" by simp
   613   from floor_correct[of "x + 1/2"] show "of_int (round x) \<le> x + 1/2" by (simp add: round_def)
   614   with A show "\<bar>of_int (round x) - x\<bar> \<le> 1/2" by linarith
   615 qed
   616 
   617 lemma round_of_int [simp]: "round (of_int n) = n"
   618   unfolding round_def by (subst floor_unique_iff) force
   619 
   620 lemma round_0 [simp]: "round 0 = 0"
   621   using round_of_int[of 0] by simp
   622 
   623 lemma round_1 [simp]: "round 1 = 1"
   624   using round_of_int[of 1] by simp
   625 
   626 lemma round_numeral [simp]: "round (numeral n) = numeral n"
   627   using round_of_int[of "numeral n"] by simp
   628 
   629 lemma round_neg_numeral [simp]: "round (-numeral n) = -numeral n"
   630   using round_of_int[of "-numeral n"] by simp
   631 
   632 lemma round_of_nat [simp]: "round (of_nat n) = of_nat n"
   633   using round_of_int[of "int n"] by simp
   634 
   635 lemma round_mono: "x \<le> y \<Longrightarrow> round x \<le> round y"
   636   unfolding round_def by (intro floor_mono) simp
   637 
   638 lemma round_unique: "of_int y > x - 1/2 \<Longrightarrow> of_int y \<le> x + 1/2 \<Longrightarrow> round x = y"
   639 unfolding round_def
   640 proof (rule floor_unique)
   641   assume "x - 1 / 2 < of_int y"
   642   from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1" by simp
   643 qed
   644 
   645 lemma round_altdef: "round x = (if frac x \<ge> 1/2 then ceiling x else floor x)"
   646   by (cases "frac x \<ge> 1/2")
   647      (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef, linarith?)+)[2])+
   648 
   649 lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
   650   unfolding round_def by (intro floor_mono) simp
   651 
   652 lemma ceiling_ge_round: "\<lceil>x\<rceil> \<ge> round x" unfolding round_altdef by simp
   653      
   654 lemma round_diff_minimal: 
   655   fixes z :: "'a :: floor_ceiling"
   656   shows "abs (z - of_int (round z)) \<le> abs (z - of_int m)"
   657 proof (cases "of_int m \<ge> z")
   658   case True
   659   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int (ceiling z) - z\<bar>"
   660     unfolding round_altdef by (simp add: ceiling_altdef frac_def) linarith?
   661   also have "of_int \<lceil>z\<rceil> - z \<ge> 0" by linarith
   662   with True have "\<bar>of_int (ceiling z) - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   663     by (simp add: ceiling_le_iff)
   664   finally show ?thesis .
   665 next
   666   case False
   667   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int (floor z) - z\<bar>"
   668     unfolding round_altdef by (simp add: ceiling_altdef frac_def) linarith?
   669   also have "z - of_int \<lfloor>z\<rfloor> \<ge> 0" by linarith
   670   with False have "\<bar>of_int (floor z) - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   671     by (simp add: le_floor_iff)
   672   finally show ?thesis .
   673 qed
   674 
   675 end