| author | paulson <lp15@cam.ac.uk> | 
| Wed, 04 Jul 2018 11:00:06 +0100 | |
| changeset 68586 | 006da53a8ac1 | 
| parent 68521 | 1bad08165162 | 
| child 68975 | 5ce4d117cea7 | 
| permissions | -rw-r--r-- | 
| 12396 | 1 | (* Title: HOL/Finite_Set.thy | 
| 63612 | 2 | Author: Tobias Nipkow | 
| 3 | Author: Lawrence C Paulson | |
| 4 | Author: Markus Wenzel | |
| 5 | Author: Jeremy Avigad | |
| 6 | Author: Andrei Popescu | |
| 12396 | 7 | *) | 
| 8 | ||
| 60758 | 9 | section \<open>Finite sets\<close> | 
| 12396 | 10 | |
| 15131 | 11 | theory Finite_Set | 
| 63612 | 12 | imports Product_Type Sum_Type Fields | 
| 15131 | 13 | begin | 
| 12396 | 14 | |
| 60758 | 15 | subsection \<open>Predicate for finite sets\<close> | 
| 12396 | 16 | |
| 63612 | 17 | context notes [[inductive_internals]] | 
| 61681 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 18 | begin | 
| 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 19 | |
| 41656 | 20 | inductive finite :: "'a set \<Rightarrow> bool" | 
| 63612 | 21 | where | 
| 22 |     emptyI [simp, intro!]: "finite {}"
 | |
| 23 | | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)" | |
| 41656 | 24 | |
| 61681 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 25 | end | 
| 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 26 | |
| 60758 | 27 | simproc_setup finite_Collect ("finite (Collect P)") = \<open>K Set_Comprehension_Pointfree.simproc\<close>
 | 
| 48109 
0a58f7eefba2
Integrated set comprehension pointfree simproc.
 Rafal Kolanski <rafal.kolanski@nicta.com.au> parents: 
48063diff
changeset | 28 | |
| 54611 
31afce809794
set_comprehension_pointfree simproc causes to many surprises if enabled by default
 traytel parents: 
54570diff
changeset | 29 | declare [[simproc del: finite_Collect]] | 
| 
31afce809794
set_comprehension_pointfree simproc causes to many surprises if enabled by default
 traytel parents: 
54570diff
changeset | 30 | |
| 41656 | 31 | lemma finite_induct [case_names empty insert, induct set: finite]: | 
| 61799 | 32 | \<comment> \<open>Discharging \<open>x \<notin> F\<close> entails extra work.\<close> | 
| 41656 | 33 | assumes "finite F" | 
| 34 |   assumes "P {}"
 | |
| 35 | and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)" | |
| 36 | shows "P F" | |
| 63404 | 37 | using \<open>finite F\<close> | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 38 | proof induct | 
| 41656 | 39 |   show "P {}" by fact
 | 
| 63404 | 40 | next | 
| 41 | fix x F | |
| 42 | assume F: "finite F" and P: "P F" | |
| 41656 | 43 | show "P (insert x F)" | 
| 44 | proof cases | |
| 45 | assume "x \<in> F" | |
| 63404 | 46 | then have "insert x F = F" by (rule insert_absorb) | 
| 41656 | 47 | with P show ?thesis by (simp only:) | 
| 48 | next | |
| 49 | assume "x \<notin> F" | |
| 50 | from F this P show ?thesis by (rule insert) | |
| 51 | qed | |
| 52 | qed | |
| 53 | ||
| 51622 | 54 | lemma infinite_finite_induct [case_names infinite empty insert]: | 
| 55 | assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A" | |
| 63404 | 56 |     and empty: "P {}"
 | 
| 57 | and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)" | |
| 51622 | 58 | shows "P A" | 
| 59 | proof (cases "finite A") | |
| 63404 | 60 | case False | 
| 61 | with infinite show ?thesis . | |
| 51622 | 62 | next | 
| 63404 | 63 | case True | 
| 64 | then show ?thesis by (induct A) (fact empty insert)+ | |
| 51622 | 65 | qed | 
| 66 | ||
| 41656 | 67 | |
| 60758 | 68 | subsubsection \<open>Choice principles\<close> | 
| 12396 | 69 | |
| 67443 
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
 wenzelm parents: 
63982diff
changeset | 70 | lemma ex_new_if_finite: \<comment> \<open>does not depend on def of finite at all\<close> | 
| 14661 | 71 | assumes "\<not> finite (UNIV :: 'a set)" and "finite A" | 
| 72 | shows "\<exists>a::'a. a \<notin> A" | |
| 73 | proof - | |
| 28823 | 74 | from assms have "A \<noteq> UNIV" by blast | 
| 41656 | 75 | then show ?thesis by blast | 
| 12396 | 76 | qed | 
| 77 | ||
| 60758 | 78 | text \<open>A finite choice principle. Does not need the SOME choice operator.\<close> | 
| 15484 | 79 | |
| 63404 | 80 | lemma finite_set_choice: "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)" | 
| 41656 | 81 | proof (induct rule: finite_induct) | 
| 63404 | 82 | case empty | 
| 83 | then show ?case by simp | |
| 29923 | 84 | next | 
| 85 | case (insert a A) | |
| 63404 | 86 | then obtain f b where f: "\<forall>x\<in>A. P x (f x)" and ab: "P a b" | 
| 87 | by auto | |
| 88 | show ?case (is "\<exists>f. ?P f") | |
| 29923 | 89 | proof | 
| 63404 | 90 | show "?P (\<lambda>x. if x = a then b else f x)" | 
| 91 | using f ab by auto | |
| 29923 | 92 | qed | 
| 93 | qed | |
| 94 | ||
| 23878 | 95 | |
| 60758 | 96 | subsubsection \<open>Finite sets are the images of initial segments of natural numbers\<close> | 
| 15392 | 97 | |
| 15510 | 98 | lemma finite_imp_nat_seg_image_inj_on: | 
| 63404 | 99 | assumes "finite A" | 
| 41656 | 100 |   shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
 | 
| 63404 | 101 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 102 | proof induct | 
| 15392 | 103 | case empty | 
| 41656 | 104 | show ?case | 
| 105 | proof | |
| 63404 | 106 |     show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}"
 | 
| 107 | by simp | |
| 15510 | 108 | qed | 
| 15392 | 109 | next | 
| 110 | case (insert a A) | |
| 23389 | 111 | have notinA: "a \<notin> A" by fact | 
| 63404 | 112 |   from insert.hyps obtain n f where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}"
 | 
| 113 | by blast | |
| 114 |   then have "insert a A = f(n:=a) ` {i. i < Suc n}" and "inj_on (f(n:=a)) {i. i < Suc n}"
 | |
| 115 | using notinA by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq) | |
| 116 | then show ?case by blast | |
| 15392 | 117 | qed | 
| 118 | ||
| 63404 | 119 | lemma nat_seg_image_imp_finite: "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
 | 
| 41656 | 120 | proof (induct n arbitrary: A) | 
| 63404 | 121 | case 0 | 
| 122 | then show ?case by simp | |
| 15392 | 123 | next | 
| 124 | case (Suc n) | |
| 125 |   let ?B = "f ` {i. i < n}"
 | |
| 63404 | 126 | have finB: "finite ?B" by (rule Suc.hyps[OF refl]) | 
| 15392 | 127 | show ?case | 
| 63404 | 128 | proof (cases "\<exists>k<n. f n = f k") | 
| 129 | case True | |
| 130 | then have "A = ?B" | |
| 131 | using Suc.prems by (auto simp:less_Suc_eq) | |
| 132 | then show ?thesis | |
| 133 | using finB by simp | |
| 15392 | 134 | next | 
| 63404 | 135 | case False | 
| 136 | then have "A = insert (f n) ?B" | |
| 137 | using Suc.prems by (auto simp:less_Suc_eq) | |
| 138 | then show ?thesis using finB by simp | |
| 15392 | 139 | qed | 
| 140 | qed | |
| 141 | ||
| 63982 | 142 | lemma finite_conv_nat_seg_image: "finite A \<longleftrightarrow> (\<exists>n f. A = f ` {i::nat. i < n})"
 | 
| 41656 | 143 | by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on) | 
| 15392 | 144 | |
| 32988 | 145 | lemma finite_imp_inj_to_nat_seg: | 
| 41656 | 146 | assumes "finite A" | 
| 63982 | 147 |   shows "\<exists>f n. f ` A = {i::nat. i < n} \<and> inj_on f A"
 | 
| 32988 | 148 | proof - | 
| 63404 | 149 | from finite_imp_nat_seg_image_inj_on [OF \<open>finite A\<close>] | 
| 63612 | 150 |   obtain f and n :: nat where bij: "bij_betw f {i. i<n} A"
 | 
| 63404 | 151 | by (auto simp: bij_betw_def) | 
| 33057 | 152 |   let ?f = "the_inv_into {i. i<n} f"
 | 
| 63404 | 153 |   have "inj_on ?f A \<and> ?f ` A = {i. i<n}"
 | 
| 33057 | 154 | by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij]) | 
| 63404 | 155 | then show ?thesis by blast | 
| 32988 | 156 | qed | 
| 157 | ||
| 63404 | 158 | lemma finite_Collect_less_nat [iff]: "finite {n::nat. n < k}"
 | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44835diff
changeset | 159 | by (fastforce simp: finite_conv_nat_seg_image) | 
| 29920 | 160 | |
| 63404 | 161 | lemma finite_Collect_le_nat [iff]: "finite {n::nat. n \<le> k}"
 | 
| 41656 | 162 | by (simp add: le_eq_less_or_eq Collect_disj_eq) | 
| 15392 | 163 | |
| 41656 | 164 | |
| 60758 | 165 | subsubsection \<open>Finiteness and common set operations\<close> | 
| 12396 | 166 | |
| 63404 | 167 | lemma rev_finite_subset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A" | 
| 41656 | 168 | proof (induct arbitrary: A rule: finite_induct) | 
| 169 | case empty | |
| 170 | then show ?case by simp | |
| 171 | next | |
| 172 | case (insert x F A) | |
| 63404 | 173 |   have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})"
 | 
| 174 | by fact+ | |
| 41656 | 175 | show "finite A" | 
| 176 | proof cases | |
| 177 | assume x: "x \<in> A" | |
| 178 |     with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
 | |
| 179 |     with r have "finite (A - {x})" .
 | |
| 63404 | 180 |     then have "finite (insert x (A - {x}))" ..
 | 
| 181 |     also have "insert x (A - {x}) = A"
 | |
| 182 | using x by (rule insert_Diff) | |
| 41656 | 183 | finally show ?thesis . | 
| 12396 | 184 | next | 
| 60595 | 185 | show ?thesis when "A \<subseteq> F" | 
| 186 | using that by fact | |
| 41656 | 187 | assume "x \<notin> A" | 
| 63404 | 188 | with A show "A \<subseteq> F" | 
| 189 | by (simp add: subset_insert_iff) | |
| 12396 | 190 | qed | 
| 191 | qed | |
| 192 | ||
| 63404 | 193 | lemma finite_subset: "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A" | 
| 41656 | 194 | by (rule rev_finite_subset) | 
| 29901 | 195 | |
| 41656 | 196 | lemma finite_UnI: | 
| 197 | assumes "finite F" and "finite G" | |
| 198 | shows "finite (F \<union> G)" | |
| 199 | using assms by induct simp_all | |
| 31992 | 200 | |
| 63404 | 201 | lemma finite_Un [iff]: "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G" | 
| 41656 | 202 | by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"]) | 
| 31992 | 203 | |
| 41656 | 204 | lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A" | 
| 12396 | 205 | proof - | 
| 41656 | 206 |   have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
 | 
| 207 |   then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
 | |
| 23389 | 208 | then show ?thesis by simp | 
| 12396 | 209 | qed | 
| 210 | ||
| 63404 | 211 | lemma finite_Int [simp, intro]: "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)" | 
| 41656 | 212 | by (blast intro: finite_subset) | 
| 213 | ||
| 214 | lemma finite_Collect_conjI [simp, intro]: | |
| 215 |   "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
 | |
| 216 | by (simp add: Collect_conj_eq) | |
| 217 | ||
| 218 | lemma finite_Collect_disjI [simp]: | |
| 219 |   "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
 | |
| 220 | by (simp add: Collect_disj_eq) | |
| 221 | ||
| 63404 | 222 | lemma finite_Diff [simp, intro]: "finite A \<Longrightarrow> finite (A - B)" | 
| 41656 | 223 | by (rule finite_subset, rule Diff_subset) | 
| 29901 | 224 | |
| 225 | lemma finite_Diff2 [simp]: | |
| 41656 | 226 | assumes "finite B" | 
| 227 | shows "finite (A - B) \<longleftrightarrow> finite A" | |
| 29901 | 228 | proof - | 
| 63404 | 229 | have "finite A \<longleftrightarrow> finite ((A - B) \<union> (A \<inter> B))" | 
| 230 | by (simp add: Un_Diff_Int) | |
| 231 | also have "\<dots> \<longleftrightarrow> finite (A - B)" | |
| 232 | using \<open>finite B\<close> by simp | |
| 29901 | 233 | finally show ?thesis .. | 
| 234 | qed | |
| 235 | ||
| 63404 | 236 | lemma finite_Diff_insert [iff]: "finite (A - insert a B) \<longleftrightarrow> finite (A - B)" | 
| 41656 | 237 | proof - | 
| 238 |   have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
 | |
| 239 |   moreover have "A - insert a B = A - B - {a}" by auto
 | |
| 240 | ultimately show ?thesis by simp | |
| 241 | qed | |
| 242 | ||
| 63404 | 243 | lemma finite_compl [simp]: | 
| 41656 | 244 | "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)" | 
| 245 | by (simp add: Compl_eq_Diff_UNIV) | |
| 12396 | 246 | |
| 63404 | 247 | lemma finite_Collect_not [simp]: | 
| 41656 | 248 |   "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
 | 
