| author | wenzelm | 
| Mon, 24 Jun 2019 16:26:25 +0200 | |
| changeset 70359 | 470d4f145e4c | 
| parent 70178 | 4900351361b0 | 
| child 70723 | 4e39d87c9737 | 
| 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 | |
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 165 | subsection \<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 | |
| 69275 | 259 | lemma finite_UN [simp]: "finite A \<Longrightarrow> finite (\<Union>(B ` A)) \<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})"
 | |
| 69286 | 294 | by (simp add: inj_on_image_set_diff) | 
| 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 | |
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 322 | by (clarsimp simp del: image_insert simp add: image_insert [symmetric]) blast | 
| 41656 | 323 | qed | 
| 324 | ||
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 325 | lemma all_subset_image: "(\<forall>B. B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. B \<subseteq> A \<longrightarrow> P(f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 326 | by (safe elim!: subset_imageE) (use image_mono in \<open>blast+\<close>) (* slow *) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 327 | |
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 328 | lemma all_finite_subset_image: | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 329 | "(\<forall>B. finite B \<and> B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 330 | proof safe | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 331 | fix B :: "'a set" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 332 | assume B: "finite B" "B \<subseteq> f ` A" and P: "\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B)" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 333 | show "P B" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 334 | using finite_subset_image [OF B] P by blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 335 | qed blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 336 | |
| 70178 
4900351361b0
Lindelöf spaces and supporting material
 paulson <lp15@cam.ac.uk> parents: 
70019diff
changeset | 337 | lemma ex_finite_subset_image: | 
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 338 | "(\<exists>B. finite B \<and> B \<subseteq> f ` A \<and> P B) \<longleftrightarrow> (\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 339 | proof safe | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 340 | fix B :: "'a set" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 341 | assume B: "finite B" "B \<subseteq> f ` A" and "P B" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 342 | show "\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B)" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 343 | using finite_subset_image [OF B] \<open>P B\<close> by blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 344 | qed blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 345 | |
| 63404 | 346 | lemma finite_vimage_IntI: "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)" | 
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 347 | proof (induct rule: finite_induct) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 348 | case (insert x F) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 349 | then show ?case | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 350 | by (simp add: vimage_insert [of h x F] finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 351 | qed simp | 
| 13825 | 352 | |
| 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 | 353 | lemma finite_finite_vimage_IntI: | 
| 63612 | 354 | assumes "finite F" | 
| 355 |     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 | 356 | 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 | 357 | 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 | 358 |   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 | 359 | 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 | 360 | 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 | 361 | 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 | 362 | 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 | 363 | |
| 63404 | 364 | lemma finite_vimageI: "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)" | 
| 43991 | 365 | using finite_vimage_IntI[of F h UNIV] by auto | 
| 366 | ||
| 63404 | 367 | lemma finite_vimageD': "finite (f -` A) \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> finite A" | 
| 368 | 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 | 369 | |
| 63404 | 370 | lemma finite_vimageD: "finite (h -` F) \<Longrightarrow> surj h \<Longrightarrow> finite F" | 
| 371 | by (auto dest: finite_vimageD') | |
| 34111 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 372 | |
| 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 373 | 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 | 374 | 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 | 375 | |
| 41656 | 376 | lemma finite_Collect_bex [simp]: | 
| 377 | assumes "finite A" | |
| 378 |   shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
 | |
| 379 | proof - | |
| 380 |   have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
 | |
| 381 | with assms show ?thesis by simp | |
| 382 | qed | |
| 12396 | 383 | |
| 41656 | 384 | lemma finite_Collect_bounded_ex [simp]: | 
| 385 |   assumes "finite {y. P y}"
 | |
| 386 |   shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
 | |
| 387 | proof - | |
| 63404 | 388 |   have "{x. \<exists>y. P y \<and> Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})"
 | 
| 389 | by auto | |
| 390 | with assms show ?thesis | |
| 391 | by simp | |
| 41656 | 392 | qed | 
| 29920 | 393 | |
| 63404 | 394 | lemma finite_Plus: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)" | 
| 41656 | 395 | by (simp add: Plus_def) | 
| 17022 | 396 | |
| 63404 | 397 | lemma finite_PlusD: | 
| 31080 | 398 | fixes A :: "'a set" and B :: "'b set" | 
| 399 | assumes fin: "finite (A <+> B)" | |
| 400 | shows "finite A" "finite B" | |
| 401 | proof - | |
| 63404 | 402 | have "Inl ` A \<subseteq> A <+> B" | 
| 403 | by auto | |
| 404 |   then have "finite (Inl ` A :: ('a + 'b) set)"
 | |
| 405 | using fin by (rule finite_subset) | |
| 406 | then show "finite A" | |
| 407 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 408 | next | 
| 63404 | 409 | have "Inr ` B \<subseteq> A <+> B" | 
| 410 | by auto | |
| 411 |   then have "finite (Inr ` B :: ('a + 'b) set)"
 | |
| 412 | using fin by (rule finite_subset) | |
| 413 | then show "finite B" | |
| 414 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 415 | qed | 
| 416 | ||
| 63404 | 417 | lemma finite_Plus_iff [simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B" | 
| 41656 | 418 | by (auto intro: finite_PlusD finite_Plus) | 
| 31080 | 419 | |
| 41656 | 420 | lemma finite_Plus_UNIV_iff [simp]: | 
| 421 |   "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | |
| 422 | by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff) | |
| 12396 | 423 | |
| 40786 
0a54cfc9add3
gave more standard finite set rules simp and intro attribute
 nipkow parents: 
40716diff
changeset | 424 | lemma finite_SigmaI [simp, intro]: | 
| 63404 | 425 | "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (SIGMA a:A. B a)" | 
| 426 | unfolding Sigma_def by blast | |
| 12396 | 427 | |
| 51290 | 428 | lemma finite_SigmaI2: | 
| 429 |   assumes "finite {x\<in>A. B x \<noteq> {}}"
 | |
| 430 | and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)" | |
| 431 | shows "finite (Sigma A B)" | |
| 432 | proof - | |
| 63404 | 433 |   from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)"
 | 
| 434 | by auto | |
| 435 |   also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B"
 | |
| 436 | by auto | |
| 51290 | 437 | finally show ?thesis . | 
| 438 | qed | |
| 439 | ||
| 63404 | 440 | lemma finite_cartesian_product: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)" | 
| 15402 | 441 | by (rule finite_SigmaI) | 
| 442 | ||
| 12396 | 443 | lemma finite_Prod_UNIV: | 
| 41656 | 444 |   "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
 | 
| 445 | by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product) | |
| 12396 | 446 | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 447 | lemma finite_cartesian_productD1: | 
| 42207 | 448 |   assumes "finite (A \<times> B)" and "B \<noteq> {}"
 | 
| 449 | shows "finite A" | |
| 450 | proof - | |
| 451 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 452 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 453 |   then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}"
 | 
| 454 | by simp | |
| 60758 | 455 |   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 | 456 | by (simp add: image_comp) | 
| 63404 | 457 |   then have "\<exists>n f. A = f ` {i::nat. i < n}"
 | 
| 458 | by blast | |
| 42207 | 459 | then show ?thesis | 
| 460 | by (auto simp add: finite_conv_nat_seg_image) | |
| 461 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 462 | |
| 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 463 | lemma finite_cartesian_productD2: | 
| 42207 | 464 |   assumes "finite (A \<times> B)" and "A \<noteq> {}"
 | 
| 465 | shows "finite B" | |
| 466 | proof - | |
| 467 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 468 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 469 |   then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}"
 | 
| 470 | by simp | |
| 60758 | 471 |   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 | 472 | by (simp add: image_comp) | 
| 63404 | 473 |   then have "\<exists>n f. B = f ` {i::nat. i < n}"
 | 
| 474 | by blast | |
| 42207 | 475 | then show ?thesis | 
| 476 | by (auto simp add: finite_conv_nat_seg_image) | |
| 477 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 478 | |
| 57025 | 479 | lemma finite_cartesian_product_iff: | 
| 480 |   "finite (A \<times> B) \<longleftrightarrow> (A = {} \<or> B = {} \<or> (finite A \<and> finite B))"
 | |
| 481 | by (auto dest: finite_cartesian_productD1 finite_cartesian_productD2 finite_cartesian_product) | |
| 482 | ||
| 63404 | 483 | lemma finite_prod: | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 484 |   "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | 
| 57025 | 485 | 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 | 486 | |
| 63404 | 487 | lemma finite_Pow_iff [iff]: "finite (Pow A) \<longleftrightarrow> finite A" | 
| 12396 | 488 | proof | 
| 489 | assume "finite (Pow A)" | |
| 63404 | 490 |   then have "finite ((\<lambda>x. {x}) ` A)"
 | 
| 63612 | 491 | by (blast intro: finite_subset) (* somewhat slow *) | 
| 63404 | 492 | then show "finite A" | 
| 493 | by (rule finite_imageD [unfolded inj_on_def]) simp | |
| 12396 | 494 | next | 
| 495 | assume "finite A" | |
| 41656 | 496 | then show "finite (Pow A)" | 
| 35216 | 497 | by induct (simp_all add: Pow_insert) | 
| 12396 | 498 | qed | 
| 499 | ||
| 63404 | 500 | corollary finite_Collect_subsets [simp, intro]: "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
 | 
| 41656 | 501 | by (simp add: Pow_def [symmetric]) | 
| 29918 | 502 | |
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 503 | lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)" | 
| 63404 | 504 | 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 | 505 | |
| 63404 | 506 | lemma finite_UnionD: "finite (\<Union>A) \<Longrightarrow> finite A" | 
| 41656 | 507 | by (blast intro: finite_subset [OF subset_Pow_Union]) | 
| 15392 | 508 | |
| 67511 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 509 | lemma finite_bind: | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 510 | assumes "finite S" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 511 | assumes "\<forall>x \<in> S. finite (f x)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 512 | shows "finite (Set.bind S f)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 513 | using assms by (simp add: bind_UNION) | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 514 | |
| 68463 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 515 | 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 | 516 | unfolding Set.filter_def by simp | 
| 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 517 | |
| 63404 | 518 | lemma finite_set_of_finite_funs: | 
| 519 | assumes "finite A" "finite B" | |
| 520 |   shows "finite {f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
 | |
| 521 | proof - | |
| 53820 | 522 |   let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
 | 
| 63404 | 523 | have "?F ` ?S \<subseteq> Pow(A \<times> B)" | 
| 524 | by auto | |
| 525 | from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" | |
| 526 | by simp | |
| 53820 | 527 | have 2: "inj_on ?F ?S" | 
| 63612 | 528 | by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff) (* somewhat slow *) | 
| 63404 | 529 | show ?thesis | 
| 530 | by (rule finite_imageD [OF 1 2]) | |
| 53820 | 531 | qed | 
| 15392 | 532 | |
| 58195 | 533 | lemma not_finite_existsD: | 
| 534 |   assumes "\<not> finite {a. P a}"
 | |
| 535 | shows "\<exists>a. P a" | |
| 536 | proof (rule classical) | |
| 63404 | 537 | assume "\<not> ?thesis" | 
| 58195 | 538 | with assms show ?thesis by auto | 
| 539 | qed | |
| 540 | ||
| 541 | ||
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 542 | subsection \<open>Further induction rules on finite sets\<close> | 
| 41656 | 543 | |
| 544 | lemma finite_ne_induct [case_names singleton insert, consumes 2]: | |
| 545 |   assumes "finite F" and "F \<noteq> {}"
 | |
