src/HOL/Library/Infinite_Set.thy
author haftmann
Sat, 16 Jan 2010 17:15:28 +0100
changeset 34928 156925dd67af
parent 34111 dbc0fb6e7eae
child 35056 d97b5c3af6d5
permissions -rw-r--r--
dropped some old primrecs and some constdefs
huffman@27407
     1
(*  Title:      HOL/Library/Infinite_Set.thy
wenzelm@20809
     2
    Author:     Stephan Merz
wenzelm@20809
     3
*)
wenzelm@20809
     4
wenzelm@20809
     5
header {* Infinite Sets and Related Concepts *}
wenzelm@20809
     6
wenzelm@20809
     7
theory Infinite_Set
haftmann@30663
     8
imports Main
wenzelm@20809
     9
begin
wenzelm@20809
    10
wenzelm@20809
    11
subsection "Infinite Sets"
wenzelm@20809
    12
wenzelm@20809
    13
text {*
wenzelm@20809
    14
  Some elementary facts about infinite sets, mostly by Stefan Merz.
wenzelm@20809
    15
  Beware! Because "infinite" merely abbreviates a negation, these
wenzelm@20809
    16
  lemmas may not work well with @{text "blast"}.
wenzelm@20809
    17
*}
wenzelm@20809
    18
wenzelm@20809
    19
abbreviation
wenzelm@21404
    20
  infinite :: "'a set \<Rightarrow> bool" where
wenzelm@20809
    21
  "infinite S == \<not> finite S"
wenzelm@20809
    22
wenzelm@20809
    23
text {*
wenzelm@20809
    24
  Infinite sets are non-empty, and if we remove some elements from an
wenzelm@20809
    25
  infinite set, the result is still infinite.
wenzelm@20809
    26
*}
wenzelm@20809
    27
wenzelm@20809
    28
lemma infinite_imp_nonempty: "infinite S ==> S \<noteq> {}"
wenzelm@20809
    29
  by auto
wenzelm@20809
    30
wenzelm@20809
    31
lemma infinite_remove:
wenzelm@20809
    32
  "infinite S \<Longrightarrow> infinite (S - {a})"
wenzelm@20809
    33
  by simp
wenzelm@20809
    34
wenzelm@20809
    35
lemma Diff_infinite_finite:
wenzelm@20809
    36
  assumes T: "finite T" and S: "infinite S"
wenzelm@20809
    37
  shows "infinite (S - T)"
wenzelm@20809
    38
  using T
wenzelm@20809
    39
proof induct
wenzelm@20809
    40
  from S
wenzelm@20809
    41
  show "infinite (S - {})" by auto
wenzelm@20809
    42
next
wenzelm@20809
    43
  fix T x
wenzelm@20809
    44
  assume ih: "infinite (S - T)"
wenzelm@20809
    45
  have "S - (insert x T) = (S - T) - {x}"
wenzelm@20809
    46
    by (rule Diff_insert)
wenzelm@20809
    47
  with ih
wenzelm@20809
    48
  show "infinite (S - (insert x T))"
wenzelm@20809
    49
    by (simp add: infinite_remove)
wenzelm@20809
    50
qed
wenzelm@20809
    51
wenzelm@20809
    52
lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)"
wenzelm@20809
    53
  by simp
wenzelm@20809
    54
wenzelm@20809
    55
lemma infinite_super:
wenzelm@20809
    56
  assumes T: "S \<subseteq> T" and S: "infinite S"
wenzelm@20809
    57
  shows "infinite T"
wenzelm@20809
    58
proof
wenzelm@20809
    59
  assume "finite T"
wenzelm@20809
    60
  with T have "finite S" by (simp add: finite_subset)
wenzelm@20809
    61
  with S show False by simp
wenzelm@20809
    62
qed
wenzelm@20809
    63
wenzelm@20809
    64
text {*
wenzelm@20809
    65
  As a concrete example, we prove that the set of natural numbers is
wenzelm@20809
    66
  infinite.
wenzelm@20809
    67
*}
wenzelm@20809
    68
wenzelm@20809
    69
lemma finite_nat_bounded:
wenzelm@20809
    70
  assumes S: "finite (S::nat set)"
wenzelm@20809
    71
  shows "\<exists>k. S \<subseteq> {..<k}"  (is "\<exists>k. ?bounded S k")
wenzelm@20809
    72
using S
wenzelm@20809
    73
proof induct
wenzelm@20809
    74
  have "?bounded {} 0" by simp
wenzelm@20809
    75
  then show "\<exists>k. ?bounded {} k" ..
wenzelm@20809
    76
next
wenzelm@20809
    77
  fix S x
wenzelm@20809
    78
  assume "\<exists>k. ?bounded S k"
wenzelm@20809
    79
  then obtain k where k: "?bounded S k" ..
wenzelm@20809
    80
  show "\<exists>k. ?bounded (insert x S) k"
wenzelm@20809
    81
  proof (cases "x < k")
wenzelm@20809
    82
    case True
wenzelm@20809
    83
    with k show ?thesis by auto
wenzelm@20809
    84
  next
wenzelm@20809
    85
    case False
