src/HOL/Archimedean_Field.thy
 author nipkow Fri Aug 05 09:30:20 2016 +0200 (2016-08-05) changeset 63597 bef0277ec73b parent 63540 f8652d0534fa child 63599 f560147710fb permissions -rw-r--r--
tuned floor lemmas
1 (*  Title:      HOL/Archimedean_Field.thy
2     Author:     Brian Huffman
3 *)
5 section \<open>Archimedean Fields, Floor and Ceiling Functions\<close>
7 theory Archimedean_Field
8 imports Main
9 begin
11 lemma cInf_abs_ge:
12   fixes S :: "'a::{linordered_idom,conditionally_complete_linorder} set"
13   assumes "S \<noteq> {}"
14     and bdd: "\<And>x. x\<in>S \<Longrightarrow> \<bar>x\<bar> \<le> a"
15   shows "\<bar>Inf S\<bar> \<le> a"
16 proof -
17   have "Sup (uminus ` S) = - (Inf S)"
18   proof (rule antisym)
19     show "- (Inf S) \<le> Sup (uminus ` S)"
20       apply (subst minus_le_iff)
21       apply (rule cInf_greatest [OF \<open>S \<noteq> {}\<close>])
22       apply (subst minus_le_iff)
23       apply (rule cSup_upper)
24        apply force
25       using bdd
26       apply (force simp: abs_le_iff bdd_above_def)
27       done
28   next
29     show "Sup (uminus ` S) \<le> - Inf S"
30       apply (rule cSup_least)
31       using \<open>S \<noteq> {}\<close>
32        apply force
33       apply clarsimp
34       apply (rule cInf_lower)
35        apply assumption
36       using bdd
38       apply (rule_tac x = "- a" in exI)
39       apply force
40       done
41   qed
42   with cSup_abs_le [of "uminus ` S"] assms show ?thesis
43     by fastforce
44 qed
46 lemma cSup_asclose:
47   fixes S :: "'a::{linordered_idom,conditionally_complete_linorder} set"
48   assumes S: "S \<noteq> {}"
49     and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e"
50   shows "\<bar>Sup S - l\<bar> \<le> e"
51 proof -
52   have *: "\<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e" for x l e :: 'a
53     by arith
54   have "bdd_above S"
55     using b by (auto intro!: bdd_aboveI[of _ "l + e"])
56   with S b show ?thesis
57     unfolding * by (auto intro!: cSup_upper2 cSup_least)
58 qed
60 lemma cInf_asclose:
61   fixes S :: "'a::{linordered_idom,conditionally_complete_linorder} set"
62   assumes S: "S \<noteq> {}"
63     and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e"
64   shows "\<bar>Inf S - l\<bar> \<le> e"
65 proof -
66   have *: "\<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e" for x l e :: 'a
67     by arith
68   have "bdd_below S"
69     using b by (auto intro!: bdd_belowI[of _ "l - e"])
70   with S b show ?thesis
71     unfolding * by (auto intro!: cInf_lower2 cInf_greatest)
72 qed
75 subsection \<open>Class of Archimedean fields\<close>
77 text \<open>Archimedean fields have no infinite elements.\<close>
79 class archimedean_field = linordered_field +
80   assumes ex_le_of_int: "\<exists>z. x \<le> of_int z"
82 lemma ex_less_of_int: "\<exists>z. x < of_int z"
83   for x :: "'a::archimedean_field"
84 proof -
85   from ex_le_of_int obtain z where "x \<le> of_int z" ..
86   then have "x < of_int (z + 1)" by simp
87   then show ?thesis ..
88 qed
90 lemma ex_of_int_less: "\<exists>z. of_int z < x"
91   for x :: "'a::archimedean_field"
92 proof -
93   from ex_less_of_int obtain z where "- x < of_int z" ..
94   then have "of_int (- z) < x" by simp
95   then show ?thesis ..
96 qed
98 lemma reals_Archimedean2: "\<exists>n. x < of_nat n"
99   for x :: "'a::archimedean_field"
100 proof -
101   obtain z where "x < of_int z"
102     using ex_less_of_int ..
103   also have "\<dots> \<le> of_int (int (nat z))"
104     by simp
105   also have "\<dots> = of_nat (nat z)"
106     by (simp only: of_int_of_nat_eq)
107   finally show ?thesis ..
108 qed
110 lemma real_arch_simple: "\<exists>n. x \<le> of_nat n"
111   for x :: "'a::archimedean_field"
112 proof -
113   obtain n where "x < of_nat n"
114     using reals_Archimedean2 ..
115   then have "x \<le> of_nat n"
116     by simp
117   then show ?thesis ..
118 qed
120 text \<open>Archimedean fields have no infinitesimal elements.\<close>
122 lemma reals_Archimedean:
123   fixes x :: "'a::archimedean_field"
124   assumes "0 < x"
125   shows "\<exists>n. inverse (of_nat (Suc n)) < x"
126 proof -
127   from \<open>0 < x\<close> have "0 < inverse x"
128     by (rule positive_imp_inverse_positive)
129   obtain n where "inverse x < of_nat n"
130     using reals_Archimedean2 ..
131   then obtain m where "inverse x < of_nat (Suc m)"
132     using \<open>0 < inverse x\<close> by (cases n) (simp_all del: of_nat_Suc)
133   then have "inverse (of_nat (Suc m)) < inverse (inverse x)"
134     using \<open>0 < inverse x\<close> by (rule less_imp_inverse_less)
135   then have "inverse (of_nat (Suc m)) < x"
136     using \<open>0 < x\<close> by (simp add: nonzero_inverse_inverse_eq)
137   then show ?thesis ..
138 qed
140 lemma ex_inverse_of_nat_less:
141   fixes x :: "'a::archimedean_field"
142   assumes "0 < x"
143   shows "\<exists>n>0. inverse (of_nat n) < x"
144   using reals_Archimedean [OF \<open>0 < x\<close>] by auto
146 lemma ex_less_of_nat_mult:
147   fixes x :: "'a::archimedean_field"
148   assumes "0 < x"
149   shows "\<exists>n. y < of_nat n * x"
150 proof -
151   obtain n where "y / x < of_nat n"
152     using reals_Archimedean2 ..
153   with \<open>0 < x\<close> have "y < of_nat n * x"
155   then show ?thesis ..
156 qed
159 subsection \<open>Existence and uniqueness of floor function\<close>
161 lemma exists_least_lemma:
162   assumes "\<not> P 0" and "\<exists>n. P n"
163   shows "\<exists>n. \<not> P n \<and> P (Suc n)"
164 proof -
165   from \<open>\<exists>n. P n\<close> have "P (Least P)"
166     by (rule LeastI_ex)
167   with \<open>\<not> P 0\<close> obtain n where "Least P = Suc n"
168     by (cases "Least P") auto
169   then have "n < Least P"
170     by simp
171   then have "\<not> P n"
172     by (rule not_less_Least)
173   then have "\<not> P n \<and> P (Suc n)"
174     using \<open>P (Least P)\<close> \<open>Least P = Suc n\<close> by simp
175   then show ?thesis ..
176 qed
178 lemma floor_exists:
179   fixes x :: "'a::archimedean_field"
180   shows "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
181 proof (cases "0 \<le> x")
182   case True
183   then have "\<not> x < of_nat 0"
184     by simp
185   then have "\<exists>n. \<not> x < of_nat n \<and> x < of_nat (Suc n)"
186     using reals_Archimedean2 by (rule exists_least_lemma)
187   then obtain n where "\<not> x < of_nat n \<and> x < of_nat (Suc n)" ..
188   then have "of_int (int n) \<le> x \<and> x < of_int (int n + 1)"
189     by simp
190   then show ?thesis ..
191 next
192   case False
193   then have "\<not> - x \<le> of_nat 0"
194     by simp
195   then have "\<exists>n. \<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)"
196     using real_arch_simple by (rule exists_least_lemma)
197   then obtain n where "\<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)" ..
198   then have "of_int (- int n - 1) \<le> x \<and> x < of_int (- int n - 1 + 1)"
199     by simp
200   then show ?thesis ..
201 qed
203 lemma floor_exists1: "\<exists>!z. of_int z \<le> x \<and> x < of_int (z + 1)"
204   for x :: "'a::archimedean_field"
205 proof (rule ex_ex1I)
206   show "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
207     by (rule floor_exists)
208 next
209   fix y z
210   assume "of_int y \<le> x \<and> x < of_int (y + 1)"
211     and "of_int z \<le> x \<and> x < of_int (z + 1)"
212   with le_less_trans [of "of_int y" "x" "of_int (z + 1)"]
213        le_less_trans [of "of_int z" "x" "of_int (y + 1)"] show "y = z"
215 qed
218 subsection \<open>Floor function\<close>
220 class floor_ceiling = archimedean_field +
221   fixes floor :: "'a \<Rightarrow> int"  ("\<lfloor>_\<rfloor>")
222   assumes floor_correct: "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)"
224 lemma floor_unique: "of_int z \<le> x \<Longrightarrow> x < of_int z + 1 \<Longrightarrow> \<lfloor>x\<rfloor> = z"
225   using floor_correct [of x] floor_exists1 [of x] by auto
227 lemma floor_unique_iff: "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
228   for x :: "'a::floor_ceiling"
229   using floor_correct floor_unique by auto
231 lemma of_int_floor_le [simp]: "of_int \<lfloor>x\<rfloor> \<le> x"
232   using floor_correct ..
234 lemma le_floor_iff: "z \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z \<le> x"
235 proof
236   assume "z \<le> \<lfloor>x\<rfloor>"
237   then have "(of_int z :: 'a) \<le> of_int \<lfloor>x\<rfloor>" by simp
238   also have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
239   finally show "of_int z \<le> x" .
240 next
241   assume "of_int z \<le> x"
242   also have "x < of_int (\<lfloor>x\<rfloor> + 1)" using floor_correct ..
243   finally show "z \<le> \<lfloor>x\<rfloor>" by (simp del: of_int_add)
244 qed
246 lemma floor_less_iff: "\<lfloor>x\<rfloor> < z \<longleftrightarrow> x < of_int z"
247   by (simp add: not_le [symmetric] le_floor_iff)
249 lemma less_floor_iff: "z < \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z + 1 \<le> x"
250   using le_floor_iff [of "z + 1" x] by auto
252 lemma floor_le_iff: "\<lfloor>x\<rfloor> \<le> z \<longleftrightarrow> x < of_int z + 1"
253   by (simp add: not_less [symmetric] less_floor_iff)
255 lemma floor_split[arith_split]: "P \<lfloor>t\<rfloor> \<longleftrightarrow> (\<forall>i. of_int i \<le> t \<and> t < of_int i + 1 \<longrightarrow> P i)"
256   by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
258 lemma floor_mono:
259   assumes "x \<le> y"
260   shows "\<lfloor>x\<rfloor> \<le> \<lfloor>y\<rfloor>"
261 proof -
262   have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
263   also note \<open>x \<le> y\<close>
264   finally show ?thesis by (simp add: le_floor_iff)
265 qed
267 lemma floor_less_cancel: "\<lfloor>x\<rfloor> < \<lfloor>y\<rfloor> \<Longrightarrow> x < y"
268   by (auto simp add: not_le [symmetric] floor_mono)
270 lemma floor_of_int [simp]: "\<lfloor>of_int z\<rfloor> = z"
271   by (rule floor_unique) simp_all
273 lemma floor_of_nat [simp]: "\<lfloor>of_nat n\<rfloor> = int n"
274   using floor_of_int [of "of_nat n"] by simp
276 lemma le_floor_add: "\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor> \<le> \<lfloor>x + y\<rfloor>"
280 text \<open>Floor with numerals.\<close>
282 lemma floor_zero [simp]: "\<lfloor>0\<rfloor> = 0"
283   using floor_of_int [of 0] by simp
285 lemma floor_one [simp]: "\<lfloor>1\<rfloor> = 1"
286   using floor_of_int [of 1] by simp
288 lemma floor_numeral [simp]: "\<lfloor>numeral v\<rfloor> = numeral v"
289   using floor_of_int [of "numeral v"] by simp
291 lemma floor_neg_numeral [simp]: "\<lfloor>- numeral v\<rfloor> = - numeral v"
292   using floor_of_int [of "- numeral v"] by simp
294 lemma zero_le_floor [simp]: "0 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 0 \<le> x"
297 lemma one_le_floor [simp]: "1 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
300 lemma numeral_le_floor [simp]: "numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v \<le> x"
303 lemma neg_numeral_le_floor [simp]: "- numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v \<le> x"
306 lemma zero_less_floor [simp]: "0 < \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
309 lemma one_less_floor [simp]: "1 < \<lfloor>x\<rfloor> \<longleftrightarrow> 2 \<le> x"
312 lemma numeral_less_floor [simp]: "numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v + 1 \<le> x"
315 lemma neg_numeral_less_floor [simp]: "- numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v + 1 \<le> x"
318 lemma floor_le_zero [simp]: "\<lfloor>x\<rfloor> \<le> 0 \<longleftrightarrow> x < 1"
321 lemma floor_le_one [simp]: "\<lfloor>x\<rfloor> \<le> 1 \<longleftrightarrow> x < 2"
324 lemma floor_le_numeral [simp]: "\<lfloor>x\<rfloor> \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
327 lemma floor_le_neg_numeral [simp]: "\<lfloor>x\<rfloor> \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
330 lemma floor_less_zero [simp]: "\<lfloor>x\<rfloor> < 0 \<longleftrightarrow> x < 0"
333 lemma floor_less_one [simp]: "\<lfloor>x\<rfloor> < 1 \<longleftrightarrow> x < 1"
336 lemma floor_less_numeral [simp]: "\<lfloor>x\<rfloor> < numeral v \<longleftrightarrow> x < numeral v"
339 lemma floor_less_neg_numeral [simp]: "\<lfloor>x\<rfloor> < - numeral v \<longleftrightarrow> x < - numeral v"
343 text \<open>Addition and subtraction of integers.\<close>
345 lemma floor_add_of_int [simp]: "\<lfloor>x + of_int z\<rfloor> = \<lfloor>x\<rfloor> + z"
346   using floor_correct [of x] by (simp add: floor_unique)
348 lemma floor_add_numeral [simp]: "\<lfloor>x + numeral v\<rfloor> = \<lfloor>x\<rfloor> + numeral v"
349   using floor_add_of_int [of x "numeral v"] by simp
351 lemma floor_add_one [simp]: "\<lfloor>x + 1\<rfloor> = \<lfloor>x\<rfloor> + 1"
352   using floor_add_of_int [of x 1] by simp
354 lemma floor_diff_of_int [simp]: "\<lfloor>x - of_int z\<rfloor> = \<lfloor>x\<rfloor> - z"
357 lemma floor_uminus_of_int [simp]: "\<lfloor>- (of_int z)\<rfloor> = - z"
358   by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
360 lemma floor_diff_numeral [simp]: "\<lfloor>x - numeral v\<rfloor> = \<lfloor>x\<rfloor> - numeral v"
361   using floor_diff_of_int [of x "numeral v"] by simp
363 lemma floor_diff_one [simp]: "\<lfloor>x - 1\<rfloor> = \<lfloor>x\<rfloor> - 1"
364   using floor_diff_of_int [of x 1] by simp
366 lemma le_mult_floor:
367   assumes "0 \<le> a" and "0 \<le> b"
368   shows "\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor> \<le> \<lfloor>a * b\<rfloor>"
369 proof -
370   have "of_int \<lfloor>a\<rfloor> \<le> a" and "of_int \<lfloor>b\<rfloor> \<le> b"
371     by (auto intro: of_int_floor_le)
372   then have "of_int (\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor>) \<le> a * b"
373     using assms by (auto intro!: mult_mono)
374   also have "a * b < of_int (\<lfloor>a * b\<rfloor> + 1)"
375     using floor_correct[of "a * b"] by auto
376   finally show ?thesis
377     unfolding of_int_less_iff by simp
378 qed
380 lemma floor_divide_of_int_eq: "\<lfloor>of_int k / of_int l\<rfloor> = k div l"
381   for k l :: int
382 proof (cases "l = 0")
383   case True
384   then show ?thesis by simp
385 next
386   case False
387   have *: "\<lfloor>of_int (k mod l) / of_int l :: 'a\<rfloor> = 0"
388   proof (cases "l > 0")
389     case True
390     then show ?thesis
391       by (auto intro: floor_unique)
392   next
393     case False
394     obtain r where "r = - l"
395       by blast
396     then have l: "l = - r"
397       by simp
398     with \<open>l \<noteq> 0\<close> False have "r > 0"
399       by simp
400     with l show ?thesis
401       using pos_mod_bound [of r]
402       by (auto simp add: zmod_zminus2_eq_if less_le field_simps intro: floor_unique)
403   qed
404   have "(of_int k :: 'a) = of_int (k div l * l + k mod l)"
405     by simp
406   also have "\<dots> = (of_int (k div l) + of_int (k mod l) / of_int l) * of_int l"
408   finally have "(of_int k / of_int l :: 'a) = \<dots> / of_int l"
409     by simp
410   then have "(of_int k / of_int l :: 'a) = of_int (k div l) + of_int (k mod l) / of_int l"
411     using False by (simp only:) (simp add: field_simps)
412   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>"
413     by simp
414   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>"
416   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"
417     by simp
418   with * show ?thesis
419     by simp
420 qed
422 lemma floor_divide_of_nat_eq: "\<lfloor>of_nat m / of_nat n\<rfloor> = of_nat (m div n)"
423   for m n :: nat
424 proof (cases "n = 0")
425   case True
426   then show ?thesis by simp
427 next
428   case False
429   then have *: "\<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> = 0"
430     by (auto intro: floor_unique)
431   have "(of_nat m :: 'a) = of_nat (m div n * n + m mod n)"
432     by simp
433   also have "\<dots> = (of_nat (m div n) + of_nat (m mod n) / of_nat n) * of_nat n"
435   finally have "(of_nat m / of_nat n :: 'a) = \<dots> / of_nat n"
436     by simp
437   then have "(of_nat m / of_nat n :: 'a) = of_nat (m div n) + of_nat (m mod n) / of_nat n"
438     using False by (simp only:) simp
439   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>"
440     by simp
441   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>"
443   moreover have "(of_nat (m div n) :: 'a) = of_int (of_nat (m div n))"
444     by simp
445   ultimately have "\<lfloor>of_nat m / of_nat n :: 'a\<rfloor> =
446       \<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> + of_nat (m div n)"
448   with * show ?thesis
449     by simp
450 qed
453 subsection \<open>Ceiling function\<close>
455 definition ceiling :: "'a::floor_ceiling \<Rightarrow> int"  ("\<lceil>_\<rceil>")
456   where "\<lceil>x\<rceil> = - \<lfloor>- x\<rfloor>"
458 lemma ceiling_correct: "of_int \<lceil>x\<rceil> - 1 < x \<and> x \<le> of_int \<lceil>x\<rceil>"
459   unfolding ceiling_def using floor_correct [of "- x"]
462 lemma ceiling_unique: "of_int z - 1 < x \<Longrightarrow> x \<le> of_int z \<Longrightarrow> \<lceil>x\<rceil> = z"
463   unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
465 lemma le_of_int_ceiling [simp]: "x \<le> of_int \<lceil>x\<rceil>"
466   using ceiling_correct ..
468 lemma ceiling_le_iff: "\<lceil>x\<rceil> \<le> z \<longleftrightarrow> x \<le> of_int z"
469   unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
471 lemma less_ceiling_iff: "z < \<lceil>x\<rceil> \<longleftrightarrow> of_int z < x"
472   by (simp add: not_le [symmetric] ceiling_le_iff)
474 lemma ceiling_less_iff: "\<lceil>x\<rceil> < z \<longleftrightarrow> x \<le> of_int z - 1"
475   using ceiling_le_iff [of x "z - 1"] by simp
477 lemma le_ceiling_iff: "z \<le> \<lceil>x\<rceil> \<longleftrightarrow> of_int z - 1 < x"
478   by (simp add: not_less [symmetric] ceiling_less_iff)
480 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> \<lceil>x\<rceil> \<ge> \<lceil>y\<rceil>"
481   unfolding ceiling_def by (simp add: floor_mono)
483 lemma ceiling_less_cancel: "\<lceil>x\<rceil> < \<lceil>y\<rceil> \<Longrightarrow> x < y"
484   by (auto simp add: not_le [symmetric] ceiling_mono)
486 lemma ceiling_of_int [simp]: "\<lceil>of_int z\<rceil> = z"
487   by (rule ceiling_unique) simp_all
489 lemma ceiling_of_nat [simp]: "\<lceil>of_nat n\<rceil> = int n"
490   using ceiling_of_int [of "of_nat n"] by simp
492 lemma ceiling_add_le: "\<lceil>x + y\<rceil> \<le> \<lceil>x\<rceil> + \<lceil>y\<rceil>"
496 text \<open>Ceiling with numerals.\<close>
498 lemma ceiling_zero [simp]: "\<lceil>0\<rceil> = 0"
499   using ceiling_of_int [of 0] by simp
501 lemma ceiling_one [simp]: "\<lceil>1\<rceil> = 1"
502   using ceiling_of_int [of 1] by simp
504 lemma ceiling_numeral [simp]: "\<lceil>numeral v\<rceil> = numeral v"
505   using ceiling_of_int [of "numeral v"] by simp
507 lemma ceiling_neg_numeral [simp]: "\<lceil>- numeral v\<rceil> = - numeral v"
508   using ceiling_of_int [of "- numeral v"] by simp
510 lemma ceiling_le_zero [simp]: "\<lceil>x\<rceil> \<le> 0 \<longleftrightarrow> x \<le> 0"
513 lemma ceiling_le_one [simp]: "\<lceil>x\<rceil> \<le> 1 \<longleftrightarrow> x \<le> 1"
516 lemma ceiling_le_numeral [simp]: "\<lceil>x\<rceil> \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
519 lemma ceiling_le_neg_numeral [simp]: "\<lceil>x\<rceil> \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
522 lemma ceiling_less_zero [simp]: "\<lceil>x\<rceil> < 0 \<longleftrightarrow> x \<le> -1"
525 lemma ceiling_less_one [simp]: "\<lceil>x\<rceil> < 1 \<longleftrightarrow> x \<le> 0"
528 lemma ceiling_less_numeral [simp]: "\<lceil>x\<rceil> < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
531 lemma ceiling_less_neg_numeral [simp]: "\<lceil>x\<rceil> < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
534 lemma zero_le_ceiling [simp]: "0 \<le> \<lceil>x\<rceil> \<longleftrightarrow> -1 < x"
537 lemma one_le_ceiling [simp]: "1 \<le> \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
540 lemma numeral_le_ceiling [simp]: "numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> numeral v - 1 < x"
543 lemma neg_numeral_le_ceiling [simp]: "- numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> - numeral v - 1 < x"
546 lemma zero_less_ceiling [simp]: "0 < \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
549 lemma one_less_ceiling [simp]: "1 < \<lceil>x\<rceil> \<longleftrightarrow> 1 < x"
552 lemma numeral_less_ceiling [simp]: "numeral v < \<lceil>x\<rceil> \<longleftrightarrow> numeral v < x"
555 lemma neg_numeral_less_ceiling [simp]: "- numeral v < \<lceil>x\<rceil> \<longleftrightarrow> - numeral v < x"
558 lemma ceiling_altdef: "\<lceil>x\<rceil> = (if x = of_int \<lfloor>x\<rfloor> then \<lfloor>x\<rfloor> else \<lfloor>x\<rfloor> + 1)"
559   by (intro ceiling_unique; simp, linarith?)
561 lemma floor_le_ceiling [simp]: "\<lfloor>x\<rfloor> \<le> \<lceil>x\<rceil>"
565 text \<open>Addition and subtraction of integers.\<close>
567 lemma ceiling_add_of_int [simp]: "\<lceil>x + of_int z\<rceil> = \<lceil>x\<rceil> + z"
568   using ceiling_correct [of x] by (simp add: ceiling_def)
570 lemma ceiling_add_numeral [simp]: "\<lceil>x + numeral v\<rceil> = \<lceil>x\<rceil> + numeral v"
571   using ceiling_add_of_int [of x "numeral v"] by simp
573 lemma ceiling_add_one [simp]: "\<lceil>x + 1\<rceil> = \<lceil>x\<rceil> + 1"
574   using ceiling_add_of_int [of x 1] by simp
576 lemma ceiling_diff_of_int [simp]: "\<lceil>x - of_int z\<rceil> = \<lceil>x\<rceil> - z"
579 lemma ceiling_diff_numeral [simp]: "\<lceil>x - numeral v\<rceil> = \<lceil>x\<rceil> - numeral v"
580   using ceiling_diff_of_int [of x "numeral v"] by simp
582 lemma ceiling_diff_one [simp]: "\<lceil>x - 1\<rceil> = \<lceil>x\<rceil> - 1"
583   using ceiling_diff_of_int [of x 1] by simp
585 lemma ceiling_split[arith_split]: "P \<lceil>t\<rceil> \<longleftrightarrow> (\<forall>i. of_int i - 1 < t \<and> t \<le> of_int i \<longrightarrow> P i)"
586   by (auto simp add: ceiling_unique ceiling_correct)
588 lemma ceiling_diff_floor_le_1: "\<lceil>x\<rceil> - \<lfloor>x\<rfloor> \<le> 1"
589 proof -
590   have "of_int \<lceil>x\<rceil> - 1 < x"
591     using ceiling_correct[of x] by simp
592   also have "x < of_int \<lfloor>x\<rfloor> + 1"
593     using floor_correct[of x] by simp_all
594   finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
595     by simp
596   then show ?thesis
597     unfolding of_int_less_iff by simp
598 qed
601 subsection \<open>Negation\<close>
603 lemma floor_minus: "\<lfloor>- x\<rfloor> = - \<lceil>x\<rceil>"
604   unfolding ceiling_def by simp
606 lemma ceiling_minus: "\<lceil>- x\<rceil> = - \<lfloor>x\<rfloor>"
607   unfolding ceiling_def by simp
610 subsection \<open>Frac Function\<close>
612 definition frac :: "'a \<Rightarrow> 'a::floor_ceiling"
613   where "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
615 lemma frac_lt_1: "frac x < 1"
616   by (simp add: frac_def) linarith
618 lemma frac_eq_0_iff [simp]: "frac x = 0 \<longleftrightarrow> x \<in> \<int>"
619   by (simp add: frac_def) (metis Ints_cases Ints_of_int floor_of_int )
621 lemma frac_ge_0 [simp]: "frac x \<ge> 0"
622   unfolding frac_def by linarith
624 lemma frac_gt_0_iff [simp]: "frac x > 0 \<longleftrightarrow> x \<notin> \<int>"
625   by (metis frac_eq_0_iff frac_ge_0 le_less less_irrefl)
627 lemma frac_of_int [simp]: "frac (of_int z) = 0"
630 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)"
631 proof -
632   have "\<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>" if "x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
634   moreover
635   have "\<lfloor>x + y\<rfloor> = 1 + (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>)" if "\<not> x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
636     using that
638     apply (auto simp add: algebra_simps)
639     apply linarith
640     done
641   ultimately show ?thesis
642     by (auto simp add: frac_def algebra_simps)
643 qed
645 lemma floor_add2[simp]: "frac x = 0 \<or> frac y = 0 \<Longrightarrow> \<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>"
649   "frac (x + y) = (if frac x + frac y < 1 then frac x + frac y else (frac x + frac y) - 1)"
652 lemma frac_unique_iff: "frac x = a \<longleftrightarrow> x - a \<in> \<int> \<and> 0 \<le> a \<and> a < 1"
653   for x :: "'a::floor_ceiling"
654   apply (auto simp: Ints_def frac_def algebra_simps floor_unique)
655    apply linarith+
656   done
658 lemma frac_eq: "frac x = x \<longleftrightarrow> 0 \<le> x \<and> x < 1"
661 lemma frac_neg: "frac (- x) = (if x \<in> \<int> then 0 else 1 - frac x)"
662   for x :: "'a::floor_ceiling"
663   apply (auto simp add: frac_unique_iff)
665   apply (meson frac_lt_1 less_iff_diff_less_0 not_le not_less_iff_gr_or_eq)
666   done
669 subsection \<open>Rounding to the nearest integer\<close>
671 definition round :: "'a::floor_ceiling \<Rightarrow> int"
672   where "round x = \<lfloor>x + 1/2\<rfloor>"
674 lemma of_int_round_ge: "of_int (round x) \<ge> x - 1/2"
675   and of_int_round_le: "of_int (round x) \<le> x + 1/2"
676   and of_int_round_abs_le: "\<bar>of_int (round x) - x\<bar> \<le> 1/2"
677   and of_int_round_gt: "of_int (round x) > x - 1/2"
678 proof -
679   from floor_correct[of "x + 1/2"] have "x + 1/2 < of_int (round x) + 1"
681   from add_strict_right_mono[OF this, of "-1"] show A: "of_int (round x) > x - 1/2"
682     by simp
683   then show "of_int (round x) \<ge> x - 1/2"
684     by simp
685   from floor_correct[of "x + 1/2"] show "of_int (round x) \<le> x + 1/2"
687   with A show "\<bar>of_int (round x) - x\<bar> \<le> 1/2"
688     by linarith
689 qed
691 lemma round_of_int [simp]: "round (of_int n) = n"
692   unfolding round_def by (subst floor_unique_iff) force
694 lemma round_0 [simp]: "round 0 = 0"
695   using round_of_int[of 0] by simp
697 lemma round_1 [simp]: "round 1 = 1"
698   using round_of_int[of 1] by simp
700 lemma round_numeral [simp]: "round (numeral n) = numeral n"
701   using round_of_int[of "numeral n"] by simp
703 lemma round_neg_numeral [simp]: "round (-numeral n) = -numeral n"
704   using round_of_int[of "-numeral n"] by simp
706 lemma round_of_nat [simp]: "round (of_nat n) = of_nat n"
707   using round_of_int[of "int n"] by simp
709 lemma round_mono: "x \<le> y \<Longrightarrow> round x \<le> round y"
710   unfolding round_def by (intro floor_mono) simp
712 lemma round_unique: "of_int y > x - 1/2 \<Longrightarrow> of_int y \<le> x + 1/2 \<Longrightarrow> round x = y"
713   unfolding round_def
714 proof (rule floor_unique)
715   assume "x - 1 / 2 < of_int y"
716   from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1"
717     by simp
718 qed
720 lemma round_altdef: "round x = (if frac x \<ge> 1/2 then \<lceil>x\<rceil> else \<lfloor>x\<rfloor>)"
721   by (cases "frac x \<ge> 1/2")
722     (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef; linarith)+)[2])+
724 lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
725   unfolding round_def by (intro floor_mono) simp
727 lemma ceiling_ge_round: "\<lceil>x\<rceil> \<ge> round x"
728   unfolding round_altdef by simp
730 lemma round_diff_minimal: "\<bar>z - of_int (round z)\<bar> \<le> \<bar>z - of_int m\<bar>"
731   for z :: "'a::floor_ceiling"
732 proof (cases "of_int m \<ge> z")
733   case True
734   then have "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lceil>z\<rceil> - z\<bar>"
735     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith
736   also have "of_int \<lceil>z\<rceil> - z \<ge> 0"
737     by linarith
738   with True have "\<bar>of_int \<lceil>z\<rceil> - z\<bar> \<le> \<bar>z - of_int m\<bar>"