| author | wenzelm | 
| Thu, 26 Oct 2023 22:10:22 +0200 | |
| changeset 78851 | db37cae970a6 | 
| parent 78801 | 42ae6e0ecfd4 | 
| child 79772 | 817d33f8aa7f | 
| permissions | -rw-r--r-- | 
| 12396 | 1 | (* Title: HOL/Finite_Set.thy | 
| 63612 | 2 | Author: Tobias Nipkow | 
| 3 | Author: Lawrence C Paulson | |
| 4 | Author: Markus Wenzel | |
| 5 | Author: Jeremy Avigad | |
| 6 | Author: Andrei Popescu | |
| 12396 | 7 | *) | 
| 8 | ||
| 60758 | 9 | section \<open>Finite sets\<close> | 
| 12396 | 10 | |
| 15131 | 11 | theory Finite_Set | 
| 77695 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 12 | imports Product_Type Sum_Type Fields Relation | 
| 15131 | 13 | begin | 
| 12396 | 14 | |
| 60758 | 15 | subsection \<open>Predicate for finite sets\<close> | 
| 12396 | 16 | |
| 63612 | 17 | context notes [[inductive_internals]] | 
| 61681 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 18 | begin | 
| 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 19 | |
| 41656 | 20 | inductive finite :: "'a set \<Rightarrow> bool" | 
| 63612 | 21 | where | 
| 22 |     emptyI [simp, intro!]: "finite {}"
 | |
| 23 | | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)" | |
| 41656 | 24 | |
| 61681 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 25 | end | 
| 
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
 wenzelm parents: 
61605diff
changeset | 26 | |
| 78801 | 27 | simproc_setup finite_Collect ("finite (Collect P)") = \<open>K Set_Comprehension_Pointfree.proc\<close>
 | 
| 48109 
0a58f7eefba2
Integrated set comprehension pointfree simproc.
 Rafal Kolanski <rafal.kolanski@nicta.com.au> parents: 
48063diff
changeset | 28 | |
| 54611 
31afce809794
set_comprehension_pointfree simproc causes to many surprises if enabled by default
 traytel parents: 
54570diff
changeset | 29 | declare [[simproc del: finite_Collect]] | 
| 
31afce809794
set_comprehension_pointfree simproc causes to many surprises if enabled by default
 traytel parents: 
54570diff
changeset | 30 | |
| 41656 | 31 | lemma finite_induct [case_names empty insert, induct set: finite]: | 
| 61799 | 32 | \<comment> \<open>Discharging \<open>x \<notin> F\<close> entails extra work.\<close> | 
| 41656 | 33 | assumes "finite F" | 
| 34 |   assumes "P {}"
 | |
| 35 | and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)" | |
| 36 | shows "P F" | |
| 63404 | 37 | using \<open>finite F\<close> | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 38 | proof induct | 
| 41656 | 39 |   show "P {}" by fact
 | 
| 63404 | 40 | next | 
| 41 | fix x F | |
| 42 | assume F: "finite F" and P: "P F" | |
| 41656 | 43 | show "P (insert x F)" | 
| 44 | proof cases | |
| 45 | assume "x \<in> F" | |
| 63404 | 46 | then have "insert x F = F" by (rule insert_absorb) | 
| 41656 | 47 | with P show ?thesis by (simp only:) | 
| 48 | next | |
| 49 | assume "x \<notin> F" | |
| 50 | from F this P show ?thesis by (rule insert) | |
| 51 | qed | |
| 52 | qed | |
| 53 | ||
| 51622 | 54 | lemma infinite_finite_induct [case_names infinite empty insert]: | 
| 55 | assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A" | |
| 63404 | 56 |     and empty: "P {}"
 | 
| 57 | and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)" | |
| 51622 | 58 | shows "P A" | 
| 59 | proof (cases "finite A") | |
| 63404 | 60 | case False | 
| 61 | with infinite show ?thesis . | |
| 51622 | 62 | next | 
| 63404 | 63 | case True | 
| 64 | then show ?thesis by (induct A) (fact empty insert)+ | |
| 51622 | 65 | qed | 
| 66 | ||
| 41656 | 67 | |
| 60758 | 68 | subsubsection \<open>Choice principles\<close> | 
| 12396 | 69 | |
| 67443 
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
 wenzelm parents: 
63982diff
changeset | 70 | lemma ex_new_if_finite: \<comment> \<open>does not depend on def of finite at all\<close> | 
| 14661 | 71 | assumes "\<not> finite (UNIV :: 'a set)" and "finite A" | 
| 72 | shows "\<exists>a::'a. a \<notin> A" | |
| 73 | proof - | |
| 28823 | 74 | from assms have "A \<noteq> UNIV" by blast | 
| 41656 | 75 | then show ?thesis by blast | 
| 12396 | 76 | qed | 
| 77 | ||
| 60758 | 78 | text \<open>A finite choice principle. Does not need the SOME choice operator.\<close> | 
| 15484 | 79 | |
| 63404 | 80 | lemma finite_set_choice: "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)" | 
| 41656 | 81 | proof (induct rule: finite_induct) | 
| 63404 | 82 | case empty | 
| 83 | then show ?case by simp | |
| 29923 | 84 | next | 
| 85 | case (insert a A) | |
| 63404 | 86 | then obtain f b where f: "\<forall>x\<in>A. P x (f x)" and ab: "P a b" | 
| 87 | by auto | |
| 88 | show ?case (is "\<exists>f. ?P f") | |
| 29923 | 89 | proof | 
| 63404 | 90 | show "?P (\<lambda>x. if x = a then b else f x)" | 
| 91 | using f ab by auto | |
| 29923 | 92 | qed | 
| 93 | qed | |
| 94 | ||
| 23878 | 95 | |
| 60758 | 96 | subsubsection \<open>Finite sets are the images of initial segments of natural numbers\<close> | 
| 15392 | 97 | |
| 15510 | 98 | lemma finite_imp_nat_seg_image_inj_on: | 
| 63404 | 99 | assumes "finite A" | 
| 41656 | 100 |   shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
 | 
| 63404 | 101 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 102 | proof induct | 
| 15392 | 103 | case empty | 
| 41656 | 104 | show ?case | 
| 105 | proof | |
| 63404 | 106 |     show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}"
 | 
| 107 | by simp | |
| 15510 | 108 | qed | 
| 15392 | 109 | next | 
| 110 | case (insert a A) | |
| 23389 | 111 | have notinA: "a \<notin> A" by fact | 
| 63404 | 112 |   from insert.hyps obtain n f where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}"
 | 
| 113 | by blast | |
| 114 |   then have "insert a A = f(n:=a) ` {i. i < Suc n}" and "inj_on (f(n:=a)) {i. i < Suc n}"
 | |
| 115 | using notinA by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq) | |
| 116 | then show ?case by blast | |
| 15392 | 117 | qed | 
| 118 | ||
| 63404 | 119 | lemma nat_seg_image_imp_finite: "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
 | 
| 41656 | 120 | proof (induct n arbitrary: A) | 
| 63404 | 121 | case 0 | 
| 122 | then show ?case by simp | |
| 15392 | 123 | next | 
| 124 | case (Suc n) | |
| 125 |   let ?B = "f ` {i. i < n}"
 | |
| 63404 | 126 | have finB: "finite ?B" by (rule Suc.hyps[OF refl]) | 
| 15392 | 127 | show ?case | 
| 63404 | 128 | proof (cases "\<exists>k<n. f n = f k") | 
| 129 | case True | |
| 130 | then have "A = ?B" | |
| 131 | using Suc.prems by (auto simp:less_Suc_eq) | |
| 132 | then show ?thesis | |
| 133 | using finB by simp | |
| 15392 | 134 | next | 
| 63404 | 135 | case False | 
| 136 | then have "A = insert (f n) ?B" | |
| 137 | using Suc.prems by (auto simp:less_Suc_eq) | |
| 138 | then show ?thesis using finB by simp | |
| 15392 | 139 | qed | 
| 140 | qed | |
| 141 | ||
| 63982 | 142 | lemma finite_conv_nat_seg_image: "finite A \<longleftrightarrow> (\<exists>n f. A = f ` {i::nat. i < n})"
 | 
| 41656 | 143 | by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on) | 
| 15392 | 144 | |
| 32988 | 145 | lemma finite_imp_inj_to_nat_seg: | 
| 41656 | 146 | assumes "finite A" | 
| 63982 | 147 |   shows "\<exists>f n. f ` A = {i::nat. i < n} \<and> inj_on f A"
 | 
| 32988 | 148 | proof - | 
| 63404 | 149 | from finite_imp_nat_seg_image_inj_on [OF \<open>finite A\<close>] | 
| 63612 | 150 |   obtain f and n :: nat where bij: "bij_betw f {i. i<n} A"
 | 
| 63404 | 151 | by (auto simp: bij_betw_def) | 
| 33057 | 152 |   let ?f = "the_inv_into {i. i<n} f"
 | 
| 63404 | 153 |   have "inj_on ?f A \<and> ?f ` A = {i. i<n}"
 | 
| 33057 | 154 | by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij]) | 
| 63404 | 155 | then show ?thesis by blast | 
| 32988 | 156 | qed | 
| 157 | ||
| 63404 | 158 | lemma finite_Collect_less_nat [iff]: "finite {n::nat. n < k}"
 | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44835diff
changeset | 159 | by (fastforce simp: finite_conv_nat_seg_image) | 
| 29920 | 160 | |
| 63404 | 161 | lemma finite_Collect_le_nat [iff]: "finite {n::nat. n \<le> k}"
 | 
| 41656 | 162 | by (simp add: le_eq_less_or_eq Collect_disj_eq) | 
| 15392 | 163 | |
| 41656 | 164 | |
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 165 | subsection \<open>Finiteness and common set operations\<close> | 
| 12396 | 166 | |
| 63404 | 167 | lemma rev_finite_subset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A" | 
| 41656 | 168 | proof (induct arbitrary: A rule: finite_induct) | 
| 169 | case empty | |
| 170 | then show ?case by simp | |
| 171 | next | |
| 172 | case (insert x F A) | |
| 63404 | 173 |   have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})"
 | 
| 174 | by fact+ | |
| 41656 | 175 | show "finite A" | 
| 176 | proof cases | |
| 177 | assume x: "x \<in> A" | |
| 178 |     with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
 | |
| 179 |     with r have "finite (A - {x})" .
 | |
| 63404 | 180 |     then have "finite (insert x (A - {x}))" ..
 | 
| 181 |     also have "insert x (A - {x}) = A"
 | |
| 182 | using x by (rule insert_Diff) | |
| 41656 | 183 | finally show ?thesis . | 
| 12396 | 184 | next | 
| 60595 | 185 | show ?thesis when "A \<subseteq> F" | 
| 186 | using that by fact | |
| 41656 | 187 | assume "x \<notin> A" | 
| 63404 | 188 | with A show "A \<subseteq> F" | 
| 189 | by (simp add: subset_insert_iff) | |
| 12396 | 190 | qed | 
| 191 | qed | |
| 192 | ||
| 63404 | 193 | lemma finite_subset: "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A" | 
| 41656 | 194 | by (rule rev_finite_subset) | 
| 29901 | 195 | |
| 78099 
4d9349989d94
more uniform simproc_setup: avoid vacuous abstraction over morphism, which sometimes captures context values in its functional closure;
 wenzelm parents: 
78014diff
changeset | 196 | simproc_setup finite ("finite A") = \<open>
 | 
| 76422 | 197 | let | 
| 198 |   val finite_subset = @{thm finite_subset}
 | |
| 199 |   val Eq_TrueI = @{thm Eq_TrueI}
 | |
| 200 | ||
| 201 | fun is_subset A th = case Thm.prop_of th of | |
| 202 | (_ $ (Const (\<^const_name>\<open>less_eq\<close>, Type (\<^type_name>\<open>fun\<close>, [Type (\<^type_name>\<open>set\<close>, _), _])) $ A' $ B)) | |
| 203 | => if A aconv A' then SOME(B,th) else NONE | |
| 204 | | _ => NONE; | |
| 205 | ||
| 206 | fun is_finite th = case Thm.prop_of th of | |
| 207 | (_ $ (Const (\<^const_name>\<open>finite\<close>, _) $ A)) => SOME(A,th) | |
| 208 | | _ => NONE; | |
| 209 | ||
| 210 | fun comb (A,sub_th) (A',fin_th) ths = if A aconv A' then (sub_th,fin_th) :: ths else ths | |
| 211 | ||
| 78099 
4d9349989d94
more uniform simproc_setup: avoid vacuous abstraction over morphism, which sometimes captures context values in its functional closure;
 wenzelm parents: 
78014diff
changeset | 212 | fun proc ctxt ct = | 
| 76422 | 213 | (let | 
| 214 | val _ $ A = Thm.term_of ct | |
| 78099 
4d9349989d94
more uniform simproc_setup: avoid vacuous abstraction over morphism, which sometimes captures context values in its functional closure;
 wenzelm parents: 
78014diff
changeset | 215 | val prems = Simplifier.prems_of ctxt | 
| 76422 | 216 | val fins = map_filter is_finite prems | 
| 217 | val subsets = map_filter (is_subset A) prems | |
| 218 | in case fold_product comb subsets fins [] of | |
| 219 | (sub_th,fin_th) :: _ => SOME((fin_th RS (sub_th RS finite_subset)) RS Eq_TrueI) | |
| 220 | | _ => NONE | |
| 221 | end) | |
| 78099 
4d9349989d94
more uniform simproc_setup: avoid vacuous abstraction over morphism, which sometimes captures context values in its functional closure;
 wenzelm parents: 
78014diff
changeset | 222 | in K proc end | 
| 76422 | 223 | \<close> | 
| 224 | ||
| 76447 | 225 | (* Needs to be used with care *) | 
| 226 | declare [[simproc del: finite]] | |
| 227 | ||
| 41656 | 228 | lemma finite_UnI: | 
| 229 | assumes "finite F" and "finite G" | |
| 230 | shows "finite (F \<union> G)" | |
| 231 | using assms by induct simp_all | |
| 31992 | 232 | |
| 63404 | 233 | lemma finite_Un [iff]: "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G" | 
| 41656 | 234 | by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"]) | 
| 31992 | 235 | |
| 41656 | 236 | lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A" | 
| 12396 | 237 | proof - | 
| 41656 | 238 |   have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
 | 
| 239 |   then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
 | |
| 23389 | 240 | then show ?thesis by simp | 
| 12396 | 241 | qed | 
| 242 | ||
| 63404 | 243 | lemma finite_Int [simp, intro]: "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)" | 
| 41656 | 244 | by (blast intro: finite_subset) | 
| 245 | ||
| 246 | lemma finite_Collect_conjI [simp, intro]: | |
| 247 |   "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
 | |
| 248 | by (simp add: Collect_conj_eq) | |
| 249 | ||
| 250 | lemma finite_Collect_disjI [simp]: | |
| 251 |   "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
 | |
| 252 | by (simp add: Collect_disj_eq) | |
| 253 | ||
| 63404 | 254 | lemma finite_Diff [simp, intro]: "finite A \<Longrightarrow> finite (A - B)" | 
| 41656 | 255 | by (rule finite_subset, rule Diff_subset) | 
| 29901 | 256 | |
| 257 | lemma finite_Diff2 [simp]: | |
| 41656 | 258 | assumes "finite B" | 
| 259 | shows "finite (A - B) \<longleftrightarrow> finite A" | |
| 29901 | 260 | proof - | 
| 63404 | 261 | have "finite A \<longleftrightarrow> finite ((A - B) \<union> (A \<inter> B))" | 
| 262 | by (simp add: Un_Diff_Int) | |
| 263 | also have "\<dots> \<longleftrightarrow> finite (A - B)" | |
| 264 | using \<open>finite B\<close> by simp | |
| 29901 | 265 | finally show ?thesis .. | 
| 266 | qed | |
| 267 | ||
| 63404 | 268 | lemma finite_Diff_insert [iff]: "finite (A - insert a B) \<longleftrightarrow> finite (A - B)" | 
| 41656 | 269 | proof - | 
| 270 |   have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
 | |
| 271 |   moreover have "A - insert a B = A - B - {a}" by auto
 | |
| 272 | ultimately show ?thesis by simp | |
| 273 | qed | |
| 274 | ||
| 63404 | 275 | lemma finite_compl [simp]: | 
| 41656 | 276 | "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)" | 
| 277 | by (simp add: Compl_eq_Diff_UNIV) | |
| 12396 | 278 | |
| 63404 | 279 | lemma finite_Collect_not [simp]: | 
| 41656 | 280 |   "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
 | 
| 281 | by (simp add: Collect_neg_eq) | |
| 282 | ||
| 283 | lemma finite_Union [simp, intro]: | |
| 63404 | 284 | "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite (\<Union>A)" | 
| 41656 | 285 | by (induct rule: finite_induct) simp_all | 
| 286 | ||
| 287 | lemma finite_UN_I [intro]: | |
| 288 | "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)" | |
| 289 | by (induct rule: finite_induct) simp_all | |
| 29903 | 290 | |
| 69275 | 291 | lemma finite_UN [simp]: "finite A \<Longrightarrow> finite (\<Union>(B ` A)) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))" | 
| 41656 | 292 | by (blast intro: finite_subset) | 
| 293 | ||
| 63404 | 294 | lemma finite_Inter [intro]: "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)" | 
| 41656 | 295 | by (blast intro: Inter_lower finite_subset) | 
| 12396 | 296 | |
| 63404 | 297 | lemma finite_INT [intro]: "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)" | 
| 41656 | 298 | by (blast intro: INT_lower finite_subset) | 
| 13825 | 299 | |
| 63404 | 300 | lemma finite_imageI [simp, intro]: "finite F \<Longrightarrow> finite (h ` F)" | 
| 41656 | 301 | by (induct rule: finite_induct) simp_all | 
| 13825 | 302 | |
| 63404 | 303 | lemma finite_image_set [simp]: "finite {x. P x} \<Longrightarrow> finite {f x |x. P x}"
 | 
| 31768 | 304 | by (simp add: image_Collect [symmetric]) | 
| 305 | ||
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 306 | lemma finite_image_set2: | 
| 63404 | 307 |   "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {f x y |x y. P x \<and> Q y}"
 | 
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 308 |   by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {f x y}"]) auto
 | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 309 | |
| 41656 | 310 | lemma finite_imageD: | 
| 42206 | 311 | assumes "finite (f ` A)" and "inj_on f A" | 
| 312 | shows "finite A" | |
| 63404 | 313 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 314 | proof (induct "f ` A" arbitrary: A) | 
| 63404 | 315 | case empty | 
| 316 | then show ?case by simp | |
| 42206 | 317 | next | 
| 318 | case (insert x B) | |
| 63404 | 319 | then have B_A: "insert x B = f ` A" | 
| 320 | by simp | |
| 321 | then obtain y where "x = f y" and "y \<in> A" | |
| 322 | by blast | |
| 323 |   from B_A \<open>x \<notin> B\<close> have "B = f ` A - {x}"
 | |
| 324 | by blast | |
| 325 |   with B_A \<open>x \<notin> B\<close> \<open>x = f y\<close> \<open>inj_on f A\<close> \<open>y \<in> A\<close> have "B = f ` (A - {y})"
 | |
| 69286 | 326 | by (simp add: inj_on_image_set_diff) | 
| 63404 | 327 |   moreover from \<open>inj_on f A\<close> have "inj_on f (A - {y})"
 | 
| 328 | by (rule inj_on_diff) | |
| 329 |   ultimately have "finite (A - {y})"
 | |
| 330 | by (rule insert.hyps) | |
| 331 | then show "finite A" | |
| 332 | by simp | |
| 42206 | 333 | qed | 
| 12396 | 334 | |
| 63404 | 335 | lemma finite_image_iff: "inj_on f A \<Longrightarrow> finite (f ` A) \<longleftrightarrow> finite A" | 
| 336 | using finite_imageD by blast | |
| 62618 
f7f2467ab854
Refactoring (moving theorems into better locations), plus a bit of new material
 paulson <lp15@cam.ac.uk> parents: 
62481diff
changeset | 337 | |
| 63404 | 338 | lemma finite_surj: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B" | 
| 41656 | 339 | by (erule finite_subset) (rule finite_imageI) | 
| 12396 | 340 | |
| 63404 | 341 | lemma finite_range_imageI: "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))" | 
| 41656 | 342 | by (drule finite_imageI) (simp add: range_composition) | 
| 13825 | 343 | |
| 41656 | 344 | lemma finite_subset_image: | 
| 345 | assumes "finite B" | |
| 346 | shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C" | |
| 63404 | 347 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 348 | proof induct | 
| 63404 | 349 | case empty | 
| 350 | then show ?case by simp | |
| 41656 | 351 | next | 
| 63404 | 352 | case insert | 
| 353 | then show ?case | |
| 71258 
d67924987c34
a few new and tidier proofs (mostly about finite sets)
 paulson <lp15@cam.ac.uk> parents: 
70723diff
changeset | 354 | by (clarsimp simp del: image_insert simp add: image_insert [symmetric]) blast | 
| 41656 | 355 | qed | 
| 356 | ||
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 357 | lemma all_subset_image: "(\<forall>B. B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. B \<subseteq> A \<longrightarrow> P(f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 358 | by (safe elim!: subset_imageE) (use image_mono in \<open>blast+\<close>) (* slow *) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 359 | |
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 360 | lemma all_finite_subset_image: | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 361 | "(\<forall>B. finite B \<and> B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 362 | proof safe | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 363 | fix B :: "'a set" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 364 | assume B: "finite B" "B \<subseteq> f ` A" and P: "\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B)" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 365 | show "P B" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 366 | using finite_subset_image [OF B] P by blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 367 | qed blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 368 | |
| 70178 
4900351361b0
Lindelöf spaces and supporting material
 paulson <lp15@cam.ac.uk> parents: 
70019diff
changeset | 369 | lemma ex_finite_subset_image: | 
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 370 | "(\<exists>B. finite B \<and> B \<subseteq> f ` A \<and> P B) \<longleftrightarrow> (\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B))" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 371 | proof safe | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 372 | fix B :: "'a set" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 373 | assume B: "finite B" "B \<subseteq> f ` A" and "P B" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 374 | show "\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B)" | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 375 | using finite_subset_image [OF B] \<open>P B\<close> by blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 376 | qed blast | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 377 | |
| 63404 | 378 | lemma finite_vimage_IntI: "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)" | 
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 379 | proof (induct rule: finite_induct) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 380 | case (insert x F) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 381 | then show ?case | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 382 | by (simp add: vimage_insert [of h x F] finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2) | 
| 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 383 | qed simp | 
| 13825 | 384 | |
| 61762 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 385 | lemma finite_finite_vimage_IntI: | 
| 63612 | 386 | assumes "finite F" | 
| 387 |     and "\<And>y. y \<in> F \<Longrightarrow> finite ((h -` {y}) \<inter> A)"
 | |
| 61762 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 388 | shows "finite (h -` F \<inter> A)" | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 389 | proof - | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 390 |   have *: "h -` F \<inter> A = (\<Union> y\<in>F. (h -` {y}) \<inter> A)"
 | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 391 | by blast | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 392 | show ?thesis | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 393 | by (simp only: * assms finite_UN_I) | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 394 | qed | 
| 
d50b993b4fb9
Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
 paulson <lp15@cam.ac.uk> parents: 
61681diff
changeset | 395 | |
| 63404 | 396 | lemma finite_vimageI: "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)" | 
| 43991 | 397 | using finite_vimage_IntI[of F h UNIV] by auto | 
| 398 | ||
| 63404 | 399 | lemma finite_vimageD': "finite (f -` A) \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> finite A" | 
| 400 | by (auto simp add: subset_image_iff intro: finite_subset[rotated]) | |
| 59519 
2fb0c0fc62a3
add more general version of finite_vimageD
 Andreas Lochbihler parents: 
59504diff
changeset | 401 | |
| 63404 | 402 | lemma finite_vimageD: "finite (h -` F) \<Longrightarrow> surj h \<Longrightarrow> finite F" | 
| 403 | by (auto dest: finite_vimageD') | |
| 34111 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 404 | |
| 
1b015caba46c
add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
 huffman parents: 
34007diff
changeset | 405 | 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 | 406 | 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 | 407 | |
| 74438 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 408 | lemma finite_inverse_image_gen: | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 409 | assumes "finite A" "inj_on f D" | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 410 |   shows "finite {j\<in>D. f j \<in> A}"
 | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 411 | using finite_vimage_IntI [OF assms] | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 412 | by (simp add: Collect_conj_eq inf_commute vimage_def) | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 413 | |
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 414 | lemma finite_inverse_image: | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 415 | assumes "finite A" "inj f" | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 416 |   shows "finite {j. f j \<in> A}"
 | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 417 | using finite_inverse_image_gen [OF assms] by simp | 
| 
5827b91ef30e
new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
 paulson <lp15@cam.ac.uk> parents: 
74223diff
changeset | 418 | |
| 41656 | 419 | lemma finite_Collect_bex [simp]: | 
| 420 | assumes "finite A" | |
| 421 |   shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
 | |
| 422 | proof - | |
| 423 |   have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
 | |
| 424 | with assms show ?thesis by simp | |
| 425 | qed | |
| 12396 | 426 | |
| 41656 | 427 | lemma finite_Collect_bounded_ex [simp]: | 
| 428 |   assumes "finite {y. P y}"
 | |
| 429 |   shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
 | |
| 430 | proof - | |
| 63404 | 431 |   have "{x. \<exists>y. P y \<and> Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})"
 | 
| 432 | by auto | |
| 433 | with assms show ?thesis | |
| 434 | by simp | |
| 41656 | 435 | qed | 
| 29920 | 436 | |
| 63404 | 437 | lemma finite_Plus: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)" | 
| 41656 | 438 | by (simp add: Plus_def) | 
| 17022 | 439 | |
| 63404 | 440 | lemma finite_PlusD: | 
| 31080 | 441 | fixes A :: "'a set" and B :: "'b set" | 
| 442 | assumes fin: "finite (A <+> B)" | |
| 443 | shows "finite A" "finite B" | |
| 444 | proof - | |
| 63404 | 445 | have "Inl ` A \<subseteq> A <+> B" | 
| 446 | by auto | |
| 447 |   then have "finite (Inl ` A :: ('a + 'b) set)"
 | |
| 448 | using fin by (rule finite_subset) | |
| 449 | then show "finite A" | |
| 450 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 451 | next | 
| 63404 | 452 | have "Inr ` B \<subseteq> A <+> B" | 
| 453 | by auto | |
| 454 |   then have "finite (Inr ` B :: ('a + 'b) set)"
 | |
| 455 | using fin by (rule finite_subset) | |
| 456 | then show "finite B" | |
| 457 | by (rule finite_imageD) (auto intro: inj_onI) | |
| 31080 | 458 | qed | 
| 459 | ||
| 63404 | 460 | lemma finite_Plus_iff [simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B" | 
| 41656 | 461 | by (auto intro: finite_PlusD finite_Plus) | 
| 31080 | 462 | |
| 41656 | 463 | lemma finite_Plus_UNIV_iff [simp]: | 
| 464 |   "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | |
| 465 | by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff) | |
| 12396 | 466 | |
| 40786 
0a54cfc9add3
gave more standard finite set rules simp and intro attribute
 nipkow parents: 
40716diff
changeset | 467 | lemma finite_SigmaI [simp, intro]: | 
| 63404 | 468 | "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (SIGMA a:A. B a)" | 
| 469 | unfolding Sigma_def by blast | |
| 12396 | 470 | |
| 51290 | 471 | lemma finite_SigmaI2: | 
| 472 |   assumes "finite {x\<in>A. B x \<noteq> {}}"
 | |
| 473 | and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)" | |
| 474 | shows "finite (Sigma A B)" | |
| 475 | proof - | |
| 63404 | 476 |   from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)"
 | 
| 477 | by auto | |
| 478 |   also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B"
 | |
| 479 | by auto | |
| 51290 | 480 | finally show ?thesis . | 
| 481 | qed | |
| 482 | ||
| 63404 | 483 | lemma finite_cartesian_product: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)" | 
| 15402 | 484 | by (rule finite_SigmaI) | 
| 485 | ||
| 12396 | 486 | lemma finite_Prod_UNIV: | 
| 41656 | 487 |   "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
 | 
