src/HOL/ex/Landau.thy
author hoelzl
Thu Jan 31 11:31:27 2013 +0100 (2013-01-31)
changeset 50999 3de230ed0547
parent 41413 64cd30d6b0b8
permissions -rw-r--r--
introduce order topology
haftmann@31381
     1
haftmann@31381
     2
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@31381
     3
haftmann@31381
     4
header {* Comparing growth of functions on natural numbers by a preorder relation *}
haftmann@31381
     5
haftmann@31381
     6
theory Landau
wenzelm@41413
     7
imports Main "~~/src/HOL/Library/Preorder"
haftmann@31381
     8
begin
haftmann@31381
     9
haftmann@31381
    10
text {*
haftmann@36809
    11
  We establish a preorder releation @{text "\<lesssim>"} on functions from
haftmann@36809
    12
  @{text "\<nat>"} to @{text "\<nat>"} such that @{text "f \<lesssim> g \<longleftrightarrow> f \<in> O(g)"}.
haftmann@31381
    13
*}
haftmann@31381
    14
haftmann@31381
    15
subsection {* Auxiliary *}
haftmann@31381
    16
haftmann@31381
    17
lemma Ex_All_bounded:
haftmann@31381
    18
  fixes n :: nat
haftmann@31381
    19
  assumes "\<exists>n. \<forall>m\<ge>n. P m"
haftmann@31381
    20
  obtains m where "m \<ge> n" and "P m"
haftmann@31381
    21
proof -
haftmann@31381
    22
  from assms obtain q where m_q: "\<forall>m\<ge>q. P m" ..
haftmann@31381
    23
  let ?m = "max q n"
haftmann@31381
    24
  have "?m \<ge> n" by auto
haftmann@31381
    25
  moreover from m_q have "P ?m" by auto
haftmann@31381
    26
  ultimately show thesis ..
haftmann@31381
    27
qed
haftmann@31381
    28
    
haftmann@31381
    29
haftmann@31381
    30
subsection {* The @{text "\<lesssim>"} relation *}
haftmann@31381
    31
haftmann@31381
    32
definition less_eq_fun :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat) \<Rightarrow> bool" (infix "\<lesssim>" 50) where
haftmann@31381
    33
  "f \<lesssim> g \<longleftrightarrow> (\<exists>c n. \<forall>m\<ge>n. f m \<le> Suc c * g m)"
haftmann@31381
    34
haftmann@31381
    35
lemma less_eq_fun_intro:
haftmann@31381
    36
  assumes "\<exists>c n. \<forall>m\<ge>n. f m \<le> Suc c * g m"
haftmann@31381
    37
  shows "f \<lesssim> g"
haftmann@31381
    38
  unfolding less_eq_fun_def by (rule assms)
haftmann@31381
    39
haftmann@31381
    40
lemma less_eq_fun_not_intro:
haftmann@31381
    41
  assumes "\<And>c n. \<exists>m\<ge>n. Suc c * g m < f m"
haftmann@31381
    42
  shows "\<not> f \<lesssim> g"
haftmann@31381
    43
  using assms unfolding less_eq_fun_def linorder_not_le [symmetric]
haftmann@31381
    44
  by blast
haftmann@31381
    45
haftmann@31381
    46
lemma less_eq_fun_elim:
haftmann@31381
    47
  assumes "f \<lesssim> g"
haftmann@31381
    48
  obtains n c where "\<And>m. m \<ge> n \<Longrightarrow> f m \<le> Suc c * g m"
haftmann@31381
    49
  using assms unfolding less_eq_fun_def by blast
haftmann@31381
    50
haftmann@31381
    51
lemma less_eq_fun_not_elim:
haftmann@31381
    52
  assumes "\<not> f \<lesssim> g"
haftmann@31381
    53
  obtains m where "m \<ge> n" and "Suc c * g m < f m"
haftmann@31381
    54
  using assms unfolding less_eq_fun_def linorder_not_le [symmetric]
haftmann@31381
    55
  by blast
haftmann@31381
    56
haftmann@31381
    57
lemma less_eq_fun_refl:
haftmann@31381
    58
  "f \<lesssim> f"
haftmann@31381
    59
proof (rule less_eq_fun_intro)
haftmann@31381
    60
  have "\<exists>n. \<forall>m\<ge>n. f m \<le> Suc 0 * f m" by auto
haftmann@31381
    61
  then show "\<exists>c n. \<forall>m\<ge>n. f m \<le> Suc c * f m" by blast
haftmann@31381
    62
qed
haftmann@31381
    63
