src/HOL/Archimedean_Field.thy
author wenzelm
Sun Dec 27 21:46:36 2015 +0100 (2015-12-27)
changeset 61942 f02b26f7d39d
parent 61738 c4f6031f1310
child 61944 5d06ecfdb472
permissions -rw-r--r--
prefer symbols for "floor", "ceiling";
wenzelm@41959
     1
(*  Title:      HOL/Archimedean_Field.thy
wenzelm@41959
     2
    Author:     Brian Huffman
huffman@30096
     3
*)
huffman@30096
     4
wenzelm@60758
     5
section \<open>Archimedean Fields, Floor and Ceiling Functions\<close>
huffman@30096
     6
huffman@30096
     7
theory Archimedean_Field
huffman@30096
     8
imports Main
huffman@30096
     9
begin
huffman@30096
    10
wenzelm@60758
    11
subsection \<open>Class of Archimedean fields\<close>
huffman@30096
    12
wenzelm@60758
    13
text \<open>Archimedean fields have no infinite elements.\<close>
huffman@30096
    14
huffman@47108
    15
class archimedean_field = linordered_field +
huffman@30096
    16
  assumes ex_le_of_int: "\<exists>z. x \<le> of_int z"
huffman@30096
    17
huffman@30096
    18
lemma ex_less_of_int:
huffman@30096
    19
  fixes x :: "'a::archimedean_field" shows "\<exists>z. x < of_int z"
huffman@30096
    20
proof -
huffman@30096
    21
  from ex_le_of_int obtain z where "x \<le> of_int z" ..
huffman@30096
    22
  then have "x < of_int (z + 1)" by simp
huffman@30096
    23
  then show ?thesis ..
huffman@30096
    24
qed
huffman@30096
    25
huffman@30096
    26
lemma ex_of_int_less:
huffman@30096
    27
  fixes x :: "'a::archimedean_field" shows "\<exists>z. of_int z < x"
huffman@30096
    28
proof -
huffman@30096
    29
  from ex_less_of_int obtain z where "- x < of_int z" ..
huffman@30096
    30
  then have "of_int (- z) < x" by simp
huffman@30096
    31
  then show ?thesis ..
huffman@30096
    32
qed
huffman@30096
    33
huffman@30096
    34
lemma ex_less_of_nat:
huffman@30096
    35
  fixes x :: "'a::archimedean_field" shows "\<exists>n. x < of_nat n"
huffman@30096
    36
proof -
huffman@30096
    37
  obtain z where "x < of_int z" using ex_less_of_int ..
huffman@30096
    38
  also have "\<dots> \<le> of_int (int (nat z))" by simp
huffman@30096
    39
  also have "\<dots> = of_nat (nat z)" by (simp only: of_int_of_nat_eq)
huffman@30096
    40
  finally show ?thesis ..
huffman@30096
    41
qed
huffman@30096
    42
huffman@30096
    43
lemma ex_le_of_nat:
huffman@30096
    44
  fixes x :: "'a::archimedean_field" shows "\<exists>n. x \<le> of_nat n"
huffman@30096
    45
proof -
huffman@30096
    46
  obtain n where "x < of_nat n" using ex_less_of_nat ..
huffman@30096
    47
  then have "x \<le> of_nat n" by simp
huffman@30096
    48
  then show ?thesis ..
huffman@30096
    49
qed
huffman@30096
    50
wenzelm@60758
    51
text \<open>Archimedean fields have no infinitesimal elements.\<close>
huffman@30096
    52
huffman@30096
    53
lemma ex_inverse_of_nat_Suc_less:
huffman@30096
    54
  fixes x :: "'a::archimedean_field"
huffman@30096
    55
  assumes "0 < x" shows "\<exists>n. inverse (of_nat (Suc n)) < x"
huffman@30096
    56
proof -
wenzelm@60758
    57
  from \<open>0 < x\<close> have "0 < inverse x"
huffman@30096
    58
    by (rule positive_imp_inverse_positive)
huffman@30096
    59
  obtain n where "inverse x < of_nat n"
huffman@30096
    60
    using ex_less_of_nat ..
huffman@30096
    61
  then obtain m where "inverse x < of_nat (Suc m)"
wenzelm@60758
    62
    using \<open>0 < inverse x\<close> by (cases n) (simp_all del: of_nat_Suc)
huffman@30096
    63
  then have "inverse (of_nat (Suc m)) < inverse (inverse x)"
wenzelm@60758
    64
    using \<open>0 < inverse x\<close> by (rule less_imp_inverse_less)
huffman@30096
    65
  then have "inverse (of_nat (Suc m)) < x"
wenzelm@60758
    66
    using \<open>0 < x\<close> by (simp add: nonzero_inverse_inverse_eq)
huffman@30096
    67
  then show ?thesis ..
huffman@30096
    68
qed
huffman@30096
    69
huffman@30096
    70
lemma ex_inverse_of_nat_less:
huffman@30096
    71
  fixes x :: "'a::archimedean_field"
huffman@30096
    72
  assumes "0 < x" shows "\<exists>n>0. inverse (of_nat n) < x"
wenzelm@60758
    73
  using ex_inverse_of_nat_Suc_less [OF \<open>0 < x\<close>] by auto
huffman@30096
    74
huffman@30096
    75
lemma ex_less_of_nat_mult:
huffman@30096
    76
  fixes x :: "'a::archimedean_field"
huffman@30096
    77
  assumes "0 < x" shows "\<exists>n. y < of_nat n * x"
huffman@30096
    78
proof -
huffman@30096
    79
  obtain n where "y / x < of_nat n" using ex_less_of_nat ..
wenzelm@60758
    80
  with \<open>0 < x\<close> have "y < of_nat n * x" by (simp add: pos_divide_less_eq)
huffman@30096
    81
  then show ?thesis ..
huffman@30096
    82
qed
huffman@30096
    83
huffman@30096
    84
wenzelm@60758
    85
subsection \<open>Existence and uniqueness of floor function\<close>
huffman@30096
    86
huffman@30096
    87
lemma exists_least_lemma:
huffman@30096
    88
  assumes "\<not> P 0" and "\<exists>n. P n"
