src/HOL/Archimedean_Field.thy
author paulson <lp15@cam.ac.uk>
Thu Mar 05 17:30:29 2015 +0000 (2015-03-05)
changeset 59613 7103019278f0
parent 58889 5b7a9633cfa8
child 59984 4f1eccec320c
permissions -rw-r--r--
The function frac. Various lemmas about limits, series, the exp function, etc.
wenzelm@41959
     1
(*  Title:      HOL/Archimedean_Field.thy
wenzelm@41959
     2
    Author:     Brian Huffman
huffman@30096
     3
*)
huffman@30096
     4
wenzelm@58889
     5
section {* Archimedean Fields, Floor and Ceiling Functions *}
huffman@30096
     6
huffman@30096
     7
theory Archimedean_Field
huffman@30096
     8
imports Main
huffman@30096
     9
begin
huffman@30096
    10
huffman@30096
    11
subsection {* Class of Archimedean fields *}
huffman@30096
    12
huffman@30096
    13
text {* Archimedean fields have no infinite elements. *}
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
huffman@30096
    51
text {* Archimedean fields have no infinitesimal elements. *}
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 -
huffman@30096
    57
  from `0 < x` 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)"
huffman@30096
    62
    using `0 < inverse x` by (cases n) (simp_all del: of_nat_Suc)
huffman@30096
    63
  then have "inverse (of_nat (Suc m)) < inverse (inverse x)"
huffman@30096
    64
    using `0 < inverse x` by (rule less_imp_inverse_less)
huffman@30096
    65
  then have "inverse (of_nat (Suc m)) < x"
huffman@30096
    66
    using `0 < x` 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"
huffman@30096
    73
  using ex_inverse_of_nat_Suc_less [OF `0 < x`] 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 ..
huffman@30096
    80
  with `0 < x` 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
huffman@30096
    85
subsection {* Existence and uniqueness of floor function *}
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 -
huffman@30096
    91
  from `\<exists>n. P n` have "P (Least P)" by (rule LeastI_ex)
huffman@30096
    92
  with `\<not> P 0` 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)"
huffman@30096
    97
    using `P (Least P)` `Least P = Suc n` 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
huffman@30096
   138
subsection {* Floor function *}
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
notation (HTML output)
huffman@30096
   148
  floor  ("\<lfloor>_\<rfloor>")
huffman@30096
   149
huffman@30096
   150
lemma floor_unique: "\<lbrakk>of_int z \<le> x; x < of_int z + 1\<rbrakk> \<Longrightarrow> floor x = z"
huffman@30096
   151
  using floor_correct [of x] floor_exists1 [of x] by auto
huffman@30096
   152
lp15@59613
   153
lemma floor_unique_iff:
lp15@59613
   154
  fixes x :: "'a::floor_ceiling"
lp15@59613
   155
  shows  "\<lfloor>x\<rfloor> = a \<longleftrightarrow> of_int a \<le> x \<and> x < of_int a + 1"
lp15@59613
   156
using floor_correct floor_unique by auto
lp15@59613
   157
huffman@30096
   158
lemma of_int_floor_le: "of_int (floor x) \<le> x"
huffman@30096
   159
  using floor_correct ..
huffman@30096
   160
huffman@30096
   161
lemma le_floor_iff: "z \<le> floor x \<longleftrightarrow> of_int z \<le> x"
huffman@30096
   162
proof
huffman@30096
   163
  assume "z \<le> floor x"
huffman@30096
   164
  then have "(of_int z :: 'a) \<le> of_int (floor x)" by simp
huffman@30096
   165
  also have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
huffman@30096
   166
  finally show "of_int z \<le> x" .
huffman@30096
   167
next
huffman@30096
   168
  assume "of_int z \<le> x"
huffman@30096
   169
  also have "x < of_int (floor x + 1)" using floor_correct ..
huffman@30096
   170
  finally show "z \<le> floor x" by (simp del: of_int_add)