haftmann@31381
    64
lemma less_eq_fun_trans:
haftmann@31381
    65
  assumes f_g: "f \<lesssim> g" and g_h: "g \<lesssim> h"
haftmann@31381
    66
  shows f_h: "f \<lesssim> h"
haftmann@31381
    67
proof -
haftmann@31381
    68
  from f_g obtain n\<^isub>1 c\<^isub>1
haftmann@31381
    69
    where P1: "\<And>m. m \<ge> n\<^isub>1 \<Longrightarrow> f m \<le> Suc c\<^isub>1 * g m"
haftmann@31381
    70
  by (erule less_eq_fun_elim)
haftmann@31381
    71
  moreover from g_h obtain n\<^isub>2 c\<^isub>2
haftmann@31381
    72
    where P2: "\<And>m. m \<ge> n\<^isub>2 \<Longrightarrow> g m \<le> Suc c\<^isub>2 * h m"
haftmann@31381
    73
  by (erule less_eq_fun_elim)
haftmann@31381
    74
  ultimately have "\<And>m. m \<ge> max n\<^isub>1 n\<^isub>2 \<Longrightarrow> f m \<le> Suc c\<^isub>1 * g m \<and> g m \<le> Suc c\<^isub>2 * h m"
haftmann@31381
    75
  by auto
haftmann@31381
    76
  moreover {
haftmann@31381
    77
    fix k l r :: nat
haftmann@31381
    78
    assume k_l: "k \<le> Suc c\<^isub>1 * l" and l_r: "l \<le> Suc c\<^isub>2 * r"
haftmann@31381
    79
    from l_r have "Suc c\<^isub>1 * l \<le> (Suc c\<^isub>1 * Suc c\<^isub>2) * r"
haftmann@31381
    80
    by (auto simp add: mult_le_cancel_left mult_assoc simp del: times_nat.simps mult_Suc_right)
haftmann@31381
    81
    with k_l have "k \<le> (Suc c\<^isub>1 * Suc c\<^isub>2) * r" by (rule preorder_class.order_trans)
haftmann@31381
    82
  }
haftmann@31381
    83
  ultimately have "\<And>m. m \<ge> max n\<^isub>1 n\<^isub>2 \<Longrightarrow> f m \<le> (Suc c\<^isub>1 * Suc c\<^isub>2) * h m" by auto
haftmann@31381
    84
  then have "\<And>m. m \<ge> max n\<^isub>1 n\<^isub>2 \<Longrightarrow> f m \<le> Suc ((Suc c\<^isub>1 * Suc c\<^isub>2) - 1) * h m" by auto
haftmann@31381
    85
  then show ?thesis unfolding less_eq_fun_def by blast
haftmann@31381
    86
qed
haftmann@31381
    87
haftmann@31381
    88
haftmann@31381
    89
subsection {* The @{text "\<approx>"} relation, the equivalence relation induced by @{text "\<lesssim>"} *}
haftmann@31381
    90
haftmann@31381
    91
definition equiv_fun :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat) \<Rightarrow> bool" (infix "\<cong>" 50) where
haftmann@31381
    92
  "f \<cong> g \<longleftrightarrow> (\<exists>d c n. \<forall>m\<ge>n. g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m)"
haftmann@31381
    93
haftmann@31381
    94
lemma equiv_fun_intro:
haftmann@31381
    95
  assumes "\<exists>d c n. \<forall>m\<ge>n. g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m"
haftmann@31381
    96
  shows "f \<cong> g"
haftmann@31381
    97
  unfolding equiv_fun_def by (rule assms)
haftmann@31381
    98
haftmann@31381
    99
lemma equiv_fun_not_intro:
haftmann@31381
   100
  assumes "\<And>d c n. \<exists>m\<ge>n. Suc d * f m < g m \<or> Suc c * g m < f m"
haftmann@31381
   101
  shows "\<not> f \<cong> g"
haftmann@31381
   102
  unfolding equiv_fun_def
haftmann@31381
   103
  by (auto simp add: assms linorder_not_le
haftmann@31381
   104
    simp del: times_nat.simps mult_Suc_right)
haftmann@31381
   105
haftmann@31381
   106
lemma equiv_fun_elim:
haftmann@31381
   107
  assumes "f \<cong> g"
haftmann@31381
   108
  obtains n d c
haftmann@31381
   109
    where "\<And>m. m \<ge> n \<Longrightarrow> g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m"
haftmann@31381
   110
  using assms unfolding equiv_fun_def by blast
haftmann@31381
   111