wenzelm@20809
    86
    with k have "?bounded S (Suc x)" by auto
wenzelm@20809
    87
    then show ?thesis by auto
wenzelm@20809
    88
  qed
wenzelm@20809
    89
qed
wenzelm@20809
    90
wenzelm@20809
    91
lemma finite_nat_iff_bounded:
wenzelm@20809
    92
  "finite (S::nat set) = (\<exists>k. S \<subseteq> {..<k})"  (is "?lhs = ?rhs")
wenzelm@20809
    93
proof
wenzelm@20809
    94
  assume ?lhs
wenzelm@20809
    95
  then show ?rhs by (rule finite_nat_bounded)
wenzelm@20809
    96
next
wenzelm@20809
    97
  assume ?rhs
wenzelm@20809
    98
  then obtain k where "S \<subseteq> {..<k}" ..
wenzelm@20809
    99
  then show "finite S"
wenzelm@20809
   100
    by (rule finite_subset) simp
wenzelm@20809
   101
qed
wenzelm@20809
   102
wenzelm@20809
   103
lemma finite_nat_iff_bounded_le:
wenzelm@20809
   104
  "finite (S::nat set) = (\<exists>k. S \<subseteq> {..k})"  (is "?lhs = ?rhs")
wenzelm@20809
   105
proof
wenzelm@20809
   106
  assume ?lhs
wenzelm@20809
   107
  then obtain k where "S \<subseteq> {..<k}"
wenzelm@20809
   108
    by (blast dest: finite_nat_bounded)
wenzelm@20809
   109
  then have "S \<subseteq> {..k}" by auto
wenzelm@20809
   110
  then show ?rhs ..
wenzelm@20809
   111
next
wenzelm@20809
   112
  assume ?rhs
wenzelm@20809
   113
  then obtain k where "S \<subseteq> {..k}" ..
wenzelm@20809
   114
  then show "finite S"
wenzelm@20809
   115
    by (rule finite_subset) simp
wenzelm@20809
   116
qed
wenzelm@20809
   117
wenzelm@20809
   118
lemma infinite_nat_iff_unbounded:
wenzelm@20809
   119
  "infinite (S::nat set) = (\<forall>m. \<exists>n. m<n \<and> n\<in>S)"
wenzelm@20809
   120
  (is "?lhs = ?rhs")
wenzelm@20809
   121
proof
wenzelm@20809
   122
  assume ?lhs
wenzelm@20809
   123
  show ?rhs
wenzelm@20809
   124
  proof (rule ccontr)
wenzelm@20809
   125
    assume "\<not> ?rhs"
wenzelm@20809
   126
    then obtain m where m: "\<forall>n. m<n \<longrightarrow> n\<notin>S" by blast
wenzelm@20809
   127
    then have "S \<subseteq> {..m}"
wenzelm@20809
   128
      by (auto simp add: sym [OF linorder_not_less])
wenzelm@20809
   129
    with `?lhs` show False
wenzelm@20809
   130
      by (simp add: finite_nat_iff_bounded_le)
wenzelm@20809
   131
  qed
wenzelm@20809
   132
next
wenzelm@20809
   133
  assume ?rhs
wenzelm@20809
   134
  show ?lhs
wenzelm@20809
   135
  proof
wenzelm@20809
   136
    assume "finite S"
wenzelm@20809
   137
    then obtain m where "S \<subseteq> {..m}"
wenzelm@20809
   138
      by (auto simp add: finite_nat_iff_bounded_le)
wenzelm@20809
   139
    then have "\<forall>n. m<n \<longrightarrow> n\<notin>S" by auto
wenzelm@20809
   140
    with `?rhs` show False by blast
wenzelm@20809
   141
  qed
wenzelm@20809
   142
qed
wenzelm@20809
   143
wenzelm@20809
   144
lemma infinite_nat_iff_unbounded_le:
wenzelm@20809
   145
  "infinite (S::nat set) = (\<forall>m. \<exists>n. m\<le>n \<and> n\<in>S)"
wenzelm@20809
   146
  (is "?lhs = ?rhs")
wenzelm@20809
   147
proof
wenzelm@20809
   148
  assume ?lhs
wenzelm@20809
   149
  show ?rhs
wenzelm@20809
   150
  proof
wenzelm@20809
   151
    fix m
wenzelm@20809
   152
    from `?lhs` obtain n where "m<n \<and> n\<in>S"
wenzelm@20809
   153
      by (auto simp add: infinite_nat_iff_unbounded)
wenzelm@20809
   154
    then have "m\<le>n \<and> n\<in>S" by simp
wenzelm@20809
   155
    then show "\<exists>n. m \<le> n \<and> n \<in> S" ..
wenzelm@20809
   156
  qed
wenzelm@20809
   157
next
wenzelm@20809
   158
  assume ?rhs
wenzelm@20809
   159
  show ?lhs
wenzelm@20809
   160
  proof (auto simp add: infinite_nat_iff_unbounded)
wenzelm@20809
   161
    fix m
wenzelm@20809
   162
    from `?rhs` obtain n where "Suc m \<le> n \<and> n\<in>S"
wenzelm@20809
   163
      by blast