| 546 |   assumes "\<And>x. P {x}"
 | |
| 547 |     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
 | |
| 548 | shows "P F" | |
| 63404 | 549 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 550 | proof induct | 
| 63404 | 551 | case empty | 
| 552 | then show ?case by simp | |
| 41656 | 553 | next | 
| 63404 | 554 | case (insert x F) | 
| 555 | then show ?case by cases auto | |
| 41656 | 556 | qed | 
| 557 | ||
| 558 | lemma finite_subset_induct [consumes 2, case_names empty insert]: | |
| 559 | assumes "finite F" and "F \<subseteq> A" | |
| 63612 | 560 |     and empty: "P {}"
 | 
| 41656 | 561 | and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)" | 
| 562 | shows "P F" | |
| 63404 | 563 | 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 | 564 | proof induct | 
| 41656 | 565 |   show "P {}" by fact
 | 
| 31441 | 566 | next | 
| 41656 | 567 | fix x F | 
| 63404 | 568 | assume "finite F" and "x \<notin> F" and P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | 
| 41656 | 569 | show "P (insert x F)" | 
| 570 | proof (rule insert) | |
| 571 | from i show "x \<in> A" by blast | |
| 572 | from i have "F \<subseteq> A" by blast | |
| 573 | with P show "P F" . | |
| 574 | show "finite F" by fact | |
| 575 | show "x \<notin> F" by fact | |
| 576 | qed | |
| 577 | qed | |
| 578 | ||
| 579 | lemma finite_empty_induct: | |
| 580 | assumes "finite A" | |
| 63612 | 581 | and "P A" | 
| 41656 | 582 |     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
 | 
| 583 |   shows "P {}"
 | |
| 584 | proof - | |
| 63404 | 585 | have "P (A - B)" if "B \<subseteq> A" for B :: "'a set" | 
| 41656 | 586 | proof - | 
| 63404 | 587 | from \<open>finite A\<close> that have "finite B" | 
| 588 | by (rule rev_finite_subset) | |
| 60758 | 589 | from this \<open>B \<subseteq> A\<close> show "P (A - B)" | 
| 41656 | 590 | proof induct | 
| 591 | case empty | |
| 60758 | 592 | from \<open>P A\<close> show ?case by simp | 
| 41656 | 593 | next | 
| 594 | case (insert b B) | |
| 595 |       have "P (A - B - {b})"
 | |
| 596 | proof (rule remove) | |
| 63404 | 597 | from \<open>finite A\<close> show "finite (A - B)" | 
| 598 | by induct auto | |
| 599 | from insert show "b \<in> A - B" | |
| 600 | by simp | |
| 601 | from insert show "P (A - B)" | |
| 602 | by simp | |
| 41656 | 603 | qed | 
| 63404 | 604 |       also have "A - B - {b} = A - insert b B"
 | 
| 605 | by (rule Diff_insert [symmetric]) | |
| 41656 | 606 | finally show ?case . | 
| 607 | qed | |
| 608 | qed | |
| 609 | then have "P (A - A)" by blast | |
| 610 | then show ?thesis by simp | |
| 31441 | 611 | qed | 
| 612 | ||
| 58195 | 613 | lemma finite_update_induct [consumes 1, case_names const update]: | 
| 614 |   assumes finite: "finite {a. f a \<noteq> c}"
 | |
| 63404 | 615 | and const: "P (\<lambda>a. c)" | 
| 616 |     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 | 617 | shows "P f" | 
| 63404 | 618 | using finite | 
| 619 | proof (induct "{a. f a \<noteq> c}" arbitrary: f)
 | |
| 620 | case empty | |
| 621 | with const show ?case by simp | |
| 58195 | 622 | next | 
| 623 | case (insert a A) | |
| 624 |   then have "A = {a'. (f(a := c)) a' \<noteq> c}" and "f a \<noteq> c"
 | |
| 625 | by auto | |
| 60758 | 626 |   with \<open>finite A\<close> have "finite {a'. (f(a := c)) a' \<noteq> c}"
 | 
| 58195 | 627 | by simp | 
| 628 | have "(f(a := c)) a = c" | |
| 629 | by simp | |
| 60758 | 630 |   from insert \<open>A = {a'. (f(a := c)) a' \<noteq> c}\<close> have "P (f(a := c))"
 | 
| 58195 | 631 | by simp | 
| 63404 | 632 |   with \<open>finite {a'. (f(a := c)) a' \<noteq> c}\<close> \<open>(f(a := c)) a = c\<close> \<open>f a \<noteq> c\<close>
 | 
| 633 | have "P ((f(a := c))(a := f a))" | |
| 58195 | 634 | by (rule update) | 
| 635 | then show ?case by simp | |
| 636 | qed | |
| 637 | ||
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 638 | lemma finite_subset_induct' [consumes 2, case_names empty insert]: | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 639 | assumes "finite F" and "F \<subseteq> A" | 
| 63612 | 640 |     and empty: "P {}"
 | 
| 641 | 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 | 642 | shows "P F" | 
| 63915 | 643 | using assms(1,2) | 
| 644 | proof induct | |
| 645 |   show "P {}" by fact
 | |
| 646 | next | |
| 647 | fix x F | |
| 648 | assume "finite F" and "x \<notin> F" and | |
| 649 | P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | |
| 650 | show "P (insert x F)" | |
| 651 | proof (rule insert) | |
| 652 | from i show "x \<in> A" by blast | |
| 653 | from i have "F \<subseteq> A" by blast | |
| 654 | with P show "P F" . | |
| 655 | show "finite F" by fact | |
| 656 | show "x \<notin> F" by fact | |
| 657 | show "F \<subseteq> A" by fact | |
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 658 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 659 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 660 | |
| 58195 | 661 | |
| 61799 | 662 | 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 | 663 | |
| 63612 | 664 | class finite = | 
| 665 | assumes finite_UNIV: "finite (UNIV :: 'a set)" | |
| 27430 | 666 | begin | 
| 667 | ||
| 61076 | 668 | lemma finite [simp]: "finite (A :: 'a set)" | 
| 26441 | 669 | 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 | 670 | |
| 61076 | 671 | lemma finite_code [code]: "finite (A :: 'a set) \<longleftrightarrow> True" | 
| 40922 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 672 | by simp | 
| 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 673 | |
| 27430 | 674 | end | 
| 675 | ||
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 676 | instance prod :: (finite, finite) finite | 
| 61169 | 677 | by standard (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite) | 
| 26146 | 678 | |
| 63404 | 679 | lemma inj_graph: "inj (\<lambda>f. {(x, y). y = f x})"
 | 
| 680 | 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 | 681 | |
| 26146 | 682 | instance "fun" :: (finite, finite) finite | 
| 683 | proof | |
| 63404 | 684 |   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 | 685 | proof (rule finite_imageD) | 
| 63404 | 686 |     let ?graph = "\<lambda>f::'a \<Rightarrow> 'b. {(x, y). y = f x}"
 | 
| 687 | have "range ?graph \<subseteq> Pow UNIV" | |
| 688 | by simp | |
| 26792 | 689 |     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
 | 
| 690 | by (simp only: finite_Pow_iff finite) | |
| 691 | ultimately show "finite (range ?graph)" | |
| 692 | by (rule finite_subset) | |
| 63404 | 693 | show "inj ?graph" | 
| 694 | 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 | 695 | 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 | 696 | 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 | 697 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 698 | instance bool :: finite | 
| 61169 | 699 | by standard (simp add: UNIV_bool) | 
| 44831 | 700 | |
| 45962 | 701 | instance set :: (finite) finite | 
| 61169 | 702 | by standard (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite) | 
| 45962 | 703 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 704 | instance unit :: finite | 
| 61169 | 705 | by standard (simp add: UNIV_unit) | 
| 44831 | 706 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 707 | instance sum :: (finite, finite) finite | 
| 61169 | 708 | by standard (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite) | 
| 27981 | 709 | |
| 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 | 710 | |
| 60758 | 711 | subsection \<open>A basic fold functional for finite sets\<close> | 
| 15392 | 712 | |
| 60758 | 713 | text \<open>The intended behaviour is | 
| 63404 | 714 |   \<open>fold f z {x\<^sub>1, \<dots>, x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)\<close>
 | 
| 715 | if \<open>f\<close> is ``left-commutative'': | |
| 60758 | 716 | \<close> | 
| 15392 | 717 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 718 | locale comp_fun_commute = | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 719 | 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 | 720 | 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 | 721 | begin | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 722 | |
| 51489 | 723 | 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 | 724 | 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 | 725 | |
| 63404 | 726 | lemma commute_left_comp: "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)" | 
| 51489 | 727 | by (simp add: o_assoc comp_fun_commute) | 
| 728 | ||
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 729 | end | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 730 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 731 | inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
 | 
| 63404 | 732 | for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b | 
| 63612 | 733 | where | 
| 734 |     emptyI [intro]: "fold_graph f z {} z"
 | |
| 735 | | 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 | 736 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 737 | 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 | 738 | |
| 68521 | 739 | lemma fold_graph_closed_lemma: | 
| 740 | "fold_graph f z A x \<and> x \<in> B" | |
| 741 | if "fold_graph g z A x" | |
| 742 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 743 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 744 | "z \<in> B" | |
| 745 | using that(1-3) | |
| 746 | proof (induction rule: fold_graph.induct) | |
| 747 | case (insertI x A y) | |
| 748 | have "fold_graph f z A y" "y \<in> B" | |
| 749 | unfolding atomize_conj | |
| 750 | by (rule insertI.IH) (auto intro: insertI.prems) | |
| 751 | then have "g x y \<in> B" and f_eq: "f x y = g x y" | |
| 752 | by (auto simp: insertI.prems) | |
| 753 | moreover have "fold_graph f z (insert x A) (f x y)" | |
| 754 | by (rule fold_graph.insertI; fact) | |
| 755 | ultimately | |
| 756 | show ?case | |
| 757 | by (simp add: f_eq) | |
| 758 | qed (auto intro!: that) | |
| 759 | ||
| 760 | lemma fold_graph_closed_eq: | |
| 761 | "fold_graph f z A = fold_graph g z A" | |
| 762 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 763 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 764 | "z \<in> B" | |
| 765 | using fold_graph_closed_lemma[of f z A _ B g] fold_graph_closed_lemma[of g z A _ B f] that | |
| 766 | by auto | |
| 767 | ||
| 63404 | 768 | definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
 | 
| 769 | where "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)" | |
| 15392 | 770 | |
| 68521 | 771 | lemma fold_closed_eq: "fold f z A = fold g z A" | 
| 772 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 773 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 774 | "z \<in> B" | |
| 775 | unfolding Finite_Set.fold_def | |
| 776 | by (subst fold_graph_closed_eq[where B=B and g=g]) (auto simp: that) | |
| 777 | ||
| 63404 | 778 | text \<open> | 
| 779 | A tempting alternative for the definiens is | |
| 69593 | 780 | \<^term>\<open>if finite A then THE y. fold_graph f z A y else e\<close>. | 
| 63404 | 781 | It allows the removal of finiteness assumptions from the theorems | 
| 782 | \<open>fold_comm\<close>, \<open>fold_reindex\<close> and \<open>fold_distrib\<close>. | |
| 783 | The proofs become ugly. It is not worth the effort. (???) | |
| 784 | \<close> | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 785 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 786 | lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x" | 
| 63404 | 787 | by (induct rule: finite_induct) auto | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 788 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 789 | |
| 69593 | 790 | subsubsection \<open>From \<^const>\<open>fold_graph\<close> to \<^term>\<open>fold\<close>\<close> | 
| 15392 | 791 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 792 | 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 | 793 | 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 | 794 | |
| 51489 | 795 | lemma fold_graph_finite: | 
| 796 | assumes "fold_graph f z A y" | |
| 797 | shows "finite A" | |
| 798 | using assms by induct simp_all | |
| 799 | ||
| 36045 | 800 | lemma fold_graph_insertE_aux: | 
| 801 |   "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'"
 | |
