src/HOL/Library/Infinite_Set.thy
author wenzelm
Tue May 15 13:57:39 2018 +0200 (16 months ago)
changeset 68189 6163c90694ef
parent 67408 4a4c14b24800
child 68406 6beb45f6cf67
permissions -rw-r--r--
tuned headers;
huffman@27407
     1
(*  Title:      HOL/Library/Infinite_Set.thy
wenzelm@20809
     2
    Author:     Stephan Merz
wenzelm@20809
     3
*)
wenzelm@20809
     4
wenzelm@60500
     5
section \<open>Infinite Sets and Related Concepts\<close>
wenzelm@20809
     6
wenzelm@20809
     7
theory Infinite_Set
wenzelm@64967
     8
  imports Main
wenzelm@20809
     9
begin
wenzelm@20809
    10
wenzelm@64967
    11
subsection \<open>The set of natural numbers is infinite\<close>
wenzelm@20809
    12
wenzelm@64967
    13
lemma infinite_nat_iff_unbounded_le: "infinite S \<longleftrightarrow> (\<forall>m. \<exists>n\<ge>m. n \<in> S)"
wenzelm@64967
    14
  for S :: "nat set"
hoelzl@60040
    15
  using frequently_cofinite[of "\<lambda>x. x \<in> S"]
hoelzl@60040
    16
  by (simp add: cofinite_eq_sequentially frequently_def eventually_sequentially)
hoelzl@60040
    17
wenzelm@64967
    18
lemma infinite_nat_iff_unbounded: "infinite S \<longleftrightarrow> (\<forall>m. \<exists>n>m. n \<in> S)"
wenzelm@64967
    19
  for S :: "nat set"
hoelzl@60040
    20
  using frequently_cofinite[of "\<lambda>x. x \<in> S"]
hoelzl@60040
    21
  by (simp add: cofinite_eq_sequentially frequently_def eventually_at_top_dense)
wenzelm@20809
    22
wenzelm@64967
    23
lemma finite_nat_iff_bounded: "finite S \<longleftrightarrow> (\<exists>k. S \<subseteq> {..<k})"
wenzelm@64967
    24
  for S :: "nat set"
hoelzl@60040
    25
  using infinite_nat_iff_unbounded_le[of S] by (simp add: subset_eq) (metis not_le)
wenzelm@20809
    26
wenzelm@64967
    27
lemma finite_nat_iff_bounded_le: "finite S \<longleftrightarrow> (\<exists>k. S \<subseteq> {.. k})"
wenzelm@64967
    28
  for S :: "nat set"
hoelzl@60040
    29
  using infinite_nat_iff_unbounded[of S] by (simp add: subset_eq) (metis not_le)
wenzelm@20809
    30
wenzelm@64967
    31
lemma finite_nat_bounded: "finite S \<Longrightarrow> \<exists>k. S \<subseteq> {..<k}"
wenzelm@64967
    32
  for S :: "nat set"
hoelzl@60040
    33
  by (simp add: finite_nat_iff_bounded)
wenzelm@20809
    34
lp15@61762
    35
wenzelm@60500
    36
text \<open>
wenzelm@20809
    37
  For a set of natural numbers to be infinite, it is enough to know
wenzelm@61585
    38
  that for any number larger than some \<open>k\<close>, there is some larger
wenzelm@20809
    39
  number that is an element of the set.
wenzelm@60500
    40
\<close>
wenzelm@20809
    41
hoelzl@60040
    42
lemma unbounded_k_infinite: "\<forall>m>k. \<exists>n>m. n \<in> S \<Longrightarrow> infinite (S::nat set)"
wenzelm@64967
    43
  apply (clarsimp simp add: finite_nat_set_iff_bounded)
wenzelm@64967
    44
  apply (drule_tac x="Suc (max m k)" in spec)
wenzelm@64967
    45
  using less_Suc_eq apply fastforce
wenzelm@64967
    46
  done
wenzelm@20809
    47
huffman@35056
    48
