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