wenzelm@20809
   164
    then have "m<n \<and> n\<in>S" by simp
wenzelm@20809
   165
    then show "\<exists>n. m < n \<and> n \<in> S" ..
wenzelm@20809
   166
  qed
wenzelm@20809
   167
qed
wenzelm@20809
   168
wenzelm@20809
   169
text {*
wenzelm@20809
   170
  For a set of natural numbers to be infinite, it is enough to know
wenzelm@20809
   171
  that for any number larger than some @{text k}, there is some larger
wenzelm@20809
   172
  number that is an element of the set.
wenzelm@20809
   173
*}
wenzelm@20809
   174
wenzelm@20809
   175
lemma unbounded_k_infinite:
wenzelm@20809
   176
  assumes k: "\<forall>m. k<m \<longrightarrow> (\<exists>n. m<n \<and> n\<in>S)"
wenzelm@20809
   177
  shows "infinite (S::nat set)"
wenzelm@20809
   178
proof -
wenzelm@20809
   179
  {
wenzelm@20809
   180
    fix m have "\<exists>n. m<n \<and> n\<in>S"
wenzelm@20809
   181
    proof (cases "k<m")
wenzelm@20809
   182
      case True
wenzelm@20809
   183
      with k show ?thesis by blast
wenzelm@20809
   184
    next
wenzelm@20809
   185
      case False
wenzelm@20809
   186
      from k obtain n where "Suc k < n \<and> n\<in>S" by auto
wenzelm@20809
   187
      with False have "m<n \<and> n\<in>S" by auto
wenzelm@20809
   188
      then show ?thesis ..
wenzelm@20809
   189
    qed
wenzelm@20809
   190
  }
wenzelm@20809
   191
  then show ?thesis
wenzelm@20809
   192
    by (auto simp add: infinite_nat_iff_unbounded)
wenzelm@20809
   193
qed
wenzelm@20809
   194
wenzelm@20809
   195
lemma nat_infinite [simp]: "infinite (UNIV :: nat set)"
wenzelm@20809
   196
  by (auto simp add: infinite_nat_iff_unbounded)
wenzelm@20809
   197
wenzelm@20809
   198
lemma nat_not_finite [elim]: "finite (UNIV::nat set) \<Longrightarrow> R"
wenzelm@20809
   199
  by simp
wenzelm@20809
   200
wenzelm@20809
   201
text {*
wenzelm@20809
   202
  Every infinite set contains a countable subset. More precisely we
wenzelm@20809
   203
  show that a set @{text S} is infinite if and only if there exists an
wenzelm@20809
   204
  injective function from the naturals into @{text S}.
wenzelm@20809
   205
*}
wenzelm@20809
   206
wenzelm@20809
   207
lemma range_inj_infinite:
wenzelm@20809
   208
  "inj (f::nat \<Rightarrow> 'a) \<Longrightarrow> infinite (range f)"
wenzelm@20809
   209
proof
huffman@27407
   210
  assume "finite (range f)" and "inj f"
wenzelm@20809
   211
  then have "finite (UNIV::nat set)"
huffman@27407
   212
    by (rule finite_imageD)
wenzelm@20809
   213
  then show False by simp
wenzelm@20809
   214
qed
wenzelm@20809
   215
paulson@22226
   216
lemma int_infinite [simp]:
paulson@22226
   217
  shows "infinite (UNIV::int set)"
paulson@22226
   218
proof -
paulson@22226
   219
  from inj_int have "infinite (range int)" by (rule range_inj_infinite)
paulson@22226
   220
  moreover 
paulson@22226
   221
  have "range int \<subseteq> (UNIV::int set)" by simp
paulson@22226
   222
  ultimately show "infinite (UNIV::int set)" by (simp add: infinite_super)
paulson@22226
   223
qed
paulson@22226
   224
wenzelm@20809
   225
text {*
wenzelm@20809
   226
  The ``only if'' direction is harder because it requires the
wenzelm@20809
   227
  construction of a sequence of pairwise different elements of an
wenzelm@20809
   228
  infinite set @{text S}. The idea is to construct a sequence of
wenzelm@20809
   229
  non-empty and infinite subsets of @{text S} obtained by successively
wenzelm@20809
   230
  removing elements of @{text S}.
wenzelm@20809
   231
*}
wenzelm@20809
   232
wenzelm@20809
   233
lemma linorder_injI:
wenzelm@20809
   234
  assumes hyp: "!!x y. x < (y::'a::linorder) ==> f x \<noteq> f y"
wenzelm@20809
   235
  shows "inj f"
wenzelm@20809
   236
proof (rule inj_onI)
wenzelm@20809
   237
  fix x y
wenzelm@20809
   238
  assume f_eq: "f x = f y"
wenzelm@20809
   239
  show "x = y"
wenzelm@20809
   240
  proof (rule linorder_cases)
wenzelm@20809
   241
    assume "x < y"
wenzelm@20809
   242
    with hyp have "f x \<noteq> f y" by blast
wenzelm@20809
   243
    with f_eq show ?thesis by simp
wenzelm@20809
   244
  next
wenzelm@20809
   245
    assume "x = y"
wenzelm@20809
   246
    then show ?thesis .