lemma nat_not_finite: "finite (UNIV::nat set) \<Longrightarrow> R"
wenzelm@20809
    49
  by simp
wenzelm@20809
    50
wenzelm@20809
    51
lemma range_inj_infinite:
wenzelm@64967
    52
  fixes f :: "nat \<Rightarrow> 'a"
wenzelm@64967
    53
  assumes "inj f"
wenzelm@64967
    54
  shows "infinite (range f)"
wenzelm@20809
    55
proof
wenzelm@64967
    56
  assume "finite (range f)"
wenzelm@64967
    57
  from this assms have "finite (UNIV::nat set)"
huffman@27407
    58
    by (rule finite_imageD)
wenzelm@20809
    59
  then show False by simp
wenzelm@20809
    60
qed
wenzelm@20809
    61
wenzelm@64967
    62
wenzelm@64967
    63
subsection \<open>The set of integers is also infinite\<close>
lp15@61762
    64
wenzelm@64967
    65
lemma infinite_int_iff_infinite_nat_abs: "infinite S \<longleftrightarrow> infinite ((nat \<circ> abs) ` S)"
wenzelm@64967
    66
  for S :: "int set"
haftmann@66837
    67
proof -
haftmann@66837
    68
  have "inj_on nat (abs ` A)" for A
haftmann@66837
    69
    by (rule inj_onI) auto
haftmann@66837
    70
  then show ?thesis
haftmann@66837
    71
    by (auto simp add: image_comp [symmetric] dest: finite_image_absD finite_imageD)
haftmann@66837
    72
qed
lp15@61762
    73
wenzelm@64967
    74
proposition infinite_int_iff_unbounded_le: "infinite S \<longleftrightarrow> (\<forall>m. \<exists>n. \<bar>n\<bar> \<ge> m \<and> n \<in> S)"
wenzelm@64967
    75
  for S :: "int set"
wenzelm@64967
    76
  by (simp add: infinite_int_iff_infinite_nat_abs infinite_nat_iff_unbounded_le o_def image_def)
wenzelm@64967
    77
    (metis abs_ge_zero nat_le_eq_zle le_nat_iff)
lp15@61762
    78
wenzelm@64967
    79
proposition infinite_int_iff_unbounded: "infinite S \<longleftrightarrow> (\<forall>m. \<exists>n. \<bar>n\<bar> > m \<and> n \<in> S)"
wenzelm@64967
    80
  for S :: "int set"
wenzelm@64967
    81
  by (simp add: infinite_int_iff_infinite_nat_abs infinite_nat_iff_unbounded o_def image_def)
wenzelm@64967
    82
    (metis (full_types) nat_le_iff nat_mono not_le)
lp15@61762
    83
wenzelm@64967
    84
proposition finite_int_iff_bounded: "finite S \<longleftrightarrow> (\<exists>k. abs ` S \<subseteq> {..<k})"
wenzelm@64967
    85
  for S :: "int set"
lp15@61762
    86
  using infinite_int_iff_unbounded_le[of S] by (simp add: subset_eq) (metis not_le)
lp15@61762
    87
wenzelm@64967
    88
proposition finite_int_iff_bounded_le: "finite S \<longleftrightarrow> (\<exists>k. abs ` S \<subseteq> {.. k})"
wenzelm@64967
    89
  for S :: "int set"
lp15@61762
    90
  using infinite_int_iff_unbounded[of S] by (simp add: subset_eq) (metis not_le)
lp15@61762
    91
wenzelm@64967
    92
wenzelm@64967
    93
subsection \<open>Infinitely Many and Almost All\<close>
wenzelm@20809
    94
wenzelm@60500
    95
text \<open>
wenzelm@20809
    96
  We often need to reason about the existence of infinitely many
wenzelm@20809
    97
  (resp., all but finitely many) objects satisfying some predicate, so
wenzelm@20809
    98
  we introduce corresponding binders and their proof rules.
wenzelm@60500
    99
\<close>
wenzelm@20809
   100
wenzelm@64967
   101
