--- a/src/HOL/Finite.ML Thu Dec 06 00:37:59 2001 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,903 +0,0 @@
-(* Title: HOL/Finite.ML
- ID: $Id$
- Author: Lawrence C Paulson & Tobias Nipkow
- Copyright 1995 University of Cambridge & TU Muenchen
-
-Finite sets and their cardinality.
-*)
-
-section "finite";
-
-(*Discharging ~ x:y entails extra work*)
-val major::prems = Goal
- "[| finite F; P({}); \
-\ !!F x. [| finite F; x ~: F; P(F) |] ==> P(insert x F) \
-\ |] ==> P(F)";
-by (rtac (major RS Finites.induct) 1);
-by (excluded_middle_tac "a:A" 2);
-by (etac (insert_absorb RS ssubst) 3 THEN assume_tac 3); (*backtracking!*)
-by (REPEAT (ares_tac prems 1));
-qed "finite_induct";
-
-val major::subs::prems = Goal
- "[| finite F; F <= A; \
-\ P({}); \
-\ !!F a. [| finite F; a:A; a ~: F; P(F) |] ==> P(insert a F) \
-\ |] ==> P(F)";
-by (rtac (subs RS rev_mp) 1);
-by (rtac (major RS finite_induct) 1);
-by (ALLGOALS (blast_tac (claset() addIs prems)));
-qed "finite_subset_induct";
-
-Addsimps Finites.intrs;
-AddSIs Finites.intrs;
-
-(*The union of two finite sets is finite*)
-Goal "[| finite F; finite G |] ==> finite(F Un G)";
-by (etac finite_induct 1);
-by (ALLGOALS Asm_simp_tac);
-qed "finite_UnI";
-
-(*Every subset of a finite set is finite*)
-Goal "finite B ==> ALL A. A<=B --> finite A";
-by (etac finite_induct 1);
-by (ALLGOALS (simp_tac (simpset() addsimps [subset_insert_iff])));
-by Safe_tac;
- by (eres_inst_tac [("t","A")] (insert_Diff RS subst) 1);
- by (ALLGOALS Blast_tac);
-val lemma = result();
-
-Goal "[| A<=B; finite B |] ==> finite A";
-by (dtac lemma 1);
-by (Blast_tac 1);
-qed "finite_subset";
-
-Goal "finite(F Un G) = (finite F & finite G)";
-by (blast_tac (claset()
- addIs [inst "B" "?X Un ?Y" finite_subset, finite_UnI]) 1);
-qed "finite_Un";
-AddIffs[finite_Un];
-
-(*The converse obviously fails*)
-Goal "finite F | finite G ==> finite(F Int G)";
-by (blast_tac (claset() addIs [finite_subset]) 1);
-qed "finite_Int";
-
-Addsimps [finite_Int];
-AddIs [finite_Int];
-
-Goal "finite(insert a A) = finite A";
-by (stac insert_is_Un 1);
-by (simp_tac (HOL_ss addsimps [finite_Un]) 1);
-by (Blast_tac 1);
-qed "finite_insert";
-Addsimps[finite_insert];
-
-(*The image of a finite set is finite *)
-Goal "finite F ==> finite(h`F)";
-by (etac finite_induct 1);
-by (Simp_tac 1);
-by (Asm_simp_tac 1);
-qed "finite_imageI";
-
-Goal "finite (range g) ==> finite (range (%x. f (g x)))";
-by (Simp_tac 1);
-by (etac finite_imageI 1);
-qed "finite_range_imageI";
-
-val major::prems = Goal
- "[| finite c; finite b; \
-\ P(b); \
-\ !!x y. [| finite y; x:y; P(y) |] ==> P(y-{x}) \
-\ |] ==> c<=b --> P(b-c)";
-by (rtac (major RS finite_induct) 1);
-by (stac Diff_insert 2);
-by (ALLGOALS (asm_simp_tac
- (simpset() addsimps prems@[Diff_subset RS finite_subset])));
-val lemma = result();
-
-val prems = Goal
- "[| finite A; \
-\ P(A); \
-\ !!a A. [| finite A; a:A; P(A) |] ==> P(A-{a}) \
-\ |] ==> P({})";
-by (rtac (Diff_cancel RS subst) 1);
-by (rtac (lemma RS mp) 1);
-by (REPEAT (ares_tac (subset_refl::prems) 1));
-qed "finite_empty_induct";
-
-
-(* finite B ==> finite (B - Ba) *)
-bind_thm ("finite_Diff", Diff_subset RS finite_subset);
-Addsimps [finite_Diff];
-
-Goal "finite(A - insert a B) = finite(A-B)";
-by (stac Diff_insert 1);
-by (case_tac "a : A-B" 1);
-by (rtac (finite_insert RS sym RS trans) 1);
-by (stac insert_Diff 1);
-by (ALLGOALS Asm_full_simp_tac);
-qed "finite_Diff_insert";
-AddIffs [finite_Diff_insert];
-
-(*lemma merely for classical reasoner in the proof below: force_tac can't
- prove it.*)
-Goal "finite(A-{}) = finite A";
-by (Simp_tac 1);
-val lemma = result();
-
-(*Lemma for proving finite_imageD*)
-Goal "finite B ==> ALL A. f`A = B --> inj_on f A --> finite A";
-by (etac finite_induct 1);
- by (ALLGOALS Asm_simp_tac);
-by (Clarify_tac 1);
-by (subgoal_tac "EX y:A. f y = x & F = f`(A-{y})" 1);
- by (Clarify_tac 1);
- by (full_simp_tac (simpset() addsimps [inj_on_def]) 1);
- by (blast_tac (claset() addSDs [lemma RS iffD1]) 1);
-by (thin_tac "ALL A. ?PP(A)" 1);
-by (forward_tac [[equalityD2, insertI1] MRS subsetD] 1);
-by (Clarify_tac 1);
-by (res_inst_tac [("x","xa")] bexI 1);
-by (ALLGOALS
- (asm_full_simp_tac (simpset() addsimps [inj_on_image_set_diff])));
-val lemma = result();
-
-Goal "[| finite(f`A); inj_on f A |] ==> finite A";
-by (dtac lemma 1);
-by (Blast_tac 1);
-qed "finite_imageD";
-
-(** The finite UNION of finite sets **)
-
-Goal "finite A ==> (ALL a:A. finite(B a)) --> finite(UN a:A. B a)";
-by (etac finite_induct 1);
-by (ALLGOALS Asm_simp_tac);
-bind_thm("finite_UN_I", ballI RSN (2, result() RS mp));
-
-(*strengthen RHS to
- ((ALL x:A. finite (B x)) & finite {x. x:A & B x ~= {}}) ?
- we'd need to prove
- finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x ~= {}}
- by induction*)
-Goal "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))";
-by (blast_tac (claset() addIs [finite_UN_I, finite_subset]) 1);
-qed "finite_UN";
-Addsimps [finite_UN];
-
-(** Sigma of finite sets **)
-
-Goalw [Sigma_def]
- "[| finite A; ALL a:A. finite(B a) |] ==> finite(SIGMA a:A. B a)";
-by (blast_tac (claset() addSIs [finite_UN_I]) 1);
-bind_thm("finite_SigmaI", ballI RSN (2,result()));
-Addsimps [finite_SigmaI];
-
-Goal "[| finite (UNIV::'a set); finite (UNIV::'b set)|] ==> finite (UNIV::('a * 'b) set)";
-by (subgoal_tac "(UNIV::('a * 'b) set) = Sigma UNIV (%x. UNIV)" 1);
-by (etac ssubst 1);
-by (etac finite_SigmaI 1);
-by Auto_tac;
-qed "finite_Prod_UNIV";
-
-Goal "finite (UNIV :: ('a::finite * 'b::finite) set)";
-by (rtac (finite_Prod_UNIV) 1);
-by (rtac finite 1);
-by (rtac finite 1);
-qed "finite_Prod";
-
-Goal "finite (UNIV :: unit set)";
-by (subgoal_tac "UNIV = {()}" 1);
-by (etac ssubst 1);
-by Auto_tac;
-qed "finite_unit";
-
-(** The powerset of a finite set **)
-
-Goal "finite(Pow A) ==> finite A";
-by (subgoal_tac "finite ((%x.{x})`A)" 1);
-by (rtac finite_subset 2);
-by (assume_tac 3);
-by (ALLGOALS
- (fast_tac (claset() addSDs [rewrite_rule [inj_on_def] finite_imageD])));
-val lemma = result();
-
-Goal "finite(Pow A) = finite A";
-by (rtac iffI 1);
-by (etac lemma 1);
-(*Opposite inclusion: finite A ==> finite (Pow A) *)
-by (etac finite_induct 1);
-by (ALLGOALS
- (asm_simp_tac
- (simpset() addsimps [finite_UnI, finite_imageI, Pow_insert])));
-qed "finite_Pow_iff";
-AddIffs [finite_Pow_iff];
-
-Goal "finite(r^-1) = finite r";
-by (subgoal_tac "r^-1 = (%(x,y).(y,x))`r" 1);
- by (Asm_simp_tac 1);
- by (rtac iffI 1);
- by (etac (rewrite_rule [inj_on_def] finite_imageD) 1);
- by (simp_tac (simpset() addsplits [split_split]) 1);
- by (etac finite_imageI 1);
-by (simp_tac (simpset() addsimps [converse_def,image_def]) 1);
-by Auto_tac;
-by (rtac bexI 1);
-by (assume_tac 2);
-by (Simp_tac 1);
-qed "finite_converse";
-AddIffs [finite_converse];
-
-Goal "finite (lessThan (k::nat))";
-by (induct_tac "k" 1);
-by (simp_tac (simpset() addsimps [lessThan_Suc]) 2);
-by Auto_tac;
-qed "finite_lessThan";
-
-Goal "finite (atMost (k::nat))";
-by (induct_tac "k" 1);
-by (simp_tac (simpset() addsimps [atMost_Suc]) 2);
-by Auto_tac;
-qed "finite_atMost";
-AddIffs [finite_lessThan, finite_atMost];
-
-(* A bounded set of natural numbers is finite *)
-Goal "(ALL i:N. i<(n::nat)) ==> finite N";
-by (rtac finite_subset 1);
- by (rtac finite_lessThan 2);
-by Auto_tac;
-qed "bounded_nat_set_is_finite";
-
-(** Finiteness of transitive closure (thanks to Sidi Ehmety) **)
-
-(*A finite relation has a finite field ( = domain U range) *)
-Goal "finite r ==> finite (Field r)";
-by (etac finite_induct 1);
-by (auto_tac (claset(),
- simpset() addsimps [Field_def, Domain_insert, Range_insert]));
-qed "finite_Field";
-
-Goal "r^+ <= Field r <*> Field r";
-by (Clarify_tac 1);
-by (etac trancl_induct 1);
-by (auto_tac (claset(), simpset() addsimps [Field_def]));
-qed "trancl_subset_Field2";
-
-Goal "finite (r^+) = finite r";
-by Auto_tac;
-by (rtac (trancl_subset_Field2 RS finite_subset) 2);
-by (rtac finite_SigmaI 2);
-by (blast_tac (claset() addIs [r_into_trancl, finite_subset]) 1);
-by (auto_tac (claset(), simpset() addsimps [finite_Field]));
-qed "finite_trancl";
-
-
-section "Finite cardinality -- 'card'";
-
-bind_thm ("cardR_emptyE", cardR.mk_cases "({},n) : cardR");
-bind_thm ("cardR_insertE", cardR.mk_cases "(insert a A,n) : cardR");
-
-AddSEs [cardR_emptyE];
-AddSIs cardR.intrs;
-
-Goal "[| (A,n) : cardR |] ==> a : A --> (EX m. n = Suc m)";
-by (etac cardR.induct 1);
- by (Blast_tac 1);
-by (Blast_tac 1);
-qed "cardR_SucD";
-
-Goal "(A,m): cardR ==> (ALL n a. m = Suc n --> a:A --> (A-{a},n) : cardR)";
-by (etac cardR.induct 1);
- by Auto_tac;
-by (asm_simp_tac (simpset() addsimps [insert_Diff_if]) 1);
-by Auto_tac;
-by (ftac cardR_SucD 1);
-by (Blast_tac 1);
-val lemma = result();
-
-Goal "[| (insert a A, Suc m) : cardR; a ~: A |] ==> (A,m) : cardR";
-by (dtac lemma 1);
-by (Asm_full_simp_tac 1);
-val lemma = result();
-
-Goal "(A,m): cardR ==> (ALL n. (A,n) : cardR --> n=m)";
-by (etac cardR.induct 1);
- by (safe_tac (claset() addSEs [cardR_insertE]));
-by (rename_tac "B b m" 1 THEN case_tac "a = b" 1);
- by (subgoal_tac "A = B" 1);
- by (blast_tac (claset() addEs [equalityE]) 2);
- by (Blast_tac 1);
-by (subgoal_tac "EX C. A = insert b C & B = insert a C" 1);
- by (res_inst_tac [("x","A Int B")] exI 2);
- by (blast_tac (claset() addEs [equalityE]) 2);
-by (forw_inst_tac [("A","B")] cardR_SucD 1);
-by (blast_tac (claset() addDs [lemma]) 1);
-qed_spec_mp "cardR_determ";
-
-Goal "(A,n) : cardR ==> finite(A)";
-by (etac cardR.induct 1);
-by Auto_tac;
-qed "cardR_imp_finite";
-
-Goal "finite(A) ==> EX n. (A, n) : cardR";
-by (etac finite_induct 1);
-by Auto_tac;
-qed "finite_imp_cardR";
-
-Goalw [card_def] "(A,n) : cardR ==> card A = n";
-by (blast_tac (claset() addIs [cardR_determ]) 1);
-qed "card_equality";
-
-Goalw [card_def] "card {} = 0";
-by (Blast_tac 1);
-qed "card_empty";
-Addsimps [card_empty];
-
-Goal "x ~: A \
-\ ==> ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)";
-by Auto_tac;
-by (res_inst_tac [("A1", "A")] (finite_imp_cardR RS exE) 1);
-by (force_tac (claset() addDs [cardR_imp_finite], simpset()) 1);
-by (blast_tac (claset() addIs [cardR_determ]) 1);
-val lemma = result();
-
-Goalw [card_def]
- "[| finite A; x ~: A |] ==> card (insert x A) = Suc(card A)";
-by (asm_simp_tac (simpset() addsimps [lemma]) 1);
-by (rtac the_equality 1);
-by (auto_tac (claset() addIs [finite_imp_cardR],
- simpset() addcongs [conj_cong]
- addsimps [symmetric card_def,
- card_equality]));
-qed "card_insert_disjoint";
-Addsimps [card_insert_disjoint];
-
-(* Delete rules to do with cardR relation: obsolete *)
-Delrules [cardR_emptyE];
-Delrules cardR.intrs;
-
-Goal "finite A ==> (card A = 0) = (A = {})";
-by Auto_tac;
-by (dres_inst_tac [("a","x")] mk_disjoint_insert 1);
-by (Clarify_tac 1);
-by (rotate_tac ~1 1);
-by Auto_tac;
-qed "card_0_eq";
-Addsimps[card_0_eq];
-
-Goal "finite A ==> card(insert x A) = (if x:A then card A else Suc(card(A)))";
-by (asm_simp_tac (simpset() addsimps [insert_absorb]) 1);
-qed "card_insert_if";
-
-Goal "[| finite A; x: A |] ==> Suc (card (A-{x})) = card A";
-by (res_inst_tac [("t", "A")] (insert_Diff RS subst) 1);
-by (assume_tac 1);
-by (Asm_simp_tac 1);
-qed "card_Suc_Diff1";
-
-Goal "[| finite A; x: A |] ==> card (A-{x}) = card A - 1";
-by (asm_simp_tac (simpset() addsimps [card_Suc_Diff1 RS sym]) 1);
-qed "card_Diff_singleton";
-
-Goal "finite A ==> card (A-{x}) = (if x:A then card A - 1 else card A)";
-by (asm_simp_tac (simpset() addsimps [card_Diff_singleton]) 1);
-qed "card_Diff_singleton_if";
-
-Goal "finite A ==> card(insert x A) = Suc(card(A-{x}))";
-by (asm_simp_tac (simpset() addsimps [card_insert_if,card_Suc_Diff1]) 1);
-qed "card_insert";
-
-Goal "finite A ==> card A <= card (insert x A)";
-by (asm_simp_tac (simpset() addsimps [card_insert_if]) 1);
-qed "card_insert_le";
-
-Goal "finite B ==> ALL A. A <= B --> card B <= card A --> A = B";
-by (etac finite_induct 1);
- by (Simp_tac 1);
-by (Clarify_tac 1);
-by (subgoal_tac "finite A & A-{x} <= F" 1);
- by (blast_tac (claset() addIs [finite_subset]) 2);
-by (dres_inst_tac [("x","A-{x}")] spec 1);
-by (asm_full_simp_tac (simpset() addsimps [card_Diff_singleton_if]
- addsplits [split_if_asm]) 1);
-by (case_tac "card A" 1);
-by Auto_tac;
-qed_spec_mp "card_seteq";
-
-Goalw [psubset_def] "[| finite B; A < B |] ==> card A < card B";
-by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
-by (blast_tac (claset() addDs [card_seteq]) 1);
-qed "psubset_card_mono" ;
-
-Goal "[| finite B; A <= B |] ==> card A <= card B";
-by (case_tac "A=B" 1);
- by (Asm_simp_tac 1);
-by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
-by (blast_tac (claset() addDs [card_seteq] addIs [order_less_imp_le]) 1);
-qed "card_mono" ;
-
-Goal "[| finite A; finite B |] \
-\ ==> card A + card B = card (A Un B) + card (A Int B)";
-by (etac finite_induct 1);
-by (Simp_tac 1);
-by (asm_simp_tac (simpset() addsimps [insert_absorb, Int_insert_left]) 1);
-qed "card_Un_Int";
-
-Goal "[| finite A; finite B; A Int B = {} |] \
-\ ==> card (A Un B) = card A + card B";
-by (asm_simp_tac (simpset() addsimps [card_Un_Int]) 1);
-qed "card_Un_disjoint";
-
-Goal "[| finite A; B<=A |] ==> card A - card B = card (A - B)";
-by (subgoal_tac "(A-B) Un B = A" 1);
-by (Blast_tac 2);
-by (rtac (add_right_cancel RS iffD1) 1);
-by (rtac (card_Un_disjoint RS subst) 1);
-by (etac ssubst 4);
-by (Blast_tac 3);
-by (ALLGOALS
- (asm_simp_tac
- (simpset() addsimps [add_commute, not_less_iff_le,
- add_diff_inverse, card_mono, finite_subset])));
-qed "card_Diff_subset";
-
-Goal "[| finite A; x: A |] ==> card(A-{x}) < card A";
-by (rtac Suc_less_SucD 1);
-by (asm_simp_tac (simpset() addsimps [card_Suc_Diff1]) 1);
-qed "card_Diff1_less";
-
-Goal "[| finite A; x: A; y: A |] ==> card(A-{x}-{y}) < card A";
-by (case_tac "x=y" 1);
-by (asm_simp_tac (simpset() addsimps [card_Diff1_less]) 1);
-by (rtac less_trans 1);
-by (ALLGOALS (force_tac (claset() addSIs [card_Diff1_less], simpset())));
-qed "card_Diff2_less";
-
-Goal "finite A ==> card(A-{x}) <= card A";
-by (case_tac "x: A" 1);
-by (ALLGOALS (asm_simp_tac (simpset() addsimps [card_Diff1_less, less_imp_le])));
-qed "card_Diff1_le";
-
-Goal "[| finite B; A <= B; card A < card B |] ==> A < B";
-by (etac psubsetI 1);
-by (Blast_tac 1);
-qed "card_psubset";
-
-(*** Cardinality of image ***)
-
-Goal "finite A ==> card (f ` A) <= card A";
-by (etac finite_induct 1);
- by (Simp_tac 1);
-by (asm_simp_tac (simpset() addsimps [le_SucI, finite_imageI,
- card_insert_if]) 1);
-qed "card_image_le";
-
-Goal "finite(A) ==> inj_on f A --> card (f ` A) = card A";
-by (etac finite_induct 1);
-by (ALLGOALS Asm_simp_tac);
-by Safe_tac;
-by (rewtac inj_on_def);
-by (Blast_tac 1);
-by (stac card_insert_disjoint 1);
-by (etac finite_imageI 1);
-by (Blast_tac 1);
-by (Blast_tac 1);
-qed_spec_mp "card_image";
-
-Goal "[| finite A; f`A <= A; inj_on f A |] ==> f`A = A";
-by (asm_simp_tac (simpset() addsimps [card_seteq, card_image]) 1);
-qed "endo_inj_surj";
-
-(*** Cardinality of the Powerset ***)
-
-Goal "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"; (* FIXME numeral 2 (!?) *)
-by (etac finite_induct 1);
-by (ALLGOALS (asm_simp_tac (simpset() addsimps [Pow_insert])));
-by (stac card_Un_disjoint 1);
-by (EVERY (map (blast_tac (claset() addIs [finite_imageI])) [3,2,1]));
-by (subgoal_tac "inj_on (insert x) (Pow F)" 1);
-by (asm_simp_tac (simpset() addsimps [card_image, Pow_insert]) 1);
-by (rewtac inj_on_def);
-by (blast_tac (claset() addSEs [equalityE]) 1);
-qed "card_Pow";
-
-
-(*Relates to equivalence classes. Based on a theorem of F. Kammueller's.
- The "finite C" premise is redundant*)
-Goal "finite C ==> finite (Union C) --> \
-\ (ALL c : C. k dvd card c) --> \
-\ (ALL c1: C. ALL c2: C. c1 ~= c2 --> c1 Int c2 = {}) \
-\ --> k dvd card(Union C)";
-by (etac finite_induct 1);
-by (ALLGOALS Asm_simp_tac);
-by (Clarify_tac 1);
-by (stac card_Un_disjoint 1);
-by (ALLGOALS
- (asm_full_simp_tac (simpset()
- addsimps [dvd_add, disjoint_eq_subset_Compl])));
-by (thin_tac "ALL c:F. ?PP(c)" 1);
-by (thin_tac "ALL c:F. ?PP(c) & ?QQ(c)" 1);
-by (Clarify_tac 1);
-by (ball_tac 1);
-by (Blast_tac 1);
-qed_spec_mp "dvd_partition";
-
-
-(*** foldSet ***)
-
-bind_thm ("empty_foldSetE", foldSet.mk_cases "({}, x) : foldSet f e");
-
-AddSEs [empty_foldSetE];
-AddIs foldSet.intrs;
-
-Goal "[| (A-{x},y) : foldSet f e; x: A |] ==> (A, f x y) : foldSet f e";
-by (etac (insert_Diff RS subst) 1 THEN resolve_tac foldSet.intrs 1);
-by Auto_tac;
-qed "Diff1_foldSet";
-
-Goal "(A, x) : foldSet f e ==> finite(A)";
-by (eresolve_tac [foldSet.induct] 1);
-by Auto_tac;
-qed "foldSet_imp_finite";
-
-Addsimps [foldSet_imp_finite];
-
-
-Goal "finite(A) ==> EX x. (A, x) : foldSet f e";
-by (etac finite_induct 1);
-by Auto_tac;
-qed "finite_imp_foldSet";
-
-
-Open_locale "LC";
-
-val f_lcomm = thm "lcomm";
-
-
-Goal "ALL A x. card(A) < n --> (A, x) : foldSet f e --> \
-\ (ALL y. (A, y) : foldSet f e --> y=x)";
-by (induct_tac "n" 1);
-by (auto_tac (claset(), simpset() addsimps [less_Suc_eq]));
-by (etac foldSet.elim 1);
-by (Blast_tac 1);
-by (etac foldSet.elim 1);
-by (Blast_tac 1);
-by (Clarify_tac 1);
-(*force simplification of "card A < card (insert ...)"*)
-by (etac rev_mp 1);
-by (asm_simp_tac (simpset() addsimps [less_Suc_eq_le]) 1);
-by (rtac impI 1);
-(** LEVEL 10 **)
-by (rename_tac "Aa xa ya Ab xb yb" 1 THEN case_tac "xa=xb" 1);
- by (subgoal_tac "Aa = Ab" 1);
- by (blast_tac (claset() addSEs [equalityE]) 2);
- by (Blast_tac 1);
-(*case xa ~= xb*)
-by (subgoal_tac "Aa-{xb} = Ab-{xa} & xb : Aa & xa : Ab" 1);
- by (blast_tac (claset() addSEs [equalityE]) 2);
-by (Clarify_tac 1);
-by (subgoal_tac "Aa = insert xb Ab - {xa}" 1);
- by (Blast_tac 2);
-(** LEVEL 20 **)
-by (subgoal_tac "card Aa <= card Ab" 1);
- by (rtac (Suc_le_mono RS subst) 2);
- by (asm_simp_tac (simpset() addsimps [card_Suc_Diff1]) 2);
-by (res_inst_tac [("A1", "Aa-{xb}"), ("f1","f")]
- (finite_imp_foldSet RS exE) 1);
-by (blast_tac (claset() addIs [foldSet_imp_finite, finite_Diff]) 1);
-by (ftac Diff1_foldSet 1 THEN assume_tac 1);
-by (subgoal_tac "ya = f xb x" 1);
- by (blast_tac (claset() delrules [equalityCE]) 2);
-by (subgoal_tac "(Ab - {xa}, x) : foldSet f e" 1);
- by (Asm_full_simp_tac 2);
-by (subgoal_tac "yb = f xa x" 1);
- by (blast_tac (claset() delrules [equalityCE]
- addDs [Diff1_foldSet]) 2);
-by (asm_simp_tac (simpset() addsimps [f_lcomm]) 1);
-val lemma = result();
-
-
-Goal "[| (A, x) : foldSet f e; (A, y) : foldSet f e |] ==> y=x";
-by (blast_tac (claset() addIs [ObjectLogic.rulify lemma]) 1);
-qed "foldSet_determ";
-
-Goalw [fold_def] "(A,y) : foldSet f e ==> fold f e A = y";
-by (blast_tac (claset() addIs [foldSet_determ]) 1);
-qed "fold_equality";
-
-Goalw [fold_def] "fold f e {} = e";
-by (Blast_tac 1);
-qed "fold_empty";
-Addsimps [fold_empty];
-
-
-Goal "x ~: A ==> \
-\ ((insert x A, v) : foldSet f e) = \
-\ (EX y. (A, y) : foldSet f e & v = f x y)";
-by Auto_tac;
-by (res_inst_tac [("A1", "A"), ("f1","f")] (finite_imp_foldSet RS exE) 1);
-by (force_tac (claset() addDs [foldSet_imp_finite], simpset()) 1);
-by (blast_tac (claset() addIs [foldSet_determ]) 1);
-val lemma = result();
-
-Goalw [fold_def]
- "[| finite A; x ~: A |] ==> fold f e (insert x A) = f x (fold f e A)";
-by (asm_simp_tac (simpset() addsimps [lemma]) 1);
-by (rtac the_equality 1);
-by (auto_tac (claset() addIs [finite_imp_foldSet],
- simpset() addcongs [conj_cong]
- addsimps [symmetric fold_def,
- fold_equality]));
-qed "fold_insert";
-
-Goal "finite A ==> ALL e. f x (fold f e A) = fold f (f x e) A";
-by (etac finite_induct 1);
-by (Simp_tac 1);
-by (asm_simp_tac (simpset() addsimps [f_lcomm, fold_insert]) 1);
-qed_spec_mp "fold_commute";
-
-Goal "[| finite A; finite B |] \
-\ ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)";
-by (etac finite_induct 1);
-by (Simp_tac 1);
-by (asm_simp_tac (simpset() addsimps [fold_insert, fold_commute,
- Int_insert_left, insert_absorb]) 1);
-qed "fold_nest_Un_Int";
-
-Goal "[| finite A; finite B; A Int B = {} |] \
-\ ==> fold f e (A Un B) = fold f (fold f e B) A";
-by (asm_simp_tac (simpset() addsimps [fold_nest_Un_Int]) 1);
-qed "fold_nest_Un_disjoint";
-
-(* Delete rules to do with foldSet relation: obsolete *)
-Delsimps [foldSet_imp_finite];
-Delrules [empty_foldSetE];
-Delrules foldSet.intrs;
-
-Close_locale "LC";
-
-Open_locale "ACe";
-
-(*We enter a more restrictive framework, with f :: ['a,'a] => 'a
- instead of ['b,'a] => 'a
- At present, none of these results are used!*)
-
-val f_ident = thm "ident";
-val f_commute = thm "commute";
-val f_assoc = thm "assoc";
-
-
-Goal "f x (f y z) = f y (f x z)";
-by (rtac (f_commute RS trans) 1);
-by (rtac (f_assoc RS trans) 1);
-by (rtac (f_commute RS arg_cong) 1);
-qed "f_left_commute";
-
-val f_ac = [f_assoc, f_commute, f_left_commute];
-
-Goal "f e x = x";
-by (stac f_commute 1);
-by (rtac f_ident 1);
-qed "f_left_ident";
-
-val f_idents = [f_left_ident, f_ident];
-
-Goal "[| finite A; finite B |] \
-\ ==> f (fold f e A) (fold f e B) = \
-\ f (fold f e (A Un B)) (fold f e (A Int B))";
-by (etac finite_induct 1);
-by (simp_tac (simpset() addsimps f_idents) 1);
-by (asm_simp_tac (simpset() addsimps f_ac @ f_idents @
- [export fold_insert,insert_absorb, Int_insert_left]) 1);
-qed "fold_Un_Int";
-
-Goal "[| finite A; finite B; A Int B = {} |] \
-\ ==> fold f e (A Un B) = f (fold f e A) (fold f e B)";
-by (asm_simp_tac (simpset() addsimps fold_Un_Int::f_idents) 1);
-qed "fold_Un_disjoint";
-
-Goal
- "[| finite A; finite B |] ==> A Int B = {} --> \
-\ fold (f o g) e (A Un B) = f (fold (f o g) e A) (fold (f o g) e B)";
-by (etac finite_induct 1);
-by (simp_tac (simpset() addsimps f_idents) 1);
-by (asm_full_simp_tac (simpset() addsimps f_ac @ f_idents @
- [export fold_insert,insert_absorb, Int_insert_left]) 1);
-qed "fold_Un_disjoint2";
-
-Close_locale "ACe";
-
-
-(*** setsum: generalized summation over a set ***)
-
-Goalw [setsum_def] "setsum f {} = 0";
-by (Simp_tac 1);
-qed "setsum_empty";
-Addsimps [setsum_empty];
-
-Goalw [setsum_def]
- "!!f. [| finite F; a ~: F |] ==> \
-\ setsum f (insert a F) = f a + setsum f F";
-by (asm_simp_tac (simpset() addsimps [export fold_insert,
- thm "plus_ac0_left_commute"]) 1);
-qed "setsum_insert";
-Addsimps [setsum_insert];
-
-Goal "setsum (%i. 0) A = 0";
-by (case_tac "finite A" 1);
- by (asm_simp_tac (simpset() addsimps [setsum_def]) 2);
-by (etac finite_induct 1);
-by Auto_tac;
-qed "setsum_0";
-
-Goal "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))";
-by (etac finite_induct 1);
-by Auto_tac;
-qed "setsum_eq_0_iff";
-Addsimps [setsum_eq_0_iff];
-
-Goal "setsum f A = Suc n ==> EX a:A. 0 < f a";
-by (case_tac "finite A" 1);
- by (asm_full_simp_tac (simpset() addsimps [setsum_def]) 2);
-by (etac rev_mp 1);
-by (etac finite_induct 1);
-by Auto_tac;
-qed "setsum_SucD";
-
-(*Could allow many "card" proofs to be simplified*)
-Goal "finite A ==> card A = setsum (%x. 1) A";
-by (etac finite_induct 1);
-by Auto_tac;
-qed "card_eq_setsum";
-
-(*The reversed orientation looks more natural, but LOOPS as a simprule!*)
-Goal "!!g. [| finite A; finite B |] \
-\ ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B";
-by (etac finite_induct 1);
-by (Simp_tac 1);
-by (asm_full_simp_tac (simpset() addsimps (thms "plus_ac0") @
- [Int_insert_left, insert_absorb]) 1);
-qed "setsum_Un_Int";
-
-Goal "[| finite A; finite B; A Int B = {} |] \
-\ ==> setsum g (A Un B) = setsum g A + setsum g B";
-by (stac (setsum_Un_Int RS sym) 1);
-by Auto_tac;
-qed "setsum_Un_disjoint";
-
-Goal "!!f::'a=>'b::plus_ac0. finite I \
-\ ==> (ALL i:I. finite (A i)) --> \
-\ (ALL i:I. ALL j:I. i~=j --> A i Int A j = {}) --> \
-\ setsum f (UNION I A) = setsum (%i. setsum f (A i)) I";
-by (etac finite_induct 1);
- by (Simp_tac 1);
-by (Clarify_tac 1);
-by (subgoal_tac "ALL i:F. x ~= i" 1);
- by (Blast_tac 2);
-by (subgoal_tac "A x Int UNION F A = {}" 1);
- by (Blast_tac 2);
-by (asm_simp_tac (simpset() addsimps [setsum_Un_disjoint]) 1);
-qed_spec_mp "setsum_UN_disjoint";
-
-Goal "setsum (%x. f x + g x) A = (setsum f A + setsum g A)";
-by (case_tac "finite A" 1);
- by (asm_full_simp_tac (simpset() addsimps [setsum_def]) 2);
-by (etac finite_induct 1);
-by Auto_tac;
-by (simp_tac (simpset() addsimps (thms "plus_ac0")) 1);
-qed "setsum_addf";
-
-(** For the natural numbers, we have subtraction **)
-
-Goal "[| finite A; finite B |] \
-\ ==> (setsum f (A Un B) :: nat) = \
-\ setsum f A + setsum f B - setsum f (A Int B)";
-by (stac (setsum_Un_Int RS sym) 1);
-by Auto_tac;
-qed "setsum_Un";
-
-Goal "(setsum f (A-{a}) :: nat) = \
-\ (if a:A then setsum f A - f a else setsum f A)";
-by (case_tac "finite A" 1);
- by (asm_full_simp_tac (simpset() addsimps [setsum_def]) 2);
-by (etac finite_induct 1);
-by (auto_tac (claset(), simpset() addsimps [insert_Diff_if]));
-by (dres_inst_tac [("a","a")] mk_disjoint_insert 1);
-by Auto_tac;
-qed_spec_mp "setsum_diff1";
-
-val prems = Goal
- "[| A = B; !!x. x:B ==> f x = g x|] \
-\ ==> setsum f A = setsum g B";
-by (case_tac "finite B" 1);
- by (asm_simp_tac (simpset() addsimps [setsum_def]@prems) 2);
-by (simp_tac (simpset() addsimps prems) 1);
-by (subgoal_tac
- "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C" 1);
- by (asm_simp_tac (simpset() addsimps prems) 1);
-by (etac finite_induct 1);
- by (Simp_tac 1);
-by (asm_simp_tac (simpset() addsimps subset_insert_iff::prems) 1);
-by (Clarify_tac 1);
-by (subgoal_tac "finite C" 1);
- by (blast_tac (claset() addDs [rotate_prems 1 finite_subset]) 2);
-by (subgoal_tac "C = insert x (C-{x})" 1);
- by (Blast_tac 2);
-by (etac ssubst 1);
-by (dtac spec 1);
-by (mp_tac 1);
-by (asm_full_simp_tac (simpset() addsimps Ball_def::prems) 1);
-qed "setsum_cong";
-
-
-(*** Basic theorem about "choose". By Florian Kammueller, tidied by LCP ***)
-
-Goal "finite A ==> card {B. B <= A & card B = 0} = 1";
-by (asm_simp_tac (simpset() addcongs [conj_cong]
- addsimps [finite_subset RS card_0_eq]) 1);
-by (simp_tac (simpset() addcongs [rev_conj_cong]) 1);
-qed "card_s_0_eq_empty";
-
-Goal "[| finite M; x ~: M |] \
-\ ==> {s. s <= insert x M & card(s) = Suc k} \
-\ = {s. s <= M & card(s) = Suc k} Un \
-\ {s. EX t. t <= M & card(t) = k & s = insert x t}";
-by Safe_tac;
-by (auto_tac (claset() addIs [finite_subset RS card_insert_disjoint],
- simpset()));
-by (dres_inst_tac [("x","xa - {x}")] spec 1);
-by (subgoal_tac ("x ~: xa") 1);
-by Auto_tac;
-by (etac rev_mp 1 THEN stac card_Diff_singleton 1);
-by (auto_tac (claset() addIs [finite_subset], simpset()));
-qed "choose_deconstruct";
-
-Goal "[| finite(A); finite(B); f`A <= B; inj_on f A |] \
-\ ==> card A <= card B";
-by (auto_tac (claset() addIs [card_mono],
- simpset() addsimps [card_image RS sym]));
-qed "card_inj_on_le";
-
-Goal "[| finite A; finite B; \
-\ f`A <= B; inj_on f A; g`B <= A; inj_on g B |] \
-\ ==> card(A) = card(B)";
-by (auto_tac (claset() addIs [le_anti_sym,card_inj_on_le], simpset()));
-qed "card_bij_eq";
-
-Goal "[| finite A; x ~: A |] \
-\ ==> card{B. EX C. C <= A & card(C) = k & B = insert x C} = \
-\ card {B. B <= A & card(B) = k}";
-by (res_inst_tac [("f", "%s. s - {x}"), ("g","insert x")] card_bij_eq 1);
-by (res_inst_tac [("B","Pow(insert x A)")] finite_subset 1);
-by (res_inst_tac [("B","Pow(A)")] finite_subset 3);
-by (REPEAT(Fast_tac 1));
-(* arity *)
-by (auto_tac (claset() addSEs [equalityE], simpset() addsimps [inj_on_def]));
-by (stac Diff_insert0 1);
-by Auto_tac;
-qed "constr_bij";
-
-(* Main theorem: combinatorial theorem about number of subsets of a set *)
-Goal "(ALL A. finite A --> card {B. B <= A & card B = k} = (card A choose k))";
-by (induct_tac "k" 1);
- by (simp_tac (simpset() addsimps [card_s_0_eq_empty]) 1);
-(* first 0 case finished *)
-(* prepare finite set induction *)
-by (rtac allI 1 THEN rtac impI 1);
-(* second induction *)
-by (etac finite_induct 1);
-by (ALLGOALS
- (asm_simp_tac (simpset() addcongs [conj_cong]
- addsimps [card_s_0_eq_empty, choose_deconstruct])));
-by (stac card_Un_disjoint 1);
- by (force_tac (claset(), simpset() addsimps [constr_bij]) 4);
- by (Force_tac 3);
- by (blast_tac (claset() addIs [finite_Pow_iff RS iffD2,
- inst "B" "Pow (insert ?x ?F)" finite_subset]) 2);
-by (blast_tac (claset() addIs [finite_Pow_iff RS iffD2
- RSN (2, finite_subset)]) 1);
-qed "n_sub_lemma";
-
-Goal "finite A ==> card {B. B <= A & card(B) = k} = ((card A) choose k)";
-by (asm_simp_tac (simpset() addsimps [n_sub_lemma]) 1);
-qed "n_subsets";
--- a/src/HOL/Finite.thy Thu Dec 06 00:37:59 2001 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-(* Title: HOL/Finite.thy
- ID: $Id$
- Author: Lawrence C Paulson & Tobias Nipkow
- Copyright 1995 University of Cambridge & TU Muenchen
-
-Finite sets, their cardinality, and a fold functional.
-*)
-
-Finite = Divides + Power + Inductive + SetInterval +
-
-consts Finites :: 'a set set
-
-inductive "Finites"
- intrs
- emptyI "{} : Finites"
- insertI "A : Finites ==> insert a A : Finites"
-
-syntax finite :: 'a set => bool
-translations "finite A" == "A : Finites"
-
-axclass finite < type
- finite "finite UNIV"
-
-(* This definition, although traditional, is ugly to work with
-constdefs
- card :: 'a set => nat
- "card A == LEAST n. ? f. A = {f i |i. i<n}"
-Therefore we have switched to an inductive one:
-*)
-
-consts cardR :: "('a set * nat) set"
-
-inductive cardR
-intrs
- EmptyI "({},0) : cardR"
- InsertI "[| (A,n) : cardR; a ~: A |] ==> (insert a A, Suc n) : cardR"
-
-constdefs
- card :: 'a set => nat
- "card A == THE n. (A,n) : cardR"
-
-(*
-A "fold" functional for finite sets. For n non-negative we have
- fold f e {x1,...,xn} = f x1 (... (f xn e))
-where f is at least left-commutative.
-*)
-
-consts foldSet :: "[['b,'a] => 'a, 'a] => ('b set * 'a) set"
-
-inductive "foldSet f e"
- intrs
- emptyI "({}, e) : foldSet f e"
-
- insertI "[| x ~: A; (A,y) : foldSet f e |]
- ==> (insert x A, f x y) : foldSet f e"
-
-constdefs
- fold :: "[['b,'a] => 'a, 'a, 'b set] => 'a"
- "fold f e A == THE x. (A,x) : foldSet f e"
-
- setsum :: "('a => 'b) => 'a set => 'b::plus_ac0"
- "setsum f A == if finite A then fold (op+ o f) 0 A else 0"
-
-syntax
- "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0" ("\\<Sum>_:_. _" [0, 51, 10] 10)
-syntax (xsymbols)
- "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0" ("\\<Sum>_\\<in>_. _" [0, 51, 10] 10)
-translations
- "\\<Sum>i:A. b" == "setsum (%i. b) A" (* Beware of argument permutation! *)
-
-
-locale LC =
- fixes
- f :: ['b,'a] => 'a
- assumes
- lcomm "f x (f y z) = f y (f x z)"
-
-locale ACe =
- fixes
- f :: ['a,'a] => 'a
- e :: 'a
- assumes
- ident "f x e = x"
- commute "f x y = f y x"
- assoc "f (f x y) z = f x (f y z)"
-
-end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Finite_Set.ML Thu Dec 06 00:38:55 2001 +0100
@@ -0,0 +1,128 @@
+
+(* legacy ML bindings *)
+
+structure Finites =
+struct
+ val intrs = thms "Finites.intros";
+ val elims = thms "Finites.cases";
+ val elim = thm "Finites.cases";
+ val induct = thm "Finites.induct";
+ val mk_cases = InductivePackage.the_mk_cases (the_context ()) "Finite_Set.Finites";
+ val [emptyI, insertI] = thms "Finites.intros";
+end;
+
+structure cardR =
+struct
+ val intrs = thms "cardR.intros";
+ val elims = thms "cardR.cases";
+ val elim = thm "cardR.cases";
+ val induct = thm "cardR.induct";
+ val mk_cases = InductivePackage.the_mk_cases (the_context ()) "Finite_Set.cardR";
+ val [EmptyI, InsertI] = thms "cardR.intros";
+end;
+
+structure foldSet =
+struct
+ val intrs = thms "foldSet.intros";
+ val elims = thms "foldSet.cases";
+ val elim = thm "foldSet.cases";
+ val induct = thm "foldSet.induct";
+ val mk_cases = InductivePackage.the_mk_cases (the_context ()) "Finite_Set.foldSet";
+ val [emptyI, insertI] = thms "foldSet.intros";
+end;
+
+val Diff1_foldSet = thm "Diff1_foldSet";
+val bounded_nat_set_is_finite = thm "bounded_nat_set_is_finite";
+val cardR_SucD = thm "cardR_SucD";
+val cardR_determ = thm "cardR_determ";
+val cardR_emptyE = thm "cardR_emptyE";
+val cardR_imp_finite = thm "cardR_imp_finite";
+val cardR_insertE = thm "cardR_insertE";
+val card_0_eq = thm "card_0_eq";
+val card_Diff1_le = thm "card_Diff1_le";
+val card_Diff1_less = thm "card_Diff1_less";
+val card_Diff2_less = thm "card_Diff2_less";
+val card_Diff_singleton = thm "card_Diff_singleton";
+val card_Diff_singleton_if = thm "card_Diff_singleton_if";
+val card_Diff_subset = thm "card_Diff_subset";
+val card_Pow = thm "card_Pow";
+val card_Suc_Diff1 = thm "card_Suc_Diff1";
+val card_Un_Int = thm "card_Un_Int";
+val card_Un_disjoint = thm "card_Un_disjoint";
+val card_bij_eq = thm "card_bij_eq";
+val card_def = thm "card_def";
+val card_empty = thm "card_empty";
+val card_eq_setsum = thm "card_eq_setsum";
+val card_equality = thm "card_equality";
+val card_image = thm "card_image";
+val card_image_le = thm "card_image_le";
+val card_inj_on_le = thm "card_inj_on_le";
+val card_insert = thm "card_insert";
+val card_insert_disjoint = thm "card_insert_disjoint";
+val card_insert_if = thm "card_insert_if";
+val card_insert_le = thm "card_insert_le";
+val card_mono = thm "card_mono";
+val card_psubset = thm "card_psubset";
+val card_s_0_eq_empty = thm "card_s_0_eq_empty";
+val card_seteq = thm "card_seteq";
+val choose_deconstruct = thm "choose_deconstruct";
+val constr_bij = thm "constr_bij";
+val dvd_partition = thm "dvd_partition";
+val empty_foldSetE = thm "empty_foldSetE";
+val endo_inj_surj = thm "endo_inj_surj";
+val finite = thm "finite";
+val finiteI = thm "finiteI";
+val finite_Diff = thm "finite_Diff";
+val finite_Diff_insert = thm "finite_Diff_insert";
+val finite_Field = thm "finite_Field";
+val finite_Int = thm "finite_Int";
+val finite_Pow_iff = thm "finite_Pow_iff";
+val finite_Prod_UNIV = thm "finite_Prod_UNIV";
+val finite_SigmaI = thm "finite_SigmaI";
+val finite_UN = thm "finite_UN";
+val finite_UN_I = thm "finite_UN_I";
+val finite_Un = thm "finite_Un";
+val finite_UnI = thm "finite_UnI";
+val finite_atMost = thm "finite_atMost";
+val finite_converse = thm "finite_converse";
+val finite_empty_induct = thm "finite_empty_induct";
+val finite_imageD = thm "finite_imageD";
+val finite_imageI = thm "finite_imageI";
+val finite_imp_cardR = thm "finite_imp_cardR";
+val finite_imp_foldSet = thm "finite_imp_foldSet";
+val finite_induct = thm "finite_induct";
+val finite_insert = thm "finite_insert";
+val finite_lessThan = thm "finite_lessThan";
+val finite_range_imageI = thm "finite_range_imageI";
+val finite_subset = thm "finite_subset";
+val finite_subset_induct = thm "finite_subset_induct";
+val finite_trancl = thm "finite_trancl";
+val foldSet_determ = thm "foldSet_determ";
+val foldSet_imp_finite = thm "foldSet_imp_finite";
+val fold_Un_Int = thm "fold_Un_Int";
+val fold_Un_disjoint = thm "fold_Un_disjoint";
+val fold_Un_disjoint2 = thm "fold_Un_disjoint2";
+val fold_commute = thm "fold_commute";
+val fold_def = thm "fold_def";
+val fold_empty = thm "fold_empty";
+val fold_equality = thm "fold_equality";
+val fold_insert = thm "fold_insert";
+val fold_nest_Un_Int = thm "fold_nest_Un_Int";
+val fold_nest_Un_disjoint = thm "fold_nest_Un_disjoint";
+val n_sub_lemma = thm "n_sub_lemma";
+val n_subsets = thm "n_subsets";
+val psubset_card_mono = thm "psubset_card_mono";
+val setsum_0 = thm "setsum_0";
+val setsum_SucD = thm "setsum_SucD";
+val setsum_UN_disjoint = thm "setsum_UN_disjoint";
+val setsum_Un = thm "setsum_Un";
+val setsum_Un_Int = thm "setsum_Un_Int";
+val setsum_Un_disjoint = thm "setsum_Un_disjoint";
+val setsum_addf = thm "setsum_addf";
+val setsum_cong = thm "setsum_cong";
+val setsum_def = thm "setsum_def";
+val setsum_diff1 = thm "setsum_diff1";
+val setsum_empty = thm "setsum_empty";
+val setsum_eq_0_iff = thm "setsum_eq_0_iff";
+val setsum_insert = thm "setsum_insert";
+val trancl_subset_Field2 = thm "trancl_subset_Field2";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Finite_Set.thy Thu Dec 06 00:38:55 2001 +0100
@@ -0,0 +1,947 @@
+(* Title: HOL/Finite_Set.thy
+ ID: $Id$
+ Author: Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
+ License: GPL (GNU GENERAL PUBLIC LICENSE)
+*)
+
+header {* Finite sets *}
+
+theory Finite_Set = Divides + Power + Inductive + SetInterval:
+
+subsection {* Collection of finite sets *}
+
+consts Finites :: "'a set set"
+
+inductive Finites
+ intros
+ emptyI [simp, intro!]: "{} : Finites"
+ insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
+
+syntax
+ finite :: "'a set => bool"
+translations
+ "finite A" == "A : Finites"
+
+axclass finite \<subseteq> type
+ finite: "finite UNIV"
+
+
+lemma finite_induct [case_names empty insert, induct set: Finites]:
+ "finite F ==>
+ P {} ==> (!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
+ -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
+proof -
+ assume "P {}" and insert: "!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
+ assume "finite F"
+ thus "P F"
+ proof induct
+ show "P {}" .
+ fix F x assume F: "finite F" and P: "P F"
+ show "P (insert x F)"
+ proof cases
+ assume "x \<in> F"
+ hence "insert x F = F" by (rule insert_absorb)
+ with P show ?thesis by (simp only:)
+ next
+ assume "x \<notin> F"
+ from F this P show ?thesis by (rule insert)
+ qed
+ qed
+qed
+
+lemma finite_subset_induct [consumes 2, case_names empty insert]:
+ "finite F ==> F \<subseteq> A ==>
+ P {} ==> (!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
+ P F"
+proof -
+ assume "P {}" and insert: "!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
+ assume "finite F"
+ thus "F \<subseteq> A ==> P F"
+ proof induct
+ show "P {}" .
+ fix F x assume "finite F" and "x \<notin> F"
+ and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
+ show "P (insert x F)"
+ proof (rule insert)
+ from i show "x \<in> A" by blast
+ from i have "F \<subseteq> A" by blast
+ with P show "P F" .
+ qed
+ qed
+qed
+
+lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
+ -- {* The union of two finite sets is finite. *}
+ by (induct set: Finites) simp_all
+
+lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
+ -- {* Every subset of a finite set is finite. *}
+proof -
+ assume "finite B"
+ thus "!!A. A \<subseteq> B ==> finite A"
+ proof induct
+ case empty
+ thus ?case by simp
+ next
+ case (insert F x A)
+ have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
+ show "finite A"
+ proof cases
+ assume x: "x \<in> A"
+ with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
+ with r have "finite (A - {x})" .
+ hence "finite (insert x (A - {x}))" ..
+ also have "insert x (A - {x}) = A" by (rule insert_Diff)
+ finally show ?thesis .
+ next
+ show "A \<subseteq> F ==> ?thesis" .
+ assume "x \<notin> A"
+ with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
+ qed
+ qed
+qed
+
+lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
+ by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
+
+lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
+ -- {* The converse obviously fails. *}
+ by (blast intro: finite_subset)
+
+lemma finite_insert [simp]: "finite (insert a A) = finite A"
+ apply (subst insert_is_Un)
+ apply (simp only: finite_Un)
+ apply blast
+ done
+
+lemma finite_imageI: "finite F ==> finite (h ` F)"
+ -- {* The image of a finite set is finite. *}
+ by (induct set: Finites) simp_all
+
+lemma finite_range_imageI:
+ "finite (range g) ==> finite (range (%x. f (g x)))"
+ apply (drule finite_imageI)
+ apply simp
+ done
+
+lemma finite_empty_induct:
+ "finite A ==>
+ P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
+proof -
+ assume "finite A"
+ and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
+ have "P (A - A)"
+ proof -
+ fix c b :: "'a set"
+ presume c: "finite c" and b: "finite b"
+ and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
+ from c show "c \<subseteq> b ==> P (b - c)"
+ proof induct
+ case empty
+ from P1 show ?case by simp
+ next
+ case (insert F x)
+ have "P (b - F - {x})"
+ proof (rule P2)
+ from _ b show "finite (b - F)" by (rule finite_subset) blast
+ from insert show "x \<in> b - F" by simp
+ from insert show "P (b - F)" by simp
+ qed
+ also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
+ finally show ?case .
+ qed
+ next
+ show "A \<subseteq> A" ..
+ qed
+ thus "P {}" by simp
+qed
+
+lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
+ by (rule Diff_subset [THEN finite_subset])
+
+lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
+ apply (subst Diff_insert)
+ apply (case_tac "a : A - B")
+ apply (rule finite_insert [symmetric, THEN trans])
+ apply (subst insert_Diff)
+ apply simp_all
+ done
+
+
+lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
+proof -
+ have aux: "!!A. finite (A - {}) = finite A" by simp
+ fix B :: "'a set"
+ assume "finite B"
+ thus "!!A. f`A = B ==> inj_on f A ==> finite A"
+ apply induct
+ apply simp
+ apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
+ apply clarify
+ apply (simp (no_asm_use) add: inj_on_def)
+ apply (blast dest!: aux [THEN iffD1])
+ apply atomize
+ apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
+ apply (frule subsetD [OF equalityD2 insertI1])
+ apply clarify
+ apply (rule_tac x = xa in bexI)
+ apply (simp_all add: inj_on_image_set_diff)
+ done
+qed (rule refl)
+
+
+subsubsection {* The finite UNION of finite sets *}
+
+lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
+ by (induct set: Finites) simp_all
+
+text {*
+ Strengthen RHS to
+ @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x ~= {}})"}?
+
+ We'd need to prove
+ @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x ~= {}}"}
+ by induction. *}
+
+lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
+ by (blast intro: finite_UN_I finite_subset)
+
+
+subsubsection {* Sigma of finite sets *}
+
+lemma finite_SigmaI [simp]:
+ "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
+ by (unfold Sigma_def) (blast intro!: finite_UN_I)
+
+lemma finite_Prod_UNIV:
+ "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
+ apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
+ apply (erule ssubst)
+ apply (erule finite_SigmaI)
+ apply auto
+ done
+
+instance unit :: finite
+proof
+ have "finite {()}" by simp
+ also have "{()} = UNIV" by auto
+ finally show "finite (UNIV :: unit set)" .
+qed
+
+instance * :: (finite, finite) finite
+proof
+ show "finite (UNIV :: ('a \<times> 'b) set)"
+ proof (rule finite_Prod_UNIV)
+ show "finite (UNIV :: 'a set)" by (rule finite)
+ show "finite (UNIV :: 'b set)" by (rule finite)
+ qed
+qed
+
+
+subsubsection {* The powerset of a finite set *}
+
+lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
+proof
+ assume "finite (Pow A)"
+ with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
+ thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
+next
+ assume "finite A"
+ thus "finite (Pow A)"
+ by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
+qed
+
+lemma finite_converse [iff]: "finite (r^-1) = finite r"
+ apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
+ apply simp
+ apply (rule iffI)
+ apply (erule finite_imageD [unfolded inj_on_def])
+ apply (simp split add: split_split)
+ apply (erule finite_imageI)
+ apply (simp add: converse_def image_def)
+ apply auto
+ apply (rule bexI)
+ prefer 2 apply assumption
+ apply simp
+ done
+
+lemma finite_lessThan [iff]: (fixes k :: nat) "finite {..k(}"
+ by (induct k) (simp_all add: lessThan_Suc)
+
+lemma finite_atMost [iff]: (fixes k :: nat) "finite {..k}"
+ by (induct k) (simp_all add: atMost_Suc)
+
+lemma bounded_nat_set_is_finite:
+ "(ALL i:N. i < (n::nat)) ==> finite N"
+ -- {* A bounded set of natural numbers is finite. *}
+ apply (rule finite_subset)
+ apply (rule_tac [2] finite_lessThan)
+ apply auto
+ done
+
+
+subsubsection {* Finiteness of transitive closure *}
+
+text {* (Thanks to Sidi Ehmety) *}
+
+lemma finite_Field: "finite r ==> finite (Field r)"
+ -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
+ apply (induct set: Finites)
+ apply (auto simp add: Field_def Domain_insert Range_insert)
+ done
+
+lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
+ apply clarify
+ apply (erule trancl_induct)
+ apply (auto simp add: Field_def)
+ done
+
+lemma finite_trancl: "finite (r^+) = finite r"
+ apply auto
+ prefer 2
+ apply (rule trancl_subset_Field2 [THEN finite_subset])
+ apply (rule finite_SigmaI)
+ prefer 3
+ apply (blast intro: r_into_trancl finite_subset)
+ apply (auto simp add: finite_Field)
+ done
+
+
+subsection {* Finite cardinality *}
+
+text {*
+ This definition, although traditional, is ugly to work with: @{text
+ "card A == LEAST n. EX f. A = {f i | i. i < n}"}. Therefore we have
+ switched to an inductive one:
+*}
+
+consts cardR :: "('a set \<times> nat) set"
+
+inductive cardR
+ intros
+ EmptyI: "({}, 0) : cardR"
+ InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
+
+constdefs
+ card :: "'a set => nat"
+ "card A == THE n. (A, n) : cardR"
+
+inductive_cases cardR_emptyE: "({}, n) : cardR"
+inductive_cases cardR_insertE: "(insert a A,n) : cardR"
+
+lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
+ by (induct set: cardR) simp_all
+
+lemma cardR_determ_aux1:
+ "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
+ apply (induct set: cardR)
+ apply auto
+ apply (simp add: insert_Diff_if)
+ apply auto
+ apply (drule cardR_SucD)
+ apply (blast intro!: cardR.intros)
+ done
+
+lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
+ by (drule cardR_determ_aux1) auto
+
+lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
+ apply (induct set: cardR)
+ apply (safe elim!: cardR_emptyE cardR_insertE)
+ apply (rename_tac B b m)
+ apply (case_tac "a = b")
+ apply (subgoal_tac "A = B")
+ prefer 2 apply (blast elim: equalityE)
+ apply blast
+ apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
+ prefer 2
+ apply (rule_tac x = "A Int B" in exI)
+ apply (blast elim: equalityE)
+ apply (frule_tac A = B in cardR_SucD)
+ apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
+ done
+
+lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
+ by (induct set: cardR) simp_all
+
+lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
+ by (induct set: Finites) (auto intro!: cardR.intros)
+
+lemma card_equality: "(A,n) : cardR ==> card A = n"
+ by (unfold card_def) (blast intro: cardR_determ)
+
+lemma card_empty [simp]: "card {} = 0"
+ by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
+
+lemma card_insert_disjoint [simp]:
+ "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
+proof -
+ assume x: "x \<notin> A"
+ hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
+ apply (auto intro!: cardR.intros)
+ apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
+ apply (force dest: cardR_imp_finite)
+ apply (blast intro!: cardR.intros intro: cardR_determ)
+ done
+ assume "finite A"
+ thus ?thesis
+ apply (simp add: card_def aux)
+ apply (rule the_equality)
+ apply (auto intro: finite_imp_cardR
+ cong: conj_cong simp: card_def [symmetric] card_equality)
+ done
+qed
+
+lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
+ apply auto
+ apply (drule_tac a = x in mk_disjoint_insert)
+ apply clarify
+ apply (rotate_tac -1)
+ apply auto
+ done
+
+lemma card_insert_if:
+ "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
+ by (simp add: insert_absorb)
+
+lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
+ apply (rule_tac t = A in insert_Diff [THEN subst])
+ apply assumption
+ apply simp
+ done
+
+lemma card_Diff_singleton:
+ "finite A ==> x: A ==> card (A - {x}) = card A - 1"
+ by (simp add: card_Suc_Diff1 [symmetric])
+
+lemma card_Diff_singleton_if:
+ "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
+ by (simp add: card_Diff_singleton)
+
+lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
+ by (simp add: card_insert_if card_Suc_Diff1)
+
+lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
+ by (simp add: card_insert_if)
+
+lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
+ apply (induct set: Finites)
+ apply simp
+ apply clarify
+ apply (subgoal_tac "finite A & A - {x} <= F")
+ prefer 2 apply (blast intro: finite_subset)
+ apply atomize
+ apply (drule_tac x = "A - {x}" in spec)
+ apply (simp add: card_Diff_singleton_if split add: split_if_asm)
+ apply (case_tac "card A")
+ apply auto
+ done
+
+lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
+ apply (simp add: psubset_def linorder_not_le [symmetric])
+ apply (blast dest: card_seteq)
+ done
+
+lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
+ apply (case_tac "A = B")
+ apply simp
+ apply (simp add: linorder_not_less [symmetric])
+ apply (blast dest: card_seteq intro: order_less_imp_le)
+ done
+
+lemma card_Un_Int: "finite A ==> finite B
+ ==> card A + card B = card (A Un B) + card (A Int B)"
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: insert_absorb Int_insert_left)
+ done
+
+lemma card_Un_disjoint: "finite A ==> finite B
+ ==> A Int B = {} ==> card (A Un B) = card A + card B"
+ by (simp add: card_Un_Int)
+
+lemma card_Diff_subset:
+ "finite A ==> B <= A ==> card A - card B = card (A - B)"
+ apply (subgoal_tac "(A - B) Un B = A")
+ prefer 2 apply blast
+ apply (rule add_right_cancel [THEN iffD1])
+ apply (rule card_Un_disjoint [THEN subst])
+ apply (erule_tac [4] ssubst)
+ prefer 3 apply blast
+ apply (simp_all add: add_commute not_less_iff_le
+ add_diff_inverse card_mono finite_subset)
+ done
+
+lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
+ apply (rule Suc_less_SucD)
+ apply (simp add: card_Suc_Diff1)
+ done
+
+lemma card_Diff2_less:
+ "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
+ apply (case_tac "x = y")
+ apply (simp add: card_Diff1_less)
+ apply (rule less_trans)
+ prefer 2 apply (auto intro!: card_Diff1_less)
+ done
+
+lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
+ apply (case_tac "x : A")
+ apply (simp_all add: card_Diff1_less less_imp_le)
+ done
+
+lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
+ apply (erule psubsetI)
+ apply blast
+ done
+
+
+subsubsection {* Cardinality of image *}
+
+lemma card_image_le: "finite A ==> card (f ` A) <= card A"
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: le_SucI finite_imageI card_insert_if)
+ done
+
+lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
+ apply (induct set: Finites)
+ apply simp_all
+ apply atomize
+ apply safe
+ apply (unfold inj_on_def)
+ apply blast
+ apply (subst card_insert_disjoint)
+ apply (erule finite_imageI)
+ apply blast
+ apply blast
+ done
+
+lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
+ by (simp add: card_seteq card_image)
+
+
+subsubsection {* Cardinality of the Powerset *}
+
+lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A" (* FIXME numeral 2 (!?) *)
+ apply (induct set: Finites)
+ apply (simp_all add: Pow_insert)
+ apply (subst card_Un_disjoint)
+ apply blast
+ apply (blast intro: finite_imageI)
+ apply blast
+ apply (subgoal_tac "inj_on (insert x) (Pow F)")
+ apply (simp add: card_image Pow_insert)
+ apply (unfold inj_on_def)
+ apply (blast elim!: equalityE)
+ done
+
+text {*
+ \medskip Relates to equivalence classes. Based on a theorem of
+ F. Kammüller's. The @{prop "finite C"} premise is redundant.
+*}
+
+lemma dvd_partition:
+ "finite C ==> finite (Union C) ==>
+ ALL c : C. k dvd card c ==>
+ (ALL c1: C. ALL c2: C. c1 ~= c2 --> c1 Int c2 = {}) ==>
+ k dvd card (Union C)"
+ apply (induct set: Finites)
+ apply simp_all
+ apply clarify
+ apply (subst card_Un_disjoint)
+ apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
+ done
+
+
+subsection {* A fold functional for finite sets *}
+
+text {*
+ For @{text n} non-negative we have @{text "fold f e {x1, ..., xn} =
+ f x1 (... (f xn e))"} where @{text f} is at least left-commutative.
+*}
+
+consts
+ foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
+
+inductive "foldSet f e"
+ intros
+ emptyI [intro]: "({}, e) : foldSet f e"
+ insertI [intro]: "x \<notin> A ==> (A, y) : foldSet f e ==> (insert x A, f x y) : foldSet f e"
+
+inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
+
+constdefs
+ fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
+ "fold f e A == THE x. (A, x) : foldSet f e"
+
+lemma Diff1_foldSet: "(A - {x}, y) : foldSet f e ==> x: A ==> (A, f x y) : foldSet f e"
+ apply (erule insert_Diff [THEN subst], rule foldSet.intros)
+ apply auto
+ done
+
+lemma foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
+ by (induct set: foldSet) auto
+
+lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f e"
+ by (induct set: Finites) auto
+
+
+subsubsection {* Left-commutative operations *}
+
+locale LC =
+ fixes f :: "'b => 'a => 'a" (infixl "\<cdot>" 70)
+ assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
+
+lemma (in LC) foldSet_determ_aux:
+ "ALL A x. card A < n --> (A, x) : foldSet f e -->
+ (ALL y. (A, y) : foldSet f e --> y = x)"
+ apply (induct n)
+ apply (auto simp add: less_Suc_eq)
+ apply (erule foldSet.cases)
+ apply blast
+ apply (erule foldSet.cases)
+ apply blast
+ apply clarify
+ txt {* force simplification of @{text "card A < card (insert ...)"}. *}
+ apply (erule rev_mp)
+ apply (simp add: less_Suc_eq_le)
+ apply (rule impI)
+ apply (rename_tac Aa xa ya Ab xb yb, case_tac "xa = xb")
+ apply (subgoal_tac "Aa = Ab")
+ prefer 2 apply (blast elim!: equalityE)
+ apply blast
+ txt {* case @{prop "xa \<notin> xb"}. *}
+ apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
+ prefer 2 apply (blast elim!: equalityE)
+ apply clarify
+ apply (subgoal_tac "Aa = insert xb Ab - {xa}")
+ prefer 2 apply blast
+ apply (subgoal_tac "card Aa <= card Ab")
+ prefer 2
+ apply (rule Suc_le_mono [THEN subst])
+ apply (simp add: card_Suc_Diff1)
+ apply (rule_tac A1 = "Aa - {xb}" and f1 = f in finite_imp_foldSet [THEN exE])
+ apply (blast intro: foldSet_imp_finite finite_Diff)
+ apply (frule (1) Diff1_foldSet)
+ apply (subgoal_tac "ya = f xb x")
+ prefer 2 apply (blast del: equalityCE)
+ apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
+ prefer 2 apply simp
+ apply (subgoal_tac "yb = f xa x")
+ prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
+ apply (simp (no_asm_simp) add: left_commute)
+ done
+
+lemma (in LC) foldSet_determ: "(A, x) : foldSet f e ==> (A, y) : foldSet f e ==> y = x"
+ by (blast intro: foldSet_determ_aux [rule_format])
+
+lemma (in LC) fold_equality: "(A, y) : foldSet f e ==> fold f e A = y"
+ by (unfold fold_def) (blast intro: foldSet_determ)
+
+lemma fold_empty [simp]: "fold f e {} = e"
+ by (unfold fold_def) blast
+
+lemma (in LC) fold_insert_aux: "x \<notin> A ==>
+ ((insert x A, v) : foldSet f e) =
+ (EX y. (A, y) : foldSet f e & v = f x y)"
+ apply auto
+ apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
+ apply (fastsimp dest: foldSet_imp_finite)
+ apply (blast intro: foldSet_determ)
+ done
+
+lemma (in LC) fold_insert:
+ "finite A ==> x \<notin> A ==> fold f e (insert x A) = f x (fold f e A)"
+ apply (unfold fold_def)
+ apply (simp add: fold_insert_aux)
+ apply (rule the_equality)
+ apply (auto intro: finite_imp_foldSet
+ cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
+ done
+
+lemma (in LC) fold_commute: "finite A ==> (!!e. f x (fold f e A) = fold f (f x e) A)"
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: left_commute fold_insert)
+ done
+
+lemma (in LC) fold_nest_Un_Int:
+ "finite A ==> finite B
+ ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)"
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: fold_insert fold_commute Int_insert_left insert_absorb)
+ done
+
+lemma (in LC) fold_nest_Un_disjoint:
+ "finite A ==> finite B ==> A Int B = {}
+ ==> fold f e (A Un B) = fold f (fold f e B) A"
+ by (simp add: fold_nest_Un_Int)
+
+declare foldSet_imp_finite [simp del]
+ empty_foldSetE [rule del] foldSet.intros [rule del]
+ -- {* Delete rules to do with @{text foldSet} relation. *}
+
+
+
+subsubsection {* Commutative monoids *}
+
+text {*
+ We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
+ instead of @{text "'b => 'a => 'a"}.
+*}
+
+locale ACe =
+ fixes f :: "'a => 'a => 'a" (infixl "\<cdot>" 70)
+ and e :: 'a
+ assumes ident [simp]: "x \<cdot> e = x"
+ and commute: "x \<cdot> y = y \<cdot> x"
+ and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
+
+lemma (in ACe) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
+proof -
+ have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
+ also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
+ also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
+ finally show ?thesis .
+qed
+
+lemma (in ACe)
+ AC: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)" "x \<cdot> y = y \<cdot> x" "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
+ by (rule assoc, rule commute, rule left_commute) (* FIXME localize "lemmas" (!??) *)
+
+lemma (in ACe [simp]) left_ident: "e \<cdot> x = x"
+proof -
+ have "x \<cdot> e = x" by (rule ident)
+ thus ?thesis by (subst commute)
+qed
+
+lemma (in ACe) fold_Un_Int:
+ "finite A ==> finite B ==>
+ fold f e A \<cdot> fold f e B = fold f e (A Un B) \<cdot> fold f e (A Int B)"
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: AC fold_insert insert_absorb Int_insert_left)
+ done
+
+lemma (in ACe) fold_Un_disjoint:
+ "finite A ==> finite B ==> A Int B = {} ==>
+ fold f e (A Un B) = fold f e A \<cdot> fold f e B"
+ by (simp add: fold_Un_Int)
+
+lemma (in ACe) fold_Un_disjoint2:
+ "finite A ==> finite B ==> A Int B = {} ==>
+ fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
+proof -
+ assume b: "finite B"
+ assume "finite A"
+ thus "A Int B = {} ==>
+ fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
+ proof induct
+ case empty
+ thus ?case by simp
+ next
+ case (insert F x)
+ have "fold (f \<circ> g) e (insert x F \<union> B) = fold (f \<circ> g) e (insert x (F \<union> B))"
+ by simp
+ also have "... = (f \<circ> g) x (fold (f \<circ> g) e (F \<union> B))"
+ by (rule fold_insert) (insert b insert, auto simp add: left_commute) (* FIXME import of fold_insert (!?) *)
+ also from insert have "fold (f \<circ> g) e (F \<union> B) =
+ fold (f \<circ> g) e F \<cdot> fold (f \<circ> g) e B" by blast
+ also have "(f \<circ> g) x ... = (f \<circ> g) x (fold (f \<circ> g) e F) \<cdot> fold (f \<circ> g) e B"
+ by (simp add: AC)
+ also have "(f \<circ> g) x (fold (f \<circ> g) e F) = fold (f \<circ> g) e (insert x F)"
+ by (rule fold_insert [symmetric]) (insert b insert, auto simp add: left_commute)
+ finally show ?case .
+ qed
+qed
+
+
+subsection {* Generalized summation over a set *}
+
+constdefs
+ setsum :: "('a => 'b) => 'a set => 'b::plus_ac0"
+ "setsum f A == if finite A then fold (op + o f) 0 A else 0"
+
+syntax
+ "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0" ("\<Sum>_:_. _" [0, 51, 10] 10)
+syntax (xsymbols)
+ "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0" ("\<Sum>_\<in>_. _" [0, 51, 10] 10)
+translations
+ "\<Sum>i:A. b" == "setsum (%i. b) A" -- {* Beware of argument permutation! *}
+
+
+lemma setsum_empty [simp]: "setsum f {} = 0"
+ by (simp add: setsum_def)
+
+lemma setsum_insert [simp]:
+ "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
+ by (simp add: setsum_def fold_insert plus_ac0_left_commute)
+
+lemma setsum_0: "setsum (\<lambda>i. 0) A = 0"
+ apply (case_tac "finite A")
+ prefer 2 apply (simp add: setsum_def)
+ apply (erule finite_induct)
+ apply auto
+ done
+
+lemma setsum_eq_0_iff [simp]:
+ "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
+ by (induct set: Finites) auto
+
+lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
+ apply (case_tac "finite A")
+ prefer 2 apply (simp add: setsum_def)
+ apply (erule rev_mp)
+ apply (erule finite_induct)
+ apply auto
+ done
+
+lemma card_eq_setsum: "finite A ==> card A = setsum (\<lambda>x. 1) A"
+ -- {* Could allow many @{text "card"} proofs to be simplified. *}
+ by (induct set: Finites) auto
+
+lemma setsum_Un_Int: "finite A ==> finite B
+ ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
+ -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
+ apply (induct set: Finites)
+ apply simp
+ apply (simp add: plus_ac0 Int_insert_left insert_absorb)
+ done
+
+lemma setsum_Un_disjoint: "finite A ==> finite B
+ ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
+ apply (subst setsum_Un_Int [symmetric])
+ apply auto
+ done
+
+lemma setsum_UN_disjoint: (fixes f :: "'a => 'b::plus_ac0")
+ "finite I ==> (ALL i:I. finite (A i)) ==>
+ (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
+ setsum f (UNION I A) = setsum (\<lambda>i. setsum f (A i)) I"
+ apply (induct set: Finites)
+ apply simp
+ apply atomize
+ apply (subgoal_tac "ALL i:F. x \<noteq> i")
+ prefer 2 apply blast
+ apply (subgoal_tac "A x Int UNION F A = {}")
+ prefer 2 apply blast
+ apply (simp add: setsum_Un_disjoint)
+ done
+
+lemma setsum_addf: "setsum (\<lambda>x. f x + g x) A = (setsum f A + setsum g A)"
+ apply (case_tac "finite A")
+ prefer 2 apply (simp add: setsum_def)
+ apply (erule finite_induct)
+ apply auto
+ apply (simp add: plus_ac0)
+ done
+
+lemma setsum_Un: "finite A ==> finite B ==>
+ (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
+ -- {* For the natural numbers, we have subtraction. *}
+ apply (subst setsum_Un_Int [symmetric])
+ apply auto
+ done
+
+lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
+ (if a:A then setsum f A - f a else setsum f A)"
+ apply (case_tac "finite A")
+ prefer 2 apply (simp add: setsum_def)
+ apply (erule finite_induct)
+ apply (auto simp add: insert_Diff_if)
+ apply (drule_tac a = a in mk_disjoint_insert)
+ apply auto
+ done
+
+lemma setsum_cong:
+ "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
+ apply (case_tac "finite B")
+ prefer 2 apply (simp add: setsum_def)
+ apply simp
+ apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C")
+ apply simp
+ apply (erule finite_induct)
+ apply simp
+ apply (simp add: subset_insert_iff)
+ apply clarify
+ apply (subgoal_tac "finite C")
+ prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
+ apply (subgoal_tac "C = insert x (C - {x})")
+ prefer 2 apply blast
+ apply (erule ssubst)
+ apply (drule spec)
+ apply (erule (1) notE impE)
+ apply (simp add: Ball_def)
+ done
+
+
+text {*
+ \medskip Basic theorem about @{text "choose"}. By Florian
+ Kammüller, tidied by LCP.
+*}
+
+lemma card_s_0_eq_empty:
+ "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
+ apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
+ apply (simp cong add: rev_conj_cong)
+ done
+
+lemma choose_deconstruct: "finite M ==> x \<notin> M
+ ==> {s. s <= insert x M & card(s) = Suc k}
+ = {s. s <= M & card(s) = Suc k} Un
+ {s. EX t. t <= M & card(t) = k & s = insert x t}"
+ apply safe
+ apply (auto intro: finite_subset [THEN card_insert_disjoint])
+ apply (drule_tac x = "xa - {x}" in spec)
+ apply (subgoal_tac "x ~: xa")
+ apply auto
+ apply (erule rev_mp, subst card_Diff_singleton)
+ apply (auto intro: finite_subset)
+ done
+
+lemma card_inj_on_le:
+ "finite A ==> finite B ==> f ` A \<subseteq> B ==> inj_on f A ==> card A <= card B"
+ by (auto intro: card_mono simp add: card_image [symmetric])
+
+lemma card_bij_eq: "finite A ==> finite B ==>
+ f ` A \<subseteq> B ==> inj_on f A ==> g ` B \<subseteq> A ==> inj_on g B ==> card A = card B"
+ by (auto intro: le_anti_sym card_inj_on_le)
+
+lemma constr_bij: "finite A ==> x \<notin> A ==>
+ card {B. EX C. C <= A & card(C) = k & B = insert x C} =
+ card {B. B <= A & card(B) = k}"
+ apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
+ apply (rule_tac B = "Pow (insert x A) " in finite_subset)
+ apply (rule_tac [3] B = "Pow (A) " in finite_subset)
+ apply fast+
+ txt {* arity *}
+ apply (auto elim!: equalityE simp add: inj_on_def)
+ apply (subst Diff_insert0)
+ apply auto
+ done
+
+text {*
+ Main theorem: combinatorial statement about number of subsets of a set.
+*}
+
+lemma n_sub_lemma:
+ "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
+ apply (induct k)
+ apply (simp add: card_s_0_eq_empty)
+ apply atomize
+ apply (rotate_tac -1, erule finite_induct)
+ apply (simp_all (no_asm_simp) cong add: conj_cong add: card_s_0_eq_empty choose_deconstruct)
+ apply (subst card_Un_disjoint)
+ prefer 4 apply (force simp add: constr_bij)
+ prefer 3 apply force
+ prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
+ finite_subset [of _ "Pow (insert x F)", standard])
+ apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
+ done
+
+theorem n_subsets: "finite A ==> card {B. B <= A & card(B) = k} = (card A choose k)"
+ by (simp add: n_sub_lemma)
+
+end
--- a/src/HOL/IsaMakefile Thu Dec 06 00:37:59 2001 +0100
+++ b/src/HOL/IsaMakefile Thu Dec 06 00:38:55 2001 +0100
@@ -78,7 +78,7 @@
$(SRC)/TFL/rules.ML $(SRC)/TFL/tfl.ML $(SRC)/TFL/thms.ML $(SRC)/TFL/thry.ML \
$(SRC)/TFL/usyntax.ML $(SRC)/TFL/utils.ML \
Datatype.thy Datatype_Universe.ML Datatype_Universe.thy Divides.ML \
- Divides.thy Finite.ML Finite.thy Fun.ML Fun.thy Gfp.ML Gfp.thy \
+ Divides.thy Finite_Set.ML Finite_Set.thy Fun.ML Fun.thy Gfp.ML Gfp.thy \
Hilbert_Choice.thy Hilbert_Choice_lemmas.ML HOL.ML \
HOL.thy HOL_lemmas.ML Inductive.thy Integ/Bin.ML Integ/Bin.thy \
Integ/Equiv.ML Integ/Equiv.thy Integ/Int.ML Integ/Int.thy \