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