lemma not_INFM [simp]: "\<not> (INFM x. P x) \<longleftrightarrow> (MOST x. \<not> P x)"
wenzelm@64967
   102
  by (rule not_frequently)
wenzelm@64967
   103
wenzelm@64967
   104
lemma not_MOST [simp]: "\<not> (MOST x. P x) \<longleftrightarrow> (INFM x. \<not> P x)"
wenzelm@64967
   105
  by (rule not_eventually)
huffman@34112
   106
huffman@34112
   107
lemma INFM_const [simp]: "(INFM x::'a. P) \<longleftrightarrow> P \<and> infinite (UNIV::'a set)"
hoelzl@60040
   108
  by (simp add: frequently_const_iff)
huffman@34112
   109
huffman@34112
   110
lemma MOST_const [simp]: "(MOST x::'a. P) \<longleftrightarrow> P \<or> finite (UNIV::'a set)"
hoelzl@60040
   111
  by (simp add: eventually_const_iff)
wenzelm@20809
   112
hoelzl@60040
   113
lemma INFM_imp_distrib: "(INFM x. P x \<longrightarrow> Q x) \<longleftrightarrow> ((MOST x. P x) \<longrightarrow> (INFM x. Q x))"
wenzelm@64967
   114
  by (rule frequently_imp_iff)
huffman@34112
   115
hoelzl@60040
   116
lemma MOST_imp_iff: "MOST x. P x \<Longrightarrow> (MOST x. P x \<longrightarrow> Q x) \<longleftrightarrow> (MOST x. Q x)"
lp15@61810
   117
  by (auto intro: eventually_rev_mp eventually_mono)
huffman@34113
   118
hoelzl@60040
   119
lemma INFM_conjI: "INFM x. P x \<Longrightarrow> MOST x. Q x \<Longrightarrow> INFM x. P x \<and> Q x"
lp15@61810
   120
  by (rule frequently_rev_mp[of P]) (auto elim: eventually_mono)
huffman@34112
   121
wenzelm@64967
   122
wenzelm@60500
   123
text \<open>Properties of quantifiers with injective functions.\<close>
huffman@34112
   124
wenzelm@53239
   125
lemma INFM_inj: "INFM x. P (f x) \<Longrightarrow> inj f \<Longrightarrow> INFM x. P x"
hoelzl@60040
   126
  using finite_vimageI[of "{x. P x}" f] by (auto simp: frequently_cofinite)
huffman@27407
   127
wenzelm@53239
   128
lemma MOST_inj: "MOST x. P x \<Longrightarrow> inj f \<Longrightarrow> MOST x. P (f x)"
hoelzl@60040
   129
  using finite_vimageI[of "{x. \<not> P x}" f] by (auto simp: eventually_cofinite)
huffman@34112
   130
wenzelm@64967
   131
wenzelm@60500
   132
text \<open>Properties of quantifiers with singletons.\<close>
huffman@34112
   133
huffman@34112
   134
lemma not_INFM_eq [simp]:
huffman@34112
   135
  "\<not> (INFM x. x = a)"
huffman@34112
   136
  "\<not> (INFM x. a = x)"
hoelzl@60040
   137
  unfolding frequently_cofinite by simp_all
huffman@34112
   138
huffman@34112
   139
lemma MOST_neq [simp]:
huffman@34112
   140
  "MOST x. x \<noteq> a"
huffman@34112
   141
  "MOST x. a \<noteq> x"
hoelzl@60040
   142
  unfolding eventually_cofinite by simp_all
huffman@27407
   143
huffman@34112
   144
lemma INFM_neq [simp]:
huffman@34112
   145
  "(INFM x::'a. x \<noteq> a) \<longleftrightarrow> infinite (UNIV::'a set)"
huffman@34112
   146
  "(INFM x::'a. a \<noteq> x) \<longleftrightarrow> infinite (UNIV::'a set)"
hoelzl@60040
   147
  unfolding frequently_cofinite by simp_all
huffman@34112
   148
