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