| 802 | proof (induct set: fold_graph) | |
| 63404 | 803 | case emptyI | 
| 804 | then show ?case by simp | |
| 805 | next | |
| 806 | case (insertI x A y) | |
| 807 | show ?case | |
| 36045 | 808 | proof (cases "x = a") | 
| 63404 | 809 | case True | 
| 810 | with insertI show ?thesis by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 811 | next | 
| 63404 | 812 | case False | 
| 36045 | 813 |     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
 | 
| 814 | using insertI by auto | |
| 42875 | 815 | have "f x y = f a (f x y')" | 
| 36045 | 816 | unfolding y by (rule fun_left_comm) | 
| 42875 | 817 |     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
 | 
| 60758 | 818 | using y' and \<open>x \<noteq> a\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 819 | by (simp add: insert_Diff_if fold_graph.insertI) | 
| 63404 | 820 | ultimately show ?thesis | 
| 821 | by fast | |
| 15392 | 822 | qed | 
| 63404 | 823 | qed | 
| 36045 | 824 | |
| 825 | lemma fold_graph_insertE: | |
| 826 | assumes "fold_graph f z (insert x A) v" and "x \<notin> A" | |
| 827 | obtains y where "v = f x y" and "fold_graph f z A y" | |
| 63404 | 828 | 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 | 829 | |
| 63404 | 830 | lemma fold_graph_determ: "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x" | 
| 36045 | 831 | proof (induct arbitrary: y set: fold_graph) | 
| 63404 | 832 | case emptyI | 
| 833 | then show ?case by fast | |
| 834 | next | |
| 36045 | 835 | case (insertI x A y v) | 
| 60758 | 836 | from \<open>fold_graph f z (insert x A) v\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 837 | obtain y' where "v = f x y'" and "fold_graph f z A y'" | 
| 838 | by (rule fold_graph_insertE) | |
| 63404 | 839 | from \<open>fold_graph f z A y'\<close> have "y' = y" | 
| 840 | by (rule insertI) | |
| 841 | with \<open>v = f x y'\<close> show "v = f x y" | |
| 842 | by simp | |
| 843 | qed | |
| 15392 | 844 | |
| 63404 | 845 | lemma fold_equality: "fold_graph f z A y \<Longrightarrow> fold f z A = y" | 
| 51489 | 846 | by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite) | 
| 15392 | 847 | |
| 42272 | 848 | lemma fold_graph_fold: | 
| 849 | assumes "finite A" | |
| 850 | shows "fold_graph f z A (fold f z A)" | |
| 851 | proof - | |
| 63404 | 852 | from assms have "\<exists>x. fold_graph f z A x" | 
| 853 | by (rule finite_imp_fold_graph) | |
| 42272 | 854 | moreover note fold_graph_determ | 
| 63404 | 855 | ultimately have "\<exists>!x. fold_graph f z A x" | 
| 856 | by (rule ex_ex1I) | |
| 857 | then have "fold_graph f z A (The (fold_graph f z A))" | |
| 858 | by (rule theI') | |
| 859 | with assms show ?thesis | |
| 860 | by (simp add: fold_def) | |
| 42272 | 861 | qed | 
| 36045 | 862 | |
| 61799 | 863 | text \<open>The base case for \<open>fold\<close>:\<close> | 
| 15392 | 864 | |
| 63404 | 865 | lemma (in -) fold_infinite [simp]: "\<not> finite A \<Longrightarrow> fold f z A = z" | 
| 866 | by (auto simp: fold_def) | |
| 51489 | 867 | |
| 63404 | 868 | lemma (in -) fold_empty [simp]: "fold f z {} = z"
 | 
| 869 | by (auto simp: fold_def) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 870 | |
| 69593 | 871 | text \<open>The various recursion equations for \<^const>\<open>fold\<close>:\<close> | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 872 | |
| 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 | 873 | lemma fold_insert [simp]: | 
| 42875 | 874 | assumes "finite A" and "x \<notin> A" | 
| 875 | shows "fold f z (insert x A) = f x (fold f z A)" | |
| 876 | proof (rule fold_equality) | |
| 51489 | 877 | fix z | 
| 63404 | 878 | from \<open>finite A\<close> have "fold_graph f z A (fold f z A)" | 
| 879 | by (rule fold_graph_fold) | |
| 880 | with \<open>x \<notin> A\<close> have "fold_graph f z (insert x A) (f x (fold f z A))" | |
| 881 | by (rule fold_graph.insertI) | |
| 882 | then show "fold_graph f z (insert x A) (f x (fold f z A))" | |
| 883 | by simp | |
| 42875 | 884 | qed | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 885 | |
| 51489 | 886 | declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del] | 
| 61799 | 887 | \<comment> \<open>No more proofs involve these.\<close> | 
| 51489 | 888 | |
| 63404 | 889 | 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 | 890 | proof (induct rule: finite_induct) | 
| 63404 | 891 | case empty | 
| 892 | then show ?case by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 893 | next | 
| 63404 | 894 | case insert | 
| 895 | then show ?case | |
| 51489 | 896 | 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 | 897 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 898 | |
| 63404 | 899 | lemma fold_insert2: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | 
| 51489 | 900 | by (simp add: fold_fun_left_comm) | 
| 15392 | 901 | |
| 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 | 902 | lemma fold_rec: | 
| 42875 | 903 | assumes "finite A" and "x \<in> A" | 
| 904 |   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 | 905 | proof - | 
| 63404 | 906 |   have A: "A = insert x (A - {x})"
 | 
| 907 | using \<open>x \<in> A\<close> by blast | |
| 908 |   then have "fold f z A = fold f z (insert x (A - {x}))"
 | |
| 909 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 910 |   also have "\<dots> = f x (fold f z (A - {x}))"
 | 
| 60758 | 911 | by (rule fold_insert) (simp add: \<open>finite A\<close>)+ | 
| 15535 | 912 | finally show ?thesis . | 
| 913 | qed | |
| 914 | ||
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 915 | lemma fold_insert_remove: | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 916 | assumes "finite A" | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 917 |   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 | 918 | proof - | 
| 63404 | 919 | from \<open>finite A\<close> have "finite (insert x A)" | 
| 920 | by auto | |
| 921 | moreover have "x \<in> insert x A" | |
| 922 | by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 923 |   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 | 924 | by (rule fold_rec) | 
| 63404 | 925 | then show ?thesis | 
| 926 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 927 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 928 | |
| 57598 | 929 | lemma fold_set_union_disj: | 
| 930 |   assumes "finite A" "finite B" "A \<inter> B = {}"
 | |
| 931 | shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B" | |
| 63404 | 932 | using assms(2,1,3) by induct simp_all | 
| 57598 | 933 | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 934 | end | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 935 | |
| 69593 | 936 | text \<open>Other properties of \<^const>\<open>fold\<close>:\<close> | 
| 48619 | 937 | |
| 938 | lemma fold_image: | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 939 | assumes "inj_on g A" | 
| 51489 | 940 | 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 | 941 | proof (cases "finite A") | 
| 63404 | 942 | case False | 
| 943 | with assms show ?thesis | |
| 944 | by (auto dest: finite_imageD simp add: fold_def) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 945 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 946 | case True | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 947 | 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 | 948 | proof | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 949 | fix w | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 950 | 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 | 951 | proof | 
| 63404 | 952 | assume ?P | 
| 953 | then show ?Q | |
| 954 | using assms | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 955 | proof (induct "g ` A" w arbitrary: A) | 
| 63404 | 956 | case emptyI | 
| 957 | then show ?case by (auto intro: fold_graph.emptyI) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 958 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 959 | case (insertI x A r B) | 
| 63404 | 960 | from \<open>inj_on g B\<close> \<open>x \<notin> A\<close> \<open>insert x A = image g B\<close> obtain x' A' | 
| 961 | 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 | 962 | by (rule inj_img_insertE) | 
| 63404 | 963 | 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 | 964 | by (auto intro: insertI.hyps) | 
| 60758 | 965 | 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 | 966 | by (rule fold_graph.insertI) | 
| 63404 | 967 | then show ?case | 
| 968 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 969 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 970 | next | 
| 63404 | 971 | assume ?Q | 
| 972 | then show ?P | |
| 973 | using assms | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 974 | proof induct | 
| 63404 | 975 | case emptyI | 
| 976 | then show ?case | |
| 977 | by (auto intro: fold_graph.emptyI) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 978 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 979 | case (insertI x A r) | 
| 63404 | 980 | from \<open>x \<notin> A\<close> insertI.prems have "g x \<notin> g ` A" | 
| 981 | by auto | |
| 982 | moreover from insertI have "fold_graph f z (g ` A) r" | |
| 983 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 984 | 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 | 985 | by (rule fold_graph.insertI) | 
| 63404 | 986 | then show ?case | 
| 987 | by simp | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 988 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 989 | qed | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 990 | qed | 
| 63404 | 991 | with True assms show ?thesis | 
| 992 | by (auto simp add: fold_def) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 993 | qed | 
| 15392 | 994 | |
| 49724 | 995 | lemma fold_cong: | 
| 996 | assumes "comp_fun_commute f" "comp_fun_commute g" | |
| 63404 | 997 | and "finite A" | 
| 998 | and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x" | |
| 51489 | 999 | and "s = t" and "A = B" | 
| 1000 | shows "fold f s A = fold g t B" | |
| 49724 | 1001 | proof - | 
| 63404 | 1002 | have "fold f s A = fold g s A" | 
| 1003 | using \<open>finite A\<close> cong | |
| 1004 | proof (induct A) | |
| 1005 | case empty | |
| 1006 | then show ?case by simp | |
| 49724 | 1007 | next | 
| 63404 | 1008 | case insert | 
| 60758 | 1009 | interpret f: comp_fun_commute f by (fact \<open>comp_fun_commute f\<close>) | 
| 1010 | interpret g: comp_fun_commute g by (fact \<open>comp_fun_commute g\<close>) | |
| 49724 | 1011 | from insert show ?case by simp | 
| 1012 | qed | |
| 1013 | with assms show ?thesis by simp | |
| 1014 | qed | |
| 1015 | ||
| 1016 | ||
| 60758 | 1017 | text \<open>A simplified version for idempotent functions:\<close> | 
| 15480 | 1018 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1019 | locale comp_fun_idem = comp_fun_commute + | 
| 51489 | 1020 | 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 | 1021 | 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 | 1022 | |
| 42869 
43b0f61f56d0
use point-free characterization for locale fun_left_comm_idem
 haftmann parents: 