| 249 | by (simp add: Collect_neg_eq) | |
| 250 | ||
| 251 | lemma finite_Union [simp, intro]: | |
| 63404 | 252 | "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite (\<Union>A)" | 
| 41656 | 253 | by (induct rule: finite_induct) simp_all | 
| 254 | ||
| 255 | lemma finite_UN_I [intro]: | |
| 256 | "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)" | |
| 257 | by (induct rule: finite_induct) simp_all | |
| 29903 | 258 | |
| 63404 | 259 | lemma finite_UN [simp]: "finite A \<Longrightarrow> finite (UNION A B) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))" | 
| 41656 | 260 | by (blast intro: finite_subset) | 
| 261 | ||
| 63404 | 262 | lemma finite_Inter [intro]: "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)" | 
| 41656 | 263 | by (blast intro: Inter_lower finite_subset) | 
| 12396 | 264 | |
| 63404 | 265 | lemma finite_INT [intro]: "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)" | 
| 41656 | 266 | by (blast intro: INT_lower finite_subset) | 
| 13825 | 267 | |
| 63404 | 268 | lemma finite_imageI [simp, intro]: "finite F \<Longrightarrow> finite (h ` F)" | 
| 41656 | 269 | by (induct rule: finite_induct) simp_all | 
| 13825 | 270 | |
| 63404 | 271 | lemma finite_image_set [simp]: "finite {x. P x} \<Longrightarrow> finite {f x |x. P x}"
 | 
| 31768 | 272 | by (simp add: image_Collect [symmetric]) | 
| 273 | ||
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 274 | lemma finite_image_set2: | 
| 63404 | 275 |   "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {f x y |x y. P x \<and> Q y}"
 | 
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 276 |   by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {f x y}"]) auto
 | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 277 | |
| 41656 | 278 | lemma finite_imageD: | 
| 42206 | 279 | assumes "finite (f ` A)" and "inj_on f A" | 
| 280 | shows "finite A" | |
| 63404 | 281 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 282 | proof (induct "f ` A" arbitrary: A) | 
| 63404 | 283 | case empty | 
| 284 | then show ?case by simp | |
| 42206 | 285 | next | 
| 286 | case (insert x B) | |
| 63404 | 287 | then have B_A: "insert x B = f ` A" | 
| 288 | by simp | |
| 289 | then obtain y where "x = f y" and "y \<in> A" | |
| 290 | by blast | |
| 291 |   from B_A \<open>x \<notin> B\<close> have "B = f ` A - {x}"
 | |
| 292 | by blast | |
| 293 |   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})"
 | |
| 60303 | 294 | by (simp add: inj_on_image_set_diff Set.Diff_subset) | 
| 63404 | 295 |   moreover from \<open>inj_on f A\<close> have "inj_on f (A - {y})"
 | 
| 296 | by (rule inj_on_diff) | |
| 297 |   ultimately have "finite (A - {y})"
 | |
| 298 | by (rule insert.hyps) | |
| 299 | then show "finite A" | |
| 300 | by simp | |
| 42206 | 301 | qed | 
| 12396 | 302 | |
| 63404 | 303 | lemma finite_image_iff: "inj_on f A \<Longrightarrow> finite (f ` A) \<longleftrightarrow> finite A" | 
| 304 | using finite_imageD by blast | |
| 62618 
f7f2467ab854
Refactoring (moving theorems into better locations), plus a bit of new material
 paulson <lp15@cam.ac.uk> parents: 
62481diff
changeset | 305 | |
| 63404 | 306 | lemma finite_surj: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B" | 
| 41656 | 307 | by (erule finite_subset) (rule finite_imageI) | 
| 12396 | 308 | |
| 63404 | 309 | lemma finite_range_imageI: "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))" | 
| 41656 | 310 | by (drule finite_imageI) (simp add: range_composition) | 
| 13825 | 311 | |
| 41656 | 312 | lemma finite_subset_image: | 
| 313 | assumes "finite B" | |
| 314 | shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C" | |
| 63404 | 315 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 316 | proof induct | 
| 63404 | 317 | case empty | 
| 318 | then show ?case by simp | |
| 41656 | 319 | next | 
| 63404 | 320 | case insert | 
| 321 | then show ?case | |
| 63612 | 322 | by (clarsimp simp del: image_insert simp add: image_insert [symmetric]) blast (* slow *) | 
| 41656 | 323 | qed | 
| 324 | ||
| 63404 | 325 | lemma finite_vimage_IntI: "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)" | 
| 41656 | 326 | apply (induct rule: finite_induct) | 
| 21575 | 327 | apply simp_all | 
| 14430 
5cb24165a2e1
new material from Avigad, and simplified treatment of division by 0
 paulson parents: 
14331diff
changeset | 328 | apply (subst vimage_insert) | 
| 43991 | 329 | apply (simp add: finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2) | 
| 13825 | 330 | done | 
| 331 | ||
| 61762 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 332 | lemma finite_finite_vimage_IntI: | 
| 63612 | 333 | assumes "finite F" | 
| 334 |     and "\<And>y. y \<in> F \<Longrightarrow> finite ((h -` {y}) \<inter> A)"
 | |
| 61762 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 335 | shows "finite (h -` F \<inter> A)" | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 336 | proof - | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 337 |   have *: "h -` F \<inter> A = (\<Union> y\<in>F. (h -` {y}) \<inter> A)"
 | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 338 | by blast | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 339 | show ?thesis | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 340 | by (simp only: * assms finite_UN_I) | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 341 | qed | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 342 | |
| 63404 | 343 | lemma finite_vimageI: "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)" | 
| 43991 | 344 | using finite_vimage_IntI[of F h UNIV] by auto | 
| 345 | ||
| 63404 | 346 | lemma finite_vimageD': "finite (f -` A) \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> finite A" | 
| 347 | by (auto simp add: subset_image_iff intro: finite_subset[rotated]) | |
| 59519 
2fb0c0fc62a3
add more general version of finite_vimageD
 Andreas Lochbihler parents: 
59504diff
changeset | 348 | |
| 63404 | 349 | lemma finite_vimageD: "finite (h -` F) \<Longrightarrow> surj h \<Longrightarrow> finite F" | 
| 350 | by (auto dest: finite_vimageD') | |
| 34111 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 351 | |
| 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 352 | lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F" | 
| 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 353 | unfolding bij_def by (auto elim: finite_vimageD finite_vimageI) | 
| 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 354 | |
| 41656 | 355 | lemma finite_Collect_bex [simp]: | 
| 356 | assumes "finite A" | |
| 357 |   shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
 | |
| 358 | proof - | |
| 359 |   have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
 | |
| 360 | with assms show ?thesis by simp | |
| 361 | qed | |
| 12396 | 362 | |
| 41656 | 363 | lemma finite_Collect_bounded_ex [simp]: | 
| 364 |   assumes "finite {y. P y}"
 | |
| 365 |   shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
 | |
| 366 | proof - | |
| 63404 | 367 |   have "{x. \<exists>y. P y \<and> Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})"
 | 
| 368 | by auto | |
| 369 | with assms show ?thesis | |
| 370 | by simp | |
| 41656 | 371 | qed | 
| 29920 | 372 | |
| 63404 | 373 | lemma finite_Plus: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)" | 
| 41656 | 374 | by (simp add: Plus_def) | 
| 17022 | 375 | |
| 63404 | 376 | lemma finite_PlusD: | 
| 31080 | 377 | fixes A :: "'a set" and B :: "'b set" | 
| 378 | assumes fin: "finite (A <+> B)" | |
| 379 | shows "finite A" "finite B" | |
| 380 | proof - | |
| 63404 | 381 | have "Inl ` A \<subseteq> A <+> B" | 
| 382 | by auto | |
| 383 |   then have "finite (Inl ` A :: ('a + 'b) set)"
 | |
| 384 | using fin by (rule finite_subset) | |
| 385 | then show "finite A" | |
| 386 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 387 | next | 
| 63404 | 388 | have "Inr ` B \<subseteq> A <+> B" | 
| 389 | by auto | |
| 390 |   then have "finite (Inr ` B :: ('a + 'b) set)"
 | |
| 391 | using fin by (rule finite_subset) | |
| 392 | then show "finite B" | |
| 393 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 394 | qed | 
| 395 | ||
| 63404 | 396 | lemma finite_Plus_iff [simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B" | 
| 41656 | 397 | by (auto intro: finite_PlusD finite_Plus) | 
| 31080 | 398 | |
| 41656 | 399 | lemma finite_Plus_UNIV_iff [simp]: | 
| 400 |   "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | |
| 401 | by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff) | |
| 12396 | 402 | |
| 40786 
0a54cfc9add3
gave more standard finite set rules simp and intro attribute
 nipkow parents: 
40716diff
changeset | 403 | lemma finite_SigmaI [simp, intro]: | 
| 63404 | 404 | "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (SIGMA a:A. B a)" | 
| 405 | unfolding Sigma_def by blast | |
| 12396 | 406 | |
| 51290 | 407 | lemma finite_SigmaI2: | 
| 408 |   assumes "finite {x\<in>A. B x \<noteq> {}}"
 | |
| 409 | and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)" | |
| 410 | shows "finite (Sigma A B)" | |
| 411 | proof - | |
| 63404 | 412 |   from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)"
 | 
| 413 | by auto | |
| 414 |   also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B"
 | |
| 415 | by auto | |
| 51290 | 416 | finally show ?thesis . | 
| 417 | qed | |
| 418 | ||
| 63404 | 419 | lemma finite_cartesian_product: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)" | 
| 15402 | 420 | by (rule finite_SigmaI) | 
| 421 | ||
| 12396 | 422 | lemma finite_Prod_UNIV: | 
| 41656 | 423 |   "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
 | 
| 424 | by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product) | |
| 12396 | 425 | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 426 | lemma finite_cartesian_productD1: | 
| 42207 | 427 |   assumes "finite (A \<times> B)" and "B \<noteq> {}"
 | 
| 428 | shows "finite A" | |
| 429 | proof - | |
| 430 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 431 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 432 |   then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}"
 | 
| 433 | by simp | |
| 60758 | 434 |   with \<open>B \<noteq> {}\<close> have "A = (fst \<circ> f) ` {i::nat. i < n}"
 | 
| 56154 
f0a927235162
more complete set of lemmas wrt. image and composition
 haftmann parents: 
55096diff
changeset | 435 | by (simp add: image_comp) | 
| 63404 | 436 |   then have "\<exists>n f. A = f ` {i::nat. i < n}"
 | 
| 437 | by blast | |
| 42207 | 438 | then show ?thesis | 
| 439 | by (auto simp add: finite_conv_nat_seg_image) | |
| 440 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 441 | |
| 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 442 | lemma finite_cartesian_productD2: | 
| 42207 | 443 |   assumes "finite (A \<times> B)" and "A \<noteq> {}"
 | 
| 444 | shows "finite B" | |
| 445 | proof - | |
| 446 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 447 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 448 |   then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}"
 | 
| 449 | by simp | |
| 60758 | 450 |   with \<open>A \<noteq> {}\<close> have "B = (snd \<circ> f) ` {i::nat. i < n}"
 | 
| 56154 
f0a927235162
more complete set of lemmas wrt. image and composition
 haftmann parents: 
55096diff
changeset | 451 | by (simp add: image_comp) | 
| 63404 | 452 |   then have "\<exists>n f. B = f ` {i::nat. i < n}"
 | 
| 453 | by blast | |
| 42207 | 454 | then show ?thesis | 
| 455 | by (auto simp add: finite_conv_nat_seg_image) | |
| 456 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 457 | |
| 57025 | 458 | lemma finite_cartesian_product_iff: | 
| 459 |   "finite (A \<times> B) \<longleftrightarrow> (A = {} \<or> B = {} \<or> (finite A \<and> finite B))"
 | |
| 460 | by (auto dest: finite_cartesian_productD1 finite_cartesian_productD2 finite_cartesian_product) | |
| 461 | ||
| 63404 | 462 | lemma finite_prod: | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 463 |   "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | 
| 57025 | 464 | using finite_cartesian_product_iff[of UNIV UNIV] by simp | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 465 | |
| 63404 | 466 | lemma finite_Pow_iff [iff]: "finite (Pow A) \<longleftrightarrow> finite A" | 
| 12396 | 467 | proof | 
| 468 | assume "finite (Pow A)" | |
| 63404 | 469 |   then have "finite ((\<lambda>x. {x}) ` A)"
 | 
| 63612 | 470 | by (blast intro: finite_subset) (* somewhat slow *) | 
| 63404 | 471 | then show "finite A" | 
| 472 | by (rule finite_imageD [unfolded inj_on_def]) simp | |
| 12396 | 473 | next | 
| 474 | assume "finite A" | |
| 41656 | 475 | then show "finite (Pow A)" | 
| 35216 | 476 | by induct (simp_all add: Pow_insert) | 
| 12396 | 477 | qed | 
| 478 | ||
| 63404 | 479 | corollary finite_Collect_subsets [simp, intro]: "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
 | 
| 41656 | 480 | by (simp add: Pow_def [symmetric]) | 
| 29918 | 481 | |
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 482 | lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)" | 
| 63404 | 483 | by (simp only: finite_Pow_iff Pow_UNIV[symmetric]) | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 484 | |
| 63404 | 485 | lemma finite_UnionD: "finite (\<Union>A) \<Longrightarrow> finite A" | 
| 41656 | 486 | by (blast intro: finite_subset [OF subset_Pow_Union]) | 
| 15392 | 487 | |
| 67511 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 488 | lemma finite_bind: | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 489 | assumes "finite S" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 490 | assumes "\<forall>x \<in> S. finite (f x)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 491 | shows "finite (Set.bind S f)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 492 | using assms by (simp add: bind_UNION) | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 493 | |
| 68463 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 494 | lemma finite_filter [simp]: "finite S \<Longrightarrow> finite (Set.filter P S)" | 
| 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 495 | unfolding Set.filter_def by simp | 
| 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 496 | |
| 63404 | 497 | lemma finite_set_of_finite_funs: | 
| 498 | assumes "finite A" "finite B" | |
| 499 |   shows "finite {f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
 | |