wenzelm@20809
   247
  next
wenzelm@20809
   248
    assume "y < x"
wenzelm@20809
   249
    with hyp have "f y \<noteq> f x" by blast
wenzelm@20809
   250
    with f_eq show ?thesis by simp
wenzelm@20809
   251
  qed
wenzelm@20809
   252
qed
wenzelm@20809
   253
wenzelm@20809
   254
lemma infinite_countable_subset:
wenzelm@20809
   255
  assumes inf: "infinite (S::'a set)"
wenzelm@20809
   256
  shows "\<exists>f. inj (f::nat \<Rightarrow> 'a) \<and> range f \<subseteq> S"
wenzelm@20809
   257
proof -
wenzelm@20809
   258
  def Sseq \<equiv> "nat_rec S (\<lambda>n T. T - {SOME e. e \<in> T})"
wenzelm@20809
   259
  def pick \<equiv> "\<lambda>n. (SOME e. e \<in> Sseq n)"
wenzelm@20809
   260
  have Sseq_inf: "\<And>n. infinite (Sseq n)"
wenzelm@20809
   261
  proof -
wenzelm@20809
   262
    fix n
wenzelm@20809
   263
    show "infinite (Sseq n)"
wenzelm@20809
   264
    proof (induct n)
wenzelm@20809
   265
      from inf show "infinite (Sseq 0)"
wenzelm@20809
   266
        by (simp add: Sseq_def)
wenzelm@20809
   267
    next
wenzelm@20809
   268
      fix n
wenzelm@20809
   269
      assume "infinite (Sseq n)" then show "infinite (Sseq (Suc n))"
wenzelm@20809
   270
        by (simp add: Sseq_def infinite_remove)
wenzelm@20809
   271
    qed
wenzelm@20809
   272
  qed
wenzelm@20809
   273
  have Sseq_S: "\<And>n. Sseq n \<subseteq> S"
wenzelm@20809
   274
  proof -
wenzelm@20809
   275
    fix n
wenzelm@20809
   276
    show "Sseq n \<subseteq> S"
wenzelm@20809
   277
      by (induct n) (auto simp add: Sseq_def)
wenzelm@20809
   278
  qed
wenzelm@20809
   279
  have Sseq_pick: "\<And>n. pick n \<in> Sseq n"
wenzelm@20809
   280
  proof -
wenzelm@20809
   281
    fix n
wenzelm@20809
   282
    show "pick n \<in> Sseq n"
wenzelm@20809
   283
    proof (unfold pick_def, rule someI_ex)
wenzelm@20809
   284
      from Sseq_inf have "infinite (Sseq n)" .
wenzelm@20809
   285
      then have "Sseq n \<noteq> {}" by auto
wenzelm@20809
   286
      then show "\<exists>x. x \<in> Sseq n" by auto
wenzelm@20809
   287
    qed
wenzelm@20809
   288
  qed
wenzelm@20809
   289
  with Sseq_S have rng: "range pick \<subseteq> S"
wenzelm@20809
   290
    by auto
wenzelm@20809
   291
  have pick_Sseq_gt: "\<And>n m. pick n \<notin> Sseq (n + Suc m)"
wenzelm@20809
   292
  proof -
wenzelm@20809
   293
    fix n m
wenzelm@20809
   294
    show "pick n \<notin> Sseq (n + Suc m)"
wenzelm@20809
   295
      by (induct m) (auto simp add: Sseq_def pick_def)
wenzelm@20809
   296
  qed
wenzelm@20809
   297
  have pick_pick: "\<And>n m. pick n \<noteq> pick (n + Suc m)"
wenzelm@20809
   298
  proof -
wenzelm@20809
   299
    fix n m
wenzelm@20809
   300
    from Sseq_pick have "pick (n + Suc m) \<in> Sseq (n + Suc m)" .
wenzelm@20809
   301
    moreover from pick_Sseq_gt
wenzelm@20809
   302
    have "pick n \<notin> Sseq (n + Suc m)" .
wenzelm@20809
   303
    ultimately show "pick n \<noteq> pick (n + Suc m)"
wenzelm@20809
   304
      by auto
wenzelm@20809
   305
  qed
wenzelm@20809
   306
  have inj: "inj pick"
wenzelm@20809
   307
  proof (rule linorder_injI)
wenzelm@20809
   308
    fix i j :: nat
wenzelm@20809
   309
    assume "i < j"
wenzelm@20809
   310
    show "pick i \<noteq> pick j"
wenzelm@20809
   311
    proof
wenzelm@20809
   312
      assume eq: "pick i = pick j"
wenzelm@20809
   313
      from `i < j` obtain k where "j = i + Suc k"
wenzelm@20809
   314
        by (auto simp add: less_iff_Suc_add)
wenzelm@20809
   315
      with pick_pick have "pick i \<noteq> pick j" by simp
wenzelm@20809
   316
      with eq show False by simp
wenzelm@20809
   317
    qed
wenzelm@20809
   318
  qed
wenzelm@20809
   319
  from rng inj show ?thesis by auto
wenzelm@20809
   320
qed
wenzelm@20809
   321
wenzelm@20809
   322