huffman@30096
    89
  shows "\<exists>n. \<not> P n \<and> P (Suc n)"
huffman@30096
    90
proof -
wenzelm@60758
    91
  from \<open>\<exists>n. P n\<close> have "P (Least P)" by (rule LeastI_ex)
wenzelm@60758
    92
  with \<open>\<not> P 0\<close> obtain n where "Least P = Suc n"
huffman@30096
    93
    by (cases "Least P") auto
huffman@30096
    94
  then have "n < Least P" by simp
huffman@30096
    95
  then have "\<not> P n" by (rule not_less_Least)
huffman@30096
    96
  then have "\<not> P n \<and> P (Suc n)"
wenzelm@60758
    97
    using \<open>P (Least P)\<close> \<open>Least P = Suc n\<close> by simp
huffman@30096
    98
  then show ?thesis ..
huffman@30096
    99
qed
huffman@30096
   100
huffman@30096
   101
lemma floor_exists:
huffman@30096
   102
  fixes x :: "'a::archimedean_field"
huffman@30096
   103
  shows "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
huffman@30096
   104
proof (cases)
huffman@30096
   105
  assume "0 \<le> x"
huffman@30096
   106
  then have "\<not> x < of_nat 0" by simp
huffman@30096
   107
  then have "\<exists>n. \<not> x < of_nat n \<and> x < of_nat (Suc n)"
huffman@30096
   108
    using ex_less_of_nat by (rule exists_least_lemma)
huffman@30096
   109
  then obtain n where "\<not> x < of_nat n \<and> x < of_nat (Suc n)" ..
huffman@30096
   110
  then have "of_int (int n) \<le> x \<and> x < of_int (int n + 1)" by simp
huffman@30096
   111
  then show ?thesis ..
huffman@30096
   112
next
huffman@30096
   113
  assume "\<not> 0 \<le> x"
huffman@30096
   114
  then have "\<not> - x \<le> of_nat 0" by simp
huffman@30096
   115
  then have "\<exists>n. \<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)"
huffman@30096
   116
    using ex_le_of_nat by (rule exists_least_lemma)
huffman@30096
   117
  then obtain n where "\<not> - x \<le> of_nat n \<and> - x \<le> of_nat (Suc n)" ..
huffman@30096
   118
  then have "of_int (- int n - 1) \<le> x \<and> x < of_int (- int n - 1 + 1)" by simp
huffman@30096
   119
  then show ?thesis ..
huffman@30096
   120
qed
huffman@30096
   121
huffman@30096
   122
lemma floor_exists1:
huffman@30096
   123
  fixes x :: "'a::archimedean_field"
huffman@30096
   124
  shows "\<exists>!z. of_int z \<le> x \<and> x < of_int (z + 1)"
huffman@30096
   125
proof (rule ex_ex1I)
huffman@30096
   126
  show "\<exists>z. of_int z \<le> x \<and> x < of_int (z + 1)"
huffman@30096
   127
    by (rule floor_exists)
huffman@30096
   128
next
huffman@30096
   129
  fix y z assume
huffman@30096
   130
    "of_int y \<le> x \<and> x < of_int (y + 1)"
huffman@30096
   131
    "of_int z \<le> x \<and> x < of_int (z + 1)"
hoelzl@54281
   132
  with le_less_trans [of "of_int y" "x" "of_int (z + 1)"]
hoelzl@54281
   133
       le_less_trans [of "of_int z" "x" "of_int (y + 1)"]
huffman@30096
   134
  show "y = z" by (simp del: of_int_add)
huffman@30096
   135
qed
huffman@30096
   136
huffman@30096
   137
wenzelm@60758
   138
subsection \<open>Floor function\<close>
huffman@30096
   139
bulwahn@43732
   140
class floor_ceiling = archimedean_field +
wenzelm@61942
   141
  fixes floor :: "'a \<Rightarrow> int"  ("\<lfloor>_\<rfloor>")
wenzelm@61942
   142
  assumes floor_correct: "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)"
huffman@30096
   143
wenzelm@61942
   144
lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> \<lfloor>x\<rfloor> = z"
huffman@30096
   145
  using floor_correct [of x] floor_exists1 [of x] by auto
huffman@30096
   146
lp15@59613
   147
lemma floor_unique_iff:
lp15@59613
   148
  fixes x :: "'a::floor_ceiling"
lp15@59613
   149
  shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
lp15@59613
   150
using floor_correct floor_unique by auto
lp15@59613
   151
wenzelm@61942
   152
lemma of_int_floor_le [simp]: "of_int \<lfloor>x\<rfloor> \<le> x"
huffman@30096
   153
  using floor_correct ..
huffman@30096
   154
wenzelm@61942
   155
lemma le_floor_iff: "z \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z \<le> x"
huffman@30096
   156
proof
wenzelm@61942
   157
  assume "z \<le> \<lfloor>x\<rfloor>"
wenzelm@61942
   158
  then have "(of_int z :: 'a) \<le> of_int \<lfloor>x\<rfloor>" by simp
wenzelm@61942
   159
  also have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
huffman@30096
   160
  finally show "of_int z \<le> x" .
huffman@30096
   161
next
huffman@30096
   162
  assume "of_int z \<le> x"
wenzelm@61942
   163
  also have "x < of_int (\<lfloor>x\<rfloor> + 1)" using floor_correct ..
wenzelm@61942
   164
  finally show "z \<le> \<lfloor>x\<rfloor>" by (simp del: of_int_add)
huffman@30096
   165
qed
huffman@30096
   166
wenzelm@61942
   167
lemma floor_less_iff: "\<lfloor>x\<rfloor> < z \<longleftrightarrow> x < of_int z"
huffman@30096
   168
  by (simp add: not_le [symmetric] le_floor_iff)
huffman@30096
   169
wenzelm@61942
   170
lemma less_floor_iff: "z < \<lfloor>x\<rfloor> \<longleftrightarrow> of_int z + 1 \<le> x"
huffman@30096
   171
  using le_floor_iff [of "z + 1" x] by auto
huffman@30096
   172
wenzelm@61942
   173