42809diff
changeset | 1023 | 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 | 1024 | 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 | 1025 | |
| 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 | 1026 | lemma fold_insert_idem: | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1027 | assumes fin: "finite A" | 
| 51489 | 1028 | shows "fold f z (insert x A) = f x (fold f z A)" | 
| 15480 | 1029 | proof cases | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1030 | assume "x \<in> A" | 
| 63404 | 1031 | then obtain B where "A = insert x B" and "x \<notin> B" | 
| 1032 | by (rule set_insert) | |
| 1033 | then show ?thesis | |
| 1034 | using assms by (simp add: comp_fun_idem fun_left_idem) | |
| 15480 | 1035 | next | 
| 63404 | 1036 | assume "x \<notin> A" | 
| 1037 | then show ?thesis | |
| 1038 | using assms by simp | |
| 15480 | 1039 | qed | 
| 1040 | ||
| 51489 | 1041 | 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 | 1042 | |
| 63404 | 1043 | lemma fold_insert_idem2: "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | 
| 51489 | 1044 | by (simp add: fold_fun_left_comm) | 
| 15484 | 1045 | |
| 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 | 1046 | 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 | 1047 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1048 | |
| 61799 | 1049 | 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 | 1050 | |
| 63404 | 1051 | lemma (in comp_fun_commute) comp_comp_fun_commute: "comp_fun_commute (f \<circ> g)" | 
| 1052 | by standard (simp_all add: comp_fun_commute) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1053 | |
| 63404 | 1054 | 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 | 1055 | 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 | 1056 | (simp_all add: comp_fun_idem) | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1057 | |
| 63404 | 1058 | 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 | 1059 | proof | 
| 63404 | 1060 | 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 | 1061 | proof (cases "x = y") | 
| 63404 | 1062 | case True | 
| 1063 | then show ?thesis by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1064 | next | 
| 63404 | 1065 | case False | 
| 1066 | show ?thesis | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1067 | proof (induct "g x" arbitrary: g) | 
| 63404 | 1068 | case 0 | 
| 1069 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1070 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1071 | case (Suc n g) | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1072 | 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 | 1073 | proof (induct "g y" arbitrary: g) | 
| 63404 | 1074 | case 0 | 
| 1075 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1076 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1077 | case (Suc n g) | 
| 63040 | 1078 | define h where "h z = g z - 1" for z | 
| 63404 | 1079 | with Suc have "n = h y" | 
| 1080 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1081 | 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 | 1082 | by auto | 
| 63404 | 1083 | from Suc h_def have "g y = Suc (h y)" | 
| 1084 | by simp | |
| 1085 | then show ?case | |
| 1086 | 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 | 1087 | qed | 
| 63040 | 1088 | define h where "h z = (if z = x then g x - 1 else g z)" for z | 
| 63404 | 1089 | with Suc have "n = h x" | 
| 1090 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1091 | 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 | 1092 | by auto | 
| 63404 | 1093 | with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" | 
| 1094 | by simp | |
| 1095 | from Suc h_def have "g x = Suc (h x)" | |
| 1096 | by simp | |
| 1097 | then show ?case | |
| 1098 | 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 | 1099 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1100 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1101 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1102 | |
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1103 | |
| 69593 | 1104 | subsubsection \<open>Expressing set operations via \<^const>\<open>fold\<close>\<close> | 
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1105 | |
| 63404 | 1106 | lemma comp_fun_commute_const: "comp_fun_commute (\<lambda>_. f)" | 
| 1107 | by standard rule | |
| 51489 | 1108 | |
| 63404 | 1109 | lemma comp_fun_idem_insert: "comp_fun_idem insert" | 
| 1110 | by standard auto | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1111 | |
| 63404 | 1112 | lemma comp_fun_idem_remove: "comp_fun_idem Set.remove" | 
| 1113 | by standard auto | |
| 31992 | 1114 | |
| 63404 | 1115 | lemma (in semilattice_inf) comp_fun_idem_inf: "comp_fun_idem inf" | 
| 1116 | by standard (auto simp add: inf_left_commute) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1117 | |
| 63404 | 1118 | lemma (in semilattice_sup) comp_fun_idem_sup: "comp_fun_idem sup" | 
| 1119 | by standard (auto simp add: sup_left_commute) | |
| 31992 | 1120 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1121 | lemma union_fold_insert: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1122 | assumes "finite A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1123 | shows "A \<union> B = fold insert B A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1124 | proof - | 
| 63404 | 1125 | interpret comp_fun_idem insert | 
| 1126 | by (fact comp_fun_idem_insert) | |
| 1127 | from \<open>finite A\<close> show ?thesis | |
| 1128 | by (induct A arbitrary: B) simp_all | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1129 | qed | 
| 31992 | 1130 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1131 | lemma minus_fold_remove: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1132 | assumes "finite A" | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1133 | shows "B - A = fold Set.remove B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1134 | proof - | 
| 63404 | 1135 | interpret comp_fun_idem Set.remove | 
| 1136 | by (fact comp_fun_idem_remove) | |
| 1137 | from \<open>finite A\<close> have "fold Set.remove B A = B - A" | |
| 63612 | 1138 | by (induct A arbitrary: B) auto (* slow *) | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1139 | then show ?thesis .. | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1140 | qed | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1141 | |
| 51489 | 1142 | lemma comp_fun_commute_filter_fold: | 
| 1143 | "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')" | |
| 63404 | 1144 | proof - | 
| 48619 | 1145 | interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert) | 
| 61169 | 1146 | show ?thesis by standard (auto simp: fun_eq_iff) | 
| 48619 | 1147 | qed | 
| 1148 | ||
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1149 | lemma Set_filter_fold: | 
| 48619 | 1150 | assumes "finite A" | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1151 |   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
 | 
| 63404 | 1152 | using assms | 
| 1153 | by induct | |
| 1154 | (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 | 1155 | |
| 63404 | 1156 | lemma inter_Set_filter: | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1157 | assumes "finite B" | 
| 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1158 | shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B" | 
| 63404 | 1159 | using assms | 
| 1160 | by induct (auto simp: Set.filter_def) | |
| 48619 | 1161 | |
| 1162 | lemma image_fold_insert: | |
| 1163 | assumes "finite A" | |
| 1164 |   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
 | |
| 1165 | proof - | |
| 63404 | 1166 | interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" | 
| 1167 | by standard auto | |
| 1168 | show ?thesis | |
| 1169 | using assms by (induct A) auto | |
| 48619 | 1170 | qed | 
| 1171 | ||
| 1172 | lemma Ball_fold: | |
| 1173 | assumes "finite A" | |
| 1174 | shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A" | |
| 1175 | proof - | |
| 63404 | 1176 | interpret comp_fun_commute "\<lambda>k s. s \<and> P k" | 
| 1177 | by standard auto | |
| 1178 | show ?thesis | |
| 1179 | using assms by (induct A) auto | |
| 48619 | 1180 | qed | 
| 1181 | ||
| 1182 | lemma Bex_fold: | |
| 1183 | assumes "finite A" | |
| 1184 | shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A" | |
| 1185 | proof - | |
| 63404 | 1186 | interpret comp_fun_commute "\<lambda>k s. s \<or> P k" | 
| 1187 | by standard auto | |
| 1188 | show ?thesis | |
| 1189 | using assms by (induct A) auto | |
| 48619 | 1190 | qed | 
| 1191 | ||
| 63404 | 1192 | lemma comp_fun_commute_Pow_fold: "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" | 
| 63612 | 1193 | by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast (* somewhat slow *) | 
| 48619 | 1194 | |
| 1195 | lemma Pow_fold: | |
| 1196 | assumes "finite A" | |
| 1197 |   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
 | |
| 1198 | proof - | |
| 63404 | 1199 | interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" | 
| 1200 | by (rule comp_fun_commute_Pow_fold) | |
| 1201 | show ?thesis | |
| 1202 | using assms by (induct A) (auto simp: Pow_insert) | |
| 48619 | 1203 | qed | 
| 1204 | ||
| 1205 | lemma fold_union_pair: | |
| 1206 | assumes "finite B" | |
| 1207 |   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
 | |
| 1208 | proof - | |
| 63404 | 1209 | interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" | 
| 1210 | by standard auto | |
| 1211 | show ?thesis | |
| 1212 | using assms by (induct arbitrary: A) simp_all | |
| 48619 | 1213 | qed | 
| 1214 | ||
| 63404 | 1215 | lemma comp_fun_commute_product_fold: | 
| 1216 | "finite B \<Longrightarrow> comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" | |
| 1217 | by standard (auto simp: fold_union_pair [symmetric]) | |
| 48619 | 1218 | |
| 1219 | lemma product_fold: | |
| 63404 | 1220 | assumes "finite A" "finite B" | 
| 51489 | 1221 |   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
 | 
| 63404 | 1222 | using assms unfolding Sigma_def | 
| 1223 | by (induct A) | |
| 1224 | (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair) | |
| 48619 | 1225 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1226 | context complete_lattice | 
| 31992 | 1227 | begin | 
| 1228 | ||
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1229 | lemma inf_Inf_fold_inf: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1230 | assumes "finite A" | 
| 51489 | 1231 | shows "inf (Inf A) B = fold inf B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1232 | proof - | 
| 63404 | 1233 | interpret comp_fun_idem inf | 
| 1234 | by (fact comp_fun_idem_inf) | |
| 1235 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1236 | 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 | 1237 | qed | 
| 31992 | 1238 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1239 | lemma sup_Sup_fold_sup: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1240 | assumes "finite A" | 
| 51489 | 1241 | shows "sup (Sup A) B = fold sup B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1242 | proof - | 
| 63404 | 1243 | interpret comp_fun_idem sup | 
| 1244 | by (fact comp_fun_idem_sup) | |
| 1245 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1246 | by (induct A arbitrary: B) (simp_all add: sup_commute fun_eq_iff) | |
| 31992 | 1247 | qed | 
| 1248 | ||
| 63404 | 1249 | lemma Inf_fold_inf: "finite A \<Longrightarrow> Inf A = fold inf top A" | 
| 1250 | 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 | 1251 | |
| 63404 | 1252 | lemma Sup_fold_sup: "finite A \<Longrightarrow> Sup A = fold sup bot A" | 
| 1253 | using sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2) | |
| 31992 | 1254 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1255 | lemma inf_INF_fold_inf: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1256 | assumes "finite A" | 
| 69275 | 1257 | shows "inf B (\<Sqinter>(f ` A)) = fold (inf \<circ> f) B A" (is "?inf = ?fold") | 
| 63404 | 1258 | proof - | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1259 | 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 | 1260 | interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1261 | from \<open>finite A\<close> have "?fold = ?inf" | 
| 1262 | by (induct A arbitrary: B) (simp_all add: inf_left_commute) | |
| 1263 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1264 | qed | 
| 31992 | 1265 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1266 | lemma sup_SUP_fold_sup: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1267 | assumes "finite A" | 
| 69275 | 1268 | shows "sup B (\<Squnion>(f ` A)) = fold (sup \<circ> f) B A" (is "?sup = ?fold") | 
| 63404 | 1269 | proof - | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1270 | 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 | 1271 | interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1272 | from \<open>finite A\<close> have "?fold = ?sup" | 
| 1273 | by (induct A arbitrary: B) (simp_all add: sup_left_commute) | |
| 1274 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1275 | qed | 
| 31992 | 1276 | |
| 69275 | 1277 | lemma INF_fold_inf: "finite A \<Longrightarrow> \<Sqinter>(f ` A) = fold (inf \<circ> f) top A" | 
| 63404 | 1278 | using inf_INF_fold_inf [of A top] by simp | 
| 31992 | 1279 | |
| 69275 | 1280 | lemma SUP_fold_sup: "finite A \<Longrightarrow> \<Squnion>(f ` A) = fold (sup \<circ> f) bot A" | 
| 63404 | 1281 | using sup_SUP_fold_sup [of A bot] by simp | 
| 31992 | 1282 | |
| 1283 | end | |
| 1284 | ||
| 1285 | ||
| 60758 | 1286 | 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 | 1287 | |
| 60758 | 1288 | subsubsection \<open>The natural case\<close> | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1289 | |
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1290 | locale folding = | 
| 63612 | 1291 | 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 | 1292 | 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 | 1293 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1294 | |
| 54870 | 1295 | interpretation fold?: comp_fun_commute f | 
| 63612 | 1296 | 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 | 1297 | |
| 51489 | 1298 | definition F :: "'a set \<Rightarrow> 'b" | 
| 63404 | 1299 | where eq_fold: "F A = fold f z A" | 
| 51489 | 1300 | |
| 61169 | 1301 | lemma empty [simp]:"F {} = z"
 | 