lemma infinite_iff_countable_subset:
wenzelm@20809
   323
    "infinite S = (\<exists>f. inj (f::nat \<Rightarrow> 'a) \<and> range f \<subseteq> S)"
wenzelm@20809
   324
  by (auto simp add: infinite_countable_subset range_inj_infinite infinite_super)
wenzelm@20809
   325
wenzelm@20809
   326
text {*
wenzelm@20809
   327
  For any function with infinite domain and finite range there is some
wenzelm@20809
   328
  element that is the image of infinitely many domain elements.  In
wenzelm@20809
   329
  particular, any infinite sequence of elements from a finite set
wenzelm@20809
   330
  contains some element that occurs infinitely often.
wenzelm@20809
   331
*}
wenzelm@20809
   332
wenzelm@20809
   333
lemma inf_img_fin_dom:
wenzelm@20809
   334
  assumes img: "finite (f`A)" and dom: "infinite A"
wenzelm@20809
   335
  shows "\<exists>y \<in> f`A. infinite (f -` {y})"
wenzelm@20809
   336
proof (rule ccontr)
wenzelm@20809
   337
  assume "\<not> ?thesis"
wenzelm@20809
   338
  with img have "finite (UN y:f`A. f -` {y})" by (blast intro: finite_UN_I)
wenzelm@20809
   339
  moreover have "A \<subseteq> (UN y:f`A. f -` {y})" by auto
wenzelm@20809
   340
  moreover note dom
wenzelm@20809
   341
  ultimately show False by (simp add: infinite_super)
wenzelm@20809
   342
qed
wenzelm@20809
   343
wenzelm@20809
   344
lemma inf_img_fin_domE:
wenzelm@20809
   345
  assumes "finite (f`A)" and "infinite A"
wenzelm@20809
   346
  obtains y where "y \<in> f`A" and "infinite (f -` {y})"
wenzelm@23394
   347
  using assms by (blast dest: inf_img_fin_dom)
wenzelm@20809
   348
wenzelm@20809
   349
wenzelm@20809
   350
subsection "Infinitely Many and Almost All"
wenzelm@20809
   351
wenzelm@20809
   352
text {*
wenzelm@20809
   353
  We often need to reason about the existence of infinitely many
wenzelm@20809
   354
  (resp., all but finitely many) objects satisfying some predicate, so
wenzelm@20809
   355
  we introduce corresponding binders and their proof rules.
wenzelm@20809
   356
*}
wenzelm@20809
   357
wenzelm@20809
   358
definition
berghofe@22432
   359
  Inf_many :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "INFM " 10) where
wenzelm@20809
   360
  "Inf_many P = infinite {x. P x}"
wenzelm@21404
   361
wenzelm@21404
   362
definition
wenzelm@21404
   363
  Alm_all :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "MOST " 10) where
berghofe@22432
   364
  "Alm_all P = (\<not> (INFM x. \<not> P x))"
wenzelm@20809
   365
wenzelm@21210
   366
notation (xsymbols)
wenzelm@21404
   367
  Inf_many  (binder "\<exists>\<^sub>\<infinity>" 10) and
wenzelm@20809
   368
  Alm_all  (binder "\<forall>\<^sub>\<infinity>" 10)
wenzelm@20809
   369
wenzelm@21210
   370
notation (HTML output)
wenzelm@21404
   371
  Inf_many  (binder "\<exists>\<^sub>\<infinity>" 10) and
wenzelm@20809
   372
  Alm_all  (binder "\<forall>\<^sub>\<infinity>" 10)
wenzelm@20809
   373
huffman@34110
   374
lemma INFM_iff_infinite: "(INFM x. P x) \<longleftrightarrow> infinite {x. P x}"
huffman@34110
   375
  unfolding Inf_many_def ..
wenzelm@20809
   376
huffman@34110
   377
lemma MOST_iff_cofinite: "(MOST x. P x) \<longleftrightarrow> finite {x. \<not> P x}"
huffman@34110
   378
  unfolding Alm_all_def Inf_many_def by simp
huffman@34110
   379
huffman@34110
   380
(* legacy name *)
huffman@34110
   381
lemmas MOST_iff_finiteNeg = MOST_iff_cofinite
huffman@34110
   382
huffman@34110
   383
lemma not_INFM [simp]: "\<not> (INFM x. P x) \<longleftrightarrow> (MOST x. \<not> P x)"
huffman@34110
   384
  unfolding Alm_all_def not_not ..
huffman@34110
   385
huffman@34110
   386
lemma not_MOST [simp]: "\<not> (MOST x. P x) \<longleftrightarrow> (INFM x. \<not> P x)"
huffman@34110
   387
  unfolding Alm_all_def not_not ..
huffman@34110
   388
huffman@34110
   389
lemma INFM_const [simp]: "(INFM x::'a. P) \<longleftrightarrow> P \<and> infinite (UNIV::'a set)"
huffman@34110
   390
  unfolding Inf_many_def by simp
huffman@34110
   391
huffman@34110
   392
lemma MOST_const [simp]: "(MOST x::'a. P) \<longleftrightarrow> P \<or> finite (UNIV::'a set)"
huffman@34110
   393
  unfolding Alm_all_def by simp