lemma floor_le_iff: "\<lfloor>x\<rfloor> \<le> z \<longleftrightarrow> x < of_int z + 1"
huffman@30096
   174
  by (simp add: not_less [symmetric] less_floor_iff)
huffman@30096
   175
wenzelm@61942
   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)"
hoelzl@58040
   177
  by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
hoelzl@58040
   178
wenzelm@61942
   179
lemma floor_mono:
wenzelm@61942
   180
  assumes "x \<le> y"
wenzelm@61942
   181
  shows "\<lfloor>x\<rfloor> \<le> \<lfloor>y\<rfloor>"
huffman@30096
   182
proof -
wenzelm@61942
   183
  have "of_int \<lfloor>x\<rfloor> \<le> x" by (rule of_int_floor_le)
wenzelm@60758
   184
  also note \<open>x \<le> y\<close>
huffman@30096
   185
  finally show ?thesis by (simp add: le_floor_iff)
huffman@30096
   186
qed
huffman@30096
   187
wenzelm@61942
   188
lemma floor_less_cancel: "\<lfloor>x\<rfloor> < \<lfloor>y\<rfloor> \<Longrightarrow> x < y"
huffman@30096
   189
  by (auto simp add: not_le [symmetric] floor_mono)
huffman@30096
   190
wenzelm@61942
   191
lemma floor_of_int [simp]: "\<lfloor>of_int z\<rfloor> = z"
huffman@30096
   192
  by (rule floor_unique) simp_all
huffman@30096
   193
wenzelm@61942
   194
lemma floor_of_nat [simp]: "\<lfloor>of_nat n\<rfloor> = int n"
huffman@30096
   195
  using floor_of_int [of "of_nat n"] by simp
huffman@30096
   196
wenzelm@61942
   197
lemma le_floor_add: "\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor> \<le> \<lfloor>x + y\<rfloor>"
huffman@47307
   198
  by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
huffman@47307
   199
wenzelm@60758
   200
text \<open>Floor with numerals\<close>
huffman@30096
   201
wenzelm@61942
   202
lemma floor_zero [simp]: "\<lfloor>0\<rfloor> = 0"
huffman@30096
   203
  using floor_of_int [of 0] by simp
huffman@30096
   204
wenzelm@61942
   205
lemma floor_one [simp]: "\<lfloor>1\<rfloor> = 1"
huffman@30096
   206
  using floor_of_int [of 1] by simp
huffman@30096
   207
wenzelm@61942
   208
lemma floor_numeral [simp]: "\<lfloor>numeral v\<rfloor> = numeral v"
huffman@47108
   209
  using floor_of_int [of "numeral v"] by simp
huffman@47108
   210
wenzelm@61942
   211
lemma floor_neg_numeral [simp]: "\<lfloor>- numeral v\<rfloor> = - numeral v"
haftmann@54489
   212
  using floor_of_int [of "- numeral v"] by simp
huffman@30096
   213
wenzelm@61942
   214
lemma zero_le_floor [simp]: "0 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 0 \<le> x"
huffman@30096
   215
  by (simp add: le_floor_iff)
huffman@30096
   216
wenzelm@61942
   217
lemma one_le_floor [simp]: "1 \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
huffman@30096
   218
  by (simp add: le_floor_iff)
huffman@30096
   219
huffman@47108
   220
lemma numeral_le_floor [simp]:
wenzelm@61942
   221
  "numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v \<le> x"
huffman@47108
   222
  by (simp add: le_floor_iff)
huffman@47108
   223
huffman@47108
   224
lemma neg_numeral_le_floor [simp]:
wenzelm@61942
   225
  "- numeral v \<le> \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v \<le> x"
huffman@30096
   226
  by (simp add: le_floor_iff)
huffman@30096
   227
wenzelm@61942
   228
lemma zero_less_floor [simp]: "0 < \<lfloor>x\<rfloor> \<longleftrightarrow> 1 \<le> x"
huffman@30096
   229
  by (simp add: less_floor_iff)
huffman@30096
   230
wenzelm@61942
   231
lemma one_less_floor [simp]: "1 < \<lfloor>x\<rfloor> \<longleftrightarrow> 2 \<le> x"
huffman@30096
   232
  by (simp add: less_floor_iff)
huffman@30096
   233
huffman@47108
   234
lemma numeral_less_floor [simp]:
wenzelm@61942
   235
  "numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> numeral v + 1 \<le> x"
huffman@47108
   236
  by (simp add: less_floor_iff)
huffman@47108
   237
huffman@47108
   238
lemma neg_numeral_less_floor [simp]:
wenzelm@61942
   239
  "- numeral v < \<lfloor>x\<rfloor> \<longleftrightarrow> - numeral v + 1 \<le> x"
huffman@30096
   240
  by (simp add: less_floor_iff)
huffman@30096
   241
wenzelm@61942
   242
lemma floor_le_zero [simp]: "\<lfloor>x\<rfloor> \<le> 0 \<longleftrightarrow> x < 1"
huffman@30096
   243
  by (simp add: floor_le_iff)
huffman@30096
   244
wenzelm@61942
   245
lemma floor_le_one [simp]: "\<lfloor>x\<rfloor> \<le> 1 \<longleftrightarrow> x < 2"
huffman@30096
   246
  by (simp add: floor_le_iff)
huffman@30096
   247
huffman@47108
   248
lemma floor_le_numeral [simp]:
wenzelm@61942
   249
  "\<lfloor>x\<rfloor> \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
huffman@47108
   250
  by (simp add: floor_le_iff)
huffman@47108
   251
huffman@47108
   252
lemma floor_le_neg_numeral [simp]:
wenzelm@61942
   253
  "\<lfloor>x\<rfloor> \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
huffman@30096
   254
  by (simp add: floor_le_iff)
huffman@30096
   255
wenzelm@61942
   256
lemma floor_less_zero [simp]: "\<lfloor>x\<rfloor> < 0 \<longleftrightarrow> x < 0"
huffman@30096
   257
  by (simp add: floor_less_iff)
huffman@30096
   258
wenzelm@61942
   259
lemma floor_less_one [simp]: "\<lfloor>x\<rfloor> < 1 \<longleftrightarrow> x < 1"
huffman@30096
   260
  by (simp add: floor_less_iff)