huffman@30096
   171
qed
huffman@30096
   172
huffman@30096
   173
lemma floor_less_iff: "floor x < z \<longleftrightarrow> x < of_int z"
huffman@30096
   174
  by (simp add: not_le [symmetric] le_floor_iff)
huffman@30096
   175
huffman@30096
   176
lemma less_floor_iff: "z < floor x \<longleftrightarrow> of_int z + 1 \<le> x"
huffman@30096
   177
  using le_floor_iff [of "z + 1" x] by auto
huffman@30096
   178
huffman@30096
   179
lemma floor_le_iff: "floor x \<le> z \<longleftrightarrow> x < of_int z + 1"
huffman@30096
   180
  by (simp add: not_less [symmetric] less_floor_iff)
huffman@30096
   181
hoelzl@58040
   182
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
   183
  by (metis floor_correct floor_unique less_floor_iff not_le order_refl)
hoelzl@58040
   184
huffman@30096
   185
lemma floor_mono: assumes "x \<le> y" shows "floor x \<le> floor y"
huffman@30096
   186
proof -
huffman@30096
   187
  have "of_int (floor x) \<le> x" by (rule of_int_floor_le)
huffman@30096
   188
  also note `x \<le> y`
huffman@30096
   189
  finally show ?thesis by (simp add: le_floor_iff)
huffman@30096
   190
qed
huffman@30096
   191
huffman@30096
   192
lemma floor_less_cancel: "floor x < floor y \<Longrightarrow> x < y"
huffman@30096
   193
  by (auto simp add: not_le [symmetric] floor_mono)
huffman@30096
   194
huffman@30096
   195
lemma floor_of_int [simp]: "floor (of_int z) = z"
huffman@30096
   196
  by (rule floor_unique) simp_all
huffman@30096
   197
huffman@30096
   198
lemma floor_of_nat [simp]: "floor (of_nat n) = int n"
huffman@30096
   199
  using floor_of_int [of "of_nat n"] by simp
huffman@30096
   200
huffman@47307
   201
lemma le_floor_add: "floor x + floor y \<le> floor (x + y)"
huffman@47307
   202
  by (simp only: le_floor_iff of_int_add add_mono of_int_floor_le)
huffman@47307
   203
huffman@30096
   204
text {* Floor with numerals *}
huffman@30096
   205
huffman@30096
   206
lemma floor_zero [simp]: "floor 0 = 0"
huffman@30096
   207
  using floor_of_int [of 0] by simp
huffman@30096
   208
huffman@30096
   209
lemma floor_one [simp]: "floor 1 = 1"
huffman@30096
   210
  using floor_of_int [of 1] by simp
huffman@30096
   211
huffman@47108
   212
lemma floor_numeral [simp]: "floor (numeral v) = numeral v"
huffman@47108
   213
  using floor_of_int [of "numeral v"] by simp
huffman@47108
   214
haftmann@54489
   215
lemma floor_neg_numeral [simp]: "floor (- numeral v) = - numeral v"
haftmann@54489
   216
  using floor_of_int [of "- numeral v"] by simp
huffman@30096
   217
huffman@30096
   218
lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
huffman@30096
   219
  by (simp add: le_floor_iff)
huffman@30096
   220
huffman@30096
   221
lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
huffman@30096
   222
  by (simp add: le_floor_iff)
huffman@30096
   223
huffman@47108
   224
lemma numeral_le_floor [simp]:
huffman@47108
   225
  "numeral v \<le> floor x \<longleftrightarrow> numeral v \<le> x"
huffman@47108
   226
  by (simp add: le_floor_iff)
huffman@47108
   227
huffman@47108
   228
lemma neg_numeral_le_floor [simp]:
haftmann@54489
   229
  "- numeral v \<le> floor x \<longleftrightarrow> - numeral v \<le> x"
huffman@30096
   230
  by (simp add: le_floor_iff)
