diff -r fb63942e470e -r 7195acc2fe93 src/HOL/Finite_Set.thy --- a/src/HOL/Finite_Set.thy Fri Aug 05 16:36:03 2016 +0200 +++ b/src/HOL/Finite_Set.thy Fri Aug 05 18:14:28 2016 +0200 @@ -1,24 +1,26 @@ (* Title: HOL/Finite_Set.thy - Author: Tobias Nipkow, Lawrence C Paulson and Markus Wenzel - with contributions by Jeremy Avigad and Andrei Popescu + Author: Tobias Nipkow + Author: Lawrence C Paulson + Author: Markus Wenzel + Author: Jeremy Avigad + Author: Andrei Popescu *) section \Finite sets\ theory Finite_Set -imports Product_Type Sum_Type Fields + imports Product_Type Sum_Type Fields begin subsection \Predicate for finite sets\ -context - notes [[inductive_internals]] +context notes [[inductive_internals]] begin inductive finite :: "'a set \ bool" -where - emptyI [simp, intro!]: "finite {}" -| insertI [simp, intro!]: "finite A \ finite (insert a A)" + where + emptyI [simp, intro!]: "finite {}" + | insertI [simp, intro!]: "finite A \ finite (insert a A)" end @@ -145,7 +147,7 @@ shows "\f n::nat. f ` A = {i. i < n} \ inj_on f A" proof - from finite_imp_nat_seg_image_inj_on [OF \finite A\] - obtain f and n::nat where bij: "bij_betw f {i. i ?f ` A = {i. i inj_on h A \ finite (h -` F \ A)" @@ -328,7 +330,8 @@ done lemma finite_finite_vimage_IntI: - assumes "finite F" and "\y. y \ F \ finite ((h -` {y}) \ A)" + assumes "finite F" + and "\y. y \ F \ finite ((h -` {y}) \ A)" shows "finite (h -` F \ A)" proof - have *: "h -` F \ A = (\ y\F. (h -` {y}) \ A)" @@ -464,7 +467,7 @@ proof assume "finite (Pow A)" then have "finite ((\x. {x}) ` A)" - by (blast intro: finite_subset) + by (blast intro: finite_subset) (* somewhat slow *) then show "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp next @@ -492,7 +495,7 @@ from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" by simp have 2: "inj_on ?F ?S" - by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff) + by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff) (* somewhat slow *) show ?thesis by (rule finite_imageD [OF 1 2]) qed @@ -524,7 +527,7 @@ lemma finite_subset_induct [consumes 2, case_names empty insert]: assumes "finite F" and "F \ A" - assumes empty: "P {}" + and empty: "P {}" and insert: "\a F. finite F \ a \ A \ a \ F \ P F \ P (insert a F)" shows "P F" using \finite F\ \F \ A\ @@ -545,7 +548,7 @@ lemma finite_empty_induct: assumes "finite A" - assumes "P A" + and "P A" and remove: "\a A. finite A \ a \ A \ P A \ P (A - {a})" shows "P {}" proof - @@ -604,8 +607,8 @@ lemma finite_subset_induct' [consumes 2, case_names empty insert]: assumes "finite F" and "F \ A" - and empty: "P {}" - and insert: "\a F. \finite F; a \ A; F \ A; a \ F; P F \ \ P (insert a F)" + and empty: "P {}" + and insert: "\a F. \finite F; a \ A; F \ A; a \ F; P F \ \ P (insert a F)" shows "P F" proof - from \finite F\ @@ -632,7 +635,8 @@ subsection \Class \finite\\ -class finite = assumes finite_UNIV: "finite (UNIV :: 'a set)" +class finite = + assumes finite_UNIV: "finite (UNIV :: 'a set)" begin lemma finite [simp]: "finite (A :: 'a set)" @@ -700,9 +704,9 @@ inductive fold_graph :: "('a \ 'b \ 'b) \ 'b \ 'a set \ 'b \ bool" for f :: "'a \ 'b \ 'b" and z :: 'b -where - emptyI [intro]: "fold_graph f z {} z" -| insertI [intro]: "x \ A \ fold_graph f z A y \ fold_graph f z (insert x A) (f x y)" + where + emptyI [intro]: "fold_graph f z {} z" + | insertI [intro]: "x \ A \ fold_graph f z A y \ fold_graph f z (insert x A) (f x y)" inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x" @@ -1069,7 +1073,7 @@ interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove) from \finite A\ have "fold Set.remove B A = B - A" - by (induct A arbitrary: B) auto + by (induct A arbitrary: B) auto (* slow *) then show ?thesis .. qed @@ -1124,7 +1128,7 @@ qed lemma comp_fun_commute_Pow_fold: "comp_fun_commute (\x A. A \ Set.insert x ` A)" - by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast + by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast (* somewhat slow *) lemma Pow_fold: assumes "finite A" @@ -1222,13 +1226,12 @@ subsubsection \The natural case\ locale folding = - fixes f :: "'a \ 'b \ 'b" - fixes z :: "'b" + fixes f :: "'a \ 'b \ 'b" and z :: "'b" assumes comp_fun_commute: "f y \ f x = f x \ f y" begin interpretation fold?: comp_fun_commute f - by standard (insert comp_fun_commute, simp add: fun_eq_iff) + by standard (use comp_fun_commute in \simp add: fun_eq_iff\) definition F :: "'a set \ 'b" where eq_fold: "F A = fold f z A" @@ -1332,14 +1335,14 @@ lemma card_Suc_Diff1: "finite A \ x \ A \ Suc (card (A - {x})) = card A" apply (rule insert_Diff [THEN subst, where t = A]) - apply assumption + apply assumption apply (simp del: insert_Diff_single) done lemma card_insert_le_m1: "n > 0 \ card y \ n - 1 \ card (insert x y) \ n" apply (cases "finite y") - apply (cases "x \ y") - apply (auto simp: insert_absorb) + apply (cases "x \ y") + apply (auto simp: insert_absorb) done lemma card_Diff_singleton: "finite A \ x \ A \ card (A - {x}) = card A - 1" @@ -1397,7 +1400,7 @@ lemma card_seteq: "finite B \ (\A. A \ B \ card B \ card A \ A = B)" apply (induct rule: finite_induct) - apply simp + apply simp apply clarify apply (subgoal_tac "finite A \ A - {x} \ F") prefer 2 apply (blast intro: finite_subset, atomize) @@ -1430,7 +1433,7 @@ lemma card_Un_le: "card (A \ B) \ card A + card B" apply (cases "finite A") apply (cases "finite B") - using le_iff_add card_Un_Int apply blast + apply (use le_iff_add card_Un_Int in blast) apply simp apply simp done @@ -1539,7 +1542,7 @@ lemma insert_partition: "x \ F \ \c1 \ insert x F. \c2 \ insert x F. c1 \ c2 \ c1 \ c2 = {} \ x \ \F = {}" - by auto + by auto (* somewhat slow *) lemma finite_psubset_induct [consumes 1, case_names psubset]: assumes finite: "finite A" @@ -1597,13 +1600,13 @@ and remove: "\A. finite A \ A \ {} \ A \ B \ (\x. x \ A \ P (A - {x})) \ P A" shows "P B" proof (cases "finite B") - assume "\finite B" + case False then show ?thesis by (rule infinite) next + case True define A where "A = B" - assume "finite B" - then have "finite A" "A \ B" - by (simp_all add: A_def) + with True have "finite A" "A \ B" + by simp_all then show "P A" proof (induct "card A" arbitrary: A) case 0 @@ -1623,9 +1626,9 @@ lemma finite_remove_induct [consumes 1, case_names empty remove]: fixes P :: "'a set \ bool" - assumes finite: "finite B" - and empty: "P {}" - and rm: "\A. finite A \ A \ {} \ A \ B \ (\x. x \ A \ P (A - {x})) \ P A" + assumes "finite B" + and "P {}" + and "\A. finite A \ A \ {} \ A \ B \ (\x. x \ A \ P (A - {x})) \ P A" defines "B' \ B" shows "P B'" by (induct B' rule: remove_induct) (simp_all add: assms) @@ -1636,10 +1639,14 @@ "finite C \ finite (\C) \ (\c\C. card c = k) \ (\c1 \ C. \c2 \ C. c1 \ c2 \ c1 \ c2 = {}) \ k * card C = card (\C)" - apply (induct rule: finite_induct) - apply simp - apply (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\(insert x F)"]) - done +proof (induct rule: finite_induct) + case empty + then show ?case by simp +next + case (insert x F) + then show ?case + by (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\(insert _ _)"]) +qed lemma card_eq_UNIV_imp_eq_UNIV: assumes fin: "finite (UNIV :: 'a set)" @@ -1679,7 +1686,7 @@ show "b \ A - {b}" by blast show "card (A - {b}) = k" and "k = 0 \ A - {b} = {}" - using assms b fin by(fastforce dest:mk_disjoint_insert)+ + using assms b fin by (fastforce dest: mk_disjoint_insert)+ qed qed @@ -1688,7 +1695,7 @@ (\b B. A = insert b B \ b \ B \ card B = k \ (k = 0 \ B = {}))" apply (auto elim!: card_eq_SucD) apply (subst card.insert) - apply (auto simp add: intro:ccontr) + apply (auto simp add: intro:ccontr) done lemma card_1_singletonE: @@ -1761,7 +1768,7 @@ qed lemma bij_betw_same_card: "bij_betw f A B \ card A = card B" - by(auto simp: card_image bij_betw_def) + by (auto simp: card_image bij_betw_def) lemma endo_inj_surj: "finite A \ f ` A \ A \ inj_on f A \ f ` A = A" by (simp add: card_seteq card_image) @@ -1852,12 +1859,12 @@ from finite_Pow_iff[THEN iffD2, OF \finite B\] have "finite (?F ` A)" by (blast intro: rev_finite_subset) from pigeonhole_infinite [where f = ?F, OF assms(1) this] - obtain a0 where "a0 \ A" and 1: "\ finite {a\A. ?F a = ?F a0}" .. + obtain a0 where "a0 \ A" and infinite: "\ finite {a\A. ?F a = ?F a0}" .. obtain b0 where "b0 \ B" and "R a0 b0" using \a0 \ A\ assms(3) by blast - have "finite {a\A. ?F a = ?F a0}" if "finite{a:A. R a b0}" + have "finite {a\A. ?F a = ?F a0}" if "finite {a\A. R a b0}" using \b0 \ B\ \R a0 b0\ that by (blast intro: rev_finite_subset) - with 1 \b0 : B\ show ?thesis + with infinite \b0 \ B\ show ?thesis by blast qed @@ -1896,7 +1903,7 @@ then show ?case apply simp apply (subst card_Un_disjoint) - apply (auto simp add: disjoint_eq_subset_Compl) + apply (auto simp add: disjoint_eq_subset_Compl) done qed qed @@ -1914,10 +1921,12 @@ by (simp add: eq_card_imp_inj_on) qed -lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \ surj f \ inj f" for f :: "'a \ 'a" +lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \ surj f \ inj f" + for f :: "'a \ 'a" by (blast intro: finite_surj_inj subset_UNIV) -lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \ inj f \ surj f" for f :: "'a \ 'a" +lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \ inj f \ surj f" + for f :: "'a \ 'a" by (fastforce simp:surj_def dest!: endo_inj_surj) corollary infinite_UNIV_nat [iff]: "\ finite (UNIV :: nat set)" @@ -2013,7 +2022,7 @@ using psubset.hyps by blast show False apply (rule psubset.IH [where B = "A - {x}"]) - using \x \ A\ apply blast + apply (use \x \ A\ in blast) apply (simp add: \X (A - {x})\) done qed