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