src/HOL/Finite_Set.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62093 bd73a2279fcd
child 62390 842917225d56
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
wenzelm@12396
     1
(*  Title:      HOL/Finite_Set.thy
wenzelm@12396
     2
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
blanchet@55020
     3
                with contributions by Jeremy Avigad and Andrei Popescu
wenzelm@12396
     4
*)
wenzelm@12396
     5
wenzelm@60758
     6
section \<open>Finite sets\<close>
wenzelm@12396
     7
nipkow@15131
     8
theory Finite_Set
traytel@55096
     9
imports Product_Type Sum_Type Nat
nipkow@15131
    10
begin
wenzelm@12396
    11
wenzelm@60758
    12
subsection \<open>Predicate for finite sets\<close>
wenzelm@12396
    13
wenzelm@61681
    14
context
wenzelm@62093
    15
  notes [[inductive_internals]]
wenzelm@61681
    16
begin
wenzelm@61681
    17
haftmann@41656
    18
inductive finite :: "'a set \<Rightarrow> bool"
berghofe@22262
    19
  where
berghofe@22262
    20
    emptyI [simp, intro!]: "finite {}"
haftmann@41656
    21
  | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)"
haftmann@41656
    22
wenzelm@61681
    23
end
wenzelm@61681
    24
wenzelm@60758
    25
simproc_setup finite_Collect ("finite (Collect P)") = \<open>K Set_Comprehension_Pointfree.simproc\<close>
rafal@48109
    26
traytel@54611
    27
declare [[simproc del: finite_Collect]]
traytel@54611
    28
haftmann@41656
    29
lemma finite_induct [case_names empty insert, induct set: finite]:
wenzelm@61799
    30
  \<comment> \<open>Discharging \<open>x \<notin> F\<close> entails extra work.\<close>
haftmann@41656
    31
  assumes "finite F"
haftmann@41656
    32
  assumes "P {}"
haftmann@41656
    33
    and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
haftmann@41656
    34
  shows "P F"
wenzelm@60758
    35
using \<open>finite F\<close>
wenzelm@46898
    36
proof induct
haftmann@41656
    37
  show "P {}" by fact
haftmann@41656
    38
  fix x F assume F: "finite F" and P: "P F"
haftmann@41656
    39
  show "P (insert x F)"
haftmann@41656
    40
  proof cases
haftmann@41656
    41
    assume "x \<in> F"
haftmann@41656
    42
    hence "insert x F = F" by (rule insert_absorb)
haftmann@41656
    43
    with P show ?thesis by (simp only:)
haftmann@41656
    44
  next
haftmann@41656
    45
    assume "x \<notin> F"
haftmann@41656
    46
    from F this P show ?thesis by (rule insert)
haftmann@41656
    47
  qed
haftmann@41656
    48
qed
haftmann@41656
    49
haftmann@51622
    50
lemma infinite_finite_induct [case_names infinite empty insert]:
haftmann@51622
    51
  assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A"
haftmann@51622
    52
  assumes empty: "P {}"
haftmann@51622
    53
  assumes insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
haftmann@51622
    54
  shows "P A"
haftmann@51622
    55
proof (cases "finite A")
haftmann@51622
    56
  case False with infinite show ?thesis .
haftmann@51622
    57
next
haftmann@51622
    58
  case True then show ?thesis by (induct A) (fact empty insert)+
haftmann@51622
    59
qed
haftmann@51622
    60
haftmann@41656
    61
wenzelm@60758
    62
subsubsection \<open>Choice principles\<close>
wenzelm@12396
    63
wenzelm@61799
    64
lemma ex_new_if_finite: \<comment> "does not depend on def of finite at all"
wenzelm@14661
    65
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    66
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    67
proof -
haftmann@28823
    68
  from assms have "A \<noteq> UNIV" by blast
haftmann@41656
    69
  then show ?thesis by blast
wenzelm@12396
    70
qed
wenzelm@12396
    71
wenzelm@60758
    72
text \<open>A finite choice principle. Does not need the SOME choice operator.\<close>
nipkow@15484
    73
nipkow@29923
    74
lemma finite_set_choice:
haftmann@41656
    75
  "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
haftmann@41656
    76
proof (induct rule: finite_induct)
haftmann@41656
    77
  case empty then show ?case by simp
nipkow@29923
    78
next
nipkow@29923
    79
  case (insert a A)
nipkow@29923
    80
  then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
nipkow@29923
    81
  show ?case (is "EX f. ?P f")
nipkow@29923
    82
  proof
nipkow@29923
    83
    show "?P(%x. if x = a then b else f x)" using f ab by auto
nipkow@29923
    84
  qed
nipkow@29923
    85
qed
nipkow@29923
    86
haftmann@23878
    87
wenzelm@60758
    88
subsubsection \<open>Finite sets are the images of initial segments of natural numbers\<close>
nipkow@15392
    89
paulson@15510
    90
lemma finite_imp_nat_seg_image_inj_on:
haftmann@41656
    91
  assumes "finite A" 
haftmann@41656
    92
  shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
wenzelm@46898
    93
using assms
wenzelm@46898
    94
proof induct
nipkow@15392
    95
  case empty
haftmann@41656
    96
  show ?case
haftmann@41656
    97
  proof
haftmann@41656
    98
    show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}" by simp 
paulson@15510
    99
  qed
nipkow@15392
   100
next
nipkow@15392
   101
  case (insert a A)
wenzelm@23389
   102
  have notinA: "a \<notin> A" by fact
paulson@15510
   103
  from insert.hyps obtain n f
paulson@15510
   104
    where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
paulson@15510
   105
  hence "insert a A = f(n:=a) ` {i. i < Suc n}"
paulson@15510
   106
        "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
paulson@15510
   107
    by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
nipkow@15392
   108
  thus ?case by blast
nipkow@15392
   109
qed
nipkow@15392
   110
nipkow@15392
   111
lemma nat_seg_image_imp_finite:
haftmann@41656
   112
  "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
haftmann@41656
   113
proof (induct n arbitrary: A)
nipkow@15392
   114
  case 0 thus ?case by simp
nipkow@15392
   115
next
nipkow@15392
   116
  case (Suc n)
nipkow@15392
   117
  let ?B = "f ` {i. i < n}"
nipkow@15392
   118
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
nipkow@15392
   119
  show ?case
nipkow@15392
   120
  proof cases
nipkow@15392
   121
    assume "\<exists>k<n. f n = f k"
nipkow@15392
   122
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   123
    thus ?thesis using finB by simp
nipkow@15392
   124
  next
nipkow@15392
   125
    assume "\<not>(\<exists> k<n. f n = f k)"
nipkow@15392
   126
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   127
    thus ?thesis using finB by simp
nipkow@15392
   128
  qed
nipkow@15392
   129
qed
nipkow@15392
   130
nipkow@15392
   131
lemma finite_conv_nat_seg_image:
haftmann@41656
   132
  "finite A \<longleftrightarrow> (\<exists>(n::nat) f. A = f ` {i::nat. i < n})"
haftmann@41656
   133
  by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
nipkow@15392
   134
nipkow@32988
   135
lemma finite_imp_inj_to_nat_seg:
haftmann@41656
   136
  assumes "finite A"
haftmann@41656
   137
  shows "\<exists>f n::nat. f ` A = {i. i < n} \<and> inj_on f A"
nipkow@32988
   138
proof -
wenzelm@60758
   139
  from finite_imp_nat_seg_image_inj_on[OF \<open>finite A\<close>]
nipkow@32988
   140
  obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
nipkow@32988
   141
    by (auto simp:bij_betw_def)
nipkow@33057
   142
  let ?f = "the_inv_into {i. i<n} f"
nipkow@32988
   143
  have "inj_on ?f A & ?f ` A = {i. i<n}"
nipkow@33057
   144
    by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
nipkow@32988
   145
  thus ?thesis by blast
nipkow@32988
   146
qed
nipkow@32988
   147
haftmann@41656
   148
lemma finite_Collect_less_nat [iff]:
haftmann@41656
   149
  "finite {n::nat. n < k}"
nipkow@44890
   150
  by (fastforce simp: finite_conv_nat_seg_image)
nipkow@29920
   151
haftmann@41656
   152
lemma finite_Collect_le_nat [iff]:
haftmann@41656
   153
  "finite {n::nat. n \<le> k}"
haftmann@41656
   154
  by (simp add: le_eq_less_or_eq Collect_disj_eq)
nipkow@15392
   155
haftmann@41656
   156
wenzelm@60758
   157
subsubsection \<open>Finiteness and common set operations\<close>
wenzelm@12396
   158
haftmann@41656
   159
lemma rev_finite_subset:
haftmann@41656
   160
  "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A"
haftmann@41656
   161
proof (induct arbitrary: A rule: finite_induct)
haftmann@41656
   162
  case empty
haftmann@41656
   163
  then show ?case by simp
haftmann@41656
   164
next
haftmann@41656
   165
  case (insert x F A)
haftmann@41656
   166
  have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})" by fact+
haftmann@41656
   167
  show "finite A"
haftmann@41656
   168
  proof cases
haftmann@41656
   169
    assume x: "x \<in> A"
haftmann@41656
   170
    with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
haftmann@41656
   171
    with r have "finite (A - {x})" .
haftmann@41656
   172
    hence "finite (insert x (A - {x}))" ..
haftmann@41656
   173
    also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
haftmann@41656
   174
    finally show ?thesis .
wenzelm@12396
   175
  next
wenzelm@60595
   176
    show ?thesis when "A \<subseteq> F"
wenzelm@60595
   177
      using that by fact
haftmann@41656
   178
    assume "x \<notin> A"
haftmann@41656
   179
    with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   180
  qed
wenzelm@12396
   181
qed
wenzelm@12396
   182
haftmann@41656
   183
lemma finite_subset:
haftmann@41656
   184
  "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
haftmann@41656
   185
  by (rule rev_finite_subset)
nipkow@29901
   186
haftmann@41656
   187
lemma finite_UnI:
haftmann@41656
   188
  assumes "finite F" and "finite G"
haftmann@41656
   189
  shows "finite (F \<union> G)"
haftmann@41656
   190
  using assms by induct simp_all
nipkow@31992
   191
haftmann@41656
   192
lemma finite_Un [iff]:
haftmann@41656
   193
  "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G"
haftmann@41656
   194
  by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"])
nipkow@31992
   195
haftmann@41656
   196
lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A"
wenzelm@12396
   197
proof -
haftmann@41656
   198
  have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
haftmann@41656
   199
  then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
wenzelm@23389
   200
  then show ?thesis by simp
wenzelm@12396
   201
qed
wenzelm@12396
   202
haftmann@41656
   203
lemma finite_Int [simp, intro]:
haftmann@41656
   204
  "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)"
haftmann@41656
   205
  by (blast intro: finite_subset)
haftmann@41656
   206
haftmann@41656
   207
lemma finite_Collect_conjI [simp, intro]:
haftmann@41656
   208
  "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
haftmann@41656
   209
  by (simp add: Collect_conj_eq)
haftmann@41656
   210
haftmann@41656
   211
lemma finite_Collect_disjI [simp]:
haftmann@41656
   212
  "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
haftmann@41656
   213
  by (simp add: Collect_disj_eq)
haftmann@41656
   214
haftmann@41656
   215
lemma finite_Diff [simp, intro]:
haftmann@41656
   216
  "finite A \<Longrightarrow> finite (A - B)"
haftmann@41656
   217
  by (rule finite_subset, rule Diff_subset)
nipkow@29901
   218
nipkow@29901
   219
lemma finite_Diff2 [simp]:
haftmann@41656
   220
  assumes "finite B"
haftmann@41656
   221
  shows "finite (A - B) \<longleftrightarrow> finite A"
nipkow@29901
   222
proof -
haftmann@41656
   223
  have "finite A \<longleftrightarrow> finite((A - B) \<union> (A \<inter> B))" by (simp add: Un_Diff_Int)
wenzelm@60758
   224
  also have "\<dots> \<longleftrightarrow> finite (A - B)" using \<open>finite B\<close> by simp
nipkow@29901
   225
  finally show ?thesis ..
nipkow@29901
   226
qed
nipkow@29901
   227
haftmann@41656
   228
lemma finite_Diff_insert [iff]:
haftmann@41656
   229
  "finite (A - insert a B) \<longleftrightarrow> finite (A - B)"
haftmann@41656
   230
proof -
haftmann@41656
   231
  have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
haftmann@41656
   232
  moreover have "A - insert a B = A - B - {a}" by auto
haftmann@41656
   233
  ultimately show ?thesis by simp
haftmann@41656
   234
qed
haftmann@41656
   235
nipkow@29901
   236
lemma finite_compl[simp]:
haftmann@41656
   237
  "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)"
haftmann@41656
   238
  by (simp add: Compl_eq_Diff_UNIV)
wenzelm@12396
   239
nipkow@29916
   240
lemma finite_Collect_not[simp]:
haftmann@41656
   241
  "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
haftmann@41656
   242
  by (simp add: Collect_neg_eq)
haftmann@41656
   243
haftmann@41656
   244
lemma finite_Union [simp, intro]:
haftmann@41656
   245
  "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite(\<Union>A)"
haftmann@41656
   246
  by (induct rule: finite_induct) simp_all
haftmann@41656
   247
haftmann@41656
   248
lemma finite_UN_I [intro]:
haftmann@41656
   249
  "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)"
haftmann@41656
   250
  by (induct rule: finite_induct) simp_all
nipkow@29903
   251
haftmann@41656
   252
lemma finite_UN [simp]:
haftmann@41656
   253
  "finite A \<Longrightarrow> finite (UNION A B) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))"
