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