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