haftmann@41656
   254
  by (blast intro: finite_subset)
haftmann@41656
   255
haftmann@41656
   256
lemma finite_Inter [intro]:
haftmann@41656
   257
  "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)"
haftmann@41656
   258
  by (blast intro: Inter_lower finite_subset)
wenzelm@12396
   259
haftmann@41656
   260
lemma finite_INT [intro]:
haftmann@41656
   261
  "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)"
haftmann@41656
   262
  by (blast intro: INT_lower finite_subset)
paulson@13825
   263
haftmann@41656
   264
lemma finite_imageI [simp, intro]:
haftmann@41656
   265
  "finite F \<Longrightarrow> finite (h ` F)"
haftmann@41656
   266
  by (induct rule: finite_induct) simp_all
paulson@13825
   267
haftmann@31768
   268
lemma finite_image_set [simp]:
haftmann@31768
   269
  "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
haftmann@31768
   270
  by (simp add: image_Collect [symmetric])
haftmann@31768
   271
lp15@59504
   272
lemma finite_image_set2:
lp15@59504
   273
  "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {f x y | x y. P x \<and> Q y}"
lp15@59504
   274
  by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {f x y}"]) auto
lp15@59504
   275
haftmann@41656
   276
lemma finite_imageD:
haftmann@42206
   277
  assumes "finite (f ` A)" and "inj_on f A"
haftmann@42206
   278
  shows "finite A"
wenzelm@46898
   279
using assms
wenzelm@46898
   280
proof (induct "f ` A" arbitrary: A)
haftmann@42206
   281
  case empty then show ?case by simp
haftmann@42206
   282
next
haftmann@42206
   283
  case (insert x B)
haftmann@42206
   284
  then have B_A: "insert x B = f ` A" by simp
haftmann@42206
   285
  then obtain y where "x = f y" and "y \<in> A" by blast
wenzelm@60758
   286
  from B_A \<open>x \<notin> B\<close> have "B = f ` A - {x}" by blast
wenzelm@60758
   287
  with B_A \<open>x \<notin> B\<close> \<open>x = f y\<close> \<open>inj_on f A\<close> \<open>y \<in> A\<close> have "B = f ` (A - {y})" 
paulson@60303
   288
    by (simp add: inj_on_image_set_diff Set.Diff_subset)
wenzelm@60758
   289
  moreover from \<open>inj_on f A\<close> have "inj_on f (A - {y})" by (rule inj_on_diff)
haftmann@42206
   290
  ultimately have "finite (A - {y})" by (rule insert.hyps)
haftmann@42206
   291
  then show "finite A" by simp
haftmann@42206
   292
qed
wenzelm@12396
   293
haftmann@41656
   294
lemma finite_surj:
haftmann@41656
   295
  "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B"
haftmann@41656
   296
  by (erule finite_subset) (rule finite_imageI)
wenzelm@12396
   297
haftmann@41656
   298
lemma finite_range_imageI:
haftmann@41656
   299
  "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))"
haftmann@41656
   300
  by (drule finite_imageI) (simp add: range_composition)
paulson@13825
   301
haftmann@41656
   302
lemma finite_subset_image:
haftmann@41656
   303
  assumes "finite B"
haftmann@41656
   304
  shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
wenzelm@46898
   305
using assms
wenzelm@46898
   306
proof induct
haftmann@41656
   307
  case empty then show ?case by simp
haftmann@41656
   308
next
haftmann@41656
   309
  case insert then show ?case
haftmann@41656
   310
    by (clarsimp simp del: image_insert simp add: image_insert [symmetric])
haftmann@41656
   311
       blast
haftmann@41656
   312
qed
haftmann@41656
   313
hoelzl@43991
   314
lemma finite_vimage_IntI:
hoelzl@43991
   315
  "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)"
haftmann@41656
   316
  apply (induct rule: finite_induct)
wenzelm@21575
   317
   apply simp_all
paulson@14430
   318
  apply (subst vimage_insert)
hoelzl@43991
   319
  apply (simp add: finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2)
paulson@13825
   320
  done
paulson@13825
   321
lp15@61762
   322
lemma finite_finite_vimage_IntI:
lp15@61762
   323
  assumes "finite F" and "\<And>y. y \<in> F \<Longrightarrow> finite ((h -` {y}) \<inter> A)"
lp15@61762
   324
  shows "finite (h -` F \<inter> A)"
lp15@61762
   325
proof -
lp15@61762
   326
  have *: "h -` F \<inter> A = (\<Union> y\<in>F. (h -` {y}) \<inter> A)"
lp15@61762
   327
    by blast
lp15@61762
   328
  show ?thesis
lp15@61762
   329
    by (simp only: * assms finite_UN_I)
lp15@61762
   330
qed
lp15@61762
   331
hoelzl@43991
   332
lemma finite_vimageI:
hoelzl@43991
   333
  "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)"
hoelzl@43991
   334
  using finite_vimage_IntI[of F h UNIV] by auto
hoelzl@43991
   335
Andreas@59519
   336
lemma finite_vimageD': "\<lbrakk> finite (f -` A); A \<subseteq> range f \<rbrakk> \<Longrightarrow> finite A"
Andreas@59519
   337
by(auto simp add: subset_image_iff intro: finite_subset[rotated])
Andreas@59519
   338
Andreas@59519
   339
lemma finite_vimageD: "\<lbrakk> finite (h -` F); surj h \<rbrakk> \<Longrightarrow> finite F"
Andreas@59519
   340
by(auto dest: finite_vimageD')
huffman@34111
   341
huffman@34111
   342
lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
huffman@34111
   343
  unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
huffman@34111
   344
haftmann@41656
   345
lemma finite_Collect_bex [simp]:
haftmann@41656
   346
  assumes "finite A"
haftmann@41656
   347
  shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
haftmann@41656
   348
proof -
haftmann@41656
   349
  have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
haftmann@41656
   350
  with assms show ?thesis by simp
haftmann@41656
   351
qed
wenzelm@12396
   352
haftmann@41656
   353
lemma finite_Collect_bounded_ex [simp]:
haftmann@41656
   354
  assumes "finite {y. P y}"
haftmann@41656
   355
  shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
haftmann@41656
   356
proof -
haftmann@41656
   357
  have "{x. EX y. P y & Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})" by auto
haftmann@41656
   358
  with assms show ?thesis by simp
haftmann@41656
   359
qed
nipkow@29920
   360
haftmann@41656
   361
lemma finite_Plus:
haftmann@41656
   362
  "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)"
haftmann@41656
   363
  by (simp add: Plus_def)
nipkow@17022
   364
nipkow@31080
   365
lemma finite_PlusD: 
nipkow@31080
   366
  fixes A :: "'a set" and B :: "'b set"
nipkow@31080
   367
  assumes fin: "finite (A <+> B)"
nipkow@31080
   368
  shows "finite A" "finite B"
nipkow@31080
   369
proof -
nipkow@31080
   370
  have "Inl ` A \<subseteq> A <+> B" by auto
haftmann@41656
   371
  then have "finite (Inl ` A :: ('a + 'b) set)" using fin by (rule finite_subset)
haftmann@41656
   372
  then show "finite A" by (rule finite_imageD) (auto intro: inj_onI)
nipkow@31080
   373
next
nipkow@31080
   374
  have "Inr ` B \<subseteq> A <+> B" by auto
haftmann@41656
   375
  then have "finite (Inr ` B :: ('a + 'b) set)" using fin by (rule finite_subset)
haftmann@41656
   376
  then show "finite B" by (rule finite_imageD) (auto intro: inj_onI)
nipkow@31080
   377
qed
nipkow@31080
   378
haftmann@41656
   379
lemma finite_Plus_iff [simp]:
haftmann@41656
   380
  "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
haftmann@41656
   381
  by (auto intro: finite_PlusD finite_Plus)
nipkow@31080
   382
haftmann@41656
   383
lemma finite_Plus_UNIV_iff [simp]:
haftmann@41656
   384
  "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
haftmann@41656
   385
  by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff)
wenzelm@12396
   386
nipkow@40786
   387
lemma finite_SigmaI [simp, intro]:
haftmann@41656
   388
  "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) ==> finite (SIGMA a:A. B a)"
nipkow@40786
   389
  by (unfold Sigma_def) blast
wenzelm@12396
   390
Andreas@51290
   391
lemma finite_SigmaI2:
Andreas@51290
   392
  assumes "finite {x\<in>A. B x \<noteq> {}}"
Andreas@51290
   393
  and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)"
Andreas@51290
   394
  shows "finite (Sigma A B)"
Andreas@51290
   395
proof -
Andreas@51290
   396
  from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)" by auto
Andreas@51290
   397
  also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B" by auto
Andreas@51290
   398
  finally show ?thesis .
Andreas@51290
   399
qed
Andreas@51290
   400
haftmann@41656
   401
lemma finite_cartesian_product:
haftmann@41656
   402
  "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)"
nipkow@15402
   403
  by (rule finite_SigmaI)
nipkow@15402
   404
wenzelm@12396
   405
lemma finite_Prod_UNIV:
haftmann@41656
   406
  "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
haftmann@41656
   407
  by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product)
wenzelm@12396
   408
paulson@15409
   409
lemma finite_cartesian_productD1:
haftmann@42207
   410
  assumes "finite (A \<times> B)" and "B \<noteq> {}"
haftmann@42207
   411
  shows "finite A"
haftmann@42207
   412
proof -
haftmann@42207
   413
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
haftmann@42207
   414
    by (auto simp add: finite_conv_nat_seg_image)
haftmann@42207
   415
  then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}" by simp
wenzelm@60758
   416
  with \<open>B \<noteq> {}\<close> have "A = (fst \<circ> f) ` {i::nat. i < n}"
haftmann@56154
   417
    by (simp add: image_comp)
haftmann@42207
   418
  then have "\<exists>n f. A = f ` {i::nat. i < n}" by blast
haftmann@42207
   419
  then show ?thesis
haftmann@42207
   420
    by (auto simp add: finite_conv_nat_seg_image)
haftmann@42207
   421
qed
paulson@15409
   422
paulson@15409
   423
lemma finite_cartesian_productD2:
haftmann@42207
   424
  assumes "finite (A \<times> B)" and "A \<noteq> {}"
haftmann@42207
   425
  shows "finite B"
haftmann@42207
   426
proof -
haftmann@42207
   427
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
haftmann@42207
   428
    by (auto simp add: finite_conv_nat_seg_image)
haftmann@42207
   429
  then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}" by simp
wenzelm@60758
   430
  with \<open>A \<noteq> {}\<close> have "B = (snd \<circ> f) ` {i::nat. i < n}"
haftmann@56154
   431
    by (simp add: image_comp)
haftmann@42207
   432
  then have "\<exists>n f. B = f ` {i::nat. i < n}" by blast
haftmann@42207
   433
  then show ?thesis
haftmann@42207
   434
    by (auto simp add: finite_conv_nat_seg_image)
haftmann@42207
   435
qed
paulson@15409
   436
hoelzl@57025
   437
lemma finite_cartesian_product_iff:
hoelzl@57025
   438
  "finite (A \<times> B) \<longleftrightarrow> (A = {} \<or> B = {} \<or> (finite A \<and> finite B))"
hoelzl@57025
   439
  by (auto dest: finite_cartesian_productD1 finite_cartesian_productD2 finite_cartesian_product)
hoelzl@57025
   440
Andreas@48175
   441
lemma finite_prod: 
Andreas@48175
   442
  "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
hoelzl@57025
   443
  using finite_cartesian_product_iff[of UNIV UNIV] by simp
Andreas@48175
   444
haftmann@41656
   445
lemma finite_Pow_iff [iff]:
haftmann@41656
   446
  "finite (Pow A) \<longleftrightarrow> finite A"
wenzelm@12396
   447
proof
wenzelm@12396
   448
  assume "finite (Pow A)"
haftmann@41656
   449
  then have "finite ((%x. {x}) ` A)" by (blast intro: finite_subset)
haftmann@41656
   450
  then show "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   451
next
wenzelm@12396
   452
  assume "finite A"
haftmann@41656
   453
  then show "finite (Pow A)"
huffman@35216
   454
    by induct (simp_all add: Pow_insert)
wenzelm@12396
   455
qed
wenzelm@12396
   456
haftmann@41656
   457
corollary finite_Collect_subsets [simp, intro]:
haftmann@41656
   458
  "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
haftmann@41656
   459
  by (simp add: Pow_def [symmetric])
nipkow@29918
   460
Andreas@48175
   461
lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)"
Andreas@48175
   462
by(simp only: finite_Pow_iff Pow_UNIV[symmetric])
Andreas@48175
   463
nipkow@15392
   464
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
haftmann@41656
   465
  by (blast intro: finite_subset [OF subset_Pow_Union])
nipkow@15392
   466
nipkow@53820
   467
lemma finite_set_of_finite_funs: assumes "finite A" "finite B"
nipkow@53820
   468
shows "finite{f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
nipkow@53820
   469
proof-
nipkow@53820
   470
  let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
nipkow@53820
   471
  have "?F ` ?S \<subseteq> Pow(A \<times> B)" by auto
nipkow@53820
   472
  from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" by simp
nipkow@53820
   473
  have 2: "inj_on ?F ?S"
nipkow@53820
   474
    by(fastforce simp add: inj_on_def set_eq_iff fun_eq_iff)
nipkow@53820
   475
  show ?thesis by(rule finite_imageD[OF 1 2])
nipkow@53820
   476
qed
nipkow@15392
   477
haftmann@58195
   478
lemma not_finite_existsD:
haftmann@58195
   479
  assumes "\<not> finite {a. P a}"