huffman@30096
   261
huffman@47108
   262
lemma floor_less_numeral [simp]:
wenzelm@61942
   263
  "\<lfloor>x\<rfloor> < numeral v \<longleftrightarrow> x < numeral v"
huffman@47108
   264
  by (simp add: floor_less_iff)
huffman@47108
   265
huffman@47108
   266
lemma floor_less_neg_numeral [simp]:
wenzelm@61942
   267
  "\<lfloor>x\<rfloor> < - numeral v \<longleftrightarrow> x < - numeral v"
huffman@30096
   268
  by (simp add: floor_less_iff)
huffman@30096
   269
wenzelm@60758
   270
text \<open>Addition and subtraction of integers\<close>
huffman@30096
   271
wenzelm@61942
   272
lemma floor_add_of_int [simp]: "\<lfloor>x + of_int z\<rfloor> = \<lfloor>x\<rfloor> + z"
huffman@30096
   273
  using floor_correct [of x] by (simp add: floor_unique)
huffman@30096
   274
huffman@47108
   275
lemma floor_add_numeral [simp]:
wenzelm@61942
   276
    "\<lfloor>x + numeral v\<rfloor> = \<lfloor>x\<rfloor> + numeral v"
huffman@47108
   277
  using floor_add_of_int [of x "numeral v"] by simp
huffman@47108
   278
wenzelm@61942
   279
lemma floor_add_one [simp]: "\<lfloor>x + 1\<rfloor> = \<lfloor>x\<rfloor> + 1"
huffman@30096
   280
  using floor_add_of_int [of x 1] by simp
huffman@30096
   281
wenzelm@61942
   282
lemma floor_diff_of_int [simp]: "\<lfloor>x - of_int z\<rfloor> = \<lfloor>x\<rfloor> - z"
huffman@30096
   283
  using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
huffman@30096
   284
wenzelm@61942
   285
lemma floor_uminus_of_int [simp]: "\<lfloor>- (of_int z)\<rfloor> = - z"
lp15@59613
   286
  by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
lp15@59613
   287
huffman@47108
   288
lemma floor_diff_numeral [simp]:
wenzelm@61942
   289
  "\<lfloor>x - numeral v\<rfloor> = \<lfloor>x\<rfloor> - numeral v"
huffman@47108
   290
  using floor_diff_of_int [of x "numeral v"] by simp
huffman@47108
   291
wenzelm@61942
   292
lemma floor_diff_one [simp]: "\<lfloor>x - 1\<rfloor> = \<lfloor>x\<rfloor> - 1"
huffman@30096
   293
  using floor_diff_of_int [of x 1] by simp
huffman@30096
   294
hoelzl@58097
   295
lemma le_mult_floor:
hoelzl@58097
   296
  assumes "0 \<le> a" and "0 \<le> b"
wenzelm@61942
   297
  shows "\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor> \<le> \<lfloor>a * b\<rfloor>"
hoelzl@58097
   298
proof -
wenzelm@61942
   299
  have "of_int \<lfloor>a\<rfloor> \<le> a"
wenzelm@61942
   300
    and "of_int \<lfloor>b\<rfloor> \<le> b" by (auto intro: of_int_floor_le)
wenzelm@61942
   301
  hence "of_int (\<lfloor>a\<rfloor> * \<lfloor>b\<rfloor>) \<le> a * b"
hoelzl@58097
   302
    using assms by (auto intro!: mult_mono)
wenzelm@61942
   303
  also have "a * b < of_int (\<lfloor>a * b\<rfloor> + 1)"
hoelzl@58097
   304
    using floor_correct[of "a * b"] by auto
hoelzl@58097
   305
  finally show ?thesis unfolding of_int_less_iff by simp
hoelzl@58097
   306
qed
hoelzl@58097
   307
haftmann@59984
   308
lemma floor_divide_of_int_eq:
haftmann@59984
   309
  fixes k l :: int
haftmann@60128
   310
  shows "\<lfloor>of_int k / of_int l\<rfloor> = k div l"
haftmann@59984
   311
proof (cases "l = 0")
haftmann@59984
   312
  case True then show ?thesis by simp
haftmann@59984
   313
next
haftmann@59984
   314
  case False
haftmann@59984
   315
  have *: "\<lfloor>of_int (k mod l) / of_int l :: 'a\<rfloor> = 0"
haftmann@59984
   316
  proof (cases "l > 0")
haftmann@59984
   317
    case True then show ?thesis
haftmann@59984
   318
      by (auto intro: floor_unique)
haftmann@59984
   319
  next
haftmann@59984
   320
    case False
haftmann@59984
   321
    obtain r where "r = - l" by blast
haftmann@59984
   322
    then have l: "l = - r" by simp
wenzelm@60758
   323
    moreover with \<open>l \<noteq> 0\<close> False have "r > 0" by simp
haftmann@59984
   324
    ultimately show ?thesis using pos_mod_bound [of r]
haftmann@59984
   325
      by (auto simp add: zmod_zminus2_eq_if less_le field_simps intro: floor_unique)
haftmann@59984
   326
  qed
haftmann@59984
   327
  have "(of_int k :: 'a) = of_int (k div l * l + k mod l)"
haftmann@59984
   328
    by simp
haftmann@59984
   329
  also have "\<dots> = (of_int (k div l) + of_int (k mod l) / of_int l) * of_int l"
haftmann@59984
   330
    using False by (simp only: of_int_add) (simp add: field_simps)
haftmann@59984
   331
  finally have "(of_int k / of_int l :: 'a) = \<dots> / of_int l"
haftmann@59984
   332
    by simp 
haftmann@59984
   333
  then have "(of_int k / of_int l :: 'a) = of_int (k div l) + of_int (k mod l) / of_int l"
haftmann@59984
   334
    using False by (simp only:) (simp add: field_simps)
haftmann@59984
   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>" 
haftmann@59984
   336
    by simp
haftmann@59984
   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>"
haftmann@59984
   338
    by (simp add: ac_simps)
haftmann@60128
   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"
haftmann@59984
   340
    by simp
haftmann@59984
   341
  with * show ?thesis by simp