| 488 | by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product) | |
| 12396 | 489 | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 490 | lemma finite_cartesian_productD1: | 
| 42207 | 491 |   assumes "finite (A \<times> B)" and "B \<noteq> {}"
 | 
| 492 | shows "finite A" | |
| 493 | proof - | |
| 494 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 495 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 496 |   then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}"
 | 
| 497 | by simp | |
| 60758 | 498 |   with \<open>B \<noteq> {}\<close> have "A = (fst \<circ> f) ` {i::nat. i < n}"
 | 
| 56154 
f0a927235162
more complete set of lemmas wrt. image and composition
 haftmann parents: 
55096diff
changeset | 499 | by (simp add: image_comp) | 
| 63404 | 500 |   then have "\<exists>n f. A = f ` {i::nat. i < n}"
 | 
| 501 | by blast | |
| 42207 | 502 | then show ?thesis | 
| 503 | by (auto simp add: finite_conv_nat_seg_image) | |
| 504 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 505 | |
| 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 506 | lemma finite_cartesian_productD2: | 
| 42207 | 507 |   assumes "finite (A \<times> B)" and "A \<noteq> {}"
 | 
| 508 | shows "finite B" | |
| 509 | proof - | |
| 510 |   from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
 | |
| 511 | by (auto simp add: finite_conv_nat_seg_image) | |
| 63404 | 512 |   then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}"
 | 
| 513 | by simp | |
| 60758 | 514 |   with \<open>A \<noteq> {}\<close> have "B = (snd \<circ> f) ` {i::nat. i < n}"
 | 
| 56154 
f0a927235162
more complete set of lemmas wrt. image and composition
 haftmann parents: 
55096diff
changeset | 515 | by (simp add: image_comp) | 
| 63404 | 516 |   then have "\<exists>n f. B = f ` {i::nat. i < n}"
 | 
| 517 | by blast | |
| 42207 | 518 | then show ?thesis | 
| 519 | by (auto simp add: finite_conv_nat_seg_image) | |
| 520 | qed | |
| 15409 
a063687d24eb
new and stronger lemmas and improved simplification for finite sets
 paulson parents: 
15402diff
changeset | 521 | |
| 57025 | 522 | lemma finite_cartesian_product_iff: | 
| 523 |   "finite (A \<times> B) \<longleftrightarrow> (A = {} \<or> B = {} \<or> (finite A \<and> finite B))"
 | |
| 524 | by (auto dest: finite_cartesian_productD1 finite_cartesian_productD2 finite_cartesian_product) | |
| 525 | ||
| 63404 | 526 | lemma finite_prod: | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 527 |   "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
 | 
| 57025 | 528 | using finite_cartesian_product_iff[of UNIV UNIV] by simp | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 529 | |
| 63404 | 530 | lemma finite_Pow_iff [iff]: "finite (Pow A) \<longleftrightarrow> finite A" | 
| 12396 | 531 | proof | 
| 532 | assume "finite (Pow A)" | |
| 63404 | 533 |   then have "finite ((\<lambda>x. {x}) ` A)"
 | 
| 63612 | 534 | by (blast intro: finite_subset) (* somewhat slow *) | 
| 63404 | 535 | then show "finite A" | 
| 536 | by (rule finite_imageD [unfolded inj_on_def]) simp | |
| 12396 | 537 | next | 
| 538 | assume "finite A" | |
| 41656 | 539 | then show "finite (Pow A)" | 
| 35216 | 540 | by induct (simp_all add: Pow_insert) | 
| 12396 | 541 | qed | 
| 542 | ||
| 63404 | 543 | corollary finite_Collect_subsets [simp, intro]: "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
 | 
| 41656 | 544 | by (simp add: Pow_def [symmetric]) | 
| 29918 | 545 | |
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 546 | lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)" | 
| 63404 | 547 | by (simp only: finite_Pow_iff Pow_UNIV[symmetric]) | 
| 48175 
fea68365c975
add finiteness lemmas for 'a * 'b and 'a set
 Andreas Lochbihler parents: 
48128diff
changeset | 548 | |
| 63404 | 549 | lemma finite_UnionD: "finite (\<Union>A) \<Longrightarrow> finite A" | 
| 41656 | 550 | by (blast intro: finite_subset [OF subset_Pow_Union]) | 
| 15392 | 551 | |
| 67511 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 552 | lemma finite_bind: | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 553 | assumes "finite S" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 554 | assumes "\<forall>x \<in> S. finite (f x)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 555 | shows "finite (Set.bind S f)" | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 556 | using assms by (simp add: bind_UNION) | 
| 
a6f5a78712af
include lemmas generally useful for combinatorial proofs
 bulwahn parents: 
67457diff
changeset | 557 | |
| 68463 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 558 | lemma finite_filter [simp]: "finite S \<Longrightarrow> finite (Set.filter P S)" | 
| 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 559 | unfolding Set.filter_def by simp | 
| 
410818a69ee3
material on finite sets and maps
 Lars Hupel <lars.hupel@mytum.de> parents: 
67511diff
changeset | 560 | |
| 63404 | 561 | lemma finite_set_of_finite_funs: | 
| 562 | assumes "finite A" "finite B" | |
| 563 |   shows "finite {f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
 | |
| 564 | proof - | |
| 53820 | 565 |   let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
 | 
| 63404 | 566 | have "?F ` ?S \<subseteq> Pow(A \<times> B)" | 
| 567 | by auto | |
| 568 | from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" | |
| 569 | by simp | |
| 53820 | 570 | have 2: "inj_on ?F ?S" | 
| 63612 | 571 | by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff) (* somewhat slow *) | 
| 63404 | 572 | show ?thesis | 
| 573 | by (rule finite_imageD [OF 1 2]) | |
| 53820 | 574 | qed | 
| 15392 | 575 | |
| 58195 | 576 | lemma not_finite_existsD: | 
| 577 |   assumes "\<not> finite {a. P a}"
 | |
| 578 | shows "\<exists>a. P a" | |
| 579 | proof (rule classical) | |
| 63404 | 580 | assume "\<not> ?thesis" | 
| 58195 | 581 | with assms show ?thesis by auto | 
| 582 | qed | |
| 583 | ||
| 77695 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 584 | lemma finite_converse [iff]: "finite (r\<inverse>) \<longleftrightarrow> finite r" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 585 | unfolding converse_def conversep_iff | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 586 | using [[simproc add: finite_Collect]] | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 587 | by (auto elim: finite_imageD simp: inj_on_def) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 588 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 589 | lemma finite_Domain: "finite r \<Longrightarrow> finite (Domain r)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 590 | by (induct set: finite) auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 591 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 592 | lemma finite_Range: "finite r \<Longrightarrow> finite (Range r)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 593 | by (induct set: finite) auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 594 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 595 | lemma finite_Field: "finite r \<Longrightarrow> finite (Field r)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 596 | by (simp add: Field_def finite_Domain finite_Range) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 597 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 598 | lemma finite_Image[simp]: "finite R \<Longrightarrow> finite (R `` A)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 599 | by(rule finite_subset[OF _ finite_Range]) auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 600 | |
| 58195 | 601 | |
| 68975 
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
 paulson <lp15@cam.ac.uk> parents: 
68521diff
changeset | 602 | subsection \<open>Further induction rules on finite sets\<close> | 
| 41656 | 603 | |
| 604 | lemma finite_ne_induct [case_names singleton insert, consumes 2]: | |
| 605 |   assumes "finite F" and "F \<noteq> {}"
 | |
| 606 |   assumes "\<And>x. P {x}"
 | |
| 607 |     and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
 | |
| 608 | shows "P F" | |
| 63404 | 609 | using assms | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 610 | proof induct | 
| 63404 | 611 | case empty | 
| 612 | then show ?case by simp | |
| 41656 | 613 | next | 
| 63404 | 614 | case (insert x F) | 
| 615 | then show ?case by cases auto | |
| 41656 | 616 | qed | 
| 617 | ||
| 618 | lemma finite_subset_induct [consumes 2, case_names empty insert]: | |
| 619 | assumes "finite F" and "F \<subseteq> A" | |
| 63612 | 620 |     and empty: "P {}"
 | 
| 41656 | 621 | and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)" | 
| 622 | shows "P F" | |
| 63404 | 623 | using \<open>finite F\<close> \<open>F \<subseteq> A\<close> | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 624 | proof induct | 
| 41656 | 625 |   show "P {}" by fact
 | 
| 31441 | 626 | next | 
| 41656 | 627 | fix x F | 
| 63404 | 628 | assume "finite F" and "x \<notin> F" and P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | 
| 41656 | 629 | show "P (insert x F)" | 
| 630 | proof (rule insert) | |
| 631 | from i show "x \<in> A" by blast | |
| 632 | from i have "F \<subseteq> A" by blast | |
| 633 | with P show "P F" . | |
| 634 | show "finite F" by fact | |
| 635 | show "x \<notin> F" by fact | |
| 636 | qed | |
| 637 | qed | |
| 638 | ||
| 639 | lemma finite_empty_induct: | |
| 640 | assumes "finite A" | |
| 63612 | 641 | and "P A" | 
| 41656 | 642 |     and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
 | 
| 643 |   shows "P {}"
 | |
| 644 | proof - | |
| 63404 | 645 | have "P (A - B)" if "B \<subseteq> A" for B :: "'a set" | 
| 41656 | 646 | proof - | 
| 63404 | 647 | from \<open>finite A\<close> that have "finite B" | 
| 648 | by (rule rev_finite_subset) | |
| 60758 | 649 | from this \<open>B \<subseteq> A\<close> show "P (A - B)" | 
| 41656 | 650 | proof induct | 
| 651 | case empty | |
| 60758 | 652 | from \<open>P A\<close> show ?case by simp | 
| 41656 | 653 | next | 
| 654 | case (insert b B) | |
| 655 |       have "P (A - B - {b})"
 | |
| 656 | proof (rule remove) | |
| 63404 | 657 | from \<open>finite A\<close> show "finite (A - B)" | 
| 658 | by induct auto | |
| 659 | from insert show "b \<in> A - B" | |
| 660 | by simp | |
| 661 | from insert show "P (A - B)" | |
| 662 | by simp | |
| 41656 | 663 | qed | 
| 63404 | 664 |       also have "A - B - {b} = A - insert b B"
 | 
| 665 | by (rule Diff_insert [symmetric]) | |
| 41656 | 666 | finally show ?case . | 
| 667 | qed | |
| 668 | qed | |
| 669 | then have "P (A - A)" by blast | |
| 670 | then show ?thesis by simp | |
| 31441 | 671 | qed | 
| 672 | ||
| 58195 | 673 | lemma finite_update_induct [consumes 1, case_names const update]: | 
| 674 |   assumes finite: "finite {a. f a \<noteq> c}"
 | |
| 63404 | 675 | and const: "P (\<lambda>a. c)" | 
| 676 |     and update: "\<And>a b f. finite {a. f a \<noteq> c} \<Longrightarrow> f a = c \<Longrightarrow> b \<noteq> c \<Longrightarrow> P f \<Longrightarrow> P (f(a := b))"
 | |
| 58195 | 677 | shows "P f" | 
| 63404 | 678 | using finite | 
| 679 | proof (induct "{a. f a \<noteq> c}" arbitrary: f)
 | |
| 680 | case empty | |
| 681 | with const show ?case by simp | |
| 58195 | 682 | next | 
| 683 | case (insert a A) | |
| 684 |   then have "A = {a'. (f(a := c)) a' \<noteq> c}" and "f a \<noteq> c"
 | |
| 685 | by auto | |
| 60758 | 686 |   with \<open>finite A\<close> have "finite {a'. (f(a := c)) a' \<noteq> c}"
 | 
| 58195 | 687 | by simp | 
| 688 | have "(f(a := c)) a = c" | |
| 689 | by simp | |
| 60758 | 690 |   from insert \<open>A = {a'. (f(a := c)) a' \<noteq> c}\<close> have "P (f(a := c))"
 | 
| 58195 | 691 | by simp | 
| 63404 | 692 |   with \<open>finite {a'. (f(a := c)) a' \<noteq> c}\<close> \<open>(f(a := c)) a = c\<close> \<open>f a \<noteq> c\<close>
 | 
| 693 | have "P ((f(a := c))(a := f a))" | |
| 58195 | 694 | by (rule update) | 
| 695 | then show ?case by simp | |
| 696 | qed | |
| 697 | ||
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 698 | lemma finite_subset_induct' [consumes 2, case_names empty insert]: | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 699 | assumes "finite F" and "F \<subseteq> A" | 
| 63612 | 700 |     and empty: "P {}"
 | 
| 701 | and insert: "\<And>a F. \<lbrakk>finite F; a \<in> A; F \<subseteq> A; a \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert a F)" | |
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 702 | shows "P F" | 
| 63915 | 703 | using assms(1,2) | 
| 704 | proof induct | |
| 705 |   show "P {}" by fact
 | |
