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