haftmann@59984
   342
qed
haftmann@59984
   343
haftmann@59984
   344
lemma floor_divide_of_nat_eq:
haftmann@59984
   345
  fixes m n :: nat
haftmann@59984
   346
  shows "\<lfloor>of_nat m / of_nat n\<rfloor> = of_nat (m div n)"
haftmann@59984
   347
proof (cases "n = 0")
haftmann@59984
   348
  case True then show ?thesis by simp
haftmann@59984
   349
next
haftmann@59984
   350
  case False
haftmann@59984
   351
  then have *: "\<lfloor>of_nat (m mod n) / of_nat n :: 'a\<rfloor> = 0"
haftmann@59984
   352
    by (auto intro: floor_unique)
haftmann@59984
   353
  have "(of_nat m :: 'a) = of_nat (m div n * n + m mod n)"
haftmann@59984
   354
    by simp
haftmann@59984
   355
  also have "\<dots> = (of_nat (m div n) + of_nat (m mod n) / of_nat n) * of_nat n"
haftmann@59984
   356
    using False by (simp only: of_nat_add) (simp add: field_simps of_nat_mult)
haftmann@59984
   357
  finally have "(of_nat m / of_nat n :: 'a) = \<dots> / of_nat n"
haftmann@59984
   358
    by simp 
haftmann@59984
   359
  then have "(of_nat m / of_nat n :: 'a) = of_nat (m div n) + of_nat (m mod n) / of_nat n"
haftmann@59984
   360
    using False by (simp only:) simp
haftmann@59984
   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>" 
haftmann@59984
   362
    by simp
haftmann@59984
   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>"
haftmann@59984
   364
    by (simp add: ac_simps)
haftmann@59984
   365
  moreover have "(of_nat (m div n) :: 'a) = of_int (of_nat (m div n))"
haftmann@59984
   366
    by simp
haftmann@59984
   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)"
haftmann@59984
   368
    by (simp only: floor_add_of_int)
haftmann@59984
   369
  with * show ?thesis by simp
haftmann@59984
   370
qed
haftmann@59984
   371
haftmann@59984
   372
wenzelm@60758
   373
subsection \<open>Ceiling function\<close>
huffman@30096
   374
wenzelm@61942
   375
definition ceiling :: "'a::floor_ceiling \<Rightarrow> int"  ("\<lceil>_\<rceil>")
wenzelm@61942
   376
  where "\<lceil>x\<rceil> = - \<lfloor>- x\<rfloor>"
huffman@30096
   377
wenzelm@61942
   378
lemma ceiling_correct: "of_int \<lceil>x\<rceil> - 1 < x \<and> x \<le> of_int \<lceil>x\<rceil>"
lp15@61649
   379
  unfolding ceiling_def using floor_correct [of "- x"] by (simp add: le_minus_iff) 
huffman@30096
   380
wenzelm@61942
   381
lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> \<lceil>x\<rceil> = z"
huffman@30096
   382
  unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
huffman@30096
   383
wenzelm@61942
   384
lemma le_of_int_ceiling [simp]: "x \<le> of_int \<lceil>x\<rceil>"
huffman@30096
   385
  using ceiling_correct ..
huffman@30096
   386
wenzelm@61942
   387
lemma ceiling_le_iff: "\<lceil>x\<rceil> \<le> z \<longleftrightarrow> x \<le> of_int z"
huffman@30096
   388
  unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
huffman@30096
   389
wenzelm@61942
   390
lemma less_ceiling_iff: "z < \<lceil>x\<rceil> \<longleftrightarrow> of_int z < x"
huffman@30096
   391
  by (simp add: not_le [symmetric] ceiling_le_iff)
huffman@30096
   392
wenzelm@61942
   393
lemma ceiling_less_iff: "\<lceil>x\<rceil> < z \<longleftrightarrow> x \<le> of_int z - 1"
huffman@30096
   394
  using ceiling_le_iff [of x "z - 1"] by simp
huffman@30096
   395
wenzelm@61942
   396
lemma le_ceiling_iff: "z \<le> \<lceil>x\<rceil> \<longleftrightarrow> of_int z - 1 < x"
huffman@30096
   397
  by (simp add: not_less [symmetric] ceiling_less_iff)
huffman@30096
   398
wenzelm@61942
   399
lemma ceiling_mono: "x \<ge> y \<Longrightarrow> \<lceil>x\<rceil> \<ge> \<lceil>y\<rceil>"
huffman@30096
   400
  unfolding ceiling_def by (simp add: floor_mono)
huffman@30096
   401
wenzelm@61942
   402
lemma ceiling_less_cancel: "\<lceil>x\<rceil> < \<lceil>y\<rceil> \<Longrightarrow> x < y"
huffman@30096
   403
  by (auto simp add: not_le [symmetric] ceiling_mono)
huffman@30096
   404
wenzelm@61942
   405
lemma ceiling_of_int [simp]: "\<lceil>of_int z\<rceil> = z"
huffman@30096
   406
  by (rule ceiling_unique) simp_all
huffman@30096
   407
wenzelm@61942
   408
lemma ceiling_of_nat [simp]: "\<lceil>of_nat n\<rceil> = int n"
huffman@30096
   409
  using ceiling_of_int [of "of_nat n"] by simp
huffman@30096
   410
wenzelm@61942
   411
lemma ceiling_add_le: "\<lceil>x + y\<rceil> \<le> \<lceil>x\<rceil> + \<lceil>y\<rceil>"
huffman@47307
   412
  by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
huffman@47307
   413
wenzelm@60758
   414
text \<open>Ceiling with numerals\<close>
huffman@30096
   415
wenzelm@61942
   416
lemma ceiling_zero [simp]: "\<lceil>0\<rceil> = 0"
huffman@30096
   417
  using ceiling_of_int [of 0] by simp
huffman@30096
   418
wenzelm@61942
   419
lemma ceiling_one [simp]: "\<lceil>1\<rceil> = 1"
huffman@30096
   420
  using ceiling_of_int [of 1] by simp
huffman@30096
   421
wenzelm@61942
   422