huffman@34112
   149
lemma MOST_eq [simp]:
huffman@34112
   150
  "(MOST x::'a. x = a) \<longleftrightarrow> finite (UNIV::'a set)"
huffman@34112
   151
  "(MOST x::'a. a = x) \<longleftrightarrow> finite (UNIV::'a set)"
hoelzl@60040
   152
  unfolding eventually_cofinite by simp_all
huffman@34112
   153
huffman@34112
   154
lemma MOST_eq_imp:
huffman@34112
   155
  "MOST x. x = a \<longrightarrow> P x"
huffman@34112
   156
  "MOST x. a = x \<longrightarrow> P x"
hoelzl@60040
   157
  unfolding eventually_cofinite by simp_all
huffman@34112
   158
wenzelm@64967
   159
wenzelm@60500
   160
text \<open>Properties of quantifiers over the naturals.\<close>
huffman@27407
   161
wenzelm@64967
   162
lemma MOST_nat: "(\<forall>\<^sub>\<infinity>n. P n) \<longleftrightarrow> (\<exists>m. \<forall>n>m. P n)"
wenzelm@64967
   163
  for P :: "nat \<Rightarrow> bool"
hoelzl@60040
   164
  by (auto simp add: eventually_cofinite finite_nat_iff_bounded_le subset_eq not_le[symmetric])
hoelzl@60040
   165
wenzelm@64967
   166
lemma MOST_nat_le: "(\<forall>\<^sub>\<infinity>n. P n) \<longleftrightarrow> (\<exists>m. \<forall>n\<ge>m. P n)"
wenzelm@64967
   167
  for P :: "nat \<Rightarrow> bool"
hoelzl@60040
   168
  by (auto simp add: eventually_cofinite finite_nat_iff_bounded subset_eq not_le[symmetric])
hoelzl@60040
   169
wenzelm@64967
   170
lemma INFM_nat: "(\<exists>\<^sub>\<infinity>n. P n) \<longleftrightarrow> (\<forall>m. \<exists>n>m. P n)"
wenzelm@64967
   171
  for P :: "nat \<Rightarrow> bool"
hoelzl@60040
   172
  by (simp add: frequently_cofinite infinite_nat_iff_unbounded)
wenzelm@20809
   173
wenzelm@64967
   174
lemma INFM_nat_le: "(\<exists>\<^sub>\<infinity>n. P n) \<longleftrightarrow> (\<forall>m. \<exists>n\<ge>m. P n)"
wenzelm@64967
   175
  for P :: "nat \<Rightarrow> bool"
hoelzl@60040
   176
  by (simp add: frequently_cofinite infinite_nat_iff_unbounded_le)
hoelzl@60040
   177
hoelzl@60040
   178
lemma MOST_INFM: "infinite (UNIV::'a set) \<Longrightarrow> MOST x::'a. P x \<Longrightarrow> INFM x::'a. P x"
hoelzl@60040
   179
  by (simp add: eventually_frequently)
hoelzl@60040
   180
hoelzl@60040
   181
lemma MOST_Suc_iff: "(MOST n. P (Suc n)) \<longleftrightarrow> (MOST n. P n)"
wenzelm@64697
   182
  by (simp add: cofinite_eq_sequentially)
wenzelm@20809
   183
wenzelm@64967
   184
lemma MOST_SucI: "MOST n. P n \<Longrightarrow> MOST n. P (Suc n)"
wenzelm@64967
   185
  and MOST_SucD: "MOST n. P (Suc n) \<Longrightarrow> MOST n. P n"
hoelzl@60040
   186
  by (simp_all add: MOST_Suc_iff)
hoelzl@60040
   187
hoelzl@60040
   188
lemma MOST_ge_nat: "MOST n::nat. m \<le> n"
haftmann@66837
   189
  by (simp add: cofinite_eq_sequentially)
wenzelm@20809
   190
wenzelm@67408
   191
\<comment> \<open>legacy names\<close>
hoelzl@60040
   192