huffman@30096
   231
huffman@30096
   232
lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
huffman@30096
   233
  by (simp add: less_floor_iff)
huffman@30096
   234
huffman@30096
   235
lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
huffman@30096
   236
  by (simp add: less_floor_iff)
huffman@30096
   237
huffman@47108
   238
lemma numeral_less_floor [simp]:
huffman@47108
   239
  "numeral v < floor x \<longleftrightarrow> numeral v + 1 \<le> x"
huffman@47108
   240
  by (simp add: less_floor_iff)
huffman@47108
   241
huffman@47108
   242
lemma neg_numeral_less_floor [simp]:
haftmann@54489
   243
  "- numeral v < floor x \<longleftrightarrow> - numeral v + 1 \<le> x"
huffman@30096
   244
  by (simp add: less_floor_iff)
huffman@30096
   245
huffman@30096
   246
lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
huffman@30096
   247
  by (simp add: floor_le_iff)
huffman@30096
   248
huffman@30096
   249
lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
huffman@30096
   250
  by (simp add: floor_le_iff)
huffman@30096
   251
huffman@47108
   252
lemma floor_le_numeral [simp]:
huffman@47108
   253
  "floor x \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
huffman@47108
   254
  by (simp add: floor_le_iff)
huffman@47108
   255
huffman@47108
   256
lemma floor_le_neg_numeral [simp]:
haftmann@54489
   257
  "floor x \<le> - numeral v \<longleftrightarrow> x < - numeral v + 1"
huffman@30096
   258
  by (simp add: floor_le_iff)
huffman@30096
   259
huffman@30096
   260
lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
huffman@30096
   261
  by (simp add: floor_less_iff)
huffman@30096
   262
huffman@30096
   263
lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
huffman@30096
   264
  by (simp add: floor_less_iff)
huffman@30096
   265
huffman@47108
   266
lemma floor_less_numeral [simp]:
huffman@47108
   267
  "floor x < numeral v \<longleftrightarrow> x < numeral v"
huffman@47108
   268
  by (simp add: floor_less_iff)
huffman@47108
   269
huffman@47108
   270
lemma floor_less_neg_numeral [simp]:
haftmann@54489
   271
  "floor x < - numeral v \<longleftrightarrow> x < - numeral v"
huffman@30096
   272
  by (simp add: floor_less_iff)
huffman@30096
   273
huffman@30096
   274
text {* Addition and subtraction of integers *}
huffman@30096
   275
huffman@30096
   276
lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
huffman@30096
   277
  using floor_correct [of x] by (simp add: floor_unique)
huffman@30096
   278
huffman@47108
   279
lemma floor_add_numeral [simp]:
huffman@47108
   280
    "floor (x + numeral v) = floor x + numeral v"
huffman@47108
   281
  using floor_add_of_int [of x "numeral v"] by simp
huffman@47108
   282
huffman@30096
   283
lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
huffman@30096
   284
  using floor_add_of_int [of x 1] by simp
huffman@30096
   285
huffman@30096
   286
lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
huffman@30096
   287
  using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
huffman@30096
   288
lp15@59613
   289
lemma floor_uminus_of_int [simp]: "floor (- (of_int z)) = - z"
lp15@59613
   290
  by (metis floor_diff_of_int [of 0] diff_0 floor_zero)
lp15@59613
   291
huffman@47108
   292
lemma floor_diff_numeral [simp]:
huffman@47108
   293
  "floor (x - numeral v) = floor x - numeral v"
huffman@47108
   294
  using floor_diff_of_int [of x "numeral v"] by simp
huffman@47108
   295
huffman@30096
   296
lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
huffman@30096
   297
  using floor_diff_of_int [of x 1] by simp
huffman@30096
   298
hoelzl@58097
   299
lemma le_mult_floor:
hoelzl@58097
   300
  assumes "0 \<le> a" and "0 \<le> b"
