floor and ceiling definitions are not code equations -- this enables trivial evaluation of floor and ceiling
1 (* Title: HOL/Archimedean_Field.thy
5 header {* Archimedean Fields, Floor and Ceiling Functions *}
7 theory Archimedean_Field
11 subsection {* Class of Archimedean fields *}
13 text {* Archimedean fields have no infinite elements. *}
15 class archimedean_field = linordered_field + number_ring +
16 assumes ex_le_of_int: "\<exists>z. x \<le> of_int z"
19 fixes x :: "'a::archimedean_field" shows "\<exists>z. x < of_int z"
21 from ex_le_of_int obtain z where "x \<le> of_int z" ..
22 then have "x < of_int (z + 1)" by simp
27 fixes x :: "'a::archimedean_field" shows "\<exists>z. of_int z < x"
29 from ex_less_of_int obtain z where "- x < of_int z" ..
30 then have "of_int (- z) < x" by simp
35 fixes x :: "'a::archimedean_field" shows "\<exists>n. x < of_nat n"
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 ..
44 fixes x :: "'a::archimedean_field" shows "\<exists>n. x \<le> of_nat n"
46 obtain n where "x < of_nat n" using ex_less_of_nat ..
47 then have "x \<le> of_nat n" by simp
51 text {* Archimedean fields have no infinitesimal elements. *}
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"
57 from `0 < x` 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 `0 < inverse x` by (cases n) (simp_all del: of_nat_Suc)
63 then have "inverse (of_nat (Suc m)) < inverse (inverse x)"
64 using `0 < inverse x` by (rule less_imp_inverse_less)
65 then have "inverse (of_nat (Suc m)) < x"
66 using `0 < x` by (simp add: nonzero_inverse_inverse_eq)
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 `0 < x`] by auto
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"
79 obtain n where "y / x < of_nat n" using ex_less_of_nat ..
80 with `0 < x` have "y < of_nat n * x" by (simp add: pos_divide_less_eq)
85 subsection {* Existence and uniqueness of floor function *}
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)"
91 from `\<exists>n. P n` have "P (Least P)" by (rule LeastI_ex)
92 with `\<not> P 0` 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 `P (Least P)` `Least P = Suc n` by simp
102 fixes x :: "'a::archimedean_field"
103 shows "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
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
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
123 fixes x :: "'a::archimedean_field"
124 shows "\<exists>!z. of_int z \<le> x \<and> x < of_int (z + 1)"
126 show "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
127 by (rule floor_exists)
130 "of_int y \<le> x \<and> x < of_int (y + 1)"
131 "of_int z \<le> x \<and> x < of_int (z + 1)"
133 "of_int y \<le> x" "x < of_int (y + 1)"
134 "of_int z \<le> x" "x < of_int (z + 1)"
136 from le_less_trans [OF `of_int y \<le> x` `x < of_int (z + 1)`]
137 le_less_trans [OF `of_int z \<le> x` `x < of_int (y + 1)`]
138 show "y = z" by (simp del: of_int_add)
142 subsection {* Floor function *}
145 floor :: "'a::archimedean_field \<Rightarrow> int" where
146 [code del]: "floor x = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))"
149 floor ("\<lfloor>_\<rfloor>")
151 notation (HTML output)
152 floor ("\<lfloor>_\<rfloor>")
154 lemma floor_correct: "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
155 unfolding floor_def using floor_exists1 by (rule theI')
157 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> floor x = z"
158 using floor_correct [of x] floor_exists1 [of x] by auto
160 lemma of_int_floor_le: "of_int (floor x) \<le> x"
161 using floor_correct ..
163 lemma le_floor_iff: "z \<le> floor x \<longleftrightarrow> of_int z \<le> x"
165 assume "z \<le> floor x"
166 then have "(of_int z :: 'a) \<le> of_int (floor x)" by simp
167 also have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
168 finally show "of_int z \<le> x" .
170 assume "of_int z \<le> x"
171 also have "x < of_int (floor x + 1)" using floor_correct ..
172 finally show "z \<le> floor x" by (simp del: of_int_add)
175 lemma floor_less_iff: "floor x < z \<longleftrightarrow> x < of_int z"
176 by (simp add: not_le [symmetric] le_floor_iff)
178 lemma less_floor_iff: "z < floor x \<longleftrightarrow> of_int z + 1 \<le> x"
179 using le_floor_iff [of "z + 1" x] by auto
181 lemma floor_le_iff: "floor x \<le> z \<longleftrightarrow> x < of_int z + 1"
182 by (simp add: not_less [symmetric] less_floor_iff)
184 lemma floor_mono: assumes "x \<le> y" shows "floor x \<le> floor y"
186 have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
187 also note `x \<le> y`
188 finally show ?thesis by (simp add: le_floor_iff)
191 lemma floor_less_cancel: "floor x < floor y \<Longrightarrow> x < y"
192 by (auto simp add: not_le [symmetric] floor_mono)
194 lemma floor_of_int [simp]: "floor (of_int z) = z"
195 by (rule floor_unique) simp_all
197 lemma floor_of_nat [simp]: "floor (of_nat n) = int n"
198 using floor_of_int [of "of_nat n"] by simp
200 text {* Floor with numerals *}
202 lemma floor_zero [simp]: "floor 0 = 0"
203 using floor_of_int [of 0] by simp
205 lemma floor_one [simp]: "floor 1 = 1"
206 using floor_of_int [of 1] by simp
208 lemma floor_number_of [simp]: "floor (number_of v) = number_of v"
209 using floor_of_int [of "number_of v"] by simp
211 lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
212 by (simp add: le_floor_iff)
214 lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
215 by (simp add: le_floor_iff)
217 lemma number_of_le_floor [simp]: "number_of v \<le> floor x \<longleftrightarrow> number_of v \<le> x"
218 by (simp add: le_floor_iff)
220 lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
221 by (simp add: less_floor_iff)
223 lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
224 by (simp add: less_floor_iff)
226 lemma number_of_less_floor [simp]:
227 "number_of v < floor x \<longleftrightarrow> number_of v + 1 \<le> x"
228 by (simp add: less_floor_iff)
230 lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
231 by (simp add: floor_le_iff)
233 lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
234 by (simp add: floor_le_iff)
236 lemma floor_le_number_of [simp]:
237 "floor x \<le> number_of v \<longleftrightarrow> x < number_of v + 1"
238 by (simp add: floor_le_iff)
240 lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
241 by (simp add: floor_less_iff)
243 lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
244 by (simp add: floor_less_iff)
246 lemma floor_less_number_of [simp]:
247 "floor x < number_of v \<longleftrightarrow> x < number_of v"
248 by (simp add: floor_less_iff)
250 text {* Addition and subtraction of integers *}
252 lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
253 using floor_correct [of x] by (simp add: floor_unique)
255 lemma floor_add_number_of [simp]:
256 "floor (x + number_of v) = floor x + number_of v"
257 using floor_add_of_int [of x "number_of v"] by simp
259 lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
260 using floor_add_of_int [of x 1] by simp
262 lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
263 using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
265 lemma floor_diff_number_of [simp]:
266 "floor (x - number_of v) = floor x - number_of v"
267 using floor_diff_of_int [of x "number_of v"] by simp
269 lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
270 using floor_diff_of_int [of x 1] by simp
273 subsection {* Ceiling function *}
276 ceiling :: "'a::archimedean_field \<Rightarrow> int" where
277 [code del]: "ceiling x = - floor (- x)"
280 ceiling ("\<lceil>_\<rceil>")
282 notation (HTML output)
283 ceiling ("\<lceil>_\<rceil>")
285 lemma ceiling_correct: "of_int (ceiling x) - 1 < x \<and> x \<le> of_int (ceiling x)"
286 unfolding ceiling_def using floor_correct [of "- x"] by simp
288 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> ceiling x = z"
289 unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
291 lemma le_of_int_ceiling: "x \<le> of_int (ceiling x)"
292 using ceiling_correct ..
294 lemma ceiling_le_iff: "ceiling x \<le> z \<longleftrightarrow> x \<le> of_int z"
295 unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
297 lemma less_ceiling_iff: "z < ceiling x \<longleftrightarrow> of_int z < x"
298 by (simp add: not_le [symmetric] ceiling_le_iff)
300 lemma ceiling_less_iff: "ceiling x < z \<longleftrightarrow> x \<le> of_int z - 1"
301 using ceiling_le_iff [of x "z - 1"] by simp
303 lemma le_ceiling_iff: "z \<le> ceiling x \<longleftrightarrow> of_int z - 1 < x"
304 by (simp add: not_less [symmetric] ceiling_less_iff)
306 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> ceiling x \<ge> ceiling y"
307 unfolding ceiling_def by (simp add: floor_mono)
309 lemma ceiling_less_cancel: "ceiling x < ceiling y \<Longrightarrow> x < y"
310 by (auto simp add: not_le [symmetric] ceiling_mono)
312 lemma ceiling_of_int [simp]: "ceiling (of_int z) = z"
313 by (rule ceiling_unique) simp_all
315 lemma ceiling_of_nat [simp]: "ceiling (of_nat n) = int n"
316 using ceiling_of_int [of "of_nat n"] by simp
318 text {* Ceiling with numerals *}
320 lemma ceiling_zero [simp]: "ceiling 0 = 0"
321 using ceiling_of_int [of 0] by simp
323 lemma ceiling_one [simp]: "ceiling 1 = 1"
324 using ceiling_of_int [of 1] by simp
326 lemma ceiling_number_of [simp]: "ceiling (number_of v) = number_of v"
327 using ceiling_of_int [of "number_of v"] by simp
329 lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
330 by (simp add: ceiling_le_iff)
332 lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
333 by (simp add: ceiling_le_iff)
335 lemma ceiling_le_number_of [simp]:
336 "ceiling x \<le> number_of v \<longleftrightarrow> x \<le> number_of v"
337 by (simp add: ceiling_le_iff)
339 lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
340 by (simp add: ceiling_less_iff)
342 lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
343 by (simp add: ceiling_less_iff)
345 lemma ceiling_less_number_of [simp]:
346 "ceiling x < number_of v \<longleftrightarrow> x \<le> number_of v - 1"
347 by (simp add: ceiling_less_iff)
349 lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
350 by (simp add: le_ceiling_iff)
352 lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
353 by (simp add: le_ceiling_iff)
355 lemma number_of_le_ceiling [simp]:
356 "number_of v \<le> ceiling x\<longleftrightarrow> number_of v - 1 < x"
357 by (simp add: le_ceiling_iff)
359 lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
360 by (simp add: less_ceiling_iff)
362 lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
363 by (simp add: less_ceiling_iff)
365 lemma number_of_less_ceiling [simp]:
366 "number_of v < ceiling x \<longleftrightarrow> number_of v < x"
367 by (simp add: less_ceiling_iff)
369 text {* Addition and subtraction of integers *}
371 lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
372 using ceiling_correct [of x] by (simp add: ceiling_unique)
374 lemma ceiling_add_number_of [simp]:
375 "ceiling (x + number_of v) = ceiling x + number_of v"
376 using ceiling_add_of_int [of x "number_of v"] by simp
378 lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
379 using ceiling_add_of_int [of x 1] by simp
381 lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
382 using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
384 lemma ceiling_diff_number_of [simp]:
385 "ceiling (x - number_of v) = ceiling x - number_of v"
386 using ceiling_diff_of_int [of x "number_of v"] by simp
388 lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
389 using ceiling_diff_of_int [of x 1] by simp
392 subsection {* Negation *}
394 lemma floor_minus: "floor (- x) = - ceiling x"
395 unfolding ceiling_def by simp
397 lemma ceiling_minus: "ceiling (- x) = - floor x"
398 unfolding ceiling_def by simp