| 500 | proof - | |
| 53820 | 501 |   let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
 | 
| 63404 | 502 | have "?F ` ?S \<subseteq> Pow(A \<times> B)" | 
| 503 | by auto | |
| 504 | from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" | |
| 505 | by simp | |
| 53820 | 506 | have 2: "inj_on ?F ?S" | 
| 63612 | 507 | by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff) (* somewhat slow *) | 
| 63404 | 508 | show ?thesis | 
| 509 | by (rule finite_imageD [OF 1 2]) | |
| 53820 | 510 | qed | 
| 15392 | 511 | |
| 58195 | 512 | lemma not_finite_existsD: | 
| 513 |   assumes "\<not> finite {a. P a}"
 | |
| 514 | shows "\<exists>a. P a" | |
| 515 | proof (rule classical) | |
| 63404 | 516 | assume "\<not> ?thesis" | 
| 58195 | 517 | with assms show ?thesis by auto | 
| 518 | qed | |
| 519 | ||
| 520 | ||
| 60758 | 521 | subsubsection \<open>Further induction rules on finite sets\<close> | 
| 41656 | 522 | |
| 523 | lemma finite_ne_induct [case_names singleton insert, consumes 2]: | |
| 524 |   assumes "finite F" and "F \<noteq> {}"
 | |
| 525 |   assumes "\<And>x. P {x}"
 | |
| 526 |     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
 | |
| 527 | shows "P F" | |
| 63404 | 528 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 529 | proof induct | 
| 63404 | 530 | case empty | 
| 531 | then show ?case by simp | |
| 41656 | 532 | next | 
| 63404 | 533 | case (insert x F) | 
| 534 | then show ?case by cases auto | |
| 41656 | 535 | qed | 
| 536 | ||
| 537 | lemma finite_subset_induct [consumes 2, case_names empty insert]: | |
| 538 | assumes "finite F" and "F \<subseteq> A" | |
| 63612 | 539 |     and empty: "P {}"
 | 
| 41656 | 540 | and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)" | 
| 541 | shows "P F" | |
| 63404 | 542 | using \<open>finite F\<close> \<open>F \<subseteq> A\<close> | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 543 | proof induct | 
| 41656 | 544 |   show "P {}" by fact
 | 
| 31441 | 545 | next | 
| 41656 | 546 | fix x F | 
| 63404 | 547 | assume "finite F" and "x \<notin> F" and P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | 
| 41656 | 548 | show "P (insert x F)" | 
| 549 | proof (rule insert) | |
| 550 | from i show "x \<in> A" by blast | |
| 551 | from i have "F \<subseteq> A" by blast | |
| 552 | with P show "P F" . | |
| 553 | show "finite F" by fact | |
| 554 | show "x \<notin> F" by fact | |
| 555 | qed | |
| 556 | qed | |
| 557 | ||
| 558 | lemma finite_empty_induct: | |
| 559 | assumes "finite A" | |
| 63612 | 560 | and "P A" | 
| 41656 | 561 |     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
 | 
| 562 |   shows "P {}"
 | |
| 563 | proof - | |
| 63404 | 564 | have "P (A - B)" if "B \<subseteq> A" for B :: "'a set" | 
| 41656 | 565 | proof - | 
| 63404 | 566 | from \<open>finite A\<close> that have "finite B" | 
| 567 | by (rule rev_finite_subset) | |
| 60758 | 568 | from this \<open>B \<subseteq> A\<close> show "P (A - B)" | 
| 41656 | 569 | proof induct | 
| 570 | case empty | |
| 60758 | 571 | from \<open>P A\<close> show ?case by simp | 
| 41656 | 572 | next | 
| 573 | case (insert b B) | |
| 574 |       have "P (A - B - {b})"
 | |
| 575 | proof (rule remove) | |
| 63404 | 576 | from \<open>finite A\<close> show "finite (A - B)" | 
| 577 | by induct auto | |
| 578 | from insert show "b \<in> A - B" | |
| 579 | by simp | |
| 580 | from insert show "P (A - B)" | |
| 581 | by simp | |
| 41656 | 582 | qed | 
| 63404 | 583 |       also have "A - B - {b} = A - insert b B"
 | 
| 584 | by (rule Diff_insert [symmetric]) | |
| 41656 | 585 | finally show ?case . | 
| 586 | qed | |
| 587 | qed | |
| 588 | then have "P (A - A)" by blast | |
| 589 | then show ?thesis by simp | |
| 31441 | 590 | qed | 
| 591 | ||
| 58195 | 592 | lemma finite_update_induct [consumes 1, case_names const update]: | 
| 593 |   assumes finite: "finite {a. f a \<noteq> c}"
 | |
| 63404 | 594 | and const: "P (\<lambda>a. c)" | 
| 595 |     and 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))"
 | |
| 58195 | 596 | shows "P f" | 
| 63404 | 597 | using finite | 
| 598 | proof (induct "{a. f a \<noteq> c}" arbitrary: f)
 | |
| 599 | case empty | |
| 600 | with const show ?case by simp | |
| 58195 | 601 | next | 
| 602 | case (insert a A) | |
| 603 |   then have "A = {a'. (f(a := c)) a' \<noteq> c}" and "f a \<noteq> c"
 | |
| 604 | by auto | |
| 60758 | 605 |   with \<open>finite A\<close> have "finite {a'. (f(a := c)) a' \<noteq> c}"
 | 
| 58195 | 606 | by simp | 
| 607 | have "(f(a := c)) a = c" | |
| 608 | by simp | |
| 60758 | 609 |   from insert \<open>A = {a'. (f(a := c)) a' \<noteq> c}\<close> have "P (f(a := c))"
 | 
| 58195 | 610 | by simp | 
| 63404 | 611 |   with \<open>finite {a'. (f(a := c)) a' \<noteq> c}\<close> \<open>(f(a := c)) a = c\<close> \<open>f a \<noteq> c\<close>
 | 
| 612 | have "P ((f(a := c))(a := f a))" | |
| 58195 | 613 | by (rule update) | 
| 614 | then show ?case by simp | |
| 615 | qed | |
| 616 | ||
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 617 | lemma finite_subset_induct' [consumes 2, case_names empty insert]: | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 618 | assumes "finite F" and "F \<subseteq> A" | 
| 63612 | 619 |     and empty: "P {}"
 | 
| 620 | and insert: "\<And>a F. \<lbrakk>finite F; a \<in> A; F \<subseteq> A; a \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert a F)" | |
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 621 | shows "P F" | 
| 63915 | 622 | using assms(1,2) | 
| 623 | proof induct | |
| 624 |   show "P {}" by fact
 | |
| 625 | next | |
| 626 | fix x F | |
| 627 | assume "finite F" and "x \<notin> F" and | |
| 628 | P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | |
| 629 | show "P (insert x F)" | |
| 630 | proof (rule insert) | |
| 631 | from i show "x \<in> A" by blast | |
| 632 | from i have "F \<subseteq> A" by blast | |
| 633 | with P show "P F" . | |
| 634 | show "finite F" by fact | |
| 635 | show "x \<notin> F" by fact | |
| 636 | show "F \<subseteq> A" by fact | |
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 637 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 638 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 639 | |
| 58195 | 640 | |
| 61799 | 641 | subsection \<open>Class \<open>finite\<close>\<close> | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 642 | |
| 63612 | 643 | class finite = | 
| 644 | assumes finite_UNIV: "finite (UNIV :: 'a set)" | |
| 27430 | 645 | begin | 
| 646 | ||
| 61076 | 647 | lemma finite [simp]: "finite (A :: 'a set)" | 
| 26441 | 648 | by (rule subset_UNIV finite_UNIV finite_subset)+ | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 649 | |
| 61076 | 650 | lemma finite_code [code]: "finite (A :: 'a set) \<longleftrightarrow> True" | 
| 40922 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 651 | by simp | 
| 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 652 | |
| 27430 | 653 | end | 
| 654 | ||
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 655 | instance prod :: (finite, finite) finite | 
| 61169 | 656 | by standard (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite) | 
| 26146 | 657 | |
| 63404 | 658 | lemma inj_graph: "inj (\<lambda>f. {(x, y). y = f x})"
 | 
| 659 | by (rule inj_onI) (auto simp add: set_eq_iff fun_eq_iff) | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 660 | |
| 26146 | 661 | instance "fun" :: (finite, finite) finite | 
| 662 | proof | |
| 63404 | 663 |   show "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
 | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 664 | proof (rule finite_imageD) | 
| 63404 | 665 |     let ?graph = "\<lambda>f::'a \<Rightarrow> 'b. {(x, y). y = f x}"
 | 
| 666 | have "range ?graph \<subseteq> Pow UNIV" | |
| 667 | by simp | |
| 26792 | 668 |     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
 | 
| 669 | by (simp only: finite_Pow_iff finite) | |
| 670 | ultimately show "finite (range ?graph)" | |
| 671 | by (rule finite_subset) | |
| 63404 | 672 | show "inj ?graph" | 
| 673 | by (rule inj_graph) | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 674 | qed | 
| 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 675 | qed | 
| 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 676 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 677 | instance bool :: finite | 
| 61169 | 678 | by standard (simp add: UNIV_bool) | 
| 44831 | 679 | |
| 45962 | 680 | instance set :: (finite) finite | 
| 61169 | 681 | by standard (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite) | 
| 45962 | 682 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 683 | instance unit :: finite | 
| 61169 | 684 | by standard (simp add: UNIV_unit) | 
| 44831 | 685 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 686 | instance sum :: (finite, finite) finite | 
| 61169 | 687 | by standard (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite) | 
| 27981 | 688 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 689 | |
| 60758 | 690 | subsection \<open>A basic fold functional for finite sets\<close> | 
| 15392 | 691 | |
| 60758 | 692 | text \<open>The intended behaviour is | 
| 63404 | 693 |   \<open>fold f z {x\<^sub>1, \<dots>, x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)\<close>
 | 
| 694 | if \<open>f\<close> is ``left-commutative'': | |
| 60758 | 695 | \<close> | 
| 15392 | 696 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 697 | locale comp_fun_commute = | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 698 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 699 | assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y" | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 700 | begin | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 701 | |
| 51489 | 702 | lemma fun_left_comm: "f y (f x z) = f x (f y z)" | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 703 | using comp_fun_commute by (simp add: fun_eq_iff) | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 704 | |
| 63404 | 705 | lemma commute_left_comp: "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)" | 
| 51489 | 706 | by (simp add: o_assoc comp_fun_commute) | 
| 707 | ||
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 708 | end | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 709 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 710 | inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
 | 
| 63404 | 711 | for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b | 
| 63612 | 712 | where | 
| 713 |     emptyI [intro]: "fold_graph f z {} z"
 | |
| 714 | | insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y \<Longrightarrow> fold_graph f z (insert x A) (f x y)" | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 715 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 716 | inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
 | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 717 | |
| 68521 | 718 | lemma fold_graph_closed_lemma: | 
| 719 | "fold_graph f z A x \<and> x \<in> B" | |
| 720 | if "fold_graph g z A x" | |
| 721 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 722 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 723 | "z \<in> B" | |
| 724 | using that(1-3) | |
| 725 | proof (induction rule: fold_graph.induct) | |
| 726 | case (insertI x A y) | |
| 727 | have "fold_graph f z A y" "y \<in> B" | |
| 728 | unfolding atomize_conj | |
| 729 | by (rule insertI.IH) (auto intro: insertI.prems) | |
| 730 | then have "g x y \<in> B" and f_eq: "f x y = g x y" | |
| 731 | by (auto simp: insertI.prems) | |
| 732 | moreover have "fold_graph f z (insert x A) (f x y)" | |
| 733 | by (rule fold_graph.insertI; fact) | |
| 734 | ultimately | |
| 735 | show ?case | |
| 736 | by (simp add: f_eq) | |
| 737 | qed (auto intro!: that) | |
| 738 | ||
| 739 | lemma fold_graph_closed_eq: | |
| 740 | "fold_graph f z A = fold_graph g z A" | |
| 741 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 742 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 743 | "z \<in> B" | |
| 744 | using fold_graph_closed_lemma[of f z A _ B g] fold_graph_closed_lemma[of g z A _ B f] that | |
| 745 | by auto | |
| 746 | ||
| 63404 | 747 | definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
 | 
| 748 | where "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)" | |
| 15392 | 749 | |
| 68521 | 750 | lemma fold_closed_eq: "fold f z A = fold g z A" | 
| 751 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 752 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 753 | "z \<in> B" | |
| 754 | unfolding Finite_Set.fold_def | |
| 755 | by (subst fold_graph_closed_eq[where B=B and g=g]) (auto simp: that) | |
| 756 | ||
| 63404 | 757 | text \<open> | 
| 758 | A tempting alternative for the definiens is | |
| 759 |   @{term "if finite A then THE y. fold_graph f z A y else e"}.
 | |
| 760 | It allows the removal of finiteness assumptions from the theorems | |
| 761 | \<open>fold_comm\<close>, \<open>fold_reindex\<close> and \<open>fold_distrib\<close>. | |
| 762 | The proofs become ugly. It is not worth the effort. (???) | |
| 763 | \<close> | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 764 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 765 | lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x" | 
| 63404 | 766 | by (induct rule: finite_induct) auto | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 767 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 768 | |
| 63404 | 769 | subsubsection \<open>From @{const fold_graph} to @{term fold}\<close>
 | 
