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