lemma Inf_many_def: "Inf_many P \<longleftrightarrow> infinite {x. P x}" by (fact frequently_cofinite)
hoelzl@60040
   193
lemma Alm_all_def: "Alm_all P \<longleftrightarrow> \<not> (INFM x. \<not> P x)" by simp
hoelzl@60040
   194
lemma INFM_iff_infinite: "(INFM x. P x) \<longleftrightarrow> infinite {x. P x}" by (fact frequently_cofinite)
hoelzl@60040
   195
lemma MOST_iff_cofinite: "(MOST x. P x) \<longleftrightarrow> finite {x. \<not> P x}" by (fact eventually_cofinite)
hoelzl@60040
   196
lemma INFM_EX: "(\<exists>\<^sub>\<infinity>x. P x) \<Longrightarrow> (\<exists>x. P x)" by (fact frequently_ex)
hoelzl@60040
   197
lemma ALL_MOST: "\<forall>x. P x \<Longrightarrow> \<forall>\<^sub>\<infinity>x. P x" by (fact always_eventually)
hoelzl@60040
   198
lemma INFM_mono: "\<exists>\<^sub>\<infinity>x. P x \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> \<exists>\<^sub>\<infinity>x. Q x" by (fact frequently_elim1)
lp15@61810
   199
lemma MOST_mono: "\<forall>\<^sub>\<infinity>x. P x \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> \<forall>\<^sub>\<infinity>x. Q x" by (fact eventually_mono)
hoelzl@60040
   200
lemma INFM_disj_distrib: "(\<exists>\<^sub>\<infinity>x. P x \<or> Q x) \<longleftrightarrow> (\<exists>\<^sub>\<infinity>x. P x) \<or> (\<exists>\<^sub>\<infinity>x. Q x)" by (fact frequently_disj_iff)
hoelzl@60040
   201
lemma MOST_rev_mp: "\<forall>\<^sub>\<infinity>x. P x \<Longrightarrow> \<forall>\<^sub>\<infinity>x. P x \<longrightarrow> Q x \<Longrightarrow> \<forall>\<^sub>\<infinity>x. Q x" by (fact eventually_rev_mp)
hoelzl@60040
   202
lemma MOST_conj_distrib: "(\<forall>\<^sub>\<infinity>x. P x \<and> Q x) \<longleftrightarrow> (\<forall>\<^sub>\<infinity>x. P x) \<and> (\<forall>\<^sub>\<infinity>x. Q x)" by (fact eventually_conj_iff)
hoelzl@60040
   203
lemma MOST_conjI: "MOST x. P x \<Longrightarrow> MOST x. Q x \<Longrightarrow> MOST x. P x \<and> Q x" by (fact eventually_conj)
hoelzl@60040
   204
lemma INFM_finite_Bex_distrib: "finite A \<Longrightarrow> (INFM y. \<exists>x\<in>A. P x y) \<longleftrightarrow> (\<exists>x\<in>A. INFM y. P x y)" by (fact frequently_bex_finite_distrib)
hoelzl@60040
   205
lemma MOST_finite_Ball_distrib: "finite A \<Longrightarrow> (MOST y. \<forall>x\<in>A. P x y) \<longleftrightarrow> (\<forall>x\<in>A. MOST y. P x y)" by (fact eventually_ball_finite_distrib)
hoelzl@60040
   206
lemma INFM_E: "INFM x. P x \<Longrightarrow> (\<And>x. P x \<Longrightarrow> thesis) \<Longrightarrow> thesis" by (fact frequentlyE)
hoelzl@60040
   207
lemma MOST_I: "(\<And>x. P x) \<Longrightarrow> MOST x. P x" by (rule eventuallyI)
hoelzl@60040
   208
lemmas MOST_iff_finiteNeg = MOST_iff_cofinite
wenzelm@20809
   209
wenzelm@20809
   210
wenzelm@64967
   211
subsection \<open>Enumeration of an Infinite Set\<close>
wenzelm@64967
   212
wenzelm@64967
   213
