library theories for debugging and parallel computing using code generation towards Isabelle/ML
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 +
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 *}
144 class floor_ceiling = archimedean_field +
145 fixes floor :: "'a \<Rightarrow> int"
146 assumes floor_correct: "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
149 floor ("\<lfloor>_\<rfloor>")
151 notation (HTML output)
152 floor ("\<lfloor>_\<rfloor>")
154 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> floor x = z"
155 using floor_correct [of x] floor_exists1 [of x] by auto
157 lemma of_int_floor_le: "of_int (floor x) \<le> x"
158 using floor_correct ..
160 lemma le_floor_iff: "z \<le> floor x \<longleftrightarrow> of_int z \<le> x"
162 assume "z \<le> floor x"
163 then have "(of_int z :: 'a) \<le> of_int (floor x)" by simp
164 also have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
165 finally show "of_int z \<le> x" .
167 assume "of_int z \<le> x"
168 also have "x < of_int (floor x + 1)" using floor_correct ..
169 finally show "z \<le> floor x" by (simp del: of_int_add)
172 lemma floor_less_iff: "floor x < z \<longleftrightarrow> x < of_int z"
173 by (simp add: not_le [symmetric] le_floor_iff)
175 lemma less_floor_iff: "z < floor x \<longleftrightarrow> of_int z + 1 \<le> x"
176 using le_floor_iff [of "z + 1" x] by auto
178 lemma floor_le_iff: "floor x \<le> z \<longleftrightarrow> x < of_int z + 1"
179 by (simp add: not_less [symmetric] less_floor_iff)
181 lemma floor_mono: assumes "x \<le> y" shows "floor x \<le> floor y"
183 have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
184 also note `x \<le> y`
185 finally show ?thesis by (simp add: le_floor_iff)
188 lemma floor_less_cancel: "floor x < floor y \<Longrightarrow> x < y"
189 by (auto simp add: not_le [symmetric] floor_mono)
191 lemma floor_of_int [simp]: "floor (of_int z) = z"
192 by (rule floor_unique) simp_all
194 lemma floor_of_nat [simp]: "floor (of_nat n) = int n"
195 using floor_of_int [of "of_nat n"] by simp
197 lemma le_floor_add: "floor x + floor y \<le> floor (x + y)"
198 by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
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_numeral [simp]: "floor (numeral v) = numeral v"
209 using floor_of_int [of "numeral v"] by simp
211 lemma floor_neg_numeral [simp]: "floor (neg_numeral v) = neg_numeral v"
212 using floor_of_int [of "neg_numeral v"] by simp
214 lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
215 by (simp add: le_floor_iff)
217 lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
218 by (simp add: le_floor_iff)
220 lemma numeral_le_floor [simp]:
221 "numeral v \<le> floor x \<longleftrightarrow> numeral v \<le> x"
222 by (simp add: le_floor_iff)
224 lemma neg_numeral_le_floor [simp]:
225 "neg_numeral v \<le> floor x \<longleftrightarrow> neg_numeral v \<le> x"
226 by (simp add: le_floor_iff)
228 lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
229 by (simp add: less_floor_iff)
231 lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
232 by (simp add: less_floor_iff)
234 lemma numeral_less_floor [simp]:
235 "numeral v < floor x \<longleftrightarrow> numeral v + 1 \<le> x"
236 by (simp add: less_floor_iff)
238 lemma neg_numeral_less_floor [simp]:
239 "neg_numeral v < floor x \<longleftrightarrow> neg_numeral v + 1 \<le> x"
240 by (simp add: less_floor_iff)
242 lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
243 by (simp add: floor_le_iff)
245 lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
246 by (simp add: floor_le_iff)
248 lemma floor_le_numeral [simp]:
249 "floor x \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
250 by (simp add: floor_le_iff)
252 lemma floor_le_neg_numeral [simp]:
253 "floor x \<le> neg_numeral v \<longleftrightarrow> x < neg_numeral v + 1"
254 by (simp add: floor_le_iff)
256 lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
257 by (simp add: floor_less_iff)
259 lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
260 by (simp add: floor_less_iff)
262 lemma floor_less_numeral [simp]:
263 "floor x < numeral v \<longleftrightarrow> x < numeral v"
264 by (simp add: floor_less_iff)
266 lemma floor_less_neg_numeral [simp]:
267 "floor x < neg_numeral v \<longleftrightarrow> x < neg_numeral v"
268 by (simp add: floor_less_iff)
270 text {* Addition and subtraction of integers *}
272 lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
273 using floor_correct [of x] by (simp add: floor_unique)
275 lemma floor_add_numeral [simp]:
276 "floor (x + numeral v) = floor x + numeral v"
277 using floor_add_of_int [of x "numeral v"] by simp
279 lemma floor_add_neg_numeral [simp]:
280 "floor (x + neg_numeral v) = floor x + neg_numeral v"
281 using floor_add_of_int [of x "neg_numeral v"] by simp
283 lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
284 using floor_add_of_int [of x 1] by simp
286 lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
287 using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
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
293 lemma floor_diff_neg_numeral [simp]:
294 "floor (x - neg_numeral v) = floor x - neg_numeral v"
295 using floor_diff_of_int [of x "neg_numeral v"] by simp
297 lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
298 using floor_diff_of_int [of x 1] by simp
301 subsection {* Ceiling function *}
304 ceiling :: "'a::floor_ceiling \<Rightarrow> int" where
305 "ceiling x = - floor (- x)"
308 ceiling ("\<lceil>_\<rceil>")
310 notation (HTML output)
311 ceiling ("\<lceil>_\<rceil>")
313 lemma ceiling_correct: "of_int (ceiling x) - 1 < x \<and> x \<le> of_int (ceiling x)"
314 unfolding ceiling_def using floor_correct [of "- x"] by simp
316 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> ceiling x = z"
317 unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
319 lemma le_of_int_ceiling: "x \<le> of_int (ceiling x)"
320 using ceiling_correct ..
322 lemma ceiling_le_iff: "ceiling x \<le> z \<longleftrightarrow> x \<le> of_int z"
323 unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
325 lemma less_ceiling_iff: "z < ceiling x \<longleftrightarrow> of_int z < x"
326 by (simp add: not_le [symmetric] ceiling_le_iff)
328 lemma ceiling_less_iff: "ceiling x < z \<longleftrightarrow> x \<le> of_int z - 1"
329 using ceiling_le_iff [of x "z - 1"] by simp
331 lemma le_ceiling_iff: "z \<le> ceiling x \<longleftrightarrow> of_int z - 1 < x"
332 by (simp add: not_less [symmetric] ceiling_less_iff)
334 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> ceiling x \<ge> ceiling y"
335 unfolding ceiling_def by (simp add: floor_mono)
337 lemma ceiling_less_cancel: "ceiling x < ceiling y \<Longrightarrow> x < y"
338 by (auto simp add: not_le [symmetric] ceiling_mono)
340 lemma ceiling_of_int [simp]: "ceiling (of_int z) = z"
341 by (rule ceiling_unique) simp_all
343 lemma ceiling_of_nat [simp]: "ceiling (of_nat n) = int n"
344 using ceiling_of_int [of "of_nat n"] by simp
346 lemma ceiling_add_le: "ceiling (x + y) \<le> ceiling x + ceiling y"
347 by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
349 text {* Ceiling with numerals *}
351 lemma ceiling_zero [simp]: "ceiling 0 = 0"
352 using ceiling_of_int [of 0] by simp
354 lemma ceiling_one [simp]: "ceiling 1 = 1"
355 using ceiling_of_int [of 1] by simp
357 lemma ceiling_numeral [simp]: "ceiling (numeral v) = numeral v"
358 using ceiling_of_int [of "numeral v"] by simp
360 lemma ceiling_neg_numeral [simp]: "ceiling (neg_numeral v) = neg_numeral v"
361 using ceiling_of_int [of "neg_numeral v"] by simp
363 lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
364 by (simp add: ceiling_le_iff)
366 lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
367 by (simp add: ceiling_le_iff)
369 lemma ceiling_le_numeral [simp]:
370 "ceiling x \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
371 by (simp add: ceiling_le_iff)
373 lemma ceiling_le_neg_numeral [simp]:
374 "ceiling x \<le> neg_numeral v \<longleftrightarrow> x \<le> neg_numeral v"
375 by (simp add: ceiling_le_iff)
377 lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
378 by (simp add: ceiling_less_iff)
380 lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
381 by (simp add: ceiling_less_iff)
383 lemma ceiling_less_numeral [simp]:
384 "ceiling x < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
385 by (simp add: ceiling_less_iff)
387 lemma ceiling_less_neg_numeral [simp]:
388 "ceiling x < neg_numeral v \<longleftrightarrow> x \<le> neg_numeral v - 1"
389 by (simp add: ceiling_less_iff)
391 lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
392 by (simp add: le_ceiling_iff)
394 lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
395 by (simp add: le_ceiling_iff)
397 lemma numeral_le_ceiling [simp]:
398 "numeral v \<le> ceiling x \<longleftrightarrow> numeral v - 1 < x"
399 by (simp add: le_ceiling_iff)
401 lemma neg_numeral_le_ceiling [simp]:
402 "neg_numeral v \<le> ceiling x \<longleftrightarrow> neg_numeral v - 1 < x"
403 by (simp add: le_ceiling_iff)
405 lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
406 by (simp add: less_ceiling_iff)
408 lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
409 by (simp add: less_ceiling_iff)
411 lemma numeral_less_ceiling [simp]:
412 "numeral v < ceiling x \<longleftrightarrow> numeral v < x"
413 by (simp add: less_ceiling_iff)
415 lemma neg_numeral_less_ceiling [simp]:
416 "neg_numeral v < ceiling x \<longleftrightarrow> neg_numeral v < x"
417 by (simp add: less_ceiling_iff)
419 text {* Addition and subtraction of integers *}
421 lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
422 using ceiling_correct [of x] by (simp add: ceiling_unique)
424 lemma ceiling_add_numeral [simp]:
425 "ceiling (x + numeral v) = ceiling x + numeral v"
426 using ceiling_add_of_int [of x "numeral v"] by simp
428 lemma ceiling_add_neg_numeral [simp]:
429 "ceiling (x + neg_numeral v) = ceiling x + neg_numeral v"
430 using ceiling_add_of_int [of x "neg_numeral v"] by simp
432 lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
433 using ceiling_add_of_int [of x 1] by simp
435 lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
436 using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
438 lemma ceiling_diff_numeral [simp]:
439 "ceiling (x - numeral v) = ceiling x - numeral v"
440 using ceiling_diff_of_int [of x "numeral v"] by simp
442 lemma ceiling_diff_neg_numeral [simp]:
443 "ceiling (x - neg_numeral v) = ceiling x - neg_numeral v"
444 using ceiling_diff_of_int [of x "neg_numeral v"] by simp
446 lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
447 using ceiling_diff_of_int [of x 1] by simp
449 lemma ceiling_diff_floor_le_1: "ceiling x - floor x \<le> 1"
451 have "of_int \<lceil>x\<rceil> - 1 < x"
452 using ceiling_correct[of x] by simp
453 also have "x < of_int \<lfloor>x\<rfloor> + 1"
454 using floor_correct[of x] by simp_all
455 finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
458 unfolding of_int_less_iff by simp
461 subsection {* Negation *}
463 lemma floor_minus: "floor (- x) = - ceiling x"
464 unfolding ceiling_def by simp
466 lemma ceiling_minus: "ceiling (- x) = - floor x"
467 unfolding ceiling_def by simp