| 15392 | 770 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 771 | context comp_fun_commute | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 772 | begin | 
| 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 773 | |
| 51489 | 774 | lemma fold_graph_finite: | 
| 775 | assumes "fold_graph f z A y" | |
| 776 | shows "finite A" | |
| 777 | using assms by induct simp_all | |
| 778 | ||
| 36045 | 779 | lemma fold_graph_insertE_aux: | 
| 780 |   "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'"
 | |
| 781 | proof (induct set: fold_graph) | |
| 63404 | 782 | case emptyI | 
| 783 | then show ?case by simp | |
| 784 | next | |
| 785 | case (insertI x A y) | |
| 786 | show ?case | |
| 36045 | 787 | proof (cases "x = a") | 
| 63404 | 788 | case True | 
| 789 | with insertI show ?thesis by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 790 | next | 
| 63404 | 791 | case False | 
| 36045 | 792 |     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
 | 
| 793 | using insertI by auto | |
| 42875 | 794 | have "f x y = f a (f x y')" | 
| 36045 | 795 | unfolding y by (rule fun_left_comm) | 
| 42875 | 796 |     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
 | 
| 60758 | 797 | using y' and \<open>x \<noteq> a\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 798 | by (simp add: insert_Diff_if fold_graph.insertI) | 
| 63404 | 799 | ultimately show ?thesis | 
| 800 | by fast | |
| 15392 | 801 | qed | 
| 63404 | 802 | qed | 
| 36045 | 803 | |
| 804 | lemma fold_graph_insertE: | |
| 805 | assumes "fold_graph f z (insert x A) v" and "x \<notin> A" | |
| 806 | obtains y where "v = f x y" and "fold_graph f z A y" | |
| 63404 | 807 | using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1]) | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 808 | |
| 63404 | 809 | lemma fold_graph_determ: "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x" | 
| 36045 | 810 | proof (induct arbitrary: y set: fold_graph) | 
| 63404 | 811 | case emptyI | 
| 812 | then show ?case by fast | |
| 813 | next | |
| 36045 | 814 | case (insertI x A y v) | 
| 60758 | 815 | from \<open>fold_graph f z (insert x A) v\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 816 | obtain y' where "v = f x y'" and "fold_graph f z A y'" | 
| 817 | by (rule fold_graph_insertE) | |
| 63404 | 818 | from \<open>fold_graph f z A y'\<close> have "y' = y" | 
| 819 | by (rule insertI) | |
| 820 | with \<open>v = f x y'\<close> show "v = f x y" | |
| 821 | by simp | |
| 822 | qed | |
| 15392 | 823 | |
| 63404 | 824 | lemma fold_equality: "fold_graph f z A y \<Longrightarrow> fold f z A = y" | 
| 51489 | 825 | by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite) | 
| 15392 | 826 | |
| 42272 | 827 | lemma fold_graph_fold: | 
| 828 | assumes "finite A" | |
| 829 | shows "fold_graph f z A (fold f z A)" | |
| 830 | proof - | |
| 63404 | 831 | from assms have "\<exists>x. fold_graph f z A x" | 
| 832 | by (rule finite_imp_fold_graph) | |
| 42272 | 833 | moreover note fold_graph_determ | 
| 63404 | 834 | ultimately have "\<exists>!x. fold_graph f z A x" | 
| 835 | by (rule ex_ex1I) | |
| 836 | then have "fold_graph f z A (The (fold_graph f z A))" | |
| 837 | by (rule theI') | |
| 838 | with assms show ?thesis | |
| 839 | by (simp add: fold_def) | |
| 42272 | 840 | qed | 
| 36045 | 841 | |
| 61799 | 842 | text \<open>The base case for \<open>fold\<close>:\<close> | 
| 15392 | 843 | |
| 63404 | 844 | lemma (in -) fold_infinite [simp]: "\<not> finite A \<Longrightarrow> fold f z A = z" | 
| 845 | by (auto simp: fold_def) | |
| 51489 | 846 | |
| 63404 | 847 | lemma (in -) fold_empty [simp]: "fold f z {} = z"
 | 
| 848 | by (auto simp: fold_def) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 849 | |
| 63404 | 850 | text \<open>The various recursion equations for @{const fold}:\<close>
 | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 851 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 852 | lemma fold_insert [simp]: | 
| 42875 | 853 | assumes "finite A" and "x \<notin> A" | 
| 854 | shows "fold f z (insert x A) = f x (fold f z A)" | |
| 855 | proof (rule fold_equality) | |
| 51489 | 856 | fix z | 
| 63404 | 857 | from \<open>finite A\<close> have "fold_graph f z A (fold f z A)" | 
| 858 | by (rule fold_graph_fold) | |
| 859 | with \<open>x \<notin> A\<close> have "fold_graph f z (insert x A) (f x (fold f z A))" | |
| 860 | by (rule fold_graph.insertI) | |
| 861 | then show "fold_graph f z (insert x A) (f x (fold f z A))" | |
| 862 | by simp | |
| 42875 | 863 | qed | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 864 | |
| 51489 | 865 | declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del] | 
| 61799 | 866 | \<comment> \<open>No more proofs involve these.\<close> | 
| 51489 | 867 | |
| 63404 | 868 | lemma fold_fun_left_comm: "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A" | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 869 | proof (induct rule: finite_induct) | 
| 63404 | 870 | case empty | 
| 871 | then show ?case by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 872 | next | 
| 63404 | 873 | case insert | 
| 874 | then show ?case | |
| 51489 | 875 | by (simp add: fun_left_comm [of x]) | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 876 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 877 | |
| 63404 | 878 | lemma fold_insert2: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | 
| 51489 | 879 | by (simp add: fold_fun_left_comm) | 
| 15392 | 880 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 881 | lemma fold_rec: | 
| 42875 | 882 | assumes "finite A" and "x \<in> A" | 
| 883 |   shows "fold f z A = f x (fold f z (A - {x}))"
 | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 884 | proof - | 
| 63404 | 885 |   have A: "A = insert x (A - {x})"
 | 
| 886 | using \<open>x \<in> A\<close> by blast | |
| 887 |   then have "fold f z A = fold f z (insert x (A - {x}))"
 | |
| 888 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 889 |   also have "\<dots> = f x (fold f z (A - {x}))"
 | 
| 60758 | 890 | by (rule fold_insert) (simp add: \<open>finite A\<close>)+ | 
| 15535 | 891 | finally show ?thesis . | 
| 892 | qed | |
| 893 | ||
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 894 | lemma fold_insert_remove: | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 895 | assumes "finite A" | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 896 |   shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
 | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 897 | proof - | 
| 63404 | 898 | from \<open>finite A\<close> have "finite (insert x A)" | 
| 899 | by auto | |
| 900 | moreover have "x \<in> insert x A" | |
| 901 | by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 902 |   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
 | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 903 | by (rule fold_rec) | 
| 63404 | 904 | then show ?thesis | 
| 905 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 906 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 907 | |
| 57598 | 908 | lemma fold_set_union_disj: | 
| 909 |   assumes "finite A" "finite B" "A \<inter> B = {}"
 | |
| 910 | shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B" | |
| 63404 | 911 | using assms(2,1,3) by induct simp_all | 
| 57598 | 912 | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 913 | end | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 914 | |
| 63404 | 915 | text \<open>Other properties of @{const fold}:\<close>
 | 
| 48619 | 916 | |
| 917 | lemma fold_image: | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 918 | assumes "inj_on g A" | 
| 51489 | 919 | shows "fold f z (g ` A) = fold (f \<circ> g) z A" | 
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 920 | proof (cases "finite A") | 
| 63404 | 921 | case False | 
| 922 | with assms show ?thesis | |
| 923 | by (auto dest: finite_imageD simp add: fold_def) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 924 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 925 | case True | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 926 | have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A" | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 927 | proof | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 928 | fix w | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 929 | show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q") | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 930 | proof | 
| 63404 | 931 | assume ?P | 
| 932 | then show ?Q | |
| 933 | using assms | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 934 | proof (induct "g ` A" w arbitrary: A) | 
| 63404 | 935 | case emptyI | 
| 936 | then show ?case by (auto intro: fold_graph.emptyI) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 937 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 938 | case (insertI x A r B) | 
| 63404 | 939 | from \<open>inj_on g B\<close> \<open>x \<notin> A\<close> \<open>insert x A = image g B\<close> obtain x' A' | 
| 940 | where "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'" | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 941 | by (rule inj_img_insertE) | 
| 63404 | 942 | from insertI.prems have "fold_graph (f \<circ> g) z A' r" | 
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 943 | by (auto intro: insertI.hyps) | 
| 60758 | 944 | with \<open>x' \<notin> A'\<close> have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)" | 
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 945 | by (rule fold_graph.insertI) | 
| 63404 | 946 | then show ?case | 
| 947 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 948 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 949 | next | 
| 63404 | 950 | assume ?Q | 
| 951 | then show ?P | |
| 952 | using assms | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 953 | proof induct | 
| 63404 | 954 | case emptyI | 
| 955 | then show ?case | |
| 956 | by (auto intro: fold_graph.emptyI) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 957 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 958 | case (insertI x A r) | 
| 63404 | 959 | from \<open>x \<notin> A\<close> insertI.prems have "g x \<notin> g ` A" | 
| 960 | by auto | |
| 961 | moreover from insertI have "fold_graph f z (g ` A) r" | |
| 962 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 963 | ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)" | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 964 | by (rule fold_graph.insertI) | 
| 63404 | 965 | then show ?case | 
| 966 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 967 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 968 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 969 | qed | 
| 63404 | 970 | with True assms show ?thesis | 
| 971 | by (auto simp add: fold_def) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 972 | qed | 
| 15392 | 973 | |
| 49724 | 974 | lemma fold_cong: | 
| 975 | assumes "comp_fun_commute f" "comp_fun_commute g" | |
| 63404 | 976 | and "finite A" | 
| 977 | and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x" | |
| 51489 | 978 | and "s = t" and "A = B" | 
| 979 | shows "fold f s A = fold g t B" | |
| 49724 | 980 | proof - | 
| 63404 | 981 | have "fold f s A = fold g s A" | 
| 982 | using \<open>finite A\<close> cong | |
| 983 | proof (induct A) | |
| 984 | case empty | |
| 985 | then show ?case by simp | |
| 49724 | 986 | next | 
| 63404 | 987 | case insert | 
| 60758 | 988 | interpret f: comp_fun_commute f by (fact \<open>comp_fun_commute f\<close>) | 
| 989 | interpret g: comp_fun_commute g by (fact \<open>comp_fun_commute g\<close>) | |
| 49724 | 990 | from insert show ?case by simp | 
| 991 | qed | |
| 992 | with assms show ?thesis by simp | |
| 993 | qed | |
| 994 | ||
| 995 | ||
| 60758 | 996 | text \<open>A simplified version for idempotent functions:\<close> | 
| 15480 | 997 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 998 | locale comp_fun_idem = comp_fun_commute + | 
| 51489 | 999 | assumes comp_fun_idem: "f x \<circ> f x = f x" | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 1000 | begin | 
| 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 1001 | |
| 42869 
43b0f61f56d0
use point-free characterization for locale fun_left_comm_idem
 haftmann parents: 