text \<open>The set's element type must be wellordered (e.g. the natural numbers).\<close>
wenzelm@20809
   214
hoelzl@60040
   215
text \<open>
hoelzl@60040
   216
  Could be generalized to
wenzelm@64967
   217
    @{prop "enumerate' S n = (SOME t. t \<in> s \<and> finite {s\<in>S. s < t} \<and> card {s\<in>S. s < t} = n)"}.
hoelzl@60040
   218
\<close>
hoelzl@60040
   219
wenzelm@53239
   220
primrec (in wellorder) enumerate :: "'a set \<Rightarrow> nat \<Rightarrow> 'a"
wenzelm@64967
   221
  where
wenzelm@64967
   222
    enumerate_0: "enumerate S 0 = (LEAST n. n \<in> S)"
wenzelm@64967
   223
  | enumerate_Suc: "enumerate S (Suc n) = enumerate (S - {LEAST n. n \<in> S}) n"
wenzelm@20809
   224
wenzelm@53239
   225
lemma enumerate_Suc': "enumerate S (Suc n) = enumerate (S - {enumerate S 0}) n"
wenzelm@20809
   226
  by simp
wenzelm@20809
   227
hoelzl@60040
   228
lemma enumerate_in_set: "infinite S \<Longrightarrow> enumerate S n \<in> S"
wenzelm@64967
   229
proof (induct n arbitrary: S)
wenzelm@64967
   230
  case 0
wenzelm@64967
   231
  then show ?case
wenzelm@64967
   232
    by (fastforce intro: LeastI dest!: infinite_imp_nonempty)
wenzelm@64967
   233
next
wenzelm@64967
   234
  case (Suc n)
wenzelm@64967
   235
  then show ?case
wenzelm@64967
   236
    by simp (metis DiffE infinite_remove)
wenzelm@64967
   237
qed
wenzelm@20809
   238
wenzelm@20809
   239
declare enumerate_0 [simp del] enumerate_Suc [simp del]
wenzelm@20809
   240
wenzelm@20809
   241
lemma enumerate_step: "infinite S \<Longrightarrow> enumerate S n < enumerate S (Suc n)"
wenzelm@20809
   242
  apply (induct n arbitrary: S)
wenzelm@20809
   243
   apply (rule order_le_neq_trans)
wenzelm@20809
   244
    apply (simp add: enumerate_0 Least_le enumerate_in_set)