lemma ceiling_numeral [simp]: "\<lceil>numeral v\<rceil> = numeral v"
huffman@47108
   423
  using ceiling_of_int [of "numeral v"] by simp
huffman@47108
   424
wenzelm@61942
   425
lemma ceiling_neg_numeral [simp]: "\<lceil>- numeral v\<rceil> = - numeral v"
haftmann@54489
   426
  using ceiling_of_int [of "- numeral v"] by simp
huffman@30096
   427
wenzelm@61942
   428
lemma ceiling_le_zero [simp]: "\<lceil>x\<rceil> \<le> 0 \<longleftrightarrow> x \<le> 0"
huffman@30096
   429
  by (simp add: ceiling_le_iff)
huffman@30096
   430
wenzelm@61942
   431
lemma ceiling_le_one [simp]: "\<lceil>x\<rceil> \<le> 1 \<longleftrightarrow> x \<le> 1"
huffman@30096
   432
  by (simp add: ceiling_le_iff)
huffman@30096
   433
huffman@47108
   434
lemma ceiling_le_numeral [simp]:
wenzelm@61942
   435
  "\<lceil>x\<rceil> \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
huffman@47108
   436
  by (simp add: ceiling_le_iff)
huffman@47108
   437
huffman@47108
   438
lemma ceiling_le_neg_numeral [simp]:
wenzelm@61942
   439
  "\<lceil>x\<rceil> \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
huffman@30096
   440
  by (simp add: ceiling_le_iff)
huffman@30096
   441
wenzelm@61942
   442
lemma ceiling_less_zero [simp]: "\<lceil>x\<rceil> < 0 \<longleftrightarrow> x \<le> -1"
huffman@30096
   443
  by (simp add: ceiling_less_iff)
huffman@30096
   444
wenzelm@61942
   445
lemma ceiling_less_one [simp]: "\<lceil>x\<rceil> < 1 \<longleftrightarrow> x \<le> 0"
huffman@30096
   446
  by (simp add: ceiling_less_iff)
huffman@30096
   447
huffman@47108
   448
lemma ceiling_less_numeral [simp]:
wenzelm@61942
   449
  "\<lceil>x\<rceil> < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
huffman@47108
   450
  by (simp add: ceiling_less_iff)
huffman@47108
   451
huffman@47108
   452
lemma ceiling_less_neg_numeral [simp]:
wenzelm@61942
   453
  "\<lceil>x\<rceil> < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
huffman@30096
   454
  by (simp add: ceiling_less_iff)
huffman@30096
   455
wenzelm@61942
   456
lemma zero_le_ceiling [simp]: "0 \<le> \<lceil>x\<rceil> \<longleftrightarrow> -1 < x"
huffman@30096
   457
  by (simp add: le_ceiling_iff)
huffman@30096
   458
wenzelm@61942
   459
lemma one_le_ceiling [simp]: "1 \<le> \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
huffman@30096
   460
  by (simp add: le_ceiling_iff)
huffman@30096
   461
huffman@47108
   462
lemma numeral_le_ceiling [simp]:
wenzelm@61942
   463
  "numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> numeral v - 1 < x"
huffman@47108
   464
  by (simp add: le_ceiling_iff)
huffman@47108
   465
huffman@47108
   466
lemma neg_numeral_le_ceiling [simp]:
wenzelm@61942
   467
  "- numeral v \<le> \<lceil>x\<rceil> \<longleftrightarrow> - numeral v - 1 < x"
huffman@30096
   468
  by (simp add: le_ceiling_iff)
huffman@30096
   469
wenzelm@61942
   470
lemma zero_less_ceiling [simp]: "0 < \<lceil>x\<rceil> \<longleftrightarrow> 0 < x"
huffman@30096
   471
  by (simp add: less_ceiling_iff)
huffman@30096
   472
wenzelm@61942
   473
lemma one_less_ceiling [simp]: "1 < \<lceil>x\<rceil> \<longleftrightarrow> 1 < x"
huffman@30096
   474
  by (simp add: less_ceiling_iff)
huffman@30096
   475
huffman@47108
   476
lemma numeral_less_ceiling [simp]:
wenzelm@61942
   477
  "numeral v < \<lceil>x\<rceil> \<longleftrightarrow> numeral v < x"
huffman@47108
   478
  by (simp add: less_ceiling_iff)
huffman@47108
   479
huffman@47108
   480
lemma neg_numeral_less_ceiling [simp]:
wenzelm@61942
   481
  "- numeral v < \<lceil>x\<rceil> \<longleftrightarrow> - numeral v < x"
huffman@30096
   482
  by (simp add: less_ceiling_iff)
huffman@30096
   483
wenzelm@61942
   484
lemma ceiling_altdef: "\<lceil>x\<rceil> = (if x = of_int \<lfloor>x\<rfloor> then \<lfloor>x\<rfloor> else \<lfloor>x\<rfloor> + 1)"
eberlm@61531
   485
  by (intro ceiling_unique, (simp, linarith?)+)
eberlm@61531
   486
wenzelm@61942
   487
lemma floor_le_ceiling [simp]: "\<lfloor>x\<rfloor> \<le> \<lceil>x\<rceil>"
wenzelm@61942
   488
  by (simp add: ceiling_altdef)
eberlm@61531
   489
wenzelm@60758
   490
text \<open>Addition and subtraction of integers\<close>
huffman@30096
   491
wenzelm@61942
   492
lemma ceiling_add_of_int [simp]: "\<lceil>x + of_int z\<rceil> = \<lceil>x\<rceil> + z"
lp15@61649
   493
  using ceiling_correct [of x] by (simp add: ceiling_def)
huffman@30096
   494
wenzelm@61942
   495
lemma ceiling_add_numeral [simp]: "\<lceil>x + numeral v\<rceil> = \<lceil>x\<rceil> + numeral v"
huffman@47108
   496
  using ceiling_add_of_int [of x "numeral v"] by simp
huffman@47108
   497
wenzelm@61942
   498
lemma ceiling_add_one [simp]: "\<lceil>x + 1\<rceil> = \<lceil>x\<rceil> + 1"
huffman@30096
   499
  using ceiling_add_of_int [of x 1] by simp
huffman@30096
   500