42809diff
changeset | 1002 | lemma fun_left_idem: "f x (f x z) = f x z" | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1003 | using comp_fun_idem by (simp add: fun_eq_iff) | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1004 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 1005 | lemma fold_insert_idem: | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1006 | assumes fin: "finite A" | 
| 51489 | 1007 | shows "fold f z (insert x A) = f x (fold f z A)" | 
| 15480 | 1008 | proof cases | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1009 | assume "x \<in> A" | 
| 63404 | 1010 | then obtain B where "A = insert x B" and "x \<notin> B" | 
| 1011 | by (rule set_insert) | |
| 1012 | then show ?thesis | |
| 1013 | using assms by (simp add: comp_fun_idem fun_left_idem) | |
| 15480 | 1014 | next | 
| 63404 | 1015 | assume "x \<notin> A" | 
| 1016 | then show ?thesis | |
| 1017 | using assms by simp | |
| 15480 | 1018 | qed | 
| 1019 | ||
| 51489 | 1020 | declare fold_insert [simp del] fold_insert_idem [simp] | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1021 | |
| 63404 | 1022 | lemma fold_insert_idem2: "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | 
| 51489 | 1023 | by (simp add: fold_fun_left_comm) | 
| 15484 | 1024 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 1025 | end | 
| 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 1026 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1027 | |
| 61799 | 1028 | subsubsection \<open>Liftings to \<open>comp_fun_commute\<close> etc.\<close> | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1029 | |
| 63404 | 1030 | lemma (in comp_fun_commute) comp_comp_fun_commute: "comp_fun_commute (f \<circ> g)" | 
| 1031 | by standard (simp_all add: comp_fun_commute) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1032 | |
| 63404 | 1033 | lemma (in comp_fun_idem) comp_comp_fun_idem: "comp_fun_idem (f \<circ> g)" | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1034 | by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales) | 
| 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1035 | (simp_all add: comp_fun_idem) | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1036 | |
| 63404 | 1037 | lemma (in comp_fun_commute) comp_fun_commute_funpow: "comp_fun_commute (\<lambda>x. f x ^^ g x)" | 
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1038 | proof | 
| 63404 | 1039 | show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y" for x y | 
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1040 | proof (cases "x = y") | 
| 63404 | 1041 | case True | 
| 1042 | then show ?thesis by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1043 | next | 
| 63404 | 1044 | case False | 
| 1045 | show ?thesis | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1046 | proof (induct "g x" arbitrary: g) | 
| 63404 | 1047 | case 0 | 
| 1048 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1049 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1050 | case (Suc n g) | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1051 | have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y" | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1052 | proof (induct "g y" arbitrary: g) | 
| 63404 | 1053 | case 0 | 
| 1054 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1055 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1056 | case (Suc n g) | 
| 63040 | 1057 | define h where "h z = g z - 1" for z | 
| 63404 | 1058 | with Suc have "n = h y" | 
| 1059 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1060 | with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y" | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1061 | by auto | 
| 63404 | 1062 | from Suc h_def have "g y = Suc (h y)" | 
| 1063 | by simp | |
| 1064 | then show ?case | |
| 1065 | by (simp add: comp_assoc hyp) (simp add: o_assoc comp_fun_commute) | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1066 | qed | 
| 63040 | 1067 | define h where "h z = (if z = x then g x - 1 else g z)" for z | 
| 63404 | 1068 | with Suc have "n = h x" | 
| 1069 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1070 | with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y" | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1071 | by auto | 
| 63404 | 1072 | with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" | 
| 1073 | by simp | |
| 1074 | from Suc h_def have "g x = Suc (h x)" | |
| 1075 | by simp | |
| 1076 | then show ?case | |
| 1077 | by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2) (simp add: comp_assoc hyp1) | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1078 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1079 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1080 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1081 | |
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1082 | |
| 60758 | 1083 | subsubsection \<open>Expressing set operations via @{const fold}\<close>
 | 
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1084 | |
| 63404 | 1085 | lemma comp_fun_commute_const: "comp_fun_commute (\<lambda>_. f)" | 
| 1086 | by standard rule | |
| 51489 | 1087 | |
| 63404 | 1088 | lemma comp_fun_idem_insert: "comp_fun_idem insert" | 
| 1089 | by standard auto | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1090 | |
| 63404 | 1091 | lemma comp_fun_idem_remove: "comp_fun_idem Set.remove" | 
| 1092 | by standard auto | |
| 31992 | 1093 | |
| 63404 | 1094 | lemma (in semilattice_inf) comp_fun_idem_inf: "comp_fun_idem inf" | 
| 1095 | by standard (auto simp add: inf_left_commute) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1096 | |
| 63404 | 1097 | lemma (in semilattice_sup) comp_fun_idem_sup: "comp_fun_idem sup" | 
| 1098 | by standard (auto simp add: sup_left_commute) | |
| 31992 | 1099 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1100 | lemma union_fold_insert: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1101 | assumes "finite A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1102 | shows "A \<union> B = fold insert B A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1103 | proof - | 
| 63404 | 1104 | interpret comp_fun_idem insert | 
| 1105 | by (fact comp_fun_idem_insert) | |
| 1106 | from \<open>finite A\<close> show ?thesis | |
| 1107 | by (induct A arbitrary: B) simp_all | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1108 | qed | 
| 31992 | 1109 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1110 | lemma minus_fold_remove: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1111 | assumes "finite A" | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1112 | shows "B - A = fold Set.remove B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1113 | proof - | 
| 63404 | 1114 | interpret comp_fun_idem Set.remove | 
| 1115 | by (fact comp_fun_idem_remove) | |
| 1116 | from \<open>finite A\<close> have "fold Set.remove B A = B - A" | |
| 63612 | 1117 | by (induct A arbitrary: B) auto (* slow *) | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1118 | then show ?thesis .. | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1119 | qed | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1120 | |
| 51489 | 1121 | lemma comp_fun_commute_filter_fold: | 
| 1122 | "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')" | |
| 63404 | 1123 | proof - | 
| 48619 | 1124 | interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert) | 
| 61169 | 1125 | show ?thesis by standard (auto simp: fun_eq_iff) | 
| 48619 | 1126 | qed | 
| 1127 | ||
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1128 | lemma Set_filter_fold: | 
| 48619 | 1129 | assumes "finite A" | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1130 |   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
 | 
| 63404 | 1131 | using assms | 
| 1132 | by induct | |
| 1133 | (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold]) | |
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1134 | |
| 63404 | 1135 | lemma inter_Set_filter: | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1136 | assumes "finite B" | 
| 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1137 | shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B" | 
| 63404 | 1138 | using assms | 
| 1139 | by induct (auto simp: Set.filter_def) | |
| 48619 | 1140 | |
| 1141 | lemma image_fold_insert: | |
| 1142 | assumes "finite A" | |
| 1143 |   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
 | |
| 1144 | proof - | |
| 63404 | 1145 | interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" | 
| 1146 | by standard auto | |
| 1147 | show ?thesis | |
| 1148 | using assms by (induct A) auto | |
| 48619 | 1149 | qed | 
| 1150 | ||
| 1151 | lemma Ball_fold: | |
| 1152 | assumes "finite A" | |
| 1153 | shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A" | |
| 1154 | proof - | |
| 63404 | 1155 | interpret comp_fun_commute "\<lambda>k s. s \<and> P k" | 
| 1156 | by standard auto | |
| 1157 | show ?thesis | |
| 1158 | using assms by (induct A) auto | |
| 48619 | 1159 | qed | 
| 1160 | ||
| 1161 | lemma Bex_fold: | |
| 1162 | assumes "finite A" | |
| 1163 | shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A" | |
| 1164 | proof - | |
| 63404 | 1165 | interpret comp_fun_commute "\<lambda>k s. s \<or> P k" | 
| 1166 | by standard auto | |
| 1167 | show ?thesis | |
| 1168 | using assms by (induct A) auto | |
| 48619 | 1169 | qed | 
| 1170 | ||
| 63404 | 1171 | lemma comp_fun_commute_Pow_fold: "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" | 
| 63612 | 1172 | by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast (* somewhat slow *) | 
| 48619 | 1173 | |
| 1174 | lemma Pow_fold: | |
| 1175 | assumes "finite A" | |
| 1176 |   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
 | |
| 1177 | proof - | |
| 63404 | 1178 | interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" | 
| 1179 | by (rule comp_fun_commute_Pow_fold) | |
| 1180 | show ?thesis | |
| 1181 | using assms by (induct A) (auto simp: Pow_insert) | |
| 48619 | 1182 | qed | 
| 1183 | ||
| 1184 | lemma fold_union_pair: | |
| 1185 | assumes "finite B" | |
| 1186 |   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
 | |
| 1187 | proof - | |
| 63404 | 1188 | interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" | 
| 1189 | by standard auto | |
| 1190 | show ?thesis | |
| 1191 | using assms by (induct arbitrary: A) simp_all | |
| 48619 | 1192 | qed | 
| 1193 | ||
| 63404 | 1194 | lemma comp_fun_commute_product_fold: | 
| 1195 | "finite B \<Longrightarrow> comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" | |
| 1196 | by standard (auto simp: fold_union_pair [symmetric]) | |
| 48619 | 1197 | |
| 1198 | lemma product_fold: | |
| 63404 | 1199 | assumes "finite A" "finite B" | 
| 51489 | 1200 |   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
 | 
| 63404 | 1201 | using assms unfolding Sigma_def | 
| 1202 | by (induct A) | |
| 1203 | (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair) | |
| 48619 | 1204 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1205 | context complete_lattice | 
| 31992 | 1206 | begin | 
| 1207 | ||
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1208 | lemma inf_Inf_fold_inf: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1209 | assumes "finite A" | 
| 51489 | 1210 | shows "inf (Inf A) B = fold inf B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1211 | proof - | 
| 63404 | 1212 | interpret comp_fun_idem inf | 
| 1213 | by (fact comp_fun_idem_inf) | |
| 1214 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1215 | by (induct A arbitrary: B) (simp_all add: inf_commute fun_eq_iff) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1216 | qed | 
| 31992 | 1217 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1218 | lemma sup_Sup_fold_sup: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1219 | assumes "finite A" | 
| 51489 | 1220 | shows "sup (Sup A) B = fold sup B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1221 | proof - | 
| 63404 | 1222 | interpret comp_fun_idem sup | 
| 1223 | by (fact comp_fun_idem_sup) | |
| 1224 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1225 | by (induct A arbitrary: B) (simp_all add: sup_commute fun_eq_iff) | |
| 31992 | 1226 | qed | 
| 1227 | ||
| 63404 | 1228 | lemma Inf_fold_inf: "finite A \<Longrightarrow> Inf A = fold inf top A" | 
| 1229 | using inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1230 | |
| 63404 | 1231 | lemma Sup_fold_sup: "finite A \<Longrightarrow> Sup A = fold sup bot A" | 
| 1232 | using sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2) | |
| 31992 | 1233 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1234 | lemma inf_INF_fold_inf: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1235 | assumes "finite A" | 
| 63404 | 1236 | shows "inf B (INFIMUM A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") | 
| 1237 | proof - | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1238 | interpret comp_fun_idem inf by (fact comp_fun_idem_inf) | 
| 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1239 | interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1240 | from \<open>finite A\<close> have "?fold = ?inf" | 
| 1241 | by (induct A arbitrary: B) (simp_all add: inf_left_commute) | |
| 1242 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1243 | qed | 
| 31992 | 1244 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1245 | lemma sup_SUP_fold_sup: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1246 | assumes "finite A" | 
| 63404 | 1247 | shows "sup B (SUPREMUM A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") | 
| 1248 | proof - | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1249 | interpret comp_fun_idem sup by (fact comp_fun_idem_sup) | 
| 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1250 | interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1251 | from \<open>finite A\<close> have "?fold = ?sup" | 
| 1252 | by (induct A arbitrary: B) (simp_all add: sup_left_commute) | |
| 1253 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1254 | qed | 
| 31992 | 1255 | |
| 63404 | 1256 | lemma INF_fold_inf: "finite A \<Longrightarrow> INFIMUM A f = fold (inf \<circ> f) top A" | 
| 1257 | using inf_INF_fold_inf [of A top] by simp | |
| 31992 | 1258 | |
| 63404 | 1259 | lemma SUP_fold_sup: "finite A \<Longrightarrow> SUPREMUM A f = fold (sup \<circ> f) bot A" | 
| 1260 | using sup_SUP_fold_sup [of A bot] by simp | |
| 31992 | 1261 | |
| 1262 | end | |
| 1263 | ||
| 1264 | ||
| 60758 | 1265 | subsection \<open>Locales as mini-packages for fold operations\<close> | 
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33960diff
changeset | 1266 | |
| 60758 | 1267 | subsubsection \<open>The natural case\<close> | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1268 | |
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1269 | locale folding = | 
| 63612 | 1270 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: "'b" | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1271 | assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1272 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1273 | |
| 54870 | 1274 | interpretation fold?: comp_fun_commute f | 
| 63612 | 1275 | by standard (use comp_fun_commute in \<open>simp add: fun_eq_iff\<close>) | 
| 54867 
c21a2465cac1
prefer ephemeral interpretation over interpretation in proof contexts;
 haftmann parents: 
54611diff
changeset | 1276 | |
| 51489 | 1277 | definition F :: "'a set \<Rightarrow> 'b" | 
| 63404 | 1278 | where eq_fold: "F A = fold f z A" | 
| 51489 | 1279 | |
| 61169 | 1280 | lemma empty [simp]:"F {} = z"
 | 
| 51489 | 1281 | by (simp add: eq_fold) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1282 | |
| 61169 | 1283 | lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F A = z" | 
| 51489 | 1284 | by (simp add: eq_fold) | 
| 63404 | 1285 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1286 | lemma insert [simp]: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1287 | assumes "finite A" and "x \<notin> A" | 
| 51489 | 1288 | shows "F (insert x A) = f x (F A)" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1289 | proof - | 
| 51489 | 1290 | from fold_insert assms | 
| 1291 | have "fold f z (insert x A) = f x (fold f z A)" by simp | |
| 60758 | 1292 | with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1293 | qed | 
| 63404 | 1294 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1295 | lemma remove: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1296 | assumes "finite A" and "x \<in> A" | 
| 51489 | 1297 |   shows "F A = f x (F (A - {x}))"
 | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1298 | proof - | 
| 60758 | 1299 | from \<open>x \<in> A\<close> obtain B where A: "A = insert x B" and "x \<notin> B" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1300 | by (auto dest: mk_disjoint_insert) | 
| 60758 | 1301 | moreover from \<open>finite A\<close> A have "finite B" by simp | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1302 | ultimately show ?thesis by simp | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1303 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1304 | |
| 63404 | 1305 | lemma insert_remove: "finite A \<Longrightarrow> F (insert x A) = f x (F (A - {x}))"
 | 
| 1306 | by (cases "x \<in> A") (simp_all add: remove insert_absorb) | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1307 | |
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33960diff
changeset | 1308 | end | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1309 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1310 | |
| 60758 | 1311 | subsubsection \<open>With idempotency\<close> | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1312 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1313 | locale folding_idem = folding + | 
| 51489 | 1314 | assumes comp_fun_idem: "f x \<circ> f x = f x" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1315 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1316 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1317 | declare insert [simp del] | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1318 | |
| 54870 | 1319 | interpretation fold?: comp_fun_idem f | 
| 61169 | 1320 | by standard (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff) | 
| 54867 
c21a2465cac1
prefer ephemeral interpretation over interpretation in proof contexts;
 haftmann parents: 
54611diff
changeset | 1321 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1322 | lemma insert_idem [simp]: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1323 | assumes "finite A" | 
| 51489 | 1324 | shows "F (insert x A) = f x (F A)" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1325 | proof - | 
| 51489 | 1326 | from fold_insert_idem assms | 
| 1327 | have "fold f z (insert x A) = f x (fold f z A)" by simp | |
| 60758 | 1328 | with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1329 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1330 | |
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1331 | end | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1332 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1333 | |
| 60758 | 1334 | subsection \<open>Finite cardinality\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1335 | |
| 60758 | 1336 | text \<open> | 
| 51489 | 1337 | The traditional definition | 
| 63404 | 1338 |   @{prop "card A \<equiv> LEAST n. \<exists>f. A = {f i |i. i < n}"}
 | 
| 51489 | 1339 | is ugly to work with. | 
| 1340 |   But now that we have @{const fold} things are easy:
 | |
| 60758 | 1341 | \<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1342 | |
| 61890 
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
 haftmann parents: 