hoelzl@58097
   301
  shows "floor a * floor b \<le> floor (a * b)"
hoelzl@58097
   302
proof -
hoelzl@58097
   303
  have "of_int (floor a) \<le> a"
hoelzl@58097
   304
    and "of_int (floor b) \<le> b" by (auto intro: of_int_floor_le)
hoelzl@58097
   305
  hence "of_int (floor a * floor b) \<le> a * b"
hoelzl@58097
   306
    using assms by (auto intro!: mult_mono)
hoelzl@58097
   307
  also have "a * b < of_int (floor (a * b) + 1)"  
hoelzl@58097
   308
    using floor_correct[of "a * b"] by auto
hoelzl@58097
   309
  finally show ?thesis unfolding of_int_less_iff by simp
hoelzl@58097
   310
qed
hoelzl@58097
   311
huffman@30096
   312
subsection {* Ceiling function *}
huffman@30096
   313
huffman@30096
   314
definition
bulwahn@43732
   315
  ceiling :: "'a::floor_ceiling \<Rightarrow> int" where
bulwahn@43733
   316
  "ceiling x = - floor (- x)"
huffman@30096
   317
huffman@30096
   318
notation (xsymbols)
huffman@30096
   319
  ceiling  ("\<lceil>_\<rceil>")
huffman@30096
   320
huffman@30096
   321
notation (HTML output)
huffman@30096
   322
  ceiling  ("\<lceil>_\<rceil>")
huffman@30096
   323
huffman@30096
   324
lemma ceiling_correct: "of_int (ceiling x) - 1 < x \<and> x \<le> of_int (ceiling x)"
huffman@30096
   325
  unfolding ceiling_def using floor_correct [of "- x"] by simp
huffman@30096
   326
huffman@30096
   327
lemma ceiling_unique: "\<lbrakk>of_int z - 1 < x; x \<le> of_int z\<rbrakk> \<Longrightarrow> ceiling x = z"
huffman@30096
   328
  unfolding ceiling_def using floor_unique [of "- z" "- x"] by simp
huffman@30096
   329
huffman@30096
   330
lemma le_of_int_ceiling: "x \<le> of_int (ceiling x)"
huffman@30096
   331
  using ceiling_correct ..
huffman@30096
   332
huffman@30096
   333
lemma ceiling_le_iff: "ceiling x \<le> z \<longleftrightarrow> x \<le> of_int z"
huffman@30096
   334
  unfolding ceiling_def using le_floor_iff [of "- z" "- x"] by auto
huffman@30096
   335
huffman@30096
   336
lemma less_ceiling_iff: "z < ceiling x \<longleftrightarrow> of_int z < x"
huffman@30096
   337
  by (simp add: not_le [symmetric] ceiling_le_iff)
huffman@30096
   338
huffman@30096
   339
lemma ceiling_less_iff: "ceiling x < z \<longleftrightarrow> x \<le> of_int z - 1"
huffman@30096
   340
  using ceiling_le_iff [of x "z - 1"] by simp
huffman@30096
   341
huffman@30096
   342
lemma le_ceiling_iff: "z \<le> ceiling x \<longleftrightarrow> of_int z - 1 < x"
huffman@30096
   343
  by (simp add: not_less [symmetric] ceiling_less_iff)
huffman@30096
   344
huffman@30096
   345
lemma ceiling_mono: "x \<ge> y \<Longrightarrow> ceiling x \<ge> ceiling y"
huffman@30096
   346
  unfolding ceiling_def by (simp add: floor_mono)
huffman@30096
   347
huffman@30096
   348
lemma ceiling_less_cancel: "ceiling x < ceiling y \<Longrightarrow> x < y"
huffman@30096
   349
  by (auto simp add: not_le [symmetric] ceiling_mono)
huffman@30096
   350
huffman@30096
   351
lemma ceiling_of_int [simp]: "ceiling (of_int z) = z"
huffman@30096
   352
  by (rule ceiling_unique) simp_all
