src/HOL/Archimedean_Field.thy
changeset 61942 f02b26f7d39d
parent 61738 c4f6031f1310
child 61944 5d06ecfdb472
equal deleted inserted replaced
61941:31f2105521ee 61942:f02b26f7d39d
   136 
   136 
   137 
   137 
   138 subsection \<open>Floor function\<close>
   138 subsection \<open>Floor function\<close>
   139 
   139 
   140 class floor_ceiling = archimedean_field +
   140 class floor_ceiling = archimedean_field +
   141   fixes floor :: "'a \<Rightarrow> int"
   141   fixes floor :: "'a \<Rightarrow> int"  ("\<lfloor>_\<rfloor>")
   142   assumes floor_correct: "of_int (floor x) \<le> x \<and> x < of_int (floor x + 1)"
   142   assumes floor_correct: "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)"
   143 
   143 
   144 notation (xsymbols)
   144 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> \<lfloor>x\<rfloor> = z"
   145   floor  ("\<lfloor>_\<rfloor>")
       
   146 
       
   147 lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> floor x = z"
       
   148   using floor_correct [of x] floor_exists1 [of x] by auto
   145   using floor_correct [of x] floor_exists1 [of x] by auto
   149 
   146 
   150 lemma floor_unique_iff:
   147 lemma floor_unique_iff:
   151   fixes x :: "'a::floor_ceiling"
   148   fixes x :: "'a::floor_ceiling"
   152   shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
   149   shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
   153 using floor_correct floor_unique by auto
   150 using floor_correct floor_unique by auto
   154 
   151 
   155 lemma of_int_floor_le [simp]: "of_int (floor x) \<le> x"
   152 lemma of_int_floor_le [simp]: "of_int \<lfloor>x\<rfloor> \<le> x"
   156   using floor_correct ..
   153   using floor_correct ..
   157 
   154 
   158 lemma le_floor_iff: "z \<le> floor x \<longleftrightarrow> of_int z \<le> x"
   155 lemma le_floor_iff: "z \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z \<le> x"
   159 proof
   156 proof
   160   assume "z \<le> floor x"
   157   assume "z \<le> \<lfloor>x\<rfloor>"
   161   then have "(of_int z :: 'a) \<le> of_int (floor x)" by simp
   158   then have "(of_int z :: 'a) \<le> of_int \<lfloor>x\<rfloor>" by simp
   162   also have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
   159   also have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
   163   finally show "of_int z \<le> x" .
   160   finally show "of_int z \<le> x" .
   164 next
   161 next
   165   assume "of_int z \<le> x"
   162   assume "of_int z \<le> x"
   166   also have "x < of_int (floor x + 1)" using floor_correct ..
   163   also have "x < of_int (\<lfloor>x\<rfloor> + 1)" using floor_correct ..
   167   finally show "z \<le> floor x" by (simp del: of_int_add)
   164   finally show "z \<le> \<lfloor>x\<rfloor>" by (simp del: of_int_add)
   168 qed
   165 qed
   169 
   166 
   170 lemma floor_less_iff: "floor x < z \<longleftrightarrow> x < of_int z"
   167 lemma floor_less_iff: "\<lfloor>x\<rfloor> < z \<longleftrightarrow> x < of_int z"
   171   by (simp add: not_le [symmetric] le_floor_iff)
   168   by (simp add: not_le [symmetric] le_floor_iff)
   172 
   169 
   173 lemma less_floor_iff: "z < floor x \<longleftrightarrow> of_int z + 1 \<le> x"
   170 lemma less_floor_iff: "z < \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z + 1 \<le> x"
   174   using le_floor_iff [of "z + 1" x] by auto
   171   using le_floor_iff [of "z + 1" x] by auto
   175 
   172 
   176 lemma floor_le_iff: "floor x \<le> z \<longleftrightarrow> x < of_int z + 1"
   173 lemma floor_le_iff: "\<lfloor>x\<rfloor> \<le> z \<longleftrightarrow> x < of_int z + 1"
   177   by (simp add: not_less [symmetric] less_floor_iff)
   174   by (simp add: not_less [symmetric] less_floor_iff)
   178 
   175 
   179 lemma floor_split[arith_split]: "P (floor t) \<longleftrightarrow> (\<forall>i. of_int i \<le> t \<and> t < of_int i + 1 \<longrightarrow> P i)"
   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)"
   180   by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
   177   by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
   181 
   178 
   182 lemma floor_mono: assumes "x \<le> y" shows "floor x \<le> floor y"
   179 lemma floor_mono:
   183 proof -
   180   assumes "x \<le> y"
   184   have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
   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)
   185   also note \<open>x \<le> y\<close>
   184   also note \<open>x \<le> y\<close>
   186   finally show ?thesis by (simp add: le_floor_iff)
   185   finally show ?thesis by (simp add: le_floor_iff)
   187 qed
   186 qed
   188 
   187 
   189 lemma floor_less_cancel: "floor x < floor y \<Longrightarrow> x < y"
   188 lemma floor_less_cancel: "\<lfloor>x\<rfloor> < \<lfloor>y\<rfloor> \<Longrightarrow> x < y"
   190   by (auto simp add: not_le [symmetric] floor_mono)
   189   by (auto simp add: not_le [symmetric] floor_mono)
   191 
   190 
   192 lemma floor_of_int [simp]: "floor (of_int z) = z"
   191 lemma floor_of_int [simp]: "\<lfloor>of_int z\<rfloor> = z"
   193   by (rule floor_unique) simp_all
   192   by (rule floor_unique) simp_all
   194 
   193 
   195 lemma floor_of_nat [simp]: "floor (of_nat n) = int n"
   194 lemma floor_of_nat [simp]: "\<lfloor>of_nat n\<rfloor> = int n"
   196   using floor_of_int [of "of_nat n"] by simp
   195   using floor_of_int [of "of_nat n"] by simp
   197 
   196 
   198 lemma le_floor_add: "floor x + floor y \<le> floor (x + y)"
   197 lemma le_floor_add: "\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor> \<le> \<lfloor>x + y\<rfloor>"
   199   by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
   198   by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
   200 
   199 
   201 text \<open>Floor with numerals\<close>
   200 text \<open>Floor with numerals\<close>
   202 
   201 
   203 lemma floor_zero [simp]: "floor 0 = 0"
   202 lemma floor_zero [simp]: "\<lfloor>0\<rfloor> = 0"
   204   using floor_of_int [of 0] by simp
   203   using floor_of_int [of 0] by simp
   205 
   204 
   206 lemma floor_one [simp]: "floor 1 = 1"
   205 lemma floor_one [simp]: "\<lfloor>1\<rfloor> = 1"
   207   using floor_of_int [of 1] by simp
   206   using floor_of_int [of 1] by simp
   208 
   207 
   209 lemma floor_numeral [simp]: "floor (numeral v) = numeral v"
   208 lemma floor_numeral [simp]: "\<lfloor>numeral v\<rfloor> = numeral v"
   210   using floor_of_int [of "numeral v"] by simp
   209   using floor_of_int [of "numeral v"] by simp
   211 
   210 
   212 lemma floor_neg_numeral [simp]: "floor (- numeral v) = - numeral v"
   211 lemma floor_neg_numeral [simp]: "\<lfloor>- numeral v\<rfloor> = - numeral v"
   213   using floor_of_int [of "- numeral v"] by simp
   212   using floor_of_int [of "- numeral v"] by simp
   214 
   213 
   215 lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
   214 lemma zero_le_floor [simp]: "0 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 0 \<le> x"
   216   by (simp add: le_floor_iff)
   215   by (simp add: le_floor_iff)
   217 
   216 
   218 lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
   217 lemma one_le_floor [simp]: "1 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
   219   by (simp add: le_floor_iff)
   218   by (simp add: le_floor_iff)
   220 
   219 
   221 lemma numeral_le_floor [simp]:
   220 lemma numeral_le_floor [simp]:
   222   "numeral v \<le> floor x \<longleftrightarrow> numeral v \<le> x"
   221   "numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v \<le> x"
   223   by (simp add: le_floor_iff)
   222   by (simp add: le_floor_iff)
   224 
   223 
   225 lemma neg_numeral_le_floor [simp]:
   224 lemma neg_numeral_le_floor [simp]:
   226   "- numeral v \<le> floor x \<longleftrightarrow> - numeral v \<le> x"
   225   "- numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v \<le> x"
   227   by (simp add: le_floor_iff)
   226   by (simp add: le_floor_iff)
   228 
   227 
   229 lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
   228 lemma zero_less_floor [simp]: "0 < \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
   230   by (simp add: less_floor_iff)
   229   by (simp add: less_floor_iff)
   231 
   230 
   232 lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
   231 lemma one_less_floor [simp]: "1 < \<lfloor>x\<rfloor> \<longleftrightarrow> 2 \<le> x"
   233   by (simp add: less_floor_iff)
   232   by (simp add: less_floor_iff)
   234 
   233 
   235 lemma numeral_less_floor [simp]:
   234 lemma numeral_less_floor [simp]:
   236   "numeral v < floor x \<longleftrightarrow> numeral v + 1 \<le> x"
   235   "numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v + 1 \<le> x"
   237   by (simp add: less_floor_iff)
   236   by (simp add: less_floor_iff)
   238 
   237 
   239 lemma neg_numeral_less_floor [simp]:
   238 lemma neg_numeral_less_floor [simp]:
   240   "- numeral v < floor x \<longleftrightarrow> - numeral v + 1 \<le> x"
   239   "- numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v + 1 \<le> x"
   241   by (simp add: less_floor_iff)
   240   by (simp add: less_floor_iff)
   242 
   241 
   243 lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
   242 lemma floor_le_zero [simp]: "\<lfloor>x\<rfloor> \<le> 0 \<longleftrightarrow> x < 1"
   244   by (simp add: floor_le_iff)
   243   by (simp add: floor_le_iff)
   245 
   244 
   246 lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
   245 lemma floor_le_one [simp]: "\<lfloor>x\<rfloor> \<le> 1 \<longleftrightarrow> x < 2"
   247   by (simp add: floor_le_iff)
   246   by (simp add: floor_le_iff)
   248 
   247 
   249 lemma floor_le_numeral [simp]:
   248 lemma floor_le_numeral [simp]:
   250   "floor x \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
   249   "\<lfloor>x\<rfloor> \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
   251   by (simp add: floor_le_iff)
   250   by (simp add: floor_le_iff)
   252 
   251 
   253 lemma floor_le_neg_numeral [simp]:
   252 lemma floor_le_neg_numeral [simp]:
   254   "floor x \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
   253   "\<lfloor>x\<rfloor> \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
   255   by (simp add: floor_le_iff)
   254   by (simp add: floor_le_iff)
   256 
   255 
   257 lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
   256 lemma floor_less_zero [simp]: "\<lfloor>x\<rfloor> < 0 \<longleftrightarrow> x < 0"
   258   by (simp add: floor_less_iff)
   257   by (simp add: floor_less_iff)
   259 
   258 
   260 lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
   259 lemma floor_less_one [simp]: "\<lfloor>x\<rfloor> < 1 \<longleftrightarrow> x < 1"
   261   by (simp add: floor_less_iff)
   260   by (simp add: floor_less_iff)
   262 
   261 
   263 lemma floor_less_numeral [simp]:
   262 lemma floor_less_numeral [simp]:
   264   "floor x < numeral v \<longleftrightarrow> x < numeral v"
   263   "\<lfloor>x\<rfloor> < numeral v \<longleftrightarrow> x < numeral v"
   265   by (simp add: floor_less_iff)
   264   by (simp add: floor_less_iff)
   266 
   265 
   267 lemma floor_less_neg_numeral [simp]:
   266 lemma floor_less_neg_numeral [simp]:
   268   "floor x < - numeral v \<longleftrightarrow> x < - numeral v"
   267   "\<lfloor>x\<rfloor> < - numeral v \<longleftrightarrow> x < - numeral v"
   269   by (simp add: floor_less_iff)
   268   by (simp add: floor_less_iff)
   270 
   269 
   271 text \<open>Addition and subtraction of integers\<close>
   270 text \<open>Addition and subtraction of integers\<close>
   272 
   271 
   273 lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
   272 lemma floor_add_of_int [simp]: "\<lfloor>x + of_int z\<rfloor> = \<lfloor>x\<rfloor> + z"
   274   using floor_correct [of x] by (simp add: floor_unique)
   273   using floor_correct [of x] by (simp add: floor_unique)
   275 
   274 
   276 lemma floor_add_numeral [simp]:
   275 lemma floor_add_numeral [simp]:
   277     "floor (x + numeral v) = floor x + numeral v"
   276     "\<lfloor>x + numeral v\<rfloor> = \<lfloor>x\<rfloor> + numeral v"
   278   using floor_add_of_int [of x "numeral v"] by simp
   277   using floor_add_of_int [of x "numeral v"] by simp
   279 
   278 
   280 lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
   279 lemma floor_add_one [simp]: "\<lfloor>x + 1\<rfloor> = \<lfloor>x\<rfloor> + 1"
   281   using floor_add_of_int [of x 1] by simp
   280   using floor_add_of_int [of x 1] by simp
   282 
   281 
   283 lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
   282 lemma floor_diff_of_int [simp]: "\<lfloor>x - of_int z\<rfloor> = \<lfloor>x\<rfloor> - z"
   284   using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
   283   using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
   285 
   284 
   286 lemma floor_uminus_of_int [simp]: "floor (- (of_int z)) = - z"
   285 lemma floor_uminus_of_int [simp]: "\<lfloor>- (of_int z)\<rfloor> = - z"
   287   by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
   286   by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
   288 
   287 
   289 lemma floor_diff_numeral [simp]:
   288 lemma floor_diff_numeral [simp]:
   290   "floor (x - numeral v) = floor x - numeral v"
   289   "\<lfloor>x - numeral v\<rfloor> = \<lfloor>x\<rfloor> - numeral v"
   291   using floor_diff_of_int [of x "numeral v"] by simp
   290   using floor_diff_of_int [of x "numeral v"] by simp
   292 
   291 
   293 lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
   292 lemma floor_diff_one [simp]: "\<lfloor>x - 1\<rfloor> = \<lfloor>x\<rfloor> - 1"
   294   using floor_diff_of_int [of x 1] by simp
   293   using floor_diff_of_int [of x 1] by simp
   295 
   294 
   296 lemma le_mult_floor:
   295 lemma le_mult_floor:
   297   assumes "0 \<le> a" and "0 \<le> b"
   296   assumes "0 \<le> a" and "0 \<le> b"
   298   shows "floor a * floor b \<le> floor (a * b)"
   297   shows "\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor> \<le> \<lfloor>a * b\<rfloor>"
   299 proof -
   298 proof -
   300   have "of_int (floor a) \<le> a"
   299   have "of_int \<lfloor>a\<rfloor> \<le> a"
   301     and "of_int (floor b) \<le> b" by (auto intro: of_int_floor_le)
   300     and "of_int \<lfloor>b\<rfloor> \<le> b" by (auto intro: of_int_floor_le)
   302   hence "of_int (floor a * floor b) \<le> a * b"
   301   hence "of_int (\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor>) \<le> a * b"
   303     using assms by (auto intro!: mult_mono)
   302     using assms by (auto intro!: mult_mono)
   304   also have "a * b < of_int (floor (a * b) + 1)"  
   303   also have "a * b < of_int (\<lfloor>a * b\<rfloor> + 1)"
   305     using floor_correct[of "a * b"] by auto
   304     using floor_correct[of "a * b"] by auto
   306   finally show ?thesis unfolding of_int_less_iff by simp
   305   finally show ?thesis unfolding of_int_less_iff by simp
   307 qed
   306 qed
   308 
   307 
   309 lemma floor_divide_of_int_eq:
   308 lemma floor_divide_of_int_eq:
   371 qed
   370 qed
   372 
   371 
   373 
   372 
   374 subsection \<open>Ceiling function\<close>
   373 subsection \<open>Ceiling function\<close>
   375 
   374 
   376 definition
   375 definition ceiling :: "'a::floor_ceiling \<Rightarrow> int"  ("\<lceil>_\<rceil>")
   377   ceiling :: "'a::floor_ceiling \<Rightarrow> int" where
   376   where "\<lceil>x\<rceil> = - \<lfloor>- x\<rfloor>"
   378   "ceiling x = - floor (- x)"
   377 
   379 
   378 lemma ceiling_correct: "of_int \<lceil>x\<rceil> - 1 < x \<and> x \<le> of_int \<lceil>x\<rceil>"
   380 notation (xsymbols)
       
   381   ceiling  ("\<lceil>_\<rceil>")
       
   382 
       
   383 lemma ceiling_correct: "of_int (ceiling x) - 1 < x \<and> x \<le> of_int (ceiling x)"
       
   384   unfolding ceiling_def using floor_correct [of "- x"] by (simp add: le_minus_iff) 
   379   unfolding ceiling_def using floor_correct [of "- x"] by (simp add: le_minus_iff) 
   385 
   380 
   386 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> ceiling x = z"
   381 lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> \<lceil>x\<rceil> = z"
   387   unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
   382   unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
   388 
   383 
   389 lemma le_of_int_ceiling [simp]: "x \<le> of_int (ceiling x)"
   384 lemma le_of_int_ceiling [simp]: "x \<le> of_int \<lceil>x\<rceil>"
   390   using ceiling_correct ..
   385   using ceiling_correct ..
   391 
   386 
   392 lemma ceiling_le_iff: "ceiling x \<le> z \<longleftrightarrow> x \<le> of_int z"
   387 lemma ceiling_le_iff: "\<lceil>x\<rceil> \<le> z \<longleftrightarrow> x \<le> of_int z"
   393   unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
   388   unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
   394 
   389 
   395 lemma less_ceiling_iff: "z < ceiling x \<longleftrightarrow> of_int z < x"
   390 lemma less_ceiling_iff: "z < \<lceil>x\<rceil> \<longleftrightarrow> of_int z < x"
   396   by (simp add: not_le [symmetric] ceiling_le_iff)
   391   by (simp add: not_le [symmetric] ceiling_le_iff)
   397 
   392 
   398 lemma ceiling_less_iff: "ceiling x < z \<longleftrightarrow> x \<le> of_int z - 1"
   393 lemma ceiling_less_iff: "\<lceil>x\<rceil> < z \<longleftrightarrow> x \<le> of_int z - 1"
   399   using ceiling_le_iff [of x "z - 1"] by simp
   394   using ceiling_le_iff [of x "z - 1"] by simp
   400 
   395 
   401 lemma le_ceiling_iff: "z \<le> ceiling x \<longleftrightarrow> of_int z - 1 < x"
   396 lemma le_ceiling_iff: "z \<le> \<lceil>x\<rceil> \<longleftrightarrow> of_int z - 1 < x"
   402   by (simp add: not_less [symmetric] ceiling_less_iff)
   397   by (simp add: not_less [symmetric] ceiling_less_iff)
   403 
   398 
   404 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> ceiling x \<ge> ceiling y"
   399 lemma ceiling_mono: "x \<ge> y \<Longrightarrow> \<lceil>x\<rceil> \<ge> \<lceil>y\<rceil>"
   405   unfolding ceiling_def by (simp add: floor_mono)
   400   unfolding ceiling_def by (simp add: floor_mono)
   406 
   401 
   407 lemma ceiling_less_cancel: "ceiling x < ceiling y \<Longrightarrow> x < y"
   402 lemma ceiling_less_cancel: "\<lceil>x\<rceil> < \<lceil>y\<rceil> \<Longrightarrow> x < y"
   408   by (auto simp add: not_le [symmetric] ceiling_mono)
   403   by (auto simp add: not_le [symmetric] ceiling_mono)
   409 
   404 
   410 lemma ceiling_of_int [simp]: "ceiling (of_int z) = z"
   405 lemma ceiling_of_int [simp]: "\<lceil>of_int z\<rceil> = z"
   411   by (rule ceiling_unique) simp_all
   406   by (rule ceiling_unique) simp_all
   412 
   407 
   413 lemma ceiling_of_nat [simp]: "ceiling (of_nat n) = int n"
   408 lemma ceiling_of_nat [simp]: "\<lceil>of_nat n\<rceil> = int n"
   414   using ceiling_of_int [of "of_nat n"] by simp
   409   using ceiling_of_int [of "of_nat n"] by simp
   415 
   410 
   416 lemma ceiling_add_le: "ceiling (x + y) \<le> ceiling x + ceiling y"
   411 lemma ceiling_add_le: "\<lceil>x + y\<rceil> \<le> \<lceil>x\<rceil> + \<lceil>y\<rceil>"
   417   by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
   412   by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
   418 
   413 
   419 text \<open>Ceiling with numerals\<close>
   414 text \<open>Ceiling with numerals\<close>
   420 
   415 
   421 lemma ceiling_zero [simp]: "ceiling 0 = 0"
   416 lemma ceiling_zero [simp]: "\<lceil>0\<rceil> = 0"
   422   using ceiling_of_int [of 0] by simp
   417   using ceiling_of_int [of 0] by simp
   423 
   418 
   424 lemma ceiling_one [simp]: "ceiling 1 = 1"
   419 lemma ceiling_one [simp]: "\<lceil>1\<rceil> = 1"
   425   using ceiling_of_int [of 1] by simp
   420   using ceiling_of_int [of 1] by simp
   426 
   421 
   427 lemma ceiling_numeral [simp]: "ceiling (numeral v) = numeral v"
   422 lemma ceiling_numeral [simp]: "\<lceil>numeral v\<rceil> = numeral v"
   428   using ceiling_of_int [of "numeral v"] by simp
   423   using ceiling_of_int [of "numeral v"] by simp
   429 
   424 
   430 lemma ceiling_neg_numeral [simp]: "ceiling (- numeral v) = - numeral v"
   425 lemma ceiling_neg_numeral [simp]: "\<lceil>- numeral v\<rceil> = - numeral v"
   431   using ceiling_of_int [of "- numeral v"] by simp
   426   using ceiling_of_int [of "- numeral v"] by simp
   432 
   427 
   433 lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
   428 lemma ceiling_le_zero [simp]: "\<lceil>x\<rceil> \<le> 0 \<longleftrightarrow> x \<le> 0"
   434   by (simp add: ceiling_le_iff)
   429   by (simp add: ceiling_le_iff)
   435 
   430 
   436 lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
   431 lemma ceiling_le_one [simp]: "\<lceil>x\<rceil> \<le> 1 \<longleftrightarrow> x \<le> 1"
   437   by (simp add: ceiling_le_iff)
   432   by (simp add: ceiling_le_iff)
   438 
   433 
   439 lemma ceiling_le_numeral [simp]:
   434 lemma ceiling_le_numeral [simp]:
   440   "ceiling x \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
   435   "\<lceil>x\<rceil> \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
   441   by (simp add: ceiling_le_iff)
   436   by (simp add: ceiling_le_iff)
   442 
   437 
   443 lemma ceiling_le_neg_numeral [simp]:
   438 lemma ceiling_le_neg_numeral [simp]:
   444   "ceiling x \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
   439   "\<lceil>x\<rceil> \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
   445   by (simp add: ceiling_le_iff)
   440   by (simp add: ceiling_le_iff)
   446 
   441 
   447 lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
   442 lemma ceiling_less_zero [simp]: "\<lceil>x\<rceil> < 0 \<longleftrightarrow> x \<le> -1"
   448   by (simp add: ceiling_less_iff)
   443   by (simp add: ceiling_less_iff)
   449 
   444 
   450 lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
   445 lemma ceiling_less_one [simp]: "\<lceil>x\<rceil> < 1 \<longleftrightarrow> x \<le> 0"
   451   by (simp add: ceiling_less_iff)
   446   by (simp add: ceiling_less_iff)
   452 
   447 
   453 lemma ceiling_less_numeral [simp]:
   448 lemma ceiling_less_numeral [simp]:
   454   "ceiling x < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
   449   "\<lceil>x\<rceil> < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
   455   by (simp add: ceiling_less_iff)
   450   by (simp add: ceiling_less_iff)
   456 
   451 
   457 lemma ceiling_less_neg_numeral [simp]:
   452 lemma ceiling_less_neg_numeral [simp]:
   458   "ceiling x < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
   453   "\<lceil>x\<rceil> < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
   459   by (simp add: ceiling_less_iff)
   454   by (simp add: ceiling_less_iff)
   460 
   455 
   461 lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
   456 lemma zero_le_ceiling [simp]: "0 \<le> \<lceil>x\<rceil> \<longleftrightarrow> -1 < x"
   462   by (simp add: le_ceiling_iff)
   457   by (simp add: le_ceiling_iff)
   463 
   458 
   464 lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
   459 lemma one_le_ceiling [simp]: "1 \<le> \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
   465   by (simp add: le_ceiling_iff)
   460   by (simp add: le_ceiling_iff)
   466 
   461 
   467 lemma numeral_le_ceiling [simp]:
   462 lemma numeral_le_ceiling [simp]:
   468   "numeral v \<le> ceiling x \<longleftrightarrow> numeral v - 1 < x"
   463   "numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> numeral v - 1 < x"
   469   by (simp add: le_ceiling_iff)
   464   by (simp add: le_ceiling_iff)
   470 
   465 
   471 lemma neg_numeral_le_ceiling [simp]:
   466 lemma neg_numeral_le_ceiling [simp]:
   472   "- numeral v \<le> ceiling x \<longleftrightarrow> - numeral v - 1 < x"
   467   "- numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> - numeral v - 1 < x"
   473   by (simp add: le_ceiling_iff)
   468   by (simp add: le_ceiling_iff)
   474 
   469 
   475 lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
   470 lemma zero_less_ceiling [simp]: "0 < \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
   476   by (simp add: less_ceiling_iff)
   471   by (simp add: less_ceiling_iff)
   477 
   472 
   478 lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
   473 lemma one_less_ceiling [simp]: "1 < \<lceil>x\<rceil> \<longleftrightarrow> 1 < x"
   479   by (simp add: less_ceiling_iff)
   474   by (simp add: less_ceiling_iff)
   480 
   475 
   481 lemma numeral_less_ceiling [simp]:
   476 lemma numeral_less_ceiling [simp]:
   482   "numeral v < ceiling x \<longleftrightarrow> numeral v < x"
   477   "numeral v < \<lceil>x\<rceil> \<longleftrightarrow> numeral v < x"
   483   by (simp add: less_ceiling_iff)
   478   by (simp add: less_ceiling_iff)
   484 
   479 
   485 lemma neg_numeral_less_ceiling [simp]:
   480 lemma neg_numeral_less_ceiling [simp]:
   486   "- numeral v < ceiling x \<longleftrightarrow> - numeral v < x"
   481   "- numeral v < \<lceil>x\<rceil> \<longleftrightarrow> - numeral v < x"
   487   by (simp add: less_ceiling_iff)
   482   by (simp add: less_ceiling_iff)
   488 
   483 
   489 lemma ceiling_altdef: "ceiling x = (if x = of_int (floor x) then floor x else floor x + 1)"
   484 lemma ceiling_altdef: "\<lceil>x\<rceil> = (if x = of_int \<lfloor>x\<rfloor> then \<lfloor>x\<rfloor> else \<lfloor>x\<rfloor> + 1)"
   490   by (intro ceiling_unique, (simp, linarith?)+)
   485   by (intro ceiling_unique, (simp, linarith?)+)
   491 
   486 
   492 lemma floor_le_ceiling [simp]: "floor x \<le> ceiling x" by (simp add: ceiling_altdef)
   487 lemma floor_le_ceiling [simp]: "\<lfloor>x\<rfloor> \<le> \<lceil>x\<rceil>"
       
   488   by (simp add: ceiling_altdef)
   493 
   489 
   494 text \<open>Addition and subtraction of integers\<close>
   490 text \<open>Addition and subtraction of integers\<close>
   495 
   491 
   496 lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
   492 lemma ceiling_add_of_int [simp]: "\<lceil>x + of_int z\<rceil> = \<lceil>x\<rceil> + z"
   497   using ceiling_correct [of x] by (simp add: ceiling_def)
   493   using ceiling_correct [of x] by (simp add: ceiling_def)
   498 
   494 
   499 lemma ceiling_add_numeral [simp]:
   495 lemma ceiling_add_numeral [simp]: "\<lceil>x + numeral v\<rceil> = \<lceil>x\<rceil> + numeral v"
   500     "ceiling (x + numeral v) = ceiling x + numeral v"
       
   501   using ceiling_add_of_int [of x "numeral v"] by simp
   496   using ceiling_add_of_int [of x "numeral v"] by simp
   502 
   497 
   503 lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
   498 lemma ceiling_add_one [simp]: "\<lceil>x + 1\<rceil> = \<lceil>x\<rceil> + 1"
   504   using ceiling_add_of_int [of x 1] by simp
   499   using ceiling_add_of_int [of x 1] by simp
   505 
   500 
   506 lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
   501 lemma ceiling_diff_of_int [simp]: "\<lceil>x - of_int z\<rceil> = \<lceil>x\<rceil> - z"
   507   using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
   502   using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
   508 
   503 
   509 lemma ceiling_diff_numeral [simp]:
   504 lemma ceiling_diff_numeral [simp]: "\<lceil>x - numeral v\<rceil> = \<lceil>x\<rceil> - numeral v"
   510   "ceiling (x - numeral v) = ceiling x - numeral v"
       
   511   using ceiling_diff_of_int [of x "numeral v"] by simp
   505   using ceiling_diff_of_int [of x "numeral v"] by simp
   512 
   506 
   513 lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
   507 lemma ceiling_diff_one [simp]: "\<lceil>x - 1\<rceil> = \<lceil>x\<rceil> - 1"
   514   using ceiling_diff_of_int [of x 1] by simp
   508   using ceiling_diff_of_int [of x 1] by simp
   515 
   509 
   516 lemma ceiling_split[arith_split]: "P (ceiling t) \<longleftrightarrow> (\<forall>i. of_int i - 1 < t \<and> t \<le> of_int i \<longrightarrow> P i)"
   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)"
   517   by (auto simp add: ceiling_unique ceiling_correct)
   511   by (auto simp add: ceiling_unique ceiling_correct)
   518 
   512 
   519 lemma ceiling_diff_floor_le_1: "ceiling x - floor x \<le> 1"
   513 lemma ceiling_diff_floor_le_1: "\<lceil>x\<rceil> - \<lfloor>x\<rfloor> \<le> 1"
   520 proof -
   514 proof -
   521   have "of_int \<lceil>x\<rceil> - 1 < x" 
   515   have "of_int \<lceil>x\<rceil> - 1 < x" 
   522     using ceiling_correct[of x] by simp
   516     using ceiling_correct[of x] by simp
   523   also have "x < of_int \<lfloor>x\<rfloor> + 1"
   517   also have "x < of_int \<lfloor>x\<rfloor> + 1"
   524     using floor_correct[of x] by simp_all
   518     using floor_correct[of x] by simp_all
   528     unfolding of_int_less_iff by simp
   522     unfolding of_int_less_iff by simp
   529 qed
   523 qed
   530 
   524 
   531 subsection \<open>Negation\<close>
   525 subsection \<open>Negation\<close>
   532 
   526 
   533 lemma floor_minus: "floor (- x) = - ceiling x"
   527 lemma floor_minus: "\<lfloor>- x\<rfloor> = - \<lceil>x\<rceil>"
   534   unfolding ceiling_def by simp
   528   unfolding ceiling_def by simp
   535 
   529 
   536 lemma ceiling_minus: "ceiling (- x) = - floor x"
   530 lemma ceiling_minus: "\<lceil>- x\<rceil> = - \<lfloor>x\<rfloor>"
   537   unfolding ceiling_def by simp
   531   unfolding ceiling_def by simp
   538 
   532 
       
   533 
   539 subsection \<open>Frac Function\<close>
   534 subsection \<open>Frac Function\<close>
   540 
       
   541 
   535 
   542 definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
   536 definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
   543   "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
   537   "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
   544 
   538 
   545 lemma frac_lt_1: "frac x < 1"
   539 lemma frac_lt_1: "frac x < 1"
   640 proof (rule floor_unique)
   634 proof (rule floor_unique)
   641   assume "x - 1 / 2 < of_int y"
   635   assume "x - 1 / 2 < of_int y"
   642   from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1" by simp
   636   from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1" by simp
   643 qed
   637 qed
   644 
   638 
   645 lemma round_altdef: "round x = (if frac x \<ge> 1/2 then ceiling x else floor x)"
   639 lemma round_altdef: "round x = (if frac x \<ge> 1/2 then \<lceil>x\<rceil> else \<lfloor>x\<rfloor>)"
   646   by (cases "frac x \<ge> 1/2")
   640   by (cases "frac x \<ge> 1/2")
   647      (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef, linarith?)+)[2])+
   641      (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef, linarith?)+)[2])+
   648 
   642 
   649 lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
   643 lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
   650   unfolding round_def by (intro floor_mono) simp
   644   unfolding round_def by (intro floor_mono) simp
   654 lemma round_diff_minimal: 
   648 lemma round_diff_minimal: 
   655   fixes z :: "'a :: floor_ceiling"
   649   fixes z :: "'a :: floor_ceiling"
   656   shows "abs (z - of_int (round z)) \<le> abs (z - of_int m)"
   650   shows "abs (z - of_int (round z)) \<le> abs (z - of_int m)"
   657 proof (cases "of_int m \<ge> z")
   651 proof (cases "of_int m \<ge> z")
   658   case True
   652   case True
   659   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int (ceiling z) - z\<bar>"
   653   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lceil>z\<rceil> - z\<bar>"
   660     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
   654     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
   661   also have "of_int \<lceil>z\<rceil> - z \<ge> 0" by linarith
   655   also have "of_int \<lceil>z\<rceil> - z \<ge> 0" by linarith
   662   with True have "\<bar>of_int (ceiling z) - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   656   with True have "\<bar>of_int \<lceil>z\<rceil> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   663     by (simp add: ceiling_le_iff)
   657     by (simp add: ceiling_le_iff)
   664   finally show ?thesis .
   658   finally show ?thesis .
   665 next
   659 next
   666   case False
   660   case False
   667   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int (floor z) - z\<bar>"
   661   hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lfloor>z\<rfloor> - z\<bar>"
   668     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
   662     unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
   669   also have "z - of_int \<lfloor>z\<rfloor> \<ge> 0" by linarith
   663   also have "z - of_int \<lfloor>z\<rfloor> \<ge> 0" by linarith
   670   with False have "\<bar>of_int (floor z) - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   664   with False have "\<bar>of_int \<lfloor>z\<rfloor> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
   671     by (simp add: le_floor_iff)
   665     by (simp add: le_floor_iff)
   672   finally show ?thesis .
   666   finally show ?thesis .
   673 qed
   667 qed
   674 
   668 
   675 end
   669 end