huffman@34110
   394
huffman@34110
   395
lemma INFM_EX: "(\<exists>\<^sub>\<infinity>x. P x) \<Longrightarrow> (\<exists>x. P x)"
huffman@34110
   396
  by (erule contrapos_pp, simp)
wenzelm@20809
   397
wenzelm@20809
   398
lemma ALL_MOST: "\<forall>x. P x \<Longrightarrow> \<forall>\<^sub>\<infinity>x. P x"
huffman@34110
   399
  by simp
huffman@34110
   400
huffman@34110
   401
lemma INFM_E: assumes "INFM x. P x" obtains x where "P x"
huffman@34110
   402
  using INFM_EX [OF assms] by (rule exE)
huffman@34110
   403
huffman@34110
   404
lemma MOST_I: assumes "\<And>x. P x" shows "MOST x. P x"
huffman@34110
   405
  using assms by simp
wenzelm@20809
   406
huffman@27407
   407
lemma INFM_mono:
wenzelm@20809
   408
  assumes inf: "\<exists>\<^sub>\<infinity>x. P x" and q: "\<And>x. P x \<Longrightarrow> Q x"
wenzelm@20809
   409
  shows "\<exists>\<^sub>\<infinity>x. Q x"
wenzelm@20809
   410
proof -
wenzelm@20809
   411
  from inf have "infinite {x. P x}" unfolding Inf_many_def .
wenzelm@20809
   412
  moreover from q have "{x. P x} \<subseteq> {x. Q x}" by auto
wenzelm@20809
   413
  ultimately show ?thesis
wenzelm@20809
   414
    by (simp add: Inf_many_def infinite_super)
wenzelm@20809
   415
qed
wenzelm@20809
   416
wenzelm@20809
   417
lemma MOST_mono: "\<forall>\<^sub>\<infinity>x. P x \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> \<forall>\<^sub>\<infinity>x. Q x"
huffman@27407
   418
  unfolding Alm_all_def by (blast intro: INFM_mono)
wenzelm@20809
   419
huffman@27407
   420
lemma INFM_disj_distrib:
huffman@27407
   421
  "(\<exists>\<^sub>\<infinity>x. P x \<or> Q x) \<longleftrightarrow> (\<exists>\<^sub>\<infinity>x. P x) \<or> (\<exists>\<^sub>\<infinity>x. Q x)"
huffman@27407
   422
  unfolding Inf_many_def by (simp add: Collect_disj_eq)
huffman@27407
   423
huffman@34110
   424
lemma INFM_imp_distrib:
huffman@34110
   425
  "(INFM x. P x \<longrightarrow> Q x) \<longleftrightarrow> ((MOST x. P x) \<longrightarrow> (INFM x. Q x))"
huffman@34110
   426
  by (simp only: imp_conv_disj INFM_disj_distrib not_MOST)
huffman@34110
   427
huffman@27407
   428
lemma MOST_conj_distrib:
huffman@27407
   429
  "(\<forall>\<^sub>\<infinity>x. P x \<and> Q x) \<longleftrightarrow> (\<forall>\<^sub>\<infinity>x. P x) \<and> (\<forall>\<^sub>\<infinity>x. Q x)"
huffman@27407
   430
  unfolding Alm_all_def by (simp add: INFM_disj_distrib del: disj_not1)
huffman@27407
   431
huffman@34110
   432
lemma MOST_conjI:
huffman@34110
   433
  "MOST x. P x \<Longrightarrow> MOST x. Q x \<Longrightarrow> MOST x. P x \<and> Q x"
huffman@34110
   434
  by (simp add: MOST_conj_distrib)
huffman@34110
   435
huffman@34111
   436
lemma INFM_conjI:
huffman@34111
   437
  "INFM x. P x \<Longrightarrow> MOST x. Q x \<Longrightarrow> INFM x. P x \<and> Q x"
huffman@34111
   438
  unfolding MOST_iff_cofinite INFM_iff_infinite
huffman@34111
   439
  apply (drule (1) Diff_infinite_finite)
huffman@34111
   440
  apply (simp add: Collect_conj_eq Collect_neg_eq)
huffman@34111
   441
  done
huffman@34111
   442
huffman@27407
   443
lemma MOST_rev_mp:
huffman@27407
   444
  assumes "\<forall>\<^sub>\<infinity>x. P x" and "\<forall>\<^sub>\<infinity>x. P x \<longrightarrow> Q x"
huffman@27407
   445
  shows "\<forall>\<^sub>\<infinity>x. Q x"
huffman@27407
   446
proof -
huffman@27407
   447
  have "\<forall>\<^sub>\<infinity>x. P x \<and> (P x \<longrightarrow> Q x)"
huffman@34110
   448
    using assms by (rule MOST_conjI)
huffman@27407
   449
  thus ?thesis by (rule MOST_mono) simp
huffman@27407
   450
qed
huffman@27407
   451
huffman@34110
   452
lemma MOST_imp_iff:
huffman@34110
   453
  assumes "MOST x. P x"
huffman@34110
   454
  shows "(MOST x. P x \<longrightarrow> Q x) \<longleftrightarrow> (MOST x. Q x)"