huffman@30096
   353
huffman@30096
   354
lemma ceiling_of_nat [simp]: "ceiling (of_nat n) = int n"
huffman@30096
   355
  using ceiling_of_int [of "of_nat n"] by simp
huffman@30096
   356
huffman@47307
   357
lemma ceiling_add_le: "ceiling (x + y) \<le> ceiling x + ceiling y"
huffman@47307
   358
  by (simp only: ceiling_le_iff of_int_add add_mono le_of_int_ceiling)
huffman@47307
   359
huffman@30096
   360
text {* Ceiling with numerals *}
huffman@30096
   361
huffman@30096
   362
lemma ceiling_zero [simp]: "ceiling 0 = 0"
huffman@30096
   363
  using ceiling_of_int [of 0] by simp
huffman@30096
   364
huffman@30096
   365
lemma ceiling_one [simp]: "ceiling 1 = 1"
huffman@30096
   366
  using ceiling_of_int [of 1] by simp
huffman@30096
   367
huffman@47108
   368
lemma ceiling_numeral [simp]: "ceiling (numeral v) = numeral v"
huffman@47108
   369
  using ceiling_of_int [of "numeral v"] by simp
huffman@47108
   370
haftmann@54489
   371
lemma ceiling_neg_numeral [simp]: "ceiling (- numeral v) = - numeral v"
haftmann@54489
   372
  using ceiling_of_int [of "- numeral v"] by simp
huffman@30096
   373
huffman@30096
   374
lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
huffman@30096
   375
  by (simp add: ceiling_le_iff)
huffman@30096
   376
huffman@30096
   377
lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
huffman@30096
   378
  by (simp add: ceiling_le_iff)
huffman@30096
   379
huffman@47108
   380
lemma ceiling_le_numeral [simp]:
huffman@47108
   381
  "ceiling x \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
huffman@47108
   382
  by (simp add: ceiling_le_iff)
huffman@47108
   383
huffman@47108
   384
lemma ceiling_le_neg_numeral [simp]:
haftmann@54489
   385
  "ceiling x \<le> - numeral v \<longleftrightarrow> x \<le> - numeral v"
huffman@30096
   386
  by (simp add: ceiling_le_iff)
huffman@30096
   387
huffman@30096
   388
lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
huffman@30096
   389
  by (simp add: ceiling_less_iff)
huffman@30096
   390
huffman@30096
   391
lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
huffman@30096
   392
  by (simp add: ceiling_less_iff)
huffman@30096
   393
huffman@47108
   394
lemma ceiling_less_numeral [simp]:
huffman@47108
   395
  "ceiling x < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
huffman@47108
   396
  by (simp add: ceiling_less_iff)
huffman@47108
   397
huffman@47108
   398
lemma ceiling_less_neg_numeral [simp]:
haftmann@54489
   399
  "ceiling x < - numeral v \<longleftrightarrow> x \<le> - numeral v - 1"
huffman@30096
   400
  by (simp add: ceiling_less_iff)
huffman@30096
   401
huffman@30096
   402
lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
huffman@30096
   403
  by (simp add: le_ceiling_iff)
huffman@30096
   404
huffman@30096
   405
lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
huffman@30096
   406
  by (simp add: le_ceiling_iff)
huffman@30096
   407
huffman@47108
   408
lemma numeral_le_ceiling [simp]:
huffman@47108
   409
  "numeral v \<le> ceiling x \<longleftrightarrow> numeral v - 1 < x"
huffman@47108
   410
  by (simp add: le_ceiling_iff)
huffman@47108
   411
huffman@47108
   412
lemma neg_numeral_le_ceiling [simp]:
haftmann@54489
   413
  "- numeral v \<le> ceiling x \<longleftrightarrow> - numeral v - 1 < x"
huffman@30096
   414
  by (simp add: le_ceiling_iff)
huffman@30096
   415
huffman@30096
   416
lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
huffman@30096
   417
  by (simp add: less_ceiling_iff)
huffman@30096
   418
huffman@30096
   419
lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
huffman@30096
   420
  by (simp add: less_ceiling_iff)
huffman@30096
   421
huffman@47108
   422
lemma numeral_less_ceiling [simp]:
huffman@47108
   423
  "numeral v < ceiling x \<longleftrightarrow> numeral v < x"
huffman@47108
   424
  by (simp add: less_ceiling_iff)
huffman@47108
   425
huffman@47108
   426
lemma neg_numeral_less_ceiling [simp]:
haftmann@54489
   427
  "- numeral v < ceiling x \<longleftrightarrow> - numeral v < x"
huffman@30096
   428
  by (simp add: less_ceiling_iff)
huffman@30096
   429
huffman@30096
   430
text {* Addition and subtraction of integers *}
huffman@30096
   431
huffman@30096
   432
lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
huffman@30096
   433
  using ceiling_correct [of x] by (simp add: ceiling_unique)
huffman@30096
   434
huffman@47108
   435
lemma ceiling_add_numeral [simp]:
huffman@47108
   436
    "ceiling (x + numeral v) = ceiling x + numeral v"
huffman@47108
   437
  using ceiling_add_of_int [of x "numeral v"] by simp
huffman@47108
   438
huffman@30096
   439
lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
huffman@30096
   440
  using ceiling_add_of_int [of x 1] by simp
huffman@30096
   441
huffman@30096
   442
lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
huffman@30096
   443
  using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
huffman@30096
   444
huffman@47108
   445
lemma ceiling_diff_numeral [simp]:
huffman@47108
   446
  "ceiling (x - numeral v) = ceiling x - numeral v"
huffman@47108
   447
  using ceiling_diff_of_int [of x "numeral v"] by simp
huffman@47108
   448
huffman@30096
   449
lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
huffman@30096
   450
  using ceiling_diff_of_int [of x 1] by simp
huffman@30096
   451
hoelzl@58040
   452
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
   453
  by (auto simp add: ceiling_unique ceiling_correct)
hoelzl@58040
   454
hoelzl@47592
   455
lemma ceiling_diff_floor_le_1: "ceiling x - floor x \<le> 1"
hoelzl@47592
   456
proof -
hoelzl@47592
   457
  have "of_int \<lceil>x\<rceil> - 1 < x" 
hoelzl@47592
   458
    using ceiling_correct[of x] by simp
hoelzl@47592
   459
  also have "x < of_int \<lfloor>x\<rfloor> + 1"
hoelzl@47592
   460
    using floor_correct[of x] by simp_all
hoelzl@47592
   461
  finally have "of_int (\<lceil>x\<rceil> - \<lfloor>x\<rfloor>) < (of_int 2::'a)"
hoelzl@47592
   462
    by simp
hoelzl@47592
   463
  then show ?thesis
hoelzl@47592
   464
    unfolding of_int_less_iff by simp
hoelzl@47592
   465
qed
huffman@30096
   466
huffman@30096
   467
subsection {* Negation *}
huffman@30096
   468
huffman@30102
   469
lemma floor_minus: "floor (- x) = - ceiling x"
huffman@30096
   470
  unfolding ceiling_def by simp
huffman@30096
   471
huffman@30102
   472
lemma ceiling_minus: "ceiling (- x) = - floor x"
huffman@30096
   473
  unfolding ceiling_def by simp
huffman@30096
   474
lp15@59613
   475
subsection {* Frac Function *}
lp15@59613
   476
lp15@59613
   477
lp15@59613
   478
definition frac :: "'a \<Rightarrow> 'a::floor_ceiling" where
lp15@59613
   479
  "frac x \<equiv> x - of_int \<lfloor>x\<rfloor>"
lp15@59613
   480
lp15@59613
   481
lemma frac_lt_1: "frac x < 1"
lp15@59613
   482
  by  (simp add: frac_def) linarith