| 51489 | 1302 | by (simp add: eq_fold) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1303 | |
| 61169 | 1304 | lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F A = z" | 
| 51489 | 1305 | by (simp add: eq_fold) | 
| 63404 | 1306 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1307 | lemma insert [simp]: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1308 | assumes "finite A" and "x \<notin> A" | 
| 51489 | 1309 | shows "F (insert x A) = f x (F A)" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1310 | proof - | 
| 51489 | 1311 | from fold_insert assms | 
| 1312 | have "fold f z (insert x A) = f x (fold f z A)" by simp | |
| 60758 | 1313 | 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 | 1314 | qed | 
| 63404 | 1315 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1316 | lemma remove: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1317 | assumes "finite A" and "x \<in> A" | 
| 51489 | 1318 |   shows "F A = f x (F (A - {x}))"
 | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1319 | proof - | 
| 60758 | 1320 | 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 | 1321 | by (auto dest: mk_disjoint_insert) | 
| 60758 | 1322 | 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 | 1323 | ultimately show ?thesis by simp | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1324 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1325 | |
| 63404 | 1326 | lemma insert_remove: "finite A \<Longrightarrow> F (insert x A) = f x (F (A - {x}))"
 | 
| 1327 | 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 | 1328 | |
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33960diff
changeset | 1329 | end | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1330 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1331 | |
| 60758 | 1332 | subsubsection \<open>With idempotency\<close> | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1333 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1334 | locale folding_idem = folding + | 
| 51489 | 1335 | 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 | 1336 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1337 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1338 | declare insert [simp del] | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1339 | |
| 54870 | 1340 | interpretation fold?: comp_fun_idem f | 
| 61169 | 1341 | 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 | 1342 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1343 | lemma insert_idem [simp]: | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1344 | assumes "finite A" | 
| 51489 | 1345 | shows "F (insert x A) = f x (F A)" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1346 | proof - | 
| 51489 | 1347 | from fold_insert_idem assms | 
| 1348 | have "fold f z (insert x A) = f x (fold f z A)" by simp | |
| 60758 | 1349 | 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 | 1350 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1351 | |
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1352 | end | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1353 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1354 | |
| 60758 | 1355 | 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 | 1356 | |
| 60758 | 1357 | text \<open> | 
| 51489 | 1358 | The traditional definition | 
| 69593 | 1359 |   \<^prop>\<open>card A \<equiv> LEAST n. \<exists>f. A = {f i |i. i < n}\<close>
 | 
| 51489 | 1360 | is ugly to work with. | 
| 69593 | 1361 | But now that we have \<^const>\<open>fold\<close> things are easy: | 
| 60758 | 1362 | \<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1363 | |
| 61890 
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
 haftmann parents: 
61810diff
changeset | 1364 | global_interpretation card: folding "\<lambda>_. Suc" 0 | 
| 61778 | 1365 | defines card = "folding.F (\<lambda>_. Suc) 0" | 
| 1366 | by standard rule | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1367 | |
| 63404 | 1368 | lemma card_infinite: "\<not> finite A \<Longrightarrow> card A = 0" | 
| 51489 | 1369 | by (fact card.infinite) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1370 | |
| 63404 | 1371 | 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 | 1372 | by (fact card.empty) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1373 | |
| 63404 | 1374 | lemma card_insert_disjoint: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)" | 
| 51489 | 1375 | by (fact card.insert) | 
| 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_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 | 1378 | 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 | 1379 | |
| 63404 | 1380 | 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 | 1381 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1382 | |
| 63404 | 1383 | 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 | 1384 | 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 | 1385 | |
| 63404 | 1386 | 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 | 1387 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1388 | |
| 63404 | 1389 | 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 | 1390 | by auto | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1391 | |
| 63404 | 1392 | lemma card_range_greater_zero: "finite (range f) \<Longrightarrow> card (range f) > 0" | 
| 63365 | 1393 | by (rule ccontr) (simp add: card_eq_0_iff) | 
| 1394 | ||
| 63404 | 1395 | lemma card_gt_0_iff: "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
 | 
| 1396 | 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 | 1397 | |
| 63404 | 1398 | lemma card_Suc_Diff1: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
 | 
| 1399 | apply (rule insert_Diff [THEN subst, where t = A]) | |
| 63612 | 1400 | apply assumption | 
| 63404 | 1401 | apply (simp del: insert_Diff_single) | 
| 1402 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1403 | |
| 63404 | 1404 | lemma card_insert_le_m1: "n > 0 \<Longrightarrow> card y \<le> n - 1 \<Longrightarrow> card (insert x y) \<le> n" | 
| 60762 | 1405 | apply (cases "finite y") | 
| 63612 | 1406 | apply (cases "x \<in> y") | 
| 1407 | apply (auto simp: insert_absorb) | |
| 60762 | 1408 | done | 
| 1409 | ||
| 63404 | 1410 | lemma card_Diff_singleton: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
 | 
| 51489 | 1411 | 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 | 1412 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1413 | lemma card_Diff_singleton_if: | 
| 51489 | 1414 |   "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
 | 
| 1415 | 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 | 1416 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1417 | lemma card_Diff_insert[simp]: | 
| 51489 | 1418 | assumes "finite A" and "a \<in> A" and "a \<notin> B" | 
| 1419 | 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 | 1420 | proof - | 
| 63404 | 1421 |   have "A - insert a B = (A - B) - {a}"
 | 
| 1422 | using assms by blast | |
| 1423 | then show ?thesis | |
| 1424 | 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 | 1425 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1426 | |
| 63404 | 1427 | lemma card_insert: "finite A \<Longrightarrow> card (insert x A) = Suc (card (A - {x}))"
 | 
| 51489 | 1428 | 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 | 1429 | |
| 63404 | 1430 | lemma card_insert_le: "finite A \<Longrightarrow> card A \<le> card (insert x A)" | 
| 1431 | 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 | 1432 | |
| 63404 | 1433 | lemma card_Collect_less_nat[simp]: "card {i::nat. i < n} = n"
 | 
| 1434 | by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq) | |
| 41987 | 1435 | |
| 63404 | 1436 | lemma card_Collect_le_nat[simp]: "card {i::nat. i \<le> n} = Suc n"
 | 
| 1437 | using card_Collect_less_nat[of "Suc n"] by (simp add: less_Suc_eq_le) | |
| 41987 | 1438 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1439 | lemma card_mono: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1440 | 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 | 1441 | 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 | 1442 | proof - | 
| 63404 | 1443 | from assms have "finite A" | 
| 1444 | by (auto intro: finite_subset) | |
| 1445 | then show ?thesis | |
| 1446 | using assms | |
| 1447 | proof (induct A arbitrary: B) | |
| 1448 | case empty | |
| 1449 | 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 | 1450 | next | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1451 | case (insert x A) | 
| 63404 | 1452 | then have "x \<in> B" | 
| 1453 | by simp | |
| 1454 |     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})"
 | |
| 1455 | by auto | |
| 1456 |     with insert.hyps have "card A \<le> card (B - {x})"
 | |
| 1457 | by auto | |
| 1458 | with \<open>finite A\<close> \<open>x \<notin> A\<close> \<open>finite B\<close> \<open>x \<in> B\<close> show ?case | |
| 1459 | 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 | 1460 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1461 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1462 | |
| 63404 | 1463 | lemma card_seteq: "finite B \<Longrightarrow> (\<And>A. A \<subseteq> B \<Longrightarrow> card B \<le> card A \<Longrightarrow> A = B)" | 
| 1464 | apply (induct rule: finite_induct) | |
| 63612 | 1465 | apply simp | 
| 63404 | 1466 | apply clarify | 
| 1467 |   apply (subgoal_tac "finite A \<and> A - {x} \<subseteq> F")
 | |
| 1468 | prefer 2 apply (blast intro: finite_subset, atomize) | |
| 1469 |   apply (drule_tac x = "A - {x}" in spec)
 | |
| 63648 | 1470 | apply (simp add: card_Diff_singleton_if split: if_split_asm) | 
| 63404 | 1471 | apply (case_tac "card A", auto) | 
| 1472 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1473 | |
| 63404 | 1474 | lemma psubset_card_mono: "finite B \<Longrightarrow> A < B \<Longrightarrow> card A < card B" | 
| 1475 | apply (simp add: psubset_eq linorder_not_le [symmetric]) | |
| 1476 | apply (blast dest: card_seteq) | |
| 1477 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1478 | |
| 51489 | 1479 | lemma card_Un_Int: | 
| 63404 | 1480 | assumes "finite A" "finite B" | 
| 51489 | 1481 | shows "card A + card B = card (A \<union> B) + card (A \<inter> B)" | 
| 63404 | 1482 | using assms | 
| 1483 | proof (induct A) | |
| 1484 | case empty | |
| 1485 | then show ?case by simp | |
| 51489 | 1486 | next | 
| 63404 | 1487 | case insert | 
| 1488 | then show ?case | |
| 51489 | 1489 | by (auto simp add: insert_absorb Int_insert_left) | 
| 1490 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1491 | |
| 63404 | 1492 | lemma card_Un_disjoint: "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> card (A \<union> B) = card A + card B"
 | 
| 1493 | 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 | 1494 | |
| 59336 | 1495 | lemma card_Un_le: "card (A \<union> B) \<le> card A + card B" | 
| 63404 | 1496 | apply (cases "finite A") | 
| 1497 | apply (cases "finite B") | |
| 63612 | 1498 | apply (use le_iff_add card_Un_Int in blast) | 
| 63404 | 1499 | apply simp | 
| 1500 | apply simp | |
| 1501 | done | |
| 59336 | 1502 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1503 | lemma card_Diff_subset: | 
| 63404 | 1504 | assumes "finite B" | 
| 1505 | and "B \<subseteq> A" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1506 | shows "card (A - B) = card A - card B" | 
| 63915 | 1507 | using assms | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1508 | proof (cases "finite A") | 
| 63404 | 1509 | case False | 
| 1510 | with assms show ?thesis | |
| 1511 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1512 | next | 
| 63404 | 1513 | case True | 
| 1514 | with assms show ?thesis | |
| 1515 | 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 | 1516 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1517 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1518 | lemma card_Diff_subset_Int: | 
| 63404 | 1519 | assumes "finite (A \<inter> B)" | 
| 1520 | 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 | 1521 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1522 | have "A - B = A - A \<inter> B" by auto | 
| 63404 | 1523 | with assms show ?thesis | 
| 1524 | 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 | 1525 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1526 | |
| 40716 | 1527 | lemma diff_card_le_card_Diff: | 
| 63404 | 1528 | assumes "finite B" | 
| 1529 | shows "card A - card B \<le> card (A - B)" | |
| 1530 | proof - | |
| 40716 | 1531 | have "card A - card B \<le> card A - card (A \<inter> B)" | 
| 1532 | using card_mono[OF assms Int_lower2, of A] by arith | |
| 63404 | 1533 | also have "\<dots> = card (A - B)" | 
| 1534 | using assms by (simp add: card_Diff_subset_Int) | |
| 40716 | 1535 | finally show ?thesis . | 
| 1536 | qed | |
| 1537 | ||
| 69312 | 1538 | lemma card_le_sym_Diff: | 
| 1539 | assumes "finite A" "finite B" "card A \<le> card B" | |
| 1540 | shows "card(A - B) \<le> card(B - A)" | |
| 1541 | proof - | |
| 1542 | have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int) | |
| 1543 | also have "\<dots> \<le> card B - card (A \<inter> B)" using assms(3) by linarith | |
| 1544 | also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute) | |
| 1545 | finally show ?thesis . | |
| 1546 | qed | |
| 1547 | ||
| 1548 | lemma card_less_sym_Diff: | |
| 1549 | assumes "finite A" "finite B" "card A < card B" | |
| 1550 | shows "card(A - B) < card(B - A)" | |
| 1551 | proof - | |
| 1552 | have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int) | |
| 1553 | also have "\<dots> < card B - card (A \<inter> B)" using assms(1,3) by (simp add: card_mono diff_less_mono) | |
| 1554 | also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute) | |
| 1555 | finally show ?thesis . | |
| 1556 | qed | |
| 1557 | ||
| 63404 | 1558 | lemma card_Diff1_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) < card A"
 | 