61810diff
changeset | 1343 | global_interpretation card: folding "\<lambda>_. Suc" 0 | 
| 61778 | 1344 | defines card = "folding.F (\<lambda>_. Suc) 0" | 
| 1345 | by standard rule | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1346 | |
| 63404 | 1347 | lemma card_infinite: "\<not> finite A \<Longrightarrow> card A = 0" | 
| 51489 | 1348 | by (fact card.infinite) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1349 | |
| 63404 | 1350 | lemma card_empty: "card {} = 0"
 | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1351 | by (fact card.empty) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1352 | |
| 63404 | 1353 | lemma card_insert_disjoint: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)" | 
| 51489 | 1354 | by (fact card.insert) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1355 | |
| 63404 | 1356 | lemma card_insert_if: "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1357 | by auto (simp add: card.insert_remove card.remove) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1358 | |
| 63404 | 1359 | lemma card_ge_0_finite: "card A > 0 \<Longrightarrow> finite A" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1360 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1361 | |
| 63404 | 1362 | lemma card_0_eq [simp]: "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
 | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1363 | by (auto dest: mk_disjoint_insert) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1364 | |
| 63404 | 1365 | lemma finite_UNIV_card_ge_0: "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1366 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1367 | |
| 63404 | 1368 | lemma card_eq_0_iff: "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
 | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1369 | by auto | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1370 | |
| 63404 | 1371 | lemma card_range_greater_zero: "finite (range f) \<Longrightarrow> card (range f) > 0" | 
| 63365 | 1372 | by (rule ccontr) (simp add: card_eq_0_iff) | 
| 1373 | ||
| 63404 | 1374 | lemma card_gt_0_iff: "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
 | 
| 1375 | by (simp add: neq0_conv [symmetric] card_eq_0_iff) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1376 | |
| 63404 | 1377 | lemma card_Suc_Diff1: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
 | 
| 1378 | apply (rule insert_Diff [THEN subst, where t = A]) | |
| 63612 | 1379 | apply assumption | 
| 63404 | 1380 | apply (simp del: insert_Diff_single) | 
| 1381 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1382 | |
| 63404 | 1383 | lemma card_insert_le_m1: "n > 0 \<Longrightarrow> card y \<le> n - 1 \<Longrightarrow> card (insert x y) \<le> n" | 
| 60762 | 1384 | apply (cases "finite y") | 
| 63612 | 1385 | apply (cases "x \<in> y") | 
| 1386 | apply (auto simp: insert_absorb) | |
| 60762 | 1387 | done | 
| 1388 | ||
| 63404 | 1389 | lemma card_Diff_singleton: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
 | 
| 51489 | 1390 | by (simp add: card_Suc_Diff1 [symmetric]) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1391 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1392 | lemma card_Diff_singleton_if: | 
| 51489 | 1393 |   "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
 | 
| 1394 | by (simp add: card_Diff_singleton) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1395 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1396 | lemma card_Diff_insert[simp]: | 
| 51489 | 1397 | assumes "finite A" and "a \<in> A" and "a \<notin> B" | 
| 1398 | shows "card (A - insert a B) = card (A - B) - 1" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1399 | proof - | 
| 63404 | 1400 |   have "A - insert a B = (A - B) - {a}"
 | 
| 1401 | using assms by blast | |
| 1402 | then show ?thesis | |
| 1403 | using assms by (simp add: card_Diff_singleton) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1404 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1405 | |
| 63404 | 1406 | lemma card_insert: "finite A \<Longrightarrow> card (insert x A) = Suc (card (A - {x}))"
 | 
| 51489 | 1407 | by (fact card.insert_remove) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1408 | |
| 63404 | 1409 | lemma card_insert_le: "finite A \<Longrightarrow> card A \<le> card (insert x A)" | 
| 1410 | by (simp add: card_insert_if) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1411 | |
| 63404 | 1412 | lemma card_Collect_less_nat[simp]: "card {i::nat. i < n} = n"
 | 
| 1413 | by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq) | |
| 41987 | 1414 | |
| 63404 | 1415 | lemma card_Collect_le_nat[simp]: "card {i::nat. i \<le> n} = Suc n"
 | 
| 1416 | using card_Collect_less_nat[of "Suc n"] by (simp add: less_Suc_eq_le) | |
| 41987 | 1417 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1418 | lemma card_mono: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1419 | assumes "finite B" and "A \<subseteq> B" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1420 | shows "card A \<le> card B" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1421 | proof - | 
| 63404 | 1422 | from assms have "finite A" | 
| 1423 | by (auto intro: finite_subset) | |
| 1424 | then show ?thesis | |
| 1425 | using assms | |
| 1426 | proof (induct A arbitrary: B) | |
| 1427 | case empty | |
| 1428 | then show ?case by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1429 | next | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1430 | case (insert x A) | 
| 63404 | 1431 | then have "x \<in> B" | 
| 1432 | by simp | |
| 1433 |     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})"
 | |
| 1434 | by auto | |
| 1435 |     with insert.hyps have "card A \<le> card (B - {x})"
 | |
| 1436 | by auto | |
| 1437 | with \<open>finite A\<close> \<open>x \<notin> A\<close> \<open>finite B\<close> \<open>x \<in> B\<close> show ?case | |
| 1438 | by simp (simp only: card.remove) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1439 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1440 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1441 | |
| 63404 | 1442 | lemma card_seteq: "finite B \<Longrightarrow> (\<And>A. A \<subseteq> B \<Longrightarrow> card B \<le> card A \<Longrightarrow> A = B)" | 
| 1443 | apply (induct rule: finite_induct) | |
| 63612 | 1444 | apply simp | 
| 63404 | 1445 | apply clarify | 
| 1446 |   apply (subgoal_tac "finite A \<and> A - {x} \<subseteq> F")
 | |
| 1447 | prefer 2 apply (blast intro: finite_subset, atomize) | |
| 1448 |   apply (drule_tac x = "A - {x}" in spec)
 | |
| 63648 | 1449 | apply (simp add: card_Diff_singleton_if split: if_split_asm) | 
| 63404 | 1450 | apply (case_tac "card A", auto) | 
| 1451 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1452 | |
| 63404 | 1453 | lemma psubset_card_mono: "finite B \<Longrightarrow> A < B \<Longrightarrow> card A < card B" | 
| 1454 | apply (simp add: psubset_eq linorder_not_le [symmetric]) | |
| 1455 | apply (blast dest: card_seteq) | |
| 1456 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1457 | |
| 51489 | 1458 | lemma card_Un_Int: | 
| 63404 | 1459 | assumes "finite A" "finite B" | 
| 51489 | 1460 | shows "card A + card B = card (A \<union> B) + card (A \<inter> B)" | 
| 63404 | 1461 | using assms | 
| 1462 | proof (induct A) | |
| 1463 | case empty | |
| 1464 | then show ?case by simp | |
| 51489 | 1465 | next | 
| 63404 | 1466 | case insert | 
| 1467 | then show ?case | |
| 51489 | 1468 | by (auto simp add: insert_absorb Int_insert_left) | 
| 1469 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1470 | |
| 63404 | 1471 | lemma card_Un_disjoint: "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> card (A \<union> B) = card A + card B"
 | 
| 1472 | using card_Un_Int [of A B] by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1473 | |
| 59336 | 1474 | lemma card_Un_le: "card (A \<union> B) \<le> card A + card B" | 
| 63404 | 1475 | apply (cases "finite A") | 
| 1476 | apply (cases "finite B") | |
| 63612 | 1477 | apply (use le_iff_add card_Un_Int in blast) | 
| 63404 | 1478 | apply simp | 
| 1479 | apply simp | |
| 1480 | done | |
| 59336 | 1481 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1482 | lemma card_Diff_subset: | 
| 63404 | 1483 | assumes "finite B" | 
| 1484 | and "B \<subseteq> A" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1485 | shows "card (A - B) = card A - card B" | 
| 63915 | 1486 | using assms | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1487 | proof (cases "finite A") | 
| 63404 | 1488 | case False | 
| 1489 | with assms show ?thesis | |
| 1490 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1491 | next | 
| 63404 | 1492 | case True | 
| 1493 | with assms show ?thesis | |
| 1494 | by (induct B arbitrary: A) simp_all | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1495 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1496 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1497 | lemma card_Diff_subset_Int: | 
| 63404 | 1498 | assumes "finite (A \<inter> B)" | 
| 1499 | shows "card (A - B) = card A - card (A \<inter> B)" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1500 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1501 | have "A - B = A - A \<inter> B" by auto | 
| 63404 | 1502 | with assms show ?thesis | 
| 1503 | by (simp add: card_Diff_subset) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1504 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1505 | |
| 40716 | 1506 | lemma diff_card_le_card_Diff: | 
| 63404 | 1507 | assumes "finite B" | 
| 1508 | shows "card A - card B \<le> card (A - B)" | |
| 1509 | proof - | |
| 40716 | 1510 | have "card A - card B \<le> card A - card (A \<inter> B)" | 
| 1511 | using card_mono[OF assms Int_lower2, of A] by arith | |
| 63404 | 1512 | also have "\<dots> = card (A - B)" | 
| 1513 | using assms by (simp add: card_Diff_subset_Int) | |
| 40716 | 1514 | finally show ?thesis . | 
| 1515 | qed | |
| 1516 | ||
| 63404 | 1517 | lemma card_Diff1_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) < card A"
 | 
| 1518 | by (rule Suc_less_SucD) (simp add: card_Suc_Diff1 del: card_Diff_insert) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1519 | |
| 63404 | 1520 | lemma card_Diff2_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> card (A - {x} - {y}) < card A"
 | 
| 1521 | apply (cases "x = y") | |
| 1522 | apply (simp add: card_Diff1_less del:card_Diff_insert) | |
| 1523 | apply (rule less_trans) | |
| 1524 | prefer 2 apply (auto intro!: card_Diff1_less simp del: card_Diff_insert) | |
| 1525 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1526 | |
| 63404 | 1527 | lemma card_Diff1_le: "finite A \<Longrightarrow> card (A - {x}) \<le> card A"
 | 
| 1528 | by (cases "x \<in> A") (simp_all add: card_Diff1_less less_imp_le) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1529 | |
| 63404 | 1530 | lemma card_psubset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> card A < card B \<Longrightarrow> A < B" | 
| 1531 | by (erule psubsetI) blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1532 | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1533 | lemma card_le_inj: | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1534 | assumes fA: "finite A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1535 | and fB: "finite B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1536 | and c: "card A \<le> card B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1537 | shows "\<exists>f. f ` A \<subseteq> B \<and> inj_on f A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1538 | using fA fB c | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1539 | proof (induct arbitrary: B rule: finite_induct) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1540 | case empty | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1541 | then show ?case by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1542 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1543 | case (insert x s t) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1544 | then show ?case | 
| 63404 | 1545 | proof (induct rule: finite_induct [OF insert.prems(1)]) | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1546 | case 1 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1547 | then show ?case by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1548 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1549 | case (2 y t) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1550 | from "2.prems"(1,2,5) "2.hyps"(1,2) have cst: "card s \<le> card t" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1551 | by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1552 | from "2.prems"(3) [OF "2.hyps"(1) cst] | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1553 | obtain f where "f ` s \<subseteq> t" "inj_on f s" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1554 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1555 | with "2.prems"(2) "2.hyps"(2) show ?case | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1556 | apply - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1557 | apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"]) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1558 | apply (auto simp add: inj_on_def) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1559 | done | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1560 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1561 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1562 | |
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1563 | lemma card_subset_eq: | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1564 | assumes fB: "finite B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1565 | and AB: "A \<subseteq> B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1566 | and c: "card A = card B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1567 | shows "A = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1568 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1569 | from fB AB have fA: "finite A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1570 | by (auto intro: finite_subset) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1571 | from fA fB have fBA: "finite (B - A)" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1572 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1573 |   have e: "A \<inter> (B - A) = {}"
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1574 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1575 | have eq: "A \<union> (B - A) = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1576 | using AB by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1577 | from card_Un_disjoint[OF fA fBA e, unfolded eq c] have "card (B - A) = 0" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1578 | by arith | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1579 |   then have "B - A = {}"
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1580 | unfolding card_eq_0_iff using fA fB by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1581 | with AB show "A = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1582 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1583 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1584 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1585 | lemma insert_partition: | 
| 63404 | 1586 |   "x \<notin> F \<Longrightarrow> \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<Longrightarrow> x \<inter> \<Union>F = {}"
 | 
| 63612 | 1587 | by auto (* somewhat slow *) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1588 | |
| 63404 | 1589 | lemma finite_psubset_induct [consumes 1, case_names psubset]: | 
| 1590 | assumes finite: "finite A" | |
| 1591 | and major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1592 | shows "P A" | 
| 63404 | 1593 | using finite | 
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1594 | proof (induct A taking: card rule: measure_induct_rule) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1595 | case (less A) | 
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1596 | have fin: "finite A" by fact | 
| 63404 | 1597 | have ih: "card B < card A \<Longrightarrow> finite B \<Longrightarrow> P B" for B by fact | 
| 1598 | have "P B" if "B \<subset> A" for B | |
| 1599 | proof - | |
| 1600 | from that have "card B < card A" | |
| 1601 | using psubset_card_mono fin by blast | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1602 | moreover | 
| 63404 | 1603 | from that have "B \<subseteq> A" | 
| 1604 | by auto | |
| 1605 | then have "finite B" | |
| 1606 | using fin finite_subset by blast | |
| 1607 | ultimately show ?thesis using ih by simp | |
| 1608 | qed | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1609 | with fin show "P A" using major by blast | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1610 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1611 | |
| 63404 | 1612 | lemma finite_induct_select [consumes 1, case_names empty select]: | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1613 | assumes "finite S" | 
| 63404 | 1614 |     and "P {}"
 | 