huffman@34110
   455
proof
huffman@34110
   456
  assume "MOST x. P x \<longrightarrow> Q x"
huffman@34110
   457
  with assms show "MOST x. Q x" by (rule MOST_rev_mp)
huffman@34110
   458
next
huffman@34110
   459
  assume "MOST x. Q x"
huffman@34110
   460
  then show "MOST x. P x \<longrightarrow> Q x" by (rule MOST_mono) simp
huffman@34110
   461
qed
huffman@27407
   462
huffman@34110
   463
lemma INFM_MOST_simps [simp]:
huffman@34110
   464
  "\<And>P Q. (INFM x. P x \<and> Q) \<longleftrightarrow> (INFM x. P x) \<and> Q"
huffman@34110
   465
  "\<And>P Q. (INFM x. P \<and> Q x) \<longleftrightarrow> P \<and> (INFM x. Q x)"
huffman@34110
   466
  "\<And>P Q. (MOST x. P x \<or> Q) \<longleftrightarrow> (MOST x. P x) \<or> Q"
huffman@34110
   467
  "\<And>P Q. (MOST x. P \<or> Q x) \<longleftrightarrow> P \<or> (MOST x. Q x)"
huffman@34110
   468
  "\<And>P Q. (MOST x. P x \<longrightarrow> Q) \<longleftrightarrow> ((INFM x. P x) \<longrightarrow> Q)"
huffman@34110
   469
  "\<And>P Q. (MOST x. P \<longrightarrow> Q x) \<longleftrightarrow> (P \<longrightarrow> (MOST x. Q x))"
huffman@34110
   470
  unfolding Alm_all_def Inf_many_def
huffman@34110
   471
  by (simp_all add: Collect_conj_eq)
huffman@27407
   472
huffman@34110
   473
text {* Properties of quantifiers with injective functions. *}
huffman@27407
   474
huffman@34110
   475
lemma INFM_inj:
huffman@34110
   476
  "INFM x. P (f x) \<Longrightarrow> inj f \<Longrightarrow> INFM x. P x"
huffman@34110
   477
  unfolding INFM_iff_infinite
huffman@34110
   478
  by (clarify, drule (1) finite_vimageI, simp)
huffman@34110
   479
huffman@34110
   480
lemma MOST_inj:
huffman@34110
   481
  "MOST x. P x \<Longrightarrow> inj f \<Longrightarrow> MOST x. P (f x)"
huffman@34110
   482
  unfolding MOST_iff_cofinite
huffman@34110
   483
  by (drule (1) finite_vimageI, simp)
huffman@34110
   484
huffman@34110
   485
text {* Properties of quantifiers with singletons. *}
huffman@34110
   486
huffman@34110
   487
lemma not_INFM_eq [simp]:
huffman@34110
   488
  "\<not> (INFM x. x = a)"
huffman@34110
   489
  "\<not> (INFM x. a = x)"
huffman@34110
   490
  unfolding INFM_iff_infinite by simp_all
huffman@34110
   491
huffman@34110
   492
lemma MOST_neq [simp]:
huffman@34110
   493
  "MOST x. x \<noteq> a"
huffman@34110
   494
  "MOST x. a \<noteq> x"
huffman@34110
   495
  unfolding MOST_iff_cofinite by simp_all
huffman@34110
   496
huffman@34110
   497
lemma INFM_neq [simp]:
huffman@34110
   498
  "(INFM x::'a. x \<noteq> a) \<longleftrightarrow> infinite (UNIV::'a set)"
huffman@34110
   499
  "(INFM x::'a. a \<noteq> x) \<longleftrightarrow> infinite (UNIV::'a set)"
huffman@34110
   500
  unfolding INFM_iff_infinite by simp_all
huffman@34110
   501
huffman@34110
   502
lemma MOST_eq [simp]:
huffman@34110
   503
  "(MOST x::'a. x = a) \<longleftrightarrow> finite (UNIV::'a set)"
huffman@34110
   504
  "(MOST x::'a. a = x) \<longleftrightarrow> finite (UNIV::'a set)"
huffman@34110
   505
  unfolding MOST_iff_cofinite by simp_all
huffman@34110
   506
huffman@34110
   507
lemma MOST_eq_imp:
huffman@34110
   508
  "MOST x. x = a \<longrightarrow> P x"
huffman@34110
   509
  "MOST x. a = x \<longrightarrow> P x"
huffman@34110
   510
  unfolding MOST_iff_cofinite by simp_all
huffman@34110
   511
huffman@34110
   512
text {* Properties of quantifiers over the naturals. *}
huffman@27407
   513
huffman@27407
   514
lemma INFM_nat: "(\<exists>\<^sub>\<infinity>n. P (n::nat)) = (\<forall>m. \<exists>n. m<n \<and> P n)"
wenzelm@20809
   515
  by (simp add: Inf_many_def infinite_nat_iff_unbounded)
wenzelm@20809
   516
huffman@27407
   517
lemma INFM_nat_le: "(\<exists>\<^sub>\<infinity>n. P (n::nat)) = (\<forall>m. \<exists>n. m\<le>n \<and> P n)"
wenzelm@20809
   518
  by (simp add: Inf_many_def infinite_nat_iff_unbounded_le)