| 1559 | 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 | 1560 | |
| 63404 | 1561 | lemma card_Diff2_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> card (A - {x} - {y}) < card A"
 | 
| 1562 | apply (cases "x = y") | |
| 1563 | apply (simp add: card_Diff1_less del:card_Diff_insert) | |
| 1564 | apply (rule less_trans) | |
| 1565 | prefer 2 apply (auto intro!: card_Diff1_less simp del: card_Diff_insert) | |
| 1566 | done | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1567 | |
| 63404 | 1568 | lemma card_Diff1_le: "finite A \<Longrightarrow> card (A - {x}) \<le> card A"
 | 
| 1569 | 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 | 1570 | |
| 63404 | 1571 | lemma card_psubset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> card A < card B \<Longrightarrow> A < B" | 
| 1572 | by (erule psubsetI) blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1573 | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1574 | 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 | 1575 | 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 | 1576 | 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 | 1577 | 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 | 1578 | 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 | 1579 | 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 | 1580 | 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 | 1581 | case empty | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1582 | 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 | 1583 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1584 | 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 | 1585 | then show ?case | 
| 63404 | 1586 | 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 | 1587 | case 1 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1588 | 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 | 1589 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1590 | 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 | 1591 | 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 | 1592 | by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1593 | 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 | 1594 | 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 | 1595 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1596 | 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 | 1597 | apply - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1598 | 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 | 1599 | 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 | 1600 | done | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1601 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1602 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1603 | |
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1604 | 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 | 1605 | 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 | 1606 | 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 | 1607 | 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 | 1608 | shows "A = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1609 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1610 | 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 | 1611 | 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 | 1612 | 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 | 1613 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1614 |   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 | 1615 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1616 | 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 | 1617 | 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 | 1618 | 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 | 1619 | by arith | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1620 |   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 | 1621 | 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 | 1622 | 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 | 1623 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1624 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1625 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1626 | lemma insert_partition: | 
| 63404 | 1627 |   "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 | 1628 | by auto (* somewhat slow *) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1629 | |
| 63404 | 1630 | lemma finite_psubset_induct [consumes 1, case_names psubset]: | 
| 1631 | assumes finite: "finite A" | |
| 1632 | 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 | 1633 | shows "P A" | 
| 63404 | 1634 | 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 | 1635 | 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 | 1636 | 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 | 1637 | have fin: "finite A" by fact | 
| 63404 | 1638 | have ih: "card B < card A \<Longrightarrow> finite B \<Longrightarrow> P B" for B by fact | 
| 1639 | have "P B" if "B \<subset> A" for B | |
| 1640 | proof - | |
| 1641 | from that have "card B < card A" | |
| 1642 | 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 | 1643 | moreover | 
| 63404 | 1644 | from that have "B \<subseteq> A" | 
| 1645 | by auto | |
| 1646 | then have "finite B" | |
| 1647 | using fin finite_subset by blast | |
| 1648 | ultimately show ?thesis using ih by simp | |
| 1649 | 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 | 1650 | 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 | 1651 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1652 | |
| 63404 | 1653 | 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 | 1654 | assumes "finite S" | 
| 63404 | 1655 |     and "P {}"
 | 
| 1656 | 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 | 1657 | shows "P S" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1658 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1659 | 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 | 1660 | 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 | 1661 | proof (induct rule: dec_induct) | 
| 63404 | 1662 |     case base with \<open>P {}\<close>
 | 
| 1663 | show ?case | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1664 |       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 | 1665 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1666 | case (step n) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1667 | 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 | 1668 | by auto | 
| 60758 | 1669 | 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 | 1670 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1671 | 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 | 1672 | by auto | 
| 60758 | 1673 | 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 | 1674 | 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 | 1675 | qed | 
| 60758 | 1676 | 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 | 1677 | 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 | 1678 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1679 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1680 | lemma remove_induct [case_names empty infinite remove]: | 
| 63404 | 1681 |   assumes empty: "P ({} :: 'a set)"
 | 
| 1682 | and infinite: "\<not> finite B \<Longrightarrow> P B" | |
| 1683 |     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 | 1684 | shows "P B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1685 | proof (cases "finite B") | 
| 63612 | 1686 | case False | 
| 63404 | 1687 | then show ?thesis by (rule infinite) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1688 | next | 
| 63612 | 1689 | case True | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1690 | define A where "A = B" | 
| 63612 | 1691 | with True have "finite A" "A \<subseteq> B" | 
| 1692 | by simp_all | |
| 63404 | 1693 | then show "P A" | 
| 1694 | proof (induct "card A" arbitrary: A) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1695 | case 0 | 
| 63404 | 1696 |     then have "A = {}" by auto
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1697 | with empty show ?case by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1698 | next | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1699 | case (Suc n A) | 
| 63404 | 1700 | from \<open>A \<subseteq> B\<close> and \<open>finite B\<close> have "finite A" | 
| 1701 | by (rule finite_subset) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1702 |     moreover from Suc.hyps have "A \<noteq> {}" by auto
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1703 | moreover note \<open>A \<subseteq> B\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1704 |     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 | 1705 | 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 | 1706 | ultimately show ?case by (rule remove) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1707 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1708 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1709 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1710 | lemma finite_remove_induct [consumes 1, case_names empty remove]: | 
| 63404 | 1711 | fixes P :: "'a set \<Rightarrow> bool" | 
| 63612 | 1712 | assumes "finite B" | 
| 1713 |     and "P {}"
 | |
| 1714 |     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 | 1715 | defines "B' \<equiv> B" | 
| 63404 | 1716 | shows "P B'" | 
| 1717 | 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 | 1718 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1719 | |
| 63404 | 1720 | 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 | 1721 | lemma card_partition [rule_format]: | 
| 63404 | 1722 | "finite C \<Longrightarrow> finite (\<Union>C) \<Longrightarrow> (\<forall>c\<in>C. card c = k) \<Longrightarrow> | 
| 1723 |     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}) \<Longrightarrow>
 | |
| 1724 | k * card C = card (\<Union>C)" | |
| 63612 | 1725 | proof (induct rule: finite_induct) | 
| 1726 | case empty | |
| 1727 | then show ?case by simp | |
| 1728 | next | |
| 1729 | case (insert x F) | |
| 1730 | then show ?case | |
| 1731 | by (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\<Union>(insert _ _)"]) | |
| 1732 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1733 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1734 | 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 | 1735 | assumes fin: "finite (UNIV :: 'a set)" | 
| 63404 | 1736 | 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 | 1737 | shows "A = (UNIV :: 'a set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1738 | proof | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1739 | 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 | 1740 | show "UNIV \<subseteq> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1741 | proof | 
| 63404 | 1742 | 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 | 1743 | proof (rule ccontr) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1744 | assume "x \<notin> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1745 | then have "A \<subset> UNIV" by auto | 
| 63404 | 1746 | with fin have "card A < card (UNIV :: 'a set)" | 
| 1747 | 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 | 1748 | 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 | 1749 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1750 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1751 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1752 | |
| 63404 | 1753 | 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 | 1754 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1755 | lemma card_eq_SucD: | 
| 63404 | 1756 | assumes "card A = Suc k" | 
| 1757 |   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 | 1758 | proof - | 
| 63404 | 1759 | have fin: "finite A" | 
| 1760 | using assms by (auto intro: ccontr) | |
| 1761 | moreover have "card A \<noteq> 0" | |
| 1762 | using assms by auto | |
| 1763 | ultimately obtain b where b: "b \<in> A" | |
| 1764 | by auto | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1765 | show ?thesis | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1766 | proof (intro exI conjI) | 
| 63404 | 1767 |     show "A = insert b (A - {b})"
 | 
| 1768 | using b by blast | |
| 1769 |     show "b \<notin> A - {b}"
 | |
| 1770 | by blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1771 |     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
 | 
| 63612 | 1772 | 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 | 1773 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1774 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1775 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1776 | lemma card_Suc_eq: | 
| 63404 | 1777 | "card A = Suc k \<longleftrightarrow> | 
| 1778 |     (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {}))"
 | |
| 1779 | apply (auto elim!: card_eq_SucD) | |
| 1780 | apply (subst card.insert) | |
| 63612 | 1781 | apply (auto simp add: intro:ccontr) | 
| 63404 | 1782 | done | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1783 | |
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1784 | lemma card_1_singletonE: | 
| 63404 | 1785 | assumes "card A = 1" | 
| 1786 |   obtains x where "A = {x}"
 | |
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1787 | using assms by (auto simp: card_Suc_eq) | 
| 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 1788 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1789 | 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 | 1790 | unfolding is_singleton_def | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 1791 | 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 | 1792 | |
| 69312 | 1793 | lemma card_le_Suc0_iff_eq: | 
| 1794 | assumes "finite A" | |
| 1795 | shows "card A \<le> Suc 0 \<longleftrightarrow> (\<forall>a1 \<in> A. \<forall>a2 \<in> A. a1 = a2)" (is "?C = ?A") | |
| 1796 | proof | |
| 1797 | assume ?C thus ?A using assms by (auto simp: le_Suc_eq dest: card_eq_SucD) | |
| 1798 | next | |
| 1799 | assume ?A | |
| 1800 | show ?C | |
| 1801 | proof cases | |
| 1802 |     assume "A = {}" thus ?C using \<open>?A\<close> by simp
 | |
| 1803 | next | |
| 1804 |     assume "A \<noteq> {}"
 | |
| 1805 |     then obtain a where "A = {a}" using \<open>?A\<close> by blast
 | |