| 1615 | and select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)" | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1616 | shows "P S" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1617 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1618 | have "0 \<le> card S" by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1619 | then have "\<exists>T \<subseteq> S. card T = card S \<and> P T" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1620 | proof (induct rule: dec_induct) | 
| 63404 | 1621 |     case base with \<open>P {}\<close>
 | 
| 1622 | show ?case | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1623 |       by (intro exI[of _ "{}"]) auto
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1624 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1625 | case (step n) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1626 | then obtain T where T: "T \<subseteq> S" "card T = n" "P T" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1627 | by auto | 
| 60758 | 1628 | with \<open>n < card S\<close> have "T \<subset> S" "P T" | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1629 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1630 | with select[of T] obtain s where "s \<in> S" "s \<notin> T" "P (insert s T)" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1631 | by auto | 
| 60758 | 1632 | with step(2) T \<open>finite S\<close> show ?case | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1633 | by (intro exI[of _ "insert s T"]) (auto dest: finite_subset) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1634 | qed | 
| 60758 | 1635 | with \<open>finite S\<close> show "P S" | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1636 | by (auto dest: card_subset_eq) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1637 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1638 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1639 | lemma remove_induct [case_names empty infinite remove]: | 
| 63404 | 1640 |   assumes empty: "P ({} :: 'a set)"
 | 
| 1641 | and infinite: "\<not> finite B \<Longrightarrow> P B" | |
| 1642 |     and remove: "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1643 | shows "P B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1644 | proof (cases "finite B") | 
| 63612 | 1645 | case False | 
| 63404 | 1646 | then show ?thesis by (rule infinite) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1647 | next | 
| 63612 | 1648 | case True | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1649 | define A where "A = B" | 
| 63612 | 1650 | with True have "finite A" "A \<subseteq> B" | 
| 1651 | by simp_all | |
| 63404 | 1652 | then show "P A" | 
| 1653 | proof (induct "card A" arbitrary: A) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1654 | case 0 | 
| 63404 | 1655 |     then have "A = {}" by auto
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1656 | with empty show ?case by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1657 | next | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1658 | case (Suc n A) | 
| 63404 | 1659 | from \<open>A \<subseteq> B\<close> and \<open>finite B\<close> have "finite A" | 
| 1660 | by (rule finite_subset) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1661 |     moreover from Suc.hyps have "A \<noteq> {}" by auto
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1662 | moreover note \<open>A \<subseteq> B\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1663 |     moreover have "P (A - {x})" if x: "x \<in> A" for x
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1664 | using x Suc.prems \<open>Suc n = card A\<close> by (intro Suc) auto | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1665 | ultimately show ?case by (rule remove) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1666 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1667 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1668 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1669 | lemma finite_remove_induct [consumes 1, case_names empty remove]: | 
| 63404 | 1670 | fixes P :: "'a set \<Rightarrow> bool" | 
| 63612 | 1671 | assumes "finite B" | 
| 1672 |     and "P {}"
 | |
| 1673 |     and "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1674 | defines "B' \<equiv> B" | 
| 63404 | 1675 | shows "P B'" | 
| 1676 | by (induct B' rule: remove_induct) (simp_all add: assms) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1677 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1678 | |
| 63404 | 1679 | text \<open>Main cardinality theorem.\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1680 | lemma card_partition [rule_format]: | 
| 63404 | 1681 | "finite C \<Longrightarrow> finite (\<Union>C) \<Longrightarrow> (\<forall>c\<in>C. card c = k) \<Longrightarrow> | 
| 1682 |     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}) \<Longrightarrow>
 | |
| 1683 | k * card C = card (\<Union>C)" | |
| 63612 | 1684 | proof (induct rule: finite_induct) | 
| 1685 | case empty | |
| 1686 | then show ?case by simp | |
| 1687 | next | |
| 1688 | case (insert x F) | |
| 1689 | then show ?case | |
| 1690 | by (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\<Union>(insert _ _)"]) | |
| 1691 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1692 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1693 | lemma card_eq_UNIV_imp_eq_UNIV: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1694 | assumes fin: "finite (UNIV :: 'a set)" | 
| 63404 | 1695 | and card: "card A = card (UNIV :: 'a set)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1696 | shows "A = (UNIV :: 'a set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1697 | proof | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1698 | show "A \<subseteq> UNIV" by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1699 | show "UNIV \<subseteq> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1700 | proof | 
| 63404 | 1701 | show "x \<in> A" for x | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1702 | proof (rule ccontr) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1703 | assume "x \<notin> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1704 | then have "A \<subset> UNIV" by auto | 
| 63404 | 1705 | with fin have "card A < card (UNIV :: 'a set)" | 
| 1706 | by (fact psubset_card_mono) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1707 | with card show False by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1708 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1709 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1710 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1711 | |
| 63404 | 1712 | text \<open>The form of a finite set of given cardinality\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1713 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1714 | lemma card_eq_SucD: | 
| 63404 | 1715 | assumes "card A = Suc k" | 
| 1716 |   shows "\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {})"
 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1717 | proof - | 
| 63404 | 1718 | have fin: "finite A" | 
| 1719 | using assms by (auto intro: ccontr) | |
| 1720 | moreover have "card A \<noteq> 0" | |
| 1721 | using assms by auto | |
| 1722 | ultimately obtain b where b: "b \<in> A" | |
| 1723 | by auto | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1724 | show ?thesis | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1725 | proof (intro exI conjI) | 
| 63404 | 1726 |     show "A = insert b (A - {b})"
 | 
| 1727 | using b by blast | |
| 1728 |     show "b \<notin> A - {b}"
 | |
| 1729 | by blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1730 |     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
 | 
| 63612 | 1731 | using assms b fin by (fastforce dest: mk_disjoint_insert)+ | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1732 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1733 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1734 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1735 | lemma card_Suc_eq: | 
| 63404 | 1736 | "card A = Suc k \<longleftrightarrow> | 
| 1737 |     (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {}))"
 | |
| 1738 | apply (auto elim!: card_eq_SucD) | |
| 1739 | apply (subst card.insert) | |
| 63612 | 1740 | apply (auto simp add: intro:ccontr) | 
| 63404 | 1741 | done | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1742 | |
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1743 | lemma card_1_singletonE: | 
| 63404 | 1744 | assumes "card A = 1" | 
| 1745 |   obtains x where "A = {x}"
 | |
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1746 | using assms by (auto simp: card_Suc_eq) | 
| 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1747 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1748 | lemma is_singleton_altdef: "is_singleton A \<longleftrightarrow> card A = 1" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1749 | unfolding is_singleton_def | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1750 | by (auto elim!: card_1_singletonE is_singletonE simp del: One_nat_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1751 | |
| 63404 | 1752 | lemma card_le_Suc_iff: | 
| 1753 | "finite A \<Longrightarrow> Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)" | |
| 1754 | by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff | |
| 1755 | dest: subset_singletonD split: nat.splits if_splits) | |
| 44744 | 1756 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1757 | lemma finite_fun_UNIVD2: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1758 |   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
 | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1759 | shows "finite (UNIV :: 'b set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1760 | proof - | 
| 63404 | 1761 | from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))" for arbitrary | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1762 | by (rule finite_imageI) | 
| 63404 | 1763 | moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)" for arbitrary | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1764 | by (rule UNIV_eq_I) auto | 
| 63404 | 1765 | ultimately show "finite (UNIV :: 'b set)" | 
| 1766 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1767 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1768 | |
| 48063 
f02b4302d5dd
remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
 huffman parents: 
47221diff
changeset | 1769 | lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1770 | unfolding UNIV_unit by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1771 | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1772 | lemma infinite_arbitrarily_large: | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1773 | assumes "\<not> finite A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1774 | shows "\<exists>B. finite B \<and> card B = n \<and> B \<subseteq> A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1775 | proof (induction n) | 
| 63404 | 1776 | case 0 | 
| 1777 |   show ?case by (intro exI[of _ "{}"]) auto
 | |
