src/HOL/Finite_Set.thy
author hoelzl
Tue Nov 05 09:44:57 2013 +0100 (2013-11-05)
changeset 54257 5c7a3b6b05a9
parent 54148 c8cc5ab4a863
child 54413 88a036a95967
permissions -rw-r--r--
generalize SUP and INF to the syntactic type classes Sup and Inf
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@53820
   443
lemma finite_set_of_finite_funs: assumes "finite A" "finite B"
nipkow@53820
   444
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
   445
proof-
nipkow@53820
   446
  let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
nipkow@53820
   447
  have "?F ` ?S \<subseteq> Pow(A \<times> B)" by auto
nipkow@53820
   448
  from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" by simp
nipkow@53820
   449
  have 2: "inj_on ?F ?S"
nipkow@53820
   450
    by(fastforce simp add: inj_on_def set_eq_iff fun_eq_iff)
nipkow@53820
   451
  show ?thesis by(rule finite_imageD[OF 1 2])
nipkow@53820
   452
qed
nipkow@15392
   453
haftmann@41656
   454
subsubsection {* Further induction rules on finite sets *}
haftmann@41656
   455
haftmann@41656
   456
lemma finite_ne_induct [case_names singleton insert, consumes 2]:
haftmann@41656
   457
  assumes "finite F" and "F \<noteq> {}"
haftmann@41656
   458
  assumes "\<And>x. P {x}"
haftmann@41656
   459
    and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
haftmann@41656
   460
  shows "P F"
wenzelm@46898
   461
using assms
wenzelm@46898
   462
proof induct
haftmann@41656
   463
  case empty then show ?case by simp
haftmann@41656
   464
next
haftmann@41656
   465
  case (insert x F) then show ?case by cases auto
haftmann@41656
   466
qed
haftmann@41656
   467
haftmann@41656
   468
lemma finite_subset_induct [consumes 2, case_names empty insert]:
haftmann@41656
   469
  assumes "finite F" and "F \<subseteq> A"
haftmann@41656
   470
  assumes empty: "P {}"
haftmann@41656
   471
    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
   472
  shows "P F"
wenzelm@46898
   473
using `finite F` `F \<subseteq> A`
wenzelm@46898
   474
proof induct
haftmann@41656
   475
  show "P {}" by fact
nipkow@31441
   476
next
haftmann@41656
   477
  fix x F
haftmann@41656
   478
  assume "finite F" and "x \<notin> F" and
haftmann@41656
   479
    P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
haftmann@41656
   480
  show "P (insert x F)"
haftmann@41656
   481
  proof (rule insert)
haftmann@41656
   482
    from i show "x \<in> A" by blast
haftmann@41656
   483
    from i have "F \<subseteq> A" by blast
haftmann@41656
   484
    with P show "P F" .
haftmann@41656
   485
    show "finite F" by fact
haftmann@41656
   486
    show "x \<notin> F" by fact
haftmann@41656
   487
  qed
haftmann@41656
   488
qed
haftmann@41656
   489
haftmann@41656
   490
lemma finite_empty_induct:
haftmann@41656
   491
  assumes "finite A"
haftmann@41656
   492
  assumes "P A"
haftmann@41656
   493
    and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
haftmann@41656
   494
  shows "P {}"
haftmann@41656
   495
proof -
haftmann@41656
   496
  have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
haftmann@41656
   497
  proof -
haftmann@41656
   498
    fix B :: "'a set"
haftmann@41656
   499
    assume "B \<subseteq> A"
haftmann@41656
   500
    with `finite A` have "finite B" by (rule rev_finite_subset)
haftmann@41656
   501
    from this `B \<subseteq> A` show "P (A - B)"
haftmann@41656
   502
    proof induct
haftmann@41656
   503
      case empty
haftmann@41656
   504
      from `P A` show ?case by simp
haftmann@41656
   505
    next
haftmann@41656
   506
      case (insert b B)
haftmann@41656
   507
      have "P (A - B - {b})"
haftmann@41656
   508
      proof (rule remove)
haftmann@41656
   509
        from `finite A` show "finite (A - B)" by induct auto
haftmann@41656
   510
        from insert show "b \<in> A - B" by simp
haftmann@41656
   511
        from insert show "P (A - B)" by simp
haftmann@41656
   512
      qed
haftmann@41656
   513
      also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
haftmann@41656
   514
      finally show ?case .
haftmann@41656
   515
    qed
haftmann@41656
   516
  qed
haftmann@41656
   517
  then have "P (A - A)" by blast
haftmann@41656
   518
  then show ?thesis by simp
nipkow@31441
   519
qed
nipkow@31441
   520
nipkow@31441
   521
haftmann@26441
   522
subsection {* Class @{text finite}  *}
haftmann@26041
   523
haftmann@29797
   524
class finite =
haftmann@26041
   525
  assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
huffman@27430
   526
begin
huffman@27430
   527
huffman@27430
   528
lemma finite [simp]: "finite (A \<Colon> 'a set)"
haftmann@26441
   529
  by (rule subset_UNIV finite_UNIV finite_subset)+
haftmann@26041
   530
haftmann@43866
   531
lemma finite_code [code]: "finite (A \<Colon> 'a set) \<longleftrightarrow> True"
bulwahn@40922
   532
  by simp
bulwahn@40922
   533
huffman@27430
   534
end
huffman@27430
   535
wenzelm@46898
   536
instance prod :: (finite, finite) finite
wenzelm@46898
   537
  by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
haftmann@26146
   538
haftmann@26041
   539
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
nipkow@39302
   540
  by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
haftmann@26041
   541
haftmann@26146
   542
instance "fun" :: (finite, finite) finite
haftmann@26146
   543
proof
haftmann@26041
   544
  show "finite (UNIV :: ('a => 'b) set)"
haftmann@26041
   545
  proof (rule finite_imageD)
haftmann@26041
   546
    let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
berghofe@26792
   547
    have "range ?graph \<subseteq> Pow UNIV" by simp
berghofe@26792
   548
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
berghofe@26792
   549
      by (simp only: finite_Pow_iff finite)
berghofe@26792
   550
    ultimately show "finite (range ?graph)"
berghofe@26792
   551
      by (rule finite_subset)
haftmann@26041
   552
    show "inj ?graph" by (rule inj_graph)
haftmann@26041
   553
  qed
haftmann@26041
   554
qed
haftmann@26041
   555
wenzelm@46898
   556
instance bool :: finite
wenzelm@46898
   557
  by default (simp add: UNIV_bool)
haftmann@44831
   558
haftmann@45962
   559