| 706 | next | |
| 707 | fix x F | |
| 708 | assume "finite F" and "x \<notin> F" and | |
| 709 | P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A" | |
| 710 | show "P (insert x F)" | |
| 711 | proof (rule insert) | |
| 712 | from i show "x \<in> A" by blast | |
| 713 | from i have "F \<subseteq> A" by blast | |
| 714 | with P show "P F" . | |
| 715 | show "finite F" by fact | |
| 716 | show "x \<notin> F" by fact | |
| 717 | show "F \<subseteq> A" by fact | |
| 63561 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 718 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 719 | qed | 
| 
fba08009ff3e
add lemmas contributed by Peter Gammie
 Andreas Lochbihler parents: 
63404diff
changeset | 720 | |
| 58195 | 721 | |
| 61799 | 722 | subsection \<open>Class \<open>finite\<close>\<close> | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 723 | |
| 63612 | 724 | class finite = | 
| 725 | assumes finite_UNIV: "finite (UNIV :: 'a set)" | |
| 27430 | 726 | begin | 
| 727 | ||
| 61076 | 728 | lemma finite [simp]: "finite (A :: 'a set)" | 
| 26441 | 729 | 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 | 730 | |
| 61076 | 731 | lemma finite_code [code]: "finite (A :: 'a set) \<longleftrightarrow> True" | 
| 40922 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 732 | by simp | 
| 
4d0f96a54e76
adding code equation for finiteness of finite types
 bulwahn parents: 
40786diff
changeset | 733 | |
| 27430 | 734 | end | 
| 735 | ||
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 736 | instance prod :: (finite, finite) finite | 
| 61169 | 737 | by standard (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite) | 
| 26146 | 738 | |
| 63404 | 739 | lemma inj_graph: "inj (\<lambda>f. {(x, y). y = f x})"
 | 
| 740 | 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 | 741 | |
| 26146 | 742 | instance "fun" :: (finite, finite) finite | 
| 743 | proof | |
| 63404 | 744 |   show "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
 | 
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 745 | proof (rule finite_imageD) | 
| 63404 | 746 |     let ?graph = "\<lambda>f::'a \<Rightarrow> 'b. {(x, y). y = f x}"
 | 
| 747 | have "range ?graph \<subseteq> Pow UNIV" | |
| 748 | by simp | |
| 26792 | 749 |     moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
 | 
| 750 | by (simp only: finite_Pow_iff finite) | |
| 751 | ultimately show "finite (range ?graph)" | |
| 752 | by (rule finite_subset) | |
| 63404 | 753 | show "inj ?graph" | 
| 754 | by (rule inj_graph) | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 755 | 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 | 756 | 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 | 757 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 758 | instance bool :: finite | 
| 61169 | 759 | by standard (simp add: UNIV_bool) | 
| 44831 | 760 | |
| 45962 | 761 | instance set :: (finite) finite | 
| 61169 | 762 | by standard (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite) | 
| 45962 | 763 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 764 | instance unit :: finite | 
| 61169 | 765 | by standard (simp add: UNIV_unit) | 
| 44831 | 766 | |
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46146diff
changeset | 767 | instance sum :: (finite, finite) finite | 
| 61169 | 768 | by standard (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite) | 
| 27981 | 769 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25571diff
changeset | 770 | |
| 60758 | 771 | subsection \<open>A basic fold functional for finite sets\<close> | 
| 15392 | 772 | |
| 73832 | 773 | text \<open> | 
| 774 |   The intended behaviour is \<open>fold f z {x\<^sub>1, \<dots>, x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)\<close>
 | |
| 775 | if \<open>f\<close> is ``left-commutative''. | |
| 776 | The commutativity requirement is relativised to the carrier set \<open>S\<close>: | |
| 60758 | 777 | \<close> | 
| 15392 | 778 | |
| 73832 | 779 | locale comp_fun_commute_on = | 
| 780 | fixes S :: "'a set" | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 781 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" | 
| 73832 | 782 | assumes comp_fun_commute_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> 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 | 783 | begin | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 784 | |
| 73832 | 785 | lemma fun_left_comm: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y (f x z) = f x (f y z)" | 
| 786 | using comp_fun_commute_on by (simp add: fun_eq_iff) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 787 | |
| 73832 | 788 | lemma commute_left_comp: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)" | 
| 789 | by (simp add: o_assoc comp_fun_commute_on) | |
| 51489 | 790 | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 791 | end | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 792 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 793 | inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
 | 
| 63404 | 794 | for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b | 
| 63612 | 795 | where | 
| 796 |     emptyI [intro]: "fold_graph f z {} z"
 | |
| 797 | | insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y \<Longrightarrow> fold_graph f z (insert x A) (f x y)" | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 798 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 799 | 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 | 800 | |
| 68521 | 801 | lemma fold_graph_closed_lemma: | 
| 802 | "fold_graph f z A x \<and> x \<in> B" | |
| 803 | if "fold_graph g z A x" | |
| 804 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 805 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 806 | "z \<in> B" | |
| 807 | using that(1-3) | |
| 808 | proof (induction rule: fold_graph.induct) | |
| 809 | case (insertI x A y) | |
| 810 | have "fold_graph f z A y" "y \<in> B" | |
| 811 | unfolding atomize_conj | |
| 812 | by (rule insertI.IH) (auto intro: insertI.prems) | |
| 813 | then have "g x y \<in> B" and f_eq: "f x y = g x y" | |
| 814 | by (auto simp: insertI.prems) | |
| 815 | moreover have "fold_graph f z (insert x A) (f x y)" | |
| 816 | by (rule fold_graph.insertI; fact) | |
| 817 | ultimately | |
| 818 | show ?case | |
| 819 | by (simp add: f_eq) | |
| 820 | qed (auto intro!: that) | |
| 821 | ||
| 822 | lemma fold_graph_closed_eq: | |
| 823 | "fold_graph f z A = fold_graph g z A" | |
| 824 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 825 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 826 | "z \<in> B" | |
| 827 | using fold_graph_closed_lemma[of f z A _ B g] fold_graph_closed_lemma[of g z A _ B f] that | |
| 828 | by auto | |
| 829 | ||
| 63404 | 830 | definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
 | 
| 831 | where "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)" | |
| 15392 | 832 | |
| 68521 | 833 | lemma fold_closed_eq: "fold f z A = fold g z A" | 
| 834 | if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b" | |
| 835 | "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B" | |
| 836 | "z \<in> B" | |
| 837 | unfolding Finite_Set.fold_def | |
| 838 | by (subst fold_graph_closed_eq[where B=B and g=g]) (auto simp: that) | |
| 839 | ||
| 63404 | 840 | text \<open> | 
| 73832 | 841 | A tempting alternative for the definition is | 
| 69593 | 842 | \<^term>\<open>if finite A then THE y. fold_graph f z A y else e\<close>. | 
| 63404 | 843 | It allows the removal of finiteness assumptions from the theorems | 
| 844 | \<open>fold_comm\<close>, \<open>fold_reindex\<close> and \<open>fold_distrib\<close>. | |
| 845 | The proofs become ugly. It is not worth the effort. (???) | |
| 846 | \<close> | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 847 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 848 | lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x" | 
| 63404 | 849 | by (induct rule: finite_induct) auto | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 850 | |
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 851 | |
| 69593 | 852 | subsubsection \<open>From \<^const>\<open>fold_graph\<close> to \<^term>\<open>fold\<close>\<close> | 
| 15392 | 853 | |
| 73832 | 854 | context comp_fun_commute_on | 
| 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 | 855 | 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 | 856 | |
| 51489 | 857 | lemma fold_graph_finite: | 
| 858 | assumes "fold_graph f z A y" | |
| 859 | shows "finite A" | |
| 860 | using assms by induct simp_all | |
| 861 | ||
| 36045 | 862 | lemma fold_graph_insertE_aux: | 
| 73832 | 863 | assumes "A \<subseteq> S" | 
| 864 | assumes "fold_graph f z A y" "a \<in> A" | |
| 865 |   shows "\<exists>y'. y = f a y' \<and> fold_graph f z (A - {a}) y'"
 | |
| 866 | using assms(2-,1) | |
| 36045 | 867 | proof (induct set: fold_graph) | 
| 63404 | 868 | case emptyI | 
| 869 | then show ?case by simp | |
| 870 | next | |
| 871 | case (insertI x A y) | |
| 872 | show ?case | |
| 36045 | 873 | proof (cases "x = a") | 
| 63404 | 874 | case True | 
| 875 | with insertI show ?thesis by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 876 | next | 
| 63404 | 877 | case False | 
| 36045 | 878 |     then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
 | 
| 879 | using insertI by auto | |
| 73832 | 880 | from insertI have "x \<in> S" "a \<in> S" by auto | 
| 881 | then have "f x y = f a (f x y')" | |
| 882 | unfolding y by (intro fun_left_comm; simp) | |
| 42875 | 883 |     moreover have "fold_graph f z (insert x A - {a}) (f x y')"
 | 
| 60758 | 884 | using y' and \<open>x \<noteq> a\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 885 | by (simp add: insert_Diff_if fold_graph.insertI) | 
| 63404 | 886 | ultimately show ?thesis | 
| 887 | by fast | |
| 15392 | 888 | qed | 
| 63404 | 889 | qed | 
| 36045 | 890 | |
| 891 | lemma fold_graph_insertE: | |
| 73832 | 892 | assumes "insert x A \<subseteq> S" | 
| 36045 | 893 | assumes "fold_graph f z (insert x A) v" and "x \<notin> A" | 
| 894 | obtains y where "v = f x y" and "fold_graph f z A y" | |
| 73832 | 895 | using assms by (auto dest: fold_graph_insertE_aux[OF \<open>insert x A \<subseteq> S\<close> _ insertI1]) | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 896 | |
| 73832 | 897 | lemma fold_graph_determ: | 
| 898 | assumes "A \<subseteq> S" | |
| 899 | assumes "fold_graph f z A x" "fold_graph f z A y" | |
| 900 | shows "y = x" | |
| 901 | using assms(2-,1) | |
| 36045 | 902 | proof (induct arbitrary: y set: fold_graph) | 
| 63404 | 903 | case emptyI | 
| 904 | then show ?case by fast | |
| 905 | next | |
| 36045 | 906 | case (insertI x A y v) | 
| 73832 | 907 | from \<open>insert x A \<subseteq> S\<close> and \<open>fold_graph f z (insert x A) v\<close> and \<open>x \<notin> A\<close> | 
| 36045 | 908 | obtain y' where "v = f x y'" and "fold_graph f z A y'" | 
| 909 | by (rule fold_graph_insertE) | |
| 73832 | 910 | from \<open>fold_graph f z A y'\<close> insertI have "y' = y" | 
| 911 | by simp | |
| 63404 | 912 | with \<open>v = f x y'\<close> show "v = f x y" | 
| 913 | by simp | |
| 914 | qed | |
| 15392 | 915 | |
| 73832 | 916 | lemma fold_equality: "A \<subseteq> S \<Longrightarrow> fold_graph f z A y \<Longrightarrow> fold f z A = y" | 
| 51489 | 917 | by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite) | 
| 15392 | 918 | |
| 42272 | 919 | lemma fold_graph_fold: | 
| 73832 | 920 | assumes "A \<subseteq> S" | 
| 42272 | 921 | assumes "finite A" | 
| 922 | shows "fold_graph f z A (fold f z A)" | |
| 923 | proof - | |
| 73832 | 924 | from \<open>finite A\<close> have "\<exists>x. fold_graph f z A x" | 
| 63404 | 925 | by (rule finite_imp_fold_graph) | 
| 73832 | 926 | moreover note fold_graph_determ[OF \<open>A \<subseteq> S\<close>] | 
| 63404 | 927 | ultimately have "\<exists>!x. fold_graph f z A x" | 
| 928 | by (rule ex_ex1I) | |
| 929 | then have "fold_graph f z A (The (fold_graph f z A))" | |
| 930 | by (rule theI') | |
| 931 | with assms show ?thesis | |
| 932 | by (simp add: fold_def) | |
| 42272 | 933 | qed | 
| 36045 | 934 | |
| 61799 | 935 | text \<open>The base case for \<open>fold\<close>:\<close> | 
| 15392 | 936 | |
| 63404 | 937 | lemma (in -) fold_infinite [simp]: "\<not> finite A \<Longrightarrow> fold f z A = z" | 
| 938 | by (auto simp: fold_def) | |
| 51489 | 939 | |
| 63404 | 940 | lemma (in -) fold_empty [simp]: "fold f z {} = z"
 | 
| 941 | by (auto simp: fold_def) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 942 | |
| 69593 | 943 | text \<open>The various recursion equations for \<^const>\<open>fold\<close>:\<close> | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 944 | |
| 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 | 945 | lemma fold_insert [simp]: | 
| 73832 | 946 | assumes "insert x A \<subseteq> S" | 
| 42875 | 947 | assumes "finite A" and "x \<notin> A" | 
| 948 | shows "fold f z (insert x A) = f x (fold f z A)" | |
| 73832 | 949 | proof (rule fold_equality[OF \<open>insert x A \<subseteq> S\<close>]) | 
| 51489 | 950 | fix z | 
| 73832 | 951 | from \<open>insert x A \<subseteq> S\<close> \<open>finite A\<close> have "fold_graph f z A (fold f z A)" | 
| 952 | by (blast intro: fold_graph_fold) | |
| 63404 | 953 | with \<open>x \<notin> A\<close> have "fold_graph f z (insert x A) (f x (fold f z A))" | 
| 954 | by (rule fold_graph.insertI) | |
| 955 | then show "fold_graph f z (insert x A) (f x (fold f z A))" | |
| 956 | by simp | |
| 42875 | 957 | qed | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 958 | |
| 51489 | 959 | declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del] | 
| 61799 | 960 | \<comment> \<open>No more proofs involve these.\<close> | 
| 51489 | 961 | |
| 73832 | 962 | lemma fold_fun_left_comm: | 
| 963 | assumes "insert x A \<subseteq> S" "finite A" | |
| 964 | shows "f x (fold f z A) = fold f (f x z) A" | |
| 965 | using assms(2,1) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 966 | proof (induct rule: finite_induct) | 
| 63404 | 967 | case empty | 
| 968 | then show ?case by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 969 | next | 
| 73832 | 970 | case (insert y F) | 
| 971 | then have "fold f (f x z) (insert y F) = f y (fold f (f x z) F)" | |
| 972 | by simp | |
| 973 | also have "\<dots> = f x (f y (fold f z F))" | |
| 974 | using insert by (simp add: fun_left_comm[where ?y=x]) | |
| 975 | also have "\<dots> = f x (fold f z (insert y F))" | |
| 976 | proof - | |
| 977 | from insert have "insert y F \<subseteq> S" by simp | |
| 978 | from fold_insert[OF this] insert show ?thesis by simp | |
| 979 | qed | |
| 980 | finally show ?case .. | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 981 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 982 | |
| 73832 | 983 | lemma fold_insert2: | 
| 984 | "insert x A \<subseteq> S \<Longrightarrow> finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | |
| 51489 | 985 | by (simp add: fold_fun_left_comm) | 
| 15392 | 986 | |
| 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 | 987 | lemma fold_rec: | 
| 73832 | 988 | assumes "A \<subseteq> S" | 
| 42875 | 989 | assumes "finite A" and "x \<in> A" | 
| 990 |   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 | 991 | proof - | 
| 63404 | 992 |   have A: "A = insert x (A - {x})"
 | 
| 993 | using \<open>x \<in> A\<close> by blast | |
| 994 |   then have "fold f z A = fold f z (insert x (A - {x}))"
 | |
| 995 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 996 |   also have "\<dots> = f x (fold f z (A - {x}))"
 | 
| 73832 | 997 | by (rule fold_insert) (use assms in \<open>auto\<close>) | 
| 15535 | 998 | finally show ?thesis . | 
| 999 | qed | |
| 1000 | ||
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1001 | lemma fold_insert_remove: | 
| 73832 | 1002 | assumes "insert x A \<subseteq> S" | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1003 | assumes "finite A" | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1004 |   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 | 1005 | proof - | 
| 63404 | 1006 | from \<open>finite A\<close> have "finite (insert x A)" | 
| 1007 | by auto | |
| 1008 | moreover have "x \<in> insert x A" | |
| 1009 | by auto | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1010 |   ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
 | 
| 73832 | 1011 | using \<open>insert x A \<subseteq> S\<close> by (blast intro: fold_rec) | 
| 63404 | 1012 | then show ?thesis | 
| 1013 | by simp | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1014 | qed | 
| 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1015 | |
| 57598 | 1016 | lemma fold_set_union_disj: | 
| 73832 | 1017 | assumes "A \<subseteq> S" "B \<subseteq> S" | 
| 57598 | 1018 |   assumes "finite A" "finite B" "A \<inter> B = {}"
 | 
| 1019 | shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B" | |
| 73832 | 1020 | using \<open>finite B\<close> assms(1,2,3,5) | 
| 1021 | proof induct | |
| 1022 | case (insert x F) | |
| 1023 | have "fold f z (A \<union> insert x F) = f x (fold f (fold f z A) F)" | |
| 1024 | using insert by auto | |
| 1025 | also have "\<dots> = fold f (fold f z A) (insert x F)" | |
| 1026 | using insert by (blast intro: fold_insert[symmetric]) | |
| 1027 | finally show ?case . | |
| 1028 | qed simp | |
| 1029 | ||
| 57598 | 1030 | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1031 | end | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1032 | |
| 69593 | 1033 | text \<open>Other properties of \<^const>\<open>fold\<close>:\<close> | 
| 48619 | 1034 | |
| 73832 | 1035 | lemma fold_graph_image: | 
| 1036 | assumes "inj_on g A" | |
| 1037 | shows "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A" | |
| 1038 | proof | |
| 1039 | fix w | |
| 1040 | show "fold_graph f z (g ` A) w = fold_graph (f o g) z A w" | |
| 1041 | proof | |
| 1042 | assume "fold_graph f z (g ` A) w" | |
| 1043 | then show "fold_graph (f \<circ> g) z A w" | |
| 1044 | using assms | |
| 1045 | proof (induct "g ` A" w arbitrary: A) | |
| 1046 | case emptyI | |
| 1047 | then show ?case by (auto intro: fold_graph.emptyI) | |
| 1048 | next | |
| 1049 | case (insertI x A r B) | |
| 1050 | from \<open>inj_on g B\<close> \<open>x \<notin> A\<close> \<open>insert x A = image g B\<close> obtain x' A' | |
| 1051 | where "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'" | |
| 1052 | by (rule inj_img_insertE) | |
| 1053 | from insertI.prems have "fold_graph (f \<circ> g) z A' r" | |
| 1054 | by (auto intro: insertI.hyps) | |
| 1055 | with \<open>x' \<notin> A'\<close> have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)" | |
| 1056 | by (rule fold_graph.insertI) | |
| 1057 | then show ?case | |
| 1058 | by simp | |
| 1059 | qed | |
| 1060 | next | |
| 1061 | assume "fold_graph (f \<circ> g) z A w" | |
| 1062 | then show "fold_graph f z (g ` A) w" | |
| 1063 | using assms | |
| 1064 | proof induct | |
| 1065 | case emptyI | |
| 1066 | then show ?case | |
| 1067 | by (auto intro: fold_graph.emptyI) | |
| 1068 | next | |
| 1069 | case (insertI x A r) | |
| 1070 | from \<open>x \<notin> A\<close> insertI.prems have "g x \<notin> g ` A" | |
| 1071 | by auto | |
| 1072 | moreover from insertI have "fold_graph f z (g ` A) r" | |
| 1073 | by simp | |
| 1074 | ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)" | |
| 1075 | by (rule fold_graph.insertI) | |
| 1076 | then show ?case | |
| 1077 | by simp | |
| 1078 | qed | |
| 1079 | qed | |
| 1080 | qed | |
| 1081 | ||
| 48619 | 1082 | lemma fold_image: | 
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1083 | assumes "inj_on g A" | 
| 51489 | 1084 | 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 | 1085 | proof (cases "finite A") | 
| 63404 | 1086 | case False | 
| 1087 | with assms show ?thesis | |
| 1088 | by (auto dest: finite_imageD simp add: fold_def) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1089 | next | 
| 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1090 | case True | 
| 73832 | 1091 | then show ?thesis | 
| 1092 | by (auto simp add: fold_def fold_graph_image[OF assms]) | |
| 51598 
5dbe537087aa
generalized lemma fold_image thanks to Peter Lammich
 haftmann parents: 