haftmann@58195
   480
  shows "\<exists>a. P a"
haftmann@58195
   481
proof (rule classical)
haftmann@58195
   482
  assume "\<not> (\<exists>a. P a)"
haftmann@58195
   483
  with assms show ?thesis by auto
haftmann@58195
   484
qed
haftmann@58195
   485
haftmann@58195
   486
wenzelm@60758
   487
subsubsection \<open>Further induction rules on finite sets\<close>
haftmann@41656
   488
haftmann@41656
   489
lemma finite_ne_induct [case_names singleton insert, consumes 2]:
haftmann@41656
   490
  assumes "finite F" and "F \<noteq> {}"
haftmann@41656
   491
  assumes "\<And>x. P {x}"
haftmann@41656
   492
    and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
haftmann@41656
   493
  shows "P F"
wenzelm@46898
   494
using assms
wenzelm@46898
   495
proof induct
haftmann@41656
   496
  case empty then show ?case by simp
haftmann@41656
   497
next
haftmann@41656
   498
  case (insert x F) then show ?case by cases auto
haftmann@41656
   499
qed
haftmann@41656
   500
haftmann@41656
   501
lemma finite_subset_induct [consumes 2, case_names empty insert]:
haftmann@41656
   502
  assumes "finite F" and "F \<subseteq> A"
haftmann@41656
   503
  assumes empty: "P {}"
haftmann@41656
   504
    and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)"
haftmann@41656
   505
  shows "P F"
wenzelm@60758
   506
using \<open>finite F\<close> \<open>F \<subseteq> A\<close>
wenzelm@46898
   507
proof induct
haftmann@41656
   508
  show "P {}" by fact
nipkow@31441
   509
next
haftmann@41656
   510
  fix x F
haftmann@41656
   511
  assume "finite F" and "x \<notin> F" and
haftmann@41656
   512
    P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
haftmann@41656
   513
  show "P (insert x F)"
haftmann@41656
   514
  proof (rule insert)
haftmann@41656
   515
    from i show "x \<in> A" by blast
haftmann@41656
   516
    from i have "F \<subseteq> A" by blast
haftmann@41656
   517
    with P show "P F" .
haftmann@41656
   518
    show "finite F" by fact
haftmann@41656
   519
    show "x \<notin> F" by fact
haftmann@41656
   520
  qed
haftmann@41656
   521
qed
haftmann@41656
   522
haftmann@41656
   523
lemma finite_empty_induct:
haftmann@41656
   524
  assumes "finite A"
haftmann@41656
   525
  assumes "P A"
haftmann@41656
   526
    and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
haftmann@41656
   527
  shows "P {}"
haftmann@41656
   528
proof -
haftmann@41656
   529
  have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
haftmann@41656
   530
  proof -
haftmann@41656
   531
    fix B :: "'a set"
haftmann@41656
   532
    assume "B \<subseteq> A"
wenzelm@60758
   533
    with \<open>finite A\<close> have "finite B" by (rule rev_finite_subset)
wenzelm@60758
   534
    from this \<open>B \<subseteq> A\<close> show "P (A - B)"
haftmann@41656
   535
    proof induct
haftmann@41656
   536
      case empty
wenzelm@60758
   537
      from \<open>P A\<close> show ?case by simp
haftmann@41656
   538
    next
haftmann@41656
   539
      case (insert b B)
haftmann@41656
   540
      have "P (A - B - {b})"
haftmann@41656
   541
      proof (rule remove)
wenzelm@60758
   542
        from \<open>finite A\<close> show "finite (A - B)" by induct auto
haftmann@41656
   543
        from insert show "b \<in> A - B" by simp
haftmann@41656
   544
        from insert show "P (A - B)" by simp
haftmann@41656
   545
      qed
haftmann@41656
   546
      also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
haftmann@41656
   547
      finally show ?case .
haftmann@41656
   548
    qed
haftmann@41656
   549
  qed
haftmann@41656
   550
  then have "P (A - A)" by blast
haftmann@41656
   551
  then show ?thesis by simp
nipkow@31441
   552
qed
nipkow@31441
   553
haftmann@58195
   554
lemma finite_update_induct [consumes 1, case_names const update]:
haftmann@58195
   555
  assumes finite: "finite {a. f a \<noteq> c}"
haftmann@58195
   556
  assumes const: "P (\<lambda>a. c)"
haftmann@58195
   557
  assumes update: "\<And>a b f. finite {a. f a \<noteq> c} \<Longrightarrow> f a = c \<Longrightarrow> b \<noteq> c \<Longrightarrow> P f \<Longrightarrow> P (f(a := b))"
haftmann@58195
   558
  shows "P f"
haftmann@58195
   559
using finite proof (induct "{a. f a \<noteq> c}" arbitrary: f)
haftmann@58195
   560
  case empty with const show ?case by simp
haftmann@58195
   561
next
haftmann@58195
   562
  case (insert a A)
haftmann@58195
   563
  then have "A = {a'. (f(a := c)) a' \<noteq> c}" and "f a \<noteq> c"
haftmann@58195
   564
    by auto
wenzelm@60758
   565
  with \<open>finite A\<close> have "finite {a'. (f(a := c)) a' \<noteq> c}"
haftmann@58195
   566
    by simp
haftmann@58195
   567
  have "(f(a := c)) a = c"
haftmann@58195
   568
    by simp
wenzelm@60758
   569
  from insert \<open>A = {a'. (f(a := c)) a' \<noteq> c}\<close> have "P (f(a := c))"
haftmann@58195
   570
    by simp
wenzelm@60758
   571
  with \<open>finite {a'. (f(a := c)) a' \<noteq> c}\<close> \<open>(f(a := c)) a = c\<close> \<open>f a \<noteq> c\<close> have "P ((f(a := c))(a := f a))"
haftmann@58195
   572
    by (rule update)
haftmann@58195
   573
  then show ?case by simp
haftmann@58195
   574
qed
haftmann@58195
   575
haftmann@58195
   576
wenzelm@61799
   577
subsection \<open>Class \<open>finite\<close>\<close>
haftmann@26041
   578
haftmann@29797
   579
class finite =
wenzelm@61076
   580
  assumes finite_UNIV: "finite (UNIV :: 'a set)"
huffman@27430
   581
begin
huffman@27430
   582
wenzelm@61076
   583
lemma finite [simp]: "finite (A :: 'a set)"
haftmann@26441
   584
  by (rule subset_UNIV finite_UNIV finite_subset)+
haftmann@26041
   585
wenzelm@61076
   586
lemma finite_code [code]: "finite (A :: 'a set) \<longleftrightarrow> True"
bulwahn@40922
   587
  by simp
bulwahn@40922
   588
huffman@27430
   589
end
huffman@27430
   590
wenzelm@46898
   591