haftmann@31381
   112
lemma equiv_fun_not_elim:
haftmann@31381
   113
  fixes n d c
haftmann@31381
   114
  assumes "\<not> f \<cong> g"
haftmann@31381
   115
  obtains m where "m \<ge> n"
haftmann@31381
   116
    and "Suc d * f m < g m \<or> Suc c * g m < f m"
haftmann@31381
   117
  using assms unfolding equiv_fun_def
haftmann@31381
   118
  by (auto simp add: linorder_not_le, blast)
haftmann@31381
   119
haftmann@31381
   120
lemma equiv_fun_less_eq_fun:
haftmann@31381
   121
  "f \<cong> g \<longleftrightarrow> f \<lesssim> g \<and> g \<lesssim> f"
haftmann@31381
   122
proof
haftmann@31381
   123
  assume x_y: "f \<cong> g"
haftmann@31381
   124
  then obtain n d c
haftmann@31381
   125
    where interv: "\<And>m. m \<ge> n \<Longrightarrow> g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m"
haftmann@31381
   126
  by (erule equiv_fun_elim)
haftmann@31381
   127
  from interv have "\<exists>c n. \<forall>m \<ge> n. f m \<le> Suc c * g m" by auto
haftmann@31381
   128
  then have f_g: "f \<lesssim> g" by (rule less_eq_fun_intro)
haftmann@31381
   129
  from interv have "\<exists>d n. \<forall>m \<ge> n. g m \<le> Suc d * f m" by auto
haftmann@31381
   130
  then have g_f: "g \<lesssim> f" by (rule less_eq_fun_intro)
haftmann@31381
   131
  from f_g g_f show "f \<lesssim> g \<and> g \<lesssim> f" by auto
haftmann@31381
   132
next
haftmann@31381
   133
  assume assm: "f \<lesssim> g \<and> g \<lesssim> f"
haftmann@31381
   134
  from assm less_eq_fun_elim obtain c n\<^isub>1 where
haftmann@31381
   135
    bound1: "\<And>m. m \<ge> n\<^isub>1 \<Longrightarrow> f m \<le> Suc c * g m" 
haftmann@31381
   136
    by blast
haftmann@31381
   137
  from assm less_eq_fun_elim obtain d n\<^isub>2 where
haftmann@31381
   138
    bound2: "\<And>m. m \<ge> n\<^isub>2 \<Longrightarrow> g m \<le> Suc d * f m"
haftmann@31381
   139
    by blast
haftmann@31381
   140
  from bound2 have "\<And>m. m \<ge> max n\<^isub>1 n\<^isub>2 \<Longrightarrow> g m \<le> Suc d * f m"
haftmann@31381
   141
  by auto
haftmann@31381
   142
  with bound1
haftmann@31381
   143
    have "\<forall>m \<ge> max n\<^isub>1 n\<^isub>2. g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m"
haftmann@31381
   144
    by auto
haftmann@31381
   145
  then
haftmann@31381
   146
    have "\<exists>d c n. \<forall>m\<ge>n. g m \<le> Suc d * f m \<and> f m \<le> Suc c * g m"
haftmann@31381
   147
    by blast
haftmann@31381
   148
  then show "f \<cong> g" by (rule equiv_fun_intro)
haftmann@31381
   149
qed
haftmann@31381
   150
haftmann@31381
   151
subsection {* The @{text "\<prec>"} relation, the strict part of @{text "\<lesssim>"} *}
haftmann@31381
   152
haftmann@31381
   153
definition less_fun :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat) \<Rightarrow> bool" (infix "\<prec>" 50) where
haftmann@31381
   154
  "f \<prec> g \<longleftrightarrow> f \<lesssim> g \<and> \<not> g \<lesssim> f"
haftmann@31381
   155
haftmann@31381
   156
lemma less_fun_intro:
haftmann@31381
   157
  assumes "\<And>c. \<exists>n. \<forall>m\<ge>n. Suc c * f m < g m"
haftmann@31381
   158
  shows "f \<prec> g"
haftmann@31381
   159
proof (unfold less_fun_def, rule conjI)
haftmann@31381
   160
  from assms obtain n
haftmann@31381
   161
    where "\<forall>m\<ge>n. Suc 0 * f m < g m" ..
haftmann@31381
   162
  then have "\<forall>m\<ge>n. f m \<le> Suc 0 * g m" by auto
haftmann@31381
   163
  then have "\<exists>c n. \<forall>m\<ge>n. f m \<le> Suc c * g m" by blast