51546diff
changeset | 1093 | qed | 
| 15392 | 1094 | |
| 49724 | 1095 | lemma fold_cong: | 
| 73832 | 1096 | assumes "comp_fun_commute_on S f" "comp_fun_commute_on S g" | 
| 1097 | and "A \<subseteq> S" "finite A" | |
| 63404 | 1098 | and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x" | 
| 51489 | 1099 | and "s = t" and "A = B" | 
| 1100 | shows "fold f s A = fold g t B" | |
| 49724 | 1101 | proof - | 
| 63404 | 1102 | have "fold f s A = fold g s A" | 
| 73832 | 1103 | using \<open>finite A\<close> \<open>A \<subseteq> S\<close> cong | 
| 63404 | 1104 | proof (induct A) | 
| 1105 | case empty | |
| 1106 | then show ?case by simp | |
| 49724 | 1107 | next | 
| 63404 | 1108 | case insert | 
| 73832 | 1109 | interpret f: comp_fun_commute_on S f by (fact \<open>comp_fun_commute_on S f\<close>) | 
| 1110 | interpret g: comp_fun_commute_on S g by (fact \<open>comp_fun_commute_on S g\<close>) | |
| 49724 | 1111 | from insert show ?case by simp | 
| 1112 | qed | |
| 1113 | with assms show ?thesis by simp | |
| 1114 | qed | |
| 1115 | ||
| 1116 | ||
| 60758 | 1117 | text \<open>A simplified version for idempotent functions:\<close> | 
| 15480 | 1118 | |
| 73832 | 1119 | locale comp_fun_idem_on = comp_fun_commute_on + | 
| 1120 | assumes comp_fun_idem_on: "x \<in> S \<Longrightarrow> 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 | 1121 | 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 | 1122 | |
| 73832 | 1123 | lemma fun_left_idem: "x \<in> S \<Longrightarrow> f x (f x z) = f x z" | 
| 1124 | using comp_fun_idem_on by (simp add: fun_eq_iff) | |
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1125 | |
| 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 | 1126 | lemma fold_insert_idem: | 
| 73832 | 1127 | assumes "insert x A \<subseteq> S" | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1128 | assumes fin: "finite A" | 
| 51489 | 1129 | shows "fold f z (insert x A) = f x (fold f z A)" | 
| 15480 | 1130 | proof cases | 
| 28853 
69eb69659bf3
Added new fold operator and renamed the old oe to fold_image.
 nipkow parents: 
28823diff
changeset | 1131 | assume "x \<in> A" | 
| 63404 | 1132 | then obtain B where "A = insert x B" and "x \<notin> B" | 
| 1133 | by (rule set_insert) | |
| 1134 | then show ?thesis | |
| 73832 | 1135 | using assms by (simp add: comp_fun_idem_on fun_left_idem) | 
| 15480 | 1136 | next | 
| 63404 | 1137 | assume "x \<notin> A" | 
| 1138 | then show ?thesis | |
| 73832 | 1139 | using assms by auto | 
| 15480 | 1140 | qed | 
| 1141 | ||
| 51489 | 1142 | 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 | 1143 | |
| 73832 | 1144 | lemma fold_insert_idem2: "insert x A \<subseteq> S \<Longrightarrow> finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A" | 
| 51489 | 1145 | by (simp add: fold_fun_left_comm) | 
| 15484 | 1146 | |
| 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 | 1147 | 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 | 1148 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1149 | |
| 73832 | 1150 | subsubsection \<open>Liftings to \<open>comp_fun_commute_on\<close> etc.\<close> | 
| 1151 | ||
| 1152 | lemma (in comp_fun_commute_on) comp_comp_fun_commute_on: | |
| 1153 | "range g \<subseteq> S \<Longrightarrow> comp_fun_commute_on R (f \<circ> g)" | |
| 1154 | by standard (force intro: comp_fun_commute_on) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1155 | |
| 73832 | 1156 | lemma (in comp_fun_idem_on) comp_comp_fun_idem_on: | 
| 1157 | assumes "range g \<subseteq> S" | |
| 1158 | shows "comp_fun_idem_on R (f \<circ> g)" | |
| 1159 | proof | |
| 1160 | interpret f_g: comp_fun_commute_on R "f o g" | |
| 1161 | by (fact comp_comp_fun_commute_on[OF \<open>range g \<subseteq> S\<close>]) | |
| 1162 | show "x \<in> R \<Longrightarrow> y \<in> R \<Longrightarrow> (f \<circ> g) y \<circ> (f \<circ> g) x = (f \<circ> g) x \<circ> (f \<circ> g) y" for x y | |
| 1163 | by (fact f_g.comp_fun_commute_on) | |
| 1164 | qed (use \<open>range g \<subseteq> S\<close> in \<open>force intro: comp_fun_idem_on\<close>) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1165 | |
| 73832 | 1166 | lemma (in comp_fun_commute_on) comp_fun_commute_on_funpow: | 
| 1167 | "comp_fun_commute_on S (\<lambda>x. f x ^^ g x)" | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1168 | proof | 
| 73832 | 1169 | fix x y assume "x \<in> S" "y \<in> S" | 
| 1170 | show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y" | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1171 | proof (cases "x = y") | 
| 63404 | 1172 | case True | 
| 1173 | then show ?thesis by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1174 | next | 
| 63404 | 1175 | case False | 
| 1176 | show ?thesis | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1177 | proof (induct "g x" arbitrary: g) | 
| 63404 | 1178 | case 0 | 
| 1179 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1180 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1181 | case (Suc n g) | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1182 | 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 | 1183 | proof (induct "g y" arbitrary: g) | 
| 63404 | 1184 | case 0 | 
| 1185 | then show ?case by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1186 | next | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1187 | case (Suc n g) | 
| 63040 | 1188 | define h where "h z = g z - 1" for z | 
| 63404 | 1189 | with Suc have "n = h y" | 
| 1190 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1191 | 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 | 1192 | by auto | 
| 63404 | 1193 | from Suc h_def have "g y = Suc (h y)" | 
| 1194 | by simp | |
| 73832 | 1195 | with \<open>x \<in> S\<close> \<open>y \<in> S\<close> show ?case | 
| 1196 | by (simp add: comp_assoc hyp) (simp add: o_assoc comp_fun_commute_on) | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1197 | qed | 
| 63040 | 1198 | define h where "h z = (if z = x then g x - 1 else g z)" for z | 
| 63404 | 1199 | with Suc have "n = h x" | 
| 1200 | by simp | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1201 | 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 | 1202 | by auto | 
| 63404 | 1203 | with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" | 
| 1204 | by simp | |
| 1205 | from Suc h_def have "g x = Suc (h x)" | |
| 1206 | by simp | |
| 1207 | then show ?case | |
| 1208 | by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2) (simp add: comp_assoc hyp1) | |
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1209 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1210 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1211 | qed | 
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1212 | |
| 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1213 | |
| 73832 | 1214 | subsubsection \<open>\<^term>\<open>UNIV\<close> as carrier set\<close> | 
| 1215 | ||
| 1216 | locale comp_fun_commute = | |
| 1217 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" | |
| 1218 | assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y" | |
| 1219 | begin | |
| 1220 | ||
| 1221 | lemma (in -) comp_fun_commute_def': "comp_fun_commute f = comp_fun_commute_on UNIV f" | |
| 1222 | unfolding comp_fun_commute_def comp_fun_commute_on_def by blast | |
| 1223 | ||
| 1224 | text \<open> | |
| 1225 | We abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that | |
| 1226 | result from instantiating the carrier set to \<^term>\<open>UNIV\<close>. | |
| 1227 | \<close> | |
| 1228 | sublocale comp_fun_commute_on UNIV f | |
| 1229 | rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True" | |
| 1230 | and "\<And>x. x \<in> UNIV \<equiv> True" | |
| 1231 | and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P" | |
| 1232 | and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)" | |
| 1233 | proof - | |
| 1234 | show "comp_fun_commute_on UNIV f" | |
| 1235 | by standard (simp add: comp_fun_commute) | |
| 1236 | qed simp_all | |
| 1237 | ||
| 1238 | end | |
| 1239 | ||
| 1240 | lemma (in comp_fun_commute) comp_comp_fun_commute: "comp_fun_commute (f o g)" | |
| 1241 | unfolding comp_fun_commute_def' by (fact comp_comp_fun_commute_on) | |
| 1242 | ||
| 1243 | lemma (in comp_fun_commute) comp_fun_commute_funpow: "comp_fun_commute (\<lambda>x. f x ^^ g x)" | |
| 1244 | unfolding comp_fun_commute_def' by (fact comp_fun_commute_on_funpow) | |
| 1245 | ||
| 1246 | locale comp_fun_idem = comp_fun_commute + | |
| 1247 | assumes comp_fun_idem: "f x o f x = f x" | |
| 1248 | begin | |
| 1249 | ||
| 1250 | lemma (in -) comp_fun_idem_def': "comp_fun_idem f = comp_fun_idem_on UNIV f" | |
| 1251 | unfolding comp_fun_idem_on_def comp_fun_idem_def comp_fun_commute_def' | |
| 1252 | unfolding comp_fun_idem_axioms_def comp_fun_idem_on_axioms_def | |
| 1253 | by blast | |
| 1254 | ||
| 1255 | text \<open> | |
| 1256 | Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that | |
| 1257 | result from instantiating the carrier set to \<^term>\<open>UNIV\<close>. | |
| 1258 | \<close> | |
| 1259 | sublocale comp_fun_idem_on UNIV f | |
| 1260 | rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True" | |
| 1261 | and "\<And>x. x \<in> UNIV \<equiv> True" | |
| 1262 | and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P" | |
| 1263 | and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)" | |
| 1264 | proof - | |
| 1265 | show "comp_fun_idem_on UNIV f" | |
| 1266 | by standard (simp_all add: comp_fun_idem comp_fun_commute) | |
| 1267 | qed simp_all | |
| 1268 | ||
| 1269 | end | |
| 1270 | ||
| 1271 | lemma (in comp_fun_idem) comp_comp_fun_idem: "comp_fun_idem (f o g)" | |
| 1272 | unfolding comp_fun_idem_def' by (fact comp_comp_fun_idem_on) | |
| 1273 | ||
| 1274 | ||
| 69593 | 1275 | subsubsection \<open>Expressing set operations via \<^const>\<open>fold\<close>\<close> | 
| 49723 
bbc2942ba09f
alternative simplification of ^^ to the righthand side;
 haftmann parents: 
48891diff
changeset | 1276 | |
| 63404 | 1277 | lemma comp_fun_commute_const: "comp_fun_commute (\<lambda>_. f)" | 
| 75669 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1278 | by standard (rule refl) | 
| 51489 | 1279 | |
| 63404 | 1280 | lemma comp_fun_idem_insert: "comp_fun_idem insert" | 
| 1281 | by standard auto | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1282 | |
| 63404 | 1283 | lemma comp_fun_idem_remove: "comp_fun_idem Set.remove" | 
| 1284 | by standard auto | |
| 31992 | 1285 | |
| 63404 | 1286 | lemma (in semilattice_inf) comp_fun_idem_inf: "comp_fun_idem inf" | 
| 1287 | by standard (auto simp add: inf_left_commute) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1288 | |
| 63404 | 1289 | lemma (in semilattice_sup) comp_fun_idem_sup: "comp_fun_idem sup" | 
| 1290 | by standard (auto simp add: sup_left_commute) | |
| 31992 | 1291 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1292 | lemma union_fold_insert: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1293 | assumes "finite A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1294 | shows "A \<union> B = fold insert B A" | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1295 | proof - | 
| 63404 | 1296 | interpret comp_fun_idem insert | 
| 1297 | by (fact comp_fun_idem_insert) | |
| 1298 | from \<open>finite A\<close> show ?thesis | |
| 1299 | by (induct A arbitrary: B) simp_all | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1300 | qed | 
| 31992 | 1301 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1302 | lemma minus_fold_remove: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1303 | assumes "finite A" | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1304 | shows "B - A = fold Set.remove B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1305 | proof - | 
| 63404 | 1306 | interpret comp_fun_idem Set.remove | 
| 1307 | by (fact comp_fun_idem_remove) | |
| 1308 | from \<open>finite A\<close> have "fold Set.remove B A = B - A" | |
| 63612 | 1309 | by (induct A arbitrary: B) auto (* slow *) | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1310 | then show ?thesis .. | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1311 | qed | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1312 | |
| 51489 | 1313 | lemma comp_fun_commute_filter_fold: | 
| 1314 | "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')" | |
| 63404 | 1315 | proof - | 
| 48619 | 1316 | interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert) | 
| 61169 | 1317 | show ?thesis by standard (auto simp: fun_eq_iff) | 
| 48619 | 1318 | qed | 
| 1319 | ||
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1320 | lemma Set_filter_fold: | 
| 48619 | 1321 | assumes "finite A" | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1322 |   shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
 | 
| 63404 | 1323 | using assms | 
| 73832 | 1324 | proof - | 
| 1325 | interpret commute_insert: comp_fun_commute "(\<lambda>x A'. if P x then Set.insert x A' else A')" | |
| 1326 | by (fact comp_fun_commute_filter_fold) | |
| 1327 | from \<open>finite A\<close> show ?thesis | |
| 1328 | by induct (auto simp add: Set.filter_def) | |
| 1329 | qed | |
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1330 | |
| 63404 | 1331 | lemma inter_Set_filter: | 
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1332 | assumes "finite B" | 
| 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 1333 | shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B" | 
| 63404 | 1334 | using assms | 
| 1335 | by induct (auto simp: Set.filter_def) | |
| 48619 | 1336 | |
| 1337 | lemma image_fold_insert: | |
| 1338 | assumes "finite A" | |
| 1339 |   shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
 | |
| 1340 | proof - | |
| 63404 | 1341 | interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" | 
| 1342 | by standard auto | |
| 1343 | show ?thesis | |
| 1344 | using assms by (induct A) auto | |
| 48619 | 1345 | qed | 
| 1346 | ||
| 1347 | lemma Ball_fold: | |
| 1348 | assumes "finite A" | |
| 1349 | shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A" | |
| 1350 | proof - | |
| 63404 | 1351 | interpret comp_fun_commute "\<lambda>k s. s \<and> P k" | 
| 1352 | by standard auto | |
| 1353 | show ?thesis | |
| 1354 | using assms by (induct A) auto | |
| 48619 | 1355 | qed | 
| 1356 | ||
| 1357 | lemma Bex_fold: | |
| 1358 | assumes "finite A" | |
| 1359 | shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A" | |
| 1360 | proof - | |
| 63404 | 1361 | interpret comp_fun_commute "\<lambda>k s. s \<or> P k" | 
| 1362 | by standard auto | |
| 1363 | show ?thesis | |
| 1364 | using assms by (induct A) auto | |
| 48619 | 1365 | qed | 
| 1366 | ||
| 63404 | 1367 | lemma comp_fun_commute_Pow_fold: "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" | 
| 73832 | 1368 | by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast | 
| 48619 | 1369 | |
| 1370 | lemma Pow_fold: | |
| 1371 | assumes "finite A" | |
| 1372 |   shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
 | |
| 1373 | proof - | |
| 63404 | 1374 | interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" | 
| 1375 | by (rule comp_fun_commute_Pow_fold) | |
| 1376 | show ?thesis | |
| 1377 | using assms by (induct A) (auto simp: Pow_insert) | |
| 48619 | 1378 | qed | 
| 1379 | ||
| 1380 | lemma fold_union_pair: | |
| 1381 | assumes "finite B" | |
| 1382 |   shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
 | |
| 1383 | proof - | |
| 63404 | 1384 | interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" | 
| 1385 | by standard auto | |
| 1386 | show ?thesis | |
| 1387 | using assms by (induct arbitrary: A) simp_all | |
| 48619 | 1388 | qed | 
| 1389 | ||
| 63404 | 1390 | lemma comp_fun_commute_product_fold: | 
| 1391 | "finite B \<Longrightarrow> comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" | |
| 1392 | by standard (auto simp: fold_union_pair [symmetric]) | |
| 48619 | 1393 | |
| 1394 | lemma product_fold: | |
| 63404 | 1395 | assumes "finite A" "finite B" | 
| 51489 | 1396 |   shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
 | 
| 73832 | 1397 | proof - | 
| 1398 | interpret commute_product: comp_fun_commute "(\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" | |
| 1399 | by (fact comp_fun_commute_product_fold[OF \<open>finite B\<close>]) | |
| 1400 | from assms show ?thesis unfolding Sigma_def | |
| 1401 | by (induct A) (simp_all add: fold_union_pair) | |
| 1402 | qed | |
| 48619 | 1403 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1404 | context complete_lattice | 
| 31992 | 1405 | begin | 
| 1406 | ||
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1407 | lemma inf_Inf_fold_inf: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1408 | assumes "finite A" | 
| 51489 | 1409 | shows "inf (Inf A) B = fold inf B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1410 | proof - | 
| 63404 | 1411 | interpret comp_fun_idem inf | 
| 1412 | by (fact comp_fun_idem_inf) | |
| 1413 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1414 | by (induct A arbitrary: B) (simp_all add: inf_commute fun_eq_iff) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1415 | qed | 
| 31992 | 1416 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1417 | lemma sup_Sup_fold_sup: | 
| 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1418 | assumes "finite A" | 
| 51489 | 1419 | shows "sup (Sup A) B = fold sup B A" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1420 | proof - | 
| 63404 | 1421 | interpret comp_fun_idem sup | 
| 1422 | by (fact comp_fun_idem_sup) | |
| 1423 | from \<open>finite A\<close> fold_fun_left_comm show ?thesis | |
| 1424 | by (induct A arbitrary: B) (simp_all add: sup_commute fun_eq_iff) | |
| 31992 | 1425 | qed | 
| 1426 | ||
| 63404 | 1427 | lemma Inf_fold_inf: "finite A \<Longrightarrow> Inf A = fold inf top A" | 
| 1428 | using inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2) | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1429 | |
| 63404 | 1430 | lemma Sup_fold_sup: "finite A \<Longrightarrow> Sup A = fold sup bot A" | 
| 1431 | using sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2) | |
| 31992 | 1432 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1433 | lemma inf_INF_fold_inf: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1434 | assumes "finite A" | 
| 69275 | 1435 | shows "inf B (\<Sqinter>(f ` A)) = fold (inf \<circ> f) B A" (is "?inf = ?fold") | 
| 63404 | 1436 | proof - | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1437 | 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 | 1438 | interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1439 | from \<open>finite A\<close> have "?fold = ?inf" | 
| 1440 | by (induct A arbitrary: B) (simp_all add: inf_left_commute) | |
| 1441 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1442 | qed | 
| 31992 | 1443 | |
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 1444 | lemma sup_SUP_fold_sup: | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1445 | assumes "finite A" | 
| 69275 | 1446 | shows "sup B (\<Squnion>(f ` A)) = fold (sup \<circ> f) B A" (is "?sup = ?fold") | 
| 63404 | 1447 | proof - | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42869diff
changeset | 1448 | 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 | 1449 | interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem) | 
| 63404 | 1450 | from \<open>finite A\<close> have "?fold = ?sup" | 
| 1451 | by (induct A arbitrary: B) (simp_all add: sup_left_commute) | |
| 1452 | then show ?thesis .. | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1453 | qed | 
| 31992 | 1454 | |
| 69275 | 1455 | lemma INF_fold_inf: "finite A \<Longrightarrow> \<Sqinter>(f ` A) = fold (inf \<circ> f) top A" | 
| 63404 | 1456 | using inf_INF_fold_inf [of A top] by simp | 
| 31992 | 1457 | |
| 69275 | 1458 | lemma SUP_fold_sup: "finite A \<Longrightarrow> \<Squnion>(f ` A) = fold (sup \<circ> f) bot A" | 
| 63404 | 1459 | using sup_SUP_fold_sup [of A bot] by simp | 
| 31992 | 1460 | |
| 72097 | 1461 | lemma finite_Inf_in: | 
| 1462 |   assumes "finite A" "A\<noteq>{}" and inf: "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> inf x y \<in> A"
 | |
| 1463 | shows "Inf A \<in> A" | |
| 1464 | proof - | |
| 1465 |   have "Inf B \<in> A" if "B \<le> A" "B\<noteq>{}" for B
 | |
| 1466 | using finite_subset [OF \<open>B \<subseteq> A\<close> \<open>finite A\<close>] that | |
| 1467 | by (induction B) (use inf in \<open>force+\<close>) | |
| 1468 | then show ?thesis | |
| 1469 | by (simp add: assms) | |
| 1470 | qed | |
| 1471 | ||
| 1472 | lemma finite_Sup_in: | |
| 1473 |   assumes "finite A" "A\<noteq>{}" and sup: "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> sup x y \<in> A"
 | |
| 1474 | shows "Sup A \<in> A" | |
| 1475 | proof - | |
| 1476 |   have "Sup B \<in> A" if "B \<le> A" "B\<noteq>{}" for B
 | |