instance set :: (finite) finite
haftmann@45962
   560
  by default (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
haftmann@45962
   561
wenzelm@46898
   562
instance unit :: finite
wenzelm@46898
   563
  by default (simp add: UNIV_unit)
haftmann@44831
   564
wenzelm@46898
   565
instance sum :: (finite, finite) finite
wenzelm@46898
   566
  by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
haftmann@27981
   567
haftmann@44831
   568
lemma finite_option_UNIV [simp]:
haftmann@44831
   569
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
haftmann@44831
   570
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
haftmann@44831
   571
wenzelm@46898
   572
instance option :: (finite) finite
wenzelm@46898
   573
  by default (simp add: UNIV_option_conv)
haftmann@44831
   574
haftmann@26041
   575
haftmann@35817
   576
subsection {* A basic fold functional for finite sets *}
nipkow@15392
   577
nipkow@15392
   578
text {* The intended behaviour is
wenzelm@53015
   579
@{text "fold f z {x\<^sub>1, ..., x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)"}
nipkow@28853
   580
if @{text f} is ``left-commutative'':
nipkow@15392
   581
*}
nipkow@15392
   582
haftmann@42871
   583
locale comp_fun_commute =
nipkow@28853
   584
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@42871
   585
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
nipkow@28853
   586
begin
nipkow@28853
   587
haftmann@51489
   588
lemma fun_left_comm: "f y (f x z) = f x (f y z)"
haftmann@42871
   589
  using comp_fun_commute by (simp add: fun_eq_iff)
nipkow@28853
   590
haftmann@51489
   591
lemma commute_left_comp:
haftmann@51489
   592
  "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
haftmann@51489
   593
  by (simp add: o_assoc comp_fun_commute)
haftmann@51489
   594
nipkow@28853
   595
end
nipkow@28853
   596
nipkow@28853
   597
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
nipkow@28853
   598
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
nipkow@28853
   599
  emptyI [intro]: "fold_graph f z {} z" |
nipkow@28853
   600
  insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
nipkow@28853
   601
      \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
nipkow@28853
   602
nipkow@28853
   603
inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
nipkow@28853
   604
nipkow@28853
   605
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
haftmann@51489
   606
  "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
nipkow@15392
   607
paulson@15498
   608
text{*A tempting alternative for the definiens is
nipkow@28853
   609
@{term "if finite A then THE y. fold_graph f z A y else e"}.
paulson@15498
   610
It allows the removal of finiteness assumptions from the theorems
nipkow@28853
   611
@{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
nipkow@28853
   612
The proofs become ugly. It is not worth the effort. (???) *}
nipkow@28853
   613
nipkow@28853
   614
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
haftmann@41656
   615
by (induct rule: finite_induct) auto
nipkow@28853
   616
nipkow@28853
   617
nipkow@28853
   618
subsubsection{*From @{const fold_graph} to @{term fold}*}
nipkow@15392
   619
haftmann@42871
   620
context comp_fun_commute
haftmann@26041
   621
begin
haftmann@26041
   622
haftmann@51489
   623
lemma fold_graph_finite:
haftmann@51489
   624
  assumes "fold_graph f z A y"
haftmann@51489
   625
  shows "finite A"
haftmann@51489
   626
  using assms by induct simp_all
haftmann@51489
   627
huffman@36045
   628
lemma fold_graph_insertE_aux:
huffman@36045
   629
  "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
   630
proof (induct set: fold_graph)
huffman@36045
   631
  case (insertI x A y) show ?case
huffman@36045
   632
  proof (cases "x = a")
huffman@36045
   633
    assume "x = a" with insertI show ?case by auto
nipkow@28853
   634
  next
huffman@36045
   635
    assume "x \<noteq> a"
huffman@36045
   636
    then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
huffman@36045
   637
      using insertI by auto
haftmann@42875
   638
    have "f x y = f a (f x y')"
huffman@36045
   639
      unfolding y by (rule fun_left_comm)
haftmann@42875
   640
    moreover have "fold_graph f z (insert x A - {a}) (f x y')"
huffman@36045
   641
      using y' and `x \<noteq> a` and `x \<notin> A`
huffman@36045
   642
      by (simp add: insert_Diff_if fold_graph.insertI)
haftmann@42875
   643
    ultimately show ?case by fast
nipkow@15392
   644
  qed
huffman@36045
   645
qed simp
huffman@36045
   646
huffman@36045
   647
lemma fold_graph_insertE:
huffman@36045
   648
  assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
huffman@36045
   649
  obtains y where "v = f x y" and "fold_graph f z A y"
huffman@36045
   650
using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
nipkow@28853
   651
nipkow@28853
   652
lemma fold_graph_determ:
nipkow@28853
   653
  "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
huffman@36045
   654
proof (induct arbitrary: y set: fold_graph)
huffman@36045
   655
  case (insertI x A y v)
huffman@36045
   656
  from `fold_graph f z (insert x A) v` and `x \<notin> A`
huffman@36045
   657
  obtain y' where "v = f x y'" and "fold_graph f z A y'"
huffman@36045
   658
    by (rule fold_graph_insertE)
huffman@36045
   659
  from `fold_graph f z A y'` have "y' = y" by (rule insertI)
huffman@36045
   660
  with `v = f x y'` show "v = f x y" by simp
huffman@36045
   661
qed fast
nipkow@15392
   662
nipkow@28853
   663
lemma fold_equality:
nipkow@28853
   664
  "fold_graph f z A y \<Longrightarrow> fold f z A = y"
haftmann@51489
   665
  by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
nipkow@15392
   666
haftmann@42272
   667
lemma fold_graph_fold:
haftmann@42272
   668
  assumes "finite A"
haftmann@42272
   669
  shows "fold_graph f z A (fold f z A)"
haftmann@42272
   670
proof -
haftmann@42272
   671
  from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
haftmann@42272
   672
  moreover note fold_graph_determ
haftmann@42272
   673
  ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
haftmann@42272
   674
  then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
haftmann@51489
   675
  with assms show ?thesis by (simp add: fold_def)
haftmann@42272
   676
qed
huffman@36045
   677
haftmann@51489
   678
text {* The base case for @{text fold}: *}
nipkow@15392
   679
haftmann@51489
   680
lemma (in -) fold_infinite [simp]:
haftmann@51489
   681
  assumes "\<not> finite A"
haftmann@51489
   682
  shows "fold f z A = z"
haftmann@51489
   683
  using assms by (auto simp add: fold_def)
haftmann@51489
   684
haftmann@51489
   685
lemma (in -) fold_empty [simp]:
haftmann@51489
   686
  "fold f z {} = z"
haftmann@51489
   687
  by (auto simp add: fold_def)
nipkow@28853
   688
nipkow@28853
   689
text{* The various recursion equations for @{const fold}: *}
nipkow@28853
   690
haftmann@26041
   691
lemma fold_insert [simp]:
haftmann@42875
   692
  assumes "finite A" and "x \<notin> A"
haftmann@42875
   693
  shows "fold f z (insert x A) = f x (fold f z A)"
haftmann@42875
   694
proof (rule fold_equality)
haftmann@51489
   695
  fix z
haftmann@42875
   696
  from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
haftmann@51489
   697
  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
   698
  then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
haftmann@42875
   699
qed
nipkow@28853
   700
haftmann@51489
   701
declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
haftmann@51489
   702
  -- {* No more proofs involve these. *}
haftmann@51489
   703
haftmann@51489
   704
lemma fold_fun_left_comm:
nipkow@28853
   705
  "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
nipkow@28853
   706
proof (induct rule: finite_induct)
nipkow@28853
   707
  case empty then show ?case by simp
nipkow@28853
   708
next
nipkow@28853
   709
  case (insert y A) then show ?case
haftmann@51489
   710
    by (simp add: fun_left_comm [of x])
nipkow@28853
   711
qed
nipkow@28853
   712
nipkow@28853
   713
lemma fold_insert2:
haftmann@51489
   714
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
haftmann@51489
   715
  by (simp add: fold_fun_left_comm)
nipkow@15392
   716
haftmann@26041
   717
lemma fold_rec:
haftmann@42875
   718
  assumes "finite A" and "x \<in> A"
haftmann@42875
   719
  shows "fold f z A = f x (fold f z (A - {x}))"
nipkow@28853
   720
proof -
nipkow@28853
   721
  have A: "A = insert x (A - {x})" using `x \<in> A` by blast
nipkow@28853
   722
  then have "fold f z A = fold f z (insert x (A - {x}))" by simp
nipkow@28853
   723
  also have "\<dots> = f x (fold f z (A - {x}))"
nipkow@28853
   724
    by (rule fold_insert) (simp add: `finite A`)+
nipkow@15535
   725
  finally show ?thesis .
nipkow@15535
   726
qed
nipkow@15535
   727
nipkow@28853
   728
lemma fold_insert_remove:
nipkow@28853
   729
  assumes "finite A"
nipkow@28853
   730
  shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
nipkow@28853
   731
proof -
nipkow@28853
   732
  from `finite A` have "finite (insert x A)" by auto
nipkow@28853
   733
  moreover have "x \<in> insert x A" by auto
nipkow@28853
   734
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
nipkow@28853
   735
    by (rule fold_rec)
nipkow@28853
   736
  then show ?thesis by simp
nipkow@28853
   737
qed
nipkow@28853
   738
haftmann@51598
   739
end
haftmann@51598
   740
kuncar@48619
   741
text{* Other properties of @{const fold}: *}
kuncar@48619
   742
kuncar@48619
   743
lemma fold_image:
haftmann@51598
   744
  assumes "inj_on g A"
haftmann@51489
   745
  shows "fold f z (g ` A) = fold (f \<circ> g) z A"
haftmann@51598
   746
proof (cases "finite A")
haftmann@51598
   747
  case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
haftmann@51598
   748
next
haftmann@51598
   749
  case True
haftmann@51598
   750
  have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
haftmann@51598
   751
  proof
haftmann@51598
   752
    fix w
haftmann@51598
   753
    show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
haftmann@51598
   754
    proof
haftmann@51598
   755
      assume ?P then show ?Q using assms
haftmann@51598
   756
      proof (induct "g ` A" w arbitrary: A)
haftmann@51598
   757
        case emptyI then show ?case by (auto intro: fold_graph.emptyI)
haftmann@51598
   758
      next
haftmann@51598
   759
        case (insertI x A r B)
haftmann@51598
   760
        from `inj_on g B` `x \<notin> A` `insert x A = image g B` obtain x' A' where
haftmann@51598
   761
          "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
haftmann@51598
   762
          by (rule inj_img_insertE)
haftmann@51598
   763
        from insertI.prems have "fold_graph (f o g) z A' r"
haftmann@51598
   764
          by (auto intro: insertI.hyps)
haftmann@51598
   765
        with `x' \<notin> A'` have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
haftmann@51598
   766
          by (rule fold_graph.insertI)
haftmann@51598
   767
        then show ?case by simp
haftmann@51598
   768
      qed
haftmann@51598
   769
    next
haftmann@51598
   770
      assume ?Q then show ?P using assms
haftmann@51598
   771
      proof induct
haftmann@51598
   772
        case emptyI thus ?case by (auto intro: fold_graph.emptyI)
haftmann@51598
   773
      next
haftmann@51598
   774
        case (insertI x A r)
haftmann@51598
   775
        from `x \<notin> A` insertI.prems have "g x \<notin> g ` A" by auto
haftmann@51598
   776
        moreover from insertI have "fold_graph f z (g ` A) r" by simp
haftmann@51598
   777
        ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
haftmann@51598
   778
          by (rule fold_graph.insertI)
haftmann@51598
   779
        then show ?case by simp
haftmann@51598
   780
      qed
haftmann@51598
   781
    qed
haftmann@51598
   782
  qed
haftmann@51598
   783
  with True assms show ?thesis by (auto simp add: fold_def)
haftmann@51598
   784
qed
nipkow@15392
   785
haftmann@49724
   786
lemma fold_cong:
haftmann@49724
   787
  assumes "comp_fun_commute f" "comp_fun_commute g"
haftmann@49724
   788
  assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
haftmann@51489
   789
    and "s = t" and "A = B"
haftmann@51489
   790
  shows "fold f s A = fold g t B"
haftmann@49724
   791
proof -
haftmann@51489
   792
  have "fold f s A = fold g s A"  
haftmann@49724
   793
  using `finite A` cong proof (induct A)
haftmann@49724
   794
    case empty then show ?case by simp
haftmann@49724
   795
  next
haftmann@49724
   796
    case (insert x A)
haftmann@49724
   797
    interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
haftmann@49724
   798
    interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
haftmann@49724
   799
    from insert show ?case by simp
haftmann@49724
   800
  qed
haftmann@49724
   801
  with assms show ?thesis by simp
haftmann@49724
   802
qed
haftmann@49724
   803
haftmann@49724
   804
haftmann@51489
   805
text {* A simplified version for idempotent functions: *}
nipkow@15480
   806
haftmann@42871
   807
locale comp_fun_idem = comp_fun_commute +
haftmann@51489
   808
  assumes comp_fun_idem: "f x \<circ> f x = f x"
haftmann@26041
   809
begin
haftmann@26041
   810
haftmann@42869
   811
lemma fun_left_idem: "f x (f x z) = f x z"
haftmann@42871
   812
  using comp_fun_idem by (simp add: fun_eq_iff)
nipkow@28853
   813
haftmann@26041
   814
lemma fold_insert_idem:
nipkow@28853
   815
  assumes fin: "finite A"
haftmann@51489
   816
  shows "fold f z (insert x A)  = f x (fold f z A)"
nipkow@15480
   817
proof cases
nipkow@28853
   818
  assume "x \<in> A"
nipkow@28853
   819
  then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
haftmann@51489
   820
  then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
nipkow@15480
   821
next
nipkow@28853
   822
  assume "x \<notin> A" then show ?thesis using assms by simp
nipkow@15480
   823
qed
nipkow@15480
   824
haftmann@51489
   825
declare fold_insert [simp del] fold_insert_idem [simp]
nipkow@28853
   826
nipkow@28853
   827
lemma fold_insert_idem2:
nipkow@28853
   828
  "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
haftmann@51489
   829
  by (simp add: fold_fun_left_comm)
nipkow@15484
   830
haftmann@26041
   831
end
haftmann@26041
   832
haftmann@35817
   833
haftmann@49723
   834
subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
haftmann@35817
   835
haftmann@42871
   836
lemma (in comp_fun_commute) comp_comp_fun_commute:
haftmann@42871
   837
  "comp_fun_commute (f \<circ> g)"
haftmann@35817
   838
proof
haftmann@42871
   839
qed (simp_all add: comp_fun_commute)
haftmann@35817
   840
haftmann@42871
   841
lemma (in comp_fun_idem) comp_comp_fun_idem:
haftmann@42871
   842
  "comp_fun_idem (f \<circ> g)"
haftmann@42871
   843
  by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
haftmann@42871
   844
    (simp_all add: comp_fun_idem)
haftmann@35817
   845
haftmann@49723
   846
lemma (in comp_fun_commute) comp_fun_commute_funpow:
haftmann@49723
   847
  "comp_fun_commute (\<lambda>x. f x ^^ g x)"
haftmann@49723
   848
proof
haftmann@49723
   849
  fix y x
haftmann@49723
   850
  show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
haftmann@49723
   851
  proof (cases "x = y")
haftmann@49723
   852
    case True then show ?thesis by simp
haftmann@49723
   853
  next
haftmann@49723
   854
    case False show ?thesis
haftmann@49723
   855
    proof (induct "g x" arbitrary: g)
haftmann@49723
   856
      case 0 then show ?case by simp
haftmann@49723
   857
    next
haftmann@49723
   858
      case (Suc n g)
haftmann@49723
   859
      have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
haftmann@49723
   860
      proof (induct "g y" arbitrary: g)
haftmann@49723
   861
        case 0 then show ?case by simp
haftmann@49723
   862
      next
haftmann@49723
   863
        case (Suc n g)
haftmann@49723
   864
        def h \<equiv> "\<lambda>z. g z - 1"
haftmann@49723
   865
        with Suc have "n = h y" by simp
haftmann@49723
   866
        with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
haftmann@49723
   867
          by auto
haftmann@49723
   868
        from Suc h_def have "g y = Suc (h y)" by simp
haftmann@49739
   869
        then show ?case by (simp add: comp_assoc hyp)
haftmann@49723
   870
          (simp add: o_assoc comp_fun_commute)
haftmann@49723
   871
      qed
haftmann@49723
   872
      def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
haftmann@49723
   873
      with Suc have "n = h x" by simp
haftmann@49723
   874
      with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
haftmann@49723
   875
        by auto
haftmann@49723
   876
      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
   877
      from Suc h_def have "g x = Suc (h x)" by simp
haftmann@49723
   878
      then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
haftmann@49739
   879
        (simp add: comp_assoc hyp1)
haftmann@49723
   880
    qed
haftmann@49723
   881
  qed
haftmann@49723
   882
qed
haftmann@49723
   883
haftmann@49723
   884
haftmann@49723
   885
subsubsection {* Expressing set operations via @{const fold} *}
haftmann@49723
   886
haftmann@51489
   887
lemma comp_fun_commute_const:
haftmann@51489
   888
  "comp_fun_commute (\<lambda>_. f)"
haftmann@51489
   889
proof
haftmann@51489
   890
qed rule
haftmann@51489
   891
haftmann@42871
   892
lemma comp_fun_idem_insert:
haftmann@42871
   893
  "comp_fun_idem insert"
haftmann@35817
   894
proof
haftmann@35817
   895
qed auto
haftmann@35817
   896
haftmann@42871
   897
lemma comp_fun_idem_remove:
haftmann@46146
   898
  "comp_fun_idem Set.remove"
haftmann@35817
   899
proof
haftmann@35817
   900
qed auto
nipkow@31992
   901
haftmann@42871
   902
lemma (in semilattice_inf) comp_fun_idem_inf:
haftmann@42871
   903
  "comp_fun_idem inf"
haftmann@35817
   904
proof
haftmann@35817
   905
qed (auto simp add: inf_left_commute)
haftmann@35817
   906
haftmann@42871
   907
lemma (in semilattice_sup) comp_fun_idem_sup:
haftmann@42871
   908
  "comp_fun_idem sup"
haftmann@35817
   909
proof
haftmann@35817
   910
qed (auto simp add: sup_left_commute)
nipkow@31992
   911
haftmann@35817
   912
lemma union_fold_insert:
haftmann@35817
   913
  assumes "finite A"
haftmann@35817
   914
  shows "A \<union> B = fold insert B A"
haftmann@35817
   915
proof -
haftmann@42871
   916
  interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
haftmann@35817
   917
  from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
haftmann@35817
   918
qed
nipkow@31992
   919
haftmann@35817
   920
lemma minus_fold_remove:
haftmann@35817
   921
  assumes "finite A"
haftmann@46146
   922
  shows "B - A = fold Set.remove B A"
haftmann@35817
   923
proof -
haftmann@46146
   924
  interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
haftmann@46146
   925
  from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
haftmann@46146
   926
  then show ?thesis ..
haftmann@35817
   927
qed
haftmann@35817
   928
haftmann@51489
   929
lemma comp_fun_commute_filter_fold:
haftmann@51489
   930
  "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
kuncar@48619
   931
proof - 
kuncar@48619
   932
  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
kuncar@48619
   933
  show ?thesis by default (auto simp: fun_eq_iff)
kuncar@48619
   934
qed
kuncar@48619
   935
kuncar@49758
   936
lemma Set_filter_fold:
kuncar@48619
   937
  assumes "finite A"
kuncar@49758
   938
  shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
kuncar@48619
   939
using assms
kuncar@48619
   940
by (induct A) 
kuncar@49758
   941
  (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
kuncar@49758
   942
kuncar@49758
   943
lemma inter_Set_filter:     
kuncar@49758
   944
  assumes "finite B"
kuncar@49758
   945
  shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
kuncar@49758
   946
using assms 
kuncar@49758
   947
by (induct B) (auto simp: Set.filter_def)
kuncar@48619
   948
kuncar@48619
   949
lemma image_fold_insert:
kuncar@48619
   950
  assumes "finite A"
kuncar@48619
   951
  shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
kuncar@48619
   952
using assms
kuncar@48619
   953
proof -
kuncar@48619
   954
  interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
kuncar@48619
   955
  show ?thesis using assms by (induct A) auto
kuncar@48619
   956
qed
kuncar@48619
   957
kuncar@48619
   958
lemma Ball_fold:
kuncar@48619
   959
  assumes "finite A"
kuncar@48619
   960
  shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
kuncar@48619
   961
using assms
kuncar@48619
   962
proof -
kuncar@48619
   963
  interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
kuncar@48619
   964
  show ?thesis using assms by (induct A) auto
kuncar@48619
   965
qed
kuncar@48619
   966
kuncar@48619
   967
lemma Bex_fold:
kuncar@48619
   968
  assumes "finite A"
kuncar@48619
   969
  shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
kuncar@48619
   970
using assms
kuncar@48619
   971
proof -
kuncar@48619
   972
  interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
kuncar@48619
   973
  show ?thesis using assms by (induct A) auto
kuncar@48619
   974
qed
kuncar@48619
   975
kuncar@48619
   976
lemma comp_fun_commute_Pow_fold: 
kuncar@48619
   977
  "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
kuncar@48619
   978
  by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
kuncar@48619
   979
kuncar@48619
   980
lemma Pow_fold:
kuncar@48619
   981
  assumes "finite A"
kuncar@48619
   982
  shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
kuncar@48619
   983
using assms
kuncar@48619
   984
proof -
kuncar@48619
   985
  interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
kuncar@48619
   986
  show ?thesis using assms by (induct A) (auto simp: Pow_insert)
kuncar@48619
   987
qed
kuncar@48619
   988
kuncar@48619
   989
lemma fold_union_pair:
kuncar@48619
   990
  assumes "finite B"
kuncar@48619
   991
  shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
kuncar@48619
   992
proof -
kuncar@48619
   993
  interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
kuncar@48619
   994
  show ?thesis using assms  by (induct B arbitrary: A) simp_all
kuncar@48619
   995
qed
kuncar@48619
   996
kuncar@48619
   997
lemma comp_fun_commute_product_fold: 
kuncar@48619
   998
  assumes "finite B"
haftmann@51489
   999
  shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
kuncar@48619
  1000
by default (auto simp: fold_union_pair[symmetric] assms)
kuncar@48619
  1001
kuncar@48619
  1002
lemma product_fold:
kuncar@48619
  1003
  assumes "finite A"
kuncar@48619
  1004
  assumes "finite B"
haftmann@51489
  1005
  shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
kuncar@48619
  1006
using assms unfolding Sigma_def 
kuncar@48619
  1007
by (induct A) 
kuncar@48619
  1008
  (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
kuncar@48619
  1009
kuncar@48619
  1010
haftmann@35817
  1011
context complete_lattice
nipkow@31992
  1012
begin
nipkow@31992
  1013
haftmann@35817
  1014
lemma inf_Inf_fold_inf:
haftmann@35817
  1015
  assumes "finite A"
haftmann@51489
  1016
  shows "inf (Inf A) B = fold inf B A"
haftmann@35817
  1017
proof -
haftmann@42871
  1018
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
haftmann@51489
  1019
  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
haftmann@51489
  1020
    (simp_all add: inf_commute fun_eq_iff)
haftmann@35817
  1021
qed
nipkow@31992
  1022
haftmann@35817
  1023
lemma sup_Sup_fold_sup:
haftmann@35817
  1024
  assumes "finite A"
haftmann@51489
  1025
  shows "sup (Sup A) B = fold sup B A"
haftmann@35817
  1026
proof -
haftmann@42871
  1027
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
haftmann@51489
  1028
  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
haftmann@51489
  1029
    (simp_all add: sup_commute fun_eq_iff)
nipkow@31992
  1030
qed
nipkow@31992
  1031
haftmann@35817
  1032
lemma Inf_fold_inf:
haftmann@35817
  1033
  assumes "finite A"
haftmann@35817
  1034
  shows "Inf A = fold inf top A"
haftmann@35817
  1035
  using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
haftmann@35817
  1036
haftmann@35817
  1037
lemma Sup_fold_sup:
haftmann@35817
  1038
  assumes "finite A"
haftmann@35817
  1039
  shows "Sup A = fold sup bot A"
haftmann@35817
  1040
  using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
nipkow@31992
  1041
haftmann@46146
  1042
lemma inf_INF_fold_inf:
haftmann@35817
  1043
  assumes "finite A"
haftmann@42873
  1044
  shows "inf B (INFI A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
haftmann@35817
  1045
proof (rule sym)
haftmann@42871
  1046
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
haftmann@42871
  1047
  interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
haftmann@42873
  1048
  from `finite A` show "?fold = ?inf"
haftmann@42869
  1049
    by (induct A arbitrary: B)
hoelzl@44928
  1050
      (simp_all add: INF_def inf_left_commute)
haftmann@35817
  1051
qed
nipkow@31992
  1052
haftmann@46146
  1053
lemma sup_SUP_fold_sup:
haftmann@35817
  1054
  assumes "finite A"
haftmann@42873
  1055
  shows "sup B (SUPR A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
haftmann@35817
  1056
proof (rule sym)
haftmann@42871
  1057
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
haftmann@42871
  1058
  interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
haftmann@42873
  1059
  from `finite A` show "?fold = ?sup"
haftmann@42869
  1060
    by (induct A arbitrary: B)
hoelzl@44928
  1061
      (simp_all add: SUP_def sup_left_commute)
haftmann@35817
  1062
qed
nipkow@31992
  1063
haftmann@46146
  1064
lemma INF_fold_inf:
haftmann@35817
  1065
  assumes "finite A"
haftmann@42873
  1066
  shows "INFI A f = fold (inf \<circ> f) top A"
haftmann@46146
  1067
  using assms inf_INF_fold_inf [of A top] by simp
nipkow@31992
  1068
haftmann@46146
  1069
lemma SUP_fold_sup:
haftmann@35817
  1070
  assumes "finite A"
haftmann@42873
  1071
  shows "SUPR A f = fold (sup \<circ> f) bot A"
haftmann@46146
  1072
  using assms sup_SUP_fold_sup [of A bot] by simp
nipkow@31992
  1073
nipkow@31992
  1074
end
nipkow@31992
  1075
nipkow@31992
  1076
haftmann@35817
  1077
subsection {* Locales as mini-packages for fold operations *}
haftmann@34007
  1078
haftmann@35817
  1079
subsubsection {* The natural case *}
haftmann@35719
  1080
haftmann@35719
  1081
locale folding =
haftmann@35719
  1082
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@51489
  1083
  fixes z :: "'b"
haftmann@42871
  1084
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
haftmann@35719
  1085
begin
haftmann@35719
  1086
haftmann@51489
  1087
definition F :: "'a set \<Rightarrow> 'b"
haftmann@51489
  1088
where
haftmann@51489
  1089
  eq_fold: "F A = fold f z A"
haftmann@51489
  1090
haftmann@35719
  1091
lemma empty [simp]:
haftmann@51489
  1092
  "F {} = z"
haftmann@51489
  1093
  by (simp add: eq_fold)
haftmann@35719
  1094
haftmann@51489
  1095
lemma infinite [simp]:
haftmann@51489
  1096
  "\<not> finite A \<Longrightarrow> F A = z"
haftmann@51489
  1097
  by (simp add: eq_fold)
haftmann@51489
  1098
 
haftmann@35719
  1099
lemma insert [simp]:
haftmann@35719
  1100
  assumes "finite A" and "x \<notin> A"
haftmann@51489
  1101
  shows "F (insert x A) = f x (F A)"
haftmann@35719
  1102
proof -
wenzelm@46898
  1103
  interpret comp_fun_commute f
wenzelm@46898
  1104
    by default (insert comp_fun_commute, simp add: fun_eq_iff)
haftmann@51489
  1105
  from fold_insert assms
haftmann@51489
  1106
  have "fold f z (insert x A) = f x (fold f z A)" by simp
nipkow@39302
  1107
  with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
haftmann@35719
  1108
qed
haftmann@51489
  1109
 
haftmann@35719
  1110
lemma remove:
haftmann@35719
  1111
  assumes "finite A" and "x \<in> A"
haftmann@51489
  1112
  shows "F A = f x (F (A - {x}))"
haftmann@35719
  1113
proof -
haftmann@35719
  1114
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@35719
  1115
    by (auto dest: mk_disjoint_insert)
wenzelm@53374
  1116
  moreover from `finite A` A have "finite B" by simp
haftmann@35719
  1117
  ultimately show ?thesis by simp
haftmann@35719
  1118
qed
haftmann@35719
  1119
haftmann@35719
  1120
lemma insert_remove:
haftmann@35719
  1121
  assumes "finite A"
haftmann@51489
  1122
  shows "F (insert x A) = f x (F (A - {x}))"
haftmann@35722
  1123
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@35719
  1124
haftmann@34007
  1125
end
haftmann@35719
  1126
haftmann@35817
  1127
haftmann@51489
  1128
subsubsection {* With idempotency *}
haftmann@35817
  1129
haftmann@35719
  1130
locale folding_idem = folding +
haftmann@51489
  1131
  assumes comp_fun_idem: "f x \<circ> f x = f x"
haftmann@35719
  1132
begin
haftmann@35719
  1133
haftmann@35817
  1134
declare insert [simp del]
haftmann@35719
  1135
haftmann@35719
  1136
lemma insert_idem [simp]:
haftmann@35719
  1137
  assumes "finite A"
haftmann@51489
  1138
  shows "F (insert x A) = f x (F A)"
haftmann@35817
  1139
proof -
haftmann@51489
  1140
  interpret comp_fun_idem f
haftmann@51489
  1141
    by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
haftmann@51489
  1142
  from fold_insert_idem assms
haftmann@51489
  1143
  have "fold f z (insert x A) = f x (fold f z A)" by simp
haftmann@51489
  1144
  with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
haftmann@35719
  1145
qed
haftmann@35719
  1146
haftmann@35719
  1147
end
haftmann@35719
  1148
haftmann@35817
  1149
haftmann@35722
  1150
subsection {* Finite cardinality *}
haftmann@35722
  1151
haftmann@51489
  1152
text {*
haftmann@51489
  1153
  The traditional definition
haftmann@51489
  1154
  @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
haftmann@51489
  1155
  is ugly to work with.
haftmann@51489
  1156
  But now that we have @{const fold} things are easy:
haftmann@35722
  1157
*}
haftmann@35722
  1158
haftmann@35722
  1159
definition card :: "'a set \<Rightarrow> nat" where
haftmann@51489
  1160
  "card = folding.F (\<lambda>_. Suc) 0"
haftmann@35722
  1161
haftmann@51489
  1162
interpretation card!: folding "\<lambda>_. Suc" 0
haftmann@51489
  1163
where
haftmann@51546
  1164
  "folding.F (\<lambda>_. Suc) 0 = card"
haftmann@51489
  1165
proof -
haftmann@51489
  1166
  show "folding (\<lambda>_. Suc)" by default rule
haftmann@51489
  1167
  then interpret card!: folding "\<lambda>_. Suc" 0 .
haftmann@51546
  1168
  from card_def show "folding.F (\<lambda>_. Suc) 0 = card" by rule
haftmann@51489
  1169
qed
haftmann@35722
  1170
haftmann@51489
  1171
lemma card_infinite:
haftmann@35722
  1172
  "\<not> finite A \<Longrightarrow> card A = 0"
haftmann@51489
  1173
  by (fact card.infinite)
haftmann@35722
  1174
haftmann@35722
  1175
lemma card_empty:
haftmann@35722
  1176
  "card {} = 0"
haftmann@35722
  1177
  by (fact card.empty)
haftmann@35722
  1178
haftmann@35722
  1179
lemma card_insert_disjoint:
haftmann@51489
  1180
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
haftmann@51489
  1181
  by (fact card.insert)
haftmann@35722
  1182
haftmann@35722
  1183
lemma card_insert_if:
haftmann@51489
  1184
  "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
haftmann@35722
  1185
  by auto (simp add: card.insert_remove card.remove)
haftmann@35722
  1186
haftmann@35722
  1187
lemma card_ge_0_finite:
haftmann@35722
  1188
  "card A > 0 \<Longrightarrow> finite A"
haftmann@35722
  1189
  by (rule ccontr) simp
haftmann@35722
  1190
blanchet@54148
  1191
lemma card_0_eq [simp]:
haftmann@35722
  1192
  "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
haftmann@35722
  1193
  by (auto dest: mk_disjoint_insert)
haftmann@35722
  1194
haftmann@35722
  1195
lemma finite_UNIV_card_ge_0:
haftmann@35722
  1196
  "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
haftmann@35722
  1197
  by (rule ccontr) simp
haftmann@35722
  1198
haftmann@35722
  1199
lemma card_eq_0_iff:
haftmann@35722
  1200
  "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
haftmann@35722
  1201
  by auto
haftmann@35722
  1202
haftmann@35722
  1203
lemma card_gt_0_iff:
haftmann@35722
  1204
  "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
haftmann@35722
  1205
  by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
haftmann@35722
  1206
haftmann@51489
  1207
lemma card_Suc_Diff1:
haftmann@51489
  1208
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
haftmann@35722
  1209
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
haftmann@35722
  1210
apply(simp del:insert_Diff_single)
haftmann@35722
  1211
done
haftmann@35722
  1212
haftmann@35722
  1213
lemma card_Diff_singleton:
haftmann@51489
  1214
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
haftmann@51489
  1215
  by (simp add: card_Suc_Diff1 [symmetric])
haftmann@35722
  1216
haftmann@35722
  1217
lemma card_Diff_singleton_if:
haftmann@51489
  1218
  "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
haftmann@51489
  1219
  by (simp add: card_Diff_singleton)
haftmann@35722
  1220
haftmann@35722
  1221
lemma card_Diff_insert[simp]:
haftmann@51489
  1222
  assumes "finite A" and "a \<in> A" and "a \<notin> B"
haftmann@51489
  1223
  shows "card (A - insert a B) = card (A - B) - 1"
haftmann@35722
  1224
proof -
haftmann@35722
  1225
  have "A - insert a B = (A - B) - {a}" using assms by blast
haftmann@51489
  1226
  then show ?thesis using assms by(simp add: card_Diff_singleton)
haftmann@35722
  1227
qed
haftmann@35722
  1228
haftmann@35722
  1229
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
haftmann@51489
  1230
  by (fact card.insert_remove)
haftmann@35722
  1231
haftmann@35722
  1232
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
haftmann@35722
  1233
by (simp add: card_insert_if)
haftmann@35722
  1234
nipkow@41987
  1235
lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
nipkow@41987
  1236
by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
nipkow@41987
  1237
nipkow@41988
  1238
lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
nipkow@41987
  1239
using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
nipkow@41987
  1240
haftmann@35722
  1241
lemma card_mono:
haftmann@35722
  1242
  assumes "finite B" and "A \<subseteq> B"
haftmann@35722
  1243
  shows "card A \<le> card B"
haftmann@35722
  1244
proof -
haftmann@35722
  1245
  from assms have "finite A" by (auto intro: finite_subset)
haftmann@35722
  1246
  then show ?thesis using assms proof (induct A arbitrary: B)
haftmann@35722
  1247
    case empty then show ?case by simp
haftmann@35722
  1248
  next
haftmann@35722
  1249
    case (insert x A)
haftmann@35722
  1250
    then have "x \<in> B" by simp
haftmann@35722
  1251
    from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
haftmann@35722
  1252
    with insert.hyps have "card A \<le> card (B - {x})" by auto
haftmann@35722
  1253
    with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
haftmann@35722
  1254
  qed
haftmann@35722
  1255
qed
haftmann@35722
  1256
haftmann@35722
  1257
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
haftmann@41656
  1258
apply (induct rule: finite_induct)
haftmann@41656
  1259
apply simp
haftmann@41656
  1260
apply clarify
haftmann@35722
  1261
apply (subgoal_tac "finite A & A - {x} <= F")
haftmann@35722
  1262
 prefer 2 apply (blast intro: finite_subset, atomize)
haftmann@35722
  1263
apply (drule_tac x = "A - {x}" in spec)
haftmann@35722
  1264
apply (simp add: card_Diff_singleton_if split add: split_if_asm)
haftmann@35722
  1265
apply (case_tac "card A", auto)
haftmann@35722
  1266
done
haftmann@35722
  1267
haftmann@35722
  1268
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
haftmann@35722
  1269
apply (simp add: psubset_eq linorder_not_le [symmetric])
haftmann@35722
  1270
apply (blast dest: card_seteq)
haftmann@35722
  1271
done
haftmann@35722
  1272
haftmann@51489
  1273
lemma card_Un_Int:
haftmann@51489
  1274
  assumes "finite A" and "finite B"
haftmann@51489
  1275
  shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
haftmann@51489
  1276
using assms proof (induct A)
haftmann@51489
  1277
  case empty then show ?case by simp
haftmann@51489
  1278
next
haftmann@51489
  1279
 case (insert x A) then show ?case
haftmann@51489
  1280
    by (auto simp add: insert_absorb Int_insert_left)
haftmann@51489
  1281
qed
haftmann@35722
  1282
haftmann@51489
  1283
lemma card_Un_disjoint:
haftmann@51489
  1284
  assumes "finite A" and "finite B"
haftmann@51489
  1285
  assumes "A \<inter> B = {}"
haftmann@51489
  1286
  shows "card (A \<union> B) = card A + card B"
haftmann@51489
  1287
using assms card_Un_Int [of A B] by simp
haftmann@35722
  1288
haftmann@35722
  1289
lemma card_Diff_subset:
haftmann@35722
  1290
  assumes "finite B" and "B \<subseteq> A"
haftmann@35722
  1291
  shows "card (A - B) = card A - card B"
haftmann@35722
  1292
proof (cases "finite A")
haftmann@35722
  1293
  case False with assms show ?thesis by simp
haftmann@35722
  1294
next
haftmann@35722
  1295
  case True with assms show ?thesis by (induct B arbitrary: A) simp_all
haftmann@35722
  1296
qed
haftmann@35722
  1297
haftmann@35722
  1298
lemma card_Diff_subset_Int:
haftmann@35722
  1299
  assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
haftmann@35722
  1300
proof -
haftmann@35722
  1301
  have "A - B = A - A \<inter> B" by auto
haftmann@35722
  1302
  thus ?thesis
haftmann@35722
  1303
    by (simp add: card_Diff_subset AB) 
haftmann@35722
  1304
qed
haftmann@35722
  1305
nipkow@40716
  1306
lemma diff_card_le_card_Diff:
nipkow@40716
  1307
assumes "finite B" shows "card A - card B \<le> card(A - B)"
nipkow@40716
  1308
proof-
nipkow@40716
  1309
  have "card A - card B \<le> card A - card (A \<inter> B)"
nipkow@40716
  1310
    using card_mono[OF assms Int_lower2, of A] by arith
nipkow@40716
  1311
  also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
nipkow@40716
  1312
  finally show ?thesis .
nipkow@40716
  1313
qed
nipkow@40716
  1314
haftmann@35722
  1315
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
haftmann@35722
  1316
apply (rule Suc_less_SucD)
haftmann@35722
  1317
apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
haftmann@35722
  1318
done
haftmann@35722
  1319
haftmann@35722
  1320
lemma card_Diff2_less:
haftmann@35722
  1321
  "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
haftmann@35722
  1322
apply (case_tac "x = y")
haftmann@35722
  1323
 apply (simp add: card_Diff1_less del:card_Diff_insert)
haftmann@35722
  1324
apply (rule less_trans)
haftmann@35722
  1325
 prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
haftmann@35722
  1326
done
haftmann@35722
  1327
haftmann@35722
  1328
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
haftmann@35722
  1329
apply (case_tac "x : A")
haftmann@35722
  1330
 apply (simp_all add: card_Diff1_less less_imp_le)
haftmann@35722
  1331
done
haftmann@35722
  1332
haftmann@35722
  1333
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
haftmann@35722
  1334
by (erule psubsetI, blast)
haftmann@35722
  1335
haftmann@35722
  1336
lemma insert_partition:
haftmann@35722
  1337
  "\<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
  1338
  \<Longrightarrow> x \<inter> \<Union> F = {}"
haftmann@35722
  1339
by auto
haftmann@35722
  1340
haftmann@35722
  1341
lemma finite_psubset_induct[consumes 1, case_names psubset]:
urbanc@36079
  1342
  assumes fin: "finite A" 
urbanc@36079
  1343
  and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
urbanc@36079
  1344
  shows "P A"
urbanc@36079
  1345
using fin
urbanc@36079
  1346
proof (induct A taking: card rule: measure_induct_rule)
haftmann@35722
  1347
  case (less A)
urbanc@36079
  1348
  have fin: "finite A" by fact
urbanc@36079
  1349
  have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
urbanc@36079
  1350
  { fix B 
urbanc@36079
  1351
    assume asm: "B \<subset> A"
urbanc@36079
  1352
    from asm have "card B < card A" using psubset_card_mono fin by blast
urbanc@36079
  1353
    moreover
urbanc@36079
  1354
    from asm have "B \<subseteq> A" by auto
urbanc@36079
  1355
    then have "finite B" using fin finite_subset by blast
urbanc@36079
  1356
    ultimately 
urbanc@36079
  1357
    have "P B" using ih by simp
urbanc@36079
  1358
  }
urbanc@36079
  1359
  with fin show "P A" using major by blast
haftmann@35722
  1360
qed
haftmann@35722
  1361
haftmann@35722
  1362
text{* main cardinality theorem *}
haftmann@35722
  1363
lemma card_partition [rule_format]:
haftmann@35722
  1364
  "finite C ==>
haftmann@35722
  1365
     finite (\<Union> C) -->
haftmann@35722
  1366
     (\<forall>c\<in>C. card c = k) -->
haftmann@35722
  1367
     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
haftmann@35722
  1368
     k * card(C) = card (\<Union> C)"
haftmann@35722
  1369
apply (erule finite_induct, simp)
haftmann@35722
  1370
apply (simp add: card_Un_disjoint insert_partition 
haftmann@35722
  1371
       finite_subset [of _ "\<Union> (insert x F)"])
haftmann@35722
  1372
done
haftmann@35722
  1373
haftmann@35722
  1374
lemma card_eq_UNIV_imp_eq_UNIV:
haftmann@35722
  1375
  assumes fin: "finite (UNIV :: 'a set)"
haftmann@35722
  1376
  and card: "card A = card (UNIV :: 'a set)"
haftmann@35722
  1377
  shows "A = (UNIV :: 'a set)"
haftmann@35722
  1378
proof
haftmann@35722
  1379
  show "A \<subseteq> UNIV" by simp
haftmann@35722
  1380
  show "UNIV \<subseteq> A"
haftmann@35722
  1381
  proof
haftmann@35722
  1382
    fix x
haftmann@35722
  1383
    show "x \<in> A"
haftmann@35722
  1384
    proof (rule ccontr)
haftmann@35722
  1385
      assume "x \<notin> A"
haftmann@35722
  1386
      then have "A \<subset> UNIV" by auto
haftmann@35722
  1387
      with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
haftmann@35722
  1388
      with card show False by simp
haftmann@35722
  1389
    qed
haftmann@35722
  1390
  qed
haftmann@35722
  1391
qed
haftmann@35722
  1392
haftmann@35722
  1393
text{*The form of a finite set of given cardinality*}
haftmann@35722
  1394
haftmann@35722
  1395
lemma card_eq_SucD:
haftmann@35722
  1396
assumes "card A = Suc k"
haftmann@35722
  1397
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
haftmann@35722
  1398
proof -
haftmann@35722
  1399
  have fin: "finite A" using assms by (auto intro: ccontr)
haftmann@35722
  1400
  moreover have "card A \<noteq> 0" using assms by auto
haftmann@35722
  1401
  ultimately obtain b where b: "b \<in> A" by auto
haftmann@35722
  1402
  show ?thesis
haftmann@35722
  1403
  proof (intro exI conjI)
haftmann@35722
  1404
    show "A = insert b (A-{b})" using b by blast
haftmann@35722
  1405
    show "b \<notin> A - {b}" by blast
haftmann@35722
  1406
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
nipkow@44890
  1407
      using assms b fin by(fastforce dest:mk_disjoint_insert)+
haftmann@35722
  1408
  qed
haftmann@35722
  1409
qed
haftmann@35722
  1410
haftmann@35722
  1411
lemma card_Suc_eq:
haftmann@35722
  1412
  "(card A = Suc k) =
haftmann@35722
  1413
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
haftmann@35722
  1414
apply(rule iffI)
haftmann@35722
  1415
 apply(erule card_eq_SucD)
haftmann@35722
  1416
apply(auto)
haftmann@51489
  1417
apply(subst card.insert)
haftmann@35722
  1418
 apply(auto intro:ccontr)
haftmann@35722
  1419
done
haftmann@35722
  1420
nipkow@44744
  1421
lemma card_le_Suc_iff: "finite A \<Longrightarrow>
nipkow@44744
  1422
  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
  1423
by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
nipkow@44744
  1424
  dest: subset_singletonD split: nat.splits if_splits)
nipkow@44744
  1425
haftmann@35722
  1426
lemma finite_fun_UNIVD2:
haftmann@35722
  1427
  assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
haftmann@35722
  1428
  shows "finite (UNIV :: 'b set)"
haftmann@35722
  1429
proof -
haftmann@46146
  1430
  from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
haftmann@46146
  1431
    by (rule finite_imageI)
haftmann@46146
  1432
  moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
haftmann@46146
  1433
    by (rule UNIV_eq_I) auto
haftmann@35722
  1434
  ultimately show "finite (UNIV :: 'b set)" by simp
haftmann@35722
  1435
qed
haftmann@35722
  1436
huffman@48063
  1437
lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
haftmann@35722
  1438
  unfolding UNIV_unit by simp
haftmann@35722
  1439
huffman@47210
  1440
lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
huffman@47210
  1441
  unfolding UNIV_bool by simp
huffman@47210
  1442
haftmann@35722
  1443
haftmann@35722
  1444
subsubsection {* Cardinality of image *}
haftmann@35722
  1445
haftmann@35722
  1446
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
haftmann@41656
  1447
apply (induct rule: finite_induct)
haftmann@35722
  1448
 apply simp
haftmann@35722
  1449
apply (simp add: le_SucI card_insert_if)
haftmann@35722
  1450
done
haftmann@35722
  1451
haftmann@35722
  1452
lemma card_image:
haftmann@35722
  1453
  assumes "inj_on f A"
haftmann@35722
  1454
  shows "card (f ` A) = card A"
haftmann@35722
  1455
proof (cases "finite A")
haftmann@35722
  1456
  case True then show ?thesis using assms by (induct A) simp_all
haftmann@35722
  1457
next
haftmann@35722
  1458
  case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
haftmann@35722
  1459
  with False show ?thesis by simp
haftmann@35722
  1460
qed
haftmann@35722
  1461
haftmann@35722
  1462
lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
haftmann@35722
  1463
by(auto simp: card_image bij_betw_def)
haftmann@35722
  1464
haftmann@35722
  1465
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
haftmann@35722
  1466
by (simp add: card_seteq card_image)
haftmann@35722
  1467
haftmann@35722
  1468
lemma eq_card_imp_inj_on:
haftmann@35722
  1469
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
haftmann@35722
  1470
apply (induct rule:finite_induct)
haftmann@35722
  1471
apply simp
haftmann@35722
  1472
apply(frule card_image_le[where f = f])
haftmann@35722
  1473
apply(simp add:card_insert_if split:if_splits)
haftmann@35722
  1474
done
haftmann@35722
  1475
haftmann@35722
  1476
lemma inj_on_iff_eq_card:
haftmann@35722
  1477
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
haftmann@35722
  1478
by(blast intro: card_image eq_card_imp_inj_on)
haftmann@35722
  1479
haftmann@35722
  1480
haftmann@35722
  1481
lemma card_inj_on_le:
haftmann@35722
  1482
  "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
haftmann@35722
  1483
apply (subgoal_tac "finite A") 
haftmann@35722
  1484
 apply (force intro: card_mono simp add: card_image [symmetric])
haftmann@35722
  1485
apply (blast intro: finite_imageD dest: finite_subset) 
haftmann@35722
  1486
done
haftmann@35722
  1487
haftmann@35722
  1488
lemma card_bij_eq:
haftmann@35722
  1489
  "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
haftmann@35722
  1490
     finite A; finite B |] ==> card A = card B"
haftmann@35722
  1491
by (auto intro: le_antisym card_inj_on_le)
haftmann@35722
  1492
hoelzl@40703
  1493
lemma bij_betw_finite:
hoelzl@40703
  1494
  assumes "bij_betw f A B"
hoelzl@40703
  1495
  shows "finite A \<longleftrightarrow> finite B"
hoelzl@40703
  1496
using assms unfolding bij_betw_def
hoelzl@40703
  1497
using finite_imageD[of f A] by auto
haftmann@35722
  1498
haftmann@41656
  1499
nipkow@37466
  1500
subsubsection {* Pigeonhole Principles *}
nipkow@37466
  1501
nipkow@40311
  1502
lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
nipkow@37466
  1503
by (auto dest: card_image less_irrefl_nat)
nipkow@37466
  1504
nipkow@37466
  1505
lemma pigeonhole_infinite:
nipkow@37466
  1506
assumes  "~ finite A" and "finite(f`A)"
nipkow@37466
  1507
shows "EX a0:A. ~finite{a:A. f a = f a0}"
nipkow@37466
  1508
proof -
nipkow@37466
  1509
  have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
nipkow@37466
  1510
  proof(induct "f`A" arbitrary: A rule: finite_induct)
nipkow@37466
  1511
    case empty thus ?case by simp
nipkow@37466
  1512
  next
nipkow@37466
  1513
    case (insert b F)
nipkow@37466
  1514
    show ?case
nipkow@37466
  1515
    proof cases
nipkow@37466
  1516
      assume "finite{a:A. f a = b}"
nipkow@37466
  1517
      hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
nipkow@37466
  1518
      also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
nipkow@37466
  1519
      finally have "~ finite({a:A. f a \<noteq> b})" .
nipkow@37466
  1520
      from insert(3)[OF _ this]
nipkow@37466
  1521
      show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
nipkow@37466
  1522
    next
nipkow@37466
  1523
      assume 1: "~finite{a:A. f a = b}"
nipkow@37466
  1524
      hence "{a \<in> A. f a = b} \<noteq> {}" by force
nipkow@37466
  1525
      thus ?thesis using 1 by blast
nipkow@37466
  1526
    qed
nipkow@37466
  1527
  qed
nipkow@37466
  1528
  from this[OF assms(2,1)] show ?thesis .
nipkow@37466
  1529
qed
nipkow@37466
  1530
nipkow@37466
  1531
lemma pigeonhole_infinite_rel:
nipkow@37466
  1532
assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
nipkow@37466
  1533
shows "EX b:B. ~finite{a:A. R a b}"
nipkow@37466
  1534
proof -
nipkow@37466
  1535
   let ?F = "%a. {b:B. R a b}"
nipkow@37466
  1536
   from finite_Pow_iff[THEN iffD2, OF `finite B`]
nipkow@37466
  1537
   have "finite(?F ` A)" by(blast intro: rev_finite_subset)
nipkow@37466
  1538
   from pigeonhole_infinite[where f = ?F, OF assms(1) this]
nipkow@37466
  1539
   obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
nipkow@37466
  1540
   obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
nipkow@37466
  1541
   { assume "finite{a:A. R a b0}"
nipkow@37466
  1542
     then have "finite {a\<in>A. ?F a = ?F a0}"
nipkow@37466
  1543
       using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
nipkow@37466
  1544
   }
nipkow@37466
  1545
   with 1 `b0 : B` show ?thesis by blast
nipkow@37466
  1546
qed
nipkow@37466
  1547
nipkow@37466
  1548
haftmann@35722
  1549
subsubsection {* Cardinality of sums *}
haftmann@35722
  1550
haftmann@35722
  1551
lemma card_Plus:
haftmann@35722
  1552
  assumes "finite A" and "finite B"
haftmann@35722
  1553
  shows "card (A <+> B) = card A + card B"
haftmann@35722
  1554
proof -
haftmann@35722
  1555
  have "Inl`A \<inter> Inr`B = {}" by fast
haftmann@35722
  1556
  with assms show ?thesis
haftmann@35722
  1557
    unfolding Plus_def
haftmann@35722
  1558
    by (simp add: card_Un_disjoint card_image)
haftmann@35722
  1559
qed
haftmann@35722
  1560
haftmann@35722
  1561
lemma card_Plus_conv_if:
haftmann@35722
  1562
  "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
haftmann@35722
  1563
  by (auto simp add: card_Plus)
haftmann@35722
  1564
haftmann@35722
  1565
haftmann@35722
  1566
subsubsection {* Cardinality of the Powerset *}
haftmann@35722
  1567
huffman@47221
  1568
lemma card_Pow: "finite A ==> card (Pow A) = 2 ^ card A"
haftmann@41656
  1569
apply (induct rule: finite_induct)
haftmann@35722
  1570
 apply (simp_all add: Pow_insert)
haftmann@35722
  1571
apply (subst card_Un_disjoint, blast)
nipkow@40786
  1572
  apply (blast, blast)
haftmann@35722
  1573
apply (subgoal_tac "inj_on (insert x) (Pow F)")
huffman@47221
  1574
 apply (subst mult_2)
haftmann@35722
  1575
 apply (simp add: card_image Pow_insert)
haftmann@35722
  1576
apply (unfold inj_on_def)
haftmann@35722
  1577
apply (blast elim!: equalityE)
haftmann@35722
  1578
done
haftmann@35722
  1579
nipkow@41987
  1580
text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
haftmann@35722
  1581
haftmann@35722
  1582
lemma dvd_partition:
haftmann@35722
  1583
  "finite (Union C) ==>
haftmann@35722
  1584
    ALL c : C. k dvd card c ==>
haftmann@35722
  1585
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
haftmann@35722
  1586
  k dvd card (Union C)"
haftmann@41656
  1587
apply (frule finite_UnionD)
haftmann@41656
  1588
apply (rotate_tac -1)
haftmann@41656
  1589
apply (induct rule: finite_induct)
haftmann@41656
  1590
apply simp_all
haftmann@41656
  1591
apply clarify
haftmann@35722
  1592
apply (subst card_Un_disjoint)
haftmann@35722
  1593
   apply (auto simp add: disjoint_eq_subset_Compl)
haftmann@35722
  1594
done
haftmann@35722
  1595
haftmann@35722
  1596
haftmann@35722
  1597
subsubsection {* Relating injectivity and surjectivity *}
haftmann@35722
  1598
haftmann@41656
  1599
lemma finite_surj_inj: "finite A \<Longrightarrow> A \<subseteq> f ` A \<Longrightarrow> inj_on f A"
haftmann@35722
  1600
apply(rule eq_card_imp_inj_on, assumption)
haftmann@35722
  1601
apply(frule finite_imageI)
haftmann@35722
  1602
apply(drule (1) card_seteq)
haftmann@35722
  1603
 apply(erule card_image_le)
haftmann@35722
  1604
apply simp
haftmann@35722
  1605
done
haftmann@35722
  1606
haftmann@35722
  1607
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  1608
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
hoelzl@40702
  1609
by (blast intro: finite_surj_inj subset_UNIV)
haftmann@35722
  1610
haftmann@35722
  1611
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  1612
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
nipkow@44890
  1613
by(fastforce simp:surj_def dest!: endo_inj_surj)
haftmann@35722
  1614
haftmann@51489
  1615
corollary infinite_UNIV_nat [iff]:
haftmann@51489
  1616
  "\<not> finite (UNIV :: nat set)"
haftmann@35722
  1617
proof
haftmann@51489
  1618
  assume "finite (UNIV :: nat set)"
haftmann@51489
  1619
  with finite_UNIV_inj_surj [of Suc]
haftmann@35722
  1620
  show False by simp (blast dest: Suc_neq_Zero surjD)
haftmann@35722
  1621
qed
haftmann@35722
  1622
blanchet@54147
  1623
lemma infinite_UNIV_char_0:
haftmann@51489
  1624
  "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
haftmann@35722
  1625
proof
haftmann@51489
  1626
  assume "finite (UNIV :: 'a set)"
haftmann@51489
  1627
  with subset_UNIV have "finite (range of_nat :: 'a set)"
haftmann@35722
  1628
    by (rule finite_subset)
haftmann@51489
  1629
  moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
haftmann@35722
  1630
    by (simp add: inj_on_def)
haftmann@51489
  1631
  ultimately have "finite (UNIV :: nat set)"
haftmann@35722
  1632
    by (rule finite_imageD)
haftmann@51489
  1633
  then show False
haftmann@35722
  1634
    by simp
haftmann@35722
  1635
qed
haftmann@35722
  1636
kuncar@49758
  1637
hide_const (open) Finite_Set.fold
haftmann@46033
  1638
haftmann@35722
  1639
end
haftmann@49723
  1640