| 1806 | thus ?C by simp | |
| 1807 | qed | |
| 1808 | qed | |
| 1809 | ||
| 63404 | 1810 | lemma card_le_Suc_iff: | 
| 69312 | 1811 | "Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)" | 
| 1812 | apply(cases "finite A") | |
| 1813 | apply (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff | |
| 63404 | 1814 | dest: subset_singletonD split: nat.splits if_splits) | 
| 69312 | 1815 | by auto | 
| 44744 | 1816 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1817 | lemma finite_fun_UNIVD2: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1818 |   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 | 1819 | shows "finite (UNIV :: 'b set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1820 | proof - | 
| 63404 | 1821 | 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 | 1822 | by (rule finite_imageI) | 
| 63404 | 1823 | 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 | 1824 | by (rule UNIV_eq_I) auto | 
| 63404 | 1825 | ultimately show "finite (UNIV :: 'b set)" | 
| 1826 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1827 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1828 | |
| 48063 
f02b4302d5dd
remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
 huffman parents: 
47221diff
changeset | 1829 | 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 | 1830 | unfolding UNIV_unit by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1831 | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1832 | 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 | 1833 | 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 | 1834 | 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 | 1835 | proof (induction n) | 
| 63404 | 1836 | case 0 | 
| 1837 |   show ?case by (intro exI[of _ "{}"]) auto
 | |
| 1838 | next | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 1839 | case (Suc n) | 
| 63404 | 1840 | then obtain B where B: "finite B \<and> card B = n \<and> B \<subseteq> A" .. | 
| 60758 | 1841 | 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 | 1842 | with B have "B \<subset> A" by auto | 
| 63404 | 1843 | then have "\<exists>x. x \<in> A - B" | 
| 1844 | by (elim psubset_imp_ex_mem) | |
| 1845 | 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 | 1846 | 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 | 1847 | by auto | 
| 63404 | 1848 | 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 | 1849 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1850 | |
| 67457 | 1851 | text \<open>Sometimes, to prove that a set is finite, it is convenient to work with finite subsets | 
| 1852 | and to show that their cardinalities are uniformly bounded. This possibility is formalized in | |
| 1853 | the next criterion.\<close> | |
| 1854 | ||
| 1855 | lemma finite_if_finite_subsets_card_bdd: | |
| 1856 | assumes "\<And>G. G \<subseteq> F \<Longrightarrow> finite G \<Longrightarrow> card G \<le> C" | |
| 1857 | shows "finite F \<and> card F \<le> C" | |
| 1858 | proof (cases "finite F") | |
| 1859 | case False | |
| 1860 | obtain n::nat where n: "n > max C 0" by auto | |
| 1861 | obtain G where G: "G \<subseteq> F" "card G = n" using infinite_arbitrarily_large[OF False] by auto | |
| 1862 | hence "finite G" using \<open>n > max C 0\<close> using card_infinite gr_implies_not0 by blast | |
| 1863 | hence False using assms G n not_less by auto | |
| 1864 | thus ?thesis .. | |
| 1865 | next | |
| 1866 | case True thus ?thesis using assms[of F] by auto | |
| 1867 | qed | |
| 1868 | ||
| 63404 | 1869 | |
| 60758 | 1870 | 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 | 1871 | |
| 63404 | 1872 | lemma card_image_le: "finite A \<Longrightarrow> card (f ` A) \<le> card A" | 
| 54570 | 1873 | 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 | 1874 | |
| 63915 | 1875 | lemma card_image: "inj_on f A \<Longrightarrow> card (f ` A) = card A" | 
| 1876 | proof (induct A rule: infinite_finite_induct) | |
| 1877 | case (infinite A) | |
| 1878 | then have "\<not> finite (f ` A)" by (auto dest: finite_imageD) | |
| 1879 | with infinite show ?case by simp | |
| 1880 | qed simp_all | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1881 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1882 | lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B" | 
| 63612 | 1883 | 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 | 1884 | |
| 63404 | 1885 | lemma endo_inj_surj: "finite A \<Longrightarrow> f ` A \<subseteq> A \<Longrightarrow> inj_on f A \<Longrightarrow> f ` A = A" | 
| 1886 | 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 | 1887 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1888 | lemma eq_card_imp_inj_on: | 
| 63404 | 1889 | assumes "finite A" "card(f ` A) = card A" | 
| 1890 | shows "inj_on f A" | |
| 1891 | using assms | |
| 54570 | 1892 | proof (induct rule:finite_induct) | 
| 63404 | 1893 | case empty | 
| 1894 | show ?case by simp | |
| 54570 | 1895 | next | 
| 1896 | case (insert x A) | |
| 63404 | 1897 | then show ?case | 
| 1898 | using card_image_le [of A f] by (simp add: card_insert_if split: if_splits) | |
| 54570 | 1899 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1900 | |
| 63404 | 1901 | lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card (f ` A) = card A" | 
| 54570 | 1902 | 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 | 1903 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1904 | lemma card_inj_on_le: | 
| 63404 | 1905 | assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" | 
| 1906 | shows "card A \<le> card B" | |
| 54570 | 1907 | proof - | 
| 63404 | 1908 | have "finite A" | 
| 1909 | using assms by (blast intro: finite_imageD dest: finite_subset) | |
| 1910 | then show ?thesis | |
| 1911 | using assms by (force intro: card_mono simp: card_image [symmetric]) | |
| 54570 | 1912 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1913 | |
| 69235 | 1914 | lemma inj_on_iff_card_le: | 
| 1915 | "\<lbrakk> finite A; finite B \<rbrakk> \<Longrightarrow> (\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)" | |
| 1916 | using card_inj_on_le[of _ A B] card_le_inj[of A B] by blast | |
| 1917 | ||
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 1918 | 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 | 1919 | 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 | 1920 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1921 | lemma card_bij_eq: | 
| 63404 | 1922 | "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 | 
| 1923 | \<Longrightarrow> card A = card B" | |
| 1924 | by (auto intro: le_antisym card_inj_on_le) | |
| 1925 | ||
| 1926 | lemma bij_betw_finite: "bij_betw f A B \<Longrightarrow> finite A \<longleftrightarrow> finite B" | |
| 1927 | 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 | 1928 | |
| 63404 | 1929 | lemma inj_on_finite: "inj_on f A \<Longrightarrow> f ` A \<le> B \<Longrightarrow> finite B \<Longrightarrow> finite A" | 
| 1930 | 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 | 1931 | |
| 63404 | 1932 | lemma card_vimage_inj: "inj f \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> card (f -` A) = card A" | 
| 1933 | by (auto 4 3 simp: subset_image_iff inj_vimage_image_eq | |
| 1934 | intro: card_image[symmetric, OF subset_inj_on]) | |
| 55020 | 1935 | |
| 41656 | 1936 | |
| 60758 | 1937 | subsubsection \<open>Pigeonhole Principles\<close> | 
| 37466 | 1938 | |
| 63404 | 1939 | lemma pigeonhole: "card A > card (f ` A) \<Longrightarrow> \<not> inj_on f A " | 
| 1940 | by (auto dest: card_image less_irrefl_nat) | |
| 37466 | 1941 | |
| 1942 | lemma pigeonhole_infinite: | |
| 63404 | 1943 | assumes "\<not> finite A" and "finite (f`A)" | 
| 1944 |   shows "\<exists>a0\<in>A. \<not> finite {a\<in>A. f a = f a0}"
 | |
| 1945 | using assms(2,1) | |
| 1946 | proof (induct "f`A" arbitrary: A rule: finite_induct) | |
| 1947 | case empty | |
| 1948 | then show ?case by simp | |
| 1949 | next | |
| 1950 | case (insert b F) | |
| 1951 | show ?case | |
| 1952 |   proof (cases "finite {a\<in>A. f a = b}")
 | |
| 1953 | case True | |
| 1954 |     with \<open>\<not> finite A\<close> have "\<not> finite (A - {a\<in>A. f a = b})"
 | |
| 1955 | by simp | |
| 1956 |     also have "A - {a\<in>A. f a = b} = {a\<in>A. f a \<noteq> b}"
 | |
| 1957 | by blast | |
| 1958 |     finally have "\<not> finite {a\<in>A. f a \<noteq> b}" .
 | |
| 1959 | from insert(3)[OF _ this] insert(2,4) show ?thesis | |
| 1960 | by simp (blast intro: rev_finite_subset) | |
| 37466 | 1961 | next | 
| 63404 | 1962 | case False | 
| 1963 |     then have "{a \<in> A. f a = b} \<noteq> {}" by force
 | |
| 1964 | with False show ?thesis by blast | |
| 37466 | 1965 | qed | 
| 1966 | qed | |
| 1967 | ||
| 1968 | lemma pigeonhole_infinite_rel: | |
| 63404 | 1969 | assumes "\<not> finite A" | 
| 1970 | and "finite B" | |
| 1971 | and "\<forall>a\<in>A. \<exists>b\<in>B. R a b" | |
| 1972 |   shows "\<exists>b\<in>B. \<not> finite {a:A. R a b}"
 | |
| 37466 | 1973 | proof - | 
| 63404 | 1974 |   let ?F = "\<lambda>a. {b\<in>B. R a b}"
 | 
| 1975 | from finite_Pow_iff[THEN iffD2, OF \<open>finite B\<close>] have "finite (?F ` A)" | |
| 1976 | by (blast intro: rev_finite_subset) | |
| 1977 | from pigeonhole_infinite [where f = ?F, OF assms(1) this] | |
| 63612 | 1978 |   obtain a0 where "a0 \<in> A" and infinite: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
 | 
| 63404 | 1979 | obtain b0 where "b0 \<in> B" and "R a0 b0" | 
| 1980 | using \<open>a0 \<in> A\<close> assms(3) by blast | |
| 63612 | 1981 |   have "finite {a\<in>A. ?F a = ?F a0}" if "finite {a\<in>A. R a b0}"
 | 
| 63404 | 1982 | using \<open>b0 \<in> B\<close> \<open>R a0 b0\<close> that by (blast intro: rev_finite_subset) | 
| 63612 | 1983 | with infinite \<open>b0 \<in> B\<close> show ?thesis | 
| 63404 | 1984 | by blast | 
| 37466 | 1985 | qed | 
| 1986 | ||
| 1987 | ||
| 60758 | 1988 | 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 | 1989 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1990 | lemma card_Plus: | 
| 63404 | 1991 | 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 | 1992 | 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 | 1993 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1994 |   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 | 1995 | with assms show ?thesis | 
| 63404 | 1996 | 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 | 1997 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1998 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1999 | lemma card_Plus_conv_if: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2000 | "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 | 2001 | 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 | 2002 | |
| 63404 | 2003 | 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 | 2004 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2005 | lemma dvd_partition: | 
| 63404 | 2006 | assumes f: "finite (\<Union>C)" | 
| 2007 |     and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
 | |
| 2008 | shows "k dvd card (\<Union>C)" | |
| 54570 | 2009 | proof - | 
| 63404 | 2010 | have "finite C" | 
| 54570 | 2011 | by (rule finite_UnionD [OF f]) | 
| 63404 | 2012 | then show ?thesis | 
| 2013 | using assms | |
| 54570 | 2014 | proof (induct rule: finite_induct) | 
| 63404 | 2015 | case empty | 
| 2016 | show ?case by simp | |
| 54570 | 2017 | next | 
| 63404 | 2018 | case insert | 
| 2019 | then show ?case | |
| 54570 | 2020 | apply simp | 
| 2021 | apply (subst card_Un_disjoint) | |
| 63612 | 2022 | apply (auto simp add: disjoint_eq_subset_Compl) | 
| 54570 | 2023 | done | 
| 2024 | qed | |
| 2025 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2026 | |
| 63404 | 2027 | |
| 60758 | 2028 | 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 | 2029 | |
| 63404 | 2030 | lemma finite_surj_inj: | 
| 2031 | assumes "finite A" "A \<subseteq> f ` A" | |
| 2032 | shows "inj_on f A" | |
| 54570 | 2033 | proof - | 
| 63404 | 2034 | have "f ` A = A" | 
| 54570 | 2035 | by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le) | 
| 2036 | then show ?thesis using assms | |
| 2037 | by (simp add: eq_card_imp_inj_on) | |
| 2038 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2039 | |
| 63612 | 2040 | lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f" | 
| 2041 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 2042 | 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 | 2043 | |
| 63612 | 2044 | lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f" | 
| 2045 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 2046 | 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 | 2047 | |
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2048 | lemma surjective_iff_injective_gen: | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2049 | assumes fS: "finite S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2050 | and fT: "finite T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2051 | and c: "card S = card T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2052 | and ST: "f ` S \<subseteq> T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2053 | shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2054 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2055 | proof | 
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2056 | assume h: "?lhs" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2057 |   {
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2058 | fix x y | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2059 | assume x: "x \<in> S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2060 | assume y: "y \<in> S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2061 | assume f: "f x = f y" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2062 | from x fS have S0: "card S \<noteq> 0" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2063 | by auto | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2064 | have "x = y" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2065 | proof (rule ccontr) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2066 | assume xy: "\<not> ?thesis" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2067 |       have th: "card S \<le> card (f ` (S - {y}))"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2068 | unfolding c | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2069 | proof (rule card_mono) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2070 |         show "finite (f ` (S - {y}))"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2071 | by (simp add: fS) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2072 | have "\<lbrakk>x \<noteq> y; x \<in> S; z \<in> S; f x = f y\<rbrakk> | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2073 | \<Longrightarrow> \<exists>x \<in> S. x \<noteq> y \<and> f z = f x" for z | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2074 | by (case_tac "z = y \<longrightarrow> z = x") auto | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2075 |         then show "T \<subseteq> f ` (S - {y})"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2076 | using h xy x y f by fastforce | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2077 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2078 |       also have " \<dots> \<le> card (S - {y})"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2079 | by (simp add: card_image_le fS) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2080 | also have "\<dots> \<le> card S - 1" using y fS by simp | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2081 | finally show False using S0 by arith | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2082 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2083 | } | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2084 | then show ?rhs | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2085 | unfolding inj_on_def by blast | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2086 | next | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2087 | assume h: ?rhs | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2088 | have "f ` S = T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2089 | by (simp add: ST c card_image card_subset_eq fT h) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2090 | then show ?lhs by blast | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2091 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2092 | |
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 2093 | hide_const (open) Finite_Set.fold | 
| 46033 | 2094 | |
| 61810 | 2095 | |
| 63404 | 2096 | subsection \<open>Infinite Sets\<close> | 
| 61810 | 2097 | |
| 2098 | text \<open> | |
| 2099 | Some elementary facts about infinite sets, mostly by Stephan Merz. | |
| 2100 | Beware! Because "infinite" merely abbreviates a negation, these | |
| 2101 | lemmas may not work well with \<open>blast\<close>. | |
| 2102 | \<close> | |
| 2103 | ||
| 2104 | abbreviation infinite :: "'a set \<Rightarrow> bool" | |
| 2105 | where "infinite S \<equiv> \<not> finite S" | |
| 2106 | ||
| 2107 | text \<open> | |
| 2108 | Infinite sets are non-empty, and if we remove some elements from an | |
| 2109 | infinite set, the result is still infinite. | |
| 2110 | \<close> | |
| 2111 | ||
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2112 | lemma infinite_UNIV_nat [iff]: "infinite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2113 | proof | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2114 | assume "finite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2115 | with finite_UNIV_inj_surj [of Suc] show False | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2116 | by simp (blast dest: Suc_neq_Zero surjD) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2117 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2118 | |
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2119 | lemma infinite_UNIV_char_0: "infinite (UNIV :: 'a::semiring_char_0 set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2120 | proof | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2121 | assume "finite (UNIV :: 'a set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2122 | with subset_UNIV have "finite (range of_nat :: 'a set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2123 | by (rule finite_subset) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2124 | moreover have "inj (of_nat :: nat \<Rightarrow> 'a)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2125 | by (simp add: inj_on_def) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2126 | ultimately have "finite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2127 | by (rule finite_imageD) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2128 | then show False | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2129 | by simp | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2130 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2131 | |
| 61810 | 2132 | lemma infinite_imp_nonempty: "infinite S \<Longrightarrow> S \<noteq> {}"
 | 
| 2133 | by auto | |
| 2134 | ||
| 2135 | lemma infinite_remove: "infinite S \<Longrightarrow> infinite (S - {a})"
 | |
| 2136 | by simp | |
| 2137 | ||
| 2138 | lemma Diff_infinite_finite: | |
| 63404 | 2139 | assumes "finite T" "infinite S" | 
| 61810 | 2140 | shows "infinite (S - T)" | 
| 63404 | 2141 | using \<open>finite T\<close> | 
| 61810 | 2142 | proof induct | 
| 63404 | 2143 |   from \<open>infinite S\<close> show "infinite (S - {})"
 | 
| 2144 | by auto | |
| 61810 | 2145 | next | 
| 2146 | fix T x | |
| 2147 | assume ih: "infinite (S - T)" | |
| 2148 |   have "S - (insert x T) = (S - T) - {x}"
 | |
| 2149 | by (rule Diff_insert) | |
| 63404 | 2150 | with ih show "infinite (S - (insert x T))" | 
| 61810 | 2151 | by (simp add: infinite_remove) | 
| 2152 | qed | |
| 2153 | ||
| 2154 | lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)" | |
| 2155 | by simp | |
| 2156 | ||
| 2157 | lemma infinite_Un: "infinite (S \<union> T) \<longleftrightarrow> infinite S \<or> infinite T" | |
| 2158 | by simp | |
| 2159 | ||
| 2160 | lemma infinite_super: | |
| 63404 | 2161 | assumes "S \<subseteq> T" | 
| 2162 | and "infinite S" | |
| 61810 | 2163 | shows "infinite T" | 
| 2164 | proof | |
| 2165 | assume "finite T" | |
| 63404 | 2166 | with \<open>S \<subseteq> T\<close> have "finite S" by (simp add: finite_subset) | 
| 2167 | with \<open>infinite S\<close> show False by simp | |
| 61810 | 2168 | qed | 
| 2169 | ||
| 2170 | proposition infinite_coinduct [consumes 1, case_names infinite]: | |
| 2171 | assumes "X A" | |
| 63404 | 2172 |     and step: "\<And>A. X A \<Longrightarrow> \<exists>x\<in>A. X (A - {x}) \<or> infinite (A - {x})"
 | 
| 61810 | 2173 | shows "infinite A" | 
| 2174 | proof | |
| 2175 | assume "finite A" | |
| 63404 | 2176 | then show False | 
| 2177 | using \<open>X A\<close> | |
| 61810 | 2178 | proof (induction rule: finite_psubset_induct) | 
| 2179 | case (psubset A) | |
| 2180 |     then obtain x where "x \<in> A" "X (A - {x}) \<or> infinite (A - {x})"
 | |
| 2181 | using local.step psubset.prems by blast | |
| 2182 |     then have "X (A - {x})"
 | |
| 2183 | using psubset.hyps by blast | |
| 2184 | show False | |
| 2185 |       apply (rule psubset.IH [where B = "A - {x}"])
 | |
| 63612 | 2186 | apply (use \<open>x \<in> A\<close> in blast) | 
| 63404 | 2187 |       apply (simp add: \<open>X (A - {x})\<close>)
 | 
| 2188 | done | |
| 61810 | 2189 | qed | 
| 2190 | qed | |
| 2191 | ||
| 2192 | text \<open> | |
| 2193 | For any function with infinite domain and finite range there is some | |
| 2194 | element that is the image of infinitely many domain elements. In | |
| 2195 | particular, any infinite sequence of elements from a finite set | |
| 2196 | contains some element that occurs infinitely often. | |
| 2197 | \<close> | |
| 2198 | ||
| 2199 | lemma inf_img_fin_dom': | |
| 63404 | 2200 | assumes img: "finite (f ` A)" | 
| 2201 | and dom: "infinite A" | |
| 61810 | 2202 |   shows "\<exists>y \<in> f ` A. infinite (f -` {y} \<inter> A)"
 | 
