src/HOL/Archimedean_Field.thy
 author paulson Mon May 23 15:33:24 2016 +0100 (2016-05-23) changeset 63114 27afe7af7379 parent 62623 dbc62f86a1a9 child 63331 247eac9758dd permissions -rw-r--r--
Lots of new material for multivariate analysis
```     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 reals_Archimedean2:
```
```    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 real_arch_simple:
```
```    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 reals_Archimedean2 ..
```
```    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 reals_Archimedean:
```
```    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 reals_Archimedean2 ..
```
```    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 reals_Archimedean [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 reals_Archimedean2 ..
```
```    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 reals_Archimedean2 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 real_arch_simple 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"  ("\<lfloor>_\<rfloor>")
```
```   142   assumes floor_correct: "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)"
```
```   143
```
```   144 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> \<lfloor>x\<rfloor> = z"
```
```   145   using floor_correct [of x] floor_exists1 [of x] by auto
```
```   146
```
```   147 lemma floor_unique_iff:
```
```   148   fixes x :: "'a::floor_ceiling"
```
```   149   shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
```
```   150 using floor_correct floor_unique by auto
```
```   151
```
```   152 lemma of_int_floor_le [simp]: "of_int \<lfloor>x\<rfloor> \<le> x"
```
```   153   using floor_correct ..
```
```   154
```
```   155 lemma le_floor_iff: "z \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z \<le> x"
```
```   156 proof
```
```   157   assume "z \<le> \<lfloor>x\<rfloor>"
```
```   158   then have "(of_int z :: 'a) \<le> of_int \<lfloor>x\<rfloor>" by simp
```
```   159   also have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
```
```   160   finally show "of_int z \<le> x" .
```
```   161 next
```
```   162   assume "of_int z \<le> x"
```
```   163   also have "x < of_int (\<lfloor>x\<rfloor> + 1)" using floor_correct ..
```
```   164   finally show "z \<le> \<lfloor>x\<rfloor>" by (simp del: of_int_add)
```
```   165 qed
```
```   166
```
```   167 lemma floor_less_iff: "\<lfloor>x\<rfloor> < z \<longleftrightarrow> x < of_int z"
```
```   168   by (simp add: not_le [symmetric] le_floor_iff)
```
```   169
```
```   170 lemma less_floor_iff: "z < \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z + 1 \<le> x"
```
```   171   using le_floor_iff [of "z + 1" x] by auto
```
```   172
```
```   173 lemma floor_le_iff: "\<lfloor>x\<rfloor> \<le> z \<longleftrightarrow> x < of_int z + 1"
```
```   174   by (simp add: not_less [symmetric] less_floor_iff)
```
```   175
```
```   176 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)"
```
```   177   by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
```
```   178
```
```   179 lemma floor_mono:
```
```   180   assumes "x \<le> y"
```
```   181   shows "\<lfloor>x\<rfloor> \<le> \<lfloor>y\<rfloor>"
```
```   182 proof -
```
```   183   have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
```
```   184   also note \<open>x \<le> y\<close>
```
```   185   finally show ?thesis by (simp add: le_floor_iff)
```
```   186 qed
```
```   187
```
```   188 lemma floor_less_cancel: "\<lfloor>x\<rfloor> < \<lfloor>y\<rfloor> \<Longrightarrow> x < y"
```
```   189   by (auto simp add: not_le [symmetric] floor_mono)
```
```   190
```
```   191 lemma floor_of_int [simp]: "\<lfloor>of_int z\<rfloor> = z"
```
```   192   by (rule floor_unique) simp_all
```
```   193
```
```   194 lemma floor_of_nat [simp]: "\<lfloor>of_nat n\<rfloor> = int n"
```
```   195   using floor_of_int [of "of_nat n"] by simp
```
```   196
```
```   197 lemma le_floor_add: "\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor> \<le> \<lfloor>x + y\<rfloor>"
```
```   198   by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
```
```   199
```
```   200 text \<open>Floor with numerals\<close>
```
```   201
```
```   202 lemma floor_zero [simp]: "\<lfloor>0\<rfloor> = 0"
```
```   203   using floor_of_int [of 0] by simp
```
```   204
```
```   205 lemma floor_one [simp]: "\<lfloor>1\<rfloor> = 1"
```
```   206   using floor_of_int [of 1] by simp
```
```   207
```
```   208 lemma floor_numeral [simp]: "\<lfloor>numeral v\<rfloor> = numeral v"
```
```   209   using floor_of_int [of "numeral v"] by simp
```
```   210
```
```   211 lemma floor_neg_numeral [simp]: "\<lfloor>- numeral v\<rfloor> = - numeral v"
```
```   212   using floor_of_int [of "- numeral v"] by simp
```
```   213
```
```   214 lemma zero_le_floor [simp]: "0 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 0 \<le> x"
```
```   215   by (simp add: le_floor_iff)
```
```   216
```
```   217 lemma one_le_floor [simp]: "1 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
```
```   218   by (simp add: le_floor_iff)
```
```   219
```
```   220 lemma numeral_le_floor [simp]:
```
```   221   "numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v \<le> x"
```
```   222   by (simp add: le_floor_iff)
```
```   223
```
```   224 lemma neg_numeral_le_floor [simp]:
```
```   225   "- numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v \<le> x"
```
```   226   by (simp add: le_floor_iff)
```
```   227
```
```   228 lemma zero_less_floor [simp]: "0 < \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
```
```   229   by (simp add: less_floor_iff)
```
```   230
```
```   231 lemma one_less_floor [simp]: "1 < \<lfloor>x\<rfloor> \<longleftrightarrow> 2 \<le> x"
```
```   232   by (simp add: less_floor_iff)
```
```   233
```
```   234 lemma numeral_less_floor [simp]:
```
```   235   "numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v + 1 \<le> x"
```
```   236   by (simp add: less_floor_iff)
```
```   237
```
```   238 lemma neg_numeral_less_floor [simp]:
```
```   239   "- numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v + 1 \<le> x"
```
```   240   by (simp add: less_floor_iff)
```
```   241
```
```   242 lemma floor_le_zero [simp]: "\<lfloor>x\<rfloor> \<le> 0 \<longleftrightarrow> x < 1"
```
```   243   by (simp add: floor_le_iff)
```
```   244
```
```   245 lemma floor_le_one [simp]: "\<lfloor>x\<rfloor> \<le> 1 \<longleftrightarrow> x < 2"
```
```   246   by (simp add: floor_le_iff)
```
```   247
```
```   248 lemma floor_le_numeral [simp]:
```
```   249   "\<lfloor>x\<rfloor> \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
```
```   250   by (simp add: floor_le_iff)
```
```   251
```
```   252 lemma floor_le_neg_numeral [simp]:
```
```   253   "\<lfloor>x\<rfloor> \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
```
```   254   by (simp add: floor_le_iff)
```
```   255
```
```   256 lemma floor_less_zero [simp]: "\<lfloor>x\<rfloor> < 0 \<longleftrightarrow> x < 0"
```
```   257   by (simp add: floor_less_iff)
```
```   258
```
```   259 lemma floor_less_one [simp]: "\<lfloor>x\<rfloor> < 1 \<longleftrightarrow> x < 1"
```
```   260   by (simp add: floor_less_iff)
```
```   261
```
```   262 lemma floor_less_numeral [simp]:
```
```   263   "\<lfloor>x\<rfloor> < numeral v \<longleftrightarrow> x < numeral v"
```
```   264   by (simp add: floor_less_iff)
```
```   265
```
```   266 lemma floor_less_neg_numeral [simp]:
```
```   267   "\<lfloor>x\<rfloor> < - numeral v \<longleftrightarrow> x < - numeral v"
```
```   268   by (simp add: floor_less_iff)
```
```   269
```
```   270 text \<open>Addition and subtraction of integers\<close>
```
```   271
```
```   272 lemma floor_add_of_int [simp]: "\<lfloor>x + of_int z\<rfloor> = \<lfloor>x\<rfloor> + z"
```
```   273   using floor_correct [of x] by (simp add: floor_unique)
```
```   274
```
```   275 lemma floor_add_numeral [simp]:
```
```   276     "\<lfloor>x + numeral v\<rfloor> = \<lfloor>x\<rfloor> + numeral v"
```
```   277   using floor_add_of_int [of x "numeral v"] by simp
```
```   278
```
```   279 lemma floor_add_one [simp]: "\<lfloor>x + 1\<rfloor> = \<lfloor>x\<rfloor> + 1"
```
```   280   using floor_add_of_int [of x 1] by simp
```
```   281
```
```   282 lemma floor_diff_of_int [simp]: "\<lfloor>x - of_int z\<rfloor> = \<lfloor>x\<rfloor> - z"
```
```   283   using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
```
```   284
```
```   285 lemma floor_uminus_of_int [simp]: "\<lfloor>- (of_int z)\<rfloor> = - z"
```
```   286   by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
```
```   287
```
```   288 lemma floor_diff_numeral [simp]:
```
```   289   "\<lfloor>x - numeral v\<rfloor> = \<lfloor>x\<rfloor> - numeral v"
```
```   290   using floor_diff_of_int [of x "numeral v"] by simp
```
```   291
```
```   292 lemma floor_diff_one [simp]: "\<lfloor>x - 1\<rfloor> = \<lfloor>x\<rfloor> - 1"
```
```   293   using floor_diff_of_int [of x 1] by simp
```
```   294
```
```   295 lemma le_mult_floor:
```
```   296   assumes "0 \<le> a" and "0 \<le> b"
```
```   297   shows "\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor> \<le> \<lfloor>a * b\<rfloor>"
```
```   298 proof -
```
```   299   have "of_int \<lfloor>a\<rfloor> \<le> a"
```
```   300     and "of_int \<lfloor>b\<rfloor> \<le> b" by (auto intro: of_int_floor_le)
```
```   301   hence "of_int (\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor>) \<le> a * b"
```
```   302     using assms by (auto intro!: mult_mono)
```
```   303   also have "a * b < of_int (\<lfloor>a * b\<rfloor> + 1)"
```
```   304     using floor_correct[of "a * b"] by auto
```
```   305   finally show ?thesis unfolding of_int_less_iff by simp
```
```   306 qed
```
```   307
```
```   308 lemma floor_divide_of_int_eq:
```
```   309   fixes k l :: int
```
```   310   shows "\<lfloor>of_int k / of_int l\<rfloor> = k div l"
```
```   311 proof (cases "l = 0")
```
```   312   case True then show ?thesis by simp
```
```   313 next
```
```   314   case False
```
```   315   have *: "\<lfloor>of_int (k mod l) / of_int l :: 'a\<rfloor> = 0"
```
```   316   proof (cases "l > 0")
```
```   317     case True then show ?thesis
```
```   318       by (auto intro: floor_unique)
```
```   319   next
```
```   320     case False
```
```   321     obtain r where "r = - l" by blast
```
```   322     then have l: "l = - r" by simp
```
```   323     moreover with \<open>l \<noteq> 0\<close> False have "r > 0" by simp
```
```   324     ultimately show ?thesis using pos_mod_bound [of r]
```
```   325       by (auto simp add: zmod_zminus2_eq_if less_le field_simps intro: floor_unique)
```
```   326   qed
```
```   327   have "(of_int k :: 'a) = of_int (k div l * l + k mod l)"
```
```   328     by simp
```
```   329   also have "\<dots> = (of_int (k div l) + of_int (k mod l) / of_int l) * of_int l"
```
```   330     using False by (simp only: of_int_add) (simp add: field_simps)
```
```   331   finally have "(of_int k / of_int l :: 'a) = \<dots> / of_int l"
```
```   332     by simp
```
```   333   then have "(of_int k / of_int l :: 'a) = of_int (k div l) + of_int (k mod l) / of_int l"
```
```   334     using False by (simp only:) (simp add: field_simps)
```
```   335   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>"
```
```   336     by simp
```
```   337   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>"
```
```   338     by (simp add: ac_simps)
```
```   339   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"
```
```   340     by simp
```
```   341   with * show ?thesis by simp
```
```   342 qed
```
```   343
```
```   344 lemma floor_divide_of_nat_eq:
```
```   345   fixes m n :: nat
```
```   346   shows "\<lfloor>of_nat m / of_nat n\<rfloor> = of_nat (m div n)"
```
```   347 proof (cases "n = 0")
```
```   348   case True then show ?thesis by simp
```
```   349 next
```
```   350   case False
```
```   351   then have *: "\<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> = 0"
```
```   352     by (auto intro: floor_unique)
```
```   353   have "(of_nat m :: 'a) = of_nat (m div n * n + m mod n)"
```
```   354     by simp
```
```   355   also have "\<dots> = (of_nat (m div n) + of_nat (m mod n) / of_nat n) * of_nat n"
```
```   356     using False by (simp only: of_nat_add) (simp add: field_simps of_nat_mult)
```
```   357   finally have "(of_nat m / of_nat n :: 'a) = \<dots> / of_nat n"
```
```   358     by simp
```
```   359   then have "(of_nat m / of_nat n :: 'a) = of_nat (m div n) + of_nat (m mod n) / of_nat n"
```
```   360     using False by (simp only:) simp
```
```   361   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>"
```
```   362     by simp
```
```   363   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>"
```
```   364     by (simp add: ac_simps)
```
```   365   moreover have "(of_nat (m div n) :: 'a) = of_int (of_nat (m div n))"
```
```   366     by simp
```
```   367   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)"
```
```   368     by (simp only: floor_add_of_int)
```
```   369   with * show ?thesis by simp
```
```   370 qed
```
```   371
```
```   372
```
```   373 subsection \<open>Ceiling function\<close>
```
```   374
```
```   375 definition ceiling :: "'a::floor_ceiling \<Rightarrow> int"  ("\<lceil>_\<rceil>")
```
```   376   where "\<lceil>x\<rceil> = - \<lfloor>- x\<rfloor>"
```
```   377
```
```   378 lemma ceiling_correct: "of_int \<lceil>x\<rceil> - 1 < x \<and> x \<le> of_int \<lceil>x\<rceil>"
```
```   379   unfolding ceiling_def using floor_correct [of "- x"] by (simp add: le_minus_iff)
```
```   380
```
```   381 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> \<lceil>x\<rceil> = z"
```
```   382   unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
```
```   383
```
```   384 lemma le_of_int_ceiling [simp]: "x \<le> of_int \<lceil>x\<rceil>"
```
```   385   using ceiling_correct ..
```
```   386
```
```   387 lemma ceiling_le_iff: "\<lceil>x\<rceil> \<le> z \<longleftrightarrow> x \<le> of_int z"
```
```   388   unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
```
```   389
```
```   390 lemma less_ceiling_iff: "z < \<lceil>x\<rceil> \<longleftrightarrow> of_int z < x"
```
```   391   by (simp add: not_le [symmetric] ceiling_le_iff)
```
```   392
```
```   393 lemma ceiling_less_iff: "\<lceil>x\<rceil> < z \<longleftrightarrow> x \<le> of_int z - 1"
```
```   394   using ceiling_le_iff [of x "z - 1"] by simp
```
```   395
```
```   396 lemma le_ceiling_iff: "z \<le> \<lceil>x\<rceil> \<longleftrightarrow> of_int z - 1 < x"
```
```   397   by (simp add: not_less [symmetric] ceiling_less_iff)
```
```   398
```
```   399 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> \<lceil>x\<rceil> \<ge> \<lceil>y\<rceil>"
```
```   400   unfolding ceiling_def by (simp add: floor_mono)
```
```   401
```
```   402 lemma ceiling_less_cancel: "\<lceil>x\<rceil> < \<lceil>y\<rceil> \<Longrightarrow> x < y"
```
```   403   by (auto simp add: not_le [symmetric] ceiling_mono)
```
```   404
```
```   405 lemma ceiling_of_int [simp]: "\<lceil>of_int z\<rceil> = z"
```
```   406   by (rule ceiling_unique) simp_all
```
```   407
```
```   408 lemma ceiling_of_nat [simp]: "\<lceil>of_nat n\<rceil> = int n"
```
```   409   using ceiling_of_int [of "of_nat n"] by simp
```
```   410
```
```   411 lemma ceiling_add_le: "\<lceil>x + y\<rceil> \<le> \<lceil>x\<rceil> + \<lceil>y\<rceil>"
```
```   412   by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
```
```   413
```
```   414 text \<open>Ceiling with numerals\<close>
```
```   415
```
```   416 lemma ceiling_zero [simp]: "\<lceil>0\<rceil> = 0"
```
```   417   using ceiling_of_int [of 0] by simp
```
```   418
```
```   419 lemma ceiling_one [simp]: "\<lceil>1\<rceil> = 1"
```
```   420   using ceiling_of_int [of 1] by simp
```
```   421
```
```   422 lemma ceiling_numeral [simp]: "\<lceil>numeral v\<rceil> = numeral v"
```
```   423   using ceiling_of_int [of "numeral v"] by simp
```
```   424
```
```   425 lemma ceiling_neg_numeral [simp]: "\<lceil>- numeral v\<rceil> = - numeral v"
```
```   426   using ceiling_of_int [of "- numeral v"] by simp
```
```   427
```
```   428 lemma ceiling_le_zero [simp]: "\<lceil>x\<rceil> \<le> 0 \<longleftrightarrow> x \<le> 0"
```
```   429   by (simp add: ceiling_le_iff)
```
```   430
```
```   431 lemma ceiling_le_one [simp]: "\<lceil>x\<rceil> \<le> 1 \<longleftrightarrow> x \<le> 1"
```
```   432   by (simp add: ceiling_le_iff)
```
```   433
```
```   434 lemma ceiling_le_numeral [simp]:
```
```   435   "\<lceil>x\<rceil> \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
```
```   436   by (simp add: ceiling_le_iff)
```
```   437
```
```   438 lemma ceiling_le_neg_numeral [simp]:
```
```   439   "\<lceil>x\<rceil> \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
```
```   440   by (simp add: ceiling_le_iff)
```
```   441
```
```   442 lemma ceiling_less_zero [simp]: "\<lceil>x\<rceil> < 0 \<longleftrightarrow> x \<le> -1"
```
```   443   by (simp add: ceiling_less_iff)
```
```   444
```
```   445 lemma ceiling_less_one [simp]: "\<lceil>x\<rceil> < 1 \<longleftrightarrow> x \<le> 0"
```
```   446   by (simp add: ceiling_less_iff)
```
```   447
```
```   448 lemma ceiling_less_numeral [simp]:
```
```   449   "\<lceil>x\<rceil> < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
```
```   450   by (simp add: ceiling_less_iff)
```
```   451
```
```   452 lemma ceiling_less_neg_numeral [simp]:
```
```   453   "\<lceil>x\<rceil> < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
```
```   454   by (simp add: ceiling_less_iff)
```
```   455
```
```   456 lemma zero_le_ceiling [simp]: "0 \<le> \<lceil>x\<rceil> \<longleftrightarrow> -1 < x"
```
```   457   by (simp add: le_ceiling_iff)
```
```   458
```
```   459 lemma one_le_ceiling [simp]: "1 \<le> \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
```
```   460   by (simp add: le_ceiling_iff)
```
```   461
```
```   462 lemma numeral_le_ceiling [simp]:
```
```   463   "numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> numeral v - 1 < x"
```
```   464   by (simp add: le_ceiling_iff)
```
```   465
```
```   466 lemma neg_numeral_le_ceiling [simp]:
```
```   467   "- numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> - numeral v - 1 < x"
```
```   468   by (simp add: le_ceiling_iff)
```
```   469
```
```   470 lemma zero_less_ceiling [simp]: "0 < \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
```
```   471   by (simp add: less_ceiling_iff)
```
```   472
```
```   473 lemma one_less_ceiling [simp]: "1 < \<lceil>x\<rceil> \<longleftrightarrow> 1 < x"
```
```   474   by (simp add: less_ceiling_iff)
```
```   475
```
```   476 lemma numeral_less_ceiling [simp]:
```
```   477   "numeral v < \<lceil>x\<rceil> \<longleftrightarrow> numeral v < x"
```
```   478   by (simp add: less_ceiling_iff)
```
```   479
```
```   480 lemma neg_numeral_less_ceiling [simp]:
```
```   481   "- numeral v < \<lceil>x\<rceil> \<longleftrightarrow> - numeral v < x"
```
```   482   by (simp add: less_ceiling_iff)
```
```   483
```
```   484 lemma ceiling_altdef: "\<lceil>x\<rceil> = (if x = of_int \<lfloor>x\<rfloor> then \<lfloor>x\<rfloor> else \<lfloor>x\<rfloor> + 1)"
```
```   485   by (intro ceiling_unique, (simp, linarith?)+)
```
```   486
```
```   487 lemma floor_le_ceiling [simp]: "\<lfloor>x\<rfloor> \<le> \<lceil>x\<rceil>"
```
```   488   by (simp add: ceiling_altdef)
```
```   489
```
```   490 text \<open>Addition and subtraction of integers\<close>
```
```   491
```
```   492 lemma ceiling_add_of_int [simp]: "\<lceil>x + of_int z\<rceil> = \<lceil>x\<rceil> + z"
```
```   493   using ceiling_correct [of x] by (simp add: ceiling_def)
```
```   494
```
```   495 lemma ceiling_add_numeral [simp]: "\<lceil>x + numeral v\<rceil> = \<lceil>x\<rceil> + numeral v"
```
```   496   using ceiling_add_of_int [of x "numeral v"] by simp
```
```   497
```
```   498 lemma ceiling_add_one [simp]: "\<lceil>x + 1\<rceil> = \<lceil>x\<rceil> + 1"
```
```   499   using ceiling_add_of_int [of x 1] by simp
```
```   500
```
```   501 lemma ceiling_diff_of_int [simp]: "\<lceil>x - of_int z\<rceil> = \<lceil>x\<rceil> - z"
```
```   502   using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
```
```   503
```
```   504 lemma ceiling_diff_numeral [simp]: "\<lceil>x - numeral v\<rceil> = \<lceil>x\<rceil> - numeral v"
```
```   505   using ceiling_diff_of_int [of x "numeral v"] by simp
```
```   506
```
```   507 lemma ceiling_diff_one [simp]: "\<lceil>x - 1\<rceil> = \<lceil>x\<rceil> - 1"
```
```   508   using ceiling_diff_of_int [of x 1] by simp
```
```   509
```
```   510 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)"
```
```   511   by (auto simp add: ceiling_unique ceiling_correct)
```
```   512
```
```   513 lemma ceiling_diff_floor_le_1: "\<lceil>x\<rceil> - \<lfloor>x\<rfloor> \<le> 1"
```
```   514 proof -
```
```   515   have "of_int \<lceil>x\<rceil> - 1 < x"
```
```   516     using ceiling_correct[of x] by simp
```
```   517   also have "x < of_int \<lfloor>x\<rfloor> + 1"
```
```   518     using floor_correct[of x] by simp_all
```
```   519   finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
```
```   520     by simp
```
```   521   then show ?thesis
```
```   522     unfolding of_int_less_iff by simp
```
```   523 qed
```
```   524
```
```   525 subsection \<open>Negation\<close>
```
```   526
```
```   527 lemma floor_minus: "\<lfloor>- x\<rfloor> = - \<lceil>x\<rceil>"
```
```   528   unfolding ceiling_def by simp
```
```   529
```
```   530 lemma ceiling_minus: "\<lceil>- x\<rceil> = - \<lfloor>x\<rfloor>"
```
```   531   unfolding ceiling_def by simp
```
```   532
```
```   533
```
```   534 subsection \<open>Frac Function\<close>
```
```   535
```
```   536 definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
```
```   537   "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
```
```   538
```
```   539 lemma frac_lt_1: "frac x < 1"
```
```   540   by  (simp add: frac_def) linarith
```
```   541
```
```   542 lemma frac_eq_0_iff [simp]: "frac x = 0 \<longleftrightarrow> x \<in> \<int>"
```
```   543   by (simp add: frac_def) (metis Ints_cases Ints_of_int floor_of_int )
```
```   544
```
```   545 lemma frac_ge_0 [simp]: "frac x \<ge> 0"
```
```   546   unfolding frac_def
```
```   547   by linarith
```
```   548
```
```   549 lemma frac_gt_0_iff [simp]: "frac x > 0 \<longleftrightarrow> x \<notin> \<int>"
```
```   550   by (metis frac_eq_0_iff frac_ge_0 le_less less_irrefl)
```
```   551
```
```   552 lemma frac_of_int [simp]: "frac (of_int z) = 0"
```
```   553   by (simp add: frac_def)
```
```   554
```
```   555 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)"
```
```   556 proof -
```
```   557   {assume "x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
```
```   558    then have "\<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>"
```
```   559      by (metis add.commute floor_unique le_floor_add le_floor_iff of_int_add)
```
```   560    }
```
```   561   moreover
```
```   562   { assume "\<not> x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
```
```   563     then have "\<lfloor>x + y\<rfloor> = 1 + (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>)"
```
```   564       apply (simp add: floor_unique_iff)
```
```   565       apply (auto simp add: algebra_simps)
```
```   566       by linarith
```
```   567   }
```
```   568   ultimately show ?thesis
```
```   569     by (auto simp add: frac_def algebra_simps)
```
```   570 qed
```
```   571
```
```   572 lemma frac_add: "frac (x + y) = (if frac x + frac y < 1 then frac x + frac y
```
```   573                                  else (frac x + frac y) - 1)"
```
```   574   by (simp add: frac_def floor_add)
```
```   575
```
```   576 lemma frac_unique_iff:
```
```   577   fixes x :: "'a::floor_ceiling"
```
```   578   shows  "frac x = a \<longleftrightarrow> x - a \<in> \<int> \<and> 0 \<le> a \<and> a < 1"
```
```   579   apply (auto simp: Ints_def frac_def algebra_simps floor_unique)
```
```   580   apply linarith+
```
```   581   done
```
```   582
```
```   583 lemma frac_eq: "(frac x) = x \<longleftrightarrow> 0 \<le> x \<and> x < 1"
```
```   584   by (simp add: frac_unique_iff)
```
```   585
```
```   586 lemma frac_neg:
```
```   587   fixes x :: "'a::floor_ceiling"
```
```   588   shows  "frac (-x) = (if x \<in> \<int> then 0 else 1 - frac x)"
```
```   589   apply (auto simp add: frac_unique_iff)
```
```   590   apply (simp add: frac_def)
```
```   591   by (meson frac_lt_1 less_iff_diff_less_0 not_le not_less_iff_gr_or_eq)
```
```   592
```
```   593
```
```   594 subsection \<open>Rounding to the nearest integer\<close>
```
```   595
```
```   596 definition round where "round x = \<lfloor>x + 1/2\<rfloor>"
```
```   597
```
```   598 lemma of_int_round_ge:     "of_int (round x) \<ge> x - 1/2"
```
```   599   and of_int_round_le:     "of_int (round x) \<le> x + 1/2"
```
```   600   and of_int_round_abs_le: "\<bar>of_int (round x) - x\<bar> \<le> 1/2"
```
```   601   and of_int_round_gt:     "of_int (round x) > x - 1/2"
```
```   602 proof -
```
```   603   from floor_correct[of "x + 1/2"] have "x + 1/2 < of_int (round x) + 1" by (simp add: round_def)
```
```   604   from add_strict_right_mono[OF this, of "-1"] show A: "of_int (round x) > x - 1/2" by simp
```
```   605   thus "of_int (round x) \<ge> x - 1/2" by simp
```
```   606   from floor_correct[of "x + 1/2"] show "of_int (round x) \<le> x + 1/2" by (simp add: round_def)
```
```   607   with A show "\<bar>of_int (round x) - x\<bar> \<le> 1/2" by linarith
```
```   608 qed
```
```   609
```
```   610 lemma round_of_int [simp]: "round (of_int n) = n"
```
```   611   unfolding round_def by (subst floor_unique_iff) force
```
```   612
```
```   613 lemma round_0 [simp]: "round 0 = 0"
```
```   614   using round_of_int[of 0] by simp
```
```   615
```
```   616 lemma round_1 [simp]: "round 1 = 1"
```
```   617   using round_of_int[of 1] by simp
```
```   618
```
```   619 lemma round_numeral [simp]: "round (numeral n) = numeral n"
```
```   620   using round_of_int[of "numeral n"] by simp
```
```   621
```
```   622 lemma round_neg_numeral [simp]: "round (-numeral n) = -numeral n"
```
```   623   using round_of_int[of "-numeral n"] by simp
```
```   624
```
```   625 lemma round_of_nat [simp]: "round (of_nat n) = of_nat n"
```
```   626   using round_of_int[of "int n"] by simp
```
```   627
```
```   628 lemma round_mono: "x \<le> y \<Longrightarrow> round x \<le> round y"
```
```   629   unfolding round_def by (intro floor_mono) simp
```
```   630
```
```   631 lemma round_unique: "of_int y > x - 1/2 \<Longrightarrow> of_int y \<le> x + 1/2 \<Longrightarrow> round x = y"
```
```   632 unfolding round_def
```
```   633 proof (rule floor_unique)
```
```   634   assume "x - 1 / 2 < of_int y"
```
```   635   from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1" by simp
```
```   636 qed
```
```   637
```
```   638 lemma round_altdef: "round x = (if frac x \<ge> 1/2 then \<lceil>x\<rceil> else \<lfloor>x\<rfloor>)"
```
```   639   by (cases "frac x \<ge> 1/2")
```
```   640      (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef, linarith?)+)[2])+
```
```   641
```
```   642 lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
```
```   643   unfolding round_def by (intro floor_mono) simp
```
```   644
```
```   645 lemma ceiling_ge_round: "\<lceil>x\<rceil> \<ge> round x" unfolding round_altdef by simp
```
```   646
```
```   647 lemma round_diff_minimal:
```
```   648   fixes z :: "'a :: floor_ceiling"
```
```   649   shows "\<bar>z - of_int (round z)\<bar> \<le> \<bar>z - of_int m\<bar>"
```
```   650 proof (cases "of_int m \<ge> z")
```
```   651   case True
```
```   652   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lceil>z\<rceil> - z\<bar>"
```
```   653     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
```
```   654   also have "of_int \<lceil>z\<rceil> - z \<ge> 0" by linarith
```
```   655   with True have "\<bar>of_int \<lceil>z\<rceil> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
```
```   656     by (simp add: ceiling_le_iff)
```
```   657   finally show ?thesis .
```
```   658 next
```
```   659   case False
```
```   660   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lfloor>z\<rfloor> - z\<bar>"
```
```   661     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
```
```   662   also have "z - of_int \<lfloor>z\<rfloor> \<ge> 0" by linarith
```
```   663   with False have "\<bar>of_int \<lfloor>z\<rfloor> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
```
```   664     by (simp add: le_floor_iff)
```
```   665   finally show ?thesis .
```
```   666 qed
```
```   667
```
```   668 end
```