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