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