lp15@59613
   483
lp15@59613
   484
lemma frac_eq_0_iff [simp]: "frac x = 0 \<longleftrightarrow> x \<in> Ints"
lp15@59613
   485
  by (simp add: frac_def) (metis Ints_cases Ints_of_int floor_of_int )
lp15@59613
   486
lp15@59613
   487
lemma frac_ge_0 [simp]: "frac x \<ge> 0"
lp15@59613
   488
  unfolding frac_def
lp15@59613
   489
  by linarith
lp15@59613
   490
lp15@59613
   491
lemma frac_gt_0_iff [simp]: "frac x > 0 \<longleftrightarrow> x \<notin> Ints"
lp15@59613
   492
  by (metis frac_eq_0_iff frac_ge_0 le_less less_irrefl)
lp15@59613
   493
lp15@59613
   494
lemma frac_of_int [simp]: "frac (of_int z) = 0"
lp15@59613
   495
  by (simp add: frac_def)
lp15@59613
   496
lp15@59613
   497
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
   498
proof -
lp15@59613
   499
  {assume "x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
lp15@59613
   500
   then have "\<lfloor>x + y\<rfloor> = \<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>"
lp15@59613
   501
     by (metis add.commute floor_unique le_floor_add le_floor_iff of_int_add)
lp15@59613
   502
   }
lp15@59613
   503
  moreover
lp15@59613
   504
  { assume "\<not> x + y < 1 + (of_int \<lfloor>x\<rfloor> + of_int \<lfloor>y\<rfloor>)"
lp15@59613
   505
    then have "\<lfloor>x + y\<rfloor> = 1 + (\<lfloor>x\<rfloor> + \<lfloor>y\<rfloor>)"
lp15@59613
   506
      apply (simp add: floor_unique_iff)
lp15@59613
   507
      apply (auto simp add: algebra_simps)
lp15@59613
   508
      by linarith    
lp15@59613
   509
  }
lp15@59613
   510
  ultimately show ?thesis
lp15@59613
   511
    by (auto simp add: frac_def algebra_simps)
lp15@59613
   512
qed
lp15@59613
   513
lp15@59613
   514
lemma frac_add: "frac (x + y) = (if frac x + frac y < 1 then frac x + frac y
lp15@59613
   515
                                 else (frac x + frac y) - 1)"  
lp15@59613
   516
  by (simp add: frac_def floor_add)
lp15@59613
   517
lp15@59613
   518
lemma frac_unique_iff:
lp15@59613
   519
  fixes x :: "'a::floor_ceiling"
lp15@59613
   520
  shows  "(frac x) = a \<longleftrightarrow> x - a \<in> Ints \<and> 0 \<le> a \<and> a < 1"
lp15@59613
   521
  apply (auto simp: Ints_def frac_def)
lp15@59613
   522
  apply linarith
lp15@59613
   523
  apply linarith
lp15@59613
   524
  by (metis (no_types) add.commute add.left_neutral eq_diff_eq floor_add_of_int floor_unique of_int_0)
lp15@59613
   525
lp15@59613
   526
lemma frac_eq: "(frac x) = x \<longleftrightarrow> 0 \<le> x \<and> x < 1"
lp15@59613
   527
  by (simp add: frac_unique_iff)
lp15@59613
   528
  
lp15@59613
   529
lemma frac_neg:
lp15@59613
   530
  fixes x :: "'a::floor_ceiling"
lp15@59613
   531
  shows  "frac (-x) = (if x \<in> Ints then 0 else 1 - frac x)"
lp15@59613
   532
  apply (auto simp add: frac_unique_iff)
lp15@59613
   533
  apply (simp add: frac_def)
lp15@59613
   534
  by (meson frac_lt_1 less_iff_diff_less_0 not_le not_less_iff_gr_or_eq)
lp15@59613
   535
huffman@30096
   536
end