instance prod :: (finite, finite) finite
wenzelm@61169
   592
  by standard (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
haftmann@26146
   593
haftmann@26041
   594
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
nipkow@39302
   595
  by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
haftmann@26041
   596
haftmann@26146
   597
instance "fun" :: (finite, finite) finite
haftmann@26146
   598
proof
haftmann@26041
   599
  show "finite (UNIV :: ('a => 'b) set)"
haftmann@26041
   600
  proof (rule finite_imageD)
haftmann@26041
   601
    let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
berghofe@26792
   602
    have "range ?graph \<subseteq> Pow UNIV" by simp
berghofe@26792
   603
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
berghofe@26792
   604
      by (simp only: finite_Pow_iff finite)
berghofe@26792
   605
    ultimately show "finite (range ?graph)"
berghofe@26792
   606
      by (rule finite_subset)
haftmann@26041
   607
    show "inj ?graph" by (rule inj_graph)
haftmann@26041
   608
  qed
haftmann@26041
   609
qed
haftmann@26041
   610
wenzelm@46898
   611
instance bool :: finite
wenzelm@61169
   612
  by standard (simp add: UNIV_bool)
haftmann@44831
   613
haftmann@45962
   614
instance set :: (finite) finite
wenzelm@61169
   615
  by standard (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
haftmann@45962
   616
wenzelm@46898
   617
instance unit :: finite
wenzelm@61169
   618
  by standard (simp add: UNIV_unit)
haftmann@44831
   619
wenzelm@46898
   620
instance sum :: (finite, finite) finite
wenzelm@61169
   621
  by standard (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
haftmann@27981
   622
haftmann@26041
   623
wenzelm@60758
   624
subsection \<open>A basic fold functional for finite sets\<close>
nipkow@15392
   625
wenzelm@60758
   626
text \<open>The intended behaviour is
wenzelm@61799
   627
\<open>fold f z {x\<^sub>1, ..., x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)\<close>
wenzelm@61799
   628
if \<open>f\<close> is ``left-commutative'':
wenzelm@60758
   629
\<close>
nipkow@15392
   630
haftmann@42871
   631
locale comp_fun_commute =
nipkow@28853
   632
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@42871
   633
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
nipkow@28853
   634
begin
nipkow@28853
   635
haftmann@51489
   636
lemma fun_left_comm: "f y (f x z) = f x (f y z)"
haftmann@42871
   637
  using comp_fun_commute by (simp add: fun_eq_iff)
nipkow@28853
   638
haftmann@51489
   639
lemma commute_left_comp:
haftmann@51489
   640
  "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
haftmann@51489
   641
  by (simp add: o_assoc comp_fun_commute)
haftmann@51489
   642
nipkow@28853
   643
end
nipkow@28853
   644
nipkow@28853
   645
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
nipkow@28853
   646
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
nipkow@28853
   647
  emptyI [intro]: "fold_graph f z {} z" |
nipkow@28853
   648
  insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
nipkow@28853
   649
      \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
nipkow@28853
   650
nipkow@28853
   651
inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
nipkow@28853
   652
nipkow@28853
   653
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
haftmann@51489
   654
  "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
nipkow@15392
   655
wenzelm@60758
   656
text\<open>A tempting alternative for the definiens is
nipkow@28853
   657
@{term "if finite A then THE y. fold_graph f z A y else e"}.
paulson@15498
   658
It allows the removal of finiteness assumptions from the theorems
wenzelm@61799
   659
\<open>fold_comm\<close>, \<open>fold_reindex\<close> and \<open>fold_distrib\<close>.
wenzelm@60758
   660
The proofs become ugly. It is not worth the effort. (???)\<close>
nipkow@28853
   661
nipkow@28853
   662
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
haftmann@41656
   663
by (induct rule: finite_induct) auto
nipkow@28853
   664
nipkow@28853
   665
wenzelm@60758
   666
subsubsection\<open>From @{const fold_graph} to @{term fold}\<close>
nipkow@15392
   667
haftmann@42871
   668
context comp_fun_commute
haftmann@26041
   669
begin
haftmann@26041
   670
haftmann@51489
   671
lemma fold_graph_finite:
haftmann@51489
   672
  assumes "fold_graph f z A y"
haftmann@51489
   673
  shows "finite A"
haftmann@51489
   674
  using assms by induct simp_all
haftmann@51489
   675
huffman@36045
   676
lemma fold_graph_insertE_aux:
huffman@36045
   677
  "fold_graph f z A y \<Longrightarrow> a \<in> A \<Longrightarrow> \<exists>y'. y = f a y' \<and> fold_graph f z (A - {a}) y'"
huffman@36045
   678
proof (induct set: fold_graph)
huffman@36045
   679
  case (insertI x A y) show ?case
huffman@36045
   680
  proof (cases "x = a")
huffman@36045
   681
    assume "x = a" with insertI show ?case by auto
nipkow@28853
   682
  next
huffman@36045
   683
    assume "x \<noteq> a"
huffman@36045
   684
    then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
huffman@36045
   685
      using insertI by auto
haftmann@42875
   686
    have "f x y = f a (f x y')"
huffman@36045
   687
      unfolding y by (rule fun_left_comm)
haftmann@42875
   688
    moreover have "fold_graph f z (insert x A - {a}) (f x y')"
wenzelm@60758
   689
      using y' and \<open>x \<noteq> a\<close> and \<open>x \<notin> A\<close>
huffman@36045
   690
      by (simp add: insert_Diff_if fold_graph.insertI)
haftmann@42875
   691
    ultimately show ?case by fast
nipkow@15392
   692
  qed
huffman@36045
   693
qed simp
huffman@36045
   694
huffman@36045
   695
lemma fold_graph_insertE:
huffman@36045
   696
  assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
huffman@36045
   697
  obtains y where "v = f x y" and "fold_graph f z A y"
huffman@36045
   698
using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
nipkow@28853
   699
nipkow@28853
   700
lemma fold_graph_determ:
nipkow@28853
   701
  "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
huffman@36045
   702
proof (induct arbitrary: y set: fold_graph)
huffman@36045
   703
  case (insertI x A y v)
wenzelm@60758
   704
  from \<open>fold_graph f z (insert x A) v\<close> and \<open>x \<notin> A\<close>
huffman@36045
   705
  obtain y' where "v = f x y'" and "fold_graph f z A y'"
huffman@36045
   706
    by (rule fold_graph_insertE)
wenzelm@60758
   707
  from \<open>fold_graph f z A y'\<close> have "y' = y" by (rule insertI)
wenzelm@60758
   708
  with \<open>v = f x y'\<close> show "v = f x y" by simp
huffman@36045
   709
qed fast
nipkow@15392
   710
nipkow@28853
   711
lemma fold_equality:
nipkow@28853
   712
  "fold_graph f z A y \<Longrightarrow> fold f z A = y"
haftmann@51489
   713
  by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
nipkow@15392
   714
haftmann@42272
   715
lemma fold_graph_fold:
haftmann@42272
   716
  assumes "finite A"
haftmann@42272
   717
  shows "fold_graph f z A (fold f z A)"
haftmann@42272
   718
proof -
haftmann@42272
   719
  from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
haftmann@42272
   720
  moreover note fold_graph_determ
haftmann@42272
   721
  ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
haftmann@42272
   722
  then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
haftmann@51489
   723
  with assms show ?thesis by (simp add: fold_def)
haftmann@42272
   724
qed
huffman@36045
   725
wenzelm@61799
   726
text \<open>The base case for \<open>fold\<close>:\<close>
nipkow@15392
   727
haftmann@51489
   728
lemma (in -) fold_infinite [simp]:
haftmann@51489
   729
  assumes "\<not> finite A"
haftmann@51489
   730
  shows "fold f z A = z"
haftmann@51489
   731
  using assms by (auto simp add: fold_def)
haftmann@51489
   732
haftmann@51489
   733
lemma (in -) fold_empty [simp]:
haftmann@51489
   734
  "fold f z {} = z"
haftmann@51489
   735
  by (auto simp add: fold_def)
nipkow@28853
   736
wenzelm@60758
   737
text\<open>The various recursion equations for @{const fold}:\<close>
nipkow@28853
   738
haftmann@26041
   739
lemma fold_insert [simp]:
haftmann@42875
   740
  assumes "finite A" and "x \<notin> A"
haftmann@42875
   741
  shows "fold f z (insert x A) = f x (fold f z A)"
haftmann@42875
   742
proof (rule fold_equality)
haftmann@51489
   743
  fix z
wenzelm@60758
   744
  from \<open>finite A\<close> have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
wenzelm@60758
   745
  with \<open>x \<notin> A\<close> have "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
haftmann@51489
   746
  then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
haftmann@42875
   747
qed
nipkow@28853
   748
haftmann@51489
   749
declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
wenzelm@61799
   750
  \<comment> \<open>No more proofs involve these.\<close>
haftmann@51489
   751
haftmann@51489
   752
lemma fold_fun_left_comm:
nipkow@28853
   753
  "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
nipkow@28853
   754
proof (induct rule: finite_induct)
nipkow@28853
   755
  case empty then show ?case by simp
nipkow@28853
   756
next
nipkow@28853
   757
  case (insert y A) then show ?case
haftmann@51489
   758
    by (simp add: fun_left_comm [of x])
nipkow@28853
   759
qed
nipkow@28853
   760
nipkow@28853
   761
lemma fold_insert2:
haftmann@51489
   762
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
haftmann@51489
   763
  by (simp add: fold_fun_left_comm)
nipkow@15392
   764
haftmann@26041
   765
lemma fold_rec:
haftmann@42875
   766
  assumes "finite A" and "x \<in> A"
haftmann@42875
   767
  shows "fold f z A = f x (fold f z (A - {x}))"
nipkow@28853
   768
proof -
wenzelm@60758
   769
  have A: "A = insert x (A - {x})" using \<open>x \<in> A\<close> by blast
nipkow@28853
   770
  then have "fold f z A = fold f z (insert x (A - {x}))" by simp
nipkow@28853
   771
  also have "\<dots> = f x (fold f z (A - {x}))"
wenzelm@60758
   772
    by (rule fold_insert) (simp add: \<open>finite A\<close>)+
nipkow@15535
   773
  finally show ?thesis .
nipkow@15535
   774
qed
nipkow@15535
   775
nipkow@28853
   776
lemma fold_insert_remove:
nipkow@28853
   777
  assumes "finite A"
nipkow@28853
   778
  shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
nipkow@28853
   779
proof -
wenzelm@60758
   780
  from \<open>finite A\<close> have "finite (insert x A)" by auto
nipkow@28853
   781
  moreover have "x \<in> insert x A" by auto
nipkow@28853
   782
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
nipkow@28853
   783
    by (rule fold_rec)
nipkow@28853
   784
  then show ?thesis by simp
nipkow@28853
   785
qed
nipkow@28853
   786
Andreas@57598
   787
lemma fold_set_union_disj:
Andreas@57598
   788
  assumes "finite A" "finite B" "A \<inter> B = {}"
Andreas@57598
   789
  shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B"
Andreas@57598
   790
using assms(2,1,3) by induction simp_all
Andreas@57598
   791
haftmann@51598
   792
end
haftmann@51598
   793
wenzelm@60758
   794
text\<open>Other properties of @{const fold}:\<close>
kuncar@48619
   795
kuncar@48619
   796
lemma fold_image:
haftmann@51598
   797
  assumes "inj_on g A"
haftmann@51489
   798
  shows "fold f z (g ` A) = fold (f \<circ> g) z A"
haftmann@51598
   799
proof (cases "finite A")
haftmann@51598
   800
  case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
haftmann@51598
   801
next
haftmann@51598
   802
  case True
haftmann@51598
   803
  have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
haftmann@51598
   804
  proof
haftmann@51598
   805
    fix w
haftmann@51598
   806
    show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
haftmann@51598
   807
    proof
haftmann@51598
   808
      assume ?P then show ?Q using assms
haftmann@51598
   809
      proof (induct "g ` A" w arbitrary: A)
haftmann@51598
   810
        case emptyI then show ?case by (auto intro: fold_graph.emptyI)
haftmann@51598
   811
      next
haftmann@51598
   812
        case (insertI x A r B)
wenzelm@60758
   813
        from \<open>inj_on g B\<close> \<open>x \<notin> A\<close> \<open>insert x A = image g B\<close> obtain x' A' where
haftmann@51598
   814
          "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
haftmann@51598
   815
          by (rule inj_img_insertE)
haftmann@51598
   816
        from insertI.prems have "fold_graph (f o g) z A' r"
haftmann@51598
   817
          by (auto intro: insertI.hyps)
wenzelm@60758
   818
        with \<open>x' \<notin> A'\<close> have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
haftmann@51598
   819
          by (rule fold_graph.insertI)
haftmann@51598
   820
        then show ?case by simp
haftmann@51598
   821
      qed
haftmann@51598
   822
    next
haftmann@51598
   823
      assume ?Q then show ?P using assms
haftmann@51598
   824
      proof induct
haftmann@51598
   825
        case emptyI thus ?case by (auto intro: fold_graph.emptyI)
haftmann@51598
   826
      next
haftmann@51598
   827
        case (insertI x A r)
wenzelm@60758
   828
        from \<open>x \<notin> A\<close> insertI.prems have "g x \<notin> g ` A" by auto
haftmann@51598
   829
        moreover from insertI have "fold_graph f z (g ` A) r" by simp
haftmann@51598
   830
        ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
haftmann@51598
   831
          by (rule fold_graph.insertI)
haftmann@51598
   832
        then show ?case by simp
haftmann@51598
   833
      qed
haftmann@51598
   834
    qed
haftmann@51598
   835
  qed
haftmann@51598
   836
  with True assms show ?thesis by (auto simp add: fold_def)
haftmann@51598
   837
qed
nipkow@15392
   838
haftmann@49724
   839
lemma fold_cong:
haftmann@49724
   840
  assumes "comp_fun_commute f" "comp_fun_commute g"
haftmann@49724
   841
  assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
haftmann@51489
   842
    and "s = t" and "A = B"
haftmann@51489
   843
  shows "fold f s A = fold g t B"
haftmann@49724
   844
proof -
haftmann@51489
   845
  have "fold f s A = fold g s A"  
wenzelm@60758
   846
  using \<open>finite A\<close> cong proof (induct A)
haftmann@49724
   847
    case empty then show ?case by simp
haftmann@49724
   848
  next
haftmann@49724
   849
    case (insert x A)
wenzelm@60758
   850
    interpret f: comp_fun_commute f by (fact \<open>comp_fun_commute f\<close>)
wenzelm@60758
   851
    interpret g: comp_fun_commute g by (fact \<open>comp_fun_commute g\<close>)
haftmann@49724
   852
    from insert show ?case by simp
haftmann@49724
   853
  qed
haftmann@49724
   854
  with assms show ?thesis by simp
haftmann@49724
   855
qed
haftmann@49724
   856
haftmann@49724
   857
wenzelm@60758
   858
text \<open>A simplified version for idempotent functions:\<close>
nipkow@15480
   859
haftmann@42871
   860
locale comp_fun_idem = comp_fun_commute +
haftmann@51489
   861
  assumes comp_fun_idem: "f x \<circ> f x = f x"
haftmann@26041
   862
begin
haftmann@26041
   863
haftmann@42869
   864
lemma fun_left_idem: "f x (f x z) = f x z"
haftmann@42871
   865
  using comp_fun_idem by (simp add: fun_eq_iff)
nipkow@28853
   866
haftmann@26041
   867
lemma fold_insert_idem:
nipkow@28853
   868
  assumes fin: "finite A"
haftmann@51489
   869
  shows "fold f z (insert x A)  = f x (fold f z A)"
nipkow@15480
   870
proof cases
nipkow@28853
   871
  assume "x \<in> A"
nipkow@28853
   872
  then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
haftmann@51489
   873
  then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
nipkow@15480
   874
next
nipkow@28853
   875
  assume "x \<notin> A" then show ?thesis using assms by simp
nipkow@15480
   876
qed
nipkow@15480
   877
haftmann@51489
   878
declare fold_insert [simp del] fold_insert_idem [simp]
nipkow@28853
   879
nipkow@28853
   880
lemma fold_insert_idem2:
nipkow@28853
   881
  "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
haftmann@51489
   882
  by (simp add: fold_fun_left_comm)
nipkow@15484
   883
haftmann@26041
   884
end
haftmann@26041
   885
haftmann@35817
   886
wenzelm@61799
   887
subsubsection \<open>Liftings to \<open>comp_fun_commute\<close> etc.\<close>
haftmann@35817
   888
haftmann@42871
   889
lemma (in comp_fun_commute) comp_comp_fun_commute:
haftmann@42871
   890
  "comp_fun_commute (f \<circ> g)"
haftmann@35817
   891
proof
haftmann@42871
   892
qed (simp_all add: comp_fun_commute)
haftmann@35817
   893
haftmann@42871
   894
lemma (in comp_fun_idem) comp_comp_fun_idem:
haftmann@42871
   895
  "comp_fun_idem (f \<circ> g)"
haftmann@42871
   896
  by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
haftmann@42871
   897
    (simp_all add: comp_fun_idem)
haftmann@35817
   898
haftmann@49723
   899
lemma (in comp_fun_commute) comp_fun_commute_funpow:
haftmann@49723
   900
  "comp_fun_commute (\<lambda>x. f x ^^ g x)"
haftmann@49723
   901
proof
haftmann@49723
   902
  fix y x
haftmann@49723
   903
  show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
haftmann@49723
   904
  proof (cases "x = y")
haftmann@49723
   905
    case True then show ?thesis by simp
haftmann@49723
   906
  next
haftmann@49723
   907
    case False show ?thesis
haftmann@49723
   908
    proof (induct "g x" arbitrary: g)
haftmann@49723
   909
      case 0 then show ?case by simp
haftmann@49723
   910
    next
haftmann@49723
   911
      case (Suc n g)
haftmann@49723
   912
      have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
haftmann@49723
   913
      proof (induct "g y" arbitrary: g)
haftmann@49723
   914
        case 0 then show ?case by simp
haftmann@49723
   915
      next
haftmann@49723
   916
        case (Suc n g)
haftmann@49723
   917
        def h \<equiv> "\<lambda>z. g z - 1"
haftmann@49723
   918
        with Suc have "n = h y" by simp
haftmann@49723
   919
        with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
haftmann@49723
   920
          by auto
haftmann@49723
   921
        from Suc h_def have "g y = Suc (h y)" by simp
haftmann@49739
   922
        then show ?case by (simp add: comp_assoc hyp)
haftmann@49723
   923
          (simp add: o_assoc comp_fun_commute)
haftmann@49723
   924
      qed
haftmann@49723
   925
      def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
haftmann@49723
   926
      with Suc have "n = h x" by simp
haftmann@49723
   927
      with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
haftmann@49723
   928
        by auto
haftmann@49723
   929
      with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" by simp
haftmann@49723
   930
      from Suc h_def have "g x = Suc (h x)" by simp
haftmann@49723
   931
      then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
haftmann@49739
   932
        (simp add: comp_assoc hyp1)
haftmann@49723
   933
    qed
haftmann@49723
   934
  qed
haftmann@49723
   935
qed
haftmann@49723
   936
haftmann@49723
   937
wenzelm@60758
   938
subsubsection \<open>Expressing set operations via @{const fold}\<close>
haftmann@49723
   939
haftmann@51489
   940
lemma comp_fun_commute_const:
haftmann@51489
   941
  "comp_fun_commute (\<lambda>_. f)"
haftmann@51489
   942
proof
haftmann@51489
   943
qed rule
haftmann@51489
   944
haftmann@42871
   945
lemma comp_fun_idem_insert:
haftmann@42871
   946
  "comp_fun_idem insert"
haftmann@35817
   947
proof
haftmann@35817
   948
qed auto
haftmann@35817
   949
haftmann@42871
   950
lemma comp_fun_idem_remove:
haftmann@46146
   951
  "comp_fun_idem Set.remove"
haftmann@35817
   952
proof
haftmann@35817
   953
qed auto
nipkow@31992
   954
haftmann@42871
   955
lemma (in semilattice_inf) comp_fun_idem_inf:
haftmann@42871
   956
  "comp_fun_idem inf"
haftmann@35817
   957
proof
haftmann@35817
   958
qed (auto simp add: inf_left_commute)
haftmann@35817
   959
haftmann@42871
   960
lemma (in semilattice_sup) comp_fun_idem_sup:
haftmann@42871
   961
  "comp_fun_idem sup"
haftmann@35817
   962
proof
haftmann@35817
   963
qed (auto simp add: sup_left_commute)
nipkow@31992
   964
haftmann@35817
   965
lemma union_fold_insert:
haftmann@35817
   966
  assumes "finite A"
haftmann@35817
   967
  shows "A \<union> B = fold insert B A"
haftmann@35817
   968
proof -
haftmann@42871
   969
  interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
wenzelm@60758
   970
  from \<open>finite A\<close> show ?thesis by (induct A arbitrary: B) simp_all
haftmann@35817
   971
qed
nipkow@31992
   972
haftmann@35817
   973
lemma minus_fold_remove:
haftmann@35817
   974
  assumes "finite A"
haftmann@46146
   975
  shows "B - A = fold Set.remove B A"
haftmann@35817
   976
proof -
haftmann@46146
   977
  interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
wenzelm@60758
   978
  from \<open>finite A\<close> have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
haftmann@46146
   979
  then show ?thesis ..
haftmann@35817
   980
qed
haftmann@35817
   981
haftmann@51489
   982
lemma comp_fun_commute_filter_fold:
haftmann@51489
   983
  "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
kuncar@48619
   984
proof - 
kuncar@48619
   985
  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
wenzelm@61169
   986
  show ?thesis by standard (auto simp: fun_eq_iff)
kuncar@48619
   987
qed
kuncar@48619
   988
kuncar@49758
   989
lemma Set_filter_fold:
kuncar@48619
   990
  assumes "finite A"
kuncar@49758
   991
  shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
kuncar@48619
   992
using assms
kuncar@48619
   993
by (induct A) 
kuncar@49758
   994
  (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
kuncar@49758
   995
kuncar@49758
   996
lemma inter_Set_filter:     
kuncar@49758
   997
  assumes "finite B"
kuncar@49758
   998
  shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
kuncar@49758
   999
using assms 
kuncar@49758
  1000
by (induct B) (auto simp: Set.filter_def)
kuncar@48619
  1001
kuncar@48619
  1002
lemma image_fold_insert:
kuncar@48619
  1003
  assumes "finite A"
kuncar@48619
  1004
  shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
kuncar@48619
  1005
using assms
kuncar@48619
  1006
proof -
wenzelm@61169
  1007
  interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by standard auto
kuncar@48619
  1008
  show ?thesis using assms by (induct A) auto
kuncar@48619
  1009
qed
kuncar@48619
  1010
kuncar@48619
  1011
lemma Ball_fold:
kuncar@48619
  1012
  assumes "finite A"
kuncar@48619
  1013
  shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
kuncar@48619
  1014
using assms
kuncar@48619
  1015
proof -
wenzelm@61169
  1016
  interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by standard auto
kuncar@48619
  1017
  show ?thesis using assms by (induct A) auto
kuncar@48619
  1018
qed
kuncar@48619
  1019
kuncar@48619
  1020
lemma Bex_fold:
kuncar@48619
  1021
  assumes "finite A"
kuncar@48619
  1022
  shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
kuncar@48619
  1023
using assms
kuncar@48619
  1024
proof -
wenzelm@61169
  1025
  interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by standard auto
kuncar@48619
  1026
  show ?thesis using assms by (induct A) auto
kuncar@48619
  1027
qed
kuncar@48619
  1028
kuncar@48619
  1029
lemma comp_fun_commute_Pow_fold: 
kuncar@48619
  1030
  "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
kuncar@48619
  1031
  by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
kuncar@48619
  1032
kuncar@48619
  1033
lemma Pow_fold:
kuncar@48619
  1034
  assumes "finite A"
kuncar@48619
  1035
  shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
kuncar@48619
  1036
using assms
kuncar@48619
  1037
proof -
kuncar@48619
  1038
  interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
kuncar@48619
  1039
  show ?thesis using assms by (induct A) (auto simp: Pow_insert)
kuncar@48619
  1040
qed
kuncar@48619
  1041
kuncar@48619
  1042
lemma fold_union_pair:
kuncar@48619
  1043
  assumes "finite B"
kuncar@48619
  1044
  shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
kuncar@48619
  1045
proof -
wenzelm@61169
  1046
  interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by standard auto
kuncar@48619
  1047
  show ?thesis using assms  by (induct B arbitrary: A) simp_all
kuncar@48619
  1048
qed
kuncar@48619
  1049
kuncar@48619
  1050
lemma comp_fun_commute_product_fold: 
kuncar@48619
  1051
  assumes "finite B"
haftmann@51489
  1052
  shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
wenzelm@61169
  1053
  by standard (auto simp: fold_union_pair[symmetric] assms)
kuncar@48619
  1054
kuncar@48619
  1055
lemma product_fold:
kuncar@48619
  1056
  assumes "finite A"
kuncar@48619
  1057
  assumes "finite B"
haftmann@51489
  1058
  shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
kuncar@48619
  1059
using assms unfolding Sigma_def 
kuncar@48619
  1060
by (induct A) 
kuncar@48619
  1061
  (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
kuncar@48619
  1062
kuncar@48619
  1063
haftmann@35817
  1064
context complete_lattice
nipkow@31992
  1065
begin
nipkow@31992
  1066
haftmann@35817
  1067
lemma inf_Inf_fold_inf:
haftmann@35817
  1068
  assumes "finite A"
haftmann@51489
  1069
  shows "inf (Inf A) B = fold inf B A"
haftmann@35817
  1070
proof -
haftmann@42871
  1071
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
wenzelm@60758
  1072
  from \<open>finite A\<close> fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
haftmann@51489
  1073
    (simp_all add: inf_commute fun_eq_iff)
haftmann@35817
  1074
qed
nipkow@31992
  1075
haftmann@35817
  1076
lemma sup_Sup_fold_sup:
haftmann@35817
  1077
  assumes "finite A"
haftmann@51489
  1078
  shows "sup (Sup A) B = fold sup B A"
haftmann@35817
  1079
proof -
haftmann@42871
  1080
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
wenzelm@60758
  1081
  from \<open>finite A\<close> fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
haftmann@51489
  1082
    (simp_all add: sup_commute fun_eq_iff)
nipkow@31992
  1083
qed
nipkow@31992
  1084
haftmann@35817
  1085
lemma Inf_fold_inf:
haftmann@35817
  1086
  assumes "finite A"
haftmann@35817
  1087
  shows "Inf A = fold inf top A"
haftmann@35817
  1088
  using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
haftmann@35817
  1089
haftmann@35817
  1090
lemma Sup_fold_sup:
haftmann@35817
  1091
  assumes "finite A"
haftmann@35817
  1092
  shows "Sup A = fold sup bot A"
haftmann@35817
  1093
  using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
nipkow@31992
  1094
haftmann@46146
  1095
lemma inf_INF_fold_inf:
haftmann@35817
  1096
  assumes "finite A"
haftmann@56218
  1097
  shows "inf B (INFIMUM A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
haftmann@35817
  1098
proof (rule sym)
haftmann@42871
  1099
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
haftmann@42871
  1100
  interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
wenzelm@60758
  1101
  from \<open>finite A\<close> show "?fold = ?inf"
haftmann@42869
  1102
    by (induct A arbitrary: B)
haftmann@56166
  1103
      (simp_all add: inf_left_commute)
haftmann@35817
  1104
qed
nipkow@31992
  1105
haftmann@46146
  1106
lemma sup_SUP_fold_sup:
haftmann@35817
  1107
  assumes "finite A"
haftmann@56218
  1108
  shows "sup B (SUPREMUM A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
haftmann@35817
  1109
proof (rule sym)
haftmann@42871
  1110
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
haftmann@42871
  1111
  interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
wenzelm@60758
  1112
  from \<open>finite A\<close> show "?fold = ?sup"
haftmann@42869
  1113
    by (induct A arbitrary: B)
haftmann@56166
  1114
      (simp_all add: sup_left_commute)
haftmann@35817
  1115
qed
nipkow@31992
  1116
haftmann@46146
  1117
lemma INF_fold_inf:
haftmann@35817
  1118
  assumes "finite A"
haftmann@56218
  1119
  shows "INFIMUM A f = fold (inf \<circ> f) top A"
haftmann@46146
  1120
  using assms inf_INF_fold_inf [of A top] by simp
nipkow@31992
  1121
haftmann@46146
  1122
lemma SUP_fold_sup:
haftmann@35817
  1123
  assumes "finite A"
haftmann@56218
  1124
  shows "SUPREMUM A f = fold (sup \<circ> f) bot A"
haftmann@46146
  1125
  using assms sup_SUP_fold_sup [of A bot] by simp
nipkow@31992
  1126
nipkow@31992
  1127
end
nipkow@31992
  1128
nipkow@31992
  1129
wenzelm@60758
  1130
subsection \<open>Locales as mini-packages for fold operations\<close>
haftmann@34007
  1131
wenzelm@60758
  1132
subsubsection \<open>The natural case\<close>
haftmann@35719
  1133
haftmann@35719
  1134
locale folding =
haftmann@35719
  1135
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@51489
  1136
  fixes z :: "'b"
haftmann@42871
  1137
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
haftmann@35719
  1138
begin
haftmann@35719
  1139
haftmann@54870
  1140
interpretation fold?: comp_fun_commute f
wenzelm@61169
  1141
  by standard (insert comp_fun_commute, simp add: fun_eq_iff)
haftmann@54867
  1142
haftmann@51489
  1143
definition F :: "'a set \<Rightarrow> 'b"
haftmann@51489
  1144
where
haftmann@51489
  1145
  eq_fold: "F A = fold f z A"
haftmann@51489
  1146
wenzelm@61169
  1147
lemma empty [simp]:"F {} = z"
haftmann@51489
  1148
  by (simp add: eq_fold)
haftmann@35719
  1149
wenzelm@61169
  1150
lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F A = z"
haftmann@51489
  1151
  by (simp add: eq_fold)
haftmann@51489
  1152
 
haftmann@35719
  1153
lemma insert [simp]:
haftmann@35719
  1154
  assumes "finite A" and "x \<notin> A"
haftmann@51489
  1155
  shows "F (insert x A) = f x (F A)"
haftmann@35719
  1156
proof -
haftmann@51489
  1157
  from fold_insert assms
haftmann@51489
  1158
  have "fold f z (insert x A) = f x (fold f z A)" by simp
wenzelm@60758
  1159
  with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff)
haftmann@35719
  1160
qed
haftmann@51489
  1161
 
haftmann@35719
  1162
lemma remove:
haftmann@35719
  1163
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1164
  shows "F A = f x (F (A - {x}))"
haftmann@35719
  1165
proof -
wenzelm@60758
  1166
  from \<open>x \<in> A\<close> obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@35719
  1167
    by (auto dest: mk_disjoint_insert)
wenzelm@60758
  1168
  moreover from \<open>finite A\<close> A have "finite B" by simp
haftmann@35719
  1169
  ultimately show ?thesis by simp
haftmann@35719
  1170
qed
haftmann@35719
  1171
haftmann@35719
  1172
lemma insert_remove:
haftmann@35719
  1173
  assumes "finite A"
haftmann@51489
  1174
  shows "F (insert x A) = f x (F (A - {x}))"
haftmann@35722
  1175
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@35719
  1176
haftmann@34007
  1177
end
haftmann@35719
  1178
haftmann@35817
  1179
wenzelm@60758
  1180
subsubsection \<open>With idempotency\<close>
haftmann@35817
  1181
haftmann@35719
  1182
locale folding_idem = folding +
haftmann@51489
  1183
  assumes comp_fun_idem: "f x \<circ> f x = f x"
haftmann@35719
  1184
begin
haftmann@35719
  1185
haftmann@35817
  1186
declare insert [simp del]
haftmann@35719
  1187
haftmann@54870
  1188
interpretation fold?: comp_fun_idem f
wenzelm@61169
  1189
  by standard (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
haftmann@54867
  1190
haftmann@35719
  1191
lemma insert_idem [simp]:
haftmann@35719
  1192
  assumes "finite A"
haftmann@51489
  1193
  shows "F (insert x A) = f x (F A)"
haftmann@35817
  1194
proof -
haftmann@51489
  1195
  from fold_insert_idem assms
haftmann@51489
  1196
  have "fold f z (insert x A) = f x (fold f z A)" by simp
wenzelm@60758
  1197
  with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff)
haftmann@35719
  1198
qed
haftmann@35719
  1199
haftmann@35719
  1200
end
haftmann@35719
  1201
haftmann@35817
  1202
wenzelm@60758
  1203
subsection \<open>Finite cardinality\<close>
haftmann@35722
  1204
wenzelm@60758
  1205
text \<open>
haftmann@51489
  1206
  The traditional definition
haftmann@51489
  1207
  @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
haftmann@51489
  1208
  is ugly to work with.
haftmann@51489
  1209
  But now that we have @{const fold} things are easy:
wenzelm@60758
  1210
\<close>
haftmann@35722
  1211
haftmann@61890
  1212
global_interpretation card: folding "\<lambda>_. Suc" 0
haftmann@61778
  1213
  defines card = "folding.F (\<lambda>_. Suc) 0"
haftmann@61778
  1214
  by standard rule
haftmann@35722
  1215
haftmann@51489
  1216
lemma card_infinite:
haftmann@35722
  1217
  "\<not> finite A \<Longrightarrow> card A = 0"
haftmann@51489
  1218
  by (fact card.infinite)
haftmann@35722
  1219
haftmann@35722
  1220
lemma card_empty:
haftmann@35722
  1221
  "card {} = 0"
haftmann@35722
  1222
  by (fact card.empty)
haftmann@35722
  1223
haftmann@35722
  1224
lemma card_insert_disjoint:
haftmann@51489
  1225
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
haftmann@51489
  1226
  by (fact card.insert)
haftmann@35722
  1227
haftmann@35722
  1228
lemma card_insert_if:
haftmann@51489
  1229
  "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
haftmann@35722
  1230
  by auto (simp add: card.insert_remove card.remove)
haftmann@35722
  1231
haftmann@35722
  1232
lemma card_ge_0_finite:
haftmann@35722
  1233
  "card A > 0 \<Longrightarrow> finite A"
haftmann@35722
  1234
  by (rule ccontr) simp
haftmann@35722
  1235
blanchet@54148
  1236
lemma card_0_eq [simp]:
haftmann@35722
  1237
  "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
haftmann@35722
  1238
  by (auto dest: mk_disjoint_insert)
haftmann@35722
  1239
haftmann@35722
  1240
lemma finite_UNIV_card_ge_0:
haftmann@35722
  1241
  "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
haftmann@35722
  1242
  by (rule ccontr) simp
haftmann@35722
  1243
haftmann@35722
  1244
lemma card_eq_0_iff:
haftmann@35722
  1245
  "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
haftmann@35722
  1246
  by auto
haftmann@35722
  1247
haftmann@35722
  1248
lemma card_gt_0_iff:
haftmann@35722
  1249
  "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
haftmann@35722
  1250
  by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
haftmann@35722
  1251
haftmann@51489
  1252
lemma card_Suc_Diff1:
haftmann@51489
  1253
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
haftmann@35722
  1254
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
haftmann@35722
  1255
apply(simp del:insert_Diff_single)
haftmann@35722
  1256
done
haftmann@35722
  1257
paulson@60762
  1258
lemma card_insert_le_m1: "n>0 \<Longrightarrow> card y \<le> n-1 \<Longrightarrow> card (insert x y) \<le> n"
paulson@60762
  1259
  apply (cases "finite y")
paulson@60762
  1260
  apply (cases "x \<in> y")
paulson@60762
  1261
  apply (auto simp: insert_absorb)
paulson@60762
  1262
  done
paulson@60762
  1263
haftmann@35722
  1264
lemma card_Diff_singleton:
haftmann@51489
  1265
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
haftmann@51489
  1266
  by (simp add: card_Suc_Diff1 [symmetric])
haftmann@35722
  1267
haftmann@35722
  1268
lemma card_Diff_singleton_if:
haftmann@51489
  1269
  "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
haftmann@51489
  1270
  by (simp add: card_Diff_singleton)
haftmann@35722
  1271
haftmann@35722
  1272
lemma card_Diff_insert[simp]:
haftmann@51489
  1273
  assumes "finite A" and "a \<in> A" and "a \<notin> B"
haftmann@51489
  1274
  shows "card (A - insert a B) = card (A - B) - 1"
haftmann@35722
  1275
proof -
haftmann@35722
  1276
  have "A - insert a B = (A - B) - {a}" using assms by blast
haftmann@51489
  1277
  then show ?thesis using assms by(simp add: card_Diff_singleton)
haftmann@35722
  1278
qed
haftmann@35722
  1279
haftmann@35722
  1280
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
haftmann@51489
  1281
  by (fact card.insert_remove)
haftmann@35722
  1282
haftmann@35722
  1283
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
haftmann@35722
  1284
by (simp add: card_insert_if)
haftmann@35722
  1285
nipkow@41987
  1286
lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
nipkow@41987
  1287
by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
nipkow@41987
  1288
nipkow@41988
  1289
lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
nipkow@41987
  1290
using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
nipkow@41987
  1291
haftmann@35722
  1292
lemma card_mono:
haftmann@35722
  1293
  assumes "finite B" and "A \<subseteq> B"
haftmann@35722
  1294
  shows "card A \<le> card B"
haftmann@35722
  1295
proof -
haftmann@35722
  1296
  from assms have "finite A" by (auto intro: finite_subset)
haftmann@35722
  1297
  then show ?thesis using assms proof (induct A arbitrary: B)
haftmann@35722
  1298
    case empty then show ?case by simp
haftmann@35722
  1299
  next
haftmann@35722
  1300
    case (insert x A)
haftmann@35722
  1301
    then have "x \<in> B" by simp
haftmann@35722
  1302
    from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
haftmann@35722
  1303
    with insert.hyps have "card A \<le> card (B - {x})" by auto
wenzelm@60758
  1304
    with \<open>finite A\<close> \<open>x \<notin> A\<close> \<open>finite B\<close> \<open>x \<in> B\<close> show ?case by simp (simp only: card.remove)
haftmann@35722
  1305
  qed
haftmann@35722
  1306
qed
haftmann@35722
  1307
haftmann@35722
  1308
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
haftmann@41656
  1309
apply (induct rule: finite_induct)
haftmann@41656
  1310
apply simp
haftmann@41656
  1311
apply clarify
haftmann@35722
  1312
apply (subgoal_tac "finite A & A - {x} <= F")
haftmann@35722
  1313
 prefer 2 apply (blast intro: finite_subset, atomize)
haftmann@35722
  1314
apply (drule_tac x = "A - {x}" in spec)
haftmann@35722
  1315
apply (simp add: card_Diff_singleton_if split add: split_if_asm)
haftmann@35722
  1316
apply (case_tac "card A", auto)
haftmann@35722
  1317
done
haftmann@35722
  1318
haftmann@35722
  1319
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
haftmann@35722
  1320
apply (simp add: psubset_eq linorder_not_le [symmetric])
haftmann@35722
  1321
apply (blast dest: card_seteq)
haftmann@35722
  1322
done
haftmann@35722
  1323
haftmann@51489
  1324
lemma card_Un_Int:
haftmann@51489
  1325
  assumes "finite A" and "finite B"
haftmann@51489
  1326
  shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
haftmann@51489
  1327
using assms proof (induct A)
haftmann@51489
  1328
  case empty then show ?case by simp
haftmann@51489
  1329
next
haftmann@51489
  1330
 case (insert x A) then show ?case
haftmann@51489
  1331
    by (auto simp add: insert_absorb Int_insert_left)
haftmann@51489
  1332
qed
haftmann@35722
  1333
haftmann@51489
  1334
lemma card_Un_disjoint:
haftmann@51489
  1335
  assumes "finite A" and "finite B"
haftmann@51489
  1336
  assumes "A \<inter> B = {}"
haftmann@51489
  1337
  shows "card (A \<union> B) = card A + card B"
haftmann@51489
  1338
using assms card_Un_Int [of A B] by simp
haftmann@35722
  1339
nipkow@59336
  1340
lemma card_Un_le: "card (A \<union> B) \<le> card A + card B"
nipkow@59336
  1341
apply(cases "finite A")
nipkow@59336
  1342
 apply(cases "finite B")
nipkow@59336
  1343
  using le_iff_add card_Un_Int apply blast
nipkow@59336
  1344
 apply simp
nipkow@59336
  1345
apply simp
nipkow@59336
  1346
done
nipkow@59336
  1347
haftmann@35722
  1348
lemma card_Diff_subset:
haftmann@35722
  1349
  assumes "finite B" and "B \<subseteq> A"
haftmann@35722
  1350
  shows "card (A - B) = card A - card B"
haftmann@35722
  1351
proof (cases "finite A")
haftmann@35722
  1352
  case False with assms show ?thesis by simp
haftmann@35722
  1353
next
haftmann@35722
  1354
  case True with assms show ?thesis by (induct B arbitrary: A) simp_all
haftmann@35722
  1355
qed
haftmann@35722
  1356
haftmann@35722
  1357
lemma card_Diff_subset_Int:
haftmann@35722
  1358
  assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
haftmann@35722
  1359
proof -
haftmann@35722
  1360
  have "A - B = A - A \<inter> B" by auto
haftmann@35722
  1361
  thus ?thesis
haftmann@35722
  1362
    by (simp add: card_Diff_subset AB) 
haftmann@35722
  1363
qed
haftmann@35722
  1364
nipkow@40716
  1365
lemma diff_card_le_card_Diff:
nipkow@40716
  1366
assumes "finite B" shows "card A - card B \<le> card(A - B)"
nipkow@40716
  1367
proof-
nipkow@40716
  1368
  have "card A - card B \<le> card A - card (A \<inter> B)"
nipkow@40716
  1369
    using card_mono[OF assms Int_lower2, of A] by arith
nipkow@40716
  1370
  also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
nipkow@40716
  1371
  finally show ?thesis .
nipkow@40716
  1372
qed
nipkow@40716
  1373
haftmann@35722
  1374
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
haftmann@35722
  1375
apply (rule Suc_less_SucD)
haftmann@35722
  1376
apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
haftmann@35722
  1377
done
haftmann@35722
  1378
haftmann@35722
  1379
lemma card_Diff2_less:
haftmann@35722
  1380
  "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
haftmann@35722
  1381
apply (case_tac "x = y")
haftmann@35722
  1382
 apply (simp add: card_Diff1_less del:card_Diff_insert)
haftmann@35722
  1383
apply (rule less_trans)
haftmann@35722
  1384
 prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
haftmann@35722
  1385
done
haftmann@35722
  1386
haftmann@35722
  1387
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
haftmann@35722
  1388
apply (case_tac "x : A")
haftmann@35722
  1389
 apply (simp_all add: card_Diff1_less less_imp_le)
haftmann@35722
  1390
done
haftmann@35722
  1391
haftmann@35722
  1392
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
haftmann@35722
  1393
by (erule psubsetI, blast)
haftmann@35722
  1394
hoelzl@54413
  1395
lemma card_le_inj:
hoelzl@54413
  1396
  assumes fA: "finite A"
hoelzl@54413
  1397
    and fB: "finite B"
hoelzl@54413
  1398
    and c: "card A \<le> card B"
hoelzl@54413
  1399
  shows "\<exists>f. f ` A \<subseteq> B \<and> inj_on f A"
hoelzl@54413
  1400
  using fA fB c
hoelzl@54413
  1401
proof (induct arbitrary: B rule: finite_induct)
hoelzl@54413
  1402
  case empty
hoelzl@54413
  1403
  then show ?case by simp
hoelzl@54413
  1404
next
hoelzl@54413
  1405
  case (insert x s t)
hoelzl@54413
  1406
  then show ?case
hoelzl@54413
  1407
  proof (induct rule: finite_induct[OF "insert.prems"(1)])
hoelzl@54413
  1408
    case 1
hoelzl@54413
  1409
    then show ?case by simp
hoelzl@54413
  1410
  next
hoelzl@54413
  1411
    case (2 y t)
hoelzl@54413
  1412
    from "2.prems"(1,2,5) "2.hyps"(1,2) have cst: "card s \<le> card t"
hoelzl@54413
  1413
      by simp
hoelzl@54413
  1414
    from "2.prems"(3) [OF "2.hyps"(1) cst]
hoelzl@54413
  1415
    obtain f where "f ` s \<subseteq> t" "inj_on f s"
hoelzl@54413
  1416
      by blast
hoelzl@54413
  1417
    with "2.prems"(2) "2.hyps"(2) show ?case
hoelzl@54413
  1418
      apply -
hoelzl@54413
  1419
      apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
hoelzl@54413
  1420
      apply (auto simp add: inj_on_def)
hoelzl@54413
  1421
      done
hoelzl@54413
  1422
  qed
hoelzl@54413
  1423
qed
hoelzl@54413
  1424
hoelzl@54413
  1425
lemma card_subset_eq:
hoelzl@54413
  1426
  assumes fB: "finite B"
hoelzl@54413
  1427
    and AB: "A \<subseteq> B"
hoelzl@54413
  1428
    and c: "card A = card B"
hoelzl@54413
  1429
  shows "A = B"
hoelzl@54413
  1430
proof -
hoelzl@54413
  1431
  from fB AB have fA: "finite A"
hoelzl@54413
  1432
    by (auto intro: finite_subset)
hoelzl@54413
  1433
  from fA fB have fBA: "finite (B - A)"
hoelzl@54413
  1434
    by auto
hoelzl@54413
  1435
  have e: "A \<inter> (B - A) = {}"
hoelzl@54413
  1436
    by blast
hoelzl@54413
  1437
  have eq: "A \<union> (B - A) = B"
hoelzl@54413
  1438
    using AB by blast
hoelzl@54413
  1439
  from card_Un_disjoint[OF fA fBA e, unfolded eq c] have "card (B - A) = 0"
hoelzl@54413
  1440
    by arith
hoelzl@54413
  1441
  then have "B - A = {}"
hoelzl@54413
  1442
    unfolding card_eq_0_iff using fA fB by simp
hoelzl@54413
  1443
  with AB show "A = B"
hoelzl@54413
  1444
    by blast
hoelzl@54413
  1445
qed
hoelzl@54413
  1446
haftmann@35722
  1447
lemma insert_partition:
haftmann@35722
  1448
  "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
wenzelm@60585
  1449
  \<Longrightarrow> x \<inter> \<Union>F = {}"
haftmann@35722
  1450
by auto
haftmann@35722
  1451
haftmann@35722
  1452
lemma finite_psubset_induct[consumes 1, case_names psubset]:
urbanc@36079
  1453
  assumes fin: "finite A" 
urbanc@36079
  1454
  and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
urbanc@36079
  1455
  shows "P A"
urbanc@36079
  1456
using fin
urbanc@36079
  1457
proof (induct A taking: card rule: measure_induct_rule)
haftmann@35722
  1458
  case (less A)
urbanc@36079
  1459
  have fin: "finite A" by fact
urbanc@36079
  1460
  have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
urbanc@36079
  1461
  { fix B 
urbanc@36079
  1462
    assume asm: "B \<subset> A"
urbanc@36079
  1463
    from asm have "card B < card A" using psubset_card_mono fin by blast
urbanc@36079
  1464
    moreover
urbanc@36079
  1465
    from asm have "B \<subseteq> A" by auto
urbanc@36079
  1466
    then have "finite B" using fin finite_subset by blast
urbanc@36079
  1467
    ultimately 
urbanc@36079
  1468
    have "P B" using ih by simp
urbanc@36079
  1469
  }
urbanc@36079
  1470
  with fin show "P A" using major by blast
haftmann@35722
  1471
qed
haftmann@35722
  1472
hoelzl@54413
  1473
lemma finite_induct_select[consumes 1, case_names empty select]:
hoelzl@54413
  1474
  assumes "finite S"
hoelzl@54413
  1475
  assumes "P {}"
hoelzl@54413
  1476
  assumes select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)"
hoelzl@54413
  1477
  shows "P S"
hoelzl@54413
  1478
proof -
hoelzl@54413
  1479
  have "0 \<le> card S" by simp
hoelzl@54413
  1480
  then have "\<exists>T \<subseteq> S. card T = card S \<and> P T"
hoelzl@54413
  1481
  proof (induct rule: dec_induct)
wenzelm@60758
  1482
    case base with \<open>P {}\<close> show ?case
hoelzl@54413
  1483
      by (intro exI[of _ "{}"]) auto
hoelzl@54413
  1484
  next
hoelzl@54413
  1485
    case (step n)
hoelzl@54413
  1486
    then obtain T where T: "T \<subseteq> S" "card T = n" "P T"
hoelzl@54413
  1487
      by auto
wenzelm@60758
  1488
    with \<open>n < card S\<close> have "T \<subset> S" "P T"
hoelzl@54413
  1489
      by auto
hoelzl@54413
  1490
    with select[of T] obtain s where "s \<in> S" "s \<notin> T" "P (insert s T)"
hoelzl@54413
  1491
      by auto
wenzelm@60758
  1492
    with step(2) T \<open>finite S\<close> show ?case
hoelzl@54413
  1493
      by (intro exI[of _ "insert s T"]) (auto dest: finite_subset)
hoelzl@54413
  1494
  qed
wenzelm@60758
  1495
  with \<open>finite S\<close> show "P S"
hoelzl@54413
  1496
    by (auto dest: card_subset_eq)
hoelzl@54413
  1497
qed
hoelzl@54413
  1498
wenzelm@60758
  1499
text\<open>main cardinality theorem\<close>
haftmann@35722
  1500
lemma card_partition [rule_format]:
haftmann@35722
  1501
  "finite C ==>
wenzelm@60585
  1502
     finite (\<Union>C) -->
haftmann@35722
  1503
     (\<forall>c\<in>C. card c = k) -->
haftmann@35722
  1504
     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
wenzelm@60585
  1505
     k * card(C) = card (\<Union>C)"
haftmann@35722
  1506
apply (erule finite_induct, simp)
haftmann@35722
  1507
apply (simp add: card_Un_disjoint insert_partition 
wenzelm@60585
  1508
       finite_subset [of _ "\<Union>(insert x F)"])
haftmann@35722
  1509
done
haftmann@35722
  1510
haftmann@35722
  1511
lemma card_eq_UNIV_imp_eq_UNIV:
haftmann@35722
  1512
  assumes fin: "finite (UNIV :: 'a set)"
haftmann@35722
  1513
  and card: "card A = card (UNIV :: 'a set)"
haftmann@35722
  1514
  shows "A = (UNIV :: 'a set)"
haftmann@35722
  1515
proof
haftmann@35722
  1516
  show "A \<subseteq> UNIV" by simp
haftmann@35722
  1517
  show "UNIV \<subseteq> A"
haftmann@35722
  1518
  proof
haftmann@35722
  1519
    fix x
haftmann@35722
  1520
    show "x \<in> A"
haftmann@35722
  1521
    proof (rule ccontr)
haftmann@35722
  1522
      assume "x \<notin> A"
haftmann@35722
  1523
      then have "A \<subset> UNIV" by auto
haftmann@35722
  1524
      with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
haftmann@35722
  1525
      with card show False by simp
haftmann@35722
  1526
    qed
haftmann@35722
  1527
  qed
haftmann@35722
  1528
qed
haftmann@35722
  1529
wenzelm@60758
  1530
text\<open>The form of a finite set of given cardinality\<close>
haftmann@35722
  1531
haftmann@35722
  1532
lemma card_eq_SucD:
haftmann@35722
  1533
assumes "card A = Suc k"
haftmann@35722
  1534
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
haftmann@35722
  1535
proof -
haftmann@35722
  1536
  have fin: "finite A" using assms by (auto intro: ccontr)
haftmann@35722
  1537
  moreover have "card A \<noteq> 0" using assms by auto
haftmann@35722
  1538
  ultimately obtain b where b: "b \<in> A" by auto
haftmann@35722
  1539
  show ?thesis
haftmann@35722
  1540
  proof (intro exI conjI)
haftmann@35722
  1541
    show "A = insert b (A-{b})" using b by blast
haftmann@35722
  1542
    show "b \<notin> A - {b}" by blast
haftmann@35722
  1543
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
nipkow@44890
  1544
      using assms b fin by(fastforce dest:mk_disjoint_insert)+
haftmann@35722
  1545
  qed
haftmann@35722
  1546
qed
haftmann@35722
  1547
haftmann@35722
  1548
lemma card_Suc_eq:
haftmann@35722
  1549
  "(card A = Suc k) =
haftmann@35722
  1550
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
paulson@54570
  1551
 apply(auto elim!: card_eq_SucD)
paulson@54570
  1552
 apply(subst card.insert)
paulson@54570
  1553
 apply(auto simp add: intro:ccontr)
paulson@54570
  1554
 done
haftmann@35722
  1555
paulson@61518
  1556
lemma card_1_singletonE:
paulson@61518
  1557
    assumes "card A = 1" obtains x where "A = {x}"
paulson@61518
  1558
  using assms by (auto simp: card_Suc_eq)
paulson@61518
  1559
nipkow@44744
  1560
lemma card_le_Suc_iff: "finite A \<Longrightarrow>
nipkow@44744
  1561
  Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
nipkow@44890
  1562
by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
nipkow@44744
  1563
  dest: subset_singletonD split: nat.splits if_splits)
nipkow@44744
  1564
haftmann@35722
  1565
lemma finite_fun_UNIVD2:
haftmann@35722
  1566
  assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
haftmann@35722
  1567
  shows "finite (UNIV :: 'b set)"
haftmann@35722
  1568
proof -
haftmann@46146
  1569
  from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
haftmann@46146
  1570
    by (rule finite_imageI)
haftmann@46146
  1571
  moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
haftmann@46146
  1572
    by (rule UNIV_eq_I) auto
haftmann@35722
  1573
  ultimately show "finite (UNIV :: 'b set)" by simp
haftmann@35722
  1574
qed
haftmann@35722
  1575
huffman@48063
  1576
lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
haftmann@35722
  1577
  unfolding UNIV_unit by simp
haftmann@35722
  1578
hoelzl@57447
  1579
lemma infinite_arbitrarily_large:
hoelzl@57447
  1580
  assumes "\<not> finite A"
hoelzl@57447
  1581
  shows "\<exists>B. finite B \<and> card B = n \<and> B \<subseteq> A"
hoelzl@57447
  1582
proof (induction n)
hoelzl@57447
  1583
  case 0 show ?case by (intro exI[of _ "{}"]) auto
hoelzl@57447
  1584
next 
hoelzl@57447
  1585
  case (Suc n)
hoelzl@57447
  1586
  then guess B .. note B = this
wenzelm@60758
  1587
  with \<open>\<not> finite A\<close> have "A \<noteq> B" by auto
hoelzl@57447
  1588
  with B have "B \<subset> A" by auto
hoelzl@57447
  1589
  hence "\<exists>x. x \<in> A - B" by (elim psubset_imp_ex_mem)
hoelzl@57447
  1590
  then guess x .. note x = this
hoelzl@57447
  1591
  with B have "finite (insert x B) \<and> card (insert x B) = Suc n \<and> insert x B \<subseteq> A"
hoelzl@57447
  1592
    by auto
hoelzl@57447
  1593
  thus "\<exists>B. finite B \<and> card B = Suc n \<and> B \<subseteq> A" ..
hoelzl@57447
  1594
qed
haftmann@35722
  1595
wenzelm@60758
  1596
subsubsection \<open>Cardinality of image\<close>
haftmann@35722
  1597
paulson@54570
  1598
lemma card_image_le: "finite A ==> card (f ` A) \<le> card A"
paulson@54570
  1599
  by (induct rule: finite_induct) (simp_all add: le_SucI card_insert_if)
haftmann@35722
  1600
haftmann@35722
  1601
lemma card_image:
haftmann@35722
  1602
  assumes "inj_on f A"
haftmann@35722
  1603
  shows "card (f ` A) = card A"
haftmann@35722
  1604
proof (cases "finite A")
haftmann@35722
  1605
  case True then show ?thesis using assms by (induct A) simp_all
haftmann@35722
  1606
next
haftmann@35722
  1607
  case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
haftmann@35722
  1608
  with False show ?thesis by simp
haftmann@35722
  1609
qed
haftmann@35722
  1610
haftmann@35722
  1611
lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
haftmann@35722
  1612
by(auto simp: card_image bij_betw_def)
haftmann@35722
  1613
haftmann@35722
  1614
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
haftmann@35722
  1615
by (simp add: card_seteq card_image)
haftmann@35722
  1616
haftmann@35722
  1617
lemma eq_card_imp_inj_on:
paulson@54570
  1618
  assumes "finite A" "card(f ` A) = card A" shows "inj_on f A"
paulson@54570
  1619
using assms
paulson@54570
  1620
proof (induct rule:finite_induct)
paulson@54570
  1621
  case empty show ?case by simp
paulson@54570
  1622
next
paulson@54570
  1623
  case (insert x A)
paulson@54570
  1624
  then show ?case using card_image_le [of A f]
paulson@54570
  1625
    by (simp add: card_insert_if split: if_splits)
paulson@54570
  1626
qed
haftmann@35722
  1627
paulson@54570
  1628
lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card(f ` A) = card A"
paulson@54570
  1629
  by (blast intro: card_image eq_card_imp_inj_on)
haftmann@35722
  1630
haftmann@35722
  1631
lemma card_inj_on_le:
paulson@54570
  1632
  assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" shows "card A \<le> card B"
paulson@54570
  1633
proof -
paulson@54570
  1634
  have "finite A" using assms
paulson@54570
  1635
    by (blast intro: finite_imageD dest: finite_subset)
paulson@54570
  1636
  then show ?thesis using assms 
paulson@54570
  1637
   by (force intro: card_mono simp: card_image [symmetric])
paulson@54570
  1638
qed
haftmann@35722
  1639
lp15@59504
  1640
lemma surj_card_le: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> card B \<le> card A"
lp15@59504
  1641
  by (blast intro: card_image_le card_mono le_trans)
lp15@59504
  1642
haftmann@35722
  1643
lemma card_bij_eq:
haftmann@35722
  1644
  "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
haftmann@35722
  1645
     finite A; finite B |] ==> card A = card B"
haftmann@35722
  1646
by (auto intro: le_antisym card_inj_on_le)
haftmann@35722
  1647
hoelzl@40703
  1648
lemma bij_betw_finite:
hoelzl@40703
  1649
  assumes "bij_betw f A B"
hoelzl@40703
  1650
  shows "finite A \<longleftrightarrow> finite B"
hoelzl@40703
  1651
using assms unfolding bij_betw_def
hoelzl@40703
  1652
using finite_imageD[of f A] by auto
haftmann@35722
  1653
blanchet@55020
  1654
lemma inj_on_finite:
blanchet@55020
  1655
assumes "inj_on f A" "f ` A \<le> B" "finite B"
blanchet@55020
  1656
shows "finite A"
blanchet@55020
  1657
using assms finite_imageD finite_subset by blast
blanchet@55020
  1658
Andreas@59520
  1659
lemma card_vimage_inj: "\<lbrakk> inj f; A \<subseteq> range f \<rbrakk> \<Longrightarrow> card (f -` A) = card A"
Andreas@59520
  1660
by(auto 4 3 simp add: subset_image_iff inj_vimage_image_eq intro: card_image[symmetric, OF subset_inj_on])
haftmann@41656
  1661
wenzelm@60758
  1662
subsubsection \<open>Pigeonhole Principles\<close>
nipkow@37466
  1663
nipkow@40311
  1664
lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
nipkow@37466
  1665
by (auto dest: card_image less_irrefl_nat)
nipkow@37466
  1666
nipkow@37466
  1667
lemma pigeonhole_infinite:
nipkow@37466
  1668
assumes  "~ finite A" and "finite(f`A)"
nipkow@37466
  1669
shows "EX a0:A. ~finite{a:A. f a = f a0}"
nipkow@37466
  1670
proof -
nipkow@37466
  1671
  have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
nipkow@37466
  1672
  proof(induct "f`A" arbitrary: A rule: finite_induct)
nipkow@37466
  1673
    case empty thus ?case by simp
nipkow@37466
  1674
  next
nipkow@37466
  1675
    case (insert b F)
nipkow@37466
  1676
    show ?case
nipkow@37466
  1677
    proof cases
nipkow@37466
  1678
      assume "finite{a:A. f a = b}"
wenzelm@60758
  1679
      hence "~ finite(A - {a:A. f a = b})" using \<open>\<not> finite A\<close> by simp
nipkow@37466
  1680
      also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
nipkow@37466
  1681
      finally have "~ finite({a:A. f a \<noteq> b})" .
nipkow@37466
  1682
      from insert(3)[OF _ this]
nipkow@37466
  1683
      show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
nipkow@37466
  1684
    next
nipkow@37466
  1685
      assume 1: "~finite{a:A. f a = b}"
nipkow@37466
  1686
      hence "{a \<in> A. f a = b} \<noteq> {}" by force
nipkow@37466
  1687
      thus ?thesis using 1 by blast
nipkow@37466
  1688
    qed
nipkow@37466
  1689
  qed
nipkow@37466
  1690
  from this[OF assms(2,1)] show ?thesis .
nipkow@37466
  1691
qed
nipkow@37466
  1692
nipkow@37466
  1693
lemma pigeonhole_infinite_rel:
nipkow@37466
  1694
assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
nipkow@37466
  1695
shows "EX b:B. ~finite{a:A. R a b}"
nipkow@37466
  1696
proof -
nipkow@37466
  1697
   let ?F = "%a. {b:B. R a b}"
wenzelm@60758
  1698
   from finite_Pow_iff[THEN iffD2, OF \<open>finite B\<close>]
nipkow@37466
  1699
   have "finite(?F ` A)" by(blast intro: rev_finite_subset)
nipkow@37466
  1700
   from pigeonhole_infinite[where f = ?F, OF assms(1) this]
nipkow@37466
  1701
   obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
wenzelm@60758
  1702
   obtain b0 where "b0 : B" and "R a0 b0" using \<open>a0:A\<close> assms(3) by blast
nipkow@37466
  1703
   { assume "finite{a:A. R a b0}"
nipkow@37466
  1704
     then have "finite {a\<in>A. ?F a = ?F a0}"
wenzelm@60758
  1705
       using \<open>b0 : B\<close> \<open>R a0 b0\<close> by(blast intro: rev_finite_subset)
nipkow@37466
  1706
   }
wenzelm@60758
  1707
   with 1 \<open>b0 : B\<close> show ?thesis by blast
nipkow@37466
  1708
qed
nipkow@37466
  1709
nipkow@37466
  1710
wenzelm@60758
  1711
subsubsection \<open>Cardinality of sums\<close>
haftmann@35722
  1712
haftmann@35722
  1713
lemma card_Plus:
haftmann@35722
  1714
  assumes "finite A" and "finite B"
haftmann@35722
  1715
  shows "card (A <+> B) = card A + card B"
haftmann@35722
  1716
proof -
haftmann@35722
  1717
  have "Inl`A \<inter> Inr`B = {}" by fast
haftmann@35722
  1718
  with assms show ?thesis
haftmann@35722
  1719
    unfolding Plus_def
haftmann@35722
  1720
    by (simp add: card_Un_disjoint card_image)
haftmann@35722
  1721
qed
haftmann@35722
  1722
haftmann@35722
  1723
lemma card_Plus_conv_if:
haftmann@35722
  1724
  "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
haftmann@35722
  1725
  by (auto simp add: card_Plus)
haftmann@35722
  1726
wenzelm@60758
  1727
text \<open>Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.\<close>
haftmann@35722
  1728
haftmann@35722
  1729
lemma dvd_partition:
paulson@54570
  1730
  assumes f: "finite (\<Union>C)" and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
paulson@54570
  1731
    shows "k dvd card (\<Union>C)"
paulson@54570
  1732
proof -
paulson@54570
  1733
  have "finite C" 
paulson@54570
  1734
    by (rule finite_UnionD [OF f])
paulson@54570
  1735
  then show ?thesis using assms
paulson@54570
  1736
  proof (induct rule: finite_induct)
paulson@54570
  1737
    case empty show ?case by simp
paulson@54570
  1738
  next
paulson@54570
  1739
    case (insert c C)
paulson@54570
  1740
    then show ?case 
paulson@54570
  1741
      apply simp
paulson@54570
  1742
      apply (subst card_Un_disjoint)
paulson@54570
  1743
      apply (auto simp add: disjoint_eq_subset_Compl)
paulson@54570
  1744
      done
paulson@54570
  1745
  qed
paulson@54570
  1746
qed
haftmann@35722
  1747
wenzelm@60758
  1748
subsubsection \<open>Relating injectivity and surjectivity\<close>
haftmann@35722
  1749
paulson@54570
  1750
lemma finite_surj_inj: assumes "finite A" "A \<subseteq> f ` A" shows "inj_on f A"
paulson@54570
  1751
proof -
paulson@54570
  1752
  have "f ` A = A" 
paulson@54570
  1753
    by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le)
paulson@54570
  1754
  then show ?thesis using assms
paulson@54570
  1755
    by (simp add: eq_card_imp_inj_on)
paulson@54570
  1756
qed
haftmann@35722
  1757
haftmann@35722
  1758
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  1759
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
hoelzl@40702
  1760
by (blast intro: finite_surj_inj subset_UNIV)
haftmann@35722
  1761
haftmann@35722
  1762
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  1763
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
nipkow@44890
  1764
by(fastforce simp:surj_def dest!: endo_inj_surj)
haftmann@35722
  1765
haftmann@51489
  1766
corollary infinite_UNIV_nat [iff]:
haftmann@51489
  1767
  "\<not> finite (UNIV :: nat set)"
haftmann@35722
  1768
proof
haftmann@51489
  1769
  assume "finite (UNIV :: nat set)"
haftmann@51489
  1770
  with finite_UNIV_inj_surj [of Suc]
haftmann@35722
  1771
  show False by simp (blast dest: Suc_neq_Zero surjD)
haftmann@35722
  1772
qed
haftmann@35722
  1773
blanchet@54147
  1774
lemma infinite_UNIV_char_0:
haftmann@51489
  1775
  "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
haftmann@35722
  1776
proof
haftmann@51489
  1777
  assume "finite (UNIV :: 'a set)"
haftmann@51489
  1778
  with subset_UNIV have "finite (range of_nat :: 'a set)"
haftmann@35722
  1779
    by (rule finite_subset)
haftmann@51489
  1780
  moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
haftmann@35722
  1781
    by (simp add: inj_on_def)
haftmann@51489
  1782
  ultimately have "finite (UNIV :: nat set)"
haftmann@35722
  1783
    by (rule finite_imageD)
haftmann@51489
  1784
  then show False
haftmann@35722
  1785
    by simp
haftmann@35722
  1786
qed
haftmann@35722
  1787
kuncar@49758
  1788
hide_const (open) Finite_Set.fold
haftmann@46033
  1789
lp15@61810
  1790
lp15@61810
  1791
subsection "Infinite Sets"
lp15@61810
  1792
lp15@61810
  1793
text \<open>
lp15@61810
  1794
  Some elementary facts about infinite sets, mostly by Stephan Merz.
lp15@61810
  1795
  Beware! Because "infinite" merely abbreviates a negation, these
lp15@61810
  1796
  lemmas may not work well with \<open>blast\<close>.
lp15@61810
  1797
\<close>
lp15@61810
  1798
lp15@61810
  1799
abbreviation infinite :: "'a set \<Rightarrow> bool"
lp15@61810
  1800
  where "infinite S \<equiv> \<not> finite S"
lp15@61810
  1801
lp15@61810
  1802
text \<open>
lp15@61810
  1803
  Infinite sets are non-empty, and if we remove some elements from an
lp15@61810
  1804
  infinite set, the result is still infinite.
lp15@61810
  1805
\<close>
lp15@61810
  1806
lp15@61810
  1807
lemma infinite_imp_nonempty: "infinite S \<Longrightarrow> S \<noteq> {}"
lp15@61810
  1808
  by auto
lp15@61810
  1809
lp15@61810
  1810
lemma infinite_remove: "infinite S \<Longrightarrow> infinite (S - {a})"
lp15@61810
  1811
  by simp
lp15@61810
  1812
lp15@61810
  1813
lemma Diff_infinite_finite:
lp15@61810
  1814
  assumes T: "finite T" and S: "infinite S"
lp15@61810
  1815
  shows "infinite (S - T)"
lp15@61810
  1816
  using T
lp15@61810
  1817
proof induct
lp15@61810
  1818
  from S
lp15@61810
  1819
  show "infinite (S - {})" by auto
lp15@61810
  1820
next
lp15@61810
  1821
  fix T x
lp15@61810
  1822
  assume ih: "infinite (S - T)"
lp15@61810
  1823
  have "S - (insert x T) = (S - T) - {x}"
lp15@61810
  1824
    by (rule Diff_insert)
lp15@61810
  1825
  with ih
lp15@61810
  1826
  show "infinite (S - (insert x T))"
lp15@61810
  1827
    by (simp add: infinite_remove)
lp15@61810
  1828
qed
lp15@61810
  1829
lp15@61810
  1830
lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)"
lp15@61810
  1831
  by simp
lp15@61810
  1832
lp15@61810
  1833
lemma infinite_Un: "infinite (S \<union> T) \<longleftrightarrow> infinite S \<or> infinite T"
lp15@61810
  1834
  by simp
lp15@61810
  1835
lp15@61810
  1836
lemma infinite_super:
lp15@61810
  1837
  assumes T: "S \<subseteq> T" and S: "infinite S"
lp15@61810
  1838
  shows "infinite T"
lp15@61810
  1839
proof
lp15@61810
  1840
  assume "finite T"
lp15@61810
  1841
  with T have "finite S" by (simp add: finite_subset)
lp15@61810
  1842
  with S show False by simp
lp15@61810
  1843
qed
lp15@61810
  1844
lp15@61810
  1845
proposition infinite_coinduct [consumes 1, case_names infinite]:
lp15@61810
  1846
  assumes "X A"
lp15@61810
  1847
  and step: "\<And>A. X A \<Longrightarrow> \<exists>x\<in>A. X (A - {x}) \<or> infinite (A - {x})"
lp15@61810
  1848
  shows "infinite A"
lp15@61810
  1849
proof
lp15@61810
  1850
  assume "finite A"
lp15@61810
  1851
  then show False using \<open>X A\<close>
lp15@61810
  1852
  proof (induction rule: finite_psubset_induct)
lp15@61810
  1853
    case (psubset A)
lp15@61810
  1854
    then obtain x where "x \<in> A" "X (A - {x}) \<or> infinite (A - {x})"
lp15@61810
  1855
      using local.step psubset.prems by blast
lp15@61810
  1856
    then have "X (A - {x})"
lp15@61810
  1857
      using psubset.hyps by blast
lp15@61810
  1858
    show False
lp15@61810
  1859
      apply (rule psubset.IH [where B = "A - {x}"])
lp15@61810
  1860
      using \<open>x \<in> A\<close> apply blast
lp15@61810
  1861
      by (simp add: \<open>X (A - {x})\<close>)
lp15@61810
  1862
  qed
lp15@61810
  1863
qed
lp15@61810
  1864
lp15@61810
  1865
text \<open>
lp15@61810
  1866
  For any function with infinite domain and finite range there is some
lp15@61810
  1867
  element that is the image of infinitely many domain elements.  In
lp15@61810
  1868
  particular, any infinite sequence of elements from a finite set
lp15@61810
  1869
  contains some element that occurs infinitely often.
lp15@61810
  1870
\<close>
lp15@61810
  1871
lp15@61810
  1872
lemma inf_img_fin_dom':
lp15@61810
  1873
  assumes img: "finite (f ` A)" and dom: "infinite A"
lp15@61810
  1874
  shows "\<exists>y \<in> f ` A. infinite (f -` {y} \<inter> A)"
lp15@61810
  1875
proof (rule ccontr)
lp15@61810
  1876
  have "A \<subseteq> (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by auto
lp15@61810
  1877
  moreover
lp15@61810
  1878
  assume "\<not> ?thesis"
lp15@61810
  1879
  with img have "finite (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by blast
lp15@61810
  1880
  ultimately have "finite A" by(rule finite_subset)
lp15@61810
  1881
  with dom show False by contradiction
lp15@61810
  1882
qed
lp15@61810
  1883
lp15@61810
  1884
lemma inf_img_fin_domE':
lp15@61810
  1885
  assumes "finite (f ` A)" and "infinite A"
lp15@61810
  1886
  obtains y where "y \<in> f`A" and "infinite (f -` {y} \<inter> A)"
lp15@61810
  1887
  using assms by (blast dest: inf_img_fin_dom')
lp15@61810
  1888
lp15@61810
  1889
lemma inf_img_fin_dom:
lp15@61810
  1890
  assumes img: "finite (f`A)" and dom: "infinite A"
lp15@61810
  1891
  shows "\<exists>y \<in> f`A. infinite (f -` {y})"
lp15@61810
  1892
using inf_img_fin_dom'[OF assms] by auto
lp15@61810
  1893
lp15@61810
  1894
lemma inf_img_fin_domE:
lp15@61810
  1895
  assumes "finite (f`A)" and "infinite A"
lp15@61810
  1896
  obtains y where "y \<in> f`A" and "infinite (f -` {y})"
lp15@61810
  1897
  using assms by (blast dest: inf_img_fin_dom)
lp15@61810
  1898
lp15@61810
  1899
proposition finite_image_absD:
lp15@61810
  1900
    fixes S :: "'a::linordered_ring set"
lp15@61810
  1901
    shows "finite (abs ` S) \<Longrightarrow> finite S"
lp15@61810
  1902
  by (rule ccontr) (auto simp: abs_eq_iff vimage_def dest: inf_img_fin_dom)
lp15@61810
  1903
haftmann@35722
  1904
end