| 2203 | proof (rule ccontr) | |
| 2204 |   have "A \<subseteq> (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by auto
 | |
| 63404 | 2205 | moreover assume "\<not> ?thesis" | 
| 61810 | 2206 |   with img have "finite (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by blast
 | 
| 63404 | 2207 | ultimately have "finite A" by (rule finite_subset) | 
| 61810 | 2208 | with dom show False by contradiction | 
| 2209 | qed | |
| 2210 | ||
| 2211 | lemma inf_img_fin_domE': | |
| 2212 | assumes "finite (f ` A)" and "infinite A" | |
| 2213 |   obtains y where "y \<in> f`A" and "infinite (f -` {y} \<inter> A)"
 | |
| 2214 | using assms by (blast dest: inf_img_fin_dom') | |
| 2215 | ||
| 2216 | lemma inf_img_fin_dom: | |
| 2217 | assumes img: "finite (f`A)" and dom: "infinite A" | |
| 2218 |   shows "\<exists>y \<in> f`A. infinite (f -` {y})"
 | |
| 63404 | 2219 | using inf_img_fin_dom'[OF assms] by auto | 
| 61810 | 2220 | |
| 2221 | lemma inf_img_fin_domE: | |
| 2222 | assumes "finite (f`A)" and "infinite A" | |
| 2223 |   obtains y where "y \<in> f`A" and "infinite (f -` {y})"
 | |
| 2224 | using assms by (blast dest: inf_img_fin_dom) | |
| 2225 | ||
| 63404 | 2226 | proposition finite_image_absD: "finite (abs ` S) \<Longrightarrow> finite S" | 
| 2227 | for S :: "'a::linordered_ring set" | |
| 61810 | 2228 | by (rule ccontr) (auto simp: abs_eq_iff vimage_def dest: inf_img_fin_dom) | 
| 2229 | ||
| 69735 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2230 | subsection \<open>The finite powerset operator\<close> | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2231 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2232 | definition Fpow :: "'a set \<Rightarrow> 'a set set" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2233 | where "Fpow A \<equiv> {X. X \<subseteq> A \<and> finite X}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2234 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2235 | lemma Fpow_mono: "A \<subseteq> B \<Longrightarrow> Fpow A \<subseteq> Fpow B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2236 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2237 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2238 | lemma empty_in_Fpow: "{} \<in> Fpow A"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2239 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2240 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2241 | lemma Fpow_not_empty: "Fpow A \<noteq> {}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2242 | using empty_in_Fpow by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2243 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2244 | lemma Fpow_subset_Pow: "Fpow A \<subseteq> Pow A" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2245 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2246 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2247 | lemma Fpow_Pow_finite: "Fpow A = Pow A Int {A. finite A}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2248 | unfolding Fpow_def Pow_def by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2249 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2250 | lemma inj_on_image_Fpow: | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2251 | assumes "inj_on f A" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2252 | shows "inj_on (image f) (Fpow A)" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2253 | using assms Fpow_subset_Pow[of A] subset_inj_on[of "image f" "Pow A"] | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2254 | inj_on_image_Pow by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2255 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2256 | lemma image_Fpow_mono: | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2257 | assumes "f ` A \<subseteq> B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2258 | shows "(image f) ` (Fpow A) \<subseteq> Fpow B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2259 | using assms by(unfold Fpow_def, auto) | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 2260 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2261 | end |