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