wenzelm@61942
   501
lemma ceiling_diff_of_int [simp]: "\<lceil>x - of_int z\<rceil> = \<lceil>x\<rceil> - z"
huffman@30096
   502
  using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
huffman@30096
   503
wenzelm@61942
   504
lemma ceiling_diff_numeral [simp]: "\<lceil>x - numeral v\<rceil> = \<lceil>x\<rceil> - numeral v"
huffman@47108
   505
  using ceiling_diff_of_int [of x "numeral v"] by simp
huffman@47108
   506
wenzelm@61942
   507
lemma ceiling_diff_one [simp]: "\<lceil>x - 1\<rceil> = \<lceil>x\<rceil> - 1"
huffman@30096
   508
  using ceiling_diff_of_int [of x 1] by simp
huffman@30096
   509
wenzelm@61942
   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)"
hoelzl@58040
   511
  by (auto simp add: ceiling_unique ceiling_correct)
hoelzl@58040
   512
wenzelm@61942
   513
lemma ceiling_diff_floor_le_1: "\<lceil>x\<rceil> - \<lfloor>x\<rfloor> \<le> 1"
hoelzl@47592
   514
proof -
hoelzl@47592
   515
  have "of_int \<lceil>x\<rceil> - 1 < x" 
hoelzl@47592
   516
    using ceiling_correct[of x] by simp
hoelzl@47592
   517
  also have "x < of_int \<lfloor>x\<rfloor> + 1"
hoelzl@47592
   518
    using floor_correct[of x] by simp_all
hoelzl@47592
   519
  finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
hoelzl@47592
   520
    by simp
hoelzl@47592
   521
  then show ?thesis
hoelzl@47592
   522
    unfolding of_int_less_iff by simp
hoelzl@47592
   523
qed
huffman@30096
   524
wenzelm@60758
   525
subsection \<open>Negation\<close>
huffman@30096
   526
wenzelm@61942
   527
lemma floor_minus: "\<lfloor>- x\<rfloor> = - \<lceil>x\<rceil>"
huffman@30096
   528
  unfolding ceiling_def by simp
huffman@30096
   529
wenzelm@61942
   530
lemma ceiling_minus: "\<lceil>- x\<rceil> = - \<lfloor>x\<rfloor>"
huffman@30096
   531
  unfolding ceiling_def by simp
huffman@30096
   532
wenzelm@61942
   533
wenzelm@60758
   534
subsection \<open>Frac Function\<close>
lp15@59613
   535
lp15@59613
   536
definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
lp15@59613
   537
  "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
lp15@59613
   538
lp15@59613
   539
lemma frac_lt_1: "frac x < 1"
lp15@59613
   540
  by  (simp add: frac_def) linarith
lp15@59613
   541
wenzelm@61070
   542
lemma frac_eq_0_iff [simp]: "frac x = 0 \<longleftrightarrow> x \<in> \<int>"
lp15@59613
   543
  by (simp add: frac_def) (metis Ints_cases Ints_of_int floor_of_int )
lp15@59613
   544
lp15@59613
   545
lemma frac_ge_0 [simp]: "frac x \<ge> 0"
lp15@59613
   546
  unfolding frac_def
lp15@59613
   547
  by linarith
lp15@59613
   548
wenzelm@61070
   549
lemma frac_gt_0_iff [simp]: "frac x > 0 \<longleftrightarrow> x \<notin> \<int>"
lp15@59613
   550
  by (metis frac_eq_0_iff frac_ge_0 le_less less_irrefl)
lp15@59613
   551
lp15@59613
   552
lemma frac_of_int [simp]: "frac (of_int z) = 0"
lp15@59613
   553
  by (simp add: frac_def)
lp15@59613
   554
lp15@59613
   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)"  
lp15@59613
   556
proof -
lp15@59613
   557
  {assume "x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
lp15@59613
   558
   then have "\<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>"
lp15@59613
   559
     by (metis add.commute floor_unique le_floor_add le_floor_iff of_int_add)
lp15@59613
   560
   }
lp15@59613
   561
  moreover
lp15@59613
   562
  { assume "\<not> x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
lp15@59613
   563
    then have "\<lfloor>x + y\<rfloor> = 1 + (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>)"
lp15@59613
   564
      apply (simp add: floor_unique_iff)
lp15@59613
   565
      apply (auto simp add: algebra_simps)
lp15@59613
   566
      by linarith    
lp15@59613
   567
  }
lp15@59613
   568
  ultimately show ?thesis
lp15@59613
   569
    by (auto simp add: frac_def algebra_simps)
lp15@59613
   570
qed
lp15@59613
   571
lp15@59613
   572
lemma frac_add: "frac (x + y) = (if frac x + frac y < 1 then frac x + frac y
lp15@59613
   573
                                 else (frac x + frac y) - 1)"  
lp15@59613
   574
  by (simp add: frac_def floor_add)
lp15@59613
   575
lp15@59613
   576
lemma frac_unique_iff:
lp15@59613
   577
  fixes x :: "'a::floor_ceiling"
wenzelm@61070
   578
  shows  "(frac x) = a \<longleftrightarrow> x - a \<in> \<int> \<and> 0 \<le> a \<and> a < 1"
lp15@59613
   579
  apply (auto simp: Ints_def frac_def)
lp15@59613
   580
  apply linarith
lp15@59613
   581
  apply linarith
lp15@59613
   582
  by (metis (no_types) add.commute add.left_neutral eq_diff_eq floor_add_of_int floor_unique of_int_0)
lp15@59613
   583
lp15@59613
   584
lemma frac_eq: "(frac x) = x \<longleftrightarrow> 0 \<le> x \<and> x < 1"
lp15@59613
   585
  by (simp add: frac_unique_iff)
lp15@59613
   586
  
lp15@59613
   587
lemma frac_neg:
lp15@59613
   588
  fixes x :: "'a::floor_ceiling"
wenzelm@61070
   589
  shows  "frac (-x) = (if x \<in> \<int> then 0 else 1 - frac x)"
lp15@59613
   590
  apply (auto simp add: frac_unique_iff)
lp15@59613
   591
  apply (simp add: frac_def)