| 1477 | using finite_subset [OF \<open>B \<subseteq> A\<close> \<open>finite A\<close>] that | |
| 1478 | by (induction B) (use sup in \<open>force+\<close>) | |
| 1479 | then show ?thesis | |
| 1480 | by (simp add: assms) | |
| 1481 | qed | |
| 1482 | ||
| 31992 | 1483 | end | 
| 1484 | ||
| 77695 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1485 | subsubsection \<open>Expressing relation operations via \<^const>\<open>fold\<close>\<close> | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1486 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1487 | lemma Id_on_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1488 | assumes "finite A" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1489 |   shows "Id_on A = Finite_Set.fold (\<lambda>x. Set.insert (Pair x x)) {} A"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1490 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1491 | interpret comp_fun_commute "\<lambda>x. Set.insert (Pair x x)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1492 | by standard auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1493 | from assms show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1494 | unfolding Id_on_def by (induct A) simp_all | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1495 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1496 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1497 | lemma comp_fun_commute_Image_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1498 | "comp_fun_commute (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1499 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1500 | interpret comp_fun_idem Set.insert | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1501 | by (fact comp_fun_idem_insert) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1502 | show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1503 | by standard (auto simp: fun_eq_iff comp_fun_commute split: prod.split) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1504 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1505 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1506 | lemma Image_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1507 | assumes "finite R" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1508 |   shows "R `` S = Finite_Set.fold (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) {} R"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1509 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1510 | interpret comp_fun_commute "(\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1511 | by (rule comp_fun_commute_Image_fold) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1512 | have *: "\<And>x F. Set.insert x F `` S = (if fst x \<in> S then Set.insert (snd x) (F `` S) else (F `` S))" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1513 | by (force intro: rev_ImageI) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1514 | show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1515 | using assms by (induct R) (auto simp: * ) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1516 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1517 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1518 | lemma insert_relcomp_union_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1519 | assumes "finite S" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1520 |   shows "{x} O S \<union> X = Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') X S"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1521 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1522 | interpret comp_fun_commute "\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A'" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1523 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1524 | interpret comp_fun_idem Set.insert | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1525 | by (fact comp_fun_idem_insert) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1526 | show "comp_fun_commute (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A')" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1527 | by standard (auto simp add: fun_eq_iff split: prod.split) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1528 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1529 |   have *: "{x} O S = {(x', z). x' = fst x \<and> (snd x, z) \<in> S}"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1530 | by (auto simp: relcomp_unfold intro!: exI) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1531 | show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1532 | unfolding * using \<open>finite S\<close> by (induct S) (auto split: prod.split) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1533 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1534 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1535 | lemma insert_relcomp_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1536 | assumes "finite S" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1537 | shows "Set.insert x R O S = | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1538 | Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') (R O S) S" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1539 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1540 |   have "Set.insert x R O S = ({x} O S) \<union> (R O S)"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1541 | by auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1542 | then show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1543 | by (auto simp: insert_relcomp_union_fold [OF assms]) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1544 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1545 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1546 | lemma comp_fun_commute_relcomp_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1547 | assumes "finite S" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1548 | shows "comp_fun_commute (\<lambda>(x,y) A. | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1549 | Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1550 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1551 | have *: "\<And>a b A. | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1552 |     Finite_Set.fold (\<lambda>(w, z) A'. if b = w then Set.insert (a, z) A' else A') A S = {(a,b)} O S \<union> A"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1553 | by (auto simp: insert_relcomp_union_fold[OF assms] cong: if_cong) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1554 | show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1555 | by standard (auto simp: * ) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1556 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1557 | |
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1558 | lemma relcomp_fold: | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1559 | assumes "finite R" "finite S" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1560 | shows "R O S = Finite_Set.fold | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1561 |     (\<lambda>(x,y) A. Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S) {} R"
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1562 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1563 | interpret commute_relcomp_fold: comp_fun_commute | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1564 | "(\<lambda>(x, y) A. Finite_Set.fold (\<lambda>(w, z) A'. if y = w then insert (x, z) A' else A') A S)" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1565 | by (fact comp_fun_commute_relcomp_fold[OF \<open>finite S\<close>]) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1566 | from assms show ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1567 | by (induct R) (auto simp: comp_fun_commute_relcomp_fold insert_relcomp_fold cong: if_cong) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1568 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 1569 | |
| 31992 | 1570 | |
| 60758 | 1571 | subsection \<open>Locales as mini-packages for fold operations\<close> | 
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33960diff
changeset | 1572 | |
| 60758 | 1573 | subsubsection \<open>The natural case\<close> | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1574 | |
| 73832 | 1575 | locale folding_on = | 
| 1576 | fixes S :: "'a set" | |
| 63612 | 1577 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: "'b" | 
| 73832 | 1578 | assumes comp_fun_commute_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y o f x = f x o f y" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1579 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1580 | |
| 73832 | 1581 | interpretation fold?: comp_fun_commute_on S f | 
| 1582 | by standard (simp add: comp_fun_commute_on) | |
| 54867 
c21a2465cac1
prefer ephemeral interpretation over interpretation in proof contexts;
 haftmann parents: 
54611diff
changeset | 1583 | |
| 51489 | 1584 | definition F :: "'a set \<Rightarrow> 'b" | 
| 73832 | 1585 | where eq_fold: "F A = Finite_Set.fold f z A" | 
| 51489 | 1586 | |
| 73832 | 1587 | lemma empty [simp]: "F {} = z"
 | 
| 51489 | 1588 | by (simp add: eq_fold) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1589 | |
| 61169 | 1590 | lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F A = z" | 
| 51489 | 1591 | by (simp add: eq_fold) | 
| 63404 | 1592 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1593 | lemma insert [simp]: | 
| 73832 | 1594 | assumes "insert x A \<subseteq> S" and "finite A" and "x \<notin> A" | 
| 51489 | 1595 | shows "F (insert x A) = f x (F A)" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1596 | proof - | 
| 51489 | 1597 | from fold_insert assms | 
| 73832 | 1598 | have "Finite_Set.fold f z (insert x A) | 
| 1599 | = f x (Finite_Set.fold f z A)" | |
| 1600 | by simp | |
| 60758 | 1601 | with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1602 | qed | 
| 63404 | 1603 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1604 | lemma remove: | 
| 73832 | 1605 | assumes "A \<subseteq> S" and "finite A" and "x \<in> A" | 
| 51489 | 1606 |   shows "F A = f x (F (A - {x}))"
 | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1607 | proof - | 
| 60758 | 1608 | from \<open>x \<in> A\<close> obtain B where A: "A = insert x B" and "x \<notin> B" | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1609 | by (auto dest: mk_disjoint_insert) | 
| 60758 | 1610 | moreover from \<open>finite A\<close> A have "finite B" by simp | 
| 73832 | 1611 | ultimately show ?thesis | 
| 1612 | using \<open>A \<subseteq> S\<close> by auto | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1613 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1614 | |
| 73832 | 1615 | lemma insert_remove: | 
| 1616 | assumes "insert x A \<subseteq> S" and "finite A" | |
| 1617 |   shows "F (insert x A) = f x (F (A - {x}))"
 | |
| 1618 | 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 | 1619 | |
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33960diff
changeset | 1620 | end | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1621 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1622 | |
| 60758 | 1623 | subsubsection \<open>With idempotency\<close> | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1624 | |
| 73832 | 1625 | locale folding_idem_on = folding_on + | 
| 1626 | assumes comp_fun_idem_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f x \<circ> f x = f x" | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1627 | begin | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1628 | |
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1629 | declare insert [simp del] | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1630 | |
| 73832 | 1631 | interpretation fold?: comp_fun_idem_on S f | 
| 1632 | by standard (simp_all add: comp_fun_commute_on comp_fun_idem_on) | |
| 54867 
c21a2465cac1
prefer ephemeral interpretation over interpretation in proof contexts;
 haftmann parents: 
54611diff
changeset | 1633 | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1634 | lemma insert_idem [simp]: | 
| 73832 | 1635 | assumes "insert x A \<subseteq> S" and "finite A" | 
| 51489 | 1636 | shows "F (insert x A) = f x (F A)" | 
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1637 | proof - | 
| 51489 | 1638 | from fold_insert_idem assms | 
| 1639 | have "fold f z (insert x A) = f x (fold f z A)" by simp | |
| 60758 | 1640 | with \<open>finite A\<close> show ?thesis by (simp add: eq_fold fun_eq_iff) | 
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1641 | qed | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1642 | |
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1643 | end | 
| 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
35577diff
changeset | 1644 | |
| 73832 | 1645 | subsubsection \<open>\<^term>\<open>UNIV\<close> as the carrier set\<close> | 
| 1646 | ||
| 1647 | locale folding = | |
| 1648 | fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: "'b" | |
| 1649 | assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y" | |
| 1650 | begin | |
| 1651 | ||
| 1652 | lemma (in -) folding_def': "folding f = folding_on UNIV f" | |
| 1653 | unfolding folding_def folding_on_def by blast | |
| 1654 | ||
| 1655 | text \<open> | |
| 1656 | Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that | |
| 1657 | result from instantiating the carrier set to \<^term>\<open>UNIV\<close>. | |
| 1658 | \<close> | |
| 1659 | sublocale folding_on UNIV f | |
| 1660 | rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True" | |
| 1661 | and "\<And>x. x \<in> UNIV \<equiv> True" | |
| 1662 | and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P" | |
| 1663 | and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)" | |
| 1664 | proof - | |
| 1665 | show "folding_on UNIV f" | |
| 1666 | by standard (simp add: comp_fun_commute) | |
| 1667 | qed simp_all | |
| 1668 | ||
| 1669 | end | |
| 1670 | ||
| 1671 | locale folding_idem = folding + | |
| 1672 | assumes comp_fun_idem: "f x \<circ> f x = f x" | |
| 1673 | begin | |
| 1674 | ||
| 1675 | lemma (in -) folding_idem_def': "folding_idem f = folding_idem_on UNIV f" | |
| 1676 | unfolding folding_idem_def folding_def' folding_idem_on_def | |
| 1677 | unfolding folding_idem_axioms_def folding_idem_on_axioms_def | |
| 1678 | by blast | |
| 1679 | ||
| 1680 | text \<open> | |
| 1681 | Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that | |
| 1682 | result from instantiating the carrier set to \<^term>\<open>UNIV\<close>. | |
| 1683 | \<close> | |
| 1684 | sublocale folding_idem_on UNIV f | |
| 1685 | rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True" | |
| 1686 | and "\<And>x. x \<in> UNIV \<equiv> True" | |
| 1687 | and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P" | |
| 1688 | and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)" | |
| 1689 | proof - | |
| 1690 | show "folding_idem_on UNIV f" | |
| 1691 | by standard (simp add: comp_fun_idem) | |
| 1692 | qed simp_all | |
| 1693 | ||
| 1694 | end | |
| 1695 | ||
| 35817 
d8b8527102f5
added locales folding_one_(idem); various streamlining and tuning
 haftmann parents: 
35796diff
changeset | 1696 | |
| 60758 | 1697 | subsection \<open>Finite cardinality\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1698 | |
| 60758 | 1699 | text \<open> | 
| 51489 | 1700 | The traditional definition | 
| 69593 | 1701 |   \<^prop>\<open>card A \<equiv> LEAST n. \<exists>f. A = {f i |i. i < n}\<close>
 | 
| 51489 | 1702 | is ugly to work with. | 
| 69593 | 1703 | But now that we have \<^const>\<open>fold\<close> things are easy: | 
| 60758 | 1704 | \<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1705 | |
| 61890 
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
 haftmann parents: 
61810diff
changeset | 1706 | global_interpretation card: folding "\<lambda>_. Suc" 0 | 
| 73832 | 1707 | defines card = "folding_on.F (\<lambda>_. Suc) 0" | 
| 75669 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1708 | by standard (rule refl) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1709 | |
| 63404 | 1710 | lemma card_insert_disjoint: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)" | 
| 51489 | 1711 | by (fact card.insert) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1712 | |
| 63404 | 1713 | lemma card_insert_if: "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1714 | 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 | 1715 | |
| 63404 | 1716 | lemma card_ge_0_finite: "card A > 0 \<Longrightarrow> finite A" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1717 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1718 | |
| 63404 | 1719 | lemma card_0_eq [simp]: "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
 | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1720 | 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 | 1721 | |
| 63404 | 1722 | lemma finite_UNIV_card_ge_0: "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1723 | by (rule ccontr) simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1724 | |
| 63404 | 1725 | lemma card_eq_0_iff: "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
 | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1726 | by auto | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1727 | |
| 63404 | 1728 | lemma card_range_greater_zero: "finite (range f) \<Longrightarrow> card (range f) > 0" | 
| 63365 | 1729 | by (rule ccontr) (simp add: card_eq_0_iff) | 
| 1730 | ||
| 63404 | 1731 | lemma card_gt_0_iff: "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
 | 
| 1732 | by (simp add: neq0_conv [symmetric] card_eq_0_iff) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1733 | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1734 | lemma card_Suc_Diff1: | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1735 |   assumes "finite A" "x \<in> A" shows "Suc (card (A - {x})) = card A"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1736 | proof - | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1737 |   have "Suc (card (A - {x})) = card (insert x (A - {x}))"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1738 | using assms by (simp add: card.insert_remove) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1739 | also have "... = card A" | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1740 | using assms by (simp add: card_insert_if) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1741 | finally show ?thesis . | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1742 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1743 | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1744 | lemma card_insert_le_m1: | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1745 | assumes "n > 0" "card y \<le> n - 1" shows "card (insert x y) \<le> n" | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1746 | using assms | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1747 | by (cases "finite y") (auto simp: card_insert_if) | 
| 60762 | 1748 | |
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1749 | lemma card_Diff_singleton: | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1750 |   assumes "x \<in> A" shows "card (A - {x}) = card A - 1"
 | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1751 | proof (cases "finite A") | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1752 | case True | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1753 | with assms show ?thesis | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1754 | by (simp add: card_Suc_Diff1 [symmetric]) | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1755 | qed auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1756 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1757 | lemma card_Diff_singleton_if: | 
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1758 |   "card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
 | 
| 51489 | 1759 | 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 | 1760 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1761 | lemma card_Diff_insert[simp]: | 
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1762 | assumes "a \<in> A" and "a \<notin> B" | 
| 51489 | 1763 | 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 | 1764 | proof - | 
| 63404 | 1765 |   have "A - insert a B = (A - B) - {a}"
 | 
| 1766 | using assms by blast | |
| 1767 | then show ?thesis | |
| 1768 | 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 | 1769 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1770 | |
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1771 | lemma card_insert_le: "card A \<le> card (insert x A)" | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1772 | proof (cases "finite A") | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1773 | case True | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1774 | then show ?thesis by (simp add: card_insert_if) | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1775 | qed auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1776 | |
| 63404 | 1777 | lemma card_Collect_less_nat[simp]: "card {i::nat. i < n} = n"
 | 
| 1778 | by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq) | |
| 41987 | 1779 | |
| 63404 | 1780 | lemma card_Collect_le_nat[simp]: "card {i::nat. i \<le> n} = Suc n"
 | 
| 1781 | using card_Collect_less_nat[of "Suc n"] by (simp add: less_Suc_eq_le) | |
| 41987 | 1782 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1783 | lemma card_mono: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1784 | 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 | 1785 | 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 | 1786 | proof - | 
| 63404 | 1787 | from assms have "finite A" | 
| 1788 | by (auto intro: finite_subset) | |
| 1789 | then show ?thesis | |
| 1790 | using assms | |
| 1791 | proof (induct A arbitrary: B) | |
| 1792 | case empty | |
| 1793 | then show ?case by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1794 | next | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1795 | case (insert x A) | 
| 63404 | 1796 | then have "x \<in> B" | 
| 1797 | by simp | |
| 1798 |     from insert have "A \<subseteq> B - {x}" and "finite (B - {x})"
 | |
| 1799 | by auto | |
| 1800 |     with insert.hyps have "card A \<le> card (B - {x})"
 | |
| 1801 | by auto | |
| 1802 | with \<open>finite A\<close> \<open>x \<notin> A\<close> \<open>finite B\<close> \<open>x \<in> B\<close> show ?case | |
| 1803 | by simp (simp only: card.remove) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1804 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1805 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1806 | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1807 | lemma card_seteq: | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1808 | assumes "finite B" and A: "A \<subseteq> B" "card B \<le> card A" | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1809 | shows "A = B" | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1810 | using assms | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1811 | proof (induction arbitrary: A rule: finite_induct) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1812 | case (insert b B) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1813 |   then have A: "finite A" "A - {b} \<subseteq> B" 
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1814 | by force+ | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1815 |   then have "card B \<le> card (A - {b})"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1816 | using insert by (auto simp add: card_Diff_singleton_if) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1817 |   then have "A - {b} = B"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1818 | using A insert.IH by auto | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1819 | then show ?case | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1820 | using insert.hyps insert.prems by auto | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1821 | qed auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1822 | |
| 63404 | 1823 | lemma psubset_card_mono: "finite B \<Longrightarrow> A < B \<Longrightarrow> card A < card B" | 
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1824 | using card_seteq [of B A] by (auto simp add: psubset_eq) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1825 | |
| 51489 | 1826 | lemma card_Un_Int: | 
| 63404 | 1827 | assumes "finite A" "finite B" | 
| 51489 | 1828 | shows "card A + card B = card (A \<union> B) + card (A \<inter> B)" | 
| 63404 | 1829 | using assms | 
| 1830 | proof (induct A) | |
| 1831 | case empty | |
| 1832 | then show ?case by simp | |
| 51489 | 1833 | next | 
| 63404 | 1834 | case insert | 
| 1835 | then show ?case | |
| 51489 | 1836 | by (auto simp add: insert_absorb Int_insert_left) | 
| 1837 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1838 | |
| 63404 | 1839 | lemma card_Un_disjoint: "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> card (A \<union> B) = card A + card B"
 | 
| 1840 | using card_Un_Int [of A B] by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1841 | |
| 72095 
cfb6c22a5636
lemmas about sets and the enumerate operator
 paulson <lp15@cam.ac.uk> parents: 
71449diff
changeset | 1842 | lemma card_Un_disjnt: "\<lbrakk>finite A; finite B; disjnt A B\<rbrakk> \<Longrightarrow> card (A \<union> B) = card A + card B" | 
| 
cfb6c22a5636
lemmas about sets and the enumerate operator
 paulson <lp15@cam.ac.uk> parents: 
71449diff
changeset | 1843 | by (simp add: card_Un_disjoint disjnt_def) | 
| 
cfb6c22a5636
lemmas about sets and the enumerate operator
 paulson <lp15@cam.ac.uk> parents: 
71449diff
changeset | 1844 | |
| 59336 | 1845 | lemma card_Un_le: "card (A \<union> B) \<le> card A + card B" | 
| 70723 
4e39d87c9737
imported new material mostly due to Sébastien Gouëzel
 paulson <lp15@cam.ac.uk> parents: 
70178diff
changeset | 1846 | proof (cases "finite A \<and> finite B") | 
| 
4e39d87c9737
imported new material mostly due to Sébastien Gouëzel
 paulson <lp15@cam.ac.uk> parents: 
70178diff
changeset | 1847 | case True | 
| 
4e39d87c9737
imported new material mostly due to Sébastien Gouëzel
 paulson <lp15@cam.ac.uk> parents: 
70178diff
changeset | 1848 | then show ?thesis | 
| 
4e39d87c9737
imported new material mostly due to Sébastien Gouëzel
 paulson <lp15@cam.ac.uk> parents: 
70178diff
changeset | 1849 | using le_iff_add card_Un_Int [of A B] by auto | 
| 
4e39d87c9737
imported new material mostly due to Sébastien Gouëzel
 paulson <lp15@cam.ac.uk> parents: 