wenzelm@20809
   519
wenzelm@20809
   520
lemma MOST_nat: "(\<forall>\<^sub>\<infinity>n. P (n::nat)) = (\<exists>m. \<forall>n. m<n \<longrightarrow> P n)"
huffman@27407
   521
  by (simp add: Alm_all_def INFM_nat)
wenzelm@20809
   522
wenzelm@20809
   523
lemma MOST_nat_le: "(\<forall>\<^sub>\<infinity>n. P (n::nat)) = (\<exists>m. \<forall>n. m\<le>n \<longrightarrow> P n)"
huffman@27407
   524
  by (simp add: Alm_all_def INFM_nat_le)
wenzelm@20809
   525
wenzelm@20809
   526
wenzelm@20809
   527
subsection "Enumeration of an Infinite Set"
wenzelm@20809
   528
wenzelm@20809
   529
text {*
wenzelm@20809
   530
  The set's element type must be wellordered (e.g. the natural numbers).
wenzelm@20809
   531
*}
wenzelm@20809
   532
haftmann@34928
   533
primrec (in wellorder) enumerate :: "'a set \<Rightarrow> nat \<Rightarrow> 'a" where
haftmann@34928
   534
    enumerate_0:   "enumerate S 0       = (LEAST n. n \<in> S)"
haftmann@34928
   535
  | enumerate_Suc: "enumerate S (Suc n) = enumerate (S - {LEAST n. n \<in> S}) n"
wenzelm@20809
   536
wenzelm@20809
   537
lemma enumerate_Suc':
wenzelm@20809
   538
    "enumerate S (Suc n) = enumerate (S - {enumerate S 0}) n"
wenzelm@20809
   539
  by simp
wenzelm@20809
   540
wenzelm@20809
   541
lemma enumerate_in_set: "infinite S \<Longrightarrow> enumerate S n : S"
nipkow@29838
   542
apply (induct n arbitrary: S)
nipkow@29838
   543
 apply (fastsimp intro: LeastI dest!: infinite_imp_nonempty)
nipkow@29838
   544
apply simp
nipkow@29838
   545
apply (metis Collect_def Collect_mem_eq DiffE infinite_remove)
nipkow@29838
   546
done
wenzelm@20809
   547
wenzelm@20809
   548
declare enumerate_0 [simp del] enumerate_Suc [simp del]
wenzelm@20809
   549
wenzelm@20809
   550
lemma enumerate_step: "infinite S \<Longrightarrow> enumerate S n < enumerate S (Suc n)"
wenzelm@20809
   551
  apply (induct n arbitrary: S)
wenzelm@20809
   552
   apply (rule order_le_neq_trans)
wenzelm@20809
   553
    apply (simp add: enumerate_0 Least_le enumerate_in_set)
wenzelm@20809
   554
   apply (simp only: enumerate_Suc')
wenzelm@20809
   555
   apply (subgoal_tac "enumerate (S - {enumerate S 0}) 0 : S - {enumerate S 0}")
wenzelm@20809
   556
    apply (blast intro: sym)
wenzelm@20809
   557
   apply (simp add: enumerate_in_set del: Diff_iff)
wenzelm@20809
   558
  apply (simp add: enumerate_Suc')
wenzelm@20809
   559
  done
wenzelm@20809
   560
wenzelm@20809
   561
lemma enumerate_mono: "m<n \<Longrightarrow> infinite S \<Longrightarrow> enumerate S m < enumerate S n"
wenzelm@20809
   562
  apply (erule less_Suc_induct)
wenzelm@20809
   563
  apply (auto intro: enumerate_step)
wenzelm@20809
   564
  done
wenzelm@20809
   565
wenzelm@20809
   566
wenzelm@20809
   567
subsection "Miscellaneous"
wenzelm@20809
   568
wenzelm@20809
   569
text {*
wenzelm@20809
   570
  A few trivial lemmas about sets that contain at most one element.
wenzelm@20809
   571
  These simplify the reasoning about deterministic automata.
wenzelm@20809
   572
*}
wenzelm@20809
   573
wenzelm@20809
   574
definition
wenzelm@21404
   575
  atmost_one :: "'a set \<Rightarrow> bool" where
wenzelm@20809
   576
  "atmost_one S = (\<forall>x y. x\<in>S \<and> y\<in>S \<longrightarrow> x=y)"
wenzelm@20809
   577
wenzelm@20809
   578
lemma atmost_one_empty: "S = {} \<Longrightarrow> atmost_one S"
wenzelm@20809
   579
  by (simp add: atmost_one_def)
wenzelm@20809
   580
wenzelm@20809
   581
lemma atmost_one_singleton: "S = {x} \<Longrightarrow> atmost_one S"
wenzelm@20809
   582
  by (simp add: atmost_one_def)
wenzelm@20809
   583
wenzelm@20809
   584
lemma atmost_one_unique [elim]: "atmost_one S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> y = x"
wenzelm@20809
   585
  by (simp add: atmost_one_def)
wenzelm@20809
   586
wenzelm@20809
   587
end