lp15@59613
   592
  by (meson frac_lt_1 less_iff_diff_less_0 not_le not_less_iff_gr_or_eq)
lp15@59613
   593
eberlm@61531
   594
eberlm@61531
   595
subsection \<open>Rounding to the nearest integer\<close>
eberlm@61531
   596
eberlm@61531
   597
definition round where "round x = \<lfloor>x + 1/2\<rfloor>"
eberlm@61531
   598
eberlm@61531
   599
lemma of_int_round_ge:     "of_int (round x) \<ge> x - 1/2"
eberlm@61531
   600
  and of_int_round_le:     "of_int (round x) \<le> x + 1/2"
eberlm@61531
   601
  and of_int_round_abs_le: "\<bar>of_int (round x) - x\<bar> \<le> 1/2"
eberlm@61531
   602
  and of_int_round_gt:     "of_int (round x) > x - 1/2"
eberlm@61531
   603
proof -
eberlm@61531
   604
  from floor_correct[of "x + 1/2"] have "x + 1/2 < of_int (round x) + 1" by (simp add: round_def)
eberlm@61531
   605
  from add_strict_right_mono[OF this, of "-1"] show A: "of_int (round x) > x - 1/2" by simp
eberlm@61531
   606
  thus "of_int (round x) \<ge> x - 1/2" by simp
eberlm@61531
   607
  from floor_correct[of "x + 1/2"] show "of_int (round x) \<le> x + 1/2" by (simp add: round_def)
eberlm@61531
   608
  with A show "\<bar>of_int (round x) - x\<bar> \<le> 1/2" by linarith
eberlm@61531
   609
qed
eberlm@61531
   610
eberlm@61531
   611
lemma round_of_int [simp]: "round (of_int n) = n"
eberlm@61531
   612
  unfolding round_def by (subst floor_unique_iff) force
eberlm@61531
   613
eberlm@61531
   614
lemma round_0 [simp]: "round 0 = 0"
eberlm@61531
   615
  using round_of_int[of 0] by simp
eberlm@61531
   616
eberlm@61531
   617
lemma round_1 [simp]: "round 1 = 1"
eberlm@61531
   618
  using round_of_int[of 1] by simp
eberlm@61531
   619
eberlm@61531
   620
lemma round_numeral [simp]: "round (numeral n) = numeral n"
eberlm@61531
   621
  using round_of_int[of "numeral n"] by simp
eberlm@61531
   622
eberlm@61531
   623
lemma round_neg_numeral [simp]: "round (-numeral n) = -numeral n"
eberlm@61531
   624
  using round_of_int[of "-numeral n"] by simp
eberlm@61531
   625
eberlm@61531
   626
lemma round_of_nat [simp]: "round (of_nat n) = of_nat n"
eberlm@61531
   627
  using round_of_int[of "int n"] by simp
eberlm@61531
   628
eberlm@61531
   629
lemma round_mono: "x \<le> y \<Longrightarrow> round x \<le> round y"
eberlm@61531
   630
  unfolding round_def by (intro floor_mono) simp
eberlm@61531
   631
eberlm@61531
   632
lemma round_unique: "of_int y > x - 1/2 \<Longrightarrow> of_int y \<le> x + 1/2 \<Longrightarrow> round x = y"
eberlm@61531
   633
unfolding round_def
eberlm@61531
   634
proof (rule floor_unique)
eberlm@61531
   635
  assume "x - 1 / 2 < of_int y"
eberlm@61531
   636
  from add_strict_left_mono[OF this, of 1] show "x + 1 / 2 < of_int y + 1" by simp
eberlm@61531
   637
qed
eberlm@61531
   638
wenzelm@61942
   639
lemma round_altdef: "round x = (if frac x \<ge> 1/2 then \<lceil>x\<rceil> else \<lfloor>x\<rfloor>)"
eberlm@61531
   640
  by (cases "frac x \<ge> 1/2")
eberlm@61531
   641
     (rule round_unique, ((simp add: frac_def field_simps ceiling_altdef, linarith?)+)[2])+
eberlm@61531
   642
eberlm@61531
   643
lemma floor_le_round: "\<lfloor>x\<rfloor> \<le> round x"
eberlm@61531
   644
  unfolding round_def by (intro floor_mono) simp
eberlm@61531
   645
eberlm@61531
   646
lemma ceiling_ge_round: "\<lceil>x\<rceil> \<ge> round x" unfolding round_altdef by simp
eberlm@61531
   647
     
eberlm@61531
   648
lemma round_diff_minimal: 
eberlm@61531
   649
  fixes z :: "'a :: floor_ceiling"
eberlm@61531
   650
  shows "abs (z - of_int (round z)) \<le> abs (z - of_int m)"
eberlm@61531
   651
proof (cases "of_int m \<ge> z")
eberlm@61531
   652
  case True
wenzelm@61942
   653
  hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lceil>z\<rceil> - z\<bar>"
lp15@61738
   654
    unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
eberlm@61531
   655
  also have "of_int \<lceil>z\<rceil> - z \<ge> 0" by linarith
wenzelm@61942
   656
  with True have "\<bar>of_int \<lceil>z\<rceil> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
eberlm@61531
   657
    by (simp add: ceiling_le_iff)
eberlm@61531
   658
  finally show ?thesis .
eberlm@61531
   659
next
eberlm@61531
   660
  case False
wenzelm@61942
   661
  hence "\<bar>z - of_int (round z)\<bar> \<le> \<bar>of_int \<lfloor>z\<rfloor> - z\<bar>"
lp15@61738
   662
    unfolding round_altdef by (simp add: field_simps ceiling_altdef frac_def) linarith?
eberlm@61531
   663
  also have "z - of_int \<lfloor>z\<rfloor> \<ge> 0" by linarith
wenzelm@61942
   664
  with False have "\<bar>of_int \<lfloor>z\<rfloor> - z\<bar> \<le> \<bar>z - of_int m\<bar>"
eberlm@61531
   665
    by (simp add: le_floor_iff)
eberlm@61531
   666
  finally show ?thesis .
eberlm@61531
   667
qed
eberlm@61531
   668
huffman@30096
   669
end