src/HOL/Filter.thy
author hoelzl
Sun Apr 12 11:33:50 2015 +0200 (2015-04-12)
changeset 60039 d55937a8f97e
parent 60038 ca431cbce2a3
child 60040 1fa1023b13b9
permissions -rw-r--r--
add cofinite filter
     1 (*  Title:      HOL/Filter.thy
     2     Author:     Brian Huffman
     3     Author:     Johannes Hölzl
     4 *)
     5 
     6 section {* Filters on predicates *}
     7 
     8 theory Filter
     9 imports Set_Interval Lifting_Set
    10 begin
    11 
    12 subsection {* Filters *}
    13 
    14 text {*
    15   This definition also allows non-proper filters.
    16 *}
    17 
    18 locale is_filter =
    19   fixes F :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
    20   assumes True: "F (\<lambda>x. True)"
    21   assumes conj: "F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x) \<Longrightarrow> F (\<lambda>x. P x \<and> Q x)"
    22   assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x)"
    23 
    24 typedef 'a filter = "{F :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter F}"
    25 proof
    26   show "(\<lambda>x. True) \<in> ?filter" by (auto intro: is_filter.intro)
    27 qed
    28 
    29 lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
    30   using Rep_filter [of F] by simp
    31 
    32 lemma Abs_filter_inverse':
    33   assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
    34   using assms by (simp add: Abs_filter_inverse)
    35 
    36 
    37 subsubsection {* Eventually *}
    38 
    39 definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
    40   where "eventually P F \<longleftrightarrow> Rep_filter F P"
    41 
    42 syntax (xsymbols)
    43   "_eventually"  :: "pttrn => 'a filter => bool => bool"      ("(3\<forall>\<^sub>F _ in _./ _)" [0, 0, 10] 10)
    44 
    45 translations
    46   "\<forall>\<^sub>Fx in F. P" == "CONST eventually (\<lambda>x. P) F"
    47 
    48 lemma eventually_Abs_filter:
    49   assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
    50   unfolding eventually_def using assms by (simp add: Abs_filter_inverse)
    51 
    52 lemma filter_eq_iff:
    53   shows "F = F' \<longleftrightarrow> (\<forall>P. eventually P F = eventually P F')"
    54   unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..
    55 
    56 lemma eventually_True [simp]: "eventually (\<lambda>x. True) F"
    57   unfolding eventually_def
    58   by (rule is_filter.True [OF is_filter_Rep_filter])
    59 
    60 lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P F"
    61 proof -
    62   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
    63   thus "eventually P F" by simp
    64 qed
    65 
    66 lemma eventually_mono:
    67   "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P F \<Longrightarrow> eventually Q F"
    68   unfolding eventually_def
    69   by (rule is_filter.mono [OF is_filter_Rep_filter])
    70 
    71 lemma eventually_conj:
    72   assumes P: "eventually (\<lambda>x. P x) F"
    73   assumes Q: "eventually (\<lambda>x. Q x) F"
    74   shows "eventually (\<lambda>x. P x \<and> Q x) F"
    75   using assms unfolding eventually_def
    76   by (rule is_filter.conj [OF is_filter_Rep_filter])
    77 
    78 lemma eventually_Ball_finite:
    79   assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
    80   shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
    81 using assms by (induct set: finite, simp, simp add: eventually_conj)
    82 
    83 lemma eventually_all_finite:
    84   fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
    85   assumes "\<And>y. eventually (\<lambda>x. P x y) net"
    86   shows "eventually (\<lambda>x. \<forall>y. P x y) net"
    87 using eventually_Ball_finite [of UNIV P] assms by simp
    88 
    89 lemma eventually_mp:
    90   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
    91   assumes "eventually (\<lambda>x. P x) F"
    92   shows "eventually (\<lambda>x. Q x) F"
    93 proof (rule eventually_mono)
    94   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
    95   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
    96     using assms by (rule eventually_conj)
    97 qed
    98 
    99 lemma eventually_rev_mp:
   100   assumes "eventually (\<lambda>x. P x) F"
   101   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   102   shows "eventually (\<lambda>x. Q x) F"
   103 using assms(2) assms(1) by (rule eventually_mp)
   104 
   105 lemma eventually_conj_iff:
   106   "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
   107   by (auto intro: eventually_conj elim: eventually_rev_mp)
   108 
   109 lemma eventually_elim1:
   110   assumes "eventually (\<lambda>i. P i) F"
   111   assumes "\<And>i. P i \<Longrightarrow> Q i"
   112   shows "eventually (\<lambda>i. Q i) F"
   113   using assms by (auto elim!: eventually_rev_mp)
   114 
   115 lemma eventually_elim2:
   116   assumes "eventually (\<lambda>i. P i) F"
   117   assumes "eventually (\<lambda>i. Q i) F"
   118   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
   119   shows "eventually (\<lambda>i. R i) F"
   120   using assms by (auto elim!: eventually_rev_mp)
   121 
   122 lemma not_eventually_impI: "eventually P F \<Longrightarrow> \<not> eventually Q F \<Longrightarrow> \<not> eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   123   by (auto intro: eventually_mp)
   124 
   125 lemma not_eventuallyD: "\<not> eventually P F \<Longrightarrow> \<exists>x. \<not> P x"
   126   by (metis always_eventually)
   127 
   128 lemma eventually_subst:
   129   assumes "eventually (\<lambda>n. P n = Q n) F"
   130   shows "eventually P F = eventually Q F" (is "?L = ?R")
   131 proof -
   132   from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   133       and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
   134     by (auto elim: eventually_elim1)
   135   then show ?thesis by (auto elim: eventually_elim2)
   136 qed
   137 
   138 ML {*
   139   fun eventually_elim_tac ctxt facts = SUBGOAL_CASES (fn (goal, i) =>
   140     let
   141       val mp_thms = facts RL @{thms eventually_rev_mp}
   142       val raw_elim_thm =
   143         (@{thm allI} RS @{thm always_eventually})
   144         |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
   145         |> fold (fn _ => fn thm => @{thm impI} RS thm) facts
   146       val cases_prop = Thm.prop_of (raw_elim_thm RS Goal.init (Thm.cterm_of ctxt goal))
   147       val cases = Rule_Cases.make_common ctxt cases_prop [(("elim", []), [])]
   148     in
   149       CASES cases (rtac raw_elim_thm i)
   150     end)
   151 *}
   152 
   153 method_setup eventually_elim = {*
   154   Scan.succeed (fn ctxt => METHOD_CASES (HEADGOAL o eventually_elim_tac ctxt))
   155 *} "elimination of eventually quantifiers"
   156 
   157 subsection \<open> Frequently as dual to eventually \<close>
   158 
   159 definition frequently :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
   160   where "frequently P F \<longleftrightarrow> \<not> eventually (\<lambda>x. \<not> P x) F"
   161 
   162 syntax (xsymbols)
   163   "_frequently"  :: "pttrn \<Rightarrow> 'a filter \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>\<^sub>F _ in _./ _)" [0, 0, 10] 10)
   164 
   165 translations
   166   "\<exists>\<^sub>Fx in F. P" == "CONST frequently (\<lambda>x. P) F"
   167 
   168 lemma not_frequently_False [simp]: "\<not> frequently (\<lambda>x. False) F"
   169   by (simp add: frequently_def)
   170 
   171 lemma frequently_ex: "frequently P F \<Longrightarrow> \<exists>x. P x"
   172   by (auto simp: frequently_def dest: not_eventuallyD)
   173 
   174 lemma frequently_mp:
   175   assumes ev: "eventually (\<lambda>x. P x \<longrightarrow> Q x) F" and P: "frequently (\<lambda>x. P x) F"
   176   shows "frequently (\<lambda>x. Q x) F"
   177 proof - 
   178   from ev have "eventually (\<lambda>x. \<not> Q x \<longrightarrow> \<not> P x) F"
   179     by (rule eventually_rev_mp) (auto intro!: always_eventually)
   180   from eventually_mp[OF this] P show ?thesis
   181     by (auto simp: frequently_def)
   182 qed
   183 
   184 lemma frequently_rev_mp:
   185   assumes "frequently (\<lambda>x. P x) F"
   186   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   187   shows "frequently (\<lambda>x. Q x) F"
   188 using assms(2) assms(1) by (rule frequently_mp)
   189 
   190 lemma frequently_mono: "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> frequently P F \<Longrightarrow> frequently Q F"
   191   using frequently_mp[of P Q] by (simp add: always_eventually)
   192 
   193 lemma frequently_disj_iff:
   194   "frequently (\<lambda>x. P x \<or> Q x) F \<longleftrightarrow> frequently (\<lambda>x. P x) F \<or> frequently (\<lambda>x. Q x) F"
   195   by (simp add: frequently_def eventually_conj_iff)
   196 
   197 lemma frequently_disj:
   198   "frequently (\<lambda>x. P x) F \<Longrightarrow> frequently (\<lambda>x. Q x) F \<Longrightarrow> frequently (\<lambda>x. P x \<or> Q x) F"
   199   by (simp add: frequently_disj_iff)
   200 
   201 lemma frequently_Bex_finite:
   202   assumes "finite A" shows "frequently (\<lambda>x. \<exists>y\<in>A. P x y) net \<longleftrightarrow> (\<exists>y\<in>A. frequently (\<lambda>x. P x y) net)"
   203   using assms by induction (auto simp: frequently_disj_iff)
   204 
   205 subsubsection {* Finer-than relation *}
   206 
   207 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
   208 filter @{term F'}. *}
   209 
   210 instantiation filter :: (type) complete_lattice
   211 begin
   212 
   213 definition le_filter_def:
   214   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
   215 
   216 definition
   217   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   218 
   219 definition
   220   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
   221 
   222 definition
   223   "bot = Abs_filter (\<lambda>P. True)"
   224 
   225 definition
   226   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
   227 
   228 definition
   229   "inf F F' = Abs_filter
   230       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   231 
   232 definition
   233   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
   234 
   235 definition
   236   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
   237 
   238 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   239   unfolding top_filter_def
   240   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
   241 
   242 lemma eventually_bot [simp]: "eventually P bot"
   243   unfolding bot_filter_def
   244   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
   245 
   246 lemma eventually_sup:
   247   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
   248   unfolding sup_filter_def
   249   by (rule eventually_Abs_filter, rule is_filter.intro)
   250      (auto elim!: eventually_rev_mp)
   251 
   252 lemma eventually_inf:
   253   "eventually P (inf F F') \<longleftrightarrow>
   254    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   255   unfolding inf_filter_def
   256   apply (rule eventually_Abs_filter, rule is_filter.intro)
   257   apply (fast intro: eventually_True)
   258   apply clarify
   259   apply (intro exI conjI)
   260   apply (erule (1) eventually_conj)
   261   apply (erule (1) eventually_conj)
   262   apply simp
   263   apply auto
   264   done
   265 
   266 lemma eventually_Sup:
   267   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
   268   unfolding Sup_filter_def
   269   apply (rule eventually_Abs_filter, rule is_filter.intro)
   270   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   271   done
   272 
   273 instance proof
   274   fix F F' F'' :: "'a filter" and S :: "'a filter set"
   275   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   276     by (rule less_filter_def) }
   277   { show "F \<le> F"
   278     unfolding le_filter_def by simp }
   279   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
   280     unfolding le_filter_def by simp }
   281   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
   282     unfolding le_filter_def filter_eq_iff by fast }
   283   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
   284     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
   285   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
   286     unfolding le_filter_def eventually_inf
   287     by (auto elim!: eventually_mono intro: eventually_conj) }
   288   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
   289     unfolding le_filter_def eventually_sup by simp_all }
   290   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
   291     unfolding le_filter_def eventually_sup by simp }
   292   { assume "F'' \<in> S" thus "Inf S \<le> F''"
   293     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   294   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
   295     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   296   { assume "F \<in> S" thus "F \<le> Sup S"
   297     unfolding le_filter_def eventually_Sup by simp }
   298   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
   299     unfolding le_filter_def eventually_Sup by simp }
   300   { show "Inf {} = (top::'a filter)"
   301     by (auto simp: top_filter_def Inf_filter_def Sup_filter_def)
   302       (metis (full_types) top_filter_def always_eventually eventually_top) }
   303   { show "Sup {} = (bot::'a filter)"
   304     by (auto simp: bot_filter_def Sup_filter_def) }
   305 qed
   306 
   307 end
   308 
   309 lemma filter_leD:
   310   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
   311   unfolding le_filter_def by simp
   312 
   313 lemma filter_leI:
   314   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
   315   unfolding le_filter_def by simp
   316 
   317 lemma eventually_False:
   318   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
   319   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
   320 
   321 abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
   322   where "trivial_limit F \<equiv> F = bot"
   323 
   324 lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
   325   by (rule eventually_False [symmetric])
   326 
   327 lemma eventually_const: "\<not> trivial_limit net \<Longrightarrow> eventually (\<lambda>x. P) net \<longleftrightarrow> P"
   328   by (cases P) (simp_all add: eventually_False)
   329 
   330 lemma eventually_Inf: "eventually P (Inf B) \<longleftrightarrow> (\<exists>X\<subseteq>B. finite X \<and> eventually P (Inf X))"
   331 proof -
   332   let ?F = "\<lambda>P. \<exists>X\<subseteq>B. finite X \<and> eventually P (Inf X)"
   333   
   334   { fix P have "eventually P (Abs_filter ?F) \<longleftrightarrow> ?F P"
   335     proof (rule eventually_Abs_filter is_filter.intro)+
   336       show "?F (\<lambda>x. True)"
   337         by (rule exI[of _ "{}"]) (simp add: le_fun_def)
   338     next
   339       fix P Q
   340       assume "?F P" then guess X ..
   341       moreover
   342       assume "?F Q" then guess Y ..
   343       ultimately show "?F (\<lambda>x. P x \<and> Q x)"
   344         by (intro exI[of _ "X \<union> Y"])
   345            (auto simp: Inf_union_distrib eventually_inf)
   346     next
   347       fix P Q
   348       assume "?F P" then guess X ..
   349       moreover assume "\<forall>x. P x \<longrightarrow> Q x"
   350       ultimately show "?F Q"
   351         by (intro exI[of _ X]) (auto elim: eventually_elim1)
   352     qed }
   353   note eventually_F = this
   354 
   355   have "Inf B = Abs_filter ?F"
   356   proof (intro antisym Inf_greatest)
   357     show "Inf B \<le> Abs_filter ?F"
   358       by (auto simp: le_filter_def eventually_F dest: Inf_superset_mono)
   359   next
   360     fix F assume "F \<in> B" then show "Abs_filter ?F \<le> F"
   361       by (auto simp add: le_filter_def eventually_F intro!: exI[of _ "{F}"])
   362   qed
   363   then show ?thesis
   364     by (simp add: eventually_F)
   365 qed
   366 
   367 lemma eventually_INF: "eventually P (INF b:B. F b) \<longleftrightarrow> (\<exists>X\<subseteq>B. finite X \<and> eventually P (INF b:X. F b))"
   368   unfolding INF_def[of B] eventually_Inf[of P "F`B"]
   369   by (metis Inf_image_eq finite_imageI image_mono finite_subset_image)
   370 
   371 lemma Inf_filter_not_bot:
   372   fixes B :: "'a filter set"
   373   shows "(\<And>X. X \<subseteq> B \<Longrightarrow> finite X \<Longrightarrow> Inf X \<noteq> bot) \<Longrightarrow> Inf B \<noteq> bot"
   374   unfolding trivial_limit_def eventually_Inf[of _ B]
   375     bot_bool_def [symmetric] bot_fun_def [symmetric] bot_unique by simp
   376 
   377 lemma INF_filter_not_bot:
   378   fixes F :: "'i \<Rightarrow> 'a filter"
   379   shows "(\<And>X. X \<subseteq> B \<Longrightarrow> finite X \<Longrightarrow> (INF b:X. F b) \<noteq> bot) \<Longrightarrow> (INF b:B. F b) \<noteq> bot"
   380   unfolding trivial_limit_def eventually_INF[of _ B]
   381     bot_bool_def [symmetric] bot_fun_def [symmetric] bot_unique by simp
   382 
   383 lemma eventually_Inf_base:
   384   assumes "B \<noteq> {}" and base: "\<And>F G. F \<in> B \<Longrightarrow> G \<in> B \<Longrightarrow> \<exists>x\<in>B. x \<le> inf F G"
   385   shows "eventually P (Inf B) \<longleftrightarrow> (\<exists>b\<in>B. eventually P b)"
   386 proof (subst eventually_Inf, safe)
   387   fix X assume "finite X" "X \<subseteq> B"
   388   then have "\<exists>b\<in>B. \<forall>x\<in>X. b \<le> x"
   389   proof induct
   390     case empty then show ?case
   391       using `B \<noteq> {}` by auto
   392   next
   393     case (insert x X)
   394     then obtain b where "b \<in> B" "\<And>x. x \<in> X \<Longrightarrow> b \<le> x"
   395       by auto
   396     with `insert x X \<subseteq> B` base[of b x] show ?case
   397       by (auto intro: order_trans)
   398   qed
   399   then obtain b where "b \<in> B" "b \<le> Inf X"
   400     by (auto simp: le_Inf_iff)
   401   then show "eventually P (Inf X) \<Longrightarrow> Bex B (eventually P)"
   402     by (intro bexI[of _ b]) (auto simp: le_filter_def)
   403 qed (auto intro!: exI[of _ "{x}" for x])
   404 
   405 lemma eventually_INF_base:
   406   "B \<noteq> {} \<Longrightarrow> (\<And>a b. a \<in> B \<Longrightarrow> b \<in> B \<Longrightarrow> \<exists>x\<in>B. F x \<le> inf (F a) (F b)) \<Longrightarrow>
   407     eventually P (INF b:B. F b) \<longleftrightarrow> (\<exists>b\<in>B. eventually P (F b))"
   408   unfolding INF_def by (subst eventually_Inf_base) auto
   409 
   410 
   411 subsubsection {* Map function for filters *}
   412 
   413 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
   414   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
   415 
   416 lemma eventually_filtermap:
   417   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
   418   unfolding filtermap_def
   419   apply (rule eventually_Abs_filter)
   420   apply (rule is_filter.intro)
   421   apply (auto elim!: eventually_rev_mp)
   422   done
   423 
   424 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
   425   by (simp add: filter_eq_iff eventually_filtermap)
   426 
   427 lemma filtermap_filtermap:
   428   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
   429   by (simp add: filter_eq_iff eventually_filtermap)
   430 
   431 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
   432   unfolding le_filter_def eventually_filtermap by simp
   433 
   434 lemma filtermap_bot [simp]: "filtermap f bot = bot"
   435   by (simp add: filter_eq_iff eventually_filtermap)
   436 
   437 lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
   438   by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)
   439 
   440 lemma filtermap_inf: "filtermap f (inf F1 F2) \<le> inf (filtermap f F1) (filtermap f F2)"
   441   by (auto simp: le_filter_def eventually_filtermap eventually_inf)
   442 
   443 lemma filtermap_INF: "filtermap f (INF b:B. F b) \<le> (INF b:B. filtermap f (F b))"
   444 proof -
   445   { fix X :: "'c set" assume "finite X"
   446     then have "filtermap f (INFIMUM X F) \<le> (INF b:X. filtermap f (F b))"
   447     proof induct
   448       case (insert x X)
   449       have "filtermap f (INF a:insert x X. F a) \<le> inf (filtermap f (F x)) (filtermap f (INF a:X. F a))"
   450         by (rule order_trans[OF _ filtermap_inf]) simp
   451       also have "\<dots> \<le> inf (filtermap f (F x)) (INF a:X. filtermap f (F a))"
   452         by (intro inf_mono insert order_refl)
   453       finally show ?case
   454         by simp
   455     qed simp }
   456   then show ?thesis
   457     unfolding le_filter_def eventually_filtermap
   458     by (subst (1 2) eventually_INF) auto
   459 qed
   460 subsubsection {* Standard filters *}
   461 
   462 definition principal :: "'a set \<Rightarrow> 'a filter" where
   463   "principal S = Abs_filter (\<lambda>P. \<forall>x\<in>S. P x)"
   464 
   465 lemma eventually_principal: "eventually P (principal S) \<longleftrightarrow> (\<forall>x\<in>S. P x)"
   466   unfolding principal_def
   467   by (rule eventually_Abs_filter, rule is_filter.intro) auto
   468 
   469 lemma eventually_inf_principal: "eventually P (inf F (principal s)) \<longleftrightarrow> eventually (\<lambda>x. x \<in> s \<longrightarrow> P x) F"
   470   unfolding eventually_inf eventually_principal by (auto elim: eventually_elim1)
   471 
   472 lemma principal_UNIV[simp]: "principal UNIV = top"
   473   by (auto simp: filter_eq_iff eventually_principal)
   474 
   475 lemma principal_empty[simp]: "principal {} = bot"
   476   by (auto simp: filter_eq_iff eventually_principal)
   477 
   478 lemma principal_eq_bot_iff: "principal X = bot \<longleftrightarrow> X = {}"
   479   by (auto simp add: filter_eq_iff eventually_principal)
   480 
   481 lemma principal_le_iff[iff]: "principal A \<le> principal B \<longleftrightarrow> A \<subseteq> B"
   482   by (auto simp: le_filter_def eventually_principal)
   483 
   484 lemma le_principal: "F \<le> principal A \<longleftrightarrow> eventually (\<lambda>x. x \<in> A) F"
   485   unfolding le_filter_def eventually_principal
   486   apply safe
   487   apply (erule_tac x="\<lambda>x. x \<in> A" in allE)
   488   apply (auto elim: eventually_elim1)
   489   done
   490 
   491 lemma principal_inject[iff]: "principal A = principal B \<longleftrightarrow> A = B"
   492   unfolding eq_iff by simp
   493 
   494 lemma sup_principal[simp]: "sup (principal A) (principal B) = principal (A \<union> B)"
   495   unfolding filter_eq_iff eventually_sup eventually_principal by auto
   496 
   497 lemma inf_principal[simp]: "inf (principal A) (principal B) = principal (A \<inter> B)"
   498   unfolding filter_eq_iff eventually_inf eventually_principal
   499   by (auto intro: exI[of _ "\<lambda>x. x \<in> A"] exI[of _ "\<lambda>x. x \<in> B"])
   500 
   501 lemma SUP_principal[simp]: "(SUP i : I. principal (A i)) = principal (\<Union>i\<in>I. A i)"
   502   unfolding filter_eq_iff eventually_Sup SUP_def by (auto simp: eventually_principal)
   503 
   504 lemma INF_principal_finite: "finite X \<Longrightarrow> (INF x:X. principal (f x)) = principal (\<Inter>x\<in>X. f x)"
   505   by (induct X rule: finite_induct) auto
   506 
   507 lemma filtermap_principal[simp]: "filtermap f (principal A) = principal (f ` A)"
   508   unfolding filter_eq_iff eventually_filtermap eventually_principal by simp
   509 
   510 subsubsection {* Order filters *}
   511 
   512 definition at_top :: "('a::order) filter"
   513   where "at_top = (INF k. principal {k ..})"
   514 
   515 lemma at_top_sub: "at_top = (INF k:{c::'a::linorder..}. principal {k ..})"
   516   by (auto intro!: INF_eq max.cobounded1 max.cobounded2 simp: at_top_def)
   517 
   518 lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
   519   unfolding at_top_def
   520   by (subst eventually_INF_base) (auto simp: eventually_principal intro: max.cobounded1 max.cobounded2)
   521 
   522 lemma eventually_ge_at_top:
   523   "eventually (\<lambda>x. (c::_::linorder) \<le> x) at_top"
   524   unfolding eventually_at_top_linorder by auto
   525 
   526 lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::{no_top, linorder}. \<forall>n>N. P n)"
   527 proof -
   528   have "eventually P (INF k. principal {k <..}) \<longleftrightarrow> (\<exists>N::'a. \<forall>n>N. P n)"
   529     by (subst eventually_INF_base) (auto simp: eventually_principal intro: max.cobounded1 max.cobounded2)
   530   also have "(INF k. principal {k::'a <..}) = at_top"
   531     unfolding at_top_def 
   532     by (intro INF_eq) (auto intro: less_imp_le simp: Ici_subset_Ioi_iff gt_ex)
   533   finally show ?thesis .
   534 qed
   535 
   536 lemma eventually_gt_at_top:
   537   "eventually (\<lambda>x. (c::_::unbounded_dense_linorder) < x) at_top"
   538   unfolding eventually_at_top_dense by auto
   539 
   540 definition at_bot :: "('a::order) filter"
   541   where "at_bot = (INF k. principal {.. k})"
   542 
   543 lemma at_bot_sub: "at_bot = (INF k:{.. c::'a::linorder}. principal {.. k})"
   544   by (auto intro!: INF_eq min.cobounded1 min.cobounded2 simp: at_bot_def)
   545 
   546 lemma eventually_at_bot_linorder:
   547   fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
   548   unfolding at_bot_def
   549   by (subst eventually_INF_base) (auto simp: eventually_principal intro: min.cobounded1 min.cobounded2)
   550 
   551 lemma eventually_le_at_bot:
   552   "eventually (\<lambda>x. x \<le> (c::_::linorder)) at_bot"
   553   unfolding eventually_at_bot_linorder by auto
   554 
   555 lemma eventually_at_bot_dense: "eventually P at_bot \<longleftrightarrow> (\<exists>N::'a::{no_bot, linorder}. \<forall>n<N. P n)"
   556 proof -
   557   have "eventually P (INF k. principal {..< k}) \<longleftrightarrow> (\<exists>N::'a. \<forall>n<N. P n)"
   558     by (subst eventually_INF_base) (auto simp: eventually_principal intro: min.cobounded1 min.cobounded2)
   559   also have "(INF k. principal {..< k::'a}) = at_bot"
   560     unfolding at_bot_def 
   561     by (intro INF_eq) (auto intro: less_imp_le simp: Iic_subset_Iio_iff lt_ex)
   562   finally show ?thesis .
   563 qed
   564 
   565 lemma eventually_gt_at_bot:
   566   "eventually (\<lambda>x. x < (c::_::unbounded_dense_linorder)) at_bot"
   567   unfolding eventually_at_bot_dense by auto
   568 
   569 lemma trivial_limit_at_bot_linorder: "\<not> trivial_limit (at_bot ::('a::linorder) filter)"
   570   unfolding trivial_limit_def
   571   by (metis eventually_at_bot_linorder order_refl)
   572 
   573 lemma trivial_limit_at_top_linorder: "\<not> trivial_limit (at_top ::('a::linorder) filter)"
   574   unfolding trivial_limit_def
   575   by (metis eventually_at_top_linorder order_refl)
   576 
   577 subsection {* Sequentially *}
   578 
   579 abbreviation sequentially :: "nat filter"
   580   where "sequentially \<equiv> at_top"
   581 
   582 lemma eventually_sequentially:
   583   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   584   by (rule eventually_at_top_linorder)
   585 
   586 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
   587   unfolding filter_eq_iff eventually_sequentially by auto
   588 
   589 lemmas trivial_limit_sequentially = sequentially_bot
   590 
   591 lemma eventually_False_sequentially [simp]:
   592   "\<not> eventually (\<lambda>n. False) sequentially"
   593   by (simp add: eventually_False)
   594 
   595 lemma le_sequentially:
   596   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   597   by (simp add: at_top_def le_INF_iff le_principal)
   598 
   599 lemma eventually_sequentiallyI:
   600   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
   601   shows "eventually P sequentially"
   602 using assms by (auto simp: eventually_sequentially)
   603 
   604 lemma eventually_sequentially_seg:
   605   "eventually (\<lambda>n. P (n + k)) sequentially \<longleftrightarrow> eventually P sequentially"
   606   unfolding eventually_sequentially
   607   apply safe
   608    apply (rule_tac x="N + k" in exI)
   609    apply rule
   610    apply (erule_tac x="n - k" in allE)
   611    apply auto []
   612   apply (rule_tac x=N in exI)
   613   apply auto []
   614   done
   615 
   616 subsection \<open> The cofinite filter \<close>
   617 
   618 definition "cofinite = Abs_filter (\<lambda>P. finite {x. \<not> P x})"
   619 
   620 lemma eventually_cofinite: "eventually P cofinite \<longleftrightarrow> finite {x. \<not> P x}"
   621   unfolding cofinite_def
   622 proof (rule eventually_Abs_filter, rule is_filter.intro)
   623   fix P Q :: "'a \<Rightarrow> bool" assume "finite {x. \<not> P x}" "finite {x. \<not> Q x}"
   624   from finite_UnI[OF this] show "finite {x. \<not> (P x \<and> Q x)}"
   625     by (rule rev_finite_subset) auto
   626 next
   627   fix P Q :: "'a \<Rightarrow> bool" assume P: "finite {x. \<not> P x}" and *: "\<forall>x. P x \<longrightarrow> Q x"
   628   from * show "finite {x. \<not> Q x}"
   629     by (intro finite_subset[OF _ P]) auto
   630 qed simp
   631 
   632 lemma cofinite_bot[simp]: "cofinite = (bot::'a filter) \<longleftrightarrow> finite (UNIV :: 'a set)"
   633   unfolding trivial_limit_def eventually_cofinite by simp
   634 
   635 lemma cofinite_eq_sequentially: "cofinite = sequentially"
   636   unfolding filter_eq_iff eventually_sequentially eventually_cofinite
   637 proof safe
   638   fix P :: "nat \<Rightarrow> bool" assume [simp]: "finite {x. \<not> P x}"
   639   show "\<exists>N. \<forall>n\<ge>N. P n"
   640   proof cases
   641     assume "{x. \<not> P x} \<noteq> {}" then show ?thesis
   642       by (intro exI[of _ "Suc (Max {x. \<not> P x})"]) (auto simp: Suc_le_eq)
   643   qed auto
   644 next
   645   fix P :: "nat \<Rightarrow> bool" and N :: nat assume "\<forall>n\<ge>N. P n"
   646   then have "{x. \<not> P x} \<subseteq> {..< N}"
   647     by (auto simp: not_le)
   648   then show "finite {x. \<not> P x}"
   649     by (blast intro: finite_subset)
   650 qed
   651 
   652 subsection {* Limits *}
   653 
   654 definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
   655   "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
   656 
   657 syntax
   658   "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
   659 
   660 translations
   661   "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
   662 
   663 lemma filterlim_iff:
   664   "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
   665   unfolding filterlim_def le_filter_def eventually_filtermap ..
   666 
   667 lemma filterlim_compose:
   668   "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
   669   unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
   670 
   671 lemma filterlim_mono:
   672   "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
   673   unfolding filterlim_def by (metis filtermap_mono order_trans)
   674 
   675 lemma filterlim_ident: "LIM x F. x :> F"
   676   by (simp add: filterlim_def filtermap_ident)
   677 
   678 lemma filterlim_cong:
   679   "F1 = F1' \<Longrightarrow> F2 = F2' \<Longrightarrow> eventually (\<lambda>x. f x = g x) F2 \<Longrightarrow> filterlim f F1 F2 = filterlim g F1' F2'"
   680   by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)
   681 
   682 lemma filterlim_mono_eventually:
   683   assumes "filterlim f F G" and ord: "F \<le> F'" "G' \<le> G"
   684   assumes eq: "eventually (\<lambda>x. f x = f' x) G'"
   685   shows "filterlim f' F' G'"
   686   apply (rule filterlim_cong[OF refl refl eq, THEN iffD1])
   687   apply (rule filterlim_mono[OF _ ord])
   688   apply fact
   689   done
   690 
   691 lemma filtermap_mono_strong: "inj f \<Longrightarrow> filtermap f F \<le> filtermap f G \<longleftrightarrow> F \<le> G"
   692   apply (auto intro!: filtermap_mono) []
   693   apply (auto simp: le_filter_def eventually_filtermap)
   694   apply (erule_tac x="\<lambda>x. P (inv f x)" in allE)
   695   apply auto
   696   done
   697 
   698 lemma filtermap_eq_strong: "inj f \<Longrightarrow> filtermap f F = filtermap f G \<longleftrightarrow> F = G"
   699   by (simp add: filtermap_mono_strong eq_iff)
   700 
   701 lemma filterlim_principal:
   702   "(LIM x F. f x :> principal S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F)"
   703   unfolding filterlim_def eventually_filtermap le_principal ..
   704 
   705 lemma filterlim_inf:
   706   "(LIM x F1. f x :> inf F2 F3) \<longleftrightarrow> ((LIM x F1. f x :> F2) \<and> (LIM x F1. f x :> F3))"
   707   unfolding filterlim_def by simp
   708 
   709 lemma filterlim_INF:
   710   "(LIM x F. f x :> (INF b:B. G b)) \<longleftrightarrow> (\<forall>b\<in>B. LIM x F. f x :> G b)"
   711   unfolding filterlim_def le_INF_iff ..
   712 
   713 lemma filterlim_INF_INF:
   714   "(\<And>m. m \<in> J \<Longrightarrow> \<exists>i\<in>I. filtermap f (F i) \<le> G m) \<Longrightarrow> LIM x (INF i:I. F i). f x :> (INF j:J. G j)"
   715   unfolding filterlim_def by (rule order_trans[OF filtermap_INF INF_mono])
   716 
   717 lemma filterlim_base:
   718   "(\<And>m x. m \<in> J \<Longrightarrow> i m \<in> I) \<Longrightarrow> (\<And>m x. m \<in> J \<Longrightarrow> x \<in> F (i m) \<Longrightarrow> f x \<in> G m) \<Longrightarrow> 
   719     LIM x (INF i:I. principal (F i)). f x :> (INF j:J. principal (G j))"
   720   by (force intro!: filterlim_INF_INF simp: image_subset_iff)
   721 
   722 lemma filterlim_base_iff: 
   723   assumes "I \<noteq> {}" and chain: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> F i \<subseteq> F j \<or> F j \<subseteq> F i"
   724   shows "(LIM x (INF i:I. principal (F i)). f x :> INF j:J. principal (G j)) \<longleftrightarrow>
   725     (\<forall>j\<in>J. \<exists>i\<in>I. \<forall>x\<in>F i. f x \<in> G j)"
   726   unfolding filterlim_INF filterlim_principal
   727 proof (subst eventually_INF_base)
   728   fix i j assume "i \<in> I" "j \<in> I"
   729   with chain[OF this] show "\<exists>x\<in>I. principal (F x) \<le> inf (principal (F i)) (principal (F j))"
   730     by auto
   731 qed (auto simp: eventually_principal `I \<noteq> {}`)
   732 
   733 lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (\<lambda>x. f (g x)) F1 F2"
   734   unfolding filterlim_def filtermap_filtermap ..
   735 
   736 lemma filterlim_sup:
   737   "filterlim f F F1 \<Longrightarrow> filterlim f F F2 \<Longrightarrow> filterlim f F (sup F1 F2)"
   738   unfolding filterlim_def filtermap_sup by auto
   739 
   740 lemma eventually_sequentially_Suc: "eventually (\<lambda>i. P (Suc i)) sequentially \<longleftrightarrow> eventually P sequentially"
   741   unfolding eventually_sequentially by (metis Suc_le_D Suc_le_mono le_Suc_eq)
   742 
   743 lemma filterlim_sequentially_Suc:
   744   "(LIM x sequentially. f (Suc x) :> F) \<longleftrightarrow> (LIM x sequentially. f x :> F)"
   745   unfolding filterlim_iff by (subst eventually_sequentially_Suc) simp
   746 
   747 lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
   748   by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)
   749 
   750 
   751 subsection {* Limits to @{const at_top} and @{const at_bot} *}
   752 
   753 lemma filterlim_at_top:
   754   fixes f :: "'a \<Rightarrow> ('b::linorder)"
   755   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z \<le> f x) F)"
   756   by (auto simp: filterlim_iff eventually_at_top_linorder elim!: eventually_elim1)
   757 
   758 lemma filterlim_at_top_mono:
   759   "LIM x F. f x :> at_top \<Longrightarrow> eventually (\<lambda>x. f x \<le> (g x::'a::linorder)) F \<Longrightarrow>
   760     LIM x F. g x :> at_top"
   761   by (auto simp: filterlim_at_top elim: eventually_elim2 intro: order_trans)
   762 
   763 lemma filterlim_at_top_dense:
   764   fixes f :: "'a \<Rightarrow> ('b::unbounded_dense_linorder)"
   765   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
   766   by (metis eventually_elim1[of _ F] eventually_gt_at_top order_less_imp_le
   767             filterlim_at_top[of f F] filterlim_iff[of f at_top F])
   768 
   769 lemma filterlim_at_top_ge:
   770   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
   771   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F)"
   772   unfolding at_top_sub[of c] filterlim_INF by (auto simp add: filterlim_principal)
   773 
   774 lemma filterlim_at_top_at_top:
   775   fixes f :: "'a::linorder \<Rightarrow> 'b::linorder"
   776   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   777   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   778   assumes Q: "eventually Q at_top"
   779   assumes P: "eventually P at_top"
   780   shows "filterlim f at_top at_top"
   781 proof -
   782   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
   783     unfolding eventually_at_top_linorder by auto
   784   show ?thesis
   785   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
   786     fix z assume "x \<le> z"
   787     with x have "P z" by auto
   788     have "eventually (\<lambda>x. g z \<le> x) at_top"
   789       by (rule eventually_ge_at_top)
   790     with Q show "eventually (\<lambda>x. z \<le> f x) at_top"
   791       by eventually_elim (metis mono bij `P z`)
   792   qed
   793 qed
   794 
   795 lemma filterlim_at_top_gt:
   796   fixes f :: "'a \<Rightarrow> ('b::unbounded_dense_linorder)" and c :: "'b"
   797   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z \<le> f x) F)"
   798   by (metis filterlim_at_top order_less_le_trans gt_ex filterlim_at_top_ge)
   799 
   800 lemma filterlim_at_bot: 
   801   fixes f :: "'a \<Rightarrow> ('b::linorder)"
   802   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x \<le> Z) F)"
   803   by (auto simp: filterlim_iff eventually_at_bot_linorder elim!: eventually_elim1)
   804 
   805 lemma filterlim_at_bot_dense:
   806   fixes f :: "'a \<Rightarrow> ('b::{dense_linorder, no_bot})"
   807   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x < Z) F)"
   808 proof (auto simp add: filterlim_at_bot[of f F])
   809   fix Z :: 'b
   810   from lt_ex [of Z] obtain Z' where 1: "Z' < Z" ..
   811   assume "\<forall>Z. eventually (\<lambda>x. f x \<le> Z) F"
   812   hence "eventually (\<lambda>x. f x \<le> Z') F" by auto
   813   thus "eventually (\<lambda>x. f x < Z) F"
   814     apply (rule eventually_mono[rotated])
   815     using 1 by auto
   816   next 
   817     fix Z :: 'b 
   818     show "\<forall>Z. eventually (\<lambda>x. f x < Z) F \<Longrightarrow> eventually (\<lambda>x. f x \<le> Z) F"
   819       by (drule spec [of _ Z], erule eventually_mono[rotated], auto simp add: less_imp_le)
   820 qed
   821 
   822 lemma filterlim_at_bot_le:
   823   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
   824   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F)"
   825   unfolding filterlim_at_bot
   826 proof safe
   827   fix Z assume *: "\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F"
   828   with *[THEN spec, of "min Z c"] show "eventually (\<lambda>x. Z \<ge> f x) F"
   829     by (auto elim!: eventually_elim1)
   830 qed simp
   831 
   832 lemma filterlim_at_bot_lt:
   833   fixes f :: "'a \<Rightarrow> ('b::unbounded_dense_linorder)" and c :: "'b"
   834   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z \<ge> f x) F)"
   835   by (metis filterlim_at_bot filterlim_at_bot_le lt_ex order_le_less_trans)
   836 
   837 
   838 subsection {* Setup @{typ "'a filter"} for lifting and transfer *}
   839 
   840 context begin interpretation lifting_syntax .
   841 
   842 definition rel_filter :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> 'b filter \<Rightarrow> bool"
   843 where "rel_filter R F G = ((R ===> op =) ===> op =) (Rep_filter F) (Rep_filter G)"
   844 
   845 lemma rel_filter_eventually:
   846   "rel_filter R F G \<longleftrightarrow> 
   847   ((R ===> op =) ===> op =) (\<lambda>P. eventually P F) (\<lambda>P. eventually P G)"
   848 by(simp add: rel_filter_def eventually_def)
   849 
   850 lemma filtermap_id [simp, id_simps]: "filtermap id = id"
   851 by(simp add: fun_eq_iff id_def filtermap_ident)
   852 
   853 lemma filtermap_id' [simp]: "filtermap (\<lambda>x. x) = (\<lambda>F. F)"
   854 using filtermap_id unfolding id_def .
   855 
   856 lemma Quotient_filter [quot_map]:
   857   assumes Q: "Quotient R Abs Rep T"
   858   shows "Quotient (rel_filter R) (filtermap Abs) (filtermap Rep) (rel_filter T)"
   859 unfolding Quotient_alt_def
   860 proof(intro conjI strip)
   861   from Q have *: "\<And>x y. T x y \<Longrightarrow> Abs x = y"
   862     unfolding Quotient_alt_def by blast
   863 
   864   fix F G
   865   assume "rel_filter T F G"
   866   thus "filtermap Abs F = G" unfolding filter_eq_iff
   867     by(auto simp add: eventually_filtermap rel_filter_eventually * rel_funI del: iffI elim!: rel_funD)
   868 next
   869   from Q have *: "\<And>x. T (Rep x) x" unfolding Quotient_alt_def by blast
   870 
   871   fix F
   872   show "rel_filter T (filtermap Rep F) F" 
   873     by(auto elim: rel_funD intro: * intro!: ext arg_cong[where f="\<lambda>P. eventually P F"] rel_funI
   874             del: iffI simp add: eventually_filtermap rel_filter_eventually)
   875 qed(auto simp add: map_fun_def o_def eventually_filtermap filter_eq_iff fun_eq_iff rel_filter_eventually
   876          fun_quotient[OF fun_quotient[OF Q identity_quotient] identity_quotient, unfolded Quotient_alt_def])
   877 
   878 lemma eventually_parametric [transfer_rule]:
   879   "((A ===> op =) ===> rel_filter A ===> op =) eventually eventually"
   880 by(simp add: rel_fun_def rel_filter_eventually)
   881 
   882 lemma frequently_parametric [transfer_rule]:
   883   "((A ===> op =) ===> rel_filter A ===> op =) frequently frequently"
   884   unfolding frequently_def[abs_def] by transfer_prover
   885 
   886 lemma rel_filter_eq [relator_eq]: "rel_filter op = = op ="
   887 by(auto simp add: rel_filter_eventually rel_fun_eq fun_eq_iff filter_eq_iff)
   888 
   889 lemma rel_filter_mono [relator_mono]:
   890   "A \<le> B \<Longrightarrow> rel_filter A \<le> rel_filter B"
   891 unfolding rel_filter_eventually[abs_def]
   892 by(rule le_funI)+(intro fun_mono fun_mono[THEN le_funD, THEN le_funD] order.refl)
   893 
   894 lemma rel_filter_conversep [simp]: "rel_filter A\<inverse>\<inverse> = (rel_filter A)\<inverse>\<inverse>"
   895 by(auto simp add: rel_filter_eventually fun_eq_iff rel_fun_def)
   896 
   897 lemma is_filter_parametric_aux:
   898   assumes "is_filter F"
   899   assumes [transfer_rule]: "bi_total A" "bi_unique A"
   900   and [transfer_rule]: "((A ===> op =) ===> op =) F G"
   901   shows "is_filter G"
   902 proof -
   903   interpret is_filter F by fact
   904   show ?thesis
   905   proof
   906     have "F (\<lambda>_. True) = G (\<lambda>x. True)" by transfer_prover
   907     thus "G (\<lambda>x. True)" by(simp add: True)
   908   next
   909     fix P' Q'
   910     assume "G P'" "G Q'"
   911     moreover
   912     from bi_total_fun[OF `bi_unique A` bi_total_eq, unfolded bi_total_def]
   913     obtain P Q where [transfer_rule]: "(A ===> op =) P P'" "(A ===> op =) Q Q'" by blast
   914     have "F P = G P'" "F Q = G Q'" by transfer_prover+
   915     ultimately have "F (\<lambda>x. P x \<and> Q x)" by(simp add: conj)
   916     moreover have "F (\<lambda>x. P x \<and> Q x) = G (\<lambda>x. P' x \<and> Q' x)" by transfer_prover
   917     ultimately show "G (\<lambda>x. P' x \<and> Q' x)" by simp
   918   next
   919     fix P' Q'
   920     assume "\<forall>x. P' x \<longrightarrow> Q' x" "G P'"
   921     moreover
   922     from bi_total_fun[OF `bi_unique A` bi_total_eq, unfolded bi_total_def]
   923     obtain P Q where [transfer_rule]: "(A ===> op =) P P'" "(A ===> op =) Q Q'" by blast
   924     have "F P = G P'" by transfer_prover
   925     moreover have "(\<forall>x. P x \<longrightarrow> Q x) \<longleftrightarrow> (\<forall>x. P' x \<longrightarrow> Q' x)" by transfer_prover
   926     ultimately have "F Q" by(simp add: mono)
   927     moreover have "F Q = G Q'" by transfer_prover
   928     ultimately show "G Q'" by simp
   929   qed
   930 qed
   931 
   932 lemma is_filter_parametric [transfer_rule]:
   933   "\<lbrakk> bi_total A; bi_unique A \<rbrakk>
   934   \<Longrightarrow> (((A ===> op =) ===> op =) ===> op =) is_filter is_filter"
   935 apply(rule rel_funI)
   936 apply(rule iffI)
   937  apply(erule (3) is_filter_parametric_aux)
   938 apply(erule is_filter_parametric_aux[where A="conversep A"])
   939 apply(auto simp add: rel_fun_def)
   940 done
   941 
   942 lemma left_total_rel_filter [transfer_rule]:
   943   assumes [transfer_rule]: "bi_total A" "bi_unique A"
   944   shows "left_total (rel_filter A)"
   945 proof(rule left_totalI)
   946   fix F :: "'a filter"
   947   from bi_total_fun[OF bi_unique_fun[OF `bi_total A` bi_unique_eq] bi_total_eq]
   948   obtain G where [transfer_rule]: "((A ===> op =) ===> op =) (\<lambda>P. eventually P F) G" 
   949     unfolding  bi_total_def by blast
   950   moreover have "is_filter (\<lambda>P. eventually P F) \<longleftrightarrow> is_filter G" by transfer_prover
   951   hence "is_filter G" by(simp add: eventually_def is_filter_Rep_filter)
   952   ultimately have "rel_filter A F (Abs_filter G)"
   953     by(simp add: rel_filter_eventually eventually_Abs_filter)
   954   thus "\<exists>G. rel_filter A F G" ..
   955 qed
   956 
   957 lemma right_total_rel_filter [transfer_rule]:
   958   "\<lbrakk> bi_total A; bi_unique A \<rbrakk> \<Longrightarrow> right_total (rel_filter A)"
   959 using left_total_rel_filter[of "A\<inverse>\<inverse>"] by simp
   960 
   961 lemma bi_total_rel_filter [transfer_rule]:
   962   assumes "bi_total A" "bi_unique A"
   963   shows "bi_total (rel_filter A)"
   964 unfolding bi_total_alt_def using assms
   965 by(simp add: left_total_rel_filter right_total_rel_filter)
   966 
   967 lemma left_unique_rel_filter [transfer_rule]:
   968   assumes "left_unique A"
   969   shows "left_unique (rel_filter A)"
   970 proof(rule left_uniqueI)
   971   fix F F' G
   972   assume [transfer_rule]: "rel_filter A F G" "rel_filter A F' G"
   973   show "F = F'"
   974     unfolding filter_eq_iff
   975   proof
   976     fix P :: "'a \<Rightarrow> bool"
   977     obtain P' where [transfer_rule]: "(A ===> op =) P P'"
   978       using left_total_fun[OF assms left_total_eq] unfolding left_total_def by blast
   979     have "eventually P F = eventually P' G" 
   980       and "eventually P F' = eventually P' G" by transfer_prover+
   981     thus "eventually P F = eventually P F'" by simp
   982   qed
   983 qed
   984 
   985 lemma right_unique_rel_filter [transfer_rule]:
   986   "right_unique A \<Longrightarrow> right_unique (rel_filter A)"
   987 using left_unique_rel_filter[of "A\<inverse>\<inverse>"] by simp
   988 
   989 lemma bi_unique_rel_filter [transfer_rule]:
   990   "bi_unique A \<Longrightarrow> bi_unique (rel_filter A)"
   991 by(simp add: bi_unique_alt_def left_unique_rel_filter right_unique_rel_filter)
   992 
   993 lemma top_filter_parametric [transfer_rule]:
   994   "bi_total A \<Longrightarrow> (rel_filter A) top top"
   995 by(simp add: rel_filter_eventually All_transfer)
   996 
   997 lemma bot_filter_parametric [transfer_rule]: "(rel_filter A) bot bot"
   998 by(simp add: rel_filter_eventually rel_fun_def)
   999 
  1000 lemma sup_filter_parametric [transfer_rule]:
  1001   "(rel_filter A ===> rel_filter A ===> rel_filter A) sup sup"
  1002 by(fastforce simp add: rel_filter_eventually[abs_def] eventually_sup dest: rel_funD)
  1003 
  1004 lemma Sup_filter_parametric [transfer_rule]:
  1005   "(rel_set (rel_filter A) ===> rel_filter A) Sup Sup"
  1006 proof(rule rel_funI)
  1007   fix S T
  1008   assume [transfer_rule]: "rel_set (rel_filter A) S T"
  1009   show "rel_filter A (Sup S) (Sup T)"
  1010     by(simp add: rel_filter_eventually eventually_Sup) transfer_prover
  1011 qed
  1012 
  1013 lemma principal_parametric [transfer_rule]:
  1014   "(rel_set A ===> rel_filter A) principal principal"
  1015 proof(rule rel_funI)
  1016   fix S S'
  1017   assume [transfer_rule]: "rel_set A S S'"
  1018   show "rel_filter A (principal S) (principal S')"
  1019     by(simp add: rel_filter_eventually eventually_principal) transfer_prover
  1020 qed
  1021 
  1022 context
  1023   fixes A :: "'a \<Rightarrow> 'b \<Rightarrow> bool"
  1024   assumes [transfer_rule]: "bi_unique A" 
  1025 begin
  1026 
  1027 lemma le_filter_parametric [transfer_rule]:
  1028   "(rel_filter A ===> rel_filter A ===> op =) op \<le> op \<le>"
  1029 unfolding le_filter_def[abs_def] by transfer_prover
  1030 
  1031 lemma less_filter_parametric [transfer_rule]:
  1032   "(rel_filter A ===> rel_filter A ===> op =) op < op <"
  1033 unfolding less_filter_def[abs_def] by transfer_prover
  1034 
  1035 context
  1036   assumes [transfer_rule]: "bi_total A"
  1037 begin
  1038 
  1039 lemma Inf_filter_parametric [transfer_rule]:
  1040   "(rel_set (rel_filter A) ===> rel_filter A) Inf Inf"
  1041 unfolding Inf_filter_def[abs_def] by transfer_prover
  1042 
  1043 lemma inf_filter_parametric [transfer_rule]:
  1044   "(rel_filter A ===> rel_filter A ===> rel_filter A) inf inf"
  1045 proof(intro rel_funI)+
  1046   fix F F' G G'
  1047   assume [transfer_rule]: "rel_filter A F F'" "rel_filter A G G'"
  1048   have "rel_filter A (Inf {F, G}) (Inf {F', G'})" by transfer_prover
  1049   thus "rel_filter A (inf F G) (inf F' G')" by simp
  1050 qed
  1051 
  1052 end
  1053 
  1054 end
  1055 
  1056 end
  1057 
  1058 end