haftmann@31381
   164
  then show "f \<lesssim> g" by (rule less_eq_fun_intro)
haftmann@31381
   165
next
haftmann@31381
   166
  show "\<not> g \<lesssim> f"
haftmann@31381
   167
  proof (rule less_eq_fun_not_intro)
haftmann@31381
   168
    fix c n :: nat
haftmann@31381
   169
    from assms have "\<exists>n. \<forall>m\<ge>n. Suc c * f m < g m" by blast
haftmann@31381
   170
    then obtain m where "m \<ge> n" and "Suc c * f m < g m"
haftmann@31381
   171
      by (rule Ex_All_bounded)
haftmann@31381
   172
    then show "\<exists>m\<ge>n. Suc c * f m < g m" by blast
haftmann@31381
   173
  qed
haftmann@31381
   174
qed
haftmann@31381
   175
haftmann@31381
   176
text {*
haftmann@31381
   177
  We would like to show (or refute) that @{text "f \<prec> g \<longleftrightarrow> f \<in> o(g)"},
haftmann@36809
   178
  i.e.~@{prop "f \<prec> g \<longleftrightarrow> (\<forall>c. \<exists>n. \<forall>m>n. f m < Suc c * g m)"} but did not
haftmann@36809
   179
  manage to do so.
haftmann@31381
   180
*}
haftmann@31381
   181
haftmann@31381
   182
haftmann@36809
   183
subsection {* Assert that @{text "\<lesssim>"} is indeed a preorder *}
haftmann@31381
   184
haftmann@31381
   185
interpretation fun_order: preorder_equiv less_eq_fun less_fun
haftmann@31381
   186
  where "preorder_equiv.equiv less_eq_fun = equiv_fun"
haftmann@31381
   187
proof -
haftmann@31381
   188
  interpret preorder_equiv less_eq_fun less_fun proof
haftmann@31381
   189
  qed (simp_all add: less_fun_def less_eq_fun_refl, auto intro: less_eq_fun_trans)
haftmann@36635
   190
  show "class.preorder_equiv less_eq_fun less_fun" using preorder_equiv_axioms .
haftmann@31381
   191
  show "preorder_equiv.equiv less_eq_fun = equiv_fun"
nipkow@39302
   192
    by (simp add: fun_eq_iff equiv_def equiv_fun_less_eq_fun)
haftmann@31381
   193
qed
haftmann@31381
   194
haftmann@31381
   195
haftmann@31381
   196
subsection {* Simple examples *}
haftmann@31381
   197
haftmann@31381
   198
lemma "(\<lambda>_. n) \<lesssim> (\<lambda>n. n)"
haftmann@31381
   199
proof (rule less_eq_fun_intro)
haftmann@31381
   200
  show "\<exists>c q. \<forall>m\<ge>q. n \<le> Suc c * m"
haftmann@31381
   201
  proof -
haftmann@31381
   202
    have "\<forall>m\<ge>n. n \<le> Suc 0 * m" by simp
haftmann@31381
   203
    then show ?thesis by blast
haftmann@31381
   204
  qed
haftmann@31381
   205
qed
haftmann@31381
   206
haftmann@31381
   207
lemma "(\<lambda>n. n) \<cong> (\<lambda>n. Suc k * n)"
haftmann@31381
   208
proof (rule equiv_fun_intro)
haftmann@31381
   209
  show "\<exists>d c n. \<forall>m\<ge>n. Suc k * m \<le> Suc d * m \<and> m \<le> Suc c * (Suc k * m)"
haftmann@31381
   210
  proof -
haftmann@31381
   211
    have "\<forall>m\<ge>n. Suc k * m \<le> Suc k * m \<and> m \<le> Suc c * (Suc k * m)" by simp
haftmann@31381
   212
    then show ?thesis by blast
haftmann@31381
   213
  qed
haftmann@31381
   214
qed  
haftmann@31381
   215
haftmann@31381
   216
lemma "(\<lambda>_. n) \<prec> (\<lambda>n. n)"
haftmann@31381
   217
proof (rule less_fun_intro)
haftmann@31381
   218
  fix c
haftmann@31381
   219
  show "\<exists>q. \<forall>m\<ge>q. Suc c * n < m"
haftmann@31381
   220
  proof -
haftmann@31381
   221
    have "\<forall>m\<ge>Suc c * n + 1. Suc c * n < m" by simp
haftmann@31381
   222
    then show ?thesis by blast
haftmann@31381
   223
  qed
haftmann@31381
   224
qed
haftmann@31381
   225
haftmann@31381
   226
end