70178diff
changeset | 1850 | qed auto | 
| 59336 | 1851 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1852 | lemma card_Diff_subset: | 
| 63404 | 1853 | assumes "finite B" | 
| 1854 | and "B \<subseteq> A" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1855 | shows "card (A - B) = card A - card B" | 
| 63915 | 1856 | using assms | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1857 | proof (cases "finite A") | 
| 63404 | 1858 | case False | 
| 1859 | with assms show ?thesis | |
| 1860 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1861 | next | 
| 63404 | 1862 | case True | 
| 1863 | with assms show ?thesis | |
| 1864 | by (induct B arbitrary: A) simp_all | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1865 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1866 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1867 | lemma card_Diff_subset_Int: | 
| 63404 | 1868 | assumes "finite (A \<inter> B)" | 
| 1869 | shows "card (A - B) = card A - card (A \<inter> B)" | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1870 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1871 | have "A - B = A - A \<inter> B" by auto | 
| 63404 | 1872 | with assms show ?thesis | 
| 1873 | by (simp add: card_Diff_subset) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1874 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1875 | |
| 40716 | 1876 | lemma diff_card_le_card_Diff: | 
| 63404 | 1877 | assumes "finite B" | 
| 1878 | shows "card A - card B \<le> card (A - B)" | |
| 1879 | proof - | |
| 40716 | 1880 | have "card A - card B \<le> card A - card (A \<inter> B)" | 
| 1881 | using card_mono[OF assms Int_lower2, of A] by arith | |
| 63404 | 1882 | also have "\<dots> = card (A - B)" | 
| 1883 | using assms by (simp add: card_Diff_subset_Int) | |
| 40716 | 1884 | finally show ?thesis . | 
| 1885 | qed | |
| 1886 | ||
| 69312 | 1887 | lemma card_le_sym_Diff: | 
| 1888 | assumes "finite A" "finite B" "card A \<le> card B" | |
| 1889 | shows "card(A - B) \<le> card(B - A)" | |
| 1890 | proof - | |
| 1891 | have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int) | |
| 1892 | also have "\<dots> \<le> card B - card (A \<inter> B)" using assms(3) by linarith | |
| 1893 | also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute) | |
| 1894 | finally show ?thesis . | |
| 1895 | qed | |
| 1896 | ||
| 1897 | lemma card_less_sym_Diff: | |
| 1898 | assumes "finite A" "finite B" "card A < card B" | |
| 1899 | shows "card(A - B) < card(B - A)" | |
| 1900 | proof - | |
| 1901 | have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int) | |
| 1902 | also have "\<dots> < card B - card (A \<inter> B)" using assms(1,3) by (simp add: card_mono diff_less_mono) | |
| 1903 | also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute) | |
| 1904 | finally show ?thesis . | |
| 1905 | qed | |
| 1906 | ||
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1907 | lemma card_Diff1_less_iff: "card (A - {x}) < card A \<longleftrightarrow> finite A \<and> x \<in> A"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1908 | proof (cases "finite A \<and> x \<in> A") | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1909 | case True | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1910 | then show ?thesis | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1911 | by (auto simp: card_gt_0_iff intro: diff_less) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1912 | qed auto | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1913 | |
| 63404 | 1914 | lemma card_Diff1_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) < card A"
 | 
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1915 | unfolding card_Diff1_less_iff by auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1916 | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1917 | lemma card_Diff2_less: | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1918 |   assumes "finite A" "x \<in> A" "y \<in> A" shows "card (A - {x} - {y}) < card A"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1919 | proof (cases "x = y") | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1920 | case True | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1921 | with assms show ?thesis | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1922 | by (simp add: card_Diff1_less del: card_Diff_insert) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1923 | next | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1924 | case False | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1925 |   then have "card (A - {x} - {y}) < card (A - {x})" "card (A - {x}) < card A"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1926 | using assms by (intro card_Diff1_less; simp)+ | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1927 | then show ?thesis | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1928 | by (blast intro: less_trans) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 1929 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1930 | |
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1931 | lemma card_Diff1_le: "card (A - {x}) \<le> card A"
 | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1932 | proof (cases "finite A") | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1933 | case True | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1934 | then show ?thesis | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1935 | by (cases "x \<in> A") (simp_all add: card_Diff1_less less_imp_le) | 
| 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1936 | qed auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1937 | |
| 63404 | 1938 | lemma card_psubset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> card A < card B \<Longrightarrow> A < B" | 
| 1939 | by (erule psubsetI) blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1940 | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1941 | lemma card_le_inj: | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1942 | assumes fA: "finite A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1943 | and fB: "finite B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1944 | and c: "card A \<le> card B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1945 | shows "\<exists>f. f ` A \<subseteq> B \<and> inj_on f A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1946 | using fA fB c | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1947 | proof (induct arbitrary: B rule: finite_induct) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1948 | case empty | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1949 | then show ?case by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1950 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1951 | case (insert x s t) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1952 | then show ?case | 
| 63404 | 1953 | proof (induct rule: finite_induct [OF insert.prems(1)]) | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1954 | case 1 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1955 | then show ?case by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1956 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1957 | case (2 y t) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1958 | from "2.prems"(1,2,5) "2.hyps"(1,2) have cst: "card s \<le> card t" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1959 | by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1960 | from "2.prems"(3) [OF "2.hyps"(1) cst] | 
| 75669 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1961 | obtain f where *: "f ` s \<subseteq> t" "inj_on f s" | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1962 | by blast | 
| 75669 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1963 | let ?g = "(\<lambda>a. if a = x then y else f a)" | 
| 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1964 | have "?g ` insert x s \<subseteq> insert y t \<and> inj_on ?g (insert x s)" | 
| 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1965 | using * "2.prems"(2) "2.hyps"(2) unfolding inj_on_def by auto | 
| 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 1966 | then show ?case by (rule exI[where ?x="?g"]) | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1967 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1968 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1969 | |
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1970 | lemma card_subset_eq: | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1971 | assumes fB: "finite B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1972 | and AB: "A \<subseteq> B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1973 | and c: "card A = card B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1974 | shows "A = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1975 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1976 | from fB AB have fA: "finite A" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1977 | by (auto intro: finite_subset) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1978 | from fA fB have fBA: "finite (B - A)" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1979 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1980 |   have e: "A \<inter> (B - A) = {}"
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1981 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1982 | have eq: "A \<union> (B - A) = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1983 | using AB by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1984 | from card_Un_disjoint[OF fA fBA e, unfolded eq c] have "card (B - A) = 0" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1985 | by arith | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1986 |   then have "B - A = {}"
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1987 | unfolding card_eq_0_iff using fA fB by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1988 | with AB show "A = B" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1989 | by blast | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1990 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 1991 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1992 | lemma insert_partition: | 
| 63404 | 1993 |   "x \<notin> F \<Longrightarrow> \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<Longrightarrow> x \<inter> \<Union>F = {}"
 | 
| 74223 
527088d4a89b
strengthened a few lemmas about finite sets and added a code equation for complex_of_real
 paulson <lp15@cam.ac.uk> parents: 
73832diff
changeset | 1994 | by auto | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 1995 | |
| 63404 | 1996 | lemma finite_psubset_induct [consumes 1, case_names psubset]: | 
| 1997 | assumes finite: "finite A" | |
| 1998 | and major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 1999 | shows "P A" | 
| 63404 | 2000 | using finite | 
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 2001 | 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 | 2002 | 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 | 2003 | have fin: "finite A" by fact | 
| 63404 | 2004 | have ih: "card B < card A \<Longrightarrow> finite B \<Longrightarrow> P B" for B by fact | 
| 2005 | have "P B" if "B \<subset> A" for B | |
| 2006 | proof - | |
| 2007 | from that have "card B < card A" | |
| 2008 | using psubset_card_mono fin by blast | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 2009 | moreover | 
| 63404 | 2010 | from that have "B \<subseteq> A" | 
| 2011 | by auto | |
| 2012 | then have "finite B" | |
| 2013 | using fin finite_subset by blast | |
| 2014 | ultimately show ?thesis using ih by simp | |
| 2015 | qed | |
| 36079 
fa0e354e6a39
simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
 Christian Urban <urbanc@in.tum.de> parents: 
36045diff
changeset | 2016 | 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 | 2017 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2018 | |
| 63404 | 2019 | lemma finite_induct_select [consumes 1, case_names empty select]: | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2020 | assumes "finite S" | 
| 63404 | 2021 |     and "P {}"
 | 
| 2022 | and select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)" | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2023 | shows "P S" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2024 | proof - | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2025 | have "0 \<le> card S" by simp | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2026 | then have "\<exists>T \<subseteq> S. card T = card S \<and> P T" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2027 | proof (induct rule: dec_induct) | 
| 63404 | 2028 |     case base with \<open>P {}\<close>
 | 
| 2029 | show ?case | |
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2030 |       by (intro exI[of _ "{}"]) auto
 | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2031 | next | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2032 | case (step n) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2033 | then obtain T where T: "T \<subseteq> S" "card T = n" "P T" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2034 | by auto | 
| 60758 | 2035 | with \<open>n < card S\<close> have "T \<subset> S" "P T" | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2036 | by auto | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2037 | with select[of T] obtain s where "s \<in> S" "s \<notin> T" "P (insert s T)" | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2038 | by auto | 
| 60758 | 2039 | with step(2) T \<open>finite S\<close> show ?case | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2040 | by (intro exI[of _ "insert s T"]) (auto dest: finite_subset) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2041 | qed | 
| 60758 | 2042 | with \<open>finite S\<close> show "P S" | 
| 54413 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2043 | by (auto dest: card_subset_eq) | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2044 | qed | 
| 
88a036a95967
add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
 hoelzl parents: 
54148diff
changeset | 2045 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2046 | lemma remove_induct [case_names empty infinite remove]: | 
| 63404 | 2047 |   assumes empty: "P ({} :: 'a set)"
 | 
| 2048 | and infinite: "\<not> finite B \<Longrightarrow> P B" | |
| 2049 |     and remove: "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2050 | shows "P B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2051 | proof (cases "finite B") | 
| 63612 | 2052 | case False | 
| 63404 | 2053 | then show ?thesis by (rule infinite) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2054 | next | 
| 63612 | 2055 | case True | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2056 | define A where "A = B" | 
| 63612 | 2057 | with True have "finite A" "A \<subseteq> B" | 
| 2058 | by simp_all | |
| 63404 | 2059 | then show "P A" | 
| 2060 | proof (induct "card A" arbitrary: A) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2061 | case 0 | 
| 63404 | 2062 |     then have "A = {}" by auto
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2063 | with empty show ?case by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2064 | next | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2065 | case (Suc n A) | 
| 63404 | 2066 | from \<open>A \<subseteq> B\<close> and \<open>finite B\<close> have "finite A" | 
| 2067 | by (rule finite_subset) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2068 |     moreover from Suc.hyps have "A \<noteq> {}" by auto
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2069 | moreover note \<open>A \<subseteq> B\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2070 |     moreover have "P (A - {x})" if x: "x \<in> A" for x
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2071 | using x Suc.prems \<open>Suc n = card A\<close> by (intro Suc) auto | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2072 | ultimately show ?case by (rule remove) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2073 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2074 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2075 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2076 | lemma finite_remove_induct [consumes 1, case_names empty remove]: | 
| 63404 | 2077 | fixes P :: "'a set \<Rightarrow> bool" | 
| 63612 | 2078 | assumes "finite B" | 
| 2079 |     and "P {}"
 | |
| 2080 |     and "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2081 | defines "B' \<equiv> B" | 
| 63404 | 2082 | shows "P B'" | 
| 2083 | by (induct B' rule: remove_induct) (simp_all add: assms) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2084 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2085 | |
| 63404 | 2086 | text \<open>Main cardinality theorem.\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2087 | lemma card_partition [rule_format]: | 
| 63404 | 2088 | "finite C \<Longrightarrow> finite (\<Union>C) \<Longrightarrow> (\<forall>c\<in>C. card c = k) \<Longrightarrow> | 
| 2089 |     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}) \<Longrightarrow>
 | |
| 2090 | k * card C = card (\<Union>C)" | |
| 63612 | 2091 | proof (induct rule: finite_induct) | 
| 2092 | case empty | |
| 2093 | then show ?case by simp | |
| 2094 | next | |
| 2095 | case (insert x F) | |
| 2096 | then show ?case | |
| 2097 | by (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\<Union>(insert _ _)"]) | |
| 2098 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2099 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2100 | 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 | 2101 | assumes fin: "finite (UNIV :: 'a set)" | 
| 63404 | 2102 | and card: "card A = card (UNIV :: 'a set)" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2103 | shows "A = (UNIV :: 'a set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2104 | proof | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2105 | 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 | 2106 | show "UNIV \<subseteq> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2107 | proof | 
| 63404 | 2108 | show "x \<in> A" for x | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2109 | proof (rule ccontr) | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2110 | assume "x \<notin> A" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2111 | then have "A \<subset> UNIV" by auto | 
| 63404 | 2112 | with fin have "card A < card (UNIV :: 'a set)" | 
| 2113 | by (fact psubset_card_mono) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2114 | 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 | 2115 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2116 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2117 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2118 | |
| 63404 | 2119 | text \<open>The form of a finite set of given cardinality\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2120 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2121 | lemma card_eq_SucD: | 
| 63404 | 2122 | assumes "card A = Suc k" | 
| 2123 |   shows "\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {})"
 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2124 | proof - | 
| 63404 | 2125 | have fin: "finite A" | 
| 2126 | using assms by (auto intro: ccontr) | |
| 2127 | moreover have "card A \<noteq> 0" | |
| 2128 | using assms by auto | |
| 2129 | ultimately obtain b where b: "b \<in> A" | |
| 2130 | by auto | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2131 | show ?thesis | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2132 | proof (intro exI conjI) | 
| 63404 | 2133 |     show "A = insert b (A - {b})"
 | 
| 2134 | using b by blast | |
| 2135 |     show "b \<notin> A - {b}"
 | |
| 2136 | by blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2137 |     show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
 | 
| 63612 | 2138 | 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 | 2139 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2140 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2141 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2142 | lemma card_Suc_eq: | 
| 63404 | 2143 | "card A = Suc k \<longleftrightarrow> | 
| 2144 |     (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {}))"
 | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2145 | by (auto simp: card_insert_if card_gt_0_iff elim!: card_eq_SucD) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2146 | |
| 73620 | 2147 | lemma card_Suc_eq_finite: | 
| 2148 | "card A = Suc k \<longleftrightarrow> (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> finite B)" | |
| 2149 | unfolding card_Suc_eq using card_gt_0_iff by fastforce | |
| 2150 | ||
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 2151 | lemma card_1_singletonE: | 
| 63404 | 2152 | assumes "card A = 1" | 
| 2153 |   obtains x where "A = {x}"
 | |
| 61518 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 2154 | using assms by (auto simp: card_Suc_eq) | 
| 
ff12606337e9
new lemmas about topology, etc., for Cauchy integral formula
 paulson parents: 
61169diff
changeset | 2155 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2156 | lemma is_singleton_altdef: "is_singleton A \<longleftrightarrow> card A = 1" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2157 | unfolding is_singleton_def | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2158 | by (auto elim!: card_1_singletonE is_singletonE simp del: One_nat_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63040diff
changeset | 2159 | |
| 71258 
d67924987c34
a few new and tidier proofs (mostly about finite sets)
 paulson <lp15@cam.ac.uk> parents: 
70723diff
changeset | 2160 | lemma card_1_singleton_iff: "card A = Suc 0 \<longleftrightarrow> (\<exists>x. A = {x})"
 | 
| 
d67924987c34
a few new and tidier proofs (mostly about finite sets)
 paulson <lp15@cam.ac.uk> parents: 
70723diff
changeset | 2161 | by (simp add: card_Suc_eq) | 
| 
d67924987c34
a few new and tidier proofs (mostly about finite sets)
 paulson <lp15@cam.ac.uk> parents: 
70723diff
changeset | 2162 | |
| 69312 | 2163 | lemma card_le_Suc0_iff_eq: | 
| 2164 | assumes "finite A" | |
| 2165 | shows "card A \<le> Suc 0 \<longleftrightarrow> (\<forall>a1 \<in> A. \<forall>a2 \<in> A. a1 = a2)" (is "?C = ?A") | |
| 2166 | proof | |
| 2167 | assume ?C thus ?A using assms by (auto simp: le_Suc_eq dest: card_eq_SucD) | |
| 2168 | next | |
| 2169 | assume ?A | |
| 2170 | show ?C | |
| 2171 | proof cases | |
| 2172 |     assume "A = {}" thus ?C using \<open>?A\<close> by simp
 | |
| 2173 | next | |
| 2174 |     assume "A \<noteq> {}"
 | |
| 2175 |     then obtain a where "A = {a}" using \<open>?A\<close> by blast
 | |
| 2176 | thus ?C by simp | |
| 2177 | qed | |
| 2178 | qed | |
| 2179 | ||
| 63404 | 2180 | lemma card_le_Suc_iff: | 
| 69312 | 2181 | "Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)" | 
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2182 | proof (cases "finite A") | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2183 | case True | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2184 | then show ?thesis | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2185 | by (fastforce simp: card_Suc_eq less_eq_nat.simps split: nat.splits) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2186 | qed auto | 
| 44744 | 2187 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2188 | lemma finite_fun_UNIVD2: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2189 |   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 | 2190 | shows "finite (UNIV :: 'b set)" | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2191 | proof - | 
| 63404 | 2192 | from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))" for arbitrary | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 2193 | by (rule finite_imageI) | 
| 63404 | 2194 | moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)" for arbitrary | 
| 46146 
6baea4fca6bd
incorporated various theorems from theory More_Set into corpus
 haftmann parents: 
46033diff
changeset | 2195 | by (rule UNIV_eq_I) auto | 
| 63404 | 2196 | ultimately show "finite (UNIV :: 'b set)" | 
| 2197 | by simp | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2198 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2199 | |
| 48063 
f02b4302d5dd
remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
 huffman parents: 
47221diff
changeset | 2200 | 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 | 2201 | unfolding UNIV_unit by simp | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2202 | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2203 | lemma infinite_arbitrarily_large: | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2204 | assumes "\<not> finite A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2205 | shows "\<exists>B. finite B \<and> card B = n \<and> B \<subseteq> A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2206 | proof (induction n) | 
| 63404 | 2207 | case 0 | 
| 2208 |   show ?case by (intro exI[of _ "{}"]) auto
 | |
| 2209 | next | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2210 | case (Suc n) | 
| 63404 | 2211 | then obtain B where B: "finite B \<and> card B = n \<and> B \<subseteq> A" .. | 
| 60758 | 2212 | with \<open>\<not> finite A\<close> have "A \<noteq> B" by auto | 
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2213 | with B have "B \<subset> A" by auto | 
| 63404 | 2214 | then have "\<exists>x. x \<in> A - B" | 
| 2215 | by (elim psubset_imp_ex_mem) | |
| 2216 | then obtain x where x: "x \<in> A - B" .. | |
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2217 | with B have "finite (insert x B) \<and> card (insert x B) = Suc n \<and> insert x B \<subseteq> A" | 
| 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2218 | by auto | 
| 63404 | 2219 | then show "\<exists>B. finite B \<and> card B = Suc n \<and> B \<subseteq> A" .. | 
| 57447 
87429bdecad5
import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
 hoelzl parents: 