wenzelm@20809
   245
   apply (simp only: enumerate_Suc')
hoelzl@60040
   246
   apply (subgoal_tac "enumerate (S - {enumerate S 0}) 0 \<in> S - {enumerate S 0}")
wenzelm@20809
   247
    apply (blast intro: sym)
wenzelm@20809
   248
   apply (simp add: enumerate_in_set del: Diff_iff)
wenzelm@20809
   249
  apply (simp add: enumerate_Suc')
wenzelm@20809
   250
  done
wenzelm@20809
   251
hoelzl@60040
   252
lemma enumerate_mono: "m < n \<Longrightarrow> infinite S \<Longrightarrow> enumerate S m < enumerate S n"
wenzelm@64967
   253
  by (induct m n rule: less_Suc_induct) (auto intro: enumerate_step)
wenzelm@20809
   254
hoelzl@50134
   255
lemma le_enumerate:
hoelzl@50134
   256
  assumes S: "infinite S"
hoelzl@50134
   257
  shows "n \<le> enumerate S n"
lp15@61810
   258
  using S
hoelzl@50134
   259
proof (induct n)
wenzelm@53239
   260
  case 0
wenzelm@53239
   261
  then show ?case by simp
wenzelm@53239
   262
next
hoelzl@50134
   263
  case (Suc n)
hoelzl@50134
   264
  then have "n \<le> enumerate S n" by simp
wenzelm@60500
   265
  also note enumerate_mono[of n "Suc n", OF _ \<open>infinite S\<close>]
hoelzl@50134
   266
  finally show ?case by simp
wenzelm@53239
   267
qed
hoelzl@50134
   268
hoelzl@50134
   269
lemma enumerate_Suc'':
hoelzl@50134
   270
  fixes S :: "'a::wellorder set"
wenzelm@53239
   271
  assumes "infinite S"
wenzelm@53239
   272
  shows "enumerate S (Suc n) = (LEAST s. s \<in> S \<and> enumerate S n < s)"
wenzelm@53239
   273
  using assms
hoelzl@50134
   274
proof (induct n arbitrary: S)
hoelzl@50134
   275
  case 0
wenzelm@53239
   276
  then have "\<forall>s \<in> S. enumerate S 0 \<le> s"
hoelzl@50134
   277
    by (auto simp: enumerate.simps intro: Least_le)
hoelzl@50134
   278
  then show ?case
hoelzl@50134
   279
    unfolding enumerate_Suc' enumerate_0[of "S - {enumerate S 0}"]
wenzelm@53239
   280
    by (intro arg_cong[where f = Least] ext) auto
hoelzl@50134
   281
next
hoelzl@50134
   282
  case (Suc n S)
hoelzl@50134
   283
  show ?case
wenzelm@60500
   284
    using enumerate_mono[OF zero_less_Suc \<open>infinite S\<close>, of n] \<open>infinite S\<close>
hoelzl@50134
   285
    apply (subst (1 2) enumerate_Suc')
hoelzl@50134
   286
    apply (subst Suc)
wenzelm@64967
   287
     apply (use \<open>infinite S\<close> in simp)
wenzelm@53239
   288
    apply (intro arg_cong[where f = Least] ext)
wenzelm@53239
   289
    apply (auto simp: enumerate_Suc'[symmetric])
wenzelm@53239
   290
    done
hoelzl@50134
   291
qed
hoelzl@50134
   292
hoelzl@50134
   293
lemma enumerate_Ex:
wenzelm@64967
   294
  fixes S :: "nat set"
wenzelm@64967
   295
  assumes S: "infinite S"
wenzelm@64967
   296
    and s: "s \<in> S"
wenzelm@64967
   297
  shows "\<exists>n. enumerate S n = s"
wenzelm@64967
   298
  using s
hoelzl@50134
   299
proof (induct s rule: less_induct)
hoelzl@50134
   300
  case (less s)
hoelzl@50134
   301
  show ?case
wenzelm@64967
   302
  proof (cases "\<exists>y\<in>S. y < s")
wenzelm@64967
   303
    case True
hoelzl@50134
   304
    let ?y = "Max {s'\<in>S. s' < s}"
wenzelm@64967
   305
    from True have y: "\<And>x. ?y < x \<longleftrightarrow> (\<forall>s'\<in>S. s' < s \<longrightarrow> s' < x)"
wenzelm@53239
   306
      by (subst Max_less_iff) auto
wenzelm@53239
   307
    then have y_in: "?y \<in> {s'\<in>S. s' < s}"
wenzelm@53239
   308
      by (intro Max_in) auto
wenzelm@53239
   309
    with less.hyps[of ?y] obtain n where "enumerate S n = ?y"
wenzelm@53239
   310
      by auto
hoelzl@50134
   311
    with S have "enumerate S (Suc n) = s"
hoelzl@50134
   312
      by (auto simp: y less enumerate_Suc'' intro!: Least_equality)
wenzelm@64967
   313
    then show ?thesis by auto
hoelzl@50134
   314
  next
wenzelm@64967
   315
    case False
hoelzl@50134
   316
    then have "\<forall>t\<in>S. s \<le> t" by auto
wenzelm@60500
   317
    with \<open>s \<in> S\<close> show ?thesis
hoelzl@50134
   318
      by (auto intro!: exI[of _ 0] Least_equality simp: enumerate_0)
hoelzl@50134
   319
  qed
hoelzl@50134
   320
qed
hoelzl@50134
   321
hoelzl@50134
   322
lemma bij_enumerate:
hoelzl@50134
   323
  fixes S :: "nat set"
hoelzl@50134
   324
  assumes S: "infinite S"
hoelzl@50134
   325
  shows "bij_betw (enumerate S) UNIV S"
hoelzl@50134
   326
proof -
hoelzl@50134
   327
  have "\<And>n m. n \<noteq> m \<Longrightarrow> enumerate S n \<noteq> enumerate S m"
wenzelm@60500
   328
    using enumerate_mono[OF _ \<open>infinite S\<close>] by (auto simp: neq_iff)
hoelzl@50134
   329
  then have "inj (enumerate S)"
hoelzl@50134
   330
    by (auto simp: inj_on_def)
wenzelm@53239
   331
  moreover have "\<forall>s \<in> S. \<exists>i. enumerate S i = s"
hoelzl@50134
   332
    using enumerate_Ex[OF S] by auto
wenzelm@60500
   333
  moreover note \<open>infinite S\<close>
hoelzl@50134
   334
  ultimately show ?thesis
hoelzl@50134
   335
    unfolding bij_betw_def by (auto intro: enumerate_in_set)
hoelzl@50134
   336
qed
hoelzl@50134
   337
wenzelm@64967
   338
text \<open>A pair of weird and wonderful lemmas from HOL Light.\<close>
lp15@63492
   339
lemma finite_transitivity_chain:
lp15@63492
   340
  assumes "finite A"
wenzelm@64967
   341
    and R: "\<And>x. \<not> R x x" "\<And>x y z. \<lbrakk>R x y; R y z\<rbrakk> \<Longrightarrow> R x z"
wenzelm@64967
   342
    and A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y. y \<in> A \<and> R x y"
lp15@63492
   343
  shows "A = {}"
wenzelm@64967
   344
  using \<open>finite A\<close> A
wenzelm@64967
   345
proof (induct A)
wenzelm@64967
   346
  case empty
wenzelm@64967
   347
  then show ?case by simp
wenzelm@64967
   348
next
lp15@63492
   349
  case (insert a A)
lp15@63492
   350
  with R show ?case
wenzelm@64967
   351
    by (metis empty_iff insert_iff)   (* somewhat slow *)
wenzelm@64967
   352
qed
lp15@63492
   353
lp15@63492
   354
corollary Union_maximal_sets:
lp15@63492
   355
  assumes "finite \<F>"
wenzelm@64967
   356
  shows "\<Union>{T \<in> \<F>. \<forall>U\<in>\<F>. \<not> T \<subset> U} = \<Union>\<F>"
lp15@63492
   357
    (is "?lhs = ?rhs")
lp15@63492
   358
proof
wenzelm@64967
   359
  show "?lhs \<subseteq> ?rhs" by force
lp15@63492
   360
  show "?rhs \<subseteq> ?lhs"
lp15@63492
   361
  proof (rule Union_subsetI)
lp15@63492
   362
    fix S
lp15@63492
   363
    assume "S \<in> \<F>"
wenzelm@64967
   364
    have "{T \<in> \<F>. S \<subseteq> T} = {}"
wenzelm@64967
   365
      if "\<not> (\<exists>y. y \<in> {T \<in> \<F>. \<forall>U\<in>\<F>. \<not> T \<subset> U} \<and> S \<subseteq> y)"
lp15@63492
   366
      apply (rule finite_transitivity_chain [of _ "\<lambda>T U. S \<subseteq> T \<and> T \<subset> U"])
wenzelm@64967
   367
         apply (use assms that in auto)
wenzelm@64967
   368
      apply (blast intro: dual_order.trans psubset_imp_subset)
wenzelm@64967
   369
      done
wenzelm@64967
   370
    with \<open>S \<in> \<F>\<close> show "\<exists>y. y \<in> {T \<in> \<F>. \<forall>U\<in>\<F>. \<not> T \<subset> U} \<and> S \<subseteq> y"
wenzelm@64967
   371
      by blast
lp15@63492
   372
  qed
wenzelm@64967
   373
qed
lp15@63492
   374
wenzelm@20809
   375
end