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