| 1778 | next | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1779 | case (Suc n) | 
| 63404 | 1780 | then obtain B where B: "finite B \<and> card B = n \<and> B \<subseteq> A" .. | 
| 60758 | 1781 | with \<open>\<not> finite A\<close> have "A \<noteq> B" by auto | 
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1782 | with B have "B \<subset> A" by auto | 
| 63404 | 1783 | then have "\<exists>x. x \<in> A - B" | 
| 1784 | by (elim psubset_imp_ex_mem) | |
| 1785 | then obtain x where x: "x \<in> A - B" .. | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1786 | with B have "finite (insert x B) \<and> card (insert x B) = Suc n \<and> insert x B \<subseteq> A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1787 | by auto | 
| 63404 | 1788 | then show "\<exists>B. finite B \<and> card B = Suc n \<and> B \<subseteq> A" .. | 
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1789 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1790 | |
| 67457 | 1791 | text \<open>Sometimes, to prove that a set is finite, it is convenient to work with finite subsets | 
| 1792 | and to show that their cardinalities are uniformly bounded. This possibility is formalized in | |
| 1793 | the next criterion.\<close> | |
| 1794 | ||
| 1795 | lemma finite_if_finite_subsets_card_bdd: | |
| 1796 | assumes "\<And>G. G \<subseteq> F \<Longrightarrow> finite G \<Longrightarrow> card G \<le> C" | |
| 1797 | shows "finite F \<and> card F \<le> C" | |
| 1798 | proof (cases "finite F") | |
| 1799 | case False | |
| 1800 | obtain n::nat where n: "n > max C 0" by auto | |
| 1801 | obtain G where G: "G \<subseteq> F" "card G = n" using infinite_arbitrarily_large[OF False] by auto | |
| 1802 | hence "finite G" using \<open>n > max C 0\<close> using card_infinite gr_implies_not0 by blast | |
| 1803 | hence False using assms G n not_less by auto | |
| 1804 | thus ?thesis .. | |
| 1805 | next | |
| 1806 | case True thus ?thesis using assms[of F] by auto | |
| 1807 | qed | |
| 1808 | ||
| 63404 | 1809 | |
| 60758 | 1810 | subsubsection \<open>Cardinality of image\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1811 | |
| 63404 | 1812 | lemma card_image_le: "finite A \<Longrightarrow> card (f ` A) \<le> card A" | 
| 54570 | 1813 | by (induct rule: finite_induct) (simp_all add: le_SucI card_insert_if) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1814 | |
| 63915 | 1815 | lemma card_image: "inj_on f A \<Longrightarrow> card (f ` A) = card A" | 
| 1816 | proof (induct A rule: infinite_finite_induct) | |
| 1817 | case (infinite A) | |
| 1818 | then have "\<not> finite (f ` A)" by (auto dest: finite_imageD) | |
| 1819 | with infinite show ?case by simp | |
| 1820 | qed simp_all | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1821 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1822 | lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B" | 
| 63612 | 1823 | by (auto simp: card_image bij_betw_def) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1824 | |
| 63404 | 1825 | lemma endo_inj_surj: "finite A \<Longrightarrow> f ` A \<subseteq> A \<Longrightarrow> inj_on f A \<Longrightarrow> f ` A = A" | 
| 1826 | by (simp add: card_seteq card_image) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1827 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1828 | lemma eq_card_imp_inj_on: | 
| 63404 | 1829 | assumes "finite A" "card(f ` A) = card A" | 
| 1830 | shows "inj_on f A" | |
| 1831 | using assms | |
| 54570 | 1832 | proof (induct rule:finite_induct) | 
| 63404 | 1833 | case empty | 
| 1834 | show ?case by simp | |
| 54570 | 1835 | next | 
| 1836 | case (insert x A) | |
| 63404 | 1837 | then show ?case | 
| 1838 | using card_image_le [of A f] by (simp add: card_insert_if split: if_splits) | |
| 54570 | 1839 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1840 | |
| 63404 | 1841 | lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card (f ` A) = card A" | 
| 54570 | 1842 | by (blast intro: card_image eq_card_imp_inj_on) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1843 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1844 | lemma card_inj_on_le: | 
| 63404 | 1845 | assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" | 
| 1846 | shows "card A \<le> card B" | |
| 54570 | 1847 | proof - | 
| 63404 | 1848 | have "finite A" | 
| 1849 | using assms by (blast intro: finite_imageD dest: finite_subset) | |
| 1850 | then show ?thesis | |
| 1851 | using assms by (force intro: card_mono simp: card_image [symmetric]) | |
| 54570 | 1852 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1853 | |
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 1854 | lemma surj_card_le: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> card B \<le> card A" | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 1855 | by (blast intro: card_image_le card_mono le_trans) | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 1856 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1857 | lemma card_bij_eq: | 
| 63404 | 1858 | "inj_on f A \<Longrightarrow> f ` A \<subseteq> B \<Longrightarrow> inj_on g B \<Longrightarrow> g ` B \<subseteq> A \<Longrightarrow> finite A \<Longrightarrow> finite B | 
| 1859 | \<Longrightarrow> card A = card B" | |
| 1860 | by (auto intro: le_antisym card_inj_on_le) | |
| 1861 | ||
| 1862 | lemma bij_betw_finite: "bij_betw f A B \<Longrightarrow> finite A \<longleftrightarrow> finite B" | |
| 1863 | unfolding bij_betw_def using finite_imageD [of f A] by auto | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1864 | |
| 63404 | 1865 | lemma inj_on_finite: "inj_on f A \<Longrightarrow> f ` A \<le> B \<Longrightarrow> finite B \<Longrightarrow> finite A" | 
| 1866 | using finite_imageD finite_subset by blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1867 | |
| 63404 | 1868 | lemma card_vimage_inj: "inj f \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> card (f -` A) = card A" | 
| 1869 | by (auto 4 3 simp: subset_image_iff inj_vimage_image_eq | |
| 1870 | intro: card_image[symmetric, OF subset_inj_on]) | |
| 55020 | 1871 | |
| 41656 | 1872 | |
| 60758 | 1873 | subsubsection \<open>Pigeonhole Principles\<close> | 
| 37466 | 1874 | |
| 63404 | 1875 | lemma pigeonhole: "card A > card (f ` A) \<Longrightarrow> \<not> inj_on f A " | 
| 1876 | by (auto dest: card_image less_irrefl_nat) | |
| 37466 | 1877 | |
| 1878 | lemma pigeonhole_infinite: | |
| 63404 | 1879 | assumes "\<not> finite A" and "finite (f`A)" | 
| 1880 |   shows "\<exists>a0\<in>A. \<not> finite {a\<in>A. f a = f a0}"
 | |
| 1881 | using assms(2,1) | |
| 1882 | proof (induct "f`A" arbitrary: A rule: finite_induct) | |
| 1883 | case empty | |
| 1884 | then show ?case by simp | |
| 1885 | next | |
| 1886 | case (insert b F) | |
| 1887 | show ?case | |
| 1888 |   proof (cases "finite {a\<in>A. f a = b}")
 | |
| 1889 | case True | |
| 1890 |     with \<open>\<not> finite A\<close> have "\<not> finite (A - {a\<in>A. f a = b})"
 | |
| 1891 | by simp | |
| 1892 |     also have "A - {a\<in>A. f a = b} = {a\<in>A. f a \<noteq> b}"
 | |
| 1893 | by blast | |
| 1894 |     finally have "\<not> finite {a\<in>A. f a \<noteq> b}" .
 | |
| 1895 | from insert(3)[OF _ this] insert(2,4) show ?thesis | |
| 1896 | by simp (blast intro: rev_finite_subset) | |
| 37466 | 1897 | next | 
| 63404 | 1898 | case False | 
| 1899 |     then have "{a \<in> A. f a = b} \<noteq> {}" by force
 | |
| 1900 | with False show ?thesis by blast | |
| 37466 | 1901 | qed | 
| 1902 | qed | |
| 1903 | ||
| 1904 | lemma pigeonhole_infinite_rel: | |
| 63404 | 1905 | assumes "\<not> finite A" | 
| 1906 | and "finite B" | |
| 1907 | and "\<forall>a\<in>A. \<exists>b\<in>B. R a b" | |
| 1908 |   shows "\<exists>b\<in>B. \<not> finite {a:A. R a b}"
 | |
| 37466 | 1909 | proof - | 
| 63404 | 1910 |   let ?F = "\<lambda>a. {b\<in>B. R a b}"
 | 
| 1911 | from finite_Pow_iff[THEN iffD2, OF \<open>finite B\<close>] have "finite (?F ` A)" | |
| 1912 | by (blast intro: rev_finite_subset) | |
| 1913 | from pigeonhole_infinite [where f = ?F, OF assms(1) this] | |
| 63612 | 1914 |   obtain a0 where "a0 \<in> A" and infinite: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
 | 
| 63404 | 1915 | obtain b0 where "b0 \<in> B" and "R a0 b0" | 
| 1916 | using \<open>a0 \<in> A\<close> assms(3) by blast | |
| 63612 | 1917 |   have "finite {a\<in>A. ?F a = ?F a0}" if "finite {a\<in>A. R a b0}"
 | 
| 63404 | 1918 | using \<open>b0 \<in> B\<close> \<open>R a0 b0\<close> that by (blast intro: rev_finite_subset) | 
| 63612 | 1919 | with infinite \<open>b0 \<in> B\<close> show ?thesis | 
| 63404 | 1920 | by blast | 
| 37466 | 1921 | qed | 
| 1922 | ||
| 1923 | ||
| 60758 | 1924 | subsubsection \<open>Cardinality of sums\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1925 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1926 | lemma card_Plus: | 
| 63404 | 1927 | assumes "finite A" "finite B" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1928 | shows "card (A <+> B) = card A + card B" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1929 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1930 |   have "Inl`A \<inter> Inr`B = {}" by fast
 | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1931 | with assms show ?thesis | 
| 63404 | 1932 | by (simp add: Plus_def card_Un_disjoint card_image) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1933 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1934 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1935 | lemma card_Plus_conv_if: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1936 | "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1937 | by (auto simp add: card_Plus) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1938 | |
| 63404 | 1939 | text \<open>Relates to equivalence classes. Based on a theorem of F. Kammüller.\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1940 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1941 | lemma dvd_partition: | 
| 63404 | 1942 | assumes f: "finite (\<Union>C)" | 
| 1943 |     and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
 | |
| 1944 | shows "k dvd card (\<Union>C)" | |
| 54570 | 1945 | proof - | 
| 63404 | 1946 | have "finite C" | 
| 54570 | 1947 | by (rule finite_UnionD [OF f]) | 
| 63404 | 1948 | then show ?thesis | 
| 1949 | using assms | |
| 54570 | 1950 | proof (induct rule: finite_induct) | 
| 63404 | 1951 | case empty | 
| 1952 | show ?case by simp | |
| 54570 | 1953 | next | 
| 63404 | 1954 | case insert | 
| 1955 | then show ?case | |
| 54570 | 1956 | apply simp | 
| 1957 | apply (subst card_Un_disjoint) | |
| 63612 | 1958 | apply (auto simp add: disjoint_eq_subset_Compl) | 
| 54570 | 1959 | done | 
| 1960 | qed | |
| 1961 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1962 | |
| 63404 | 1963 | |
| 60758 | 1964 | subsubsection \<open>Relating injectivity and surjectivity\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1965 | |
| 63404 | 1966 | lemma finite_surj_inj: | 
| 1967 | assumes "finite A" "A \<subseteq> f ` A" | |
| 1968 | shows "inj_on f A" | |
| 54570 | 1969 | proof - | 
| 63404 | 1970 | have "f ` A = A" | 
| 54570 | 1971 | by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le) | 
| 1972 | then show ?thesis using assms | |
| 1973 | by (simp add: eq_card_imp_inj_on) | |
| 1974 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1975 | |
| 63612 | 1976 | lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f" | 
| 1977 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 1978 | by (blast intro: finite_surj_inj subset_UNIV) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1979 | |
| 63612 | 1980 | lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f" | 
| 1981 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 1982 | by (fastforce simp:surj_def dest!: endo_inj_surj) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1983 | |
| 63404 | 1984 | corollary infinite_UNIV_nat [iff]: "\<not> finite (UNIV :: nat set)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1985 | proof | 
| 51489 | 1986 | assume "finite (UNIV :: nat set)" | 
| 63404 | 1987 | with finite_UNIV_inj_surj [of Suc] show False | 
| 1988 | by simp (blast dest: Suc_neq_Zero surjD) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1989 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1990 | |
| 63404 | 1991 | lemma infinite_UNIV_char_0: "\<not> finite (UNIV :: 'a::semiring_char_0 set)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1992 | proof | 
| 51489 | 1993 | assume "finite (UNIV :: 'a set)" | 
| 1994 | with subset_UNIV have "finite (range of_nat :: 'a set)" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1995 | by (rule finite_subset) | 
| 51489 | 1996 | moreover have "inj (of_nat :: nat \<Rightarrow> 'a)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1997 | by (simp add: inj_on_def) | 
| 51489 | 1998 | ultimately have "finite (UNIV :: nat set)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1999 | by (rule finite_imageD) | 
| 51489 | 2000 | then show False | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2001 | by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2002 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2003 | |
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 2004 | hide_const (open) Finite_Set.fold | 
| 46033 | 2005 | |
| 61810 | 2006 | |
| 63404 | 2007 | subsection \<open>Infinite Sets\<close> | 
| 61810 | 2008 | |
| 2009 | text \<open> | |
| 2010 | Some elementary facts about infinite sets, mostly by Stephan Merz. | |
| 2011 | Beware! Because "infinite" merely abbreviates a negation, these | |
| 2012 | lemmas may not work well with \<open>blast\<close>. | |
| 2013 | \<close> | |
| 2014 | ||
| 2015 | abbreviation infinite :: "'a set \<Rightarrow> bool" | |
| 2016 | where "infinite S \<equiv> \<not> finite S" | |
| 2017 | ||
| 2018 | text \<open> | |
| 2019 | Infinite sets are non-empty, and if we remove some elements from an | |
| 2020 | infinite set, the result is still infinite. | |
| 2021 | \<close> | |
| 2022 | ||
| 2023 | lemma infinite_imp_nonempty: "infinite S \<Longrightarrow> S \<noteq> {}"
 | |
| 2024 | by auto | |
| 2025 | ||
| 2026 | lemma infinite_remove: "infinite S \<Longrightarrow> infinite (S - {a})"
 | |
| 2027 | by simp | |
| 2028 | ||
| 2029 | lemma Diff_infinite_finite: | |
| 63404 | 2030 | assumes "finite T" "infinite S" | 
| 61810 | 2031 | shows "infinite (S - T)" | 
| 63404 | 2032 | using \<open>finite T\<close> | 
| 61810 | 2033 | proof induct | 
| 63404 | 2034 |   from \<open>infinite S\<close> show "infinite (S - {})"
 | 
| 2035 | by auto | |
| 61810 | 2036 | next | 
| 2037 | fix T x | |
| 2038 | assume ih: "infinite (S - T)" | |
| 2039 |   have "S - (insert x T) = (S - T) - {x}"
 | |
| 2040 | by (rule Diff_insert) | |
| 63404 | 2041 | with ih show "infinite (S - (insert x T))" | 
| 61810 | 2042 | by (simp add: infinite_remove) | 
| 2043 | qed | |
| 2044 | ||
| 2045 | lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)" | |
| 2046 | by simp | |
| 2047 | ||
| 2048 | lemma infinite_Un: "infinite (S \<union> T) \<longleftrightarrow> infinite S \<or> infinite T" | |
| 2049 | by simp | |
| 2050 | ||
| 2051 | lemma infinite_super: | |
| 63404 | 2052 | assumes "S \<subseteq> T" | 
| 2053 | and "infinite S" | |
| 61810 | 2054 | shows "infinite T" | 
| 2055 | proof | |
| 2056 | assume "finite T" | |
| 63404 | 2057 | with \<open>S \<subseteq> T\<close> have "finite S" by (simp add: finite_subset) | 
| 2058 | with \<open>infinite S\<close> show False by simp | |
| 61810 | 2059 | qed | 
| 2060 | ||
| 2061 | proposition infinite_coinduct [consumes 1, case_names infinite]: | |
| 2062 | assumes "X A" | |
| 63404 | 2063 |     and step: "\<And>A. X A \<Longrightarrow> \<exists>x\<in>A. X (A - {x}) \<or> infinite (A - {x})"
 | 
| 61810 | 2064 | shows "infinite A" | 
| 2065 | proof | |
| 2066 | assume "finite A" | |
| 63404 | 2067 | then show False | 
| 2068 | using \<open>X A\<close> | |
| 61810 | 2069 | proof (induction rule: finite_psubset_induct) | 
| 2070 | case (psubset A) | |
| 2071 |     then obtain x where "x \<in> A" "X (A - {x}) \<or> infinite (A - {x})"
 | |
| 2072 | using local.step psubset.prems by blast | |
| 2073 |     then have "X (A - {x})"
 | |
| 2074 | using psubset.hyps by blast | |
| 2075 | show False | |
| 2076 |       apply (rule psubset.IH [where B = "A - {x}"])
 | |
| 63612 | 2077 | apply (use \<open>x \<in> A\<close> in blast) | 
| 63404 | 2078 |       apply (simp add: \<open>X (A - {x})\<close>)
 | 
| 2079 | done | |
| 61810 | 2080 | qed | 
| 2081 | qed | |
| 2082 | ||
| 2083 | text \<open> | |
| 2084 | For any function with infinite domain and finite range there is some | |
| 2085 | element that is the image of infinitely many domain elements. In | |
| 2086 | particular, any infinite sequence of elements from a finite set | |
| 2087 | contains some element that occurs infinitely often. | |
| 2088 | \<close> | |
| 2089 | ||
| 2090 | lemma inf_img_fin_dom': | |
| 63404 | 2091 | assumes img: "finite (f ` A)" | 
| 2092 | and dom: "infinite A" | |
| 61810 | 2093 |   shows "\<exists>y \<in> f ` A. infinite (f -` {y} \<inter> A)"
 | 
| 2094 | proof (rule ccontr) | |
| 2095 |   have "A \<subseteq> (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by auto
 | |
| 63404 | 2096 | moreover assume "\<not> ?thesis" | 
| 61810 | 2097 |   with img have "finite (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by blast
 | 
| 63404 | 2098 | ultimately have "finite A" by (rule finite_subset) | 
| 61810 | 2099 | with dom show False by contradiction | 
| 2100 | qed | |
| 2101 | ||
| 2102 | lemma inf_img_fin_domE': | |
| 2103 | assumes "finite (f ` A)" and "infinite A" | |
| 2104 |   obtains y where "y \<in> f`A" and "infinite (f -` {y} \<inter> A)"
 | |
| 2105 | using assms by (blast dest: inf_img_fin_dom') | |
| 2106 | ||
| 2107 | lemma inf_img_fin_dom: | |
| 2108 | assumes img: "finite (f`A)" and dom: "infinite A" | |
| 2109 |   shows "\<exists>y \<in> f`A. infinite (f -` {y})"
 | |
| 63404 | 2110 | using inf_img_fin_dom'[OF assms] by auto | 
| 61810 | 2111 | |
| 2112 | lemma inf_img_fin_domE: | |
| 2113 | assumes "finite (f`A)" and "infinite A" | |
| 2114 |   obtains y where "y \<in> f`A" and "infinite (f -` {y})"
 | |
| 2115 | using assms by (blast dest: inf_img_fin_dom) | |
| 2116 | ||
| 63404 | 2117 | proposition finite_image_absD: "finite (abs ` S) \<Longrightarrow> finite S" | 
| 2118 | for S :: "'a::linordered_ring set" | |
| 61810 | 2119 | by (rule ccontr) (auto simp: abs_eq_iff vimage_def dest: inf_img_fin_dom) | 
| 2120 | ||
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2121 | end |