57025diff
changeset | 2220 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2221 | |
| 67457 | 2222 | text \<open>Sometimes, to prove that a set is finite, it is convenient to work with finite subsets | 
| 2223 | and to show that their cardinalities are uniformly bounded. This possibility is formalized in | |
| 2224 | the next criterion.\<close> | |
| 2225 | ||
| 2226 | lemma finite_if_finite_subsets_card_bdd: | |
| 2227 | assumes "\<And>G. G \<subseteq> F \<Longrightarrow> finite G \<Longrightarrow> card G \<le> C" | |
| 2228 | shows "finite F \<and> card F \<le> C" | |
| 2229 | proof (cases "finite F") | |
| 2230 | case False | |
| 2231 | obtain n::nat where n: "n > max C 0" by auto | |
| 2232 | obtain G where G: "G \<subseteq> F" "card G = n" using infinite_arbitrarily_large[OF False] by auto | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2233 | hence "finite G" using \<open>n > max C 0\<close> using card.infinite gr_implies_not0 by blast | 
| 67457 | 2234 | hence False using assms G n not_less by auto | 
| 2235 | thus ?thesis .. | |
| 2236 | next | |
| 2237 | case True thus ?thesis using assms[of F] by auto | |
| 2238 | qed | |
| 2239 | ||
| 75668 | 2240 | lemma obtain_subset_with_card_n: | 
| 2241 | assumes "n \<le> card S" | |
| 2242 | obtains T where "T \<subseteq> S" "card T = n" "finite T" | |
| 2243 | proof - | |
| 2244 | obtain n' where "card S = n + n'" | |
| 2245 | using le_Suc_ex[OF assms] by blast | |
| 2246 | with that show thesis | |
| 2247 | proof (induct n' arbitrary: S) | |
| 2248 | case 0 | |
| 2249 | thus ?case by (cases "finite S") auto | |
| 2250 | next | |
| 2251 | case Suc | |
| 2252 | thus ?case by (auto simp add: card_Suc_eq) | |
| 2253 | qed | |
| 2254 | qed | |
| 2255 | ||
| 2256 | lemma exists_subset_between: | |
| 2257 | assumes | |
| 2258 | "card A \<le> n" | |
| 2259 | "n \<le> card C" | |
| 2260 | "A \<subseteq> C" | |
| 2261 | "finite C" | |
| 2262 | shows "\<exists>B. A \<subseteq> B \<and> B \<subseteq> C \<and> card B = n" | |
| 2263 | using assms | |
| 2264 | proof (induct n arbitrary: A C) | |
| 2265 | case 0 | |
| 2266 |   thus ?case using finite_subset[of A C] by (intro exI[of _ "{}"], auto)
 | |
| 2267 | next | |
| 2268 | case (Suc n A C) | |
| 2269 | show ?case | |
| 2270 |   proof (cases "A = {}")
 | |
| 2271 | case True | |
| 2272 | from obtain_subset_with_card_n[OF Suc(3)] | |
| 2273 | obtain B where "B \<subseteq> C" "card B = Suc n" by blast | |
| 2274 | thus ?thesis unfolding True by blast | |
| 2275 | next | |
| 2276 | case False | |
| 2277 | then obtain a where a: "a \<in> A" by auto | |
| 2278 |     let ?A = "A - {a}" 
 | |
| 2279 |     let ?C = "C - {a}" 
 | |
| 2280 | have 1: "card ?A \<le> n" using Suc(2-) a | |
| 2281 | using finite_subset by fastforce | |
| 2282 | have 2: "card ?C \<ge> n" using Suc(2-) a by auto | |
| 2283 | from Suc(1)[OF 1 2 _ finite_subset[OF _ Suc(5)]] Suc(2-) | |
| 2284 | obtain B where "?A \<subseteq> B" "B \<subseteq> ?C" "card B = n" by blast | |
| 2285 | thus ?thesis using a Suc(2-) | |
| 2286 | by (intro exI[of _ "insert a B"], auto intro!: card_insert_disjoint finite_subset[of B C]) | |
| 2287 | qed | |
| 2288 | qed | |
| 2289 | ||
| 63404 | 2290 | |
| 60758 | 2291 | subsubsection \<open>Cardinality of image\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2292 | |
| 63404 | 2293 | lemma card_image_le: "finite A \<Longrightarrow> card (f ` A) \<le> card A" | 
| 54570 | 2294 | by (induct rule: finite_induct) (simp_all add: le_SucI card_insert_if) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2295 | |
| 63915 | 2296 | lemma card_image: "inj_on f A \<Longrightarrow> card (f ` A) = card A" | 
| 2297 | proof (induct A rule: infinite_finite_induct) | |
| 2298 | case (infinite A) | |
| 2299 | then have "\<not> finite (f ` A)" by (auto dest: finite_imageD) | |
| 2300 | with infinite show ?case by simp | |
| 2301 | qed simp_all | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2302 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2303 | lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B" | 
| 63612 | 2304 | by (auto simp: card_image bij_betw_def) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2305 | |
| 63404 | 2306 | lemma endo_inj_surj: "finite A \<Longrightarrow> f ` A \<subseteq> A \<Longrightarrow> inj_on f A \<Longrightarrow> f ` A = A" | 
| 2307 | by (simp add: card_seteq card_image) | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2308 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2309 | lemma eq_card_imp_inj_on: | 
| 63404 | 2310 | assumes "finite A" "card(f ` A) = card A" | 
| 2311 | shows "inj_on f A" | |
| 2312 | using assms | |
| 54570 | 2313 | proof (induct rule:finite_induct) | 
| 63404 | 2314 | case empty | 
| 2315 | show ?case by simp | |
| 54570 | 2316 | next | 
| 2317 | case (insert x A) | |
| 63404 | 2318 | then show ?case | 
| 2319 | using card_image_le [of A f] by (simp add: card_insert_if split: if_splits) | |
| 54570 | 2320 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2321 | |
| 63404 | 2322 | lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card (f ` A) = card A" | 
| 54570 | 2323 | by (blast intro: card_image eq_card_imp_inj_on) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2324 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2325 | lemma card_inj_on_le: | 
| 63404 | 2326 | assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" | 
| 2327 | shows "card A \<le> card B" | |
| 54570 | 2328 | proof - | 
| 63404 | 2329 | have "finite A" | 
| 2330 | using assms by (blast intro: finite_imageD dest: finite_subset) | |
| 2331 | then show ?thesis | |
| 2332 | using assms by (force intro: card_mono simp: card_image [symmetric]) | |
| 54570 | 2333 | qed | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2334 | |
| 69235 | 2335 | lemma inj_on_iff_card_le: | 
| 2336 | "\<lbrakk> finite A; finite B \<rbrakk> \<Longrightarrow> (\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)" | |
| 2337 | using card_inj_on_le[of _ A B] card_le_inj[of A B] by blast | |
| 2338 | ||
| 59504 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 2339 | lemma surj_card_le: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> card B \<le> card A" | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 2340 | by (blast intro: card_image_le card_mono le_trans) | 
| 
8c6747dba731
New lemmas and a bit of tidying up.
 paulson <lp15@cam.ac.uk> parents: 
59336diff
changeset | 2341 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2342 | lemma card_bij_eq: | 
| 63404 | 2343 | "inj_on f A \<Longrightarrow> f ` A \<subseteq> B \<Longrightarrow> inj_on g B \<Longrightarrow> g ` B \<subseteq> A \<Longrightarrow> finite A \<Longrightarrow> finite B | 
| 2344 | \<Longrightarrow> card A = card B" | |
| 2345 | by (auto intro: le_antisym card_inj_on_le) | |
| 2346 | ||
| 2347 | lemma bij_betw_finite: "bij_betw f A B \<Longrightarrow> finite A \<longleftrightarrow> finite B" | |
| 2348 | unfolding bij_betw_def using finite_imageD [of f A] by auto | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2349 | |
| 63404 | 2350 | lemma inj_on_finite: "inj_on f A \<Longrightarrow> f ` A \<le> B \<Longrightarrow> finite B \<Longrightarrow> finite A" | 
| 2351 | using finite_imageD finite_subset by blast | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2352 | |
| 74985 | 2353 | lemma card_vimage_inj_on_le: | 
| 2354 | assumes "inj_on f D" "finite A" | |
| 2355 | shows "card (f-`A \<inter> D) \<le> card A" | |
| 2356 | proof (rule card_inj_on_le) | |
| 2357 | show "inj_on f (f -` A \<inter> D)" | |
| 2358 | by (blast intro: assms inj_on_subset) | |
| 2359 | qed (use assms in auto) | |
| 2360 | ||
| 63404 | 2361 | lemma card_vimage_inj: "inj f \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> card (f -` A) = card A" | 
| 2362 | by (auto 4 3 simp: subset_image_iff inj_vimage_image_eq | |
| 2363 | intro: card_image[symmetric, OF subset_inj_on]) | |
| 55020 | 2364 | |
| 77695 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2365 | lemma card_inverse[simp]: "card (R\<inverse>) = card R" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2366 | proof - | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2367 | have *: "\<And>R. prod.swap ` R = R\<inverse>" by auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2368 |   {
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2369 | assume "\<not>finite R" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2370 | hence ?thesis | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2371 | by auto | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2372 |   } moreover {
 | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2373 | assume "finite R" | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2374 | with card_image_le[of R prod.swap] card_image_le[of "R\<inverse>" prod.swap] | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2375 | have ?thesis by (auto simp: * ) | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2376 | } ultimately show ?thesis by blast | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2377 | qed | 
| 
93531ba2c784
reversed import dependency between Relation and Finite_Set; and move theorems around
 desharna parents: 
76447diff
changeset | 2378 | |
| 60758 | 2379 | subsubsection \<open>Pigeonhole Principles\<close> | 
| 37466 | 2380 | |
| 63404 | 2381 | lemma pigeonhole: "card A > card (f ` A) \<Longrightarrow> \<not> inj_on f A " | 
| 2382 | by (auto dest: card_image less_irrefl_nat) | |
| 37466 | 2383 | |
| 2384 | lemma pigeonhole_infinite: | |
| 63404 | 2385 | assumes "\<not> finite A" and "finite (f`A)" | 
| 2386 |   shows "\<exists>a0\<in>A. \<not> finite {a\<in>A. f a = f a0}"
 | |
| 2387 | using assms(2,1) | |
| 2388 | proof (induct "f`A" arbitrary: A rule: finite_induct) | |
| 2389 | case empty | |
| 2390 | then show ?case by simp | |
| 2391 | next | |
| 2392 | case (insert b F) | |
| 2393 | show ?case | |
| 2394 |   proof (cases "finite {a\<in>A. f a = b}")
 | |
| 2395 | case True | |
| 2396 |     with \<open>\<not> finite A\<close> have "\<not> finite (A - {a\<in>A. f a = b})"
 | |
| 2397 | by simp | |
| 2398 |     also have "A - {a\<in>A. f a = b} = {a\<in>A. f a \<noteq> b}"
 | |
| 2399 | by blast | |
| 2400 |     finally have "\<not> finite {a\<in>A. f a \<noteq> b}" .
 | |
| 2401 | from insert(3)[OF _ this] insert(2,4) show ?thesis | |
| 2402 | by simp (blast intro: rev_finite_subset) | |
| 37466 | 2403 | next | 
| 63404 | 2404 | case False | 
| 2405 |     then have "{a \<in> A. f a = b} \<noteq> {}" by force
 | |
| 2406 | with False show ?thesis by blast | |
| 37466 | 2407 | qed | 
| 2408 | qed | |
| 2409 | ||
| 2410 | lemma pigeonhole_infinite_rel: | |
| 63404 | 2411 | assumes "\<not> finite A" | 
| 2412 | and "finite B" | |
| 2413 | and "\<forall>a\<in>A. \<exists>b\<in>B. R a b" | |
| 2414 |   shows "\<exists>b\<in>B. \<not> finite {a:A. R a b}"
 | |
| 37466 | 2415 | proof - | 
| 63404 | 2416 |   let ?F = "\<lambda>a. {b\<in>B. R a b}"
 | 
| 2417 | from finite_Pow_iff[THEN iffD2, OF \<open>finite B\<close>] have "finite (?F ` A)" | |
| 2418 | by (blast intro: rev_finite_subset) | |
| 2419 | from pigeonhole_infinite [where f = ?F, OF assms(1) this] | |
| 63612 | 2420 |   obtain a0 where "a0 \<in> A" and infinite: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
 | 
| 63404 | 2421 | obtain b0 where "b0 \<in> B" and "R a0 b0" | 
| 2422 | using \<open>a0 \<in> A\<close> assms(3) by blast | |
| 63612 | 2423 |   have "finite {a\<in>A. ?F a = ?F a0}" if "finite {a\<in>A. R a b0}"
 | 
| 63404 | 2424 | using \<open>b0 \<in> B\<close> \<open>R a0 b0\<close> that by (blast intro: rev_finite_subset) | 
| 63612 | 2425 | with infinite \<open>b0 \<in> B\<close> show ?thesis | 
| 63404 | 2426 | by blast | 
| 37466 | 2427 | qed | 
| 2428 | ||
| 2429 | ||
| 60758 | 2430 | subsubsection \<open>Cardinality of sums\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2431 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2432 | lemma card_Plus: | 
| 63404 | 2433 | assumes "finite A" "finite B" | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2434 | 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 | 2435 | proof - | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2436 |   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 | 2437 | with assms show ?thesis | 
| 63404 | 2438 | by (simp add: Plus_def card_Un_disjoint card_image) | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2439 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2440 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2441 | lemma card_Plus_conv_if: | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2442 | "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 | 2443 | 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 | 2444 | |
| 63404 | 2445 | text \<open>Relates to equivalence classes. Based on a theorem of F. Kammüller.\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2446 | |
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2447 | lemma dvd_partition: | 
| 63404 | 2448 | assumes f: "finite (\<Union>C)" | 
| 2449 |     and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
 | |
| 2450 | shows "k dvd card (\<Union>C)" | |
| 54570 | 2451 | proof - | 
| 63404 | 2452 | have "finite C" | 
| 54570 | 2453 | by (rule finite_UnionD [OF f]) | 
| 63404 | 2454 | then show ?thesis | 
| 2455 | using assms | |
| 54570 | 2456 | proof (induct rule: finite_induct) | 
| 63404 | 2457 | case empty | 
| 2458 | show ?case by simp | |
| 54570 | 2459 | next | 
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2460 | case (insert c C) | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2461 |     then have "c \<inter> \<Union>C = {}"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2462 | by auto | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2463 | with insert show ?case | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2464 | by (simp add: card_Un_disjoint) | 
| 54570 | 2465 | qed | 
| 2466 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2467 | |
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2468 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2469 | subsection \<open>Minimal and maximal elements of finite sets\<close> | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2470 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2471 | context begin | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2472 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2473 | qualified lemma | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2474 | assumes "finite A" and "asymp_on A R" and "transp_on A R" and "\<exists>x \<in> A. P x" | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2475 | shows | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2476 | bex_min_element_with_property: "\<exists>x \<in> A. P x \<and> (\<forall>y \<in> A. R y x \<longrightarrow> \<not> P y)" and | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2477 | bex_max_element_with_property: "\<exists>x \<in> A. P x \<and> (\<forall>y \<in> A. R x y \<longrightarrow> \<not> P y)" | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2478 | unfolding atomize_conj | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2479 | using assms | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2480 | proof (induction A rule: finite_induct) | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2481 | case empty | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2482 | hence False | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2483 | by simp_all | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2484 | thus ?case .. | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2485 | next | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2486 | case (insert x F) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2487 | |
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2488 | from insert.prems have "asymp_on F R" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2489 | using asymp_on_subset by blast | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2490 | |
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2491 | from insert.prems have "transp_on F R" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2492 | using transp_on_subset by blast | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2493 | |
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2494 | show ?case | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2495 | proof (cases "P x") | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2496 | case True | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2497 | show ?thesis | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2498 | proof (cases "\<exists>a\<in>F. P a") | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2499 | case True | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2500 | with insert.IH obtain min max where | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2501 | "min \<in> F" and "P min" and "\<forall>z \<in> F. R z min \<longrightarrow> \<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2502 | "max \<in> F" and "P max" and "\<forall>z \<in> F. R max z \<longrightarrow> \<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2503 | using \<open>asymp_on F R\<close> \<open>transp_on F R\<close> by auto | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2504 | |
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2505 | show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2506 | proof (rule conjI) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2507 | show "\<exists>y \<in> insert x F. P y \<and> (\<forall>z \<in> insert x F. R y z \<longrightarrow> \<not> P z)" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2508 | proof (cases "R max x") | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2509 | case True | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2510 | show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2511 | proof (intro bexI conjI ballI impI) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2512 | show "x \<in> insert x F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2513 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2514 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2515 | show "P x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2516 | using \<open>P x\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2517 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2518 | fix z assume "z \<in> insert x F" and "R x z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2519 | hence "z = x \<or> z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2520 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2521 | thus "\<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2522 | proof (rule disjE) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2523 | assume "z = x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2524 | hence "R x x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2525 | using \<open>R x z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2526 | moreover have "\<not> R x x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2527 | using \<open>asymp_on (insert x F) R\<close>[THEN irreflp_on_if_asymp_on, THEN irreflp_onD] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2528 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2529 | ultimately have False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2530 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2531 | thus ?thesis .. | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2532 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2533 | assume "z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2534 | moreover have "R max z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2535 | using \<open>R max x\<close> \<open>R x z\<close> | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2536 | using \<open>transp_on (insert x F) R\<close>[THEN transp_onD, of max x z] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2537 | using \<open>max \<in> F\<close> \<open>z \<in> F\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2538 | ultimately show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2539 | using \<open>\<forall>z \<in> F. R max z \<longrightarrow> \<not> P z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2540 | qed | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2541 | qed | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2542 | next | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2543 | case False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2544 | show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2545 | proof (intro bexI conjI ballI impI) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2546 | show "max \<in> insert x F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2547 | using \<open>max \<in> F\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2548 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2549 | show "P max" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2550 | using \<open>P max\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2551 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2552 | fix z assume "z \<in> insert x F" and "R max z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2553 | hence "z = x \<or> z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2554 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2555 | thus "\<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2556 | proof (rule disjE) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2557 | assume "z = x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2558 | hence False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2559 | using \<open>\<not> R max x\<close> \<open>R max z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2560 | thus ?thesis .. | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2561 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2562 | assume "z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2563 | thus ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2564 | using \<open>R max z\<close> \<open>\<forall>z\<in>F. R max z \<longrightarrow> \<not> P z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2565 | qed | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2566 | qed | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2567 | qed | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2568 | next | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2569 | show "\<exists>y \<in> insert x F. P y \<and> (\<forall>z \<in> insert x F. R z y \<longrightarrow> \<not> P z)" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2570 | proof (cases "R x min") | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2571 | case True | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2572 | show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2573 | proof (intro bexI conjI ballI impI) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2574 | show "x \<in> insert x F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2575 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2576 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2577 | show "P x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2578 | using \<open>P x\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2579 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2580 | fix z assume "z \<in> insert x F" and "R z x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2581 | hence "z = x \<or> z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2582 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2583 | thus "\<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2584 | proof (rule disjE) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2585 | assume "z = x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2586 | hence "R x x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2587 | using \<open>R z x\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2588 | moreover have "\<not> R x x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2589 | using \<open>asymp_on (insert x F) R\<close>[THEN irreflp_on_if_asymp_on, THEN irreflp_onD] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2590 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2591 | ultimately have False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2592 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2593 | thus ?thesis .. | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2594 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2595 | assume "z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2596 | moreover have "R z min" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2597 | using \<open>R z x\<close> \<open>R x min\<close> | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2598 | using \<open>transp_on (insert x F) R\<close>[THEN transp_onD, of z x min] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2599 | using \<open>min \<in> F\<close> \<open>z \<in> F\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2600 | ultimately show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2601 | using \<open>\<forall>z \<in> F. R z min \<longrightarrow> \<not> P z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2602 | qed | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2603 | qed | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2604 | next | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2605 | case False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2606 | show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2607 | proof (intro bexI conjI ballI impI) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2608 | show "min \<in> insert x F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2609 | using \<open>min \<in> F\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2610 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2611 | show "P min" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2612 | using \<open>P min\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2613 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2614 | fix z assume "z \<in> insert x F" and "R z min" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2615 | hence "z = x \<or> z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2616 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2617 | thus "\<not> P z" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2618 | proof (rule disjE) | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2619 | assume "z = x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2620 | hence False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2621 | using \<open>\<not> R x min\<close> \<open>R z min\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2622 | thus ?thesis .. | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2623 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2624 | assume "z \<in> F" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2625 | thus ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2626 | using \<open>R z min\<close> \<open>\<forall>z\<in>F. R z min \<longrightarrow> \<not> P z\<close> by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2627 | qed | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2628 | qed | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2629 | qed | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2630 | qed | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2631 | next | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2632 | case False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2633 | then show ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2634 | using \<open>\<exists>a\<in>insert x F. P a\<close> | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2635 | using \<open>asymp_on (insert x F) R\<close>[THEN asymp_onD, of x] insert_iff[of _ x F] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2636 | by blast | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2637 | qed | 
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2638 | next | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2639 | case False | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2640 | with insert.prems have "\<exists>x \<in> F. P x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2641 | by simp | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2642 | with insert.IH have | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2643 | "\<exists>y \<in> F. P y \<and> (\<forall>z\<in>F. R z y \<longrightarrow> \<not> P z)" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2644 | "\<exists>y \<in> F. P y \<and> (\<forall>z\<in>F. R y z \<longrightarrow> \<not> P z)" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2645 | using \<open>asymp_on F R\<close> \<open>transp_on F R\<close> by auto | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2646 | thus ?thesis | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2647 | using False by auto | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2648 | qed | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2649 | qed | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2650 | |
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2651 | qualified lemma | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2652 |   assumes "finite A" and "asymp_on A R" and "transp_on A R" and "A \<noteq> {}"
 | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2653 | shows | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2654 | bex_min_element: "\<exists>m \<in> A. \<forall>x \<in> A. x \<noteq> m \<longrightarrow> \<not> R x m" and | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2655 | bex_max_element: "\<exists>m \<in> A. \<forall>x \<in> A. x \<noteq> m \<longrightarrow> \<not> R m x" | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2656 |   using \<open>A \<noteq> {}\<close>
 | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2657 | bex_min_element_with_property[OF assms(1,2,3), of "\<lambda>_. True", simplified] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2658 | bex_max_element_with_property[OF assms(1,2,3), of "\<lambda>_. True", simplified] | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2659 | by blast+ | 
| 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2660 | |
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2661 | end | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2662 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2663 | text \<open>The following alternative form might sometimes be easier to work with.\<close> | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2664 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2665 | lemma is_min_element_in_set_iff: | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2666 | "asymp_on A R \<Longrightarrow> (\<forall>y \<in> A. y \<noteq> x \<longrightarrow> \<not> R y x) \<longleftrightarrow> (\<forall>y. R y x \<longrightarrow> y \<notin> A)" | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2667 | by (auto dest: asymp_onD) | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2668 | |
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2669 | lemma is_max_element_in_set_iff: | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2670 | "asymp_on A R \<Longrightarrow> (\<forall>y \<in> A. y \<noteq> x \<longrightarrow> \<not> R x y) \<longleftrightarrow> (\<forall>y. R x y \<longrightarrow> y \<notin> A)" | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2671 | by (auto dest: asymp_onD) | 
| 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2672 | |
| 77698 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2673 | context begin | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2674 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2675 | qualified lemma | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2676 |   assumes "finite A" and "A \<noteq> {}" and "transp_on A R" and "totalp_on A R"
 | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2677 | shows | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2678 | bex_least_element: "\<exists>l \<in> A. \<forall>x \<in> A. x \<noteq> l \<longrightarrow> R l x" and | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2679 | bex_greatest_element: "\<exists>g \<in> A. \<forall>x \<in> A. x \<noteq> g \<longrightarrow> R x g" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2680 | unfolding atomize_conj | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2681 | using assms | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2682 | proof (induction A rule: finite_induct) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2683 | case empty | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2684 | hence False by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2685 | thus ?case .. | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2686 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2687 | case (insert a A') | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2688 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2689 | from insert.prems(2) have transp_on_A': "transp_on A' R" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2690 | by (auto intro: transp_onI dest: transp_onD) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2691 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2692 | from insert.prems(3) have | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2693 | totalp_on_a_A'_raw: "\<forall>y \<in> A'. a \<noteq> y \<longrightarrow> R a y \<or> R y a" and | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2694 | totalp_on_A': "totalp_on A' R" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2695 | by (simp_all add: totalp_on_def) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2696 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2697 | show ?case | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2698 |   proof (cases "A' = {}")
 | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2699 | case True | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2700 | thus ?thesis by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2701 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2702 | case False | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2703 | then obtain least greatest where | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2704 | "least \<in> A'" and least_of_A': "\<forall>x\<in>A'. x \<noteq> least \<longrightarrow> R least x" and | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2705 | "greatest \<in> A'" and greatest_of_A': "\<forall>x\<in>A'. x \<noteq> greatest \<longrightarrow> R x greatest" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2706 | using insert.IH[OF _ transp_on_A' totalp_on_A'] by auto | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2707 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2708 | show ?thesis | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2709 | proof (rule conjI) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2710 | show "\<exists>l\<in>insert a A'. \<forall>x\<in>insert a A'. x \<noteq> l \<longrightarrow> R l x" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2711 | proof (cases "R a least") | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2712 | case True | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2713 | show ?thesis | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2714 | proof (intro bexI ballI impI) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2715 | show "a \<in> insert a A'" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2716 | by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2717 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2718 | fix x | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2719 | show "\<And>x. x \<in> insert a A' \<Longrightarrow> x \<noteq> a \<Longrightarrow> R a x" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2720 | using True \<open>least \<in> A'\<close> least_of_A' | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2721 | using insert.prems(2)[THEN transp_onD, of a least] | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2722 | by auto | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2723 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2724 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2725 | case False | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2726 | show ?thesis | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2727 | proof (intro bexI ballI impI) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2728 | show "least \<in> insert a A'" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2729 | using \<open>least \<in> A'\<close> by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2730 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2731 | fix x | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2732 | show "x \<in> insert a A' \<Longrightarrow> x \<noteq> least \<Longrightarrow> R least x" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2733 | using False \<open>least \<in> A'\<close> least_of_A' totalp_on_a_A'_raw | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2734 | by (cases "x = a") auto | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2735 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2736 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2737 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2738 | show "\<exists>g \<in> insert a A'. \<forall>x \<in> insert a A'. x \<noteq> g \<longrightarrow> R x g" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2739 | proof (cases "R greatest a") | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2740 | case True | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2741 | show ?thesis | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2742 | proof (intro bexI ballI impI) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2743 | show "a \<in> insert a A'" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2744 | by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2745 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2746 | fix x | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2747 | show "\<And>x. x \<in> insert a A' \<Longrightarrow> x \<noteq> a \<Longrightarrow> R x a" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2748 | using True \<open>greatest \<in> A'\<close> greatest_of_A' | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2749 | using insert.prems(2)[THEN transp_onD, of _ greatest a] | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2750 | by auto | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2751 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2752 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2753 | case False | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2754 | show ?thesis | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2755 | proof (intro bexI ballI impI) | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2756 | show "greatest \<in> insert a A'" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2757 | using \<open>greatest \<in> A'\<close> by simp | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2758 | next | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2759 | fix x | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2760 | show "x \<in> insert a A' \<Longrightarrow> x \<noteq> greatest \<Longrightarrow> R x greatest" | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2761 | using False \<open>greatest \<in> A'\<close> greatest_of_A' totalp_on_a_A'_raw | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2762 | by (cases "x = a") auto | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2763 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2764 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2765 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2766 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2767 | qed | 
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2768 | |
| 
51ed312cabeb
added lemmas Finite_Set.bex_least_element and Finite_Set.bex_greatest_element
 desharna parents: 
77697diff
changeset | 2769 | end | 
| 77696 
9c7cbad50e04
added lemmas Finite_Set.bex_min_element and Finite_Set.bex_max_element
 desharna parents: 
77695diff
changeset | 2770 | |
| 72384 | 2771 | subsubsection \<open>Finite orders\<close> | 
| 2772 | ||
| 2773 | context order | |
| 2774 | begin | |
| 2775 | ||
| 2776 | lemma finite_has_maximal: | |
| 77697 | 2777 |   assumes "finite A" and "A \<noteq> {}"
 | 
| 2778 | shows "\<exists> m \<in> A. \<forall> b \<in> A. m \<le> b \<longrightarrow> m = b" | |
| 2779 | proof - | |
| 2780 | obtain m where "m \<in> A" and m_is_max: "\<forall>x\<in>A. x \<noteq> m \<longrightarrow> \<not> m < x" | |
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2781 |     using Finite_Set.bex_max_element[OF \<open>finite A\<close> _ _ \<open>A \<noteq> {}\<close>, of "(<)"] by auto
 | 
| 77697 | 2782 | moreover have "\<forall>b \<in> A. m \<le> b \<longrightarrow> m = b" | 
| 2783 | using m_is_max by (auto simp: le_less) | |
| 2784 | ultimately show ?thesis | |
| 2785 | by auto | |
| 72384 | 2786 | qed | 
| 2787 | ||
| 2788 | lemma finite_has_maximal2: | |
| 2789 | "\<lbrakk> finite A; a \<in> A \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. a \<le> m \<and> (\<forall> b \<in> A. m \<le> b \<longrightarrow> m = b)" | |
| 2790 | using finite_has_maximal[of "{b \<in> A. a \<le> b}"] by fastforce
 | |
| 2791 | ||
| 2792 | lemma finite_has_minimal: | |
| 77697 | 2793 |   assumes "finite A" and "A \<noteq> {}"
 | 
| 2794 | shows "\<exists> m \<in> A. \<forall> b \<in> A. b \<le> m \<longrightarrow> m = b" | |
| 2795 | proof - | |
| 2796 | obtain m where "m \<in> A" and m_is_min: "\<forall>x\<in>A. x \<noteq> m \<longrightarrow> \<not> x < m" | |
| 78014 
24f0cd70790b
added lemmas Finite_Set.bex_(min|max)_element_with_property and reordered assumptions of Finite_Set.bex_(min|max)_element
 desharna parents: 
77698diff
changeset | 2797 |     using Finite_Set.bex_min_element[OF \<open>finite A\<close> _ _ \<open>A \<noteq> {}\<close>, of "(<)"] by auto
 | 
| 77697 | 2798 | moreover have "\<forall>b \<in> A. b \<le> m \<longrightarrow> m = b" | 
| 2799 | using m_is_min by (auto simp: le_less) | |
| 2800 | ultimately show ?thesis | |
| 2801 | by auto | |
| 72384 | 2802 | qed | 
| 2803 | ||
| 2804 | lemma finite_has_minimal2: | |
| 2805 | "\<lbrakk> finite A; a \<in> A \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. m \<le> a \<and> (\<forall> b \<in> A. b \<le> m \<longrightarrow> m = b)" | |
| 2806 | using finite_has_minimal[of "{b \<in> A. b \<le> a}"] by fastforce
 | |
| 2807 | ||
| 2808 | end | |
| 63404 | 2809 | |
| 60758 | 2810 | subsubsection \<open>Relating injectivity and surjectivity\<close> | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2811 | |
| 63404 | 2812 | lemma finite_surj_inj: | 
| 2813 | assumes "finite A" "A \<subseteq> f ` A" | |
| 2814 | shows "inj_on f A" | |
| 54570 | 2815 | proof - | 
| 63404 | 2816 | have "f ` A = A" | 
| 54570 | 2817 | by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le) | 
| 2818 | then show ?thesis using assms | |
| 2819 | by (simp add: eq_card_imp_inj_on) | |
| 2820 | qed | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2821 | |
| 63612 | 2822 | lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f" | 
| 2823 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 2824 | 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 | 2825 | |
| 63612 | 2826 | lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f" | 
| 2827 | for f :: "'a \<Rightarrow> 'a" | |
| 63404 | 2828 | 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 | 2829 | |
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2830 | lemma surjective_iff_injective_gen: | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2831 | assumes fS: "finite S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2832 | and fT: "finite T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2833 | and c: "card S = card T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2834 | and ST: "f ` S \<subseteq> T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2835 | shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2836 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2837 | proof | 
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2838 | assume h: "?lhs" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2839 |   {
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2840 | fix x y | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2841 | assume x: "x \<in> S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2842 | assume y: "y \<in> S" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2843 | assume f: "f x = f y" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2844 | from x fS have S0: "card S \<noteq> 0" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2845 | by auto | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2846 | have "x = y" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2847 | proof (rule ccontr) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2848 | assume xy: "\<not> ?thesis" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2849 |       have th: "card S \<le> card (f ` (S - {y}))"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2850 | unfolding c | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2851 | proof (rule card_mono) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2852 |         show "finite (f ` (S - {y}))"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2853 | by (simp add: fS) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2854 | have "\<lbrakk>x \<noteq> y; x \<in> S; z \<in> S; f x = f y\<rbrakk> | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2855 | \<Longrightarrow> \<exists>x \<in> S. x \<noteq> y \<and> f z = f x" for z | 
| 75669 
43f5dfb7fa35
tuned (some HOL lints, by Yecine Megdiche);
 Fabian Huch <huch@in.tum.de> parents: 
75668diff
changeset | 2856 | by (cases "z = y \<longrightarrow> z = x") auto | 
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2857 |         then show "T \<subseteq> f ` (S - {y})"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2858 | using h xy x y f by fastforce | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2859 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2860 |       also have " \<dots> \<le> card (S - {y})"
 | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2861 | by (simp add: card_image_le fS) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2862 | also have "\<dots> \<le> card S - 1" using y fS by simp | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2863 | finally show False using S0 by arith | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2864 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2865 | } | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2866 | then show ?rhs | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2867 | unfolding inj_on_def by blast | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2868 | next | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2869 | assume h: ?rhs | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2870 | have "f ` S = T" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2871 | by (simp add: ST c card_image card_subset_eq fT h) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2872 | then show ?lhs by blast | 
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2873 | qed | 
| 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 2874 | |
| 49758 
718f10c8bbfc
use Set.filter instead of Finite_Set.filter, which is removed then
 kuncar parents: 
49757diff
changeset | 2875 | hide_const (open) Finite_Set.fold | 
| 46033 | 2876 | |
| 61810 | 2877 | |
| 63404 | 2878 | subsection \<open>Infinite Sets\<close> | 
| 61810 | 2879 | |
| 2880 | text \<open> | |
| 2881 | Some elementary facts about infinite sets, mostly by Stephan Merz. | |
| 2882 | Beware! Because "infinite" merely abbreviates a negation, these | |
| 2883 | lemmas may not work well with \<open>blast\<close>. | |
| 2884 | \<close> | |
| 2885 | ||
| 2886 | abbreviation infinite :: "'a set \<Rightarrow> bool" | |
| 2887 | where "infinite S \<equiv> \<not> finite S" | |
| 2888 | ||
| 2889 | text \<open> | |
| 2890 | Infinite sets are non-empty, and if we remove some elements from an | |
| 2891 | infinite set, the result is still infinite. | |
| 2892 | \<close> | |
| 2893 | ||
| 70019 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2894 | lemma infinite_UNIV_nat [iff]: "infinite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2895 | proof | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2896 | assume "finite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2897 | with finite_UNIV_inj_surj [of Suc] show False | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2898 | by simp (blast dest: Suc_neq_Zero surjD) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2899 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2900 | |
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2901 | lemma infinite_UNIV_char_0: "infinite (UNIV :: 'a::semiring_char_0 set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2902 | proof | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2903 | assume "finite (UNIV :: 'a set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2904 | with subset_UNIV have "finite (range of_nat :: 'a set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2905 | by (rule finite_subset) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2906 | moreover have "inj (of_nat :: nat \<Rightarrow> 'a)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2907 | by (simp add: inj_on_def) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2908 | ultimately have "finite (UNIV :: nat set)" | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2909 | by (rule finite_imageD) | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2910 | then show False | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2911 | by simp | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2912 | qed | 
| 
095dce9892e8
A few results in Algebra, and bits for Analysis
 paulson <lp15@cam.ac.uk> parents: 
69735diff
changeset | 2913 | |
| 61810 | 2914 | lemma infinite_imp_nonempty: "infinite S \<Longrightarrow> S \<noteq> {}"
 | 
| 2915 | by auto | |
| 2916 | ||
| 2917 | lemma infinite_remove: "infinite S \<Longrightarrow> infinite (S - {a})"
 | |
| 2918 | by simp | |
| 2919 | ||
| 2920 | lemma Diff_infinite_finite: | |
| 63404 | 2921 | assumes "finite T" "infinite S" | 
| 61810 | 2922 | shows "infinite (S - T)" | 
| 63404 | 2923 | using \<open>finite T\<close> | 
| 61810 | 2924 | proof induct | 
| 63404 | 2925 |   from \<open>infinite S\<close> show "infinite (S - {})"
 | 
| 2926 | by auto | |
| 61810 | 2927 | next | 
| 2928 | fix T x | |
| 2929 | assume ih: "infinite (S - T)" | |
| 2930 |   have "S - (insert x T) = (S - T) - {x}"
 | |
| 2931 | by (rule Diff_insert) | |
| 63404 | 2932 | with ih show "infinite (S - (insert x T))" | 
| 61810 | 2933 | by (simp add: infinite_remove) | 
| 2934 | qed | |
| 2935 | ||
| 2936 | lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)" | |
| 2937 | by simp | |
| 2938 | ||
| 2939 | lemma infinite_Un: "infinite (S \<union> T) \<longleftrightarrow> infinite S \<or> infinite T" | |
| 2940 | by simp | |
| 2941 | ||
| 2942 | lemma infinite_super: | |
| 63404 | 2943 | assumes "S \<subseteq> T" | 
| 2944 | and "infinite S" | |
| 61810 | 2945 | shows "infinite T" | 
| 2946 | proof | |
| 2947 | assume "finite T" | |
| 63404 | 2948 | with \<open>S \<subseteq> T\<close> have "finite S" by (simp add: finite_subset) | 
| 2949 | with \<open>infinite S\<close> show False by simp | |
| 61810 | 2950 | qed | 
| 2951 | ||
| 2952 | proposition infinite_coinduct [consumes 1, case_names infinite]: | |
| 2953 | assumes "X A" | |
| 63404 | 2954 |     and step: "\<And>A. X A \<Longrightarrow> \<exists>x\<in>A. X (A - {x}) \<or> infinite (A - {x})"
 | 
| 61810 | 2955 | shows "infinite A" | 
| 2956 | proof | |
| 2957 | assume "finite A" | |
| 63404 | 2958 | then show False | 
| 2959 | using \<open>X A\<close> | |
| 61810 | 2960 | proof (induction rule: finite_psubset_induct) | 
| 2961 | case (psubset A) | |
| 2962 |     then obtain x where "x \<in> A" "X (A - {x}) \<or> infinite (A - {x})"
 | |
| 2963 | using local.step psubset.prems by blast | |
| 2964 |     then have "X (A - {x})"
 | |
| 2965 | using psubset.hyps by blast | |
| 2966 | show False | |
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2967 |     proof (rule psubset.IH [where B = "A - {x}"])
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2968 |       show "A - {x} \<subset> A"
 | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2969 | using \<open>x \<in> A\<close> by blast | 
| 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
72097diff
changeset | 2970 | qed fact | 
| 61810 | 2971 | qed | 
| 2972 | qed | |
| 2973 | ||
| 2974 | text \<open> | |
| 2975 | For any function with infinite domain and finite range there is some | |
| 2976 | element that is the image of infinitely many domain elements. In | |
| 2977 | particular, any infinite sequence of elements from a finite set | |
| 2978 | contains some element that occurs infinitely often. | |
| 2979 | \<close> | |
| 2980 | ||
| 2981 | lemma inf_img_fin_dom': | |
| 63404 | 2982 | assumes img: "finite (f ` A)" | 
| 2983 | and dom: "infinite A" | |
| 61810 | 2984 |   shows "\<exists>y \<in> f ` A. infinite (f -` {y} \<inter> A)"
 | 
| 2985 | proof (rule ccontr) | |
| 2986 |   have "A \<subseteq> (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by auto
 | |
| 63404 | 2987 | moreover assume "\<not> ?thesis" | 
| 61810 | 2988 |   with img have "finite (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by blast
 | 
| 63404 | 2989 | ultimately have "finite A" by (rule finite_subset) | 
| 61810 | 2990 | with dom show False by contradiction | 
| 2991 | qed | |
| 2992 | ||
| 2993 | lemma inf_img_fin_domE': | |
| 2994 | assumes "finite (f ` A)" and "infinite A" | |
| 2995 |   obtains y where "y \<in> f`A" and "infinite (f -` {y} \<inter> A)"
 | |
| 2996 | using assms by (blast dest: inf_img_fin_dom') | |
| 2997 | ||
| 2998 | lemma inf_img_fin_dom: | |
| 2999 | assumes img: "finite (f`A)" and dom: "infinite A" | |
| 3000 |   shows "\<exists>y \<in> f`A. infinite (f -` {y})"
 | |
| 63404 | 3001 | using inf_img_fin_dom'[OF assms] by auto | 
| 61810 | 3002 | |
| 3003 | lemma inf_img_fin_domE: | |
| 3004 | assumes "finite (f`A)" and "infinite A" | |
| 3005 |   obtains y where "y \<in> f`A" and "infinite (f -` {y})"
 | |
| 3006 | using assms by (blast dest: inf_img_fin_dom) | |
| 3007 | ||
| 63404 | 3008 | proposition finite_image_absD: "finite (abs ` S) \<Longrightarrow> finite S" | 
| 3009 | for S :: "'a::linordered_ring set" | |
| 61810 | 3010 | by (rule ccontr) (auto simp: abs_eq_iff vimage_def dest: inf_img_fin_dom) | 
| 3011 | ||
| 73555 | 3012 | |
| 69735 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3013 | subsection \<open>The finite powerset operator\<close> | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3014 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3015 | definition Fpow :: "'a set \<Rightarrow> 'a set set" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3016 | where "Fpow A \<equiv> {X. X \<subseteq> A \<and> finite X}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3017 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3018 | lemma Fpow_mono: "A \<subseteq> B \<Longrightarrow> Fpow A \<subseteq> Fpow B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3019 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3020 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3021 | lemma empty_in_Fpow: "{} \<in> Fpow A"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3022 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3023 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3024 | lemma Fpow_not_empty: "Fpow A \<noteq> {}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3025 | using empty_in_Fpow by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3026 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3027 | lemma Fpow_subset_Pow: "Fpow A \<subseteq> Pow A" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3028 | unfolding Fpow_def by auto | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3029 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3030 | lemma Fpow_Pow_finite: "Fpow A = Pow A Int {A. finite A}"
 | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3031 | unfolding Fpow_def Pow_def by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3032 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3033 | lemma inj_on_image_Fpow: | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3034 | assumes "inj_on f A" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3035 | shows "inj_on (image f) (Fpow A)" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3036 | using assms Fpow_subset_Pow[of A] subset_inj_on[of "image f" "Pow A"] | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3037 | inj_on_image_Pow by blast | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3038 | |
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3039 | lemma image_Fpow_mono: | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3040 | assumes "f ` A \<subseteq> B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3041 | shows "(image f) ` (Fpow A) \<subseteq> Fpow B" | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3042 | using assms by(unfold Fpow_def, auto) | 
| 
8230dca028eb
the theory of Equipollence, and moving Fpow from Cardinals into Main
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 3043 | |
| 35722 
69419a09a7ff
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
 haftmann parents